1use gdk;
6use glib;
7use glib::object::Cast;
8use glib::object::IsA;
9use glib::object::ObjectExt;
10use glib::signal::connect_raw;
11use glib::signal::SignalHandlerId;
12use glib::translate::*;
13use glib::StaticType;
14use glib::ToValue;
15use glib_sys;
16use gobject_sys;
17use gtk_sys;
18use signal::Inhibit;
19use std::boxed::Box as Box_;
20use std::fmt;
21use std::mem::transmute;
22use Actionable;
23use Align;
24use Buildable;
25use Container;
26use Widget;
27
28glib_wrapper! {
29 pub struct Switch(Object<gtk_sys::GtkSwitch, gtk_sys::GtkSwitchClass, SwitchClass>) @extends Widget, @implements Buildable, Actionable;
30
31 match fn {
32 get_type => || gtk_sys::gtk_switch_get_type(),
33 }
34}
35
36impl Switch {
37 pub fn new() -> Switch {
38 assert_initialized_main_thread!();
39 unsafe { Widget::from_glib_none(gtk_sys::gtk_switch_new()).unsafe_cast() }
40 }
41}
42
43impl Default for Switch {
44 fn default() -> Self {
45 Self::new()
46 }
47}
48
49pub struct SwitchBuilder {
50 active: Option<bool>,
51 state: Option<bool>,
52 app_paintable: Option<bool>,
53 can_default: Option<bool>,
54 can_focus: Option<bool>,
55 events: Option<gdk::EventMask>,
56 expand: Option<bool>,
57 #[cfg(any(feature = "v3_20", feature = "dox"))]
58 focus_on_click: Option<bool>,
59 halign: Option<Align>,
60 has_default: Option<bool>,
61 has_focus: Option<bool>,
62 has_tooltip: Option<bool>,
63 height_request: Option<i32>,
64 hexpand: Option<bool>,
65 hexpand_set: Option<bool>,
66 is_focus: Option<bool>,
67 margin: Option<i32>,
68 margin_bottom: Option<i32>,
69 margin_end: Option<i32>,
70 margin_start: Option<i32>,
71 margin_top: Option<i32>,
72 name: Option<String>,
73 no_show_all: Option<bool>,
74 opacity: Option<f64>,
75 parent: Option<Container>,
76 receives_default: Option<bool>,
77 sensitive: Option<bool>,
78 tooltip_markup: Option<String>,
80 tooltip_text: Option<String>,
81 valign: Option<Align>,
82 vexpand: Option<bool>,
83 vexpand_set: Option<bool>,
84 visible: Option<bool>,
85 width_request: Option<i32>,
86}
87
88impl SwitchBuilder {
89 pub fn new() -> Self {
90 Self {
91 active: None,
92 state: None,
93 app_paintable: None,
94 can_default: None,
95 can_focus: None,
96 events: None,
97 expand: None,
98 #[cfg(any(feature = "v3_20", feature = "dox"))]
99 focus_on_click: None,
100 halign: None,
101 has_default: None,
102 has_focus: None,
103 has_tooltip: None,
104 height_request: None,
105 hexpand: None,
106 hexpand_set: None,
107 is_focus: None,
108 margin: None,
109 margin_bottom: None,
110 margin_end: None,
111 margin_start: None,
112 margin_top: None,
113 name: None,
114 no_show_all: None,
115 opacity: None,
116 parent: None,
117 receives_default: None,
118 sensitive: None,
119 tooltip_markup: None,
120 tooltip_text: None,
121 valign: None,
122 vexpand: None,
123 vexpand_set: None,
124 visible: None,
125 width_request: None,
126 }
127 }
128
129 pub fn build(self) -> Switch {
130 let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
131 if let Some(ref active) = self.active {
132 properties.push(("active", active));
133 }
134 if let Some(ref state) = self.state {
135 properties.push(("state", state));
136 }
137 if let Some(ref app_paintable) = self.app_paintable {
138 properties.push(("app-paintable", app_paintable));
139 }
140 if let Some(ref can_default) = self.can_default {
141 properties.push(("can-default", can_default));
142 }
143 if let Some(ref can_focus) = self.can_focus {
144 properties.push(("can-focus", can_focus));
145 }
146 if let Some(ref events) = self.events {
147 properties.push(("events", events));
148 }
149 if let Some(ref expand) = self.expand {
150 properties.push(("expand", expand));
151 }
152 #[cfg(any(feature = "v3_20", feature = "dox"))]
153 {
154 if let Some(ref focus_on_click) = self.focus_on_click {
155 properties.push(("focus-on-click", focus_on_click));
156 }
157 }
158 if let Some(ref halign) = self.halign {
159 properties.push(("halign", halign));
160 }
161 if let Some(ref has_default) = self.has_default {
162 properties.push(("has-default", has_default));
163 }
164 if let Some(ref has_focus) = self.has_focus {
165 properties.push(("has-focus", has_focus));
166 }
167 if let Some(ref has_tooltip) = self.has_tooltip {
168 properties.push(("has-tooltip", has_tooltip));
169 }
170 if let Some(ref height_request) = self.height_request {
171 properties.push(("height-request", height_request));
172 }
173 if let Some(ref hexpand) = self.hexpand {
174 properties.push(("hexpand", hexpand));
175 }
176 if let Some(ref hexpand_set) = self.hexpand_set {
177 properties.push(("hexpand-set", hexpand_set));
178 }
179 if let Some(ref is_focus) = self.is_focus {
180 properties.push(("is-focus", is_focus));
181 }
182 if let Some(ref margin) = self.margin {
183 properties.push(("margin", margin));
184 }
185 if let Some(ref margin_bottom) = self.margin_bottom {
186 properties.push(("margin-bottom", margin_bottom));
187 }
188 if let Some(ref margin_end) = self.margin_end {
189 properties.push(("margin-end", margin_end));
190 }
191 if let Some(ref margin_start) = self.margin_start {
192 properties.push(("margin-start", margin_start));
193 }
194 if let Some(ref margin_top) = self.margin_top {
195 properties.push(("margin-top", margin_top));
196 }
197 if let Some(ref name) = self.name {
198 properties.push(("name", name));
199 }
200 if let Some(ref no_show_all) = self.no_show_all {
201 properties.push(("no-show-all", no_show_all));
202 }
203 if let Some(ref opacity) = self.opacity {
204 properties.push(("opacity", opacity));
205 }
206 if let Some(ref parent) = self.parent {
207 properties.push(("parent", parent));
208 }
209 if let Some(ref receives_default) = self.receives_default {
210 properties.push(("receives-default", receives_default));
211 }
212 if let Some(ref sensitive) = self.sensitive {
213 properties.push(("sensitive", sensitive));
214 }
215 if let Some(ref tooltip_markup) = self.tooltip_markup {
216 properties.push(("tooltip-markup", tooltip_markup));
217 }
218 if let Some(ref tooltip_text) = self.tooltip_text {
219 properties.push(("tooltip-text", tooltip_text));
220 }
221 if let Some(ref valign) = self.valign {
222 properties.push(("valign", valign));
223 }
224 if let Some(ref vexpand) = self.vexpand {
225 properties.push(("vexpand", vexpand));
226 }
227 if let Some(ref vexpand_set) = self.vexpand_set {
228 properties.push(("vexpand-set", vexpand_set));
229 }
230 if let Some(ref visible) = self.visible {
231 properties.push(("visible", visible));
232 }
233 if let Some(ref width_request) = self.width_request {
234 properties.push(("width-request", width_request));
235 }
236 glib::Object::new(Switch::static_type(), &properties)
237 .expect("object new")
238 .downcast()
239 .expect("downcast")
240 }
241
242 pub fn active(mut self, active: bool) -> Self {
243 self.active = Some(active);
244 self
245 }
246
247 pub fn state(mut self, state: bool) -> Self {
248 self.state = Some(state);
249 self
250 }
251
252 pub fn app_paintable(mut self, app_paintable: bool) -> Self {
253 self.app_paintable = Some(app_paintable);
254 self
255 }
256
257 pub fn can_default(mut self, can_default: bool) -> Self {
258 self.can_default = Some(can_default);
259 self
260 }
261
262 pub fn can_focus(mut self, can_focus: bool) -> Self {
263 self.can_focus = Some(can_focus);
264 self
265 }
266
267 pub fn events(mut self, events: gdk::EventMask) -> Self {
268 self.events = Some(events);
269 self
270 }
271
272 pub fn expand(mut self, expand: bool) -> Self {
273 self.expand = Some(expand);
274 self
275 }
276
277 #[cfg(any(feature = "v3_20", feature = "dox"))]
278 pub fn focus_on_click(mut self, focus_on_click: bool) -> Self {
279 self.focus_on_click = Some(focus_on_click);
280 self
281 }
282
283 pub fn halign(mut self, halign: Align) -> Self {
284 self.halign = Some(halign);
285 self
286 }
287
288 pub fn has_default(mut self, has_default: bool) -> Self {
289 self.has_default = Some(has_default);
290 self
291 }
292
293 pub fn has_focus(mut self, has_focus: bool) -> Self {
294 self.has_focus = Some(has_focus);
295 self
296 }
297
298 pub fn has_tooltip(mut self, has_tooltip: bool) -> Self {
299 self.has_tooltip = Some(has_tooltip);
300 self
301 }
302
303 pub fn height_request(mut self, height_request: i32) -> Self {
304 self.height_request = Some(height_request);
305 self
306 }
307
308 pub fn hexpand(mut self, hexpand: bool) -> Self {
309 self.hexpand = Some(hexpand);
310 self
311 }
312
313 pub fn hexpand_set(mut self, hexpand_set: bool) -> Self {
314 self.hexpand_set = Some(hexpand_set);
315 self
316 }
317
318 pub fn is_focus(mut self, is_focus: bool) -> Self {
319 self.is_focus = Some(is_focus);
320 self
321 }
322
323 pub fn margin(mut self, margin: i32) -> Self {
324 self.margin = Some(margin);
325 self
326 }
327
328 pub fn margin_bottom(mut self, margin_bottom: i32) -> Self {
329 self.margin_bottom = Some(margin_bottom);
330 self
331 }
332
333 pub fn margin_end(mut self, margin_end: i32) -> Self {
334 self.margin_end = Some(margin_end);
335 self
336 }
337
338 pub fn margin_start(mut self, margin_start: i32) -> Self {
339 self.margin_start = Some(margin_start);
340 self
341 }
342
343 pub fn margin_top(mut self, margin_top: i32) -> Self {
344 self.margin_top = Some(margin_top);
345 self
346 }
347
348 pub fn name(mut self, name: &str) -> Self {
349 self.name = Some(name.to_string());
350 self
351 }
352
353 pub fn no_show_all(mut self, no_show_all: bool) -> Self {
354 self.no_show_all = Some(no_show_all);
355 self
356 }
357
358 pub fn opacity(mut self, opacity: f64) -> Self {
359 self.opacity = Some(opacity);
360 self
361 }
362
363 pub fn parent(mut self, parent: &Container) -> Self {
364 self.parent = Some(parent.clone());
365 self
366 }
367
368 pub fn receives_default(mut self, receives_default: bool) -> Self {
369 self.receives_default = Some(receives_default);
370 self
371 }
372
373 pub fn sensitive(mut self, sensitive: bool) -> Self {
374 self.sensitive = Some(sensitive);
375 self
376 }
377
378 pub fn tooltip_markup(mut self, tooltip_markup: &str) -> Self {
379 self.tooltip_markup = Some(tooltip_markup.to_string());
380 self
381 }
382
383 pub fn tooltip_text(mut self, tooltip_text: &str) -> Self {
384 self.tooltip_text = Some(tooltip_text.to_string());
385 self
386 }
387
388 pub fn valign(mut self, valign: Align) -> Self {
389 self.valign = Some(valign);
390 self
391 }
392
393 pub fn vexpand(mut self, vexpand: bool) -> Self {
394 self.vexpand = Some(vexpand);
395 self
396 }
397
398 pub fn vexpand_set(mut self, vexpand_set: bool) -> Self {
399 self.vexpand_set = Some(vexpand_set);
400 self
401 }
402
403 pub fn visible(mut self, visible: bool) -> Self {
404 self.visible = Some(visible);
405 self
406 }
407
408 pub fn width_request(mut self, width_request: i32) -> Self {
409 self.width_request = Some(width_request);
410 self
411 }
412}
413
414pub const NONE_SWITCH: Option<&Switch> = None;
415
416pub trait SwitchExt: 'static {
417 fn get_active(&self) -> bool;
418
419 fn get_state(&self) -> bool;
420
421 fn set_active(&self, is_active: bool);
422
423 fn set_state(&self, state: bool);
424
425 fn connect_activate<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
426
427 fn emit_activate(&self);
428
429 fn connect_state_set<F: Fn(&Self, bool) -> Inhibit + 'static>(&self, f: F) -> SignalHandlerId;
430
431 fn connect_property_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
432
433 fn connect_property_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
434}
435
436impl<O: IsA<Switch>> SwitchExt for O {
437 fn get_active(&self) -> bool {
438 unsafe {
439 from_glib(gtk_sys::gtk_switch_get_active(
440 self.as_ref().to_glib_none().0,
441 ))
442 }
443 }
444
445 fn get_state(&self) -> bool {
446 unsafe {
447 from_glib(gtk_sys::gtk_switch_get_state(
448 self.as_ref().to_glib_none().0,
449 ))
450 }
451 }
452
453 fn set_active(&self, is_active: bool) {
454 unsafe {
455 gtk_sys::gtk_switch_set_active(self.as_ref().to_glib_none().0, is_active.to_glib());
456 }
457 }
458
459 fn set_state(&self, state: bool) {
460 unsafe {
461 gtk_sys::gtk_switch_set_state(self.as_ref().to_glib_none().0, state.to_glib());
462 }
463 }
464
465 fn connect_activate<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
466 unsafe extern "C" fn activate_trampoline<P, F: Fn(&P) + 'static>(
467 this: *mut gtk_sys::GtkSwitch,
468 f: glib_sys::gpointer,
469 ) where
470 P: IsA<Switch>,
471 {
472 let f: &F = &*(f as *const F);
473 f(&Switch::from_glib_borrow(this).unsafe_cast())
474 }
475 unsafe {
476 let f: Box_<F> = Box_::new(f);
477 connect_raw(
478 self.as_ptr() as *mut _,
479 b"activate\0".as_ptr() as *const _,
480 Some(transmute(activate_trampoline::<Self, F> as usize)),
481 Box_::into_raw(f),
482 )
483 }
484 }
485
486 fn emit_activate(&self) {
487 let _ = unsafe {
488 glib::Object::from_glib_borrow(self.to_glib_none().0 as *mut gobject_sys::GObject)
489 .emit("activate", &[])
490 .unwrap()
491 };
492 }
493
494 fn connect_state_set<F: Fn(&Self, bool) -> Inhibit + 'static>(&self, f: F) -> SignalHandlerId {
495 unsafe extern "C" fn state_set_trampoline<P, F: Fn(&P, bool) -> Inhibit + 'static>(
496 this: *mut gtk_sys::GtkSwitch,
497 state: glib_sys::gboolean,
498 f: glib_sys::gpointer,
499 ) -> glib_sys::gboolean
500 where
501 P: IsA<Switch>,
502 {
503 let f: &F = &*(f as *const F);
504 f(
505 &Switch::from_glib_borrow(this).unsafe_cast(),
506 from_glib(state),
507 )
508 .to_glib()
509 }
510 unsafe {
511 let f: Box_<F> = Box_::new(f);
512 connect_raw(
513 self.as_ptr() as *mut _,
514 b"state-set\0".as_ptr() as *const _,
515 Some(transmute(state_set_trampoline::<Self, F> as usize)),
516 Box_::into_raw(f),
517 )
518 }
519 }
520
521 fn connect_property_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
522 unsafe extern "C" fn notify_active_trampoline<P, F: Fn(&P) + 'static>(
523 this: *mut gtk_sys::GtkSwitch,
524 _param_spec: glib_sys::gpointer,
525 f: glib_sys::gpointer,
526 ) where
527 P: IsA<Switch>,
528 {
529 let f: &F = &*(f as *const F);
530 f(&Switch::from_glib_borrow(this).unsafe_cast())
531 }
532 unsafe {
533 let f: Box_<F> = Box_::new(f);
534 connect_raw(
535 self.as_ptr() as *mut _,
536 b"notify::active\0".as_ptr() as *const _,
537 Some(transmute(notify_active_trampoline::<Self, F> as usize)),
538 Box_::into_raw(f),
539 )
540 }
541 }
542
543 fn connect_property_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
544 unsafe extern "C" fn notify_state_trampoline<P, F: Fn(&P) + 'static>(
545 this: *mut gtk_sys::GtkSwitch,
546 _param_spec: glib_sys::gpointer,
547 f: glib_sys::gpointer,
548 ) where
549 P: IsA<Switch>,
550 {
551 let f: &F = &*(f as *const F);
552 f(&Switch::from_glib_borrow(this).unsafe_cast())
553 }
554 unsafe {
555 let f: Box_<F> = Box_::new(f);
556 connect_raw(
557 self.as_ptr() as *mut _,
558 b"notify::state\0".as_ptr() as *const _,
559 Some(transmute(notify_state_trampoline::<Self, F> as usize)),
560 Box_::into_raw(f),
561 )
562 }
563 }
564}
565
566impl fmt::Display for Switch {
567 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
568 write!(f, "Switch")
569 }
570}