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};
6use glib::{
7 prelude::*,
8 signal::{connect_raw, SignalHandlerId},
9 translate::*,
10};
11use std::{boxed::Box as Box_, fmt, mem::transmute};
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 | Writeable
93 ///
94 ///
95 /// #### `mode`
96 /// Readable | Writeable
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 self.builder.build()
167 }
168}
169
170mod sealed {
171 pub trait Sealed {}
172 impl<T: super::IsA<super::SizeGroup>> Sealed for T {}
173}
174
175/// Trait containing all [`struct@SizeGroup`] methods.
176///
177/// # Implementors
178///
179/// [`SizeGroup`][struct@crate::SizeGroup]
180pub trait SizeGroupExt: IsA<SizeGroup> + sealed::Sealed + 'static {
181 /// Adds a widget to a [`SizeGroup`][crate::SizeGroup]. In the future, the requisition
182 /// of the widget will be determined as the maximum of its requisition
183 /// and the requisition of the other widgets in the size group.
184 /// Whether this applies horizontally, vertically, or in both directions
185 /// depends on the mode of the size group. See [`set_mode()`][Self::set_mode()].
186 ///
187 /// When the widget is destroyed or no longer referenced elsewhere, it will
188 /// be removed from the size group.
189 /// ## `widget`
190 /// the [`Widget`][crate::Widget] to add
191 #[doc(alias = "gtk_size_group_add_widget")]
192 fn add_widget(&self, widget: &impl IsA<Widget>) {
193 unsafe {
194 ffi::gtk_size_group_add_widget(
195 self.as_ref().to_glib_none().0,
196 widget.as_ref().to_glib_none().0,
197 );
198 }
199 }
200
201 /// Gets the current mode of the size group. See [`set_mode()`][Self::set_mode()].
202 ///
203 /// # Returns
204 ///
205 /// the current mode of the size group.
206 #[doc(alias = "gtk_size_group_get_mode")]
207 #[doc(alias = "get_mode")]
208 fn mode(&self) -> SizeGroupMode {
209 unsafe { from_glib(ffi::gtk_size_group_get_mode(self.as_ref().to_glib_none().0)) }
210 }
211
212 /// Returns the list of widgets associated with `self`.
213 ///
214 /// # Returns
215 ///
216 /// a `GSList` of
217 /// widgets. The list is owned by GTK+ and should not be modified.
218 #[doc(alias = "gtk_size_group_get_widgets")]
219 #[doc(alias = "get_widgets")]
220 fn widgets(&self) -> Vec<Widget> {
221 unsafe {
222 FromGlibPtrContainer::from_glib_none(ffi::gtk_size_group_get_widgets(
223 self.as_ref().to_glib_none().0,
224 ))
225 }
226 }
227
228 /// Removes a widget from a [`SizeGroup`][crate::SizeGroup].
229 /// ## `widget`
230 /// the [`Widget`][crate::Widget] to remove
231 #[doc(alias = "gtk_size_group_remove_widget")]
232 fn remove_widget(&self, widget: &impl IsA<Widget>) {
233 unsafe {
234 ffi::gtk_size_group_remove_widget(
235 self.as_ref().to_glib_none().0,
236 widget.as_ref().to_glib_none().0,
237 );
238 }
239 }
240
241 /// Sets the [`SizeGroupMode`][crate::SizeGroupMode] of the size group. The mode of the size
242 /// group determines whether the widgets in the size group should
243 /// all have the same horizontal requisition ([`SizeGroupMode::Horizontal`][crate::SizeGroupMode::Horizontal])
244 /// all have the same vertical requisition ([`SizeGroupMode::Vertical`][crate::SizeGroupMode::Vertical]),
245 /// or should all have the same requisition in both directions
246 /// ([`SizeGroupMode::Both`][crate::SizeGroupMode::Both]).
247 /// ## `mode`
248 /// the mode to set for the size group.
249 #[doc(alias = "gtk_size_group_set_mode")]
250 fn set_mode(&self, mode: SizeGroupMode) {
251 unsafe {
252 ffi::gtk_size_group_set_mode(self.as_ref().to_glib_none().0, mode.into_glib());
253 }
254 }
255
256 #[doc(alias = "mode")]
257 fn connect_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
258 unsafe extern "C" fn notify_mode_trampoline<P: IsA<SizeGroup>, F: Fn(&P) + 'static>(
259 this: *mut ffi::GtkSizeGroup,
260 _param_spec: glib::ffi::gpointer,
261 f: glib::ffi::gpointer,
262 ) {
263 let f: &F = &*(f as *const F);
264 f(SizeGroup::from_glib_borrow(this).unsafe_cast_ref())
265 }
266 unsafe {
267 let f: Box_<F> = Box_::new(f);
268 connect_raw(
269 self.as_ptr() as *mut _,
270 b"notify::mode\0".as_ptr() as *const _,
271 Some(transmute::<_, unsafe extern "C" fn()>(
272 notify_mode_trampoline::<Self, F> as *const (),
273 )),
274 Box_::into_raw(f),
275 )
276 }
277 }
278}
279
280impl<O: IsA<SizeGroup>> SizeGroupExt for O {}
281
282impl fmt::Display for SizeGroup {
283 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
284 f.write_str("SizeGroup")
285 }
286}