| 1 | package require Metawidget |
|---|
| 2 | |
|---|
| 3 | # name: Statusbar |
|---|
| 4 | # args: args: option-value pairs |
|---|
| 5 | # creates inner widgets, initializes variables. |
|---|
| 6 | metawidget create Statusbar { |
|---|
| 7 | # two pixel gap on top for a better look |
|---|
| 8 | pack [frame $this.gap -border 0 -height 2] -fill x |
|---|
| 9 | pack [label $this.sta -border 1 -relief sunken -anchor w] -padx 1 -side left -fill both -expand 1 |
|---|
| 10 | |
|---|
| 11 | # progress bar properties |
|---|
| 12 | my -ticks 0 |
|---|
| 13 | my -progress 0 |
|---|
| 14 | |
|---|
| 15 | my iVisible 0 |
|---|
| 16 | my lStack {} |
|---|
| 17 | |
|---|
| 18 | -state normal |
|---|
| 19 | } {} -default sta |
|---|
| 20 | |
|---|
| 21 | # name: push |
|---|
| 22 | # args: sText: text to push |
|---|
| 23 | # overlays the current text with sText. can be removed with pop. |
|---|
| 24 | metawidget proc Statusbar push { sText } { |
|---|
| 25 | my lStack [linsert [my lStack] end [$this.sta cget -text]] |
|---|
| 26 | $this.sta config -text $sText |
|---|
| 27 | update |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | # name: pop |
|---|
| 31 | # args: - |
|---|
| 32 | # removes the last text defined with push. |
|---|
| 33 | metawidget proc Statusbar pop {} { |
|---|
| 34 | $this.sta config -text [lindex [my lStack] end] |
|---|
| 35 | my lStack [lreplace [my lStack] end end] |
|---|
| 36 | update |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | # name: add |
|---|
| 40 | # args: sTag: name for new field |
|---|
| 41 | # args: option-value pairs |
|---|
| 42 | # installs a new field that can be assigned with an icon and a text. |
|---|
| 43 | # sTag must be a unique identifier, args are passed to itemconf |
|---|
| 44 | metawidget proc Statusbar add { sTag args } { |
|---|
| 45 | if { [winfo exists $this._$sTag] } { |
|---|
| 46 | error "Statusbar $this: Item $sTag already exists." |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | pack [frame $this._$sTag -border 1 -relief sunken] -side left -fill y -padx 1 |
|---|
| 50 | pack [label $this._$sTag.icon -border 0 -pady 0] -side left -fill y |
|---|
| 51 | pack [label $this._$sTag.text -border 0 -pady 0] -side left -fill y |
|---|
| 52 | |
|---|
| 53 | eval itemconf $sTag $args |
|---|
| 54 | |
|---|
| 55 | return $this._$sTag |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | # name: delete |
|---|
| 59 | # args: args: list of fields as created by add |
|---|
| 60 | # deletes all fields specified by the elements of args. |
|---|
| 61 | metawidget proc Statusbar delete { args } { |
|---|
| 62 | foreach sTag $args { |
|---|
| 63 | destroy $this._$sTag |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | # name: itemconf |
|---|
| 68 | # args: sTag: field id (see add) |
|---|
| 69 | # args: option-value pairs |
|---|
| 70 | # configures a field. most options are passed to the inner icon as well as to |
|---|
| 71 | # the label. some options are passed only to the widget where they make sense. |
|---|
| 72 | metawidget proc Statusbar itemconf { sTag args } { |
|---|
| 73 | if { ! [winfo exists $this._$sTag] } { |
|---|
| 74 | error "Statusbar $this: Item $sTag does not exist." |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | foreach { sOption sValue } $args { |
|---|
| 78 | switch -- $sOption { |
|---|
| 79 | -width { |
|---|
| 80 | $this._$sTag config -width $sValue |
|---|
| 81 | pack propagate $this._$sTag [expr !$sValue] |
|---|
| 82 | } |
|---|
| 83 | -border | -relief { |
|---|
| 84 | $this._$sTag config $sOption $sValue |
|---|
| 85 | } |
|---|
| 86 | -image { |
|---|
| 87 | $this._$sTag.icon config $sOption $sValue |
|---|
| 88 | } |
|---|
| 89 | -text { |
|---|
| 90 | $this._$sTag.text config $sOption $sValue |
|---|
| 91 | } |
|---|
| 92 | default { |
|---|
| 93 | $this._$sTag.icon config $sOption $sValue |
|---|
| 94 | $this._$sTag.text config $sOption $sValue |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | # name: itemcget |
|---|
| 101 | # args: sTag: field id (see add) |
|---|
| 102 | # sOption: option name |
|---|
| 103 | # returns the value of a field option. |
|---|
| 104 | metawidget proc Statusbar itemcget { sTag sOption } { |
|---|
| 105 | if { ! [winfo exists $this._$sTag] } { |
|---|
| 106 | error "Statusbar $this: Item $sTag does not exist." |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | switch -- $sOption { |
|---|
| 110 | -border | -relief | -width { return [$this._$sTag cget $sOption] } |
|---|
| 111 | -image { return [$this._$sTag.icon cget $sOption] } |
|---|
| 112 | default { return [$this._$sTag.text cget $sOption] } |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | # name: -ticks |
|---|
| 117 | # args: iTicks: number of "lights" for the progress bar |
|---|
| 118 | # displays a progress bar with iTicks gray rectangles. a progress value is set |
|---|
| 119 | # with -progress. if iTicks is 0, the progress bar is deleted. |
|---|
| 120 | metawidget proc Statusbar -ticks { iTicks } { |
|---|
| 121 | my -ticks $iTicks |
|---|
| 122 | |
|---|
| 123 | catch { destroy $this.pro } |
|---|
| 124 | if { ! $iTicks } return |
|---|
| 125 | |
|---|
| 126 | pack [frame $this.pro -border 1 -relief sunken] -side right -fill y -padx 1 |
|---|
| 127 | |
|---|
| 128 | for { set i 0 } { $i < $iTicks } { incr i } { |
|---|
| 129 | pack [frame $this.pro.pc$i -width 8 -bg gray] -side left -fill y -padx 1 -pady 1 |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | # name: -progress |
|---|
| 134 | # args: fProg: progress (0-1) |
|---|
| 135 | # displays a progress in the progress bar. fProg must be between 0 and 1. |
|---|
| 136 | metawidget proc Statusbar -progress { fProg } { |
|---|
| 137 | my -progress $fProg |
|---|
| 138 | |
|---|
| 139 | if { $fProg < 0 } { set fProg 0 } |
|---|
| 140 | if { $fProg > 1 } { set fProg 1 } |
|---|
| 141 | |
|---|
| 142 | set iVisible [expr round( $fProg * [my -ticks] )] |
|---|
| 143 | if { $iVisible == [my iVisible] } return |
|---|
| 144 | my iVisible $iVisible |
|---|
| 145 | |
|---|
| 146 | for { set i 0 } { $i < $iVisible } { incr i } { |
|---|
| 147 | $this.pro.pc$i config -bg indianred |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | for { set i $iVisible } { $i < [my -ticks] } { incr i } { |
|---|
| 151 | $this.pro.pc$i config -bg gray |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | update |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | # name: -state |
|---|
| 158 | # args: sState: normal or withdrawn |
|---|
| 159 | # either displays the Statusbar or withdraws if from the screen. ensures that |
|---|
| 160 | # the Statusbar is packed at the bottom as the first element in the order. |
|---|
| 161 | metawidget proc Statusbar -state { sState } { |
|---|
| 162 | my -state [mkw.complete $sState {normal withdrawn}] |
|---|
| 163 | |
|---|
| 164 | if { [my -state] == "normal" } { |
|---|
| 165 | set sFirst [lindex [pack slaves [winfo parent $this]] 0] |
|---|
| 166 | if { $sFirst != {} } { |
|---|
| 167 | pack $this -side bottom -fill x -before $sFirst |
|---|
| 168 | } else { |
|---|
| 169 | pack $this -side bottom -fill x |
|---|
| 170 | } |
|---|
| 171 | } else { |
|---|
| 172 | pack forget $this |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | metawidget command Statusbar push push |
|---|
| 177 | metawidget command Statusbar pop pop |
|---|
| 178 | metawidget command Statusbar add add |
|---|
| 179 | metawidget command Statusbar delete delete |
|---|
| 180 | metawidget command Statusbar itemconf itemconf |
|---|
| 181 | metawidget command Statusbar itemcget itemcget |
|---|
| 182 | |
|---|
| 183 | metawidget option Statusbar -ticks -ticks |
|---|
| 184 | metawidget option Statusbar -progress -progress |
|---|
| 185 | metawidget option Statusbar -state -state |
|---|
| 186 | |
|---|
| 187 | proc test {} { |
|---|
| 188 | . config -bg gray50 |
|---|
| 189 | wm minsize . 400 150 |
|---|
| 190 | |
|---|
| 191 | image create photo p1 -file ./demos/images/Notebook.gif |
|---|
| 192 | image create photo p2 -file ./demos/images/Compass.gif |
|---|
| 193 | |
|---|
| 194 | pack [button .b1 -text {Show Progress} -command { |
|---|
| 195 | .s push {Loading something...} |
|---|
| 196 | foreach fProg {0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1 0} { |
|---|
| 197 | .s config -progress $fProg |
|---|
| 198 | after 100 |
|---|
| 199 | } |
|---|
| 200 | .s pop |
|---|
| 201 | }] -fill x |
|---|
| 202 | |
|---|
| 203 | pack [button .b2 -text {Add Field} -command { |
|---|
| 204 | if { [catch {incr iSeq}] } { |
|---|
| 205 | set iSeq 0 |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | .s add field$iSeq -text "[lindex {Tcl Tk is fun!} $iSeq] " |
|---|
| 209 | }] -fill x |
|---|
| 210 | |
|---|
| 211 | pack [button .b3 -text {Change Icon} -command { |
|---|
| 212 | .s itemconf myField -image p2 |
|---|
| 213 | }] -fill x |
|---|
| 214 | |
|---|
| 215 | statusbar .s -ticks 10 |
|---|
| 216 | |
|---|
| 217 | .s config -text {I am a Statusbar!} |
|---|
| 218 | .s add myField -text { Field } -image p1 |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | #test |
|---|