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