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