gtk/auto/size_group.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::{Buildable, SizeGroupMode, Widget, ffi};
6use glib::{
7 prelude::*,
8 signal::{SignalHandlerId, connect_raw},
9 translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14 /// [`SizeGroup`][crate::SizeGroup] provides a mechanism for grouping a number of widgets
15 /// together so they all request the same amount of space. This is
16 /// typically useful when you want a column of widgets to have the same
17 /// size, but you can’t use a [`Grid`][crate::Grid] widget.
18 ///
19 /// In detail, the size requested for each widget in a [`SizeGroup`][crate::SizeGroup] is
20 /// the maximum of the sizes that would have been requested for each
21 /// widget in the size group if they were not in the size group. The mode
22 /// of the size group (see [`SizeGroupExt::set_mode()`][crate::prelude::SizeGroupExt::set_mode()]) determines whether
23 /// this applies to the horizontal size, the vertical size, or both sizes.
24 ///
25 /// Note that size groups only affect the amount of space requested, not
26 /// the size that the widgets finally receive. If you want the widgets in
27 /// a [`SizeGroup`][crate::SizeGroup] to actually be the same size, you need to pack them in
28 /// such a way that they get the size they request and not more. For
29 /// example, if you are packing your widgets into a table, you would not
30 /// include the `GTK_FILL` flag.
31 ///
32 /// [`SizeGroup`][crate::SizeGroup] objects are referenced by each widget in the size group,
33 /// so once you have added all widgets to a [`SizeGroup`][crate::SizeGroup], you can drop
34 /// the initial reference to the size group with `g_object_unref()`. If the
35 /// widgets in the size group are subsequently destroyed, then they will
36 /// be removed from the size group and drop their references on the size
37 /// group; when all widgets have been removed, the size group will be
38 /// freed.
39 ///
40 /// Widgets can be part of multiple size groups; GTK+ will compute the
41 /// horizontal size of a widget from the horizontal requisition of all
42 /// widgets that can be reached from the widget by a chain of size groups
43 /// of type [`SizeGroupMode::Horizontal`][crate::SizeGroupMode::Horizontal] or [`SizeGroupMode::Both`][crate::SizeGroupMode::Both], and the
44 /// vertical size from the vertical requisition of all widgets that can be
45 /// reached from the widget by a chain of size groups of type
46 /// [`SizeGroupMode::Vertical`][crate::SizeGroupMode::Vertical] or [`SizeGroupMode::Both`][crate::SizeGroupMode::Both].
47 ///
48 /// Note that only non-contextual sizes of every widget are ever consulted
49 /// by size groups (since size groups have no knowledge of what size a widget
50 /// will be allocated in one dimension, it cannot derive how much height
51 /// a widget will receive for a given width). When grouping widgets that
52 /// trade height for width in mode [`SizeGroupMode::Vertical`][crate::SizeGroupMode::Vertical] or [`SizeGroupMode::Both`][crate::SizeGroupMode::Both]:
53 /// the height for the minimum width will be the requested height for all
54 /// widgets in the group. The same is of course true when horizontally grouping
55 /// width for height widgets.
56 ///
57 /// Widgets that trade height-for-width should set a reasonably large minimum width
58 /// by way of [`width-chars`][struct@crate::Label#width-chars] for instance. Widgets with static sizes as well
59 /// as widgets that grow (such as ellipsizing text) need no such considerations.
60 ///
61 /// # GtkSizeGroup as GtkBuildable
62 ///
63 /// Size groups can be specified in a UI definition by placing an ``<object>``
64 /// element with `class="GtkSizeGroup"` somewhere in the UI definition. The
65 /// widgets that belong to the size group are specified by a ``<widgets>`` element
66 /// that may contain multiple ``<widget>`` elements, one for each member of the
67 /// size group. The ”name” attribute gives the id of the widget.
68 ///
69 /// An example of a UI definition fragment with GtkSizeGroup:
70 ///
71 ///
72 ///
73 /// **⚠️ The following code is in xml ⚠️**
74 ///
75 /// ```xml
76 /// <object class="GtkSizeGroup">
77 /// <property name="mode">GTK_SIZE_GROUP_HORIZONTAL</property>
78 /// <widgets>
79 /// <widget name="radio1"/>
80 /// <widget name="radio2"/>
81 /// </widgets>
82 /// </object>
83 /// ```
84 ///
85 /// ## Properties
86 ///
87 ///
88 /// #### `ignore-hidden`
89 /// If [`true`], unmapped widgets are ignored when determining
90 /// the size of the group.
91 ///
92 /// Readable | Writable
93 ///
94 ///
95 /// #### `mode`
96 /// Readable | Writable
97 ///
98 /// # Implements
99 ///
100 /// [`SizeGroupExt`][trait@crate::prelude::SizeGroupExt], [`trait@glib::ObjectExt`], [`BuildableExt`][trait@crate::prelude::BuildableExt], [`BuildableExtManual`][trait@crate::prelude::BuildableExtManual]
101 #[doc(alias = "GtkSizeGroup")]
102 pub struct SizeGroup(Object<ffi::GtkSizeGroup, ffi::GtkSizeGroupClass>) @implements Buildable;
103
104 match fn {
105 type_ => || ffi::gtk_size_group_get_type(),
106 }
107}
108
109impl SizeGroup {
110 pub const NONE: Option<&'static SizeGroup> = None;
111
112 /// Create a new [`SizeGroup`][crate::SizeGroup].
113 /// ## `mode`
114 /// the mode for the new size group.
115 ///
116 /// # Returns
117 ///
118 /// a newly created [`SizeGroup`][crate::SizeGroup]
119 #[doc(alias = "gtk_size_group_new")]
120 pub fn new(mode: SizeGroupMode) -> SizeGroup {
121 assert_initialized_main_thread!();
122 unsafe { from_glib_full(ffi::gtk_size_group_new(mode.into_glib())) }
123 }
124
125 // rustdoc-stripper-ignore-next
126 /// Creates a new builder-pattern struct instance to construct [`SizeGroup`] objects.
127 ///
128 /// This method returns an instance of [`SizeGroupBuilder`](crate::builders::SizeGroupBuilder) which can be used to create [`SizeGroup`] objects.
129 pub fn builder() -> SizeGroupBuilder {
130 SizeGroupBuilder::new()
131 }
132}
133
134impl Default for SizeGroup {
135 fn default() -> Self {
136 glib::object::Object::new::<Self>()
137 }
138}
139
140// rustdoc-stripper-ignore-next
141/// A [builder-pattern] type to construct [`SizeGroup`] objects.
142///
143/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
144#[must_use = "The builder must be built to be used"]
145pub struct SizeGroupBuilder {
146 builder: glib::object::ObjectBuilder<'static, SizeGroup>,
147}
148
149impl SizeGroupBuilder {
150 fn new() -> Self {
151 Self {
152 builder: glib::object::Object::builder(),
153 }
154 }
155
156 pub fn mode(self, mode: SizeGroupMode) -> Self {
157 Self {
158 builder: self.builder.property("mode", mode),
159 }
160 }
161
162 // rustdoc-stripper-ignore-next
163 /// Build the [`SizeGroup`].
164 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
165 pub fn build(self) -> SizeGroup {
166 assert_initialized_main_thread!();
167 self.builder.build()
168 }
169}
170
171/// Trait containing all [`struct@SizeGroup`] methods.
172///
173/// # Implementors
174///
175/// [`SizeGroup`][struct@crate::SizeGroup]
176pub trait SizeGroupExt: IsA<SizeGroup> + 'static {
177 /// Adds a widget to a [`SizeGroup`][crate::SizeGroup]. In the future, the requisition
178 /// of the widget will be determined as the maximum of its requisition
179 /// and the requisition of the other widgets in the size group.
180 /// Whether this applies horizontally, vertically, or in both directions
181 /// depends on the mode of the size group. See [`set_mode()`][Self::set_mode()].
182 ///
183 /// When the widget is destroyed or no longer referenced elsewhere, it will
184 /// be removed from the size group.
185 /// ## `widget`
186 /// the [`Widget`][crate::Widget] to add
187 #[doc(alias = "gtk_size_group_add_widget")]
188 fn add_widget(&self, widget: &impl IsA<Widget>) {
189 unsafe {
190 ffi::gtk_size_group_add_widget(
191 self.as_ref().to_glib_none().0,
192 widget.as_ref().to_glib_none().0,
193 );
194 }
195 }
196
197 /// Gets the current mode of the size group. See [`set_mode()`][Self::set_mode()].
198 ///
199 /// # Returns
200 ///
201 /// the current mode of the size group.
202 #[doc(alias = "gtk_size_group_get_mode")]
203 #[doc(alias = "get_mode")]
204 fn mode(&self) -> SizeGroupMode {
205 unsafe { from_glib(ffi::gtk_size_group_get_mode(self.as_ref().to_glib_none().0)) }
206 }
207
208 /// Returns the list of widgets associated with `self`.
209 ///
210 /// # Returns
211 ///
212 /// a `GSList` of
213 /// widgets. The list is owned by GTK+ and should not be modified.
214 #[doc(alias = "gtk_size_group_get_widgets")]
215 #[doc(alias = "get_widgets")]
216 fn widgets(&self) -> Vec<Widget> {
217 unsafe {
218 FromGlibPtrContainer::from_glib_none(ffi::gtk_size_group_get_widgets(
219 self.as_ref().to_glib_none().0,
220 ))
221 }
222 }
223
224 /// Removes a widget from a [`SizeGroup`][crate::SizeGroup].
225 /// ## `widget`
226 /// the [`Widget`][crate::Widget] to remove
227 #[doc(alias = "gtk_size_group_remove_widget")]
228 fn remove_widget(&self, widget: &impl IsA<Widget>) {
229 unsafe {
230 ffi::gtk_size_group_remove_widget(
231 self.as_ref().to_glib_none().0,
232 widget.as_ref().to_glib_none().0,
233 );
234 }
235 }
236
237 /// Sets the [`SizeGroupMode`][crate::SizeGroupMode] of the size group. The mode of the size
238 /// group determines whether the widgets in the size group should
239 /// all have the same horizontal requisition ([`SizeGroupMode::Horizontal`][crate::SizeGroupMode::Horizontal])
240 /// all have the same vertical requisition ([`SizeGroupMode::Vertical`][crate::SizeGroupMode::Vertical]),
241 /// or should all have the same requisition in both directions
242 /// ([`SizeGroupMode::Both`][crate::SizeGroupMode::Both]).
243 /// ## `mode`
244 /// the mode to set for the size group.
245 #[doc(alias = "gtk_size_group_set_mode")]
246 #[doc(alias = "mode")]
247 fn set_mode(&self, mode: SizeGroupMode) {
248 unsafe {
249 ffi::gtk_size_group_set_mode(self.as_ref().to_glib_none().0, mode.into_glib());
250 }
251 }
252
253 #[doc(alias = "mode")]
254 fn connect_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
255 unsafe extern "C" fn notify_mode_trampoline<P: IsA<SizeGroup>, F: Fn(&P) + 'static>(
256 this: *mut ffi::GtkSizeGroup,
257 _param_spec: glib::ffi::gpointer,
258 f: glib::ffi::gpointer,
259 ) {
260 unsafe {
261 let f: &F = &*(f as *const F);
262 f(SizeGroup::from_glib_borrow(this).unsafe_cast_ref())
263 }
264 }
265 unsafe {
266 let f: Box_<F> = Box_::new(f);
267 connect_raw(
268 self.as_ptr() as *mut _,
269 c"notify::mode".as_ptr(),
270 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
271 notify_mode_trampoline::<Self, F> as *const (),
272 )),
273 Box_::into_raw(f),
274 )
275 }
276 }
277}
278
279impl<O: IsA<SizeGroup>> SizeGroupExt for O {}