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
6#[cfg(feature = "v4_10")]
7#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
8use crate::Accessible;
9use crate::{
10 AccessibleRole, Actionable, Align, Buildable, Button, ConstraintTarget, LayoutManager,
11 Overflow, Widget, ffi,
12};
13use glib::{
14 object::ObjectType as _,
15 prelude::*,
16 signal::{SignalHandlerId, connect_raw},
17 translate::*,
18};
19use std::boxed::Box as Box_;
20
21#[cfg(feature = "v4_10")]
22#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
23glib::wrapper! {
24 /// Shows a button which remains “pressed-in” when clicked.
25 ///
26 /// <picture>
27 /// <source srcset="toggle-button-dark.png" media="(prefers-color-scheme: dark)">
28 /// <img alt="Example GtkToggleButtons" src="toggle-button.png">
29 /// </picture>
30 ///
31 /// Clicking again will cause the toggle button to return to its normal state.
32 ///
33 /// A toggle button is created by calling either [`new()`][Self::new()] or
34 /// [`with_label()`][Self::with_label()]. If using the former, it is advisable
35 /// to pack a widget, (such as a [`Label`][crate::Label] and/or a [`Image`][crate::Image]), into the toggle
36 /// button’s container. (See [`Button`][crate::Button] for more information).
37 ///
38 /// The state of a [`ToggleButton`][crate::ToggleButton] can be set specifically using
39 /// [`ToggleButtonExt::set_active()`][crate::prelude::ToggleButtonExt::set_active()], and retrieved using
40 /// [`ToggleButtonExt::is_active()`][crate::prelude::ToggleButtonExt::is_active()].
41 ///
42 /// ## Grouping
43 ///
44 /// Toggle buttons can be grouped together, to form mutually exclusive
45 /// groups - only one of the buttons can be toggled at a time, and toggling
46 /// another one will switch the currently toggled one off.
47 ///
48 /// To add a [`ToggleButton`][crate::ToggleButton] to a group, use [`ToggleButtonExt::set_group()`][crate::prelude::ToggleButtonExt::set_group()].
49 ///
50 /// ## CSS nodes
51 ///
52 /// [`ToggleButton`][crate::ToggleButton] has a single CSS node with name button. To differentiate
53 /// it from a plain [`Button`][crate::Button], it gets the `.toggle` style class.
54 ///
55 /// ## Accessibility
56 ///
57 /// [`ToggleButton`][crate::ToggleButton] uses the [enum@Gtk.AccessibleRole.toggle_button] role.
58 ///
59 /// ## Creating two [`ToggleButton`][crate::ToggleButton] widgets.
60 ///
61 /// **⚠️ The following code is in c ⚠️**
62 ///
63 /// ```c
64 /// static void
65 /// output_state (GtkToggleButton *source,
66 /// gpointer user_data)
67 /// {
68 /// g_print ("Toggle button "%s" is active: %s",
69 /// gtk_button_get_label (GTK_BUTTON (source)),
70 /// gtk_toggle_button_get_active (source) ? "Yes" : "No");
71 /// }
72 ///
73 /// static void
74 /// make_toggles (void)
75 /// {
76 /// GtkWidget *window, *toggle1, *toggle2;
77 /// GtkWidget *box;
78 /// const char *text;
79 ///
80 /// window = gtk_window_new ();
81 /// box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
82 ///
83 /// text = "Hi, I’m toggle button one";
84 /// toggle1 = gtk_toggle_button_new_with_label (text);
85 ///
86 /// g_signal_connect (toggle1, "toggled",
87 /// G_CALLBACK (output_state),
88 /// NULL);
89 /// gtk_box_append (GTK_BOX (box), toggle1);
90 ///
91 /// text = "Hi, I’m toggle button two";
92 /// toggle2 = gtk_toggle_button_new_with_label (text);
93 /// g_signal_connect (toggle2, "toggled",
94 /// G_CALLBACK (output_state),
95 /// NULL);
96 /// gtk_box_append (GTK_BOX (box), toggle2);
97 ///
98 /// gtk_window_set_child (GTK_WINDOW (window), box);
99 /// gtk_window_present (GTK_WINDOW (window));
100 /// }
101 /// ```
102 ///
103 /// ## Properties
104 ///
105 ///
106 /// #### `active`
107 /// If the toggle button should be pressed in.
108 ///
109 /// Readable | Writable
110 ///
111 ///
112 /// #### `group`
113 /// The toggle button whose group this widget belongs to.
114 ///
115 /// Writable
116 /// <details><summary><h4>Button</h4></summary>
117 ///
118 ///
119 /// #### `can-shrink`
120 /// Whether the size of the button can be made smaller than the natural
121 /// size of its contents.
122 ///
123 /// For text buttons, setting this property will allow ellipsizing the label.
124 ///
125 /// If the contents of a button are an icon or a custom widget, setting this
126 /// property has no effect.
127 ///
128 /// Readable | Writable
129 ///
130 ///
131 /// #### `child`
132 /// The child widget.
133 ///
134 /// Readable | Writable
135 ///
136 ///
137 /// #### `has-frame`
138 /// Whether the button has a frame.
139 ///
140 /// Readable | Writable
141 ///
142 ///
143 /// #### `icon-name`
144 /// The name of the icon used to automatically populate the button.
145 ///
146 /// Readable | Writable
147 ///
148 ///
149 /// #### `label`
150 /// Text of the label inside the button, if the button contains a label widget.
151 ///
152 /// Readable | Writable
153 ///
154 ///
155 /// #### `use-underline`
156 /// If set, an underline in the text indicates that the following character is
157 /// to be used as mnemonic.
158 ///
159 /// Readable | Writable
160 /// </details>
161 /// <details><summary><h4>Widget</h4></summary>
162 ///
163 ///
164 /// #### `can-focus`
165 /// Whether the widget or any of its descendents can accept
166 /// the input focus.
167 ///
168 /// This property is meant to be set by widget implementations,
169 /// typically in their instance init function.
170 ///
171 /// Readable | Writable
172 ///
173 ///
174 /// #### `can-target`
175 /// Whether the widget can receive pointer events.
176 ///
177 /// Readable | Writable
178 ///
179 ///
180 /// #### `css-classes`
181 /// A list of css classes applied to this widget.
182 ///
183 /// Readable | Writable
184 ///
185 ///
186 /// #### `css-name`
187 /// The name of this widget in the CSS tree.
188 ///
189 /// This property is meant to be set by widget implementations,
190 /// typically in their instance init function.
191 ///
192 /// Readable | Writable | Construct Only
193 ///
194 ///
195 /// #### `cursor`
196 /// The cursor used by @widget.
197 ///
198 /// Readable | Writable
199 ///
200 ///
201 /// #### `focus-on-click`
202 /// Whether the widget should grab focus when it is clicked with the mouse.
203 ///
204 /// This property is only relevant for widgets that can take focus.
205 ///
206 /// Readable | Writable
207 ///
208 ///
209 /// #### `focusable`
210 /// Whether this widget itself will accept the input focus.
211 ///
212 /// Readable | Writable
213 ///
214 ///
215 /// #### `halign`
216 /// How to distribute horizontal space if widget gets extra space.
217 ///
218 /// Readable | Writable
219 ///
220 ///
221 /// #### `has-default`
222 /// Whether the widget is the default widget.
223 ///
224 /// Readable
225 ///
226 ///
227 /// #### `has-focus`
228 /// Whether the widget has the input focus.
229 ///
230 /// Readable
231 ///
232 ///
233 /// #### `has-tooltip`
234 /// Enables or disables the emission of the [`query-tooltip`][struct@crate::Widget#query-tooltip]
235 /// signal on @widget.
236 ///
237 /// A true value indicates that @widget can have a tooltip, in this case
238 /// the widget will be queried using [`query-tooltip`][struct@crate::Widget#query-tooltip] to
239 /// determine whether it will provide a tooltip or not.
240 ///
241 /// Readable | Writable
242 ///
243 ///
244 /// #### `height-request`
245 /// Overrides for height request of the widget.
246 ///
247 /// If this is -1, the natural request will be used.
248 ///
249 /// Readable | Writable
250 ///
251 ///
252 /// #### `hexpand`
253 /// Whether to expand horizontally.
254 ///
255 /// Readable | Writable
256 ///
257 ///
258 /// #### `hexpand-set`
259 /// Whether to use the `hexpand` property.
260 ///
261 /// Readable | Writable
262 ///
263 ///
264 /// #### `layout-manager`
265 /// The [`LayoutManager`][crate::LayoutManager] instance to use to compute
266 /// the preferred size of the widget, and allocate its children.
267 ///
268 /// This property is meant to be set by widget implementations,
269 /// typically in their instance init function.
270 ///
271 /// Readable | Writable
272 ///
273 ///
274 /// #### `limit-events`
275 /// Makes this widget act like a modal dialog, with respect to
276 /// event delivery.
277 ///
278 /// Global event controllers will not handle events with targets
279 /// inside the widget, unless they are set up to ignore propagation
280 /// limits. See [`EventControllerExt::set_propagation_limit()`][crate::prelude::EventControllerExt::set_propagation_limit()].
281 ///
282 /// Readable | Writable
283 ///
284 ///
285 /// #### `margin-bottom`
286 /// Margin on bottom side of widget.
287 ///
288 /// This property adds margin outside of the widget's normal size
289 /// request, the margin will be added in addition to the size from
290 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
291 ///
292 /// Readable | Writable
293 ///
294 ///
295 /// #### `margin-end`
296 /// Margin on end of widget, horizontally.
297 ///
298 /// This property supports left-to-right and right-to-left text
299 /// directions.
300 ///
301 /// This property adds margin outside of the widget's normal size
302 /// request, the margin will be added in addition to the size from
303 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
304 ///
305 /// Readable | Writable
306 ///
307 ///
308 /// #### `margin-start`
309 /// Margin on start of widget, horizontally.
310 ///
311 /// This property supports left-to-right and right-to-left text
312 /// directions.
313 ///
314 /// This property adds margin outside of the widget's normal size
315 /// request, the margin will be added in addition to the size from
316 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
317 ///
318 /// Readable | Writable
319 ///
320 ///
321 /// #### `margin-top`
322 /// Margin on top side of widget.
323 ///
324 /// This property adds margin outside of the widget's normal size
325 /// request, the margin will be added in addition to the size from
326 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
327 ///
328 /// Readable | Writable
329 ///
330 ///
331 /// #### `name`
332 /// The name of the widget.
333 ///
334 /// Readable | Writable
335 ///
336 ///
337 /// #### `opacity`
338 /// The requested opacity of the widget.
339 ///
340 /// Readable | Writable
341 ///
342 ///
343 /// #### `overflow`
344 /// How content outside the widget's content area is treated.
345 ///
346 /// This property is meant to be set by widget implementations,
347 /// typically in their instance init function.
348 ///
349 /// Readable | Writable
350 ///
351 ///
352 /// #### `parent`
353 /// The parent widget of this widget.
354 ///
355 /// Readable
356 ///
357 ///
358 /// #### `receives-default`
359 /// Whether the widget will receive the default action when it is focused.
360 ///
361 /// Readable | Writable
362 ///
363 ///
364 /// #### `root`
365 /// The [`Root`][crate::Root] widget of the widget tree containing this widget.
366 ///
367 /// This will be `NULL` if the widget is not contained in a root widget.
368 ///
369 /// Readable
370 ///
371 ///
372 /// #### `scale-factor`
373 /// The scale factor of the widget.
374 ///
375 /// Readable
376 ///
377 ///
378 /// #### `sensitive`
379 /// Whether the widget responds to input.
380 ///
381 /// Readable | Writable
382 ///
383 ///
384 /// #### `tooltip-markup`
385 /// Sets the text of tooltip to be the given string, which is marked up
386 /// with Pango markup.
387 ///
388 /// Also see [`Tooltip::set_markup()`][crate::Tooltip::set_markup()].
389 ///
390 /// This is a convenience property which will take care of getting the
391 /// tooltip shown if the given string is not `NULL`:
392 /// [`has-tooltip`][struct@crate::Widget#has-tooltip] will automatically be set to true
393 /// and there will be taken care of [`query-tooltip`][struct@crate::Widget#query-tooltip] in
394 /// the default signal handler.
395 ///
396 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and
397 /// [`tooltip-markup`][struct@crate::Widget#tooltip-markup] are set, the last one wins.
398 ///
399 /// Readable | Writable
400 ///
401 ///
402 /// #### `tooltip-text`
403 /// Sets the text of tooltip to be the given string.
404 ///
405 /// Also see [`Tooltip::set_text()`][crate::Tooltip::set_text()].
406 ///
407 /// This is a convenience property which will take care of getting the
408 /// tooltip shown if the given string is not `NULL`:
409 /// [`has-tooltip`][struct@crate::Widget#has-tooltip] will automatically be set to true
410 /// and there will be taken care of [`query-tooltip`][struct@crate::Widget#query-tooltip] in
411 /// the default signal handler.
412 ///
413 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and
414 /// [`tooltip-markup`][struct@crate::Widget#tooltip-markup] are set, the last one wins.
415 ///
416 /// Readable | Writable
417 ///
418 ///
419 /// #### `valign`
420 /// How to distribute vertical space if widget gets extra space.
421 ///
422 /// Readable | Writable
423 ///
424 ///
425 /// #### `vexpand`
426 /// Whether to expand vertically.
427 ///
428 /// Readable | Writable
429 ///
430 ///
431 /// #### `vexpand-set`
432 /// Whether to use the `vexpand` property.
433 ///
434 /// Readable | Writable
435 ///
436 ///
437 /// #### `visible`
438 /// Whether the widget is visible.
439 ///
440 /// Readable | Writable
441 ///
442 ///
443 /// #### `width-request`
444 /// Overrides for width request of the widget.
445 ///
446 /// If this is -1, the natural request will be used.
447 ///
448 /// Readable | Writable
449 /// </details>
450 /// <details><summary><h4>Accessible</h4></summary>
451 ///
452 ///
453 /// #### `accessible-role`
454 /// The accessible role of the given [`Accessible`][crate::Accessible] implementation.
455 ///
456 /// The accessible role cannot be changed once set.
457 ///
458 /// Readable | Writable
459 /// </details>
460 /// <details><summary><h4>Actionable</h4></summary>
461 ///
462 ///
463 /// #### `action-name`
464 /// The name of the action with which this widget should be associated.
465 ///
466 /// Readable | Writable
467 ///
468 ///
469 /// #### `action-target`
470 /// The target value of the actionable widget's action.
471 ///
472 /// Readable | Writable
473 /// </details>
474 ///
475 /// ## Signals
476 ///
477 ///
478 /// #### `toggled`
479 /// Emitted whenever the [`ToggleButton`][crate::ToggleButton]'s state is changed.
480 ///
481 ///
482 /// <details><summary><h4>Button</h4></summary>
483 ///
484 ///
485 /// #### `activate`
486 /// Emitted to animate press then release.
487 ///
488 /// This is an action signal. Applications should never connect
489 /// to this signal, but use the [`clicked`][struct@crate::Button#clicked] signal.
490 ///
491 /// The default bindings for this signal are all forms of the
492 /// <kbd>␣</kbd> and <kbd>Enter</kbd> keys.
493 ///
494 /// Action
495 ///
496 ///
497 /// #### `clicked`
498 /// Emitted when the button has been activated (pressed and released).
499 ///
500 /// Action
501 /// </details>
502 /// <details><summary><h4>Widget</h4></summary>
503 ///
504 ///
505 /// #### `destroy`
506 /// Signals that all holders of a reference to the widget should release
507 /// the reference that they hold.
508 ///
509 /// May result in finalization of the widget if all references are released.
510 ///
511 /// This signal is not suitable for saving widget state.
512 ///
513 ///
514 ///
515 ///
516 /// #### `direction-changed`
517 /// Emitted when the text direction of a widget changes.
518 ///
519 ///
520 ///
521 ///
522 /// #### `hide`
523 /// Emitted when @widget is hidden.
524 ///
525 ///
526 ///
527 ///
528 /// #### `keynav-failed`
529 /// Emitted if keyboard navigation fails.
530 ///
531 /// See [`WidgetExt::keynav_failed()`][crate::prelude::WidgetExt::keynav_failed()] for details.
532 ///
533 ///
534 ///
535 ///
536 /// #### `map`
537 /// Emitted when @widget is going to be mapped.
538 ///
539 /// A widget is mapped when the widget is visible (which is controlled with
540 /// [`visible`][struct@crate::Widget#visible]) and all its parents up to the toplevel widget
541 /// are also visible.
542 ///
543 /// The `::map` signal can be used to determine whether a widget will be drawn,
544 /// for instance it can resume an animation that was stopped during the
545 /// emission of [`unmap`][struct@crate::Widget#unmap].
546 ///
547 ///
548 ///
549 ///
550 /// #### `mnemonic-activate`
551 /// Emitted when a widget is activated via a mnemonic.
552 ///
553 /// The default handler for this signal activates @widget if @group_cycling
554 /// is false, or just makes @widget grab focus if @group_cycling is true.
555 ///
556 ///
557 ///
558 ///
559 /// #### `move-focus`
560 /// Emitted when the focus is moved.
561 ///
562 /// The `::move-focus` signal is a [keybinding signal](class.SignalAction.html).
563 ///
564 /// The default bindings for this signal are <kbd>Tab</kbd> to move forward,
565 /// and <kbd>Shift</kbd>+<kbd>Tab</kbd> to move backward.
566 ///
567 /// Action
568 ///
569 ///
570 /// #### `query-tooltip`
571 /// Emitted when the widget’s tooltip is about to be shown.
572 ///
573 /// This happens when the [`has-tooltip`][struct@crate::Widget#has-tooltip] property
574 /// is true and the hover timeout has expired with the cursor hovering
575 /// above @widget; or emitted when @widget got focus in keyboard mode.
576 ///
577 /// Using the given coordinates, the signal handler should determine
578 /// whether a tooltip should be shown for @widget. If this is the case
579 /// true should be returned, false otherwise. Note that if @keyboard_mode
580 /// is true, the values of @x and @y are undefined and should not be used.
581 ///
582 /// The signal handler is free to manipulate @tooltip with the therefore
583 /// destined function calls.
584 ///
585 ///
586 ///
587 ///
588 /// #### `realize`
589 /// Emitted when @widget is associated with a [`gdk::Surface`][crate::gdk::Surface].
590 ///
591 /// This means that [`WidgetExt::realize()`][crate::prelude::WidgetExt::realize()] has been called
592 /// or the widget has been mapped (that is, it is going to be drawn).
593 ///
594 ///
595 ///
596 ///
597 /// #### `show`
598 /// Emitted when @widget is shown.
599 ///
600 ///
601 ///
602 ///
603 /// #### `state-flags-changed`
604 /// Emitted when the widget state changes.
605 ///
606 /// See [`WidgetExt::state_flags()`][crate::prelude::WidgetExt::state_flags()].
607 ///
608 ///
609 ///
610 ///
611 /// #### `unmap`
612 /// Emitted when @widget is going to be unmapped.
613 ///
614 /// A widget is unmapped when either it or any of its parents up to the
615 /// toplevel widget have been set as hidden.
616 ///
617 /// As `::unmap` indicates that a widget will not be shown any longer,
618 /// it can be used to, for example, stop an animation on the widget.
619 ///
620 ///
621 ///
622 ///
623 /// #### `unrealize`
624 /// Emitted when the [`gdk::Surface`][crate::gdk::Surface] associated with @widget is destroyed.
625 ///
626 /// This means that [`WidgetExt::unrealize()`][crate::prelude::WidgetExt::unrealize()] has been called
627 /// or the widget has been unmapped (that is, it is going to be hidden).
628 ///
629 ///
630 /// </details>
631 ///
632 /// # Implements
633 ///
634 /// [`ToggleButtonExt`][trait@crate::prelude::ToggleButtonExt], [`ButtonExt`][trait@crate::prelude::ButtonExt], [`WidgetExt`][trait@crate::prelude::WidgetExt], [`trait@glib::ObjectExt`], [`AccessibleExt`][trait@crate::prelude::AccessibleExt], [`BuildableExt`][trait@crate::prelude::BuildableExt], [`ConstraintTargetExt`][trait@crate::prelude::ConstraintTargetExt], [`ActionableExt`][trait@crate::prelude::ActionableExt], [`WidgetExtManual`][trait@crate::prelude::WidgetExtManual], [`AccessibleExtManual`][trait@crate::prelude::AccessibleExtManual], [`ActionableExtManual`][trait@crate::prelude::ActionableExtManual]
635 #[doc(alias = "GtkToggleButton")]
636 pub struct ToggleButton(Object<ffi::GtkToggleButton, ffi::GtkToggleButtonClass>) @extends Button, Widget, @implements Accessible, Buildable, ConstraintTarget, Actionable;
637
638 match fn {
639 type_ => || ffi::gtk_toggle_button_get_type(),
640 }
641}
642
643#[cfg(not(feature = "v4_10"))]
644glib::wrapper! {
645 #[doc(alias = "GtkToggleButton")]
646 pub struct ToggleButton(Object<ffi::GtkToggleButton, ffi::GtkToggleButtonClass>) @extends Button, Widget, @implements Buildable, ConstraintTarget, Actionable;
647
648 match fn {
649 type_ => || ffi::gtk_toggle_button_get_type(),
650 }
651}
652
653impl ToggleButton {
654 pub const NONE: Option<&'static ToggleButton> = None;
655
656 /// Creates a new toggle button.
657 ///
658 /// A widget should be packed into the button, as in [`Button::new()`][crate::Button::new()].
659 ///
660 /// # Returns
661 ///
662 /// a new toggle button.
663 #[doc(alias = "gtk_toggle_button_new")]
664 pub fn new() -> ToggleButton {
665 assert_initialized_main_thread!();
666 unsafe { Widget::from_glib_none(ffi::gtk_toggle_button_new()).unsafe_cast() }
667 }
668
669 /// Creates a new toggle button with a text label.
670 /// ## `label`
671 /// a string containing the message to be placed in the toggle button.
672 ///
673 /// # Returns
674 ///
675 /// a new toggle button.
676 #[doc(alias = "gtk_toggle_button_new_with_label")]
677 #[doc(alias = "new_with_label")]
678 pub fn with_label(label: &str) -> ToggleButton {
679 assert_initialized_main_thread!();
680 unsafe {
681 Widget::from_glib_none(ffi::gtk_toggle_button_new_with_label(
682 label.to_glib_none().0,
683 ))
684 .unsafe_cast()
685 }
686 }
687
688 /// Creates a new [`ToggleButton`][crate::ToggleButton] containing a label.
689 ///
690 /// The label will be created using [`Label::with_mnemonic()`][crate::Label::with_mnemonic()],
691 /// so underscores in @label indicate the mnemonic for the button.
692 /// ## `label`
693 /// the text of the button, with an underscore in front of the
694 /// mnemonic character
695 ///
696 /// # Returns
697 ///
698 /// a new [`ToggleButton`][crate::ToggleButton]
699 #[doc(alias = "gtk_toggle_button_new_with_mnemonic")]
700 #[doc(alias = "new_with_mnemonic")]
701 pub fn with_mnemonic(label: &str) -> ToggleButton {
702 assert_initialized_main_thread!();
703 unsafe {
704 Widget::from_glib_none(ffi::gtk_toggle_button_new_with_mnemonic(
705 label.to_glib_none().0,
706 ))
707 .unsafe_cast()
708 }
709 }
710
711 // rustdoc-stripper-ignore-next
712 /// Creates a new builder-pattern struct instance to construct [`ToggleButton`] objects.
713 ///
714 /// This method returns an instance of [`ToggleButtonBuilder`](crate::builders::ToggleButtonBuilder) which can be used to create [`ToggleButton`] objects.
715 pub fn builder() -> ToggleButtonBuilder {
716 ToggleButtonBuilder::new()
717 }
718}
719
720impl Default for ToggleButton {
721 fn default() -> Self {
722 Self::new()
723 }
724}
725
726// rustdoc-stripper-ignore-next
727/// A [builder-pattern] type to construct [`ToggleButton`] objects.
728///
729/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
730#[must_use = "The builder must be built to be used"]
731pub struct ToggleButtonBuilder {
732 builder: glib::object::ObjectBuilder<'static, ToggleButton>,
733}
734
735impl ToggleButtonBuilder {
736 fn new() -> Self {
737 Self {
738 builder: glib::object::Object::builder(),
739 }
740 }
741
742 /// If the toggle button should be pressed in.
743 pub fn active(self, active: bool) -> Self {
744 Self {
745 builder: self.builder.property("active", active),
746 }
747 }
748
749 /// The toggle button whose group this widget belongs to.
750 pub fn group(self, group: &impl IsA<ToggleButton>) -> Self {
751 Self {
752 builder: self.builder.property("group", group.clone().upcast()),
753 }
754 }
755
756 /// Whether the size of the button can be made smaller than the natural
757 /// size of its contents.
758 ///
759 /// For text buttons, setting this property will allow ellipsizing the label.
760 ///
761 /// If the contents of a button are an icon or a custom widget, setting this
762 /// property has no effect.
763 #[cfg(feature = "v4_12")]
764 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
765 pub fn can_shrink(self, can_shrink: bool) -> Self {
766 Self {
767 builder: self.builder.property("can-shrink", can_shrink),
768 }
769 }
770
771 /// The child widget.
772 pub fn child(self, child: &impl IsA<Widget>) -> Self {
773 Self {
774 builder: self.builder.property("child", child.clone().upcast()),
775 }
776 }
777
778 /// Whether the button has a frame.
779 pub fn has_frame(self, has_frame: bool) -> Self {
780 Self {
781 builder: self.builder.property("has-frame", has_frame),
782 }
783 }
784
785 /// The name of the icon used to automatically populate the button.
786 pub fn icon_name(self, icon_name: impl Into<glib::GString>) -> Self {
787 Self {
788 builder: self.builder.property("icon-name", icon_name.into()),
789 }
790 }
791
792 /// Text of the label inside the button, if the button contains a label widget.
793 pub fn label(self, label: impl Into<glib::GString>) -> Self {
794 Self {
795 builder: self.builder.property("label", label.into()),
796 }
797 }
798
799 /// If set, an underline in the text indicates that the following character is
800 /// to be used as mnemonic.
801 pub fn use_underline(self, use_underline: bool) -> Self {
802 Self {
803 builder: self.builder.property("use-underline", use_underline),
804 }
805 }
806
807 /// Whether the widget or any of its descendents can accept
808 /// the input focus.
809 ///
810 /// This property is meant to be set by widget implementations,
811 /// typically in their instance init function.
812 pub fn can_focus(self, can_focus: bool) -> Self {
813 Self {
814 builder: self.builder.property("can-focus", can_focus),
815 }
816 }
817
818 /// Whether the widget can receive pointer events.
819 pub fn can_target(self, can_target: bool) -> Self {
820 Self {
821 builder: self.builder.property("can-target", can_target),
822 }
823 }
824
825 /// A list of css classes applied to this widget.
826 pub fn css_classes(self, css_classes: impl Into<glib::StrV>) -> Self {
827 Self {
828 builder: self.builder.property("css-classes", css_classes.into()),
829 }
830 }
831
832 /// The name of this widget in the CSS tree.
833 ///
834 /// This property is meant to be set by widget implementations,
835 /// typically in their instance init function.
836 pub fn css_name(self, css_name: impl Into<glib::GString>) -> Self {
837 Self {
838 builder: self.builder.property("css-name", css_name.into()),
839 }
840 }
841
842 /// The cursor used by @widget.
843 pub fn cursor(self, cursor: &gdk::Cursor) -> Self {
844 Self {
845 builder: self.builder.property("cursor", cursor.clone()),
846 }
847 }
848
849 /// Whether the widget should grab focus when it is clicked with the mouse.
850 ///
851 /// This property is only relevant for widgets that can take focus.
852 pub fn focus_on_click(self, focus_on_click: bool) -> Self {
853 Self {
854 builder: self.builder.property("focus-on-click", focus_on_click),
855 }
856 }
857
858 /// Whether this widget itself will accept the input focus.
859 pub fn focusable(self, focusable: bool) -> Self {
860 Self {
861 builder: self.builder.property("focusable", focusable),
862 }
863 }
864
865 /// How to distribute horizontal space if widget gets extra space.
866 pub fn halign(self, halign: Align) -> Self {
867 Self {
868 builder: self.builder.property("halign", halign),
869 }
870 }
871
872 /// Enables or disables the emission of the [`query-tooltip`][struct@crate::Widget#query-tooltip]
873 /// signal on @widget.
874 ///
875 /// A true value indicates that @widget can have a tooltip, in this case
876 /// the widget will be queried using [`query-tooltip`][struct@crate::Widget#query-tooltip] to
877 /// determine whether it will provide a tooltip or not.
878 pub fn has_tooltip(self, has_tooltip: bool) -> Self {
879 Self {
880 builder: self.builder.property("has-tooltip", has_tooltip),
881 }
882 }
883
884 /// Overrides for height request of the widget.
885 ///
886 /// If this is -1, the natural request will be used.
887 pub fn height_request(self, height_request: i32) -> Self {
888 Self {
889 builder: self.builder.property("height-request", height_request),
890 }
891 }
892
893 /// Whether to expand horizontally.
894 pub fn hexpand(self, hexpand: bool) -> Self {
895 Self {
896 builder: self.builder.property("hexpand", hexpand),
897 }
898 }
899
900 /// Whether to use the `hexpand` property.
901 pub fn hexpand_set(self, hexpand_set: bool) -> Self {
902 Self {
903 builder: self.builder.property("hexpand-set", hexpand_set),
904 }
905 }
906
907 /// The [`LayoutManager`][crate::LayoutManager] instance to use to compute
908 /// the preferred size of the widget, and allocate its children.
909 ///
910 /// This property is meant to be set by widget implementations,
911 /// typically in their instance init function.
912 pub fn layout_manager(self, layout_manager: &impl IsA<LayoutManager>) -> Self {
913 Self {
914 builder: self
915 .builder
916 .property("layout-manager", layout_manager.clone().upcast()),
917 }
918 }
919
920 /// Makes this widget act like a modal dialog, with respect to
921 /// event delivery.
922 ///
923 /// Global event controllers will not handle events with targets
924 /// inside the widget, unless they are set up to ignore propagation
925 /// limits. See [`EventControllerExt::set_propagation_limit()`][crate::prelude::EventControllerExt::set_propagation_limit()].
926 #[cfg(feature = "v4_18")]
927 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
928 pub fn limit_events(self, limit_events: bool) -> Self {
929 Self {
930 builder: self.builder.property("limit-events", limit_events),
931 }
932 }
933
934 /// Margin on bottom side of widget.
935 ///
936 /// This property adds margin outside of the widget's normal size
937 /// request, the margin will be added in addition to the size from
938 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
939 pub fn margin_bottom(self, margin_bottom: i32) -> Self {
940 Self {
941 builder: self.builder.property("margin-bottom", margin_bottom),
942 }
943 }
944
945 /// Margin on end of widget, horizontally.
946 ///
947 /// This property supports left-to-right and right-to-left text
948 /// directions.
949 ///
950 /// This property adds margin outside of the widget's normal size
951 /// request, the margin will be added in addition to the size from
952 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
953 pub fn margin_end(self, margin_end: i32) -> Self {
954 Self {
955 builder: self.builder.property("margin-end", margin_end),
956 }
957 }
958
959 /// Margin on start of widget, horizontally.
960 ///
961 /// This property supports left-to-right and right-to-left text
962 /// directions.
963 ///
964 /// This property adds margin outside of the widget's normal size
965 /// request, the margin will be added in addition to the size from
966 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
967 pub fn margin_start(self, margin_start: i32) -> Self {
968 Self {
969 builder: self.builder.property("margin-start", margin_start),
970 }
971 }
972
973 /// Margin on top side of widget.
974 ///
975 /// This property adds margin outside of the widget's normal size
976 /// request, the margin will be added in addition to the size from
977 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
978 pub fn margin_top(self, margin_top: i32) -> Self {
979 Self {
980 builder: self.builder.property("margin-top", margin_top),
981 }
982 }
983
984 /// The name of the widget.
985 pub fn name(self, name: impl Into<glib::GString>) -> Self {
986 Self {
987 builder: self.builder.property("name", name.into()),
988 }
989 }
990
991 /// The requested opacity of the widget.
992 pub fn opacity(self, opacity: f64) -> Self {
993 Self {
994 builder: self.builder.property("opacity", opacity),
995 }
996 }
997
998 /// How content outside the widget's content area is treated.
999 ///
1000 /// This property is meant to be set by widget implementations,
1001 /// typically in their instance init function.
1002 pub fn overflow(self, overflow: Overflow) -> Self {
1003 Self {
1004 builder: self.builder.property("overflow", overflow),
1005 }
1006 }
1007
1008 /// Whether the widget will receive the default action when it is focused.
1009 pub fn receives_default(self, receives_default: bool) -> Self {
1010 Self {
1011 builder: self.builder.property("receives-default", receives_default),
1012 }
1013 }
1014
1015 /// Whether the widget responds to input.
1016 pub fn sensitive(self, sensitive: bool) -> Self {
1017 Self {
1018 builder: self.builder.property("sensitive", sensitive),
1019 }
1020 }
1021
1022 /// Sets the text of tooltip to be the given string, which is marked up
1023 /// with Pango markup.
1024 ///
1025 /// Also see [`Tooltip::set_markup()`][crate::Tooltip::set_markup()].
1026 ///
1027 /// This is a convenience property which will take care of getting the
1028 /// tooltip shown if the given string is not `NULL`:
1029 /// [`has-tooltip`][struct@crate::Widget#has-tooltip] will automatically be set to true
1030 /// and there will be taken care of [`query-tooltip`][struct@crate::Widget#query-tooltip] in
1031 /// the default signal handler.
1032 ///
1033 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and
1034 /// [`tooltip-markup`][struct@crate::Widget#tooltip-markup] are set, the last one wins.
1035 pub fn tooltip_markup(self, tooltip_markup: impl Into<glib::GString>) -> Self {
1036 Self {
1037 builder: self
1038 .builder
1039 .property("tooltip-markup", tooltip_markup.into()),
1040 }
1041 }
1042
1043 /// Sets the text of tooltip to be the given string.
1044 ///
1045 /// Also see [`Tooltip::set_text()`][crate::Tooltip::set_text()].
1046 ///
1047 /// This is a convenience property which will take care of getting the
1048 /// tooltip shown if the given string is not `NULL`:
1049 /// [`has-tooltip`][struct@crate::Widget#has-tooltip] will automatically be set to true
1050 /// and there will be taken care of [`query-tooltip`][struct@crate::Widget#query-tooltip] in
1051 /// the default signal handler.
1052 ///
1053 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and
1054 /// [`tooltip-markup`][struct@crate::Widget#tooltip-markup] are set, the last one wins.
1055 pub fn tooltip_text(self, tooltip_text: impl Into<glib::GString>) -> Self {
1056 Self {
1057 builder: self.builder.property("tooltip-text", tooltip_text.into()),
1058 }
1059 }
1060
1061 /// How to distribute vertical space if widget gets extra space.
1062 pub fn valign(self, valign: Align) -> Self {
1063 Self {
1064 builder: self.builder.property("valign", valign),
1065 }
1066 }
1067
1068 /// Whether to expand vertically.
1069 pub fn vexpand(self, vexpand: bool) -> Self {
1070 Self {
1071 builder: self.builder.property("vexpand", vexpand),
1072 }
1073 }
1074
1075 /// Whether to use the `vexpand` property.
1076 pub fn vexpand_set(self, vexpand_set: bool) -> Self {
1077 Self {
1078 builder: self.builder.property("vexpand-set", vexpand_set),
1079 }
1080 }
1081
1082 /// Whether the widget is visible.
1083 pub fn visible(self, visible: bool) -> Self {
1084 Self {
1085 builder: self.builder.property("visible", visible),
1086 }
1087 }
1088
1089 /// Overrides for width request of the widget.
1090 ///
1091 /// If this is -1, the natural request will be used.
1092 pub fn width_request(self, width_request: i32) -> Self {
1093 Self {
1094 builder: self.builder.property("width-request", width_request),
1095 }
1096 }
1097
1098 /// The accessible role of the given [`Accessible`][crate::Accessible] implementation.
1099 ///
1100 /// The accessible role cannot be changed once set.
1101 pub fn accessible_role(self, accessible_role: AccessibleRole) -> Self {
1102 Self {
1103 builder: self.builder.property("accessible-role", accessible_role),
1104 }
1105 }
1106
1107 /// The name of the action with which this widget should be associated.
1108 pub fn action_name(self, action_name: impl Into<glib::GString>) -> Self {
1109 Self {
1110 builder: self.builder.property("action-name", action_name.into()),
1111 }
1112 }
1113
1114 /// The target value of the actionable widget's action.
1115 pub fn action_target(self, action_target: &glib::Variant) -> Self {
1116 Self {
1117 builder: self
1118 .builder
1119 .property("action-target", action_target.clone()),
1120 }
1121 }
1122
1123 // rustdoc-stripper-ignore-next
1124 /// Build the [`ToggleButton`].
1125 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
1126 pub fn build(self) -> ToggleButton {
1127 assert_initialized_main_thread!();
1128 self.builder.build()
1129 }
1130}
1131
1132/// Trait containing all [`struct@ToggleButton`] methods.
1133///
1134/// # Implementors
1135///
1136/// [`ToggleButton`][struct@crate::ToggleButton]
1137pub trait ToggleButtonExt: IsA<ToggleButton> + 'static {
1138 /// Queries a [`ToggleButton`][crate::ToggleButton] and returns its current state.
1139 ///
1140 /// Returns [`true`] if the toggle button is pressed in and [`false`]
1141 /// if it is raised.
1142 ///
1143 /// # Returns
1144 ///
1145 /// whether the button is pressed
1146 #[doc(alias = "gtk_toggle_button_get_active")]
1147 #[doc(alias = "get_active")]
1148 #[doc(alias = "active")]
1149 fn is_active(&self) -> bool {
1150 unsafe {
1151 from_glib(ffi::gtk_toggle_button_get_active(
1152 self.as_ref().to_glib_none().0,
1153 ))
1154 }
1155 }
1156
1157 /// Sets the status of the toggle button.
1158 ///
1159 /// Set to [`true`] if you want the [`ToggleButton`][crate::ToggleButton] to be “pressed in”,
1160 /// and [`false`] to raise it.
1161 ///
1162 /// If the status of the button changes, this action causes the
1163 /// [`toggled`][struct@crate::ToggleButton#toggled] signal to be emitted.
1164 /// ## `is_active`
1165 /// [`true`] or [`false`].
1166 #[doc(alias = "gtk_toggle_button_set_active")]
1167 #[doc(alias = "active")]
1168 fn set_active(&self, is_active: bool) {
1169 unsafe {
1170 ffi::gtk_toggle_button_set_active(
1171 self.as_ref().to_glib_none().0,
1172 is_active.into_glib(),
1173 );
1174 }
1175 }
1176
1177 /// Adds @self_ to the group of @group.
1178 ///
1179 /// In a group of multiple toggle buttons, only one button can be active
1180 /// at a time.
1181 ///
1182 /// Setting up groups in a cycle leads to undefined behavior.
1183 ///
1184 /// Note that the same effect can be achieved via the [`Actionable`][crate::Actionable]
1185 /// API, by using the same action with parameter type and state type 's'
1186 /// for all buttons in the group, and giving each button its own target
1187 /// value.
1188 /// ## `group`
1189 /// another [`ToggleButton`][crate::ToggleButton] to
1190 /// form a group with
1191 #[doc(alias = "gtk_toggle_button_set_group")]
1192 #[doc(alias = "group")]
1193 fn set_group(&self, group: Option<&impl IsA<ToggleButton>>) {
1194 unsafe {
1195 ffi::gtk_toggle_button_set_group(
1196 self.as_ref().to_glib_none().0,
1197 group.map(|p| p.as_ref()).to_glib_none().0,
1198 );
1199 }
1200 }
1201
1202 /// Emits the ::toggled signal on the [`ToggleButton`][crate::ToggleButton].
1203 ///
1204 /// # Deprecated since 4.10
1205 ///
1206 /// There is no good reason for an application ever to call this function.
1207 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
1208 #[allow(deprecated)]
1209 #[doc(alias = "gtk_toggle_button_toggled")]
1210 fn toggled(&self) {
1211 unsafe {
1212 ffi::gtk_toggle_button_toggled(self.as_ref().to_glib_none().0);
1213 }
1214 }
1215
1216 /// Emitted whenever the [`ToggleButton`][crate::ToggleButton]'s state is changed.
1217 #[doc(alias = "toggled")]
1218 fn connect_toggled<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1219 unsafe extern "C" fn toggled_trampoline<P: IsA<ToggleButton>, F: Fn(&P) + 'static>(
1220 this: *mut ffi::GtkToggleButton,
1221 f: glib::ffi::gpointer,
1222 ) {
1223 unsafe {
1224 let f: &F = &*(f as *const F);
1225 f(ToggleButton::from_glib_borrow(this).unsafe_cast_ref())
1226 }
1227 }
1228 unsafe {
1229 let f: Box_<F> = Box_::new(f);
1230 connect_raw(
1231 self.as_ptr() as *mut _,
1232 c"toggled".as_ptr(),
1233 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1234 toggled_trampoline::<Self, F> as *const (),
1235 )),
1236 Box_::into_raw(f),
1237 )
1238 }
1239 }
1240
1241 #[doc(alias = "active")]
1242 fn connect_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1243 unsafe extern "C" fn notify_active_trampoline<P: IsA<ToggleButton>, F: Fn(&P) + 'static>(
1244 this: *mut ffi::GtkToggleButton,
1245 _param_spec: glib::ffi::gpointer,
1246 f: glib::ffi::gpointer,
1247 ) {
1248 unsafe {
1249 let f: &F = &*(f as *const F);
1250 f(ToggleButton::from_glib_borrow(this).unsafe_cast_ref())
1251 }
1252 }
1253 unsafe {
1254 let f: Box_<F> = Box_::new(f);
1255 connect_raw(
1256 self.as_ptr() as *mut _,
1257 c"notify::active".as_ptr(),
1258 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1259 notify_active_trampoline::<Self, F> as *const (),
1260 )),
1261 Box_::into_raw(f),
1262 )
1263 }
1264 }
1265
1266 #[doc(alias = "group")]
1267 fn connect_group_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
1268 unsafe extern "C" fn notify_group_trampoline<P: IsA<ToggleButton>, F: Fn(&P) + 'static>(
1269 this: *mut ffi::GtkToggleButton,
1270 _param_spec: glib::ffi::gpointer,
1271 f: glib::ffi::gpointer,
1272 ) {
1273 unsafe {
1274 let f: &F = &*(f as *const F);
1275 f(ToggleButton::from_glib_borrow(this).unsafe_cast_ref())
1276 }
1277 }
1278 unsafe {
1279 let f: Box_<F> = Box_::new(f);
1280 connect_raw(
1281 self.as_ptr() as *mut _,
1282 c"notify::group".as_ptr(),
1283 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
1284 notify_group_trampoline::<Self, F> as *const (),
1285 )),
1286 Box_::into_raw(f),
1287 )
1288 }
1289 }
1290}
1291
1292impl<O: IsA<ToggleButton>> ToggleButtonExt for O {}