1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::LayoutChild;
use crate::Orientation;
use crate::SizeRequestMode;
use crate::Widget;
use glib::object::IsA;
use glib::translate::*;
use std::fmt;
use std::mem;

glib::wrapper! {
    /// Layout managers are delegate classes that handle the preferred size
    /// and the allocation of a widget.
    ///
    /// You typically subclass [`LayoutManager`][crate::LayoutManager] if you want to implement a
    /// layout policy for the children of a widget, or if you want to determine
    /// the size of a widget depending on its contents.
    ///
    /// Each [`Widget`][crate::Widget] can only have a [`LayoutManager`][crate::LayoutManager] instance associated
    /// to it at any given time; it is possible, though, to replace the layout
    /// manager instance using [`WidgetExt::set_layout_manager()`][crate::prelude::WidgetExt::set_layout_manager()].
    ///
    /// ## Layout properties
    ///
    /// A layout manager can expose properties for controlling the layout of
    /// each child, by creating an object type derived from [`LayoutChild`][crate::LayoutChild]
    /// and installing the properties on it as normal `GObject` properties.
    ///
    /// Each [`LayoutChild`][crate::LayoutChild] instance storing the layout properties for a
    /// specific child is created through the [`LayoutManagerExt::layout_child()`][crate::prelude::LayoutManagerExt::layout_child()]
    /// method; a [`LayoutManager`][crate::LayoutManager] controls the creation of its [`LayoutChild`][crate::LayoutChild]
    /// instances by overriding the GtkLayoutManagerClass.create_layout_child()
    /// virtual function. The typical implementation should look like:
    ///
    /// **⚠️ The following code is in c ⚠️**
    ///
    /// ```c
    /// static GtkLayoutChild *
    /// create_layout_child (GtkLayoutManager *manager,
    ///                      GtkWidget        *container,
    ///                      GtkWidget        *child)
    /// {
    ///   return g_object_new (your_layout_child_get_type (),
    ///                        "layout-manager", manager,
    ///                        "child-widget", child,
    ///                        NULL);
    /// }
    /// ```
    ///
    /// The `property::LayoutChild::layout-manager` and
    /// `property::LayoutChild::child-widget` properties
    /// on the newly created [`LayoutChild`][crate::LayoutChild] instance are mandatory. The
    /// [`LayoutManager`][crate::LayoutManager] will cache the newly created [`LayoutChild`][crate::LayoutChild] instance
    /// until the widget is removed from its parent, or the parent removes the
    /// layout manager.
    ///
    /// Each [`LayoutManager`][crate::LayoutManager] instance creating a [`LayoutChild`][crate::LayoutChild] should use
    /// [`LayoutManagerExt::layout_child()`][crate::prelude::LayoutManagerExt::layout_child()] every time it needs to query
    /// the layout properties; each [`LayoutChild`][crate::LayoutChild] instance should call
    /// [`LayoutManagerExt::layout_changed()`][crate::prelude::LayoutManagerExt::layout_changed()] every time a property is
    /// updated, in order to queue a new size measuring and allocation.
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// # Implements
    ///
    /// [`LayoutManagerExt`][trait@crate::prelude::LayoutManagerExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "GtkLayoutManager")]
    pub struct LayoutManager(Object<ffi::GtkLayoutManager, ffi::GtkLayoutManagerClass>);

    match fn {
        type_ => || ffi::gtk_layout_manager_get_type(),
    }
}

impl LayoutManager {
    pub const NONE: Option<&'static LayoutManager> = None;
}

