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