Skip to main content

gtk/subclass/
container.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::*;
4
5use glib::subclass::prelude::*;
6
7use glib::object::Cast;
8
9use super::widget::WidgetImpl;
10use crate::Widget;
11use crate::WidgetPath;
12use crate::{Container, ffi};
13
14pub trait ContainerImpl: ContainerImplExt + WidgetImpl {
15    /// Adds `widget` to `container`. Typically used for simple containers
16    /// such as [`Window`][crate::Window], [`Frame`][crate::Frame], or [`Button`][crate::Button]; for more complicated
17    /// layout containers such as [`Box`][crate::Box] or [`Grid`][crate::Grid], this function will
18    /// pick default packing parameters that may not be correct. So
19    /// consider functions such as [`BoxExt::pack_start()`][crate::prelude::BoxExt::pack_start()] and
20    /// [`GridExt::attach()`][crate::prelude::GridExt::attach()] as an alternative to [`ContainerExt::add()`][crate::prelude::ContainerExt::add()] in
21    /// those cases. A widget may be added to only one container at a time;
22    /// you can’t place the same widget inside two different containers.
23    ///
24    /// Note that some containers, such as [`ScrolledWindow`][crate::ScrolledWindow] or [`ListBox`][crate::ListBox],
25    /// may add intermediate children between the added widget and the
26    /// container.
27    /// ## `widget`
28    /// a widget to be placed inside `container`
29    fn add(&self, widget: &Widget) {
30        self.parent_add(widget)
31    }
32
33    /// Removes `widget` from `container`. `widget` must be inside `container`.
34    /// Note that `container` will own a reference to `widget`, and that this
35    /// may be the last reference held; so removing a widget from its
36    /// container can destroy that widget. If you want to use `widget`
37    /// again, you need to add a reference to it before removing it from
38    /// a container, using `g_object_ref()`. If you don’t want to use `widget`
39    /// again it’s usually more efficient to simply destroy it directly
40    /// using `gtk_widget_destroy()` since this will remove it from the
41    /// container and help break any circular reference count cycles.
42    /// ## `widget`
43    /// a current child of `container`
44    fn remove(&self, widget: &Widget) {
45        self.parent_remove(widget)
46    }
47
48    /// Signal emitted when a size recalculation is needed.
49    fn check_resize(&self) {
50        self.parent_check_resize()
51    }
52
53    /// Sets, or unsets if `child` is [`None`], the focused child of `container`.
54    ///
55    /// This function emits the GtkContainer::set_focus_child signal of
56    /// `container`. Implementations of [`Container`][crate::Container] can override the
57    /// default behaviour by overriding the class closure of this signal.
58    ///
59    /// This is function is mostly meant to be used by widgets. Applications can use
60    /// [`WidgetExt::grab_focus()`][crate::prelude::WidgetExt::grab_focus()] to manually set the focus to a specific widget.
61    /// ## `child`
62    /// a [`Widget`][crate::Widget], or [`None`]
63    fn set_focus_child(&self, widget: Option<&Widget>) {
64        self.parent_set_focus_child(widget)
65    }
66
67    /// Returns the type of the children supported by the container.
68    ///
69    /// Note that this may return `G_TYPE_NONE` to indicate that no more
70    /// children can be added, e.g. for a [`Paned`][crate::Paned] which already has two
71    /// children.
72    ///
73    /// # Returns
74    ///
75    /// a `GType`.
76    fn child_type(&self) -> glib::Type {
77        self.parent_child_type()
78    }
79
80    /// Returns a newly created widget path representing all the widget hierarchy
81    /// from the toplevel down to and including `child`.
82    /// ## `child`
83    /// a child of `container`
84    ///
85    /// # Returns
86    ///
87    /// A newly created [`WidgetPath`][crate::WidgetPath]
88    #[doc(alias = "get_path_for_child")]
89    fn path_for_child(&self, widget: &Widget) -> WidgetPath {
90        self.parent_path_for_child(widget)
91    }
92
93    /// Invokes `callback` on each direct child of `container`, including
94    /// children that are considered “internal” (implementation details
95    /// of the container). “Internal” children generally weren’t added
96    /// by the user of the container, but were added by the container
97    /// implementation itself.
98    ///
99    /// Most applications should use [`ContainerExt::foreach()`][crate::prelude::ContainerExt::foreach()], rather
100    /// than [`ContainerExt::forall()`][crate::prelude::ContainerExt::forall()].
101    /// ## `callback`
102    /// a callback
103    /// ## `callback_data`
104    /// callback user data
105    fn forall(&self, include_internals: bool, callback: &Callback) {
106        self.parent_forall(include_internals, callback);
107    }
108}
109
110mod sealed {
111    pub trait Sealed {}
112    impl<T: super::ContainerImpl> Sealed for T {}
113}
114
115pub trait ContainerImplExt: ObjectSubclass + sealed::Sealed {
116    fn parent_add(&self, widget: &Widget) {
117        unsafe {
118            let data = Self::type_data();
119            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkContainerClass;
120            if let Some(f) = (*parent_class).add {
121                f(
122                    self.obj().unsafe_cast_ref::<Container>().to_glib_none().0,
123                    widget.to_glib_none().0,
124                )
125            }
126        }
127    }
128    fn parent_remove(&self, widget: &Widget) {
129        unsafe {
130            let data = Self::type_data();
131            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkContainerClass;
132            if let Some(f) = (*parent_class).remove {
133                f(
134                    self.obj().unsafe_cast_ref::<Container>().to_glib_none().0,
135                    widget.to_glib_none().0,
136                )
137            }
138        }
139    }
140    fn parent_check_resize(&self) {
141        unsafe {
142            let data = Self::type_data();
143            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkContainerClass;
144            if let Some(f) = (*parent_class).check_resize {
145                f(self.obj().unsafe_cast_ref::<Container>().to_glib_none().0)
146            }
147        }
148    }
149    fn parent_set_focus_child(&self, widget: Option<&Widget>) {
150        unsafe {
151            let data = Self::type_data();
152            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkContainerClass;
153            if let Some(f) = (*parent_class).set_focus_child {
154                f(
155                    self.obj().unsafe_cast_ref::<Container>().to_glib_none().0,
156                    widget.to_glib_none().0,
157                )
158            }
159        }
160    }
161    fn parent_child_type(&self) -> glib::Type {
162        unsafe {
163            let data = Self::type_data();
164            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkContainerClass;
165            if let Some(f) = (*parent_class).child_type {
166                from_glib(f(self
167                    .obj()
168                    .unsafe_cast_ref::<Container>()
169                    .to_glib_none()
170                    .0))
171            } else {
172                glib::Type::UNIT
173            }
174        }
175    }
176    fn parent_path_for_child(&self, widget: &Widget) -> WidgetPath {
177        unsafe {
178            let data = Self::type_data();
179            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkContainerClass;
180            let f = (*parent_class)
181                .get_path_for_child
182                .expect("No parent class impl for \"get_path_for_child\"");
183            from_glib_none(f(
184                self.obj().unsafe_cast_ref::<Container>().to_glib_none().0,
185                widget.to_glib_none().0,
186            ))
187        }
188    }
189    fn parent_forall(&self, include_internals: bool, callback: &Callback) {
190        unsafe {
191            let data = Self::type_data();
192            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkContainerClass;
193            if let Some(f) = (*parent_class).forall {
194                f(
195                    self.obj().unsafe_cast_ref::<Container>().to_glib_none().0,
196                    include_internals.into_glib(),
197                    callback.callback,
198                    callback.user_data,
199                )
200            }
201        }
202    }
203}
204
205impl<T: ContainerImpl> ContainerImplExt for T {}
206
207unsafe impl<T: ContainerImpl> IsSubclassable<T> for Container {
208    fn class_init(class: &mut ::glib::Class<Self>) {
209        Self::parent_class_init::<T>(class);
210
211        if !crate::rt::is_initialized() {
212            panic!("GTK has to be initialized first");
213        }
214
215        let klass = class.as_mut();
216        klass.add = Some(container_add::<T>);
217        klass.remove = Some(container_remove::<T>);
218        klass.check_resize = Some(container_check_resize::<T>);
219        klass.set_focus_child = Some(container_set_focus_child::<T>);
220        klass.child_type = Some(container_child_type::<T>);
221        klass.get_path_for_child = Some(container_get_path_for_child::<T>);
222        klass.forall = Some(container_forall::<T>);
223    }
224}
225
226unsafe extern "C" fn container_add<T: ContainerImpl>(
227    ptr: *mut ffi::GtkContainer,
228    wdgtptr: *mut ffi::GtkWidget,
229) {
230    unsafe {
231        let instance = &*(ptr as *mut T::Instance);
232        let imp = instance.imp();
233        let widget: Borrowed<Widget> = from_glib_borrow(wdgtptr);
234
235        imp.add(&widget)
236    }
237}
238
239unsafe extern "C" fn container_remove<T: ContainerImpl>(
240    ptr: *mut ffi::GtkContainer,
241    wdgtptr: *mut ffi::GtkWidget,
242) {
243    unsafe {
244        let instance = &*(ptr as *mut T::Instance);
245        let imp = instance.imp();
246        let widget: Borrowed<Widget> = from_glib_borrow(wdgtptr);
247
248        imp.remove(&widget)
249    }
250}
251
252unsafe extern "C" fn container_check_resize<T: ContainerImpl>(ptr: *mut ffi::GtkContainer) {
253    unsafe {
254        let instance = &*(ptr as *mut T::Instance);
255        let imp = instance.imp();
256
257        imp.check_resize()
258    }
259}
260
261unsafe extern "C" fn container_set_focus_child<T: ContainerImpl>(
262    ptr: *mut ffi::GtkContainer,
263    wdgtptr: *mut ffi::GtkWidget,
264) {
265    unsafe {
266        let instance = &*(ptr as *mut T::Instance);
267        let imp = instance.imp();
268        let widget: Borrowed<Option<Widget>> = from_glib_borrow(wdgtptr);
269
270        imp.set_focus_child(widget.as_ref().as_ref())
271    }
272}
273
274unsafe extern "C" fn container_child_type<T: ContainerImpl>(
275    ptr: *mut ffi::GtkContainer,
276) -> glib::ffi::GType {
277    unsafe {
278        let instance = &*(ptr as *mut T::Instance);
279        let imp = instance.imp();
280
281        imp.child_type().into_glib()
282    }
283}
284
285unsafe extern "C" fn container_get_path_for_child<T: ContainerImpl>(
286    ptr: *mut ffi::GtkContainer,
287    wdgtptr: *mut ffi::GtkWidget,
288) -> *mut ffi::GtkWidgetPath {
289    unsafe {
290        let instance = &*(ptr as *mut T::Instance);
291        let imp = instance.imp();
292        let widget: Borrowed<Widget> = from_glib_borrow(wdgtptr);
293
294        imp.path_for_child(&widget).to_glib_none().0
295    }
296}
297
298unsafe extern "C" fn container_forall<T>(
299    ptr: *mut ffi::GtkContainer,
300    include_internals: glib::ffi::gboolean,
301    callback: ffi::GtkCallback,
302    user_data: glib::ffi::gpointer,
303) where
304    T: ObjectSubclass + ContainerImpl,
305{
306    unsafe {
307        let instance = &*(ptr as *mut T::Instance);
308        let imp = instance.imp();
309        let callback = Callback {
310            callback,
311            user_data,
312        };
313
314        imp.forall(from_glib(include_internals), &callback)
315    }
316}
317
318#[derive(Debug)]
319pub struct Callback {
320    callback: ffi::GtkCallback,
321    user_data: glib::ffi::gpointer,
322}
323
324impl Callback {
325    pub fn call(&self, widget: &Widget) {
326        unsafe {
327            if let Some(callback) = self.callback {
328                callback(widget.to_glib_none().0, self.user_data);
329            }
330        }
331    }
332}
333
334pub unsafe trait ContainerClassSubclassExt: ClassStruct {
335    #[doc(alias = "gtk_container_class_handle_border_width")]
336    fn handle_border_width(&mut self) {
337        unsafe {
338            let widget_class = self as *mut _ as *mut ffi::GtkContainerClass;
339            ffi::gtk_container_class_handle_border_width(widget_class);
340        }
341    }
342}
343
344unsafe impl<T: ClassStruct> ContainerClassSubclassExt for T where T::Type: ContainerImpl {}