/// Trait containing all [`struct@LayoutManager`] methods.
///
/// # Implementors
///
/// [`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]
pub trait LayoutManagerExt: 'static {
    /// Assigns the given @width, @height, and @baseline to
    /// a @widget, and computes the position and sizes of the children of
    /// the @widget using the layout management policy of @self.
    /// ## `widget`
    /// the [`Widget`][crate::Widget] using @self
    /// ## `width`
    /// the new width of the @widget
    /// ## `height`
    /// the new height of the @widget
    /// ## `baseline`
    /// the baseline position of the @widget, or -1
    #[doc(alias = "gtk_layout_manager_allocate")]
    fn allocate(&self, widget: &impl IsA<Widget>, width: i32, height: i32, baseline: i32);

    /// Retrieves a [`LayoutChild`][crate::LayoutChild] instance for the [`LayoutManager`][crate::LayoutManager],
    /// creating one if necessary.
    ///
    /// The @child widget must be a child of the widget using @self.
    ///
    /// The [`LayoutChild`][crate::LayoutChild] instance is owned by the [`LayoutManager`][crate::LayoutManager],
    /// and is guaranteed to exist as long as @child is a child of the
    /// [`Widget`][crate::Widget] using the given [`LayoutManager`][crate::LayoutManager].
    /// ## `child`
    /// a [`Widget`][crate::Widget]
    ///
    /// # Returns
    ///
    /// a [`LayoutChild`][crate::LayoutChild]
    #[doc(alias = "gtk_layout_manager_get_layout_child")]
    #[doc(alias = "get_layout_child")]
    fn layout_child(&self, child: &impl IsA<Widget>) -> LayoutChild;

    /// Retrieves the request mode of @self.
    ///
    /// # Returns
    ///
    /// a [`SizeRequestMode`][crate::SizeRequestMode]
    #[doc(alias = "gtk_layout_manager_get_request_mode")]
    #[doc(alias = "get_request_mode")]
    fn request_mode(&self) -> SizeRequestMode;

    /// Retrieves the [`Widget`][crate::Widget] using the given [`LayoutManager`][crate::LayoutManager].
    ///
    /// # Returns
    ///
    /// a [`Widget`][crate::Widget]
    #[doc(alias = "gtk_layout_manager_get_widget")]
    #[doc(alias = "get_widget")]
    fn widget(&self) -> Option<Widget>;

    /// Queues a resize on the [`Widget`][crate::Widget] using @self, if any.
    ///
    /// This function should be called by subclasses of [`LayoutManager`][crate::LayoutManager]
    /// in response to changes to their layout management policies.
    #[doc(alias = "gtk_layout_manager_layout_changed")]
    fn layout_changed(&self);

    /// Measures the size of the @widget using @self, for the
    /// given @orientation and size.
    ///
    /// See the [`Widget`][crate::Widget] documentation on layout management for
    /// more details.
    /// ## `widget`
    /// the [`Widget`][crate::Widget] using @self
    /// ## `orientation`
    /// the orientation to measure
    /// ## `for_size`
    /// Size for the opposite of @orientation; for instance, if
    ///   the @orientation is [`Orientation::Horizontal`][crate::Orientation::Horizontal], this is the height
    ///   of the widget; if the @orientation is [`Orientation::Vertical`][crate::Orientation::Vertical], this
    ///   is the width of the widget. This allows to measure the height for the
    ///   given width, and the width for the given height. Use -1 if the size
    ///   is not known
    ///
    /// # Returns
    ///
    ///
    /// ## `minimum`
    /// the minimum size for the given size and
    ///   orientation
    ///
    /// ## `natural`
    /// the natural, or preferred size for the
    ///   given size and orientation
    ///
    /// ## `minimum_baseline`
    /// the baseline position for the
    ///   minimum size
    ///
    /// ## `natural_baseline`
    /// the baseline position for the
    ///   natural size
    #[doc(alias = "gtk_layout_manager_measure")]
    fn measure(
        &self,
        widget: &impl IsA<Widget>,
        orientation: Orientation,
        for_size: i32,
    ) -> (i32, i32, i32, i32);
}

impl<O: IsA<LayoutManager>> LayoutManagerExt for O {
    fn allocate(&self, widget: &impl IsA<Widget>, width: i32, height: i32, baseline: i32) {
        unsafe {
            ffi::gtk_layout_manager_allocate(
                self.as_ref().to_glib_none().0,
                widget.as_ref().to_glib_none().0,
                width,
                height,
                baseline,
            );
        }
    }

    fn layout_child(&self, child: &impl IsA<Widget>) -> LayoutChild {
        unsafe {
            from_glib_none(ffi::gtk_layout_manager_get_layout_child(
                self.as_ref().to_glib_none().0,
                child.as_ref().to_glib_none().0,
            ))
        }
    }

    fn request_mode(&self) -> SizeRequestMode {
        unsafe {
            from_glib(ffi::gtk_layout_manager_get_request_mode(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn widget(&self) -> Option<Widget> {
        unsafe {
            from_glib_none(ffi::gtk_layout_manager_get_widget(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn layout_changed(&self) {
        unsafe {
            ffi::gtk_layout_manager_layout_changed(self.as_ref().to_glib_none().0);
        }
    }

    fn measure(
        &self,
        widget: &impl IsA<Widget>,
        orientation: Orientation,
        for_size: i32,
    ) -> (i32, i32, i32, i32) {
        unsafe {
            let mut minimum = mem::MaybeUninit::uninit();
            let mut natural = mem::MaybeUninit::uninit();
            let mut minimum_baseline = mem::MaybeUninit::uninit();
            let mut natural_baseline = mem::MaybeUninit::uninit();
            ffi::gtk_layout_manager_measure(
                self.as_ref().to_glib_none().0,
                widget.as_ref().to_glib_none().0,
                orientation.into_glib(),
                for_size,
                minimum.as_mut_ptr(),
                natural.as_mut_ptr(),
                minimum_baseline.as_mut_ptr(),
                natural_baseline.as_mut_ptr(),
            );
            (
                minimum.assume_init(),
                natural.assume_init(),
                minimum_baseline.assume_init(),
                natural_baseline.assume_init(),
            )
        }
    }
}

impl fmt::Display for LayoutManager {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("LayoutManager")
    }
}