#[cfg(any(feature = "v3_10", feature = "dox"))]
use Application;
use Error;
use Widget;
use ffi;
use glib;
use glib::object::Downcast;
use glib::object::IsA;
use glib::signal::SignalHandlerId;
use glib::signal::connect;
use glib::translate::*;
use glib_ffi;
use gobject_ffi;
use std::boxed::Box as Box_;
use std::mem;
use std::mem::transmute;
use std::ptr;
glib_wrapper! {
pub struct Builder(Object<ffi::GtkBuilder, ffi::GtkBuilderClass>);
match fn {
get_type => || ffi::gtk_builder_get_type(),
}
}
impl Builder {
pub fn new() -> Builder {
assert_initialized_main_thread!();
unsafe {
from_glib_full(ffi::gtk_builder_new())
}
}
#[cfg(any(feature = "v3_10", feature = "dox"))]
pub fn new_from_resource(resource_path: &str) -> Builder {
assert_initialized_main_thread!();
unsafe {
from_glib_full(ffi::gtk_builder_new_from_resource(resource_path.to_glib_none().0))
}
}
#[cfg(any(feature = "v3_10", feature = "dox"))]
pub fn new_from_string(string: &str) -> Builder {
assert_initialized_main_thread!();
let length = string.len() as isize;
unsafe {
from_glib_full(ffi::gtk_builder_new_from_string(string.to_glib_none().0, length))
}
}
}
impl Default for Builder {
fn default() -> Self {
Self::new()
}
}
pub trait BuilderExt {
fn add_from_resource(&self, resource_path: &str) -> Result<(), Error>;
fn add_from_string(&self, buffer: &str) -> Result<(), Error>;
fn add_objects_from_resource(&self, resource_path: &str, object_ids: &[&str]) -> Result<(), Error>;
fn add_objects_from_string(&self, buffer: &str, object_ids: &[&str]) -> Result<(), Error>;
#[cfg(any(feature = "v3_8", feature = "dox"))]
fn expose_object<P: IsA<glib::Object>>(&self, name: &str, object: &P);
fn extend_with_template<P: IsA<Widget>>(&self, widget: &P, template_type: glib::types::Type, buffer: &str) -> Result<(), Error>;
#[cfg(any(feature = "v3_10", feature = "dox"))]
fn get_application(&self) -> Option<Application>;
fn get_objects(&self) -> Vec<glib::Object>;
fn get_translation_domain(&self) -> Option<String>;
fn get_type_from_name(&self, type_name: &str) -> glib::types::Type;
#[cfg(any(feature = "v3_10", feature = "dox"))]
fn set_application(&self, application: &Application);
fn set_translation_domain<'a, P: Into<Option<&'a str>>>(&self, domain: P);
fn value_from_string_type(&self, type_: glib::types::Type, string: &str) -> Result<glib::Value, Error>;
fn connect_property_translation_domain_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}
impl<O: IsA<Builder> + IsA<glib::object::Object>> BuilderExt for O {
fn add_from_resource(&self, resource_path: &str) -> Result<(), Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::gtk_builder_add_from_resource(self.to_glib_none().0, resource_path.to_glib_none().0, &mut error);
if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) }
}
}
fn add_from_string(&self, buffer: &str) -> Result<(), Error> {
let length = buffer.len() as usize;
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::gtk_builder_add_from_string(self.to_glib_none().0, buffer.to_glib_none().0, length, &mut error);
if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) }
}
}
fn add_objects_from_resource(&self, resource_path: &str, object_ids: &[&str]) -> Result<(), Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::gtk_builder_add_objects_from_resource(self.to_glib_none().0, resource_path.to_glib_none().0, object_ids.to_glib_none().0, &mut error);
if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) }
}
}
fn add_objects_from_string(&self, buffer: &str, object_ids: &[&str]) -> Result<(), Error> {
let length = buffer.len() as usize;
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::gtk_builder_add_objects_from_string(self.to_glib_none().0, buffer.to_glib_none().0, length, object_ids.to_glib_none().0, &mut error);
if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) }
}
}
#[cfg(any(feature = "v3_8", feature = "dox"))]
fn expose_object<P: IsA<glib::Object>>(&self, name: &str, object: &P) {
unsafe {
ffi::gtk_builder_expose_object(self.to_glib_none().0, name.to_glib_none().0, object.to_glib_none().0);
}
}
fn extend_with_template<P: IsA<Widget>>(&self, widget: &P, template_type: glib::types::Type, buffer: &str) -> Result<(), Error> {
let length = buffer.len() as usize;
unsafe {
let mut error = ptr::null_mut();
let _ = ffi::gtk_builder_extend_with_template(self.to_glib_none().0, widget.to_glib_none().0, template_type.to_glib(), buffer.to_glib_none().0, length, &mut error);
if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) }
}
}
#[cfg(any(feature = "v3_10", feature = "dox"))]
fn get_application(&self) -> Option<Application> {
unsafe {
from_glib_none(ffi::gtk_builder_get_application(self.to_glib_none().0))
}
}
fn get_objects(&self) -> Vec<glib::Object> {
unsafe {
FromGlibPtrContainer::from_glib_container(ffi::gtk_builder_get_objects(self.to_glib_none().0))
}
}
fn get_translation_domain(&self) -> Option<String> {
unsafe {
from_glib_none(ffi::gtk_builder_get_translation_domain(self.to_glib_none().0))
}
}
fn get_type_from_name(&self, type_name: &str) -> glib::types::Type {
unsafe {
from_glib(ffi::gtk_builder_get_type_from_name(self.to_glib_none().0, type_name.to_glib_none().0))
}
}
#[cfg(any(feature = "v3_10", feature = "dox"))]
fn set_application(&self, application: &Application) {
unsafe {
ffi::gtk_builder_set_application(self.to_glib_none().0, application.to_glib_none().0);
}
}
fn set_translation_domain<'a, P: Into<Option<&'a str>>>(&self, domain: P) {
let domain = domain.into();
let domain = domain.to_glib_none();
unsafe {
ffi::gtk_builder_set_translation_domain(self.to_glib_none().0, domain.0);
}
}
fn value_from_string_type(&self, type_: glib::types::Type, string: &str) -> Result<glib::Value, Error> {
unsafe {
let mut value = glib::Value::uninitialized();
let mut error = ptr::null_mut();
let _ = ffi::gtk_builder_value_from_string_type(self.to_glib_none().0, type_.to_glib(), string.to_glib_none().0, value.to_glib_none_mut().0, &mut error);
if error.is_null() { Ok(value) } else { Err(from_glib_full(error)) }
}
}
fn connect_property_translation_domain_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe {
let f: Box_<Box_<Fn(&Self) + 'static>> = Box_::new(Box_::new(f));
connect(self.to_glib_none().0, "notify::translation-domain",
transmute(notify_translation_domain_trampoline::<Self> as usize), Box_::into_raw(f) as *mut _)
}
}
}
unsafe extern "C" fn notify_translation_domain_trampoline<P>(this: *mut ffi::GtkBuilder, _param_spec: glib_ffi::gpointer, f: glib_ffi::gpointer)
where P: IsA<Builder> {
callback_guard!();
let f: &&(Fn(&P) + 'static) = transmute(f);
f(&Builder::from_glib_borrow(this).downcast_unchecked())
}