1use crate::{
6 ffi, Accessible, AccessibleRole, Actionable, Align, Buildable, ConstraintTarget, LayoutManager,
7 Overflow, Widget,
8};
9use glib::{
10 object::ObjectType as _,
11 prelude::*,
12 signal::{connect_raw, SignalHandlerId},
13 translate::*,
14};
15use std::boxed::Box as Box_;
16
17glib::wrapper! {
18 #[doc(alias = "GtkButton")]
19 pub struct Button(Object<ffi::GtkButton, ffi::GtkButtonClass>) @extends Widget, @implements Accessible, Buildable, ConstraintTarget, Actionable;
20
21 match fn {
22 type_ => || ffi::gtk_button_get_type(),
23 }
24}
25
26impl Button {
27 pub const NONE: Option<&'static Button> = None;
28
29 #[doc(alias = "gtk_button_new")]
30 pub fn new() -> Button {
31 assert_initialized_main_thread!();
32 unsafe { Widget::from_glib_none(ffi::gtk_button_new()).unsafe_cast() }
33 }
34
35 #[doc(alias = "gtk_button_new_from_icon_name")]
36 #[doc(alias = "new_from_icon_name")]
37 pub fn from_icon_name(icon_name: &str) -> Button {
38 assert_initialized_main_thread!();
39 unsafe {
40 Widget::from_glib_none(ffi::gtk_button_new_from_icon_name(
41 icon_name.to_glib_none().0,
42 ))
43 .unsafe_cast()
44 }
45 }
46
47 #[doc(alias = "gtk_button_new_with_label")]
48 #[doc(alias = "new_with_label")]
49 pub fn with_label(label: &str) -> Button {
50 assert_initialized_main_thread!();
51 unsafe {
52 Widget::from_glib_none(ffi::gtk_button_new_with_label(label.to_glib_none().0))
53 .unsafe_cast()
54 }
55 }
56
57 #[doc(alias = "gtk_button_new_with_mnemonic")]
58 #[doc(alias = "new_with_mnemonic")]
59 pub fn with_mnemonic(label: &str) -> Button {
60 assert_initialized_main_thread!();
61 unsafe {
62 Widget::from_glib_none(ffi::gtk_button_new_with_mnemonic(label.to_glib_none().0))
63 .unsafe_cast()
64 }
65 }
66
67 pub fn builder() -> ButtonBuilder {
72 ButtonBuilder::new()
73 }
74}
75
76impl Default for Button {
77 fn default() -> Self {
78 Self::new()
79 }
80}
81
82#[must_use = "The builder must be built to be used"]
87pub struct ButtonBuilder {
88 builder: glib::object::ObjectBuilder<'static, Button>,
89}
90
91impl ButtonBuilder {
92 fn new() -> Self {
93 Self {
94 builder: glib::object::Object::builder(),
95 }
96 }
97
98 #[cfg(feature = "v4_12")]
99 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
100 pub fn can_shrink(self, can_shrink: bool) -> Self {
101 Self {
102 builder: self.builder.property("can-shrink", can_shrink),
103 }
104 }
105
106 pub fn child(self, child: &impl IsA<Widget>) -> Self {
107 Self {
108 builder: self.builder.property("child", child.clone().upcast()),
109 }
110 }
111
112 pub fn has_frame(self, has_frame: bool) -> Self {
113 Self {
114 builder: self.builder.property("has-frame", has_frame),
115 }
116 }
117
118 pub fn icon_name(self, icon_name: impl Into<glib::GString>) -> Self {
119 Self {
120 builder: self.builder.property("icon-name", icon_name.into()),
121 }
122 }
123
124 pub fn label(self, label: impl Into<glib::GString>) -> Self {
125 Self {
126 builder: self.builder.property("label", label.into()),
127 }
128 }
129
130 pub fn use_underline(self, use_underline: bool) -> Self {
131 Self {
132 builder: self.builder.property("use-underline", use_underline),
133 }
134 }
135
136 pub fn can_focus(self, can_focus: bool) -> Self {
137 Self {
138 builder: self.builder.property("can-focus", can_focus),
139 }
140 }
141
142 pub fn can_target(self, can_target: bool) -> Self {
143 Self {
144 builder: self.builder.property("can-target", can_target),
145 }
146 }
147
148 pub fn css_classes(self, css_classes: impl Into<glib::StrV>) -> Self {
149 Self {
150 builder: self.builder.property("css-classes", css_classes.into()),
151 }
152 }
153
154 pub fn css_name(self, css_name: impl Into<glib::GString>) -> Self {
155 Self {
156 builder: self.builder.property("css-name", css_name.into()),
157 }
158 }
159
160 pub fn cursor(self, cursor: &gdk::Cursor) -> Self {
161 Self {
162 builder: self.builder.property("cursor", cursor.clone()),
163 }
164 }
165
166 pub fn focus_on_click(self, focus_on_click: bool) -> Self {
167 Self {
168 builder: self.builder.property("focus-on-click", focus_on_click),
169 }
170 }
171
172 pub fn focusable(self, focusable: bool) -> Self {
173 Self {
174 builder: self.builder.property("focusable", focusable),
175 }
176 }
177
178 pub fn halign(self, halign: Align) -> Self {
179 Self {
180 builder: self.builder.property("halign", halign),
181 }
182 }
183
184 pub fn has_tooltip(self, has_tooltip: bool) -> Self {
185 Self {
186 builder: self.builder.property("has-tooltip", has_tooltip),
187 }
188 }
189
190 pub fn height_request(self, height_request: i32) -> Self {
191 Self {
192 builder: self.builder.property("height-request", height_request),
193 }
194 }
195
196 pub fn hexpand(self, hexpand: bool) -> Self {
197 Self {
198 builder: self.builder.property("hexpand", hexpand),
199 }
200 }
201
202 pub fn hexpand_set(self, hexpand_set: bool) -> Self {
203 Self {
204 builder: self.builder.property("hexpand-set", hexpand_set),
205 }
206 }
207
208 pub fn layout_manager(self, layout_manager: &impl IsA<LayoutManager>) -> Self {
209 Self {
210 builder: self
211 .builder
212 .property("layout-manager", layout_manager.clone().upcast()),
213 }
214 }
215
216 #[cfg(feature = "v4_18")]
217 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
218 pub fn limit_events(self, limit_events: bool) -> Self {
219 Self {
220 builder: self.builder.property("limit-events", limit_events),
221 }
222 }
223
224 pub fn margin_bottom(self, margin_bottom: i32) -> Self {
225 Self {
226 builder: self.builder.property("margin-bottom", margin_bottom),
227 }
228 }
229
230 pub fn margin_end(self, margin_end: i32) -> Self {
231 Self {
232 builder: self.builder.property("margin-end", margin_end),
233 }
234 }
235
236 pub fn margin_start(self, margin_start: i32) -> Self {
237 Self {
238 builder: self.builder.property("margin-start", margin_start),
239 }
240 }
241
242 pub fn margin_top(self, margin_top: i32) -> Self {
243 Self {
244 builder: self.builder.property("margin-top", margin_top),
245 }
246 }
247
248 pub fn name(self, name: impl Into<glib::GString>) -> Self {
249 Self {
250 builder: self.builder.property("name", name.into()),
251 }
252 }
253
254 pub fn opacity(self, opacity: f64) -> Self {
255 Self {
256 builder: self.builder.property("opacity", opacity),
257 }
258 }
259
260 pub fn overflow(self, overflow: Overflow) -> Self {
261 Self {
262 builder: self.builder.property("overflow", overflow),
263 }
264 }
265
266 pub fn receives_default(self, receives_default: bool) -> Self {
267 Self {
268 builder: self.builder.property("receives-default", receives_default),
269 }
270 }
271
272 pub fn sensitive(self, sensitive: bool) -> Self {
273 Self {
274 builder: self.builder.property("sensitive", sensitive),
275 }
276 }
277
278 pub fn tooltip_markup(self, tooltip_markup: impl Into<glib::GString>) -> Self {
279 Self {
280 builder: self
281 .builder
282 .property("tooltip-markup", tooltip_markup.into()),
283 }
284 }
285
286 pub fn tooltip_text(self, tooltip_text: impl Into<glib::GString>) -> Self {
287 Self {
288 builder: self.builder.property("tooltip-text", tooltip_text.into()),
289 }
290 }
291
292 pub fn valign(self, valign: Align) -> Self {
293 Self {
294 builder: self.builder.property("valign", valign),
295 }
296 }
297
298 pub fn vexpand(self, vexpand: bool) -> Self {
299 Self {
300 builder: self.builder.property("vexpand", vexpand),
301 }
302 }
303
304 pub fn vexpand_set(self, vexpand_set: bool) -> Self {
305 Self {
306 builder: self.builder.property("vexpand-set", vexpand_set),
307 }
308 }
309
310 pub fn visible(self, visible: bool) -> Self {
311 Self {
312 builder: self.builder.property("visible", visible),
313 }
314 }
315
316 pub fn width_request(self, width_request: i32) -> Self {
317 Self {
318 builder: self.builder.property("width-request", width_request),
319 }
320 }
321
322 pub fn accessible_role(self, accessible_role: AccessibleRole) -> Self {
323 Self {
324 builder: self.builder.property("accessible-role", accessible_role),
325 }
326 }
327
328 pub fn action_name(self, action_name: impl Into<glib::GString>) -> Self {
329 Self {
330 builder: self.builder.property("action-name", action_name.into()),
331 }
332 }
333
334 pub fn action_target(self, action_target: &glib::Variant) -> Self {
335 Self {
336 builder: self
337 .builder
338 .property("action-target", action_target.clone()),
339 }
340 }
341
342 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
345 pub fn build(self) -> Button {
346 assert_initialized_main_thread!();
347 self.builder.build()
348 }
349}
350
351pub trait ButtonExt: IsA<Button> + 'static {
352 #[cfg(feature = "v4_12")]
353 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
354 #[doc(alias = "gtk_button_get_can_shrink")]
355 #[doc(alias = "get_can_shrink")]
356 #[doc(alias = "can-shrink")]
357 fn can_shrink(&self) -> bool {
358 unsafe {
359 from_glib(ffi::gtk_button_get_can_shrink(
360 self.as_ref().to_glib_none().0,
361 ))
362 }
363 }
364
365 #[doc(alias = "gtk_button_get_child")]
366 #[doc(alias = "get_child")]
367 fn child(&self) -> Option<Widget> {
368 unsafe { from_glib_none(ffi::gtk_button_get_child(self.as_ref().to_glib_none().0)) }
369 }
370
371 #[doc(alias = "gtk_button_get_has_frame")]
372 #[doc(alias = "get_has_frame")]
373 #[doc(alias = "has-frame")]
374 fn has_frame(&self) -> bool {
375 unsafe {
376 from_glib(ffi::gtk_button_get_has_frame(
377 self.as_ref().to_glib_none().0,
378 ))
379 }
380 }
381
382 #[doc(alias = "gtk_button_get_icon_name")]
383 #[doc(alias = "get_icon_name")]
384 #[doc(alias = "icon-name")]
385 fn icon_name(&self) -> Option<glib::GString> {
386 unsafe {
387 from_glib_none(ffi::gtk_button_get_icon_name(
388 self.as_ref().to_glib_none().0,
389 ))
390 }
391 }
392
393 #[doc(alias = "gtk_button_get_label")]
394 #[doc(alias = "get_label")]
395 fn label(&self) -> Option<glib::GString> {
396 unsafe { from_glib_none(ffi::gtk_button_get_label(self.as_ref().to_glib_none().0)) }
397 }
398
399 #[doc(alias = "gtk_button_get_use_underline")]
400 #[doc(alias = "get_use_underline")]
401 #[doc(alias = "use-underline")]
402 fn uses_underline(&self) -> bool {
403 unsafe {
404 from_glib(ffi::gtk_button_get_use_underline(
405 self.as_ref().to_glib_none().0,
406 ))
407 }
408 }
409
410 #[cfg(feature = "v4_12")]
411 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
412 #[doc(alias = "gtk_button_set_can_shrink")]
413 #[doc(alias = "can-shrink")]
414 fn set_can_shrink(&self, can_shrink: bool) {
415 unsafe {
416 ffi::gtk_button_set_can_shrink(self.as_ref().to_glib_none().0, can_shrink.into_glib());
417 }
418 }
419
420 #[doc(alias = "gtk_button_set_child")]
421 #[doc(alias = "child")]
422 fn set_child(&self, child: Option<&impl IsA<Widget>>) {
423 unsafe {
424 ffi::gtk_button_set_child(
425 self.as_ref().to_glib_none().0,
426 child.map(|p| p.as_ref()).to_glib_none().0,
427 );
428 }
429 }
430
431 #[doc(alias = "gtk_button_set_has_frame")]
432 #[doc(alias = "has-frame")]
433 fn set_has_frame(&self, has_frame: bool) {
434 unsafe {
435 ffi::gtk_button_set_has_frame(self.as_ref().to_glib_none().0, has_frame.into_glib());
436 }
437 }
438
439 #[doc(alias = "gtk_button_set_icon_name")]
440 #[doc(alias = "icon-name")]
441 fn set_icon_name(&self, icon_name: &str) {
442 unsafe {
443 ffi::gtk_button_set_icon_name(
444 self.as_ref().to_glib_none().0,
445 icon_name.to_glib_none().0,
446 );
447 }
448 }
449
450 #[doc(alias = "gtk_button_set_label")]
451 #[doc(alias = "label")]
452 fn set_label(&self, label: &str) {
453 unsafe {
454 ffi::gtk_button_set_label(self.as_ref().to_glib_none().0, label.to_glib_none().0);
455 }
456 }
457
458 #[doc(alias = "gtk_button_set_use_underline")]
459 #[doc(alias = "use-underline")]
460 fn set_use_underline(&self, use_underline: bool) {
461 unsafe {
462 ffi::gtk_button_set_use_underline(
463 self.as_ref().to_glib_none().0,
464 use_underline.into_glib(),
465 );
466 }
467 }
468
469 #[doc(alias = "activate")]
470 fn connect_activate<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
471 unsafe extern "C" fn activate_trampoline<P: IsA<Button>, F: Fn(&P) + 'static>(
472 this: *mut ffi::GtkButton,
473 f: glib::ffi::gpointer,
474 ) {
475 let f: &F = &*(f as *const F);
476 f(Button::from_glib_borrow(this).unsafe_cast_ref())
477 }
478 unsafe {
479 let f: Box_<F> = Box_::new(f);
480 connect_raw(
481 self.as_ptr() as *mut _,
482 c"activate".as_ptr() as *const _,
483 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
484 activate_trampoline::<Self, F> as *const (),
485 )),
486 Box_::into_raw(f),
487 )
488 }
489 }
490
491 fn emit_activate(&self) {
492 self.emit_by_name::<()>("activate", &[]);
493 }
494
495 #[doc(alias = "clicked")]
496 fn connect_clicked<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
497 unsafe extern "C" fn clicked_trampoline<P: IsA<Button>, F: Fn(&P) + 'static>(
498 this: *mut ffi::GtkButton,
499 f: glib::ffi::gpointer,
500 ) {
501 let f: &F = &*(f as *const F);
502 f(Button::from_glib_borrow(this).unsafe_cast_ref())
503 }
504 unsafe {
505 let f: Box_<F> = Box_::new(f);
506 connect_raw(
507 self.as_ptr() as *mut _,
508 c"clicked".as_ptr() as *const _,
509 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
510 clicked_trampoline::<Self, F> as *const (),
511 )),
512 Box_::into_raw(f),
513 )
514 }
515 }
516
517 fn emit_clicked(&self) {
518 self.emit_by_name::<()>("clicked", &[]);
519 }
520
521 #[cfg(feature = "v4_12")]
522 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
523 #[doc(alias = "can-shrink")]
524 fn connect_can_shrink_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
525 unsafe extern "C" fn notify_can_shrink_trampoline<P: IsA<Button>, F: Fn(&P) + 'static>(
526 this: *mut ffi::GtkButton,
527 _param_spec: glib::ffi::gpointer,
528 f: glib::ffi::gpointer,
529 ) {
530 let f: &F = &*(f as *const F);
531 f(Button::from_glib_borrow(this).unsafe_cast_ref())
532 }
533 unsafe {
534 let f: Box_<F> = Box_::new(f);
535 connect_raw(
536 self.as_ptr() as *mut _,
537 c"notify::can-shrink".as_ptr() as *const _,
538 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
539 notify_can_shrink_trampoline::<Self, F> as *const (),
540 )),
541 Box_::into_raw(f),
542 )
543 }
544 }
545
546 #[doc(alias = "child")]
547 fn connect_child_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
548 unsafe extern "C" fn notify_child_trampoline<P: IsA<Button>, F: Fn(&P) + 'static>(
549 this: *mut ffi::GtkButton,
550 _param_spec: glib::ffi::gpointer,
551 f: glib::ffi::gpointer,
552 ) {
553 let f: &F = &*(f as *const F);
554 f(Button::from_glib_borrow(this).unsafe_cast_ref())
555 }
556 unsafe {
557 let f: Box_<F> = Box_::new(f);
558 connect_raw(
559 self.as_ptr() as *mut _,
560 c"notify::child".as_ptr() as *const _,
561 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
562 notify_child_trampoline::<Self, F> as *const (),
563 )),
564 Box_::into_raw(f),
565 )
566 }
567 }
568
569 #[doc(alias = "has-frame")]
570 fn connect_has_frame_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
571 unsafe extern "C" fn notify_has_frame_trampoline<P: IsA<Button>, F: Fn(&P) + 'static>(
572 this: *mut ffi::GtkButton,
573 _param_spec: glib::ffi::gpointer,
574 f: glib::ffi::gpointer,
575 ) {
576 let f: &F = &*(f as *const F);
577 f(Button::from_glib_borrow(this).unsafe_cast_ref())
578 }
579 unsafe {
580 let f: Box_<F> = Box_::new(f);
581 connect_raw(
582 self.as_ptr() as *mut _,
583 c"notify::has-frame".as_ptr() as *const _,
584 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
585 notify_has_frame_trampoline::<Self, F> as *const (),
586 )),
587 Box_::into_raw(f),
588 )
589 }
590 }
591
592 #[doc(alias = "icon-name")]
593 fn connect_icon_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
594 unsafe extern "C" fn notify_icon_name_trampoline<P: IsA<Button>, F: Fn(&P) + 'static>(
595 this: *mut ffi::GtkButton,
596 _param_spec: glib::ffi::gpointer,
597 f: glib::ffi::gpointer,
598 ) {
599 let f: &F = &*(f as *const F);
600 f(Button::from_glib_borrow(this).unsafe_cast_ref())
601 }
602 unsafe {
603 let f: Box_<F> = Box_::new(f);
604 connect_raw(
605 self.as_ptr() as *mut _,
606 c"notify::icon-name".as_ptr() as *const _,
607 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
608 notify_icon_name_trampoline::<Self, F> as *const (),
609 )),
610 Box_::into_raw(f),
611 )
612 }
613 }
614
615 #[doc(alias = "label")]
616 fn connect_label_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
617 unsafe extern "C" fn notify_label_trampoline<P: IsA<Button>, F: Fn(&P) + 'static>(
618 this: *mut ffi::GtkButton,
619 _param_spec: glib::ffi::gpointer,
620 f: glib::ffi::gpointer,
621 ) {
622 let f: &F = &*(f as *const F);
623 f(Button::from_glib_borrow(this).unsafe_cast_ref())
624 }
625 unsafe {
626 let f: Box_<F> = Box_::new(f);
627 connect_raw(
628 self.as_ptr() as *mut _,
629 c"notify::label".as_ptr() as *const _,
630 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
631 notify_label_trampoline::<Self, F> as *const (),
632 )),
633 Box_::into_raw(f),
634 )
635 }
636 }
637
638 #[doc(alias = "use-underline")]
639 fn connect_use_underline_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
640 unsafe extern "C" fn notify_use_underline_trampoline<
641 P: IsA<Button>,
642 F: Fn(&P) + 'static,
643 >(
644 this: *mut ffi::GtkButton,
645 _param_spec: glib::ffi::gpointer,
646 f: glib::ffi::gpointer,
647 ) {
648 let f: &F = &*(f as *const F);
649 f(Button::from_glib_borrow(this).unsafe_cast_ref())
650 }
651 unsafe {
652 let f: Box_<F> = Box_::new(f);
653 connect_raw(
654 self.as_ptr() as *mut _,
655 c"notify::use-underline".as_ptr() as *const _,
656 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
657 notify_use_underline_trampoline::<Self, F> as *const (),
658 )),
659 Box_::into_raw(f),
660 )
661 }
662 }
663}
664
665impl<O: IsA<Button>> ButtonExt for O {}