[3330] | 1 | # -*- mode: tcl; indent-tabs-mode: nil -*- |
---|
[1] | 2 | # ---------------------------------------------------------------------- |
---|
| 3 | # COMPONENT: gauge - compact readout for real values |
---|
| 4 | # |
---|
| 5 | # This widget is a readout for a real value. It has a little glyph |
---|
| 6 | # filled with color according to the value, followed by a numeric |
---|
| 7 | # representation of the value itself. The value can be edited, and |
---|
| 8 | # a list of predefined values can be associated with a menu that |
---|
| 9 | # drops down from the value. |
---|
| 10 | # ====================================================================== |
---|
| 11 | # AUTHOR: Michael McLennan, Purdue University |
---|
[3177] | 12 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[115] | 13 | # |
---|
| 14 | # See the file "license.terms" for information on usage and |
---|
| 15 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
[1] | 16 | # ====================================================================== |
---|
| 17 | package require Itk |
---|
| 18 | package require BLT |
---|
| 19 | |
---|
[22] | 20 | option add *Gauge.sampleWidth 30 widgetDefault |
---|
| 21 | option add *Gauge.sampleHeight 20 widgetDefault |
---|
[3642] | 22 | option add *Gauge.borderWidth 2 widgetDefault |
---|
| 23 | option add *Gauge.relief sunken widgetDefault |
---|
[1] | 24 | option add *Gauge.valuePosition "right" widgetDefault |
---|
| 25 | option add *Gauge.textBackground #cccccc widgetDefault |
---|
| 26 | option add *Gauge.editable yes widgetDefault |
---|
| 27 | |
---|
| 28 | itcl::class Rappture::Gauge { |
---|
| 29 | inherit itk::Widget |
---|
| 30 | |
---|
| 31 | itk_option define -editable editable Editable "" |
---|
[437] | 32 | itk_option define -state state State "normal" |
---|
[1] | 33 | itk_option define -spectrum spectrum Spectrum "" |
---|
[22] | 34 | itk_option define -type type Type "real" |
---|
[1] | 35 | itk_option define -units units Units "" |
---|
| 36 | itk_option define -minvalue minValue MinValue "" |
---|
| 37 | itk_option define -maxvalue maxValue MaxValue "" |
---|
| 38 | itk_option define -presets presets Presets "" |
---|
| 39 | itk_option define -valueposition valuePosition ValuePosition "" |
---|
| 40 | itk_option define -image image Image "" |
---|
[22] | 41 | itk_option define -samplewidth sampleWidth SampleWidth 0 |
---|
| 42 | itk_option define -sampleheight sampleHeight SampleHeight 0 |
---|
[3186] | 43 | itk_option define -log log Log "" |
---|
[5057] | 44 | itk_option define -validatecommand validateCommand ValidateCommand "" |
---|
[1] | 45 | |
---|
| 46 | constructor {args} { # defined below } |
---|
| 47 | |
---|
| 48 | public method value {args} |
---|
[17] | 49 | public method edit {option} |
---|
[22] | 50 | public method bump {delta} |
---|
[1] | 51 | |
---|
| 52 | protected method _redraw {} |
---|
| 53 | protected method _resize {} |
---|
| 54 | protected method _hilite {comp state} |
---|
| 55 | protected method _editor {option args} |
---|
| 56 | protected method _presets {option} |
---|
[22] | 57 | protected method _layout {} |
---|
[3186] | 58 | protected method _log {event args} |
---|
[1] | 59 | |
---|
| 60 | private variable _value 0 ;# value for this widget |
---|
| 61 | |
---|
| 62 | blt::bitmap define GaugeArrow { |
---|
[1850] | 63 | #define arrow_width 9 |
---|
| 64 | #define arrow_height 4 |
---|
| 65 | static unsigned char arrow_bits[] = { |
---|
| 66 | 0x7f, 0x00, 0x3e, 0x00, 0x1c, 0x00, 0x08, 0x00}; |
---|
[1] | 67 | } |
---|
| 68 | } |
---|
[1850] | 69 | |
---|
[1] | 70 | itk::usual Gauge { |
---|
| 71 | keep -cursor -font -foreground -background |
---|
| 72 | keep -selectbackground -selectforeground -selectborderwidth |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | # ---------------------------------------------------------------------- |
---|
| 76 | # CONSTRUCTOR |
---|
| 77 | # ---------------------------------------------------------------------- |
---|
| 78 | itcl::body Rappture::Gauge::constructor {args} { |
---|
[3642] | 79 | itk_option remove hull.borderwidth hull.relief |
---|
| 80 | component hull configure -borderwidth 0 |
---|
| 81 | |
---|
[1] | 82 | itk_component add icon { |
---|
[1850] | 83 | canvas $itk_interior.icon -width 1 -height 1 \ |
---|
| 84 | -borderwidth 0 -highlightthickness 0 |
---|
[1] | 85 | } { |
---|
[1850] | 86 | usual |
---|
| 87 | ignore -highlightthickness -highlightbackground -highlightcolor |
---|
[3642] | 88 | ignore -borderwidth -relief |
---|
[1] | 89 | } |
---|
| 90 | bind $itk_component(icon) <Configure> [itcl::code $this _redraw] |
---|
| 91 | |
---|
| 92 | itk_component add -protected vframe { |
---|
[1850] | 93 | frame $itk_interior.vframe |
---|
[3642] | 94 | } { |
---|
| 95 | keep -borderwidth -relief |
---|
[1] | 96 | } |
---|
| 97 | |
---|
| 98 | itk_component add value { |
---|
[3642] | 99 | label $itk_component(vframe).value -width 7 \ |
---|
| 100 | -borderwidth 1 -relief flat -textvariable [itcl::scope _value] |
---|
[1] | 101 | } { |
---|
[5089] | 102 | keep -font |
---|
[1850] | 103 | rename -background -textbackground textBackground Background |
---|
[1] | 104 | } |
---|
| 105 | pack $itk_component(value) -side left -expand yes -fill both |
---|
| 106 | |
---|
| 107 | bind $itk_component(value) <Enter> [itcl::code $this _hilite value on] |
---|
| 108 | bind $itk_component(value) <Leave> [itcl::code $this _hilite value off] |
---|
| 109 | |
---|
[17] | 110 | bind $itk_component(value) <<Cut>> [itcl::code $this edit cut] |
---|
| 111 | bind $itk_component(value) <<Copy>> [itcl::code $this edit copy] |
---|
| 112 | bind $itk_component(value) <<Paste>> [itcl::code $this edit paste] |
---|
| 113 | |
---|
| 114 | itk_component add emenu { |
---|
[1850] | 115 | menu $itk_component(value).menu -tearoff 0 |
---|
[17] | 116 | } { |
---|
[1850] | 117 | usual |
---|
| 118 | ignore -tearoff |
---|
[17] | 119 | } |
---|
| 120 | $itk_component(emenu) add command -label "Cut" -accelerator "^X" \ |
---|
[1850] | 121 | -command [list event generate $itk_component(value) <<Cut>>] |
---|
[17] | 122 | $itk_component(emenu) add command -label "Copy" -accelerator "^C" \ |
---|
[1850] | 123 | -command [list event generate $itk_component(value) <<Copy>>] |
---|
[17] | 124 | $itk_component(emenu) add command -label "Paste" -accelerator "^V" \ |
---|
[1850] | 125 | -command [list event generate $itk_component(value) <<Paste>>] |
---|
[437] | 126 | bind $itk_component(value) <<PopupMenu>> \ |
---|
[1850] | 127 | [itcl::code $this _editor menu %X %Y] |
---|
[17] | 128 | |
---|
[1] | 129 | itk_component add editor { |
---|
[1850] | 130 | Rappture::Editor $itk_interior.editor \ |
---|
| 131 | -activatecommand [itcl::code $this _editor activate] \ |
---|
| 132 | -validatecommand [itcl::code $this _editor validate] \ |
---|
| 133 | -applycommand [itcl::code $this _editor apply] |
---|
[1] | 134 | } |
---|
| 135 | bind $itk_component(value) <ButtonPress> \ |
---|
[1850] | 136 | [itcl::code $this _editor popup] |
---|
[1] | 137 | |
---|
[22] | 138 | |
---|
| 139 | itk_component add spinner { |
---|
[1850] | 140 | frame $itk_component(vframe).spinner |
---|
[22] | 141 | } |
---|
| 142 | |
---|
[3654] | 143 | itk_component add spinup { |
---|
| 144 | button $itk_component(spinner).up -image [Rappture::icon intplus] \ |
---|
[1850] | 145 | -borderwidth 1 -relief raised -highlightthickness 0 \ |
---|
[3654] | 146 | -command [itcl::code $this bump 1] |
---|
[22] | 147 | } { |
---|
[1850] | 148 | usual |
---|
| 149 | ignore -borderwidth -highlightthickness |
---|
[3642] | 150 | rename -background -buttonbackground buttonBackground Background |
---|
[22] | 151 | } |
---|
[3654] | 152 | pack $itk_component(spinup) -side left -expand yes -fill both |
---|
[22] | 153 | |
---|
[3654] | 154 | itk_component add spindn { |
---|
| 155 | button $itk_component(spinner).down -image [Rappture::icon intminus] \ |
---|
[1850] | 156 | -borderwidth 1 -relief raised -highlightthickness 0 \ |
---|
[3654] | 157 | -command [itcl::code $this bump -1] |
---|
[22] | 158 | } { |
---|
[1850] | 159 | usual |
---|
| 160 | ignore -borderwidth -highlightthickness |
---|
[3642] | 161 | rename -background -buttonbackground buttonBackground Background |
---|
[22] | 162 | } |
---|
[3654] | 163 | pack $itk_component(spindn) -side right -expand yes -fill both |
---|
[22] | 164 | |
---|
[1] | 165 | itk_component add presets { |
---|
[1850] | 166 | button $itk_component(vframe).psbtn -bitmap GaugeArrow \ |
---|
[3642] | 167 | -borderwidth 1 -highlightthickness 0 -relief raised |
---|
[1] | 168 | } { |
---|
[1850] | 169 | usual |
---|
| 170 | ignore -borderwidth -relief -highlightthickness |
---|
[1] | 171 | } |
---|
| 172 | |
---|
| 173 | bind $itk_component(presets) <Enter> [itcl::code $this _hilite presets on] |
---|
| 174 | bind $itk_component(presets) <Leave> [itcl::code $this _hilite presets off] |
---|
| 175 | |
---|
| 176 | itk_component add presetlist { |
---|
[1850] | 177 | Rappture::Dropdownlist $itk_component(presets).plist \ |
---|
| 178 | -postcommand [itcl::code $this _presets post] \ |
---|
| 179 | -unpostcommand [itcl::code $this _presets unpost] \ |
---|
[1] | 180 | } |
---|
| 181 | |
---|
| 182 | bind $itk_component(presetlist) <<DropdownlistSelect>> \ |
---|
[1850] | 183 | [itcl::code $this _presets select] |
---|
[1] | 184 | |
---|
| 185 | $itk_component(presets) configure -command \ |
---|
[1850] | 186 | [list $itk_component(presetlist) post $itk_component(vframe) left] |
---|
[1] | 187 | |
---|
| 188 | eval itk_initialize $args |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | # ---------------------------------------------------------------------- |
---|
| 192 | # USAGE: value ?-check? ?<newval>? |
---|
| 193 | # |
---|
| 194 | # Clients use this to query/set the value for this widget. With |
---|
| 195 | # no args, it returns the current value for the widget. If the |
---|
| 196 | # <newval> is specified, it sets the value of the widget and |
---|
| 197 | # sends a <<Value>> event. If the -check flag is included, the |
---|
| 198 | # new value is not actually applied, but just checked for correctness. |
---|
| 199 | # ---------------------------------------------------------------------- |
---|
| 200 | itcl::body Rappture::Gauge::value {args} { |
---|
| 201 | set onlycheck 0 |
---|
| 202 | set i [lsearch -exact $args -check] |
---|
| 203 | if {$i >= 0} { |
---|
[1850] | 204 | set onlycheck 1 |
---|
| 205 | set args [lreplace $args $i $i] |
---|
[1] | 206 | } |
---|
| 207 | |
---|
| 208 | if {[llength $args] == 1} { |
---|
[1850] | 209 | # |
---|
| 210 | # If this gauge has -units, try to convert the incoming |
---|
| 211 | # value to that system of units. Also, make sure that |
---|
| 212 | # the value is bound by any min/max value constraints. |
---|
| 213 | # |
---|
| 214 | # Keep track of the inputted units so we can give a |
---|
| 215 | # response about min and max values in familiar units. |
---|
| 216 | # |
---|
[3647] | 217 | set newval [set nv [string trim [lindex $args 0]]] |
---|
[1850] | 218 | set units $itk_option(-units) |
---|
| 219 | if {"" != $units} { |
---|
| 220 | set newval [Rappture::Units::convert $newval -context $units] |
---|
| 221 | set nvUnits [Rappture::Units::Search::for $newval] |
---|
| 222 | if { "" == $nvUnits} { |
---|
| 223 | set msg [Rappture::Units::description $units] |
---|
[3510] | 224 | error "unrecognized units in value \"$newval\": should be value with units of $msg" |
---|
[1850] | 225 | } |
---|
| 226 | set nv [Rappture::Units::convert $nv \ |
---|
| 227 | -context $units -to $units -units off] |
---|
[578] | 228 | |
---|
[1850] | 229 | # Normalize the units name |
---|
| 230 | set newval [Rappture::Units::convert $newval -units off]$nvUnits |
---|
| 231 | } |
---|
[1] | 232 | |
---|
[1850] | 233 | switch -- $itk_option(-type) { |
---|
| 234 | integer { |
---|
| 235 | if { [scan $nv "%g" value] != 1 || int($nv) != $value } { |
---|
| 236 | error "bad value \"$nv\": should be an integer value" |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | real { |
---|
[5057] | 240 | # "scan" will reject the number if the string is "NaN" or |
---|
| 241 | # "Inf" or the empty string. It also is accepts large numbers |
---|
| 242 | # (e.g. 111111111111111111111) that "string is double" |
---|
| 243 | # rejects. The problem with "scan" is that it doesn't care if |
---|
| 244 | # there are extra characters trailing the number (eg. "123a"). |
---|
| 245 | # The extra %s substitution is used to detect this case. |
---|
[3050] | 246 | if { [scan $nv "%g%s" dummy1 dummy2] != 1 } { |
---|
[1850] | 247 | error "bad value \"$nv\": should be a real number" |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | } |
---|
[553] | 251 | |
---|
[1850] | 252 | if {"" != $itk_option(-minvalue)} { |
---|
| 253 | set convMinVal [set minv $itk_option(-minvalue)] |
---|
| 254 | if {"" != $units} { |
---|
| 255 | set minv [Rappture::Units::convert $minv \ |
---|
| 256 | -context $units -to $units -units off] |
---|
| 257 | set convMinVal [Rappture::Units::convert \ |
---|
| 258 | $itk_option(-minvalue) -context $units -to $nvUnits] |
---|
| 259 | } else { |
---|
| 260 | set newval [format "%g" $newval] |
---|
| 261 | } |
---|
[556] | 262 | |
---|
[1850] | 263 | # fix for the case when the user tries to |
---|
| 264 | # compare values like minv=-500 nv=-0600 |
---|
| 265 | set nv [format "%g" $nv] |
---|
| 266 | set minv [format "%g" $minv] |
---|
[552] | 267 | |
---|
[1850] | 268 | if {$nv < $minv} { |
---|
| 269 | error "minimum value allowed here is $convMinVal" |
---|
| 270 | } |
---|
| 271 | } |
---|
[1] | 272 | |
---|
[1850] | 273 | if {"" != $itk_option(-maxvalue)} { |
---|
| 274 | set convMaxVal [set maxv $itk_option(-maxvalue)] |
---|
| 275 | if {"" != $units} { |
---|
| 276 | set maxv [Rappture::Units::convert $maxv \ |
---|
| 277 | -context $units -to $units -units off] |
---|
| 278 | set convMaxVal [Rappture::Units::convert \ |
---|
| 279 | $itk_option(-maxvalue) -context $units -to $nvUnits] |
---|
| 280 | } else { |
---|
| 281 | set newval [format "%g" $newval] |
---|
| 282 | } |
---|
[552] | 283 | |
---|
[1850] | 284 | # fix for the case when the user tries to |
---|
| 285 | # compare values like maxv=500 nv=0600 |
---|
| 286 | set nv [format "%g" $nv] |
---|
| 287 | set maxv [format "%g" $maxv] |
---|
[552] | 288 | |
---|
[1850] | 289 | if {$nv > $maxv} { |
---|
| 290 | error "maximum value allowed here is $convMaxVal" |
---|
| 291 | } |
---|
| 292 | } |
---|
[11] | 293 | |
---|
[5057] | 294 | # |
---|
| 295 | # If there's a -validatecommand option, then invoke the code |
---|
| 296 | # now to check the new value. |
---|
| 297 | # |
---|
| 298 | if {[string length $itk_option(-validatecommand)] > 0} { |
---|
| 299 | set cmd "uplevel #0 [list $itk_option(-validatecommand) [list $newval]]" |
---|
| 300 | set result [eval $cmd] |
---|
| 301 | } |
---|
| 302 | |
---|
[1850] | 303 | if {$onlycheck} { |
---|
| 304 | return |
---|
| 305 | } |
---|
[578] | 306 | |
---|
[1850] | 307 | set _value $newval |
---|
[578] | 308 | |
---|
[1850] | 309 | _redraw |
---|
| 310 | event generate $itk_component(hull) <<Value>> |
---|
[1] | 311 | |
---|
| 312 | } elseif {[llength $args] != 0} { |
---|
[1850] | 313 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
[1] | 314 | } |
---|
| 315 | return $_value |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | # ---------------------------------------------------------------------- |
---|
[17] | 319 | # USAGE: edit cut |
---|
| 320 | # USAGE: edit copy |
---|
| 321 | # USAGE: edit paste |
---|
| 322 | # |
---|
| 323 | # Used internally to handle cut/copy/paste operations for the current |
---|
| 324 | # value. Usually invoked by <<Cut>>, <<Copy>>, <<Paste>> events, but |
---|
| 325 | # can also be called directly through this method. |
---|
| 326 | # ---------------------------------------------------------------------- |
---|
| 327 | itcl::body Rappture::Gauge::edit {option} { |
---|
[437] | 328 | if {$itk_option(-state) == "disabled"} { |
---|
[1850] | 329 | return ;# disabled? then bail out here! |
---|
[437] | 330 | } |
---|
[17] | 331 | switch -- $option { |
---|
[1850] | 332 | cut { |
---|
| 333 | edit copy |
---|
| 334 | _editor popup |
---|
| 335 | $itk_component(editor) value "" |
---|
| 336 | $itk_component(editor) deactivate |
---|
| 337 | } |
---|
| 338 | copy { |
---|
| 339 | clipboard clear |
---|
| 340 | clipboard append $_value |
---|
| 341 | } |
---|
| 342 | paste { |
---|
| 343 | _editor popup |
---|
| 344 | $itk_component(editor) value [clipboard get] |
---|
| 345 | $itk_component(editor) deactivate |
---|
| 346 | } |
---|
| 347 | default { |
---|
| 348 | error "bad option \"$option\": should be cut, copy, paste" |
---|
| 349 | } |
---|
[17] | 350 | } |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | # ---------------------------------------------------------------------- |
---|
[22] | 354 | # USAGE: bump <delta> |
---|
| 355 | # |
---|
| 356 | # Changes the current value up/down by the <delta> value. Used |
---|
| 357 | # internally by the up/down spinner buttons when the value is |
---|
| 358 | # -type integer. |
---|
| 359 | # ---------------------------------------------------------------------- |
---|
| 360 | itcl::body Rappture::Gauge::bump {delta} { |
---|
| 361 | set val $_value |
---|
| 362 | if {$val == ""} { |
---|
[1850] | 363 | set val 0 |
---|
[22] | 364 | } |
---|
[442] | 365 | if {[catch {value [expr {$val+$delta}]} result]} { |
---|
[1850] | 366 | if {[regexp {allowed here is (.+)} $result match newval]} { |
---|
| 367 | set _value $newval |
---|
| 368 | $itk_component(value) configure -text $newval |
---|
| 369 | } |
---|
| 370 | if {[regexp {^bad.*: +(.)(.+)} $result match first tail] |
---|
| 371 | || [regexp {(.)(.+)} $result match first tail]} { |
---|
| 372 | set result "[string toupper $first]$tail" |
---|
| 373 | } |
---|
| 374 | bell |
---|
| 375 | Rappture::Tooltip::cue $itk_component(value) $result |
---|
[3186] | 376 | _log warning $result |
---|
[1850] | 377 | return 0 |
---|
[442] | 378 | } |
---|
[3186] | 379 | _log input [value] |
---|
[22] | 380 | } |
---|
| 381 | |
---|
| 382 | # ---------------------------------------------------------------------- |
---|
[1] | 383 | # USAGE: _redraw |
---|
| 384 | # |
---|
| 385 | # Used internally to redraw the gauge on the internal canvas based |
---|
| 386 | # on the current value and the size of the widget. In this simple |
---|
| 387 | # base class, the gauge is drawn as a colored block, with an optional |
---|
| 388 | # image in the middle of it. |
---|
| 389 | # ---------------------------------------------------------------------- |
---|
| 390 | itcl::body Rappture::Gauge::_redraw {} { |
---|
| 391 | set c $itk_component(icon) |
---|
| 392 | set w [winfo width $c] |
---|
| 393 | set h [winfo height $c] |
---|
| 394 | |
---|
| 395 | if {"" == [$c find all]} { |
---|
[1850] | 396 | # first time around, create the items |
---|
| 397 | $c create rectangle 0 0 1 1 -outline black -tags block |
---|
| 398 | $c create image 0 0 -anchor center -image "" -tags bimage |
---|
| 399 | $c create rectangle 0 0 1 1 -outline "" -fill "" -stipple gray50 -tags screen |
---|
[1] | 400 | } |
---|
| 401 | |
---|
| 402 | if {"" != $itk_option(-spectrum)} { |
---|
[1850] | 403 | set color [$itk_option(-spectrum) get $_value] |
---|
[1] | 404 | } else { |
---|
[1850] | 405 | set color "" |
---|
[1] | 406 | } |
---|
| 407 | |
---|
| 408 | # update the items based on current values |
---|
| 409 | $c coords block 0 0 [expr {$w-1}] [expr {$h-1}] |
---|
[437] | 410 | $c coords screen 0 0 $w $h |
---|
[1] | 411 | $c itemconfigure block -fill $color |
---|
| 412 | |
---|
| 413 | $c coords bimage [expr {0.5*$w}] [expr {0.5*$h}] |
---|
[437] | 414 | |
---|
| 415 | if {$itk_option(-state) == "disabled"} { |
---|
[1850] | 416 | $c itemconfigure screen -fill white |
---|
[437] | 417 | } else { |
---|
[1850] | 418 | $c itemconfigure screen -fill "" |
---|
[437] | 419 | } |
---|
[1] | 420 | } |
---|
| 421 | |
---|
| 422 | # ---------------------------------------------------------------------- |
---|
| 423 | # USAGE: _resize |
---|
| 424 | # |
---|
| 425 | # Used internally to resize the internal canvas based on the -image |
---|
| 426 | # option or the size of the text. |
---|
| 427 | # ---------------------------------------------------------------------- |
---|
| 428 | itcl::body Rappture::Gauge::_resize {} { |
---|
[22] | 429 | set w 0 |
---|
| 430 | set h 0 |
---|
| 431 | |
---|
| 432 | if {"" != $itk_option(-image) || "" != $itk_option(-spectrum)} { |
---|
[1850] | 433 | if {$itk_option(-samplewidth) > 0} { |
---|
| 434 | set w $itk_option(-samplewidth) |
---|
| 435 | } else { |
---|
| 436 | if {$itk_option(-image) != ""} { |
---|
| 437 | set w [expr {[image width $itk_option(-image)]+4}] |
---|
| 438 | } else { |
---|
| 439 | set w [winfo reqheight $itk_component(value)] |
---|
| 440 | } |
---|
| 441 | } |
---|
[1] | 442 | |
---|
[1850] | 443 | if {$itk_option(-sampleheight) > 0} { |
---|
| 444 | set h $itk_option(-sampleheight) |
---|
| 445 | } else { |
---|
| 446 | if {$itk_option(-image) != ""} { |
---|
| 447 | set h [expr {[image height $itk_option(-image)]+4}] |
---|
| 448 | } else { |
---|
| 449 | set h [winfo reqheight $itk_component(value)] |
---|
| 450 | } |
---|
| 451 | } |
---|
[1] | 452 | } |
---|
| 453 | |
---|
[22] | 454 | if {$w > 0 && $h > 0} { |
---|
[1850] | 455 | $itk_component(icon) configure -width $w -height $h |
---|
[22] | 456 | } |
---|
[1] | 457 | } |
---|
| 458 | |
---|
| 459 | # ---------------------------------------------------------------------- |
---|
| 460 | # USAGE: _hilite <component> <state> |
---|
| 461 | # |
---|
| 462 | # Used internally to resize the internal canvas based on the -image |
---|
| 463 | # option or the size of the text. |
---|
| 464 | # ---------------------------------------------------------------------- |
---|
| 465 | itcl::body Rappture::Gauge::_hilite {comp state} { |
---|
[437] | 466 | if {$itk_option(-state) == "disabled"} { |
---|
[1850] | 467 | set state 0 ;# disabled? then don't hilite |
---|
[437] | 468 | } |
---|
[1] | 469 | if {$comp == "value" && !$itk_option(-editable)} { |
---|
[1850] | 470 | $itk_component(value) configure -relief flat |
---|
| 471 | return |
---|
[1] | 472 | } |
---|
| 473 | |
---|
| 474 | if {$state} { |
---|
[3739] | 475 | $itk_component($comp) configure -relief flat |
---|
[1] | 476 | } else { |
---|
[3642] | 477 | if {$comp eq "presets"} { |
---|
| 478 | $itk_component($comp) configure -relief raised |
---|
| 479 | } else { |
---|
| 480 | $itk_component($comp) configure -relief flat |
---|
| 481 | } |
---|
[1] | 482 | } |
---|
| 483 | } |
---|
| 484 | |
---|
| 485 | # ---------------------------------------------------------------------- |
---|
| 486 | # USAGE: _editor popup |
---|
| 487 | # USAGE: _editor activate |
---|
| 488 | # USAGE: _editor validate <value> |
---|
| 489 | # USAGE: _editor apply <value> |
---|
[437] | 490 | # USAGE: _editor menu <rootx> <rooty> |
---|
[1] | 491 | # |
---|
| 492 | # Used internally to handle the various functions of the pop-up |
---|
| 493 | # editor for the value of this gauge. |
---|
| 494 | # ---------------------------------------------------------------------- |
---|
| 495 | itcl::body Rappture::Gauge::_editor {option args} { |
---|
[437] | 496 | if {$itk_option(-state) == "disabled"} { |
---|
[1850] | 497 | return ;# disabled? then bail out here! |
---|
[437] | 498 | } |
---|
[1] | 499 | switch -- $option { |
---|
[1850] | 500 | popup { |
---|
| 501 | if {$itk_option(-editable)} { |
---|
| 502 | $itk_component(editor) activate |
---|
| 503 | } |
---|
| 504 | } |
---|
| 505 | activate { |
---|
| 506 | return [list text $_value \ |
---|
| 507 | x [winfo rootx $itk_component(value)] \ |
---|
| 508 | y [winfo rooty $itk_component(value)] \ |
---|
| 509 | w [winfo width $itk_component(value)] \ |
---|
| 510 | h [winfo height $itk_component(value)]] |
---|
| 511 | } |
---|
| 512 | validate { |
---|
| 513 | if {[llength $args] != 1} { |
---|
| 514 | error "wrong # args: should be \"_editor validate val\"" |
---|
| 515 | } |
---|
| 516 | set val [lindex $args 0] |
---|
[1] | 517 | |
---|
[1850] | 518 | if {[catch {value -check $val} result]} { |
---|
| 519 | if {[regexp {allowed here is (.+)} $result match newval]} { |
---|
| 520 | $itk_component(editor) value $newval |
---|
| 521 | } |
---|
| 522 | if {[regexp {^bad.*: +(.)(.+)} $result match first tail] |
---|
| 523 | || [regexp {(.)(.+)} $result match first tail]} { |
---|
| 524 | set result "[string toupper $first]$tail" |
---|
| 525 | } |
---|
| 526 | bell |
---|
| 527 | Rappture::Tooltip::cue $itk_component(editor) $result |
---|
[3186] | 528 | _log warning $result |
---|
[1850] | 529 | return 0 |
---|
| 530 | } |
---|
| 531 | } |
---|
| 532 | apply { |
---|
| 533 | if {[llength $args] != 1} { |
---|
| 534 | error "wrong # args: should be \"_editor apply val\"" |
---|
| 535 | } |
---|
[3186] | 536 | set newval [lindex $args 0] |
---|
| 537 | value $newval |
---|
| 538 | _log input $newval |
---|
[1850] | 539 | } |
---|
| 540 | menu { |
---|
| 541 | eval tk_popup $itk_component(emenu) $args |
---|
| 542 | } |
---|
| 543 | default { |
---|
| 544 | error "bad option \"$option\": should be popup, activate, validate, apply, and menu" |
---|
| 545 | } |
---|
[1] | 546 | } |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | # ---------------------------------------------------------------------- |
---|
| 550 | # USAGE: _presets post |
---|
| 551 | # USAGE: _presets unpost |
---|
| 552 | # USAGE: _presets select |
---|
| 553 | # |
---|
| 554 | # Used internally to handle the list of presets for this gauge. The |
---|
| 555 | # post/unpost options are invoked when the list is posted or unposted |
---|
| 556 | # to manage the relief of the controlling button. The select option |
---|
| 557 | # is invoked whenever there is a selection from the list, to assign |
---|
| 558 | # the value back to the gauge. |
---|
| 559 | # ---------------------------------------------------------------------- |
---|
| 560 | itcl::body Rappture::Gauge::_presets {option} { |
---|
| 561 | switch -- $option { |
---|
[1850] | 562 | post { |
---|
| 563 | set i [$itk_component(presetlist) index $_value] |
---|
| 564 | if {$i >= 0} { |
---|
| 565 | $itk_component(presetlist) select clear 0 end |
---|
| 566 | $itk_component(presetlist) select set $i |
---|
| 567 | } |
---|
| 568 | after 10 [list $itk_component(presets) configure -relief sunken] |
---|
| 569 | } |
---|
| 570 | unpost { |
---|
[3642] | 571 | $itk_component(presets) configure -relief raised |
---|
[1850] | 572 | } |
---|
| 573 | select { |
---|
| 574 | set val [$itk_component(presetlist) current] |
---|
| 575 | if {"" != $val} { |
---|
| 576 | value $val |
---|
[3186] | 577 | _log input $val |
---|
[1850] | 578 | } |
---|
| 579 | } |
---|
| 580 | default { |
---|
| 581 | error "bad option \"$option\": should be post, unpost, select" |
---|
| 582 | } |
---|
[1] | 583 | } |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | # ---------------------------------------------------------------------- |
---|
[22] | 587 | # USAGE: _layout |
---|
| 588 | # |
---|
| 589 | # Used internally to fix the layout of widgets whenever there is a |
---|
| 590 | # change in the options that affect layout. Puts the value in the |
---|
| 591 | # proper position according to the -valueposition option. Also, |
---|
| 592 | # adds or removes the icon if it needs to be shown. |
---|
| 593 | # ---------------------------------------------------------------------- |
---|
| 594 | itcl::body Rappture::Gauge::_layout {} { |
---|
| 595 | foreach w [pack slaves $itk_component(hull)] { |
---|
[1850] | 596 | pack forget $w |
---|
[22] | 597 | } |
---|
| 598 | |
---|
| 599 | array set side2anchor { |
---|
[1850] | 600 | left e |
---|
| 601 | right w |
---|
| 602 | top s |
---|
| 603 | bottom n |
---|
[22] | 604 | } |
---|
| 605 | set pos $itk_option(-valueposition) |
---|
| 606 | pack $itk_component(vframe) -side $pos \ |
---|
[1850] | 607 | -expand yes -fill both -ipadx 2 |
---|
[22] | 608 | $itk_component(value) configure -anchor $side2anchor($pos) |
---|
| 609 | |
---|
| 610 | if {"" != $itk_option(-image) || "" != $itk_option(-spectrum)} { |
---|
[3642] | 611 | pack $itk_component(icon) -side $pos -padx 2 |
---|
[22] | 612 | } |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | # ---------------------------------------------------------------------- |
---|
[3186] | 616 | # USAGE: _log event ?arg arg...? |
---|
| 617 | # |
---|
| 618 | # Used internally to send info to the logging mechanism. If the -log |
---|
| 619 | # argument is set, then this calls the Rappture::Logger mechanism to |
---|
| 620 | # log the rest of the arguments as an action. Otherwise, it does |
---|
| 621 | # nothing. |
---|
| 622 | # ---------------------------------------------------------------------- |
---|
| 623 | itcl::body Rappture::Gauge::_log {event args} { |
---|
| 624 | if {$itk_option(-log) ne ""} { |
---|
| 625 | eval Rappture::Logger::log $event [list $itk_option(-log)] $args |
---|
| 626 | } |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | # ---------------------------------------------------------------------- |
---|
[1] | 630 | # CONFIGURATION OPTION: -editable |
---|
| 631 | # ---------------------------------------------------------------------- |
---|
| 632 | itcl::configbody Rappture::Gauge::editable { |
---|
| 633 | if {![string is boolean -strict $itk_option(-editable)]} { |
---|
[1850] | 634 | error "bad value \"$itk_option(-editable)\": should be boolean" |
---|
[1] | 635 | } |
---|
| 636 | if {!$itk_option(-editable) && [winfo ismapped $itk_component(editor)]} { |
---|
[1850] | 637 | $itk_component(editor) deactivate -abort |
---|
[1] | 638 | } |
---|
| 639 | } |
---|
| 640 | |
---|
| 641 | # ---------------------------------------------------------------------- |
---|
[437] | 642 | # CONFIGURATION OPTION: -state |
---|
| 643 | # ---------------------------------------------------------------------- |
---|
| 644 | itcl::configbody Rappture::Gauge::state { |
---|
| 645 | set valid {normal disabled} |
---|
| 646 | if {[lsearch -exact $valid $itk_option(-state)] < 0} { |
---|
[1850] | 647 | error "bad value \"$itk_option(-state)\": should be [join $valid {, }]" |
---|
[437] | 648 | } |
---|
| 649 | $itk_component(value) configure -state $itk_option(-state) |
---|
| 650 | $itk_component(spinup) configure -state $itk_option(-state) |
---|
| 651 | $itk_component(spindn) configure -state $itk_option(-state) |
---|
| 652 | $itk_component(presets) configure -state $itk_option(-state) |
---|
| 653 | _redraw ;# fix gauge |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | # ---------------------------------------------------------------------- |
---|
[1] | 657 | # CONFIGURATION OPTION: -spectrum |
---|
| 658 | # ---------------------------------------------------------------------- |
---|
| 659 | itcl::configbody Rappture::Gauge::spectrum { |
---|
| 660 | if {$itk_option(-spectrum) != "" |
---|
[1850] | 661 | && ([catch {$itk_option(-spectrum) isa ::Rappture::Spectrum} valid] |
---|
| 662 | || !$valid)} { |
---|
| 663 | error "bad option \"$itk_option(-spectrum)\": should be Rappture::Spectrum object" |
---|
[1] | 664 | } |
---|
[24] | 665 | _resize |
---|
[22] | 666 | _layout |
---|
[1] | 667 | _redraw |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | # ---------------------------------------------------------------------- |
---|
| 671 | # CONFIGURATION OPTION: -image |
---|
| 672 | # ---------------------------------------------------------------------- |
---|
| 673 | itcl::configbody Rappture::Gauge::image { |
---|
| 674 | if {$itk_option(-image) != "" |
---|
[1850] | 675 | && [catch {image width $itk_option(-image)}]} { |
---|
| 676 | error "bad value \"$itk_option(-image)\": should be Tk image" |
---|
[1] | 677 | } |
---|
| 678 | _resize |
---|
[22] | 679 | _layout |
---|
[1] | 680 | $itk_component(icon) itemconfigure bimage -image $itk_option(-image) |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | # ---------------------------------------------------------------------- |
---|
| 684 | # CONFIGURATION OPTION: -units |
---|
| 685 | # ---------------------------------------------------------------------- |
---|
| 686 | itcl::configbody Rappture::Gauge::units { |
---|
| 687 | if {$itk_option(-units) != "" |
---|
[1850] | 688 | && [::Rappture::Units::System::for $itk_option(-units)] == ""} { |
---|
| 689 | error "unrecognized system of units \"$itk_option(-units)\"" |
---|
[1] | 690 | } |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | # ---------------------------------------------------------------------- |
---|
| 694 | # CONFIGURATION OPTION: -valueposition |
---|
| 695 | # ---------------------------------------------------------------------- |
---|
| 696 | itcl::configbody Rappture::Gauge::valueposition { |
---|
| 697 | set pos $itk_option(-valueposition) |
---|
[22] | 698 | set opts {left right top bottom} |
---|
| 699 | if {[lsearch -exact $opts $pos] < 0} { |
---|
[1850] | 700 | error "bad value \"$pos\": should be [join $opts {, }]" |
---|
[1] | 701 | } |
---|
[22] | 702 | _layout |
---|
[1] | 703 | } |
---|
| 704 | |
---|
| 705 | # ---------------------------------------------------------------------- |
---|
| 706 | # CONFIGURATION OPTION: -presets |
---|
| 707 | # ---------------------------------------------------------------------- |
---|
| 708 | itcl::configbody Rappture::Gauge::presets { |
---|
| 709 | if {"" == $itk_option(-presets)} { |
---|
[1850] | 710 | pack forget $itk_component(presets) |
---|
[1] | 711 | } else { |
---|
[1850] | 712 | if {$itk_option(-valueposition) == "left"} { |
---|
| 713 | set s "left" |
---|
| 714 | } else { |
---|
| 715 | set s "right" |
---|
| 716 | } |
---|
| 717 | set first [lindex [pack slaves $itk_component(vframe)] 0] |
---|
| 718 | pack $itk_component(presets) -before $first -side $s -fill y |
---|
[1] | 719 | |
---|
[1850] | 720 | $itk_component(presetlist) delete 0 end |
---|
| 721 | $itk_component(presetlist) insert end $itk_option(-presets) |
---|
[1] | 722 | } |
---|
| 723 | } |
---|
[22] | 724 | |
---|
| 725 | # ---------------------------------------------------------------------- |
---|
| 726 | # CONFIGURATION OPTION: -type |
---|
| 727 | # ---------------------------------------------------------------------- |
---|
| 728 | itcl::configbody Rappture::Gauge::type { |
---|
| 729 | switch -- $itk_option(-type) { |
---|
[1850] | 730 | integer { |
---|
| 731 | set first [lindex [pack slaves $itk_component(vframe)] 0] |
---|
| 732 | if {$first == $itk_component(presets)} { |
---|
| 733 | pack $itk_component(spinner) -after $first -side left -fill y |
---|
| 734 | } else { |
---|
| 735 | pack $itk_component(spinner) -before $first -side right -fill y |
---|
| 736 | } |
---|
| 737 | } |
---|
| 738 | real { |
---|
| 739 | pack forget $itk_component(spinner) |
---|
| 740 | } |
---|
| 741 | default { |
---|
| 742 | error "bad number type \"$itk_option(-type)\": should be integer or real" |
---|
| 743 | } |
---|
[22] | 744 | } |
---|
| 745 | } |
---|