1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: textentry - general-purpose text entry widget |
---|
3 | # |
---|
4 | # This widget is a cross between the Tk entry and text widgets. For |
---|
5 | # one-line messages, it acts like an entry widget. For larger |
---|
6 | # messages, it morphs into a text widget. |
---|
7 | # ====================================================================== |
---|
8 | # AUTHOR: Michael McLennan, Purdue University |
---|
9 | # Copyright (c) 2004-2005 |
---|
10 | # Purdue Research Foundation, West Lafayette, IN |
---|
11 | # ====================================================================== |
---|
12 | package require Itk |
---|
13 | |
---|
14 | option add *TextEntry.size auto widgetDefault |
---|
15 | option add *TextEntry.width 0 widgetDefault |
---|
16 | option add *TextEntry.height 0 widgetDefault |
---|
17 | option add *TextEntry.editable yes widgetDefault |
---|
18 | option add *TextEntry.textBackground white widgetDefault |
---|
19 | |
---|
20 | option add *TextEntry.hintForeground gray50 widgetDefault |
---|
21 | option add *TextEntry.hintFont \ |
---|
22 | -*-helvetica-medium-r-normal-*-*-100-* widgetDefault |
---|
23 | |
---|
24 | itcl::class Rappture::TextEntry { |
---|
25 | inherit itk::Widget |
---|
26 | |
---|
27 | itk_option define -editable editable Editable "" |
---|
28 | itk_option define -width width Width 0 |
---|
29 | itk_option define -height height Height 0 |
---|
30 | |
---|
31 | constructor {xmlobj path args} { # defined below } |
---|
32 | |
---|
33 | public method value {args} |
---|
34 | |
---|
35 | public method label {} |
---|
36 | public method tooltip {} |
---|
37 | public method size {} { return $_size } |
---|
38 | |
---|
39 | protected method _layout {} |
---|
40 | |
---|
41 | private variable _dispatcher "" ;# dispatcher for !events |
---|
42 | private variable _xmlobj "" ;# XML containing description |
---|
43 | private variable _path "" ;# path in XML to this number |
---|
44 | |
---|
45 | private variable _mode "" ;# entry or text mode |
---|
46 | private variable _size "" ;# size hint from XML |
---|
47 | } |
---|
48 | |
---|
49 | itk::usual TextEntry { |
---|
50 | } |
---|
51 | |
---|
52 | # ---------------------------------------------------------------------- |
---|
53 | # CONSTRUCTOR |
---|
54 | # ---------------------------------------------------------------------- |
---|
55 | itcl::body Rappture::TextEntry::constructor {xmlobj path args} { |
---|
56 | if {![Rappture::library isvalid $xmlobj]} { |
---|
57 | error "bad value \"$xmlobj\": should be Rappture::library" |
---|
58 | } |
---|
59 | set _xmlobj $xmlobj |
---|
60 | set _path $path |
---|
61 | |
---|
62 | Rappture::dispatcher _dispatcher |
---|
63 | $_dispatcher register !layout |
---|
64 | $_dispatcher dispatch $this !layout "[itcl::code $this _layout]; list" |
---|
65 | |
---|
66 | set _size [$xmlobj get $path.size] |
---|
67 | |
---|
68 | set hints [$xmlobj get $path.about.hints] |
---|
69 | if {[string length $hints] > 0} { |
---|
70 | itk_component add hints { |
---|
71 | label $itk_interior.hints -anchor w -text $hints |
---|
72 | } { |
---|
73 | usual |
---|
74 | rename -foreground -hintforeground hintForeground Foreground |
---|
75 | rename -font -hintfont hintFont Font |
---|
76 | } |
---|
77 | pack $itk_component(hints) -side bottom -fill x |
---|
78 | } |
---|
79 | |
---|
80 | eval itk_initialize $args |
---|
81 | |
---|
82 | set str [$xmlobj get $path.default] |
---|
83 | if {"" != $str} { value $str } |
---|
84 | } |
---|
85 | |
---|
86 | # ---------------------------------------------------------------------- |
---|
87 | # USAGE: value ?-check? ?<newval>? |
---|
88 | # |
---|
89 | # Clients use this to query/set the value for this widget. With |
---|
90 | # no args, it returns the current value for the widget. If the |
---|
91 | # <newval> is specified, it sets the value of the widget and |
---|
92 | # sends a <<Value>> event. If the -check flag is included, the |
---|
93 | # new value is not actually applied, but just checked for correctness. |
---|
94 | # ---------------------------------------------------------------------- |
---|
95 | itcl::body Rappture::TextEntry::value {args} { |
---|
96 | set onlycheck 0 |
---|
97 | set i [lsearch -exact $args -check] |
---|
98 | if {$i >= 0} { |
---|
99 | set onlycheck 1 |
---|
100 | set args [lreplace $args $i $i] |
---|
101 | } |
---|
102 | |
---|
103 | if {[llength $args] == 1} { |
---|
104 | if {$onlycheck} { |
---|
105 | # someday we may add validation... |
---|
106 | return |
---|
107 | } |
---|
108 | set newval [lindex $args 0] |
---|
109 | if {$_mode == "entry"} { |
---|
110 | $itk_component(entry) configure -state normal |
---|
111 | $itk_component(entry) delete 0 end |
---|
112 | $itk_component(entry) insert 0 $newval |
---|
113 | if {!$itk_option(-editable)} { |
---|
114 | $itk_component(entry) configure -state disabled |
---|
115 | } |
---|
116 | } elseif {$_mode == "text"} { |
---|
117 | $itk_component(text) configure -state normal |
---|
118 | $itk_component(text) delete 1.0 end |
---|
119 | $itk_component(text) insert end $newval |
---|
120 | if {!$itk_option(-editable)} { |
---|
121 | $itk_component(text) configure -state disabled |
---|
122 | } |
---|
123 | } |
---|
124 | $_dispatcher event -idle !layout |
---|
125 | event generate $itk_component(hull) <<Value>> |
---|
126 | return $newval |
---|
127 | |
---|
128 | } elseif {[llength $args] != 0} { |
---|
129 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
130 | } |
---|
131 | |
---|
132 | # |
---|
133 | # Query the value and return. |
---|
134 | # |
---|
135 | if {$_mode == "entry"} { |
---|
136 | return [$itk_component(entry) get] |
---|
137 | } elseif {$_mode == "text"} { |
---|
138 | return [$itk_component(text) get 1.0 end-1char] |
---|
139 | } |
---|
140 | return "" |
---|
141 | } |
---|
142 | |
---|
143 | # ---------------------------------------------------------------------- |
---|
144 | # USAGE: label |
---|
145 | # |
---|
146 | # Clients use this to query the label associated with this widget. |
---|
147 | # Reaches into the XML and pulls out the appropriate label string. |
---|
148 | # ---------------------------------------------------------------------- |
---|
149 | itcl::body Rappture::TextEntry::label {} { |
---|
150 | set label [$_xmlobj get $_path.about.label] |
---|
151 | if {"" == $label} { |
---|
152 | set label "String" |
---|
153 | } |
---|
154 | return $label |
---|
155 | } |
---|
156 | |
---|
157 | # ---------------------------------------------------------------------- |
---|
158 | # USAGE: tooltip |
---|
159 | # |
---|
160 | # Clients use this to query the tooltip associated with this widget. |
---|
161 | # Reaches into the XML and pulls out the appropriate description |
---|
162 | # string. Returns the string that should be used with the |
---|
163 | # Rappture::Tooltip facility. |
---|
164 | # ---------------------------------------------------------------------- |
---|
165 | itcl::body Rappture::TextEntry::tooltip {} { |
---|
166 | set str [$_xmlobj get $_path.about.description] |
---|
167 | return [string trim $str] |
---|
168 | } |
---|
169 | |
---|
170 | # ---------------------------------------------------------------------- |
---|
171 | # USAGE: _layout |
---|
172 | # |
---|
173 | # Used internally to change the layout of this widget depending |
---|
174 | # on the .size hint and its contents. Switches between an entry |
---|
175 | # and a text widget. |
---|
176 | # ---------------------------------------------------------------------- |
---|
177 | itcl::body Rappture::TextEntry::_layout {} { |
---|
178 | set size $_size |
---|
179 | if {$size == "" || $size == "auto"} { |
---|
180 | # |
---|
181 | # If the size is "auto", then look at the current value |
---|
182 | # and count its lines/characters. |
---|
183 | # |
---|
184 | set val "" |
---|
185 | if {$_mode == "entry"} { |
---|
186 | set val [$itk_component(entry) get] |
---|
187 | } elseif {$_mode == "text"} { |
---|
188 | set val [$itk_component(text) get 1.0 end-1char] |
---|
189 | } |
---|
190 | |
---|
191 | set chars 0 |
---|
192 | set lines 0 |
---|
193 | foreach line [split $val \n] { |
---|
194 | incr lines |
---|
195 | if {[string length $line] > $chars} { |
---|
196 | set chars [string length $line] |
---|
197 | } |
---|
198 | } |
---|
199 | incr chars |
---|
200 | |
---|
201 | if {$lines > 1} { |
---|
202 | set size "${chars}x${lines}" |
---|
203 | } else { |
---|
204 | set size $chars |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | if {[regexp {^[0-9]+$} $size]} { |
---|
209 | # |
---|
210 | # If the size is WW, then flip to entry mode, with |
---|
211 | # a requested size of WW characters. |
---|
212 | # |
---|
213 | if {$_mode != "entry"} { |
---|
214 | set val "" |
---|
215 | if {$_mode == "text"} { |
---|
216 | set val [$itk_component(text) get 1.0 end-1char] |
---|
217 | destroy $itk_component(text) |
---|
218 | destroy $itk_component(scrollbars) |
---|
219 | } |
---|
220 | |
---|
221 | itk_component add entry { |
---|
222 | entry $itk_interior.entry |
---|
223 | } { |
---|
224 | usual |
---|
225 | rename -background -textbackground textBackground Background |
---|
226 | rename -foreground -textforeground textForeground Foreground |
---|
227 | } |
---|
228 | pack $itk_component(entry) -expand yes -fill both |
---|
229 | $itk_component(entry) configure \ |
---|
230 | -background $itk_option(-textbackground) \ |
---|
231 | -foreground $itk_option(-textforeground) |
---|
232 | |
---|
233 | $itk_component(entry) insert end $val |
---|
234 | if {!$itk_option(-editable)} { |
---|
235 | $itk_component(entry) configure -state disabled |
---|
236 | } |
---|
237 | set _mode "entry" |
---|
238 | } |
---|
239 | $itk_component(entry) configure -width $size |
---|
240 | |
---|
241 | } elseif {[regexp {^([0-9]+)x([0-9]+)$} $size match w h]} { |
---|
242 | # |
---|
243 | # If the size is WWxHH, then flip to text mode, with |
---|
244 | # a requested size of HH lines by WW characters. |
---|
245 | # |
---|
246 | if {$_mode != "text"} { |
---|
247 | set val "" |
---|
248 | if {$_mode == "entry"} { |
---|
249 | set val [$itk_component(entry) get] |
---|
250 | destroy $itk_component(entry) |
---|
251 | } |
---|
252 | |
---|
253 | itk_component add scrollbars { |
---|
254 | Rappture::Scroller $itk_interior.scrl \ |
---|
255 | -xscrollmode auto -yscrollmode auto |
---|
256 | } |
---|
257 | pack $itk_component(scrollbars) -expand yes -fill both |
---|
258 | |
---|
259 | itk_component add text { |
---|
260 | text $itk_component(scrollbars).text \ |
---|
261 | -width 1 -height 1 -wrap word |
---|
262 | } { |
---|
263 | usual |
---|
264 | rename -background -textbackground textBackground Background |
---|
265 | rename -foreground -textforeground textForeground Foreground |
---|
266 | } |
---|
267 | $itk_component(text) configure \ |
---|
268 | -background $itk_option(-textbackground) \ |
---|
269 | -foreground $itk_option(-textforeground) |
---|
270 | $itk_component(scrollbars) contents $itk_component(text) |
---|
271 | |
---|
272 | $itk_component(text) insert end $val |
---|
273 | if {!$itk_option(-editable)} { |
---|
274 | $itk_component(text) configure -state disabled |
---|
275 | } |
---|
276 | set _mode "text" |
---|
277 | } |
---|
278 | $itk_component(text) configure -width $w -height $h |
---|
279 | } |
---|
280 | |
---|
281 | # |
---|
282 | # Fix the overall widget size according to -width / -height |
---|
283 | # |
---|
284 | if {$itk_option(-width) == 0 && $itk_option(-height) == 0} { |
---|
285 | pack propagate $itk_component(hull) yes |
---|
286 | } else { |
---|
287 | pack propagate $itk_component(hull) no |
---|
288 | component hull configure \ |
---|
289 | -width $itk_option(-width) -height $itk_option(-width) |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | # ---------------------------------------------------------------------- |
---|
294 | # CONFIGURATION OPTION: -editable |
---|
295 | # ---------------------------------------------------------------------- |
---|
296 | itcl::configbody Rappture::TextEntry::editable { |
---|
297 | if {![string is boolean -strict $itk_option(-editable)]} { |
---|
298 | error "bad value \"$itk_option(-editable)\": should be boolean" |
---|
299 | } |
---|
300 | |
---|
301 | if {$itk_option(-editable)} { |
---|
302 | set state normal |
---|
303 | } else { |
---|
304 | set state disabled |
---|
305 | } |
---|
306 | if {$_mode == "entry"} { |
---|
307 | $itk_component(editor) configure -state $state |
---|
308 | } elseif {$_mode == "text"} { |
---|
309 | $itk_component(text) configure -state $state |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | # ---------------------------------------------------------------------- |
---|
314 | # CONFIGURATION OPTION: -width |
---|
315 | # ---------------------------------------------------------------------- |
---|
316 | itcl::configbody Rappture::TextEntry::width { |
---|
317 | # check size to see if it has the proper form |
---|
318 | winfo pixels $itk_component(hull) $itk_option(-width) |
---|
319 | $_dispatcher event -idle !layout |
---|
320 | } |
---|
321 | |
---|
322 | # ---------------------------------------------------------------------- |
---|
323 | # CONFIGURATION OPTION: -height |
---|
324 | # ---------------------------------------------------------------------- |
---|
325 | itcl::configbody Rappture::TextEntry::height { |
---|
326 | # check size to see if it has the proper form |
---|
327 | winfo pixels $itk_component(hull) $itk_option(-height) |
---|
328 | $_dispatcher event -idle !layout |
---|
329 | } |
---|