gtk4/auto/
layout_manager.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::{ffi, LayoutChild, Orientation, SizeRequestMode, Widget};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9    /// Handles the preferred size and allocation for children of a widget.
10    ///
11    /// You typically subclass [`LayoutManager`][crate::LayoutManager] if you want to implement a
12    /// layout policy for the children of a widget, or if you want to determine
13    /// the size of a widget depending on its contents.
14    ///
15    /// Each [`Widget`][crate::Widget] can only have a [`LayoutManager`][crate::LayoutManager] instance associated
16    /// to it at any given time; it is possible, though, to replace the layout
17    /// manager instance using [`WidgetExt::set_layout_manager()`][crate::prelude::WidgetExt::set_layout_manager()].
18    ///
19    /// ## Layout properties
20    ///
21    /// A layout manager can expose properties for controlling the layout of
22    /// each child, by creating an object type derived from [`LayoutChild`][crate::LayoutChild]
23    /// and installing the properties on it as normal `GObject` properties.
24    ///
25    /// Each [`LayoutChild`][crate::LayoutChild] instance storing the layout properties for a
26    /// specific child is created through the [`LayoutManagerExt::layout_child()`][crate::prelude::LayoutManagerExt::layout_child()]
27    /// method; a [`LayoutManager`][crate::LayoutManager] controls the creation of its [`LayoutChild`][crate::LayoutChild]
28    /// instances by overriding the GtkLayoutManagerClass.create_layout_child()
29    /// virtual function. The typical implementation should look like:
30    ///
31    /// **⚠️ The following code is in c ⚠️**
32    ///
33    /// ```c
34    /// static GtkLayoutChild *
35    /// create_layout_child (GtkLayoutManager *manager,
36    ///                      GtkWidget        *container,
37    ///                      GtkWidget        *child)
38    /// {
39    ///   return g_object_new (your_layout_child_get_type (),
40    ///                        "layout-manager", manager,
41    ///                        "child-widget", child,
42    ///                        NULL);
43    /// }
44    /// ```
45    ///
46    /// The [`layout-manager`][struct@crate::LayoutChild#layout-manager] and
47    /// [`child-widget`][struct@crate::LayoutChild#child-widget] properties
48    /// on the newly created [`LayoutChild`][crate::LayoutChild] instance are mandatory. The
49    /// [`LayoutManager`][crate::LayoutManager] will cache the newly created [`LayoutChild`][crate::LayoutChild] instance
50    /// until the widget is removed from its parent, or the parent removes the
51    /// layout manager.
52    ///
53    /// Each [`LayoutManager`][crate::LayoutManager] instance creating a [`LayoutChild`][crate::LayoutChild] should use
54    /// [`LayoutManagerExt::layout_child()`][crate::prelude::LayoutManagerExt::layout_child()] every time it needs to query
55    /// the layout properties; each [`LayoutChild`][crate::LayoutChild] instance should call
56    /// [`LayoutManagerExt::layout_changed()`][crate::prelude::LayoutManagerExt::layout_changed()] every time a property is
57    /// updated, in order to queue a new size measuring and allocation.
58    ///
59    /// This is an Abstract Base Class, you cannot instantiate it.
60    ///
61    /// # Implements
62    ///
63    /// [`LayoutManagerExt`][trait@crate::prelude::LayoutManagerExt], [`trait@glib::ObjectExt`]
64    #[doc(alias = "GtkLayoutManager")]
65    pub struct LayoutManager(Object<ffi::GtkLayoutManager, ffi::GtkLayoutManagerClass>);
66
67    match fn {
68        type_ => || ffi::gtk_layout_manager_get_type(),
69    }
70}
71
72impl LayoutManager {
73    pub const NONE: Option<&'static LayoutManager> = None;
74}
75
76/// Trait containing all [`struct@LayoutManager`] methods.
77///
78/// # Implementors
79///
80/// [`BinLayout`][struct@crate::BinLayout], [`BoxLayout`][struct@crate::BoxLayout], [`CenterLayout`][struct@crate::CenterLayout], [`ConstraintLayout`][struct@crate::ConstraintLayout], [`FixedLayout`][struct@crate::FixedLayout], [`GridLayout`][struct@crate::GridLayout], [`LayoutManager`][struct@crate::LayoutManager], [`OverlayLayout`][struct@crate::OverlayLayout]
81pub trait LayoutManagerExt: IsA<LayoutManager> + 'static {
82    /// Assigns the given @width, @height, and @baseline to
83    /// a @widget, and computes the position and sizes of the children of
84    /// the @widget using the layout management policy of @self.
85    /// ## `widget`
86    /// the [`Widget`][crate::Widget] using @self
87    /// ## `width`
88    /// the new width of the @widget
89    /// ## `height`
90    /// the new height of the @widget
91    /// ## `baseline`
92    /// the baseline position of the @widget, or -1
93    #[doc(alias = "gtk_layout_manager_allocate")]
94    fn allocate(&self, widget: &impl IsA<Widget>, width: i32, height: i32, baseline: i32) {
95        unsafe {
96            ffi::gtk_layout_manager_allocate(
97                self.as_ref().to_glib_none().0,
98                widget.as_ref().to_glib_none().0,
99                width,
100                height,
101                baseline,
102            );
103        }
104    }
105
106    /// Retrieves a [`LayoutChild`][crate::LayoutChild] instance for the [`LayoutManager`][crate::LayoutManager],
107    /// creating one if necessary.
108    ///
109    /// The @child widget must be a child of the widget using @self.
110    ///
111    /// The [`LayoutChild`][crate::LayoutChild] instance is owned by the [`LayoutManager`][crate::LayoutManager],
112    /// and is guaranteed to exist as long as @child is a child of the
113    /// [`Widget`][crate::Widget] using the given [`LayoutManager`][crate::LayoutManager].
114    /// ## `child`
115    /// a [`Widget`][crate::Widget]
116    ///
117    /// # Returns
118    ///
119    /// a [`LayoutChild`][crate::LayoutChild]
120    #[doc(alias = "gtk_layout_manager_get_layout_child")]
121    #[doc(alias = "get_layout_child")]
122    fn layout_child(&self, child: &impl IsA<Widget>) -> LayoutChild {
123        unsafe {
124            from_glib_none(ffi::gtk_layout_manager_get_layout_child(
125                self.as_ref().to_glib_none().0,
126                child.as_ref().to_glib_none().0,
127            ))
128        }
129    }
130
131    /// Retrieves the request mode of @self.
132    ///
133    /// # Returns
134    ///
135    /// a [`SizeRequestMode`][crate::SizeRequestMode]
136    #[doc(alias = "gtk_layout_manager_get_request_mode")]
137    #[doc(alias = "get_request_mode")]
138    fn request_mode(&self) -> SizeRequestMode {
139        unsafe {
140            from_glib(ffi::gtk_layout_manager_get_request_mode(
141                self.as_ref().to_glib_none().0,
142            ))
143        }
144    }
145
146    /// Retrieves the [`Widget`][crate::Widget] using the given [`LayoutManager`][crate::LayoutManager].
147    ///
148    /// # Returns
149    ///
150    /// a [`Widget`][crate::Widget]
151    #[doc(alias = "gtk_layout_manager_get_widget")]
152    #[doc(alias = "get_widget")]
153    fn widget(&self) -> Option<Widget> {
154        unsafe {
155            from_glib_none(ffi::gtk_layout_manager_get_widget(
156                self.as_ref().to_glib_none().0,
157            ))
158        }
159    }
160
161    /// Queues a resize on the [`Widget`][crate::Widget] using @self, if any.
162    ///
163    /// This function should be called by subclasses of [`LayoutManager`][crate::LayoutManager]
164    /// in response to changes to their layout management policies.
165    #[doc(alias = "gtk_layout_manager_layout_changed")]
166    fn layout_changed(&self) {
167        unsafe {
168            ffi::gtk_layout_manager_layout_changed(self.as_ref().to_glib_none().0);
169        }
170    }
171
172    /// Measures the size of the @widget using @self, for the
173    /// given @orientation and size.
174    ///
175    /// See the [`Widget`][crate::Widget] documentation on layout management for
176    /// more details.
177    /// ## `widget`
178    /// the [`Widget`][crate::Widget] using @self
179    /// ## `orientation`
180    /// the orientation to measure
181    /// ## `for_size`
182    /// Size for the opposite of @orientation; for instance, if
183    ///   the @orientation is [`Orientation::Horizontal`][crate::Orientation::Horizontal], this is the height
184    ///   of the widget; if the @orientation is [`Orientation::Vertical`][crate::Orientation::Vertical], this
185    ///   is the width of the widget. This allows to measure the height for the
186    ///   given width, and the width for the given height. Use -1 if the size
187    ///   is not known
188    ///
189    /// # Returns
190    ///
191    ///
192    /// ## `minimum`
193    /// the minimum size for the given size and
194    ///   orientation
195    ///
196    /// ## `natural`
197    /// the natural, or preferred size for the
198    ///   given size and orientation
199    ///
200    /// ## `minimum_baseline`
201    /// the baseline position for the
202    ///   minimum size
203    ///
204    /// ## `natural_baseline`
205    /// the baseline position for the
206    ///   natural size
207    #[doc(alias = "gtk_layout_manager_measure")]
208    fn measure(
209        &self,
210        widget: &impl IsA<Widget>,
211        orientation: Orientation,
212        for_size: i32,
213    ) -> (i32, i32, i32, i32) {
214        unsafe {
215            let mut minimum = std::mem::MaybeUninit::uninit();
216            let mut natural = std::mem::MaybeUninit::uninit();
217            let mut minimum_baseline = std::mem::MaybeUninit::uninit();
218            let mut natural_baseline = std::mem::MaybeUninit::uninit();
219            ffi::gtk_layout_manager_measure(
220                self.as_ref().to_glib_none().0,
221                widget.as_ref().to_glib_none().0,
222                orientation.into_glib(),
223                for_size,
224                minimum.as_mut_ptr(),
225                natural.as_mut_ptr(),
226                minimum_baseline.as_mut_ptr(),
227                natural_baseline.as_mut_ptr(),
228            );
229            (
230                minimum.assume_init(),
231                natural.assume_init(),
232                minimum_baseline.assume_init(),
233                natural_baseline.assume_init(),
234            )
235        }
236    }
237}
238
239impl<O: IsA<LayoutManager>> LayoutManagerExt for O {}