1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: IntegerEntry - widget for entering integer values |
---|
3 | # |
---|
4 | # This widget represents an <integer> entry on a control panel. |
---|
5 | # It is used to enter integral values with no units. |
---|
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::IntegerEntry { |
---|
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 IntegerEntry { |
---|
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::IntegerEntry::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 | # Create the widget and configure it properly based on other |
---|
52 | # hints in the XML. |
---|
53 | # |
---|
54 | itk_component add spinner { |
---|
55 | Rappture::Gauge $itk_interior.spinner -type integer |
---|
56 | } |
---|
57 | pack $itk_component(spinner) -expand yes -fill both |
---|
58 | bind $itk_component(spinner) <<Value>> [itcl::code $this _newValue] |
---|
59 | |
---|
60 | # if there are min/max values, plug them in. |
---|
61 | set min [$_owner xml get $path.min] |
---|
62 | if {"" != $min} { |
---|
63 | $itk_component(spinner) configure -minvalue $min |
---|
64 | } |
---|
65 | |
---|
66 | set max [$_owner xml get $path.max] |
---|
67 | if {"" != $max} { |
---|
68 | $itk_component(spinner) configure -maxvalue $max |
---|
69 | } |
---|
70 | |
---|
71 | # if there is a color, use it for the min/max spectrum |
---|
72 | set color [$_owner xml get $path.about.color] |
---|
73 | if {$color != "" && $min != "" && $max != ""} { |
---|
74 | $itk_component(spinner) configure \ |
---|
75 | -spectrum [Rappture::Spectrum ::#auto [list \ |
---|
76 | $min white $max $color]] |
---|
77 | } |
---|
78 | |
---|
79 | # if the control has an icon, plug it in |
---|
80 | set str [$_owner xml get $path.about.icon] |
---|
81 | if {$str != ""} { |
---|
82 | $itk_component(spinner) configure -image \ |
---|
83 | [image create photo -data $str] |
---|
84 | } |
---|
85 | |
---|
86 | eval itk_initialize $args |
---|
87 | |
---|
88 | # |
---|
89 | # Assign the default value to this widget, if there is one. |
---|
90 | # |
---|
91 | set str [$_owner xml get $path.default] |
---|
92 | if {"" != $str != ""} { $itk_component(spinner) value $str } |
---|
93 | } |
---|
94 | |
---|
95 | # ---------------------------------------------------------------------- |
---|
96 | # USAGE: value ?-check? ?<newval>? |
---|
97 | # |
---|
98 | # Clients use this to query/set the value for this widget. With |
---|
99 | # no args, it returns the current value for the widget. If the |
---|
100 | # <newval> is specified, it sets the value of the widget and |
---|
101 | # sends a <<Value>> event. If the -check flag is included, the |
---|
102 | # new value is not actually applied, but just checked for correctness. |
---|
103 | # ---------------------------------------------------------------------- |
---|
104 | itcl::body Rappture::IntegerEntry::value {args} { |
---|
105 | set onlycheck 0 |
---|
106 | set i [lsearch -exact $args -check] |
---|
107 | if {$i >= 0} { |
---|
108 | set onlycheck 1 |
---|
109 | set args [lreplace $args $i $i] |
---|
110 | } |
---|
111 | |
---|
112 | if {[llength $args] == 1} { |
---|
113 | if {$onlycheck} { |
---|
114 | # someday we may add validation... |
---|
115 | return |
---|
116 | } |
---|
117 | set newval [lindex $args 0] |
---|
118 | $itk_component(spinner) value $newval |
---|
119 | return $newval |
---|
120 | |
---|
121 | } elseif {[llength $args] != 0} { |
---|
122 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
123 | } |
---|
124 | |
---|
125 | # |
---|
126 | # Query the value and return. |
---|
127 | # |
---|
128 | return [$itk_component(spinner) value] |
---|
129 | } |
---|
130 | |
---|
131 | # ---------------------------------------------------------------------- |
---|
132 | # USAGE: label |
---|
133 | # |
---|
134 | # Clients use this to query the label associated with this widget. |
---|
135 | # Reaches into the XML and pulls out the appropriate label string. |
---|
136 | # ---------------------------------------------------------------------- |
---|
137 | itcl::body Rappture::IntegerEntry::label {} { |
---|
138 | set label [$_owner xml get $_path.about.label] |
---|
139 | if {"" == $label} { |
---|
140 | set label "Integer" |
---|
141 | } |
---|
142 | return $label |
---|
143 | } |
---|
144 | |
---|
145 | # ---------------------------------------------------------------------- |
---|
146 | # USAGE: tooltip |
---|
147 | # |
---|
148 | # Clients use this to query the tooltip associated with this widget. |
---|
149 | # Reaches into the XML and pulls out the appropriate description |
---|
150 | # string. Returns the string that should be used with the |
---|
151 | # Rappture::Tooltip facility. |
---|
152 | # ---------------------------------------------------------------------- |
---|
153 | itcl::body Rappture::IntegerEntry::tooltip {} { |
---|
154 | set str [$_owner xml get $_path.about.description] |
---|
155 | |
---|
156 | set min [$_owner xml get $_path.min] |
---|
157 | set max [$_owner xml get $_path.max] |
---|
158 | |
---|
159 | if {$min != "" || $max != ""} { |
---|
160 | append str "\n\nEnter an integer" |
---|
161 | |
---|
162 | if {$min != "" && $max != ""} { |
---|
163 | append str " between $min and $max" |
---|
164 | } elseif {$min != ""} { |
---|
165 | append str " greater than $min" |
---|
166 | } elseif {$max != ""} { |
---|
167 | append str " less than $max" |
---|
168 | } |
---|
169 | } |
---|
170 | return [string trim $str] |
---|
171 | } |
---|
172 | |
---|
173 | # ---------------------------------------------------------------------- |
---|
174 | # USAGE: _newValue |
---|
175 | # |
---|
176 | # Invoked automatically whenever the value in the gauge changes. |
---|
177 | # Sends a <<Value>> event to notify clients of the change. |
---|
178 | # ---------------------------------------------------------------------- |
---|
179 | itcl::body Rappture::IntegerEntry::_newValue {} { |
---|
180 | event generate $itk_component(hull) <<Value>> |
---|
181 | } |
---|
182 | |
---|
183 | # ---------------------------------------------------------------------- |
---|
184 | # CONFIGURATION OPTION: -state |
---|
185 | # ---------------------------------------------------------------------- |
---|
186 | itcl::configbody Rappture::IntegerEntry::state { |
---|
187 | set valid {normal disabled} |
---|
188 | if {[lsearch -exact $valid $itk_option(-state)] < 0} { |
---|
189 | error "bad value \"$itk_option(-state)\": should be [join $valid {, }]" |
---|
190 | } |
---|
191 | $itk_component(spinner) configure -state $itk_option(-state) |
---|
192 | } |
---|