Skip to main content

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::{Object, translate::*};
6
7use crate::{Builder, ffi, prelude::*};
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    /// s possible that memory
77    /// was leaked leading up to the reported failure. The only reasonable
78    /// thing to do when an error is detected is to call `g_error()`.
79    /// ## `filename`
80    /// the name of the file to parse
81    ///
82    /// # Returns
83    ///
84    /// [`true`] on success, [`false`] if an error occurred
85    #[doc(alias = "gtk_builder_add_from_file")]
86    pub fn add_from_file(&self, file_path: impl AsRef<Path>) -> Result<(), glib::Error> {
87        unsafe {
88            let mut error = ::std::ptr::null_mut();
89            ffi::gtk_builder_add_from_file(
90                self.to_glib_none().0,
91                file_path.as_ref().to_glib_none().0,
92                &mut error,
93            );
94            if error.is_null() {
95                Ok(())
96            } else {
97                Err(from_glib_full(error))
98            }
99        }
100    }
101}