1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: NumberEntry - widget for entering numeric values |
---|
3 | # |
---|
4 | # This widget represents a <number> entry on a control panel. |
---|
5 | # It is used to enter numeric values. |
---|
6 | # ====================================================================== |
---|
7 | # AUTHOR: Michael McLennan, Purdue University |
---|
8 | # Copyright (c) 2004-2005 Purdue Research Foundation |
---|
9 | # |
---|
10 | # See the file "license.terms" for information on usage and |
---|
11 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
12 | # ====================================================================== |
---|
13 | package require Itk |
---|
14 | |
---|
15 | itcl::class Rappture::NumberEntry { |
---|
16 | inherit itk::Widget |
---|
17 | |
---|
18 | itk_option define -state state State "normal" |
---|
19 | |
---|
20 | constructor {owner path args} { # defined below } |
---|
21 | |
---|
22 | public method value {args} |
---|
23 | |
---|
24 | public method label {} |
---|
25 | public method tooltip {} |
---|
26 | |
---|
27 | protected method _newValue {} |
---|
28 | |
---|
29 | private variable _owner "" ;# thing managing this control |
---|
30 | private variable _path "" ;# path in XML to this number |
---|
31 | } |
---|
32 | |
---|
33 | itk::usual NumberEntry { |
---|
34 | keep -cursor -font |
---|
35 | keep -foreground -background |
---|
36 | keep -textbackground |
---|
37 | keep -selectbackground -selectforeground -selectborderwidth |
---|
38 | } |
---|
39 | |
---|
40 | # ---------------------------------------------------------------------- |
---|
41 | # CONSTRUCTOR |
---|
42 | # ---------------------------------------------------------------------- |
---|
43 | itcl::body Rappture::NumberEntry::constructor {owner path args} { |
---|
44 | if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} { |
---|
45 | error "bad object \"$owner\": should be Rappture::ControlOwner" |
---|
46 | } |
---|
47 | set _owner $owner |
---|
48 | set _path $path |
---|
49 | |
---|
50 | # |
---|
51 | # Figure out what sort of control to create |
---|
52 | # |
---|
53 | set presets "" |
---|
54 | foreach pre [$_owner xml children -type preset $path] { |
---|
55 | lappend presets \ |
---|
56 | [$_owner xml get $path.$pre.value] \ |
---|
57 | [$_owner xml get $path.$pre.label] |
---|
58 | } |
---|
59 | |
---|
60 | set class Rappture::Gauge |
---|
61 | set units [$_owner xml get $path.units] |
---|
62 | if {$units != ""} { |
---|
63 | set desc [Rappture::Units::description $units] |
---|
64 | if {[string match temperature* $desc]} { |
---|
65 | set class Rappture::TemperatureGauge |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | # |
---|
70 | # Create the widget and configure it properly based on other |
---|
71 | # hints in the XML. |
---|
72 | # |
---|
73 | itk_component add gauge { |
---|
74 | $class $itk_interior.gauge -units $units -presets $presets |
---|
75 | } |
---|
76 | pack $itk_component(gauge) -expand yes -fill both |
---|
77 | bind $itk_component(gauge) <<Value>> [itcl::code $this _newValue] |
---|
78 | |
---|
79 | set min [$_owner xml get $path.min] |
---|
80 | if {"" != $min} { $itk_component(gauge) configure -minvalue $min } |
---|
81 | |
---|
82 | set max [$_owner xml get $path.max] |
---|
83 | if {"" != $max} { $itk_component(gauge) configure -maxvalue $max } |
---|
84 | |
---|
85 | if {$class == "Rappture::Gauge" && "" != $min && "" != $max} { |
---|
86 | set color [$_owner xml get $path.color] |
---|
87 | if {$color == ""} { |
---|
88 | set color blue |
---|
89 | } |
---|
90 | if {$units != ""} { |
---|
91 | set min [Rappture::Units::convert $min -to $units -units off] |
---|
92 | set max [Rappture::Units::convert $max -to $units -units off] |
---|
93 | } |
---|
94 | $itk_component(gauge) configure \ |
---|
95 | -spectrum [Rappture::Spectrum ::#auto [list \ |
---|
96 | $min white $max $color] -units $units] |
---|
97 | } |
---|
98 | |
---|
99 | # if the control has an icon, plug it in |
---|
100 | set str [$_owner xml get $path.about.icon] |
---|
101 | if {$str != ""} { |
---|
102 | $itk_component(gauge) configure -image [image create photo -data $str] |
---|
103 | } |
---|
104 | |
---|
105 | eval itk_initialize $args |
---|
106 | |
---|
107 | # |
---|
108 | # Assign the default value to this widget, if there is one. |
---|
109 | # |
---|
110 | set str [$_owner xml get $path.default] |
---|
111 | if {"" != $str != ""} { $itk_component(gauge) value $str } |
---|
112 | } |
---|
113 | |
---|
114 | # ---------------------------------------------------------------------- |
---|
115 | # USAGE: value ?-check? ?<newval>? |
---|
116 | # |
---|
117 | # Clients use this to query/set the value for this widget. With |
---|
118 | # no args, it returns the current value for the widget. If the |
---|
119 | # <newval> is specified, it sets the value of the widget and |
---|
120 | # sends a <<Value>> event. If the -check flag is included, the |
---|
121 | # new value is not actually applied, but just checked for correctness. |
---|
122 | # ---------------------------------------------------------------------- |
---|
123 | itcl::body Rappture::NumberEntry::value {args} { |
---|
124 | set onlycheck 0 |
---|
125 | set i [lsearch -exact $args -check] |
---|
126 | if {$i >= 0} { |
---|
127 | set onlycheck 1 |
---|
128 | set args [lreplace $args $i $i] |
---|
129 | } |
---|
130 | |
---|
131 | if {[llength $args] == 1} { |
---|
132 | if {$onlycheck} { |
---|
133 | # someday we may add validation... |
---|
134 | return |
---|
135 | } |
---|
136 | set newval [lindex $args 0] |
---|
137 | $itk_component(gauge) value $newval |
---|
138 | return $newval |
---|
139 | |
---|
140 | } elseif {[llength $args] != 0} { |
---|
141 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
142 | } |
---|
143 | |
---|
144 | # |
---|
145 | # Query the value and return. |
---|
146 | # |
---|
147 | return [$itk_component(gauge) value] |
---|
148 | } |
---|
149 | |
---|
150 | # ---------------------------------------------------------------------- |
---|
151 | # USAGE: label |
---|
152 | # |
---|
153 | # Clients use this to query the label associated with this widget. |
---|
154 | # Reaches into the XML and pulls out the appropriate label string. |
---|
155 | # ---------------------------------------------------------------------- |
---|
156 | itcl::body Rappture::NumberEntry::label {} { |
---|
157 | set label [$_owner xml get $_path.about.label] |
---|
158 | if {"" == $label} { |
---|
159 | set label "Number" |
---|
160 | } |
---|
161 | return $label |
---|
162 | } |
---|
163 | |
---|
164 | # ---------------------------------------------------------------------- |
---|
165 | # USAGE: tooltip |
---|
166 | # |
---|
167 | # Clients use this to query the tooltip associated with this widget. |
---|
168 | # Reaches into the XML and pulls out the appropriate description |
---|
169 | # string. Returns the string that should be used with the |
---|
170 | # Rappture::Tooltip facility. |
---|
171 | # ---------------------------------------------------------------------- |
---|
172 | itcl::body Rappture::NumberEntry::tooltip {} { |
---|
173 | set str [$_owner xml get $_path.about.description] |
---|
174 | |
---|
175 | set units [$_owner xml get $_path.units] |
---|
176 | set min [$_owner xml get $_path.min] |
---|
177 | set max [$_owner xml get $_path.max] |
---|
178 | |
---|
179 | if {$units != "" || $min != "" || $max != ""} { |
---|
180 | append str "\n\nEnter a number" |
---|
181 | |
---|
182 | if {$min != "" && $max != ""} { |
---|
183 | append str " between $min and $max" |
---|
184 | } elseif {$min != ""} { |
---|
185 | append str " greater than $min" |
---|
186 | } elseif {$max != ""} { |
---|
187 | append str " less than $max" |
---|
188 | } |
---|
189 | |
---|
190 | if {$units != ""} { |
---|
191 | set desc [Rappture::Units::description $units] |
---|
192 | append str " with units of $desc" |
---|
193 | } |
---|
194 | } |
---|
195 | return [string trim $str] |
---|
196 | } |
---|
197 | |
---|
198 | # ---------------------------------------------------------------------- |
---|
199 | # USAGE: _newValue |
---|
200 | # |
---|
201 | # Invoked automatically whenever the value in the gauge changes. |
---|
202 | # Sends a <<Value>> event to notify clients of the change. |
---|
203 | # ---------------------------------------------------------------------- |
---|
204 | itcl::body Rappture::NumberEntry::_newValue {} { |
---|
205 | event generate $itk_component(hull) <<Value>> |
---|
206 | } |
---|
207 | |
---|
208 | # ---------------------------------------------------------------------- |
---|
209 | # CONFIGURATION OPTION: -state |
---|
210 | # ---------------------------------------------------------------------- |
---|
211 | itcl::configbody Rappture::NumberEntry::state { |
---|
212 | set valid {normal disabled} |
---|
213 | if {[lsearch -exact $valid $itk_option(-state)] < 0} { |
---|
214 | error "bad value \"$itk_option(-state)\": should be [join $valid {, }]" |
---|
215 | } |
---|
216 | $itk_component(gauge) configure -state $itk_option(-state) |
---|
217 | } |
---|