gtk4/
builder.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::path::Path;
4
5use glib::{translate::*, Object};
6
7use crate::{ffi, prelude::*, Builder};
8
9impl Builder {
10    /// Parses the UI definition in the file @filename.
11    ///
12    /// If there is an error opening the file or parsing the description then
13    /// the program will be aborted. You should only ever attempt to parse
14    /// user interface descriptions that are shipped as part of your program.
15    /// ## `filename`
16    /// filename of user interface description file
17    ///
18    /// # Returns
19    ///
20    /// a [`Builder`][crate::Builder] containing the described interface
21    #[doc(alias = "gtk_builder_new_from_file")]
22    #[doc(alias = "new_from_file")]
23    pub fn from_file(file_path: impl AsRef<Path>) -> Self {
24        assert_initialized_main_thread!();
25        unsafe {
26            from_glib_full(ffi::gtk_builder_new_from_file(
27                file_path.as_ref().to_glib_none().0,
28            ))
29        }
30    }
31
32    /// Gets the current object set via gtk_builder_set_current_object().
33    ///
34    /// # Returns
35    ///
36    /// the current object
37    #[doc(alias = "gtk_builder_get_current_object")]
38    #[doc(alias = "get_current_object")]
39    pub fn current_object(&self) -> Option<Object> {
40        unsafe {
41            let ptr = ffi::gtk_builder_get_current_object(self.to_glib_none().0);
42            if ptr.is_null() {
43                None
44            } else {
45                glib::gobject_ffi::g_object_ref(ptr);
46                Some(from_glib_full(ptr))
47            }
48        }
49    }
50
51    /// Gets the object named @name.
52    ///
53    /// Note that this function does not increment the reference count
54    /// of the returned object.
55    /// ## `name`
56    /// name of object to get
57    ///
58    /// # Returns
59    ///
60    /// the object named @name
61    #[doc(alias = "gtk_builder_get_object")]
62    #[doc(alias = "get_object")]
63    pub fn object<T: IsA<Object>>(&self, name: impl IntoGStr) -> Option<T> {
64        unsafe {
65            T::ensure_type();
66            name.run_with_gstr(|name| {
67                Option::<Object>::from_glib_none(ffi::gtk_builder_get_object(
68                    self.to_glib_none().0,
69                    name.as_ptr(),
70                ))
71                .and_then(|obj| obj.dynamic_cast::<T>().ok())
72            })
73        }
74    }
75
76    /// Parses a file containing a UI definition and merges it with
77    /// the current contents of @self.
78    ///
79    /// This function is useful if you need to call
80    /// [`set_current_object()`][Self::set_current_object()]) to add user data to
81    /// callbacks before loading GtkBuilder UI. Otherwise, you probably
82    /// want [`from_file()`][Self::from_file()] instead.
83    ///
84    /// If an error occurs, 0 will be returned and @error will be assigned a
85    /// `GError` from the `GTK_BUILDER_ERROR`, `G_MARKUP_ERROR` or `G_FILE_ERROR`
86    /// domains.
87    ///
88    /// It’s not really reasonable to attempt to handle failures of this
89    /// call. You should not use this function with untrusted files (ie:
90    /// files that are not part of your application). Broken [`Builder`][crate::Builder]
91    /// files can easily crash your program, and it’s possible that memory
92    /// was leaked leading up to the reported failure. The only reasonable
93    /// thing to do when an error is detected is to call `g_error()`.
94    /// ## `filename`
95    /// the name of the file to parse
96    ///
97    /// # Returns
98    ///
99    /// [`true`] on success, [`false`] if an error occurred
100    #[doc(alias = "gtk_builder_add_from_file")]
101    pub fn add_from_file(&self, file_path: impl AsRef<Path>) -> Result<(), glib::Error> {
102        unsafe {
103            let mut error = ::std::ptr::null_mut();
104            ffi::gtk_builder_add_from_file(
105                self.to_glib_none().0,
106                file_path.as_ref().to_glib_none().0,
107                &mut error,
108            );
109            if error.is_null() {
110                Ok(())
111            } else {
112                Err(from_glib_full(error))
113            }
114        }
115    }
116}