1use crate::{
6 ffi, Accessible, AccessibleRole, Align, BaselinePosition, Buildable, ConstraintTarget,
7 LayoutManager, Orientable, Orientation, Overflow, Widget,
8};
9use glib::{
10 prelude::*,
11 signal::{connect_raw, SignalHandlerId},
12 translate::*,
13};
14use std::boxed::Box as Box_;
15
16glib::wrapper! {
17 #[doc(alias = "GtkBox")]
18 pub struct Box(Object<ffi::GtkBox, ffi::GtkBoxClass>) @extends Widget, @implements Accessible, Buildable, ConstraintTarget, Orientable;
19
20 match fn {
21 type_ => || ffi::gtk_box_get_type(),
22 }
23}
24
25impl Box {
26 pub const NONE: Option<&'static Box> = None;
27
28 #[doc(alias = "gtk_box_new")]
29 pub fn new(orientation: Orientation, spacing: i32) -> Box {
30 assert_initialized_main_thread!();
31 unsafe {
32 Widget::from_glib_none(ffi::gtk_box_new(orientation.into_glib(), spacing)).unsafe_cast()
33 }
34 }
35
36 pub fn builder() -> BoxBuilder {
41 BoxBuilder::new()
42 }
43}
44
45impl Default for Box {
46 fn default() -> Self {
47 glib::object::Object::new::<Self>()
48 }
49}
50
51#[must_use = "The builder must be built to be used"]
56pub struct BoxBuilder {
57 builder: glib::object::ObjectBuilder<'static, Box>,
58}
59
60impl BoxBuilder {
61 fn new() -> Self {
62 Self {
63 builder: glib::object::Object::builder(),
64 }
65 }
66
67 #[cfg(feature = "v4_12")]
68 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
69 pub fn baseline_child(self, baseline_child: i32) -> Self {
70 Self {
71 builder: self.builder.property("baseline-child", baseline_child),
72 }
73 }
74
75 pub fn baseline_position(self, baseline_position: BaselinePosition) -> Self {
76 Self {
77 builder: self
78 .builder
79 .property("baseline-position", baseline_position),
80 }
81 }
82
83 pub fn homogeneous(self, homogeneous: bool) -> Self {
84 Self {
85 builder: self.builder.property("homogeneous", homogeneous),
86 }
87 }
88
89 pub fn spacing(self, spacing: i32) -> Self {
90 Self {
91 builder: self.builder.property("spacing", spacing),
92 }
93 }
94
95 pub fn can_focus(self, can_focus: bool) -> Self {
96 Self {
97 builder: self.builder.property("can-focus", can_focus),
98 }
99 }
100
101 pub fn can_target(self, can_target: bool) -> Self {
102 Self {
103 builder: self.builder.property("can-target", can_target),
104 }
105 }
106
107 pub fn css_classes(self, css_classes: impl Into<glib::StrV>) -> Self {
108 Self {
109 builder: self.builder.property("css-classes", css_classes.into()),
110 }
111 }
112
113 pub fn css_name(self, css_name: impl Into<glib::GString>) -> Self {
114 Self {
115 builder: self.builder.property("css-name", css_name.into()),
116 }
117 }
118
119 pub fn cursor(self, cursor: &gdk::Cursor) -> Self {
120 Self {
121 builder: self.builder.property("cursor", cursor.clone()),
122 }
123 }
124
125 pub fn focus_on_click(self, focus_on_click: bool) -> Self {
126 Self {
127 builder: self.builder.property("focus-on-click", focus_on_click),
128 }
129 }
130
131 pub fn focusable(self, focusable: bool) -> Self {
132 Self {
133 builder: self.builder.property("focusable", focusable),
134 }
135 }
136
137 pub fn halign(self, halign: Align) -> Self {
138 Self {
139 builder: self.builder.property("halign", halign),
140 }
141 }
142
143 pub fn has_tooltip(self, has_tooltip: bool) -> Self {
144 Self {
145 builder: self.builder.property("has-tooltip", has_tooltip),
146 }
147 }
148
149 pub fn height_request(self, height_request: i32) -> Self {
150 Self {
151 builder: self.builder.property("height-request", height_request),
152 }
153 }
154
155 pub fn hexpand(self, hexpand: bool) -> Self {
156 Self {
157 builder: self.builder.property("hexpand", hexpand),
158 }
159 }
160
161 pub fn hexpand_set(self, hexpand_set: bool) -> Self {
162 Self {
163 builder: self.builder.property("hexpand-set", hexpand_set),
164 }
165 }
166
167 pub fn layout_manager(self, layout_manager: &impl IsA<LayoutManager>) -> Self {
168 Self {
169 builder: self
170 .builder
171 .property("layout-manager", layout_manager.clone().upcast()),
172 }
173 }
174
175 #[cfg(feature = "v4_18")]
176 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
177 pub fn limit_events(self, limit_events: bool) -> Self {
178 Self {
179 builder: self.builder.property("limit-events", limit_events),
180 }
181 }
182
183 pub fn margin_bottom(self, margin_bottom: i32) -> Self {
184 Self {
185 builder: self.builder.property("margin-bottom", margin_bottom),
186 }
187 }
188
189 pub fn margin_end(self, margin_end: i32) -> Self {
190 Self {
191 builder: self.builder.property("margin-end", margin_end),
192 }
193 }
194
195 pub fn margin_start(self, margin_start: i32) -> Self {
196 Self {
197 builder: self.builder.property("margin-start", margin_start),
198 }
199 }
200
201 pub fn margin_top(self, margin_top: i32) -> Self {
202 Self {
203 builder: self.builder.property("margin-top", margin_top),
204 }
205 }
206
207 pub fn name(self, name: impl Into<glib::GString>) -> Self {
208 Self {
209 builder: self.builder.property("name", name.into()),
210 }
211 }
212
213 pub fn opacity(self, opacity: f64) -> Self {
214 Self {
215 builder: self.builder.property("opacity", opacity),
216 }
217 }
218
219 pub fn overflow(self, overflow: Overflow) -> Self {
220 Self {
221 builder: self.builder.property("overflow", overflow),
222 }
223 }
224
225 pub fn receives_default(self, receives_default: bool) -> Self {
226 Self {
227 builder: self.builder.property("receives-default", receives_default),
228 }
229 }
230
231 pub fn sensitive(self, sensitive: bool) -> Self {
232 Self {
233 builder: self.builder.property("sensitive", sensitive),
234 }
235 }
236
237 pub fn tooltip_markup(self, tooltip_markup: impl Into<glib::GString>) -> Self {
238 Self {
239 builder: self
240 .builder
241 .property("tooltip-markup", tooltip_markup.into()),
242 }
243 }
244
245 pub fn tooltip_text(self, tooltip_text: impl Into<glib::GString>) -> Self {
246 Self {
247 builder: self.builder.property("tooltip-text", tooltip_text.into()),
248 }
249 }
250
251 pub fn valign(self, valign: Align) -> Self {
252 Self {
253 builder: self.builder.property("valign", valign),
254 }
255 }
256
257 pub fn vexpand(self, vexpand: bool) -> Self {
258 Self {
259 builder: self.builder.property("vexpand", vexpand),
260 }
261 }
262
263 pub fn vexpand_set(self, vexpand_set: bool) -> Self {
264 Self {
265 builder: self.builder.property("vexpand-set", vexpand_set),
266 }
267 }
268
269 pub fn visible(self, visible: bool) -> Self {
270 Self {
271 builder: self.builder.property("visible", visible),
272 }
273 }
274
275 pub fn width_request(self, width_request: i32) -> Self {
276 Self {
277 builder: self.builder.property("width-request", width_request),
278 }
279 }
280
281 pub fn accessible_role(self, accessible_role: AccessibleRole) -> Self {
282 Self {
283 builder: self.builder.property("accessible-role", accessible_role),
284 }
285 }
286
287 pub fn orientation(self, orientation: Orientation) -> Self {
288 Self {
289 builder: self.builder.property("orientation", orientation),
290 }
291 }
292
293 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
296 pub fn build(self) -> Box {
297 assert_initialized_main_thread!();
298 self.builder.build()
299 }
300}
301
302pub trait BoxExt: IsA<Box> + 'static {
303 #[doc(alias = "gtk_box_append")]
304 fn append(&self, child: &impl IsA<Widget>) {
305 unsafe {
306 ffi::gtk_box_append(
307 self.as_ref().to_glib_none().0,
308 child.as_ref().to_glib_none().0,
309 );
310 }
311 }
312
313 #[cfg(feature = "v4_12")]
314 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
315 #[doc(alias = "gtk_box_get_baseline_child")]
316 #[doc(alias = "get_baseline_child")]
317 #[doc(alias = "baseline-child")]
318 fn baseline_child(&self) -> i32 {
319 unsafe { ffi::gtk_box_get_baseline_child(self.as_ref().to_glib_none().0) }
320 }
321
322 #[doc(alias = "gtk_box_get_baseline_position")]
323 #[doc(alias = "get_baseline_position")]
324 #[doc(alias = "baseline-position")]
325 fn baseline_position(&self) -> BaselinePosition {
326 unsafe {
327 from_glib(ffi::gtk_box_get_baseline_position(
328 self.as_ref().to_glib_none().0,
329 ))
330 }
331 }
332
333 #[doc(alias = "gtk_box_get_homogeneous")]
334 #[doc(alias = "get_homogeneous")]
335 #[doc(alias = "homogeneous")]
336 fn is_homogeneous(&self) -> bool {
337 unsafe { from_glib(ffi::gtk_box_get_homogeneous(self.as_ref().to_glib_none().0)) }
338 }
339
340 #[doc(alias = "gtk_box_get_spacing")]
341 #[doc(alias = "get_spacing")]
342 fn spacing(&self) -> i32 {
343 unsafe { ffi::gtk_box_get_spacing(self.as_ref().to_glib_none().0) }
344 }
345
346 #[doc(alias = "gtk_box_insert_child_after")]
347 fn insert_child_after(&self, child: &impl IsA<Widget>, sibling: Option<&impl IsA<Widget>>) {
348 unsafe {
349 ffi::gtk_box_insert_child_after(
350 self.as_ref().to_glib_none().0,
351 child.as_ref().to_glib_none().0,
352 sibling.map(|p| p.as_ref()).to_glib_none().0,
353 );
354 }
355 }
356
357 #[doc(alias = "gtk_box_prepend")]
358 fn prepend(&self, child: &impl IsA<Widget>) {
359 unsafe {
360 ffi::gtk_box_prepend(
361 self.as_ref().to_glib_none().0,
362 child.as_ref().to_glib_none().0,
363 );
364 }
365 }
366
367 #[doc(alias = "gtk_box_remove")]
368 fn remove(&self, child: &impl IsA<Widget>) {
369 unsafe {
370 ffi::gtk_box_remove(
371 self.as_ref().to_glib_none().0,
372 child.as_ref().to_glib_none().0,
373 );
374 }
375 }
376
377 #[doc(alias = "gtk_box_reorder_child_after")]
378 fn reorder_child_after(&self, child: &impl IsA<Widget>, sibling: Option<&impl IsA<Widget>>) {
379 unsafe {
380 ffi::gtk_box_reorder_child_after(
381 self.as_ref().to_glib_none().0,
382 child.as_ref().to_glib_none().0,
383 sibling.map(|p| p.as_ref()).to_glib_none().0,
384 );
385 }
386 }
387
388 #[cfg(feature = "v4_12")]
389 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
390 #[doc(alias = "gtk_box_set_baseline_child")]
391 #[doc(alias = "baseline-child")]
392 fn set_baseline_child(&self, child: i32) {
393 unsafe {
394 ffi::gtk_box_set_baseline_child(self.as_ref().to_glib_none().0, child);
395 }
396 }
397
398 #[doc(alias = "gtk_box_set_baseline_position")]
399 #[doc(alias = "baseline-position")]
400 fn set_baseline_position(&self, position: BaselinePosition) {
401 unsafe {
402 ffi::gtk_box_set_baseline_position(
403 self.as_ref().to_glib_none().0,
404 position.into_glib(),
405 );
406 }
407 }
408
409 #[doc(alias = "gtk_box_set_homogeneous")]
410 #[doc(alias = "homogeneous")]
411 fn set_homogeneous(&self, homogeneous: bool) {
412 unsafe {
413 ffi::gtk_box_set_homogeneous(self.as_ref().to_glib_none().0, homogeneous.into_glib());
414 }
415 }
416
417 #[doc(alias = "gtk_box_set_spacing")]
418 #[doc(alias = "spacing")]
419 fn set_spacing(&self, spacing: i32) {
420 unsafe {
421 ffi::gtk_box_set_spacing(self.as_ref().to_glib_none().0, spacing);
422 }
423 }
424
425 #[cfg(feature = "v4_12")]
426 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
427 #[doc(alias = "baseline-child")]
428 fn connect_baseline_child_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
429 unsafe extern "C" fn notify_baseline_child_trampoline<P: IsA<Box>, F: Fn(&P) + 'static>(
430 this: *mut ffi::GtkBox,
431 _param_spec: glib::ffi::gpointer,
432 f: glib::ffi::gpointer,
433 ) {
434 let f: &F = &*(f as *const F);
435 f(Box::from_glib_borrow(this).unsafe_cast_ref())
436 }
437 unsafe {
438 let f: Box_<F> = Box_::new(f);
439 connect_raw(
440 self.as_ptr() as *mut _,
441 c"notify::baseline-child".as_ptr() as *const _,
442 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
443 notify_baseline_child_trampoline::<Self, F> as *const (),
444 )),
445 Box_::into_raw(f),
446 )
447 }
448 }
449
450 #[doc(alias = "baseline-position")]
451 fn connect_baseline_position_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
452 unsafe extern "C" fn notify_baseline_position_trampoline<
453 P: IsA<Box>,
454 F: Fn(&P) + 'static,
455 >(
456 this: *mut ffi::GtkBox,
457 _param_spec: glib::ffi::gpointer,
458 f: glib::ffi::gpointer,
459 ) {
460 let f: &F = &*(f as *const F);
461 f(Box::from_glib_borrow(this).unsafe_cast_ref())
462 }
463 unsafe {
464 let f: Box_<F> = Box_::new(f);
465 connect_raw(
466 self.as_ptr() as *mut _,
467 c"notify::baseline-position".as_ptr() as *const _,
468 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
469 notify_baseline_position_trampoline::<Self, F> as *const (),
470 )),
471 Box_::into_raw(f),
472 )
473 }
474 }
475
476 #[doc(alias = "homogeneous")]
477 fn connect_homogeneous_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
478 unsafe extern "C" fn notify_homogeneous_trampoline<P: IsA<Box>, F: Fn(&P) + 'static>(
479 this: *mut ffi::GtkBox,
480 _param_spec: glib::ffi::gpointer,
481 f: glib::ffi::gpointer,
482 ) {
483 let f: &F = &*(f as *const F);
484 f(Box::from_glib_borrow(this).unsafe_cast_ref())
485 }
486 unsafe {
487 let f: Box_<F> = Box_::new(f);
488 connect_raw(
489 self.as_ptr() as *mut _,
490 c"notify::homogeneous".as_ptr() as *const _,
491 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
492 notify_homogeneous_trampoline::<Self, F> as *const (),
493 )),
494 Box_::into_raw(f),
495 )
496 }
497 }
498
499 #[doc(alias = "spacing")]
500 fn connect_spacing_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
501 unsafe extern "C" fn notify_spacing_trampoline<P: IsA<Box>, F: Fn(&P) + 'static>(
502 this: *mut ffi::GtkBox,
503 _param_spec: glib::ffi::gpointer,
504 f: glib::ffi::gpointer,
505 ) {
506 let f: &F = &*(f as *const F);
507 f(Box::from_glib_borrow(this).unsafe_cast_ref())
508 }
509 unsafe {
510 let f: Box_<F> = Box_::new(f);
511 connect_raw(
512 self.as_ptr() as *mut _,
513 c"notify::spacing".as_ptr() as *const _,
514 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
515 notify_spacing_trampoline::<Self, F> as *const (),
516 )),
517 Box_::into_raw(f),
518 )
519 }
520 }
521}
522
523impl<O: IsA<Box>> BoxExt for O {}