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