| 1 | package require Metawidget |
|---|
| 2 | |
|---|
| 3 | # name: Progressbar |
|---|
| 4 | # args: args: option-value pairs |
|---|
| 5 | # creates inner widgets, initializes variables. |
|---|
| 6 | metawidget create Progressbar { |
|---|
| 7 | # padding of 1 pixel between outer frame and progress bar |
|---|
| 8 | pack [frame $this.pads -border 1 -relief flat] -fill both -expand 1 |
|---|
| 9 | |
|---|
| 10 | # progress bar and text |
|---|
| 11 | label $this.pads.pbar -border 0 -bg darkblue |
|---|
| 12 | label $this.pads.text -border 0 -padx 1 -pady 0 -bg gray90 |
|---|
| 13 | |
|---|
| 14 | my fFraction -1 |
|---|
| 15 | |
|---|
| 16 | -format %d%% |
|---|
| 17 | set_ 0 |
|---|
| 18 | } {} -default pads |
|---|
| 19 | |
|---|
| 20 | # name: -background |
|---|
| 21 | # args: sColor: background color for progress bar |
|---|
| 22 | # redirects this option to the progress bar instead of the outer frame. |
|---|
| 23 | metawidget proc Progressbar -background { {sColor {}} } { |
|---|
| 24 | if { $sColor == {} } { |
|---|
| 25 | $this.pads.pbar cget -background |
|---|
| 26 | } else { |
|---|
| 27 | $this.pads.pbar config -background $sColor |
|---|
| 28 | } |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | # name: -foreground |
|---|
| 32 | # args: sColor: foreground color for text label |
|---|
| 33 | # redirects this option to the text label instead of the outer frame. |
|---|
| 34 | metawidget proc Progressbar -foreground { {sColor {}} } { |
|---|
| 35 | if { $sColor == {} } { |
|---|
| 36 | $this.pads.text cget -foreground |
|---|
| 37 | } else { |
|---|
| 38 | $this.pads.text config -foreground $sColor |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | # name: -font |
|---|
| 43 | # args: sFont: font for text label |
|---|
| 44 | # redirects this option to the text label instead of the outer frame. |
|---|
| 45 | metawidget proc Progressbar -font { {sFont {}} } { |
|---|
| 46 | if { $sFont == {} } { |
|---|
| 47 | $this.pads.text cget -font |
|---|
| 48 | } else { |
|---|
| 49 | $this.pads.text config -font $sFont |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | # name: -format |
|---|
| 54 | # args: sFormat: format for text label |
|---|
| 55 | # stores this format for use with the text label |
|---|
| 56 | metawidget proc Progressbar -format { sFormat } { |
|---|
| 57 | my -format $sFormat |
|---|
| 58 | |
|---|
| 59 | if { $sFormat == {} } { |
|---|
| 60 | place forget $this.pads.text |
|---|
| 61 | } else { |
|---|
| 62 | place $this.pads.text -relx .5 -rely .5 -anchor c |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | # name: set |
|---|
| 67 | # args: fProg: progress (0 .. $fBase) |
|---|
| 68 | # fBase: base value |
|---|
| 69 | # sets the progress bar. fProg must be between 0 and fBase. |
|---|
| 70 | metawidget proc Progressbar set_ { fProg {fBase 100} } { |
|---|
| 71 | set fFraction [expr 1.*$fProg/$fBase] |
|---|
| 72 | if { $fFraction == [my fFraction] } return |
|---|
| 73 | my fFraction $fFraction |
|---|
| 74 | |
|---|
| 75 | if { $fFraction == 0 } { |
|---|
| 76 | place forget $this.pads.pbar |
|---|
| 77 | } else { |
|---|
| 78 | place $this.pads.pbar -x 0 -y 0 -relw $fFraction -relh 1 |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | $this.pads.text config -text [format [my -format] $fProg] |
|---|
| 82 | update |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | metawidget command Progressbar set set_ |
|---|
| 86 | |
|---|
| 87 | metawidget option Progressbar -background -background -background |
|---|
| 88 | metawidget option Progressbar -foreground -foreground -foreground |
|---|
| 89 | metawidget option Progressbar -font -font -font |
|---|
| 90 | metawidget option Progressbar -format -format |
|---|
| 91 | |
|---|
| 92 | proc test {} { |
|---|
| 93 | . config -bg gray50 |
|---|
| 94 | wm minsize . 200 150 |
|---|
| 95 | |
|---|
| 96 | grid [frame .test -relief sunken -border 2] -column 0 -row 0 |
|---|
| 97 | pack [label .test.text -text {Progress Bar:}] -side top -anchor w -padx 15 -pady 5 |
|---|
| 98 | pack [progressbar .test.prog -width 120 -height 22] -side bottom -padx 15 -pady 5 |
|---|
| 99 | |
|---|
| 100 | grid rowconf . 1 -minsize 10 |
|---|
| 101 | grid [button .anim -text {Animate} -pady 0 -command { |
|---|
| 102 | for { set i 0 } { $i <= 100 } { incr i 5 } { |
|---|
| 103 | .test.prog set $i |
|---|
| 104 | after 100 |
|---|
| 105 | } |
|---|
| 106 | .test.prog set 0 |
|---|
| 107 | }] -column 0 -row 2 -sticky we |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | #test |
|---|