gtk4/auto/
toggle_button.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#![allow(deprecated)]
5
6use crate::{
7    ffi, Accessible, AccessibleRole, Actionable, Align, Buildable, Button, ConstraintTarget,
8    LayoutManager, Overflow, Widget,
9};
10use glib::{
11    object::ObjectType as _,
12    prelude::*,
13    signal::{connect_raw, SignalHandlerId},
14    translate::*,
15};
16use std::boxed::Box as Box_;
17
18glib::wrapper! {
19    #[doc(alias = "GtkToggleButton")]
20    pub struct ToggleButton(Object<ffi::GtkToggleButton, ffi::GtkToggleButtonClass>) @extends Button, Widget, @implements Accessible, Buildable, ConstraintTarget, Actionable;
21
22    match fn {
23        type_ => || ffi::gtk_toggle_button_get_type(),
24    }
25}
26
27impl ToggleButton {
28    pub const NONE: Option<&'static ToggleButton> = None;
29
30    #[doc(alias = "gtk_toggle_button_new")]
31    pub fn new() -> ToggleButton {
32        assert_initialized_main_thread!();
33        unsafe { Widget::from_glib_none(ffi::gtk_toggle_button_new()).unsafe_cast() }
34    }
35
36    #[doc(alias = "gtk_toggle_button_new_with_label")]
37    #[doc(alias = "new_with_label")]
38    pub fn with_label(label: &str) -> ToggleButton {
39        assert_initialized_main_thread!();
40        unsafe {
41            Widget::from_glib_none(ffi::gtk_toggle_button_new_with_label(
42                label.to_glib_none().0,
43            ))
44            .unsafe_cast()
45        }
46    }
47
48    #[doc(alias = "gtk_toggle_button_new_with_mnemonic")]
49    #[doc(alias = "new_with_mnemonic")]
50    pub fn with_mnemonic(label: &str) -> ToggleButton {
51        assert_initialized_main_thread!();
52        unsafe {
53            Widget::from_glib_none(ffi::gtk_toggle_button_new_with_mnemonic(
54                label.to_glib_none().0,
55            ))
56            .unsafe_cast()
57        }
58    }
59
60    // rustdoc-stripper-ignore-next
61    /// Creates a new builder-pattern struct instance to construct [`ToggleButton`] objects.
62    ///
63    /// This method returns an instance of [`ToggleButtonBuilder`](crate::builders::ToggleButtonBuilder) which can be used to create [`ToggleButton`] objects.
64    pub fn builder() -> ToggleButtonBuilder {
65        ToggleButtonBuilder::new()
66    }
67}
68
69impl Default for ToggleButton {
70    fn default() -> Self {
71        Self::new()
72    }
73}
74
75// rustdoc-stripper-ignore-next
76/// A [builder-pattern] type to construct [`ToggleButton`] objects.
77///
78/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
79#[must_use = "The builder must be built to be used"]
80pub struct ToggleButtonBuilder {
81    builder: glib::object::ObjectBuilder<'static, ToggleButton>,
82}
83
84impl ToggleButtonBuilder {
85    fn new() -> Self {
86        Self {
87            builder: glib::object::Object::builder(),
88        }
89    }
90
91    pub fn active(self, active: bool) -> Self {
92        Self {
93            builder: self.builder.property("active", active),
94        }
95    }
96
97    pub fn group(self, group: &impl IsA<ToggleButton>) -> Self {
98        Self {
99            builder: self.builder.property("group", group.clone().upcast()),
100        }
101    }
102
103    #[cfg(feature = "v4_12")]
104    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
105    pub fn can_shrink(self, can_shrink: bool) -> Self {
106        Self {
107            builder: self.builder.property("can-shrink", can_shrink),
108        }
109    }
110
111    pub fn child(self, child: &impl IsA<Widget>) -> Self {
112        Self {
113            builder: self.builder.property("child", child.clone().upcast()),
114        }
115    }
116
117    pub fn has_frame(self, has_frame: bool) -> Self {
118        Self {
119            builder: self.builder.property("has-frame", has_frame),
120        }
121    }
122
123    pub fn icon_name(self, icon_name: impl Into<glib::GString>) -> Self {
124        Self {
125            builder: self.builder.property("icon-name", icon_name.into()),
126        }
127    }
128
129    pub fn label(self, label: impl Into<glib::GString>) -> Self {
130        Self {
131            builder: self.builder.property("label", label.into()),
132        }
133    }
134
135    pub fn use_underline(self, use_underline: bool) -> Self {
136        Self {
137            builder: self.builder.property("use-underline", use_underline),
138        }
139    }
140
141    pub fn can_focus(self, can_focus: bool) -> Self {
142        Self {
143            builder: self.builder.property("can-focus", can_focus),
144        }
145    }
146
147    pub fn can_target(self, can_target: bool) -> Self {
148        Self {
149            builder: self.builder.property("can-target", can_target),
150        }
151    }
152
153    pub fn css_classes(self, css_classes: impl Into<glib::StrV>) -> Self {
154        Self {
155            builder: self.builder.property("css-classes", css_classes.into()),
156        }
157    }
158
159    pub fn css_name(self, css_name: impl Into<glib::GString>) -> Self {
160        Self {
161            builder: self.builder.property("css-name", css_name.into()),
162        }
163    }
164
165    pub fn cursor(self, cursor: &gdk::Cursor) -> Self {
166        Self {
167            builder: self.builder.property("cursor", cursor.clone()),
168        }
169    }
170
171    pub fn focus_on_click(self, focus_on_click: bool) -> Self {
172        Self {
173            builder: self.builder.property("focus-on-click", focus_on_click),
174        }
175    }
176
177    pub fn focusable(self, focusable: bool) -> Self {
178        Self {
179            builder: self.builder.property("focusable", focusable),
180        }
181    }
182
183    pub fn halign(self, halign: Align) -> Self {
184        Self {
185            builder: self.builder.property("halign", halign),
186        }
187    }
188
189    pub fn has_tooltip(self, has_tooltip: bool) -> Self {
190        Self {
191            builder: self.builder.property("has-tooltip", has_tooltip),
192        }
193    }
194
195    pub fn height_request(self, height_request: i32) -> Self {
196        Self {
197            builder: self.builder.property("height-request", height_request),
198        }
199    }
200
201    pub fn hexpand(self, hexpand: bool) -> Self {
202        Self {
203            builder: self.builder.property("hexpand", hexpand),
204        }
205    }
206
207    pub fn hexpand_set(self, hexpand_set: bool) -> Self {
208        Self {
209            builder: self.builder.property("hexpand-set", hexpand_set),
210        }
211    }
212
213    pub fn layout_manager(self, layout_manager: &impl IsA<LayoutManager>) -> Self {
214        Self {
215            builder: self
216                .builder
217                .property("layout-manager", layout_manager.clone().upcast()),
218        }
219    }
220
221    #[cfg(feature = "v4_18")]
222    #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
223    pub fn limit_events(self, limit_events: bool) -> Self {
224        Self {
225            builder: self.builder.property("limit-events", limit_events),
226        }
227    }
228
229    pub fn margin_bottom(self, margin_bottom: i32) -> Self {
230        Self {
231            builder: self.builder.property("margin-bottom", margin_bottom),
232        }
233    }
234
235    pub fn margin_end(self, margin_end: i32) -> Self {
236        Self {
237            builder: self.builder.property("margin-end", margin_end),
238        }
239    }
240
241    pub fn margin_start(self, margin_start: i32) -> Self {
242        Self {
243            builder: self.builder.property("margin-start", margin_start),
244        }
245    }
246
247    pub fn margin_top(self, margin_top: i32) -> Self {
248        Self {
249            builder: self.builder.property("margin-top", margin_top),
250        }
251    }
252
253    pub fn name(self, name: impl Into<glib::GString>) -> Self {
254        Self {
255            builder: self.builder.property("name", name.into()),
256        }
257    }
258
259    pub fn opacity(self, opacity: f64) -> Self {
260        Self {
261            builder: self.builder.property("opacity", opacity),
262        }
263    }
264
265    pub fn overflow(self, overflow: Overflow) -> Self {
266        Self {
267            builder: self.builder.property("overflow", overflow),
268        }
269    }
270
271    pub fn receives_default(self, receives_default: bool) -> Self {
272        Self {
273            builder: self.builder.property("receives-default", receives_default),
274        }
275    }
276
277    pub fn sensitive(self, sensitive: bool) -> Self {
278        Self {
279            builder: self.builder.property("sensitive", sensitive),
280        }
281    }
282
283    pub fn tooltip_markup(self, tooltip_markup: impl Into<glib::GString>) -> Self {
284        Self {
285            builder: self
286                .builder
287                .property("tooltip-markup", tooltip_markup.into()),
288        }
289    }
290
291    pub fn tooltip_text(self, tooltip_text: impl Into<glib::GString>) -> Self {
292        Self {
293            builder: self.builder.property("tooltip-text", tooltip_text.into()),
294        }
295    }
296
297    pub fn valign(self, valign: Align) -> Self {
298        Self {
299            builder: self.builder.property("valign", valign),
300        }
301    }
302
303    pub fn vexpand(self, vexpand: bool) -> Self {
304        Self {
305            builder: self.builder.property("vexpand", vexpand),
306        }
307    }
308
309    pub fn vexpand_set(self, vexpand_set: bool) -> Self {
310        Self {
311            builder: self.builder.property("vexpand-set", vexpand_set),
312        }
313    }
314
315    pub fn visible(self, visible: bool) -> Self {
316        Self {
317            builder: self.builder.property("visible", visible),
318        }
319    }
320
321    pub fn width_request(self, width_request: i32) -> Self {
322        Self {
323            builder: self.builder.property("width-request", width_request),
324        }
325    }
326
327    pub fn accessible_role(self, accessible_role: AccessibleRole) -> Self {
328        Self {
329            builder: self.builder.property("accessible-role", accessible_role),
330        }
331    }
332
333    pub fn action_name(self, action_name: impl Into<glib::GString>) -> Self {
334        Self {
335            builder: self.builder.property("action-name", action_name.into()),
336        }
337    }
338
339    pub fn action_target(self, action_target: &glib::Variant) -> Self {
340        Self {
341            builder: self
342                .builder
343                .property("action-target", action_target.clone()),
344        }
345    }
346
347    // rustdoc-stripper-ignore-next
348    /// Build the [`ToggleButton`].
349    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
350    pub fn build(self) -> ToggleButton {
351        assert_initialized_main_thread!();
352        self.builder.build()
353    }
354}
355
356pub trait ToggleButtonExt: IsA<ToggleButton> + 'static {
357    #[doc(alias = "gtk_toggle_button_get_active")]
358    #[doc(alias = "get_active")]
359    #[doc(alias = "active")]
360    fn is_active(&self) -> bool {
361        unsafe {
362            from_glib(ffi::gtk_toggle_button_get_active(
363                self.as_ref().to_glib_none().0,
364            ))
365        }
366    }
367
368    #[doc(alias = "gtk_toggle_button_set_active")]
369    #[doc(alias = "active")]
370    fn set_active(&self, is_active: bool) {
371        unsafe {
372            ffi::gtk_toggle_button_set_active(
373                self.as_ref().to_glib_none().0,
374                is_active.into_glib(),
375            );
376        }
377    }
378
379    #[doc(alias = "gtk_toggle_button_set_group")]
380    #[doc(alias = "group")]
381    fn set_group(&self, group: Option<&impl IsA<ToggleButton>>) {
382        unsafe {
383            ffi::gtk_toggle_button_set_group(
384                self.as_ref().to_glib_none().0,
385                group.map(|p| p.as_ref()).to_glib_none().0,
386            );
387        }
388    }
389
390    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
391    #[allow(deprecated)]
392    #[doc(alias = "gtk_toggle_button_toggled")]
393    fn toggled(&self) {
394        unsafe {
395            ffi::gtk_toggle_button_toggled(self.as_ref().to_glib_none().0);
396        }
397    }
398
399    #[doc(alias = "toggled")]
400    fn connect_toggled<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
401        unsafe extern "C" fn toggled_trampoline<P: IsA<ToggleButton>, F: Fn(&P) + 'static>(
402            this: *mut ffi::GtkToggleButton,
403            f: glib::ffi::gpointer,
404        ) {
405            let f: &F = &*(f as *const F);
406            f(ToggleButton::from_glib_borrow(this).unsafe_cast_ref())
407        }
408        unsafe {
409            let f: Box_<F> = Box_::new(f);
410            connect_raw(
411                self.as_ptr() as *mut _,
412                c"toggled".as_ptr() as *const _,
413                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
414                    toggled_trampoline::<Self, F> as *const (),
415                )),
416                Box_::into_raw(f),
417            )
418        }
419    }
420
421    #[doc(alias = "active")]
422    fn connect_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
423        unsafe extern "C" fn notify_active_trampoline<P: IsA<ToggleButton>, F: Fn(&P) + 'static>(
424            this: *mut ffi::GtkToggleButton,
425            _param_spec: glib::ffi::gpointer,
426            f: glib::ffi::gpointer,
427        ) {
428            let f: &F = &*(f as *const F);
429            f(ToggleButton::from_glib_borrow(this).unsafe_cast_ref())
430        }
431        unsafe {
432            let f: Box_<F> = Box_::new(f);
433            connect_raw(
434                self.as_ptr() as *mut _,
435                c"notify::active".as_ptr() as *const _,
436                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
437                    notify_active_trampoline::<Self, F> as *const (),
438                )),
439                Box_::into_raw(f),
440            )
441        }
442    }
443
444    #[doc(alias = "group")]
445    fn connect_group_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
446        unsafe extern "C" fn notify_group_trampoline<P: IsA<ToggleButton>, F: Fn(&P) + 'static>(
447            this: *mut ffi::GtkToggleButton,
448            _param_spec: glib::ffi::gpointer,
449            f: glib::ffi::gpointer,
450        ) {
451            let f: &F = &*(f as *const F);
452            f(ToggleButton::from_glib_borrow(this).unsafe_cast_ref())
453        }
454        unsafe {
455            let f: Box_<F> = Box_::new(f);
456            connect_raw(
457                self.as_ptr() as *mut _,
458                c"notify::group".as_ptr() as *const _,
459                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
460                    notify_group_trampoline::<Self, F> as *const (),
461                )),
462                Box_::into_raw(f),
463            )
464        }
465    }
466}
467
468impl<O: IsA<ToggleButton>> ToggleButtonExt for O {}