1use gdk;
6use glib::object::Cast;
7use glib::object::IsA;
8use glib::signal::connect_raw;
9use glib::signal::SignalHandlerId;
10use glib::translate::*;
11use glib::StaticType;
12use glib::ToValue;
13use glib::Value;
14use glib_sys;
15use gobject_sys;
16use gtk_sys;
17use std::boxed::Box as Box_;
18use std::fmt;
19use std::mem::transmute;
20use Actionable;
21use Align;
22use Bin;
23use Buildable;
24use Button;
25use Container;
26use PositionType;
27use ReliefStyle;
28use ResizeMode;
29use Widget;
30
31glib_wrapper! {
32 pub struct ToggleButton(Object<gtk_sys::GtkToggleButton, gtk_sys::GtkToggleButtonClass, ToggleButtonClass>) @extends Button, Bin, Container, Widget, @implements Buildable, Actionable;
33
34 match fn {
35 get_type => || gtk_sys::gtk_toggle_button_get_type(),
36 }
37}
38
39impl ToggleButton {
40 pub fn new() -> ToggleButton {
41 assert_initialized_main_thread!();
42 unsafe { Widget::from_glib_none(gtk_sys::gtk_toggle_button_new()).unsafe_cast() }
43 }
44
45 pub fn new_with_label(label: &str) -> ToggleButton {
46 assert_initialized_main_thread!();
47 unsafe {
48 Widget::from_glib_none(gtk_sys::gtk_toggle_button_new_with_label(
49 label.to_glib_none().0,
50 ))
51 .unsafe_cast()
52 }
53 }
54
55 pub fn new_with_mnemonic(label: &str) -> ToggleButton {
56 assert_initialized_main_thread!();
57 unsafe {
58 Widget::from_glib_none(gtk_sys::gtk_toggle_button_new_with_mnemonic(
59 label.to_glib_none().0,
60 ))
61 .unsafe_cast()
62 }
63 }
64}
65
66impl Default for ToggleButton {
67 fn default() -> Self {
68 Self::new()
69 }
70}
71
72pub struct ToggleButtonBuilder {
73 active: Option<bool>,
74 draw_indicator: Option<bool>,
75 inconsistent: Option<bool>,
76 always_show_image: Option<bool>,
77 image: Option<Widget>,
78 image_position: Option<PositionType>,
79 label: Option<String>,
80 relief: Option<ReliefStyle>,
81 use_underline: Option<bool>,
82 border_width: Option<u32>,
83 child: Option<Widget>,
84 resize_mode: Option<ResizeMode>,
85 app_paintable: Option<bool>,
86 can_default: Option<bool>,
87 can_focus: Option<bool>,
88 events: Option<gdk::EventMask>,
89 expand: Option<bool>,
90 #[cfg(any(feature = "v3_20", feature = "dox"))]
91 focus_on_click: Option<bool>,
92 halign: Option<Align>,
93 has_default: Option<bool>,
94 has_focus: Option<bool>,
95 has_tooltip: Option<bool>,
96 height_request: Option<i32>,
97 hexpand: Option<bool>,
98 hexpand_set: Option<bool>,
99 is_focus: Option<bool>,
100 margin: Option<i32>,
101 margin_bottom: Option<i32>,
102 margin_end: Option<i32>,
103 margin_start: Option<i32>,
104 margin_top: Option<i32>,
105 name: Option<String>,
106 no_show_all: Option<bool>,
107 opacity: Option<f64>,
108 parent: Option<Container>,
109 receives_default: Option<bool>,
110 sensitive: Option<bool>,
111 tooltip_markup: Option<String>,
113 tooltip_text: Option<String>,
114 valign: Option<Align>,
115 vexpand: Option<bool>,
116 vexpand_set: Option<bool>,
117 visible: Option<bool>,
118 width_request: Option<i32>,
119}
120
121impl ToggleButtonBuilder {
122 pub fn new() -> Self {
123 Self {
124 active: None,
125 draw_indicator: None,
126 inconsistent: None,
127 always_show_image: None,
128 image: None,
129 image_position: None,
130 label: None,
131 relief: None,
132 use_underline: None,
133 border_width: None,
134 child: None,
135 resize_mode: None,
136 app_paintable: None,
137 can_default: None,
138 can_focus: None,
139 events: None,
140 expand: None,
141 #[cfg(any(feature = "v3_20", feature = "dox"))]
142 focus_on_click: None,
143 halign: None,
144 has_default: None,
145 has_focus: None,
146 has_tooltip: None,
147 height_request: None,
148 hexpand: None,
149 hexpand_set: None,
150 is_focus: None,
151 margin: None,
152 margin_bottom: None,
153 margin_end: None,
154 margin_start: None,
155 margin_top: None,
156 name: None,
157 no_show_all: None,
158 opacity: None,
159 parent: None,
160 receives_default: None,
161 sensitive: None,
162 tooltip_markup: None,
163 tooltip_text: None,
164 valign: None,
165 vexpand: None,
166 vexpand_set: None,
167 visible: None,
168 width_request: None,
169 }
170 }
171
172 pub fn build(self) -> ToggleButton {
173 let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
174 if let Some(ref active) = self.active {
175 properties.push(("active", active));
176 }
177 if let Some(ref draw_indicator) = self.draw_indicator {
178 properties.push(("draw-indicator", draw_indicator));
179 }
180 if let Some(ref inconsistent) = self.inconsistent {
181 properties.push(("inconsistent", inconsistent));
182 }
183 if let Some(ref always_show_image) = self.always_show_image {
184 properties.push(("always-show-image", always_show_image));
185 }
186 if let Some(ref image) = self.image {
187 properties.push(("image", image));
188 }
189 if let Some(ref image_position) = self.image_position {
190 properties.push(("image-position", image_position));
191 }
192 if let Some(ref label) = self.label {
193 properties.push(("label", label));
194 }
195 if let Some(ref relief) = self.relief {
196 properties.push(("relief", relief));
197 }
198 if let Some(ref use_underline) = self.use_underline {
199 properties.push(("use-underline", use_underline));
200 }
201 if let Some(ref border_width) = self.border_width {
202 properties.push(("border-width", border_width));
203 }
204 if let Some(ref child) = self.child {
205 properties.push(("child", child));
206 }
207 if let Some(ref resize_mode) = self.resize_mode {
208 properties.push(("resize-mode", resize_mode));
209 }
210 if let Some(ref app_paintable) = self.app_paintable {
211 properties.push(("app-paintable", app_paintable));
212 }
213 if let Some(ref can_default) = self.can_default {
214 properties.push(("can-default", can_default));
215 }
216 if let Some(ref can_focus) = self.can_focus {
217 properties.push(("can-focus", can_focus));
218 }
219 if let Some(ref events) = self.events {
220 properties.push(("events", events));
221 }
222 if let Some(ref expand) = self.expand {
223 properties.push(("expand", expand));
224 }
225 #[cfg(any(feature = "v3_20", feature = "dox"))]
226 {
227 if let Some(ref focus_on_click) = self.focus_on_click {
228 properties.push(("focus-on-click", focus_on_click));
229 }
230 }
231 if let Some(ref halign) = self.halign {
232 properties.push(("halign", halign));
233 }
234 if let Some(ref has_default) = self.has_default {
235 properties.push(("has-default", has_default));
236 }
237 if let Some(ref has_focus) = self.has_focus {
238 properties.push(("has-focus", has_focus));
239 }
240 if let Some(ref has_tooltip) = self.has_tooltip {
241 properties.push(("has-tooltip", has_tooltip));
242 }
243 if let Some(ref height_request) = self.height_request {
244 properties.push(("height-request", height_request));
245 }
246 if let Some(ref hexpand) = self.hexpand {
247 properties.push(("hexpand", hexpand));
248 }
249 if let Some(ref hexpand_set) = self.hexpand_set {
250 properties.push(("hexpand-set", hexpand_set));
251 }
252 if let Some(ref is_focus) = self.is_focus {
253 properties.push(("is-focus", is_focus));
254 }
255 if let Some(ref margin) = self.margin {
256 properties.push(("margin", margin));
257 }
258 if let Some(ref margin_bottom) = self.margin_bottom {
259 properties.push(("margin-bottom", margin_bottom));
260 }
261 if let Some(ref margin_end) = self.margin_end {
262 properties.push(("margin-end", margin_end));
263 }
264 if let Some(ref margin_start) = self.margin_start {
265 properties.push(("margin-start", margin_start));
266 }
267 if let Some(ref margin_top) = self.margin_top {
268 properties.push(("margin-top", margin_top));
269 }
270 if let Some(ref name) = self.name {
271 properties.push(("name", name));
272 }
273 if let Some(ref no_show_all) = self.no_show_all {
274 properties.push(("no-show-all", no_show_all));
275 }
276 if let Some(ref opacity) = self.opacity {
277 properties.push(("opacity", opacity));
278 }
279 if let Some(ref parent) = self.parent {
280 properties.push(("parent", parent));
281 }
282 if let Some(ref receives_default) = self.receives_default {
283 properties.push(("receives-default", receives_default));
284 }
285 if let Some(ref sensitive) = self.sensitive {
286 properties.push(("sensitive", sensitive));
287 }
288 if let Some(ref tooltip_markup) = self.tooltip_markup {
289 properties.push(("tooltip-markup", tooltip_markup));
290 }
291 if let Some(ref tooltip_text) = self.tooltip_text {
292 properties.push(("tooltip-text", tooltip_text));
293 }
294 if let Some(ref valign) = self.valign {
295 properties.push(("valign", valign));
296 }
297 if let Some(ref vexpand) = self.vexpand {
298 properties.push(("vexpand", vexpand));
299 }
300 if let Some(ref vexpand_set) = self.vexpand_set {
301 properties.push(("vexpand-set", vexpand_set));
302 }
303 if let Some(ref visible) = self.visible {
304 properties.push(("visible", visible));
305 }
306 if let Some(ref width_request) = self.width_request {
307 properties.push(("width-request", width_request));
308 }
309 glib::Object::new(ToggleButton::static_type(), &properties)
310 .expect("object new")
311 .downcast()
312 .expect("downcast")
313 }
314
315 pub fn active(mut self, active: bool) -> Self {
316 self.active = Some(active);
317 self
318 }
319
320 pub fn draw_indicator(mut self, draw_indicator: bool) -> Self {
321 self.draw_indicator = Some(draw_indicator);
322 self
323 }
324
325 pub fn inconsistent(mut self, inconsistent: bool) -> Self {
326 self.inconsistent = Some(inconsistent);
327 self
328 }
329
330 pub fn always_show_image(mut self, always_show_image: bool) -> Self {
331 self.always_show_image = Some(always_show_image);
332 self
333 }
334
335 pub fn image(mut self, image: &Widget) -> Self {
336 self.image = Some(image.clone());
337 self
338 }
339
340 pub fn image_position(mut self, image_position: PositionType) -> Self {
341 self.image_position = Some(image_position);
342 self
343 }
344
345 pub fn label(mut self, label: &str) -> Self {
346 self.label = Some(label.to_string());
347 self
348 }
349
350 pub fn relief(mut self, relief: ReliefStyle) -> Self {
351 self.relief = Some(relief);
352 self
353 }
354
355 pub fn use_underline(mut self, use_underline: bool) -> Self {
356 self.use_underline = Some(use_underline);
357 self
358 }
359
360 pub fn border_width(mut self, border_width: u32) -> Self {
361 self.border_width = Some(border_width);
362 self
363 }
364
365 pub fn child(mut self, child: &Widget) -> Self {
366 self.child = Some(child.clone());
367 self
368 }
369
370 pub fn resize_mode(mut self, resize_mode: ResizeMode) -> Self {
371 self.resize_mode = Some(resize_mode);
372 self
373 }
374
375 pub fn app_paintable(mut self, app_paintable: bool) -> Self {
376 self.app_paintable = Some(app_paintable);
377 self
378 }
379
380 pub fn can_default(mut self, can_default: bool) -> Self {
381 self.can_default = Some(can_default);
382 self
383 }
384
385 pub fn can_focus(mut self, can_focus: bool) -> Self {
386 self.can_focus = Some(can_focus);
387 self
388 }
389
390 pub fn events(mut self, events: gdk::EventMask) -> Self {
391 self.events = Some(events);
392 self
393 }
394
395 pub fn expand(mut self, expand: bool) -> Self {
396 self.expand = Some(expand);
397 self
398 }
399
400 #[cfg(any(feature = "v3_20", feature = "dox"))]
401 pub fn focus_on_click(mut self, focus_on_click: bool) -> Self {
402 self.focus_on_click = Some(focus_on_click);
403 self
404 }
405
406 pub fn halign(mut self, halign: Align) -> Self {
407 self.halign = Some(halign);
408 self
409 }
410
411 pub fn has_default(mut self, has_default: bool) -> Self {
412 self.has_default = Some(has_default);
413 self
414 }
415
416 pub fn has_focus(mut self, has_focus: bool) -> Self {
417 self.has_focus = Some(has_focus);
418 self
419 }
420
421 pub fn has_tooltip(mut self, has_tooltip: bool) -> Self {
422 self.has_tooltip = Some(has_tooltip);
423 self
424 }
425
426 pub fn height_request(mut self, height_request: i32) -> Self {
427 self.height_request = Some(height_request);
428 self
429 }
430
431 pub fn hexpand(mut self, hexpand: bool) -> Self {
432 self.hexpand = Some(hexpand);
433 self
434 }
435
436 pub fn hexpand_set(mut self, hexpand_set: bool) -> Self {
437 self.hexpand_set = Some(hexpand_set);
438 self
439 }
440
441 pub fn is_focus(mut self, is_focus: bool) -> Self {
442 self.is_focus = Some(is_focus);
443 self
444 }
445
446 pub fn margin(mut self, margin: i32) -> Self {
447 self.margin = Some(margin);
448 self
449 }
450
451 pub fn margin_bottom(mut self, margin_bottom: i32) -> Self {
452 self.margin_bottom = Some(margin_bottom);
453 self
454 }
455
456 pub fn margin_end(mut self, margin_end: i32) -> Self {
457 self.margin_end = Some(margin_end);
458 self
459 }
460
461 pub fn margin_start(mut self, margin_start: i32) -> Self {
462 self.margin_start = Some(margin_start);
463 self
464 }
465
466 pub fn margin_top(mut self, margin_top: i32) -> Self {
467 self.margin_top = Some(margin_top);
468 self
469 }
470
471 pub fn name(mut self, name: &str) -> Self {
472 self.name = Some(name.to_string());
473 self
474 }
475
476 pub fn no_show_all(mut self, no_show_all: bool) -> Self {
477 self.no_show_all = Some(no_show_all);
478 self
479 }
480
481 pub fn opacity(mut self, opacity: f64) -> Self {
482 self.opacity = Some(opacity);
483 self
484 }
485
486 pub fn parent(mut self, parent: &Container) -> Self {
487 self.parent = Some(parent.clone());
488 self
489 }
490
491 pub fn receives_default(mut self, receives_default: bool) -> Self {
492 self.receives_default = Some(receives_default);
493 self
494 }
495
496 pub fn sensitive(mut self, sensitive: bool) -> Self {
497 self.sensitive = Some(sensitive);
498 self
499 }
500
501 pub fn tooltip_markup(mut self, tooltip_markup: &str) -> Self {
502 self.tooltip_markup = Some(tooltip_markup.to_string());
503 self
504 }
505
506 pub fn tooltip_text(mut self, tooltip_text: &str) -> Self {
507 self.tooltip_text = Some(tooltip_text.to_string());
508 self
509 }
510
511 pub fn valign(mut self, valign: Align) -> Self {
512 self.valign = Some(valign);
513 self
514 }
515
516 pub fn vexpand(mut self, vexpand: bool) -> Self {
517 self.vexpand = Some(vexpand);
518 self
519 }
520
521 pub fn vexpand_set(mut self, vexpand_set: bool) -> Self {
522 self.vexpand_set = Some(vexpand_set);
523 self
524 }
525
526 pub fn visible(mut self, visible: bool) -> Self {
527 self.visible = Some(visible);
528 self
529 }
530
531 pub fn width_request(mut self, width_request: i32) -> Self {
532 self.width_request = Some(width_request);
533 self
534 }
535}
536
537pub const NONE_TOGGLE_BUTTON: Option<&ToggleButton> = None;
538
539pub trait ToggleButtonExt: 'static {
540 fn get_active(&self) -> bool;
541
542 fn get_inconsistent(&self) -> bool;
543
544 fn get_mode(&self) -> bool;
545
546 fn set_active(&self, is_active: bool);
547
548 fn set_inconsistent(&self, setting: bool);
549
550 fn set_mode(&self, draw_indicator: bool);
551
552 fn toggled(&self);
553
554 fn get_property_draw_indicator(&self) -> bool;
555
556 fn set_property_draw_indicator(&self, draw_indicator: bool);
557
558 fn connect_toggled<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
559
560 fn connect_property_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
561
562 fn connect_property_draw_indicator_notify<F: Fn(&Self) + 'static>(
563 &self,
564 f: F,
565 ) -> SignalHandlerId;
566
567 fn connect_property_inconsistent_notify<F: Fn(&Self) + 'static>(&self, f: F)
568 -> SignalHandlerId;
569}
570
571impl<O: IsA<ToggleButton>> ToggleButtonExt for O {
572 fn get_active(&self) -> bool {
573 unsafe {
574 from_glib(gtk_sys::gtk_toggle_button_get_active(
575 self.as_ref().to_glib_none().0,
576 ))
577 }
578 }
579
580 fn get_inconsistent(&self) -> bool {
581 unsafe {
582 from_glib(gtk_sys::gtk_toggle_button_get_inconsistent(
583 self.as_ref().to_glib_none().0,
584 ))
585 }
586 }
587
588 fn get_mode(&self) -> bool {
589 unsafe {
590 from_glib(gtk_sys::gtk_toggle_button_get_mode(
591 self.as_ref().to_glib_none().0,
592 ))
593 }
594 }
595
596 fn set_active(&self, is_active: bool) {
597 unsafe {
598 gtk_sys::gtk_toggle_button_set_active(
599 self.as_ref().to_glib_none().0,
600 is_active.to_glib(),
601 );
602 }
603 }
604
605 fn set_inconsistent(&self, setting: bool) {
606 unsafe {
607 gtk_sys::gtk_toggle_button_set_inconsistent(
608 self.as_ref().to_glib_none().0,
609 setting.to_glib(),
610 );
611 }
612 }
613
614 fn set_mode(&self, draw_indicator: bool) {
615 unsafe {
616 gtk_sys::gtk_toggle_button_set_mode(
617 self.as_ref().to_glib_none().0,
618 draw_indicator.to_glib(),
619 );
620 }
621 }
622
623 fn toggled(&self) {
624 unsafe {
625 gtk_sys::gtk_toggle_button_toggled(self.as_ref().to_glib_none().0);
626 }
627 }
628
629 fn get_property_draw_indicator(&self) -> bool {
630 unsafe {
631 let mut value = Value::from_type(<bool as StaticType>::static_type());
632 gobject_sys::g_object_get_property(
633 self.to_glib_none().0 as *mut gobject_sys::GObject,
634 b"draw-indicator\0".as_ptr() as *const _,
635 value.to_glib_none_mut().0,
636 );
637 value.get().unwrap()
638 }
639 }
640
641 fn set_property_draw_indicator(&self, draw_indicator: bool) {
642 unsafe {
643 gobject_sys::g_object_set_property(
644 self.to_glib_none().0 as *mut gobject_sys::GObject,
645 b"draw-indicator\0".as_ptr() as *const _,
646 Value::from(&draw_indicator).to_glib_none().0,
647 );
648 }
649 }
650
651 fn connect_toggled<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
652 unsafe extern "C" fn toggled_trampoline<P, F: Fn(&P) + 'static>(
653 this: *mut gtk_sys::GtkToggleButton,
654 f: glib_sys::gpointer,
655 ) where
656 P: IsA<ToggleButton>,
657 {
658 let f: &F = &*(f as *const F);
659 f(&ToggleButton::from_glib_borrow(this).unsafe_cast())
660 }
661 unsafe {
662 let f: Box_<F> = Box_::new(f);
663 connect_raw(
664 self.as_ptr() as *mut _,
665 b"toggled\0".as_ptr() as *const _,
666 Some(transmute(toggled_trampoline::<Self, F> as usize)),
667 Box_::into_raw(f),
668 )
669 }
670 }
671
672 fn connect_property_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
673 unsafe extern "C" fn notify_active_trampoline<P, F: Fn(&P) + 'static>(
674 this: *mut gtk_sys::GtkToggleButton,
675 _param_spec: glib_sys::gpointer,
676 f: glib_sys::gpointer,
677 ) where
678 P: IsA<ToggleButton>,
679 {
680 let f: &F = &*(f as *const F);
681 f(&ToggleButton::from_glib_borrow(this).unsafe_cast())
682 }
683 unsafe {
684 let f: Box_<F> = Box_::new(f);
685 connect_raw(
686 self.as_ptr() as *mut _,
687 b"notify::active\0".as_ptr() as *const _,
688 Some(transmute(notify_active_trampoline::<Self, F> as usize)),
689 Box_::into_raw(f),
690 )
691 }
692 }
693
694 fn connect_property_draw_indicator_notify<F: Fn(&Self) + 'static>(
695 &self,
696 f: F,
697 ) -> SignalHandlerId {
698 unsafe extern "C" fn notify_draw_indicator_trampoline<P, F: Fn(&P) + 'static>(
699 this: *mut gtk_sys::GtkToggleButton,
700 _param_spec: glib_sys::gpointer,
701 f: glib_sys::gpointer,
702 ) where
703 P: IsA<ToggleButton>,
704 {
705 let f: &F = &*(f as *const F);
706 f(&ToggleButton::from_glib_borrow(this).unsafe_cast())
707 }
708 unsafe {
709 let f: Box_<F> = Box_::new(f);
710 connect_raw(
711 self.as_ptr() as *mut _,
712 b"notify::draw-indicator\0".as_ptr() as *const _,
713 Some(transmute(
714 notify_draw_indicator_trampoline::<Self, F> as usize,
715 )),
716 Box_::into_raw(f),
717 )
718 }
719 }
720
721 fn connect_property_inconsistent_notify<F: Fn(&Self) + 'static>(
722 &self,
723 f: F,
724 ) -> SignalHandlerId {
725 unsafe extern "C" fn notify_inconsistent_trampoline<P, F: Fn(&P) + 'static>(
726 this: *mut gtk_sys::GtkToggleButton,
727 _param_spec: glib_sys::gpointer,
728 f: glib_sys::gpointer,
729 ) where
730 P: IsA<ToggleButton>,
731 {
732 let f: &F = &*(f as *const F);
733 f(&ToggleButton::from_glib_borrow(this).unsafe_cast())
734 }
735 unsafe {
736 let f: Box_<F> = Box_::new(f);
737 connect_raw(
738 self.as_ptr() as *mut _,
739 b"notify::inconsistent\0".as_ptr() as *const _,
740 Some(transmute(
741 notify_inconsistent_trampoline::<Self, F> as usize,
742 )),
743 Box_::into_raw(f),
744 )
745 }
746 }
747}
748
749impl fmt::Display for ToggleButton {
750 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
751 write!(f, "ToggleButton")
752 }
753}