Skip to main content

gtk/auto/
builder.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{Application, ffi};
6use glib::{
7    prelude::*,
8    signal::{SignalHandlerId, connect_raw},
9    translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14    /// ` tag has been added
15    /// to the format allowing one to define a widget class’s components.
16    /// See the [GtkWidget documentation][composite-templates] for details.
17    ///
18    /// ## Properties
19    ///
20    ///
21    /// #### `translation-domain`
22    ///  The translation domain used when translating property values that
23    /// have been marked as translatable in interface descriptions.
24    /// If the translation domain is [`None`], [`Builder`][crate::Builder] uses `gettext()`,
25    /// otherwise `g_dgettext()`.
26    ///
27    /// Readable | Writable
28    ///
29    /// # Implements
30    ///
31    /// [`BuilderExt`][trait@crate::prelude::BuilderExt], [`trait@glib::ObjectExt`], [`BuilderExtManual`][trait@crate::prelude::BuilderExtManual]
32    #[doc(alias = "GtkBuilder")]
33    pub struct Builder(Object<ffi::GtkBuilder, ffi::GtkBuilderClass>);
34
35    match fn {
36        type_ => || ffi::gtk_builder_get_type(),
37    }
38}
39
40impl Builder {
41    pub const NONE: Option<&'static Builder> = None;
42
43    /// Creates a new empty builder object.
44    ///
45    /// This function is only useful if you intend to make multiple calls
46    /// to `gtk_builder_add_from_file()`, [`BuilderExtManual::add_from_resource()`][crate::prelude::BuilderExtManual::add_from_resource()]
47    /// or [`BuilderExtManual::add_from_string()`][crate::prelude::BuilderExtManual::add_from_string()] in order to merge multiple UI
48    /// descriptions into a single builder.
49    ///
50    /// Most users will probably want to use `gtk_builder_new_from_file()`,
51    /// [`from_resource()`][Self::from_resource()] or [`from_string()`][Self::from_string()].
52    ///
53    /// # Returns
54    ///
55    /// a new (empty) [`Builder`][crate::Builder] object
56    #[doc(alias = "gtk_builder_new")]
57    pub fn new() -> Builder {
58        assert_initialized_main_thread!();
59        unsafe { from_glib_full(ffi::gtk_builder_new()) }
60    }
61
62    /// Builds the [GtkBuilder UI definition][BUILDER-UI]
63    /// at `resource_path`.
64    ///
65    /// If there is an error locating the resource or parsing the
66    /// description, then the program will be aborted.
67    /// ## `resource_path`
68    /// a `GResource` resource path
69    ///
70    /// # Returns
71    ///
72    /// a [`Builder`][crate::Builder] containing the described interface
73    #[doc(alias = "gtk_builder_new_from_resource")]
74    #[doc(alias = "new_from_resource")]
75    pub fn from_resource(resource_path: &str) -> Builder {
76        assert_initialized_main_thread!();
77        unsafe {
78            from_glib_full(ffi::gtk_builder_new_from_resource(
79                resource_path.to_glib_none().0,
80            ))
81        }
82    }
83
84    /// Builds the user interface described by `string` (in the
85    /// [GtkBuilder UI definition][BUILDER-UI] format).
86    ///
87    /// If `string` is [`None`]-terminated, then `length` should be -1.
88    /// If `length` is not -1, then it is the length of `string`.
89    ///
90    /// If there is an error parsing `string` then the program will be
91    /// aborted. You should not attempt to parse user interface description
92    /// from untrusted sources.
93    /// ## `string`
94    /// a user interface (XML) description
95    /// ## `length`
96    /// the length of `string`, or -1
97    ///
98    /// # Returns
99    ///
100    /// a [`Builder`][crate::Builder] containing the interface described by `string`
101    #[doc(alias = "gtk_builder_new_from_string")]
102    #[doc(alias = "new_from_string")]
103    pub fn from_string(string: &str) -> Builder {
104        assert_initialized_main_thread!();
105        let length = string.len() as _;
106        unsafe {
107            from_glib_full(ffi::gtk_builder_new_from_string(
108                string.to_glib_none().0,
109                length,
110            ))
111        }
112    }
113}
114
115impl Default for Builder {
116    fn default() -> Self {
117        Self::new()
118    }
119}
120
121/// Trait containing all [`struct@Builder`] methods.
122///
123/// # Implementors
124///
125/// [`Builder`][struct@crate::Builder]
126pub trait BuilderExt: IsA<Builder> + 'static {
127    //#[doc(alias = "gtk_builder_add_callback_symbol")]
128    //fn add_callback_symbol<P: FnOnce() + 'static>(&self, callback_name: &str, callback_symbol: P) {
129    //    unsafe { TODO: call ffi:gtk_builder_add_callback_symbol() }
130    //}
131
132    //#[doc(alias = "gtk_builder_add_callback_symbols")]
133    //fn add_callback_symbols<P: FnOnce() + 'static>(&self, first_callback_name: &str, first_callback_symbol: P, : /*Unknown conversion*//*Unimplemented*/Basic: VarArgs) {
134    //    unsafe { TODO: call ffi:gtk_builder_add_callback_symbols() }
135    //}
136
137    //#[doc(alias = "gtk_builder_connect_signals")]
138    //fn connect_signals(&self, user_data: /*Unimplemented*/Option<Basic: Pointer>) {
139    //    unsafe { TODO: call ffi:gtk_builder_connect_signals() }
140    //}
141
142    /// Add `object` to the `self` object pool so it can be referenced just like any
143    /// other object built by builder.
144    /// ## `name`
145    /// the name of the object exposed to the builder
146    /// ## `object`
147    /// the object to expose
148    #[doc(alias = "gtk_builder_expose_object")]
149    fn expose_object(&self, name: &str, object: &impl IsA<glib::Object>) {
150        unsafe {
151            ffi::gtk_builder_expose_object(
152                self.as_ref().to_glib_none().0,
153                name.to_glib_none().0,
154                object.as_ref().to_glib_none().0,
155            );
156        }
157    }
158
159    /// Gets the [`Application`][crate::Application] associated with the builder.
160    ///
161    /// The [`Application`][crate::Application] is used for creating action proxies as requested
162    /// from XML that the builder is loading.
163    ///
164    /// By default, the builder uses the default application: the one from
165    /// [`gio::Application::default()`][crate::gio::Application::default()]. If you want to use another application
166    /// for constructing proxies, use [`set_application()`][Self::set_application()].
167    ///
168    /// # Returns
169    ///
170    /// the application being used by the builder,
171    ///  or [`None`]
172    #[doc(alias = "gtk_builder_get_application")]
173    #[doc(alias = "get_application")]
174    fn application(&self) -> Option<Application> {
175        unsafe {
176            from_glib_none(ffi::gtk_builder_get_application(
177                self.as_ref().to_glib_none().0,
178            ))
179        }
180    }
181
182    /// Gets all objects that have been constructed by `self`. Note that
183    /// this function does not increment the reference counts of the returned
184    /// objects.
185    ///
186    /// # Returns
187    ///
188    /// a newly-allocated `GSList` containing all the objects
189    ///  constructed by the [`Builder`][crate::Builder] instance. It should be freed by
190    ///  `g_slist_free()`
191    #[doc(alias = "gtk_builder_get_objects")]
192    #[doc(alias = "get_objects")]
193    fn objects(&self) -> Vec<glib::Object> {
194        unsafe {
195            FromGlibPtrContainer::from_glib_container(ffi::gtk_builder_get_objects(
196                self.as_ref().to_glib_none().0,
197            ))
198        }
199    }
200
201    /// Gets the translation domain of `self`.
202    ///
203    /// # Returns
204    ///
205    /// the translation domain. This string is owned
206    /// by the builder object and must not be modified or freed.
207    #[doc(alias = "gtk_builder_get_translation_domain")]
208    #[doc(alias = "get_translation_domain")]
209    #[doc(alias = "translation-domain")]
210    fn translation_domain(&self) -> Option<glib::GString> {
211        unsafe {
212            from_glib_none(ffi::gtk_builder_get_translation_domain(
213                self.as_ref().to_glib_none().0,
214            ))
215        }
216    }
217
218    /// Looks up a type by name, using the virtual function that
219    /// [`Builder`][crate::Builder] has for that purpose. This is mainly used when
220    /// implementing the [`Buildable`][crate::Buildable] interface on a type.
221    /// ## `type_name`
222    /// type name to lookup
223    ///
224    /// # Returns
225    ///
226    /// the `GType` found for `type_name` or `G_TYPE_INVALID`
227    ///  if no type was found
228    #[doc(alias = "gtk_builder_get_type_from_name")]
229    #[doc(alias = "get_type_from_name")]
230    fn type_from_name(&self, type_name: &str) -> glib::types::Type {
231        unsafe {
232            from_glib(ffi::gtk_builder_get_type_from_name(
233                self.as_ref().to_glib_none().0,
234                type_name.to_glib_none().0,
235            ))
236        }
237    }
238
239    //#[doc(alias = "gtk_builder_lookup_callback_symbol")]
240    //fn lookup_callback_symbol(&self, callback_name: &str) -> Option<Box_<dyn Fn() + 'static>> {
241    //    unsafe { TODO: call ffi:gtk_builder_lookup_callback_symbol() }
242    //}
243
244    /// Sets the application associated with `self`.
245    ///
246    /// You only need this function if there is more than one [`gio::Application`][crate::gio::Application]
247    /// in your process. `application` cannot be [`None`].
248    /// ## `application`
249    /// a [`Application`][crate::Application]
250    #[doc(alias = "gtk_builder_set_application")]
251    fn set_application(&self, application: &impl IsA<Application>) {
252        unsafe {
253            ffi::gtk_builder_set_application(
254                self.as_ref().to_glib_none().0,
255                application.as_ref().to_glib_none().0,
256            );
257        }
258    }
259
260    /// Sets the translation domain of `self`.
261    /// See [`translation-domain`][struct@crate::Builder#translation-domain].
262    /// ## `domain`
263    /// the translation domain or [`None`]
264    #[doc(alias = "gtk_builder_set_translation_domain")]
265    #[doc(alias = "translation-domain")]
266    fn set_translation_domain(&self, domain: Option<&str>) {
267        unsafe {
268            ffi::gtk_builder_set_translation_domain(
269                self.as_ref().to_glib_none().0,
270                domain.to_glib_none().0,
271            );
272        }
273    }
274
275    /// This function demarshals a value from a string. This function
276    /// calls [`glib::Value::init()`][crate::glib::Value::init()] on the `value` argument, so it need not be
277    /// initialised beforehand.
278    ///
279    /// This function can handle char, uchar, boolean, int, uint, long,
280    /// ulong, enum, flags, float, double, string, `GdkColor`, [`gdk::RGBA`][crate::gdk::RGBA] and
281    /// [`Adjustment`][crate::Adjustment] type values. Support for [`Widget`][crate::Widget] type values is
282    /// still to come.
283    ///
284    /// Upon errors [`false`] will be returned and `error` will be assigned a
285    /// [`glib::Error`][crate::glib::Error] from the `GTK_BUILDER_ERROR` domain.
286    /// ## `pspec`
287    /// the [`glib::ParamSpec`][crate::glib::ParamSpec] for the property
288    /// ## `string`
289    /// the string representation of the value
290    ///
291    /// # Returns
292    ///
293    /// [`true`] on success
294    ///
295    /// ## `value`
296    /// the [`glib::Value`][crate::glib::Value] to store the result in
297    #[doc(alias = "gtk_builder_value_from_string")]
298    fn value_from_string(
299        &self,
300        pspec: impl AsRef<glib::ParamSpec>,
301        string: &str,
302    ) -> Result<glib::Value, glib::Error> {
303        unsafe {
304            let mut value = glib::Value::uninitialized();
305            let mut error = std::ptr::null_mut();
306            let is_ok = ffi::gtk_builder_value_from_string(
307                self.as_ref().to_glib_none().0,
308                pspec.as_ref().to_glib_none().0,
309                string.to_glib_none().0,
310                value.to_glib_none_mut().0,
311                &mut error,
312            );
313            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
314            if error.is_null() {
315                Ok(value)
316            } else {
317                Err(from_glib_full(error))
318            }
319        }
320    }
321
322    /// Like [`value_from_string()`][Self::value_from_string()], this function demarshals
323    /// a value from a string, but takes a `GType` instead of [`glib::ParamSpec`][crate::glib::ParamSpec].
324    /// This function calls [`glib::Value::init()`][crate::glib::Value::init()] on the `value` argument, so it
325    /// need not be initialised beforehand.
326    ///
327    /// Upon errors [`false`] will be returned and `error` will be assigned a
328    /// [`glib::Error`][crate::glib::Error] from the `GTK_BUILDER_ERROR` domain.
329    /// ## `type_`
330    /// the `GType` of the value
331    /// ## `string`
332    /// the string representation of the value
333    ///
334    /// # Returns
335    ///
336    /// [`true`] on success
337    ///
338    /// ## `value`
339    /// the [`glib::Value`][crate::glib::Value] to store the result in
340    #[doc(alias = "gtk_builder_value_from_string_type")]
341    fn value_from_string_type(
342        &self,
343        type_: glib::types::Type,
344        string: &str,
345    ) -> Result<glib::Value, glib::Error> {
346        unsafe {
347            let mut value = glib::Value::uninitialized();
348            let mut error = std::ptr::null_mut();
349            let is_ok = ffi::gtk_builder_value_from_string_type(
350                self.as_ref().to_glib_none().0,
351                type_.into_glib(),
352                string.to_glib_none().0,
353                value.to_glib_none_mut().0,
354                &mut error,
355            );
356            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
357            if error.is_null() {
358                Ok(value)
359            } else {
360                Err(from_glib_full(error))
361            }
362        }
363    }
364
365    #[doc(alias = "translation-domain")]
366    fn connect_translation_domain_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
367        unsafe extern "C" fn notify_translation_domain_trampoline<
368            P: IsA<Builder>,
369            F: Fn(&P) + 'static,
370        >(
371            this: *mut ffi::GtkBuilder,
372            _param_spec: glib::ffi::gpointer,
373            f: glib::ffi::gpointer,
374        ) {
375            unsafe {
376                let f: &F = &*(f as *const F);
377                f(Builder::from_glib_borrow(this).unsafe_cast_ref())
378            }
379        }
380        unsafe {
381            let f: Box_<F> = Box_::new(f);
382            connect_raw(
383                self.as_ptr() as *mut _,
384                c"notify::translation-domain".as_ptr(),
385                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
386                    notify_translation_domain_trampoline::<Self, F> as *const (),
387                )),
388                Box_::into_raw(f),
389            )
390        }
391    }
392}
393
394impl<O: IsA<Builder>> BuilderExt for O {}