1 | # -*- mode: tcl; indent-tabs-mode: nil -*- |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | # COMPONENT: controls - a container for various Rappture controls |
---|
4 | # |
---|
5 | # This widget is a smart frame acting as a container for controls. |
---|
6 | # Controls are added to this panel, and the panel itself decides |
---|
7 | # how to arrange them given available space. |
---|
8 | # ====================================================================== |
---|
9 | # AUTHOR: Michael McLennan, Purdue University |
---|
10 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
11 | # |
---|
12 | # See the file "license.terms" for information on usage and |
---|
13 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | # ====================================================================== |
---|
15 | package require Itk |
---|
16 | package require BLT |
---|
17 | |
---|
18 | option add *Controls.padding 4 widgetDefault |
---|
19 | option add *Controls.labelFont \ |
---|
20 | -*-helvetica-medium-r-normal-*-12-* widgetDefault |
---|
21 | |
---|
22 | itcl::class Rappture::Controls { |
---|
23 | inherit itk::Widget |
---|
24 | |
---|
25 | itk_option define -padding padding Padding 0 |
---|
26 | |
---|
27 | constructor {owner args} { # defined below } |
---|
28 | destructor { # defined below } |
---|
29 | |
---|
30 | public method insert {pos path} |
---|
31 | public method delete {first {last ""}} |
---|
32 | public method index {name} |
---|
33 | public method control {args} |
---|
34 | public method refresh {} |
---|
35 | |
---|
36 | protected method _layout {} |
---|
37 | protected method _monitor {name state} |
---|
38 | protected method _controlChanged {name} |
---|
39 | protected method _controlValue {path {units ""}} |
---|
40 | protected method _formatLabel {str} |
---|
41 | protected method _changeTabs {{why -program}} |
---|
42 | protected method _resize {} |
---|
43 | |
---|
44 | private variable _owner "" ;# controls belong to this owner |
---|
45 | private variable _tabs "" ;# optional tabset for groups |
---|
46 | private variable _frame "" ;# pack controls into this frame |
---|
47 | private variable _counter 0 ;# counter for control names |
---|
48 | private variable _dispatcher "" ;# dispatcher for !events |
---|
49 | private variable _controls "" ;# list of known controls |
---|
50 | private variable _showing "" ;# list of enabled (showing) controls |
---|
51 | private variable _name2info ;# maps control name => info |
---|
52 | private variable _scheme "" ;# layout scheme (tabs/hlabels) |
---|
53 | } |
---|
54 | |
---|
55 | itk::usual Controls { |
---|
56 | } |
---|
57 | |
---|
58 | # ---------------------------------------------------------------------- |
---|
59 | # CONSTRUCTOR |
---|
60 | # ---------------------------------------------------------------------- |
---|
61 | itcl::body Rappture::Controls::constructor {owner args} { |
---|
62 | Rappture::dispatcher _dispatcher |
---|
63 | $_dispatcher register !layout |
---|
64 | $_dispatcher dispatch $this !layout "[itcl::code $this _layout]; list" |
---|
65 | $_dispatcher register !resize |
---|
66 | $_dispatcher dispatch $this !resize "[itcl::code $this _resize]; list" |
---|
67 | |
---|
68 | set _owner $owner |
---|
69 | |
---|
70 | Rappture::Scroller $itk_interior.sc -xscrollmode none -yscrollmode auto |
---|
71 | pack $itk_interior.sc -expand yes -fill both |
---|
72 | set f [$itk_interior.sc contents frame] |
---|
73 | |
---|
74 | set _tabs [blt::tabset $f.tabs -borderwidth 0 -relief flat \ |
---|
75 | -side top -tearoff 0 -highlightthickness 0 \ |
---|
76 | -selectbackground $itk_option(-background) \ |
---|
77 | -selectcommand [itcl::code $this _changeTabs -user]] |
---|
78 | |
---|
79 | set _frame [frame $f.inner] |
---|
80 | pack $_frame -expand yes -fill both |
---|
81 | |
---|
82 | # |
---|
83 | # Put this frame in whenever the control frame is empty. |
---|
84 | # It forces the size to contract back now when controls are deleted. |
---|
85 | # |
---|
86 | frame $_frame.empty -width 1 -height 1 |
---|
87 | |
---|
88 | # |
---|
89 | # Set up a binding that all inserted widgets will use so that |
---|
90 | # we can monitor their size changes. |
---|
91 | # |
---|
92 | bind Controls-$this <Configure> \ |
---|
93 | [list $_dispatcher event -idle !resize] |
---|
94 | |
---|
95 | eval itk_initialize $args |
---|
96 | } |
---|
97 | |
---|
98 | # ---------------------------------------------------------------------- |
---|
99 | # DESTRUCTOR |
---|
100 | # ---------------------------------------------------------------------- |
---|
101 | itcl::body Rappture::Controls::destructor {} { |
---|
102 | delete 0 end |
---|
103 | } |
---|
104 | |
---|
105 | # ---------------------------------------------------------------------- |
---|
106 | # USAGE: insert <pos> <path> |
---|
107 | # |
---|
108 | # Clients use this to insert a control into this panel. The control |
---|
109 | # is inserted into the list at position <pos>, which can be an integer |
---|
110 | # starting from 0 or the keyword "end". Information about the control |
---|
111 | # is taken from the specified <path>. |
---|
112 | # |
---|
113 | # Returns a name that can be used to identify the control in other |
---|
114 | # methods. |
---|
115 | # ---------------------------------------------------------------------- |
---|
116 | itcl::body Rappture::Controls::insert {pos path} { |
---|
117 | if {"end" == $pos} { |
---|
118 | set pos [llength $_controls] |
---|
119 | } elseif {![string is integer $pos]} { |
---|
120 | error "bad index \"$pos\": should be integer or \"end\"" |
---|
121 | } |
---|
122 | |
---|
123 | incr _counter |
---|
124 | set name "control$_counter" |
---|
125 | set path [$_owner xml element -as path $path] |
---|
126 | |
---|
127 | set _name2info($name-path) $path |
---|
128 | set _name2info($name-label) "" |
---|
129 | set _name2info($name-type) "" |
---|
130 | set _name2info($name-value) [set w $_frame.v$name] |
---|
131 | set _name2info($name-enable) "yes" |
---|
132 | set _name2info($name-disablestyle) "greyout" |
---|
133 | |
---|
134 | set type [$_owner xml element -as type $path] |
---|
135 | set _name2info($name-type) $type |
---|
136 | switch -- $type { |
---|
137 | choice { |
---|
138 | Rappture::ChoiceEntry $w $_owner $path |
---|
139 | bind $w <<Value>> [itcl::code $this _controlChanged $name] |
---|
140 | } |
---|
141 | group { |
---|
142 | Rappture::GroupEntry $w $_owner $path |
---|
143 | } |
---|
144 | loader { |
---|
145 | Rappture::Loader $w $_owner $path -tool [$_owner tool] |
---|
146 | bind $w <<Value>> [itcl::code $this _controlChanged $name] |
---|
147 | } |
---|
148 | number { |
---|
149 | Rappture::NumberEntry $w $_owner $path |
---|
150 | bind $w <<Value>> [itcl::code $this _controlChanged $name] |
---|
151 | } |
---|
152 | integer { |
---|
153 | Rappture::IntegerEntry $w $_owner $path |
---|
154 | bind $w <<Value>> [itcl::code $this _controlChanged $name] |
---|
155 | } |
---|
156 | boolean { |
---|
157 | Rappture::BooleanEntry $w $_owner $path |
---|
158 | bind $w <<Value>> [itcl::code $this _controlChanged $name] |
---|
159 | } |
---|
160 | string { |
---|
161 | Rappture::TextEntry $w $_owner $path |
---|
162 | bind $w <<Value>> [itcl::code $this _controlChanged $name] |
---|
163 | } |
---|
164 | drawing { |
---|
165 | Rappture::DrawingEntry $w $_owner $path |
---|
166 | } |
---|
167 | image { |
---|
168 | Rappture::ImageEntry $w $_owner $path |
---|
169 | } |
---|
170 | control { |
---|
171 | set label [$_owner xml get $path.label] |
---|
172 | if {"" == $label} { set label "Simulate" } |
---|
173 | set service [$_owner xml get $path.service] |
---|
174 | button $w -text $label -command [list $service run] |
---|
175 | } |
---|
176 | separator { |
---|
177 | # no widget to create |
---|
178 | set _name2info($name-value) "--" |
---|
179 | } |
---|
180 | note { |
---|
181 | Rappture::Note $w $_owner $path |
---|
182 | } |
---|
183 | periodicelement { |
---|
184 | Rappture::PeriodicElementEntry $w $_owner $path |
---|
185 | bind $w <<Value>> [itcl::code $this _controlChanged $name] |
---|
186 | } |
---|
187 | default { |
---|
188 | error "don't know how to add control type \"$type\"" |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | # |
---|
193 | # If this element has an <enable> expression, then register |
---|
194 | # its controlling widget here. |
---|
195 | # |
---|
196 | set notify [string trim [$_owner xml get $path.about.notify]] |
---|
197 | |
---|
198 | set disablestyle [string trim [$_owner xml get $path.about.disablestyle]] |
---|
199 | if { $disablestyle != "" } { |
---|
200 | set _name2info($name-disablestyle) $disablestyle |
---|
201 | } |
---|
202 | # |
---|
203 | # If this element has an <enable> expression, then register |
---|
204 | # its controlling widget here. |
---|
205 | # |
---|
206 | set enable [string trim [$_owner xml get $path.about.enable]] |
---|
207 | if {"" == $enable} { |
---|
208 | set enable yes |
---|
209 | } |
---|
210 | if {![string is boolean $enable]} { |
---|
211 | set re {([a-zA-Z_]+[0-9]*|\([^\(\)]+\)|[a-zA-Z_]+[0-9]*\([^\(\)]+\))(\.([a-zA-Z_]+[0-9]*|\([^\(\)]+\)|[a-zA-Z_]+[0-9]*\([^\(\)]+\)))*(:[-a-zA-Z0-9/]+)?} |
---|
212 | set rest $enable |
---|
213 | set enable "" |
---|
214 | set deps "" |
---|
215 | while {1} { |
---|
216 | if {[regexp -indices $re $rest match]} { |
---|
217 | foreach {s0 s1} $match break |
---|
218 | |
---|
219 | if {[string index $rest [expr {$s0-1}]] == "\""} { |
---|
220 | # string in ""'s? then leave it alone |
---|
221 | append enable [string range $rest 0 [expr {$s0-1}]] |
---|
222 | set rest [string range $rest $s0 end] |
---|
223 | if {[regexp -indices {[^\"]+\"} $rest match]} { |
---|
224 | foreach {s0 s1} $match break |
---|
225 | append enable [string range $rest $s0 $s1] |
---|
226 | set rest [string range $rest [expr {$s1+1}] end] |
---|
227 | } else { |
---|
228 | puts stderr "WARNING: mismatched quote in enable condition for $path" |
---|
229 | puts stderr " expr: [string trim [$_owner xml get $path.about.enable]]" |
---|
230 | set rest "" |
---|
231 | } |
---|
232 | } else { |
---|
233 | # |
---|
234 | # This is a symbol which should be substituted |
---|
235 | # it can be either: |
---|
236 | # input.foo.bar |
---|
237 | # input.foo.bar:units |
---|
238 | # |
---|
239 | set cpath [string range $rest $s0 $s1] |
---|
240 | set parts [split $cpath :] |
---|
241 | set ccpath [lindex $parts 0] |
---|
242 | set units [lindex $parts 1] |
---|
243 | |
---|
244 | # make sure we have the standard path notation |
---|
245 | set stdpath [$_owner regularize $ccpath] |
---|
246 | if {"" == $stdpath} { |
---|
247 | puts stderr "WARNING: don't recognize parameter $cpath in <enable> expression for $path. This may be buried in a structure that is not yet loaded." |
---|
248 | set stdpath $ccpath |
---|
249 | } |
---|
250 | # substitute [_controlValue ...] call in place of path |
---|
251 | append enable [string range $rest 0 [expr {$s0-1}]] |
---|
252 | append enable [format {[_controlValue %s %s]} $stdpath $units] |
---|
253 | lappend deps $stdpath |
---|
254 | set rest [string range $rest [expr {$s1+1}] end] |
---|
255 | } |
---|
256 | } else { |
---|
257 | append enable $rest |
---|
258 | break |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | foreach cpath $deps { |
---|
263 | $_owner dependenciesfor $cpath $path |
---|
264 | } |
---|
265 | } |
---|
266 | set _name2info($name-enable) $enable |
---|
267 | |
---|
268 | set hidden [$_owner xml get $_name2info($name-path).hide] |
---|
269 | if { $hidden != "" } { |
---|
270 | set _name2info($name-enable) [expr !$hidden] |
---|
271 | } |
---|
272 | $_owner widgetfor $path $w |
---|
273 | |
---|
274 | if {[lsearch {control group separator note} $type] < 0} { |
---|
275 | # make a label for this control |
---|
276 | set label [$w label] |
---|
277 | if {"" != $label} { |
---|
278 | set _name2info($name-label) $_frame.l$name |
---|
279 | set font [option get $itk_component(hull) labelFont Font] |
---|
280 | label $_name2info($name-label) -text [_formatLabel $label] \ |
---|
281 | -font $font |
---|
282 | } |
---|
283 | |
---|
284 | # register the tooltip for this control |
---|
285 | set tip [$w tooltip] |
---|
286 | if {"" != $tip} { |
---|
287 | Rappture::Tooltip::for $w $tip -log $path |
---|
288 | |
---|
289 | # add the tooltip to the label too, if there is one |
---|
290 | if {$_name2info($name-label) != ""} { |
---|
291 | Rappture::Tooltip::for $_name2info($name-label) $tip -log $path |
---|
292 | } |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | # insert the new control onto the known list |
---|
297 | set _controls [linsert $_controls $pos $name] |
---|
298 | _monitor $name on |
---|
299 | |
---|
300 | # now that we have a new control, we should fix the layout |
---|
301 | $_dispatcher event -idle !layout |
---|
302 | _controlChanged $name |
---|
303 | |
---|
304 | return $name |
---|
305 | } |
---|
306 | |
---|
307 | # ---------------------------------------------------------------------- |
---|
308 | # USAGE: delete <first> ?<last>? |
---|
309 | # |
---|
310 | # Clients use this to delete one or more controls from this widget. |
---|
311 | # The <first> and <last> represent the integer index of the desired |
---|
312 | # control. You can use the "index" method to convert a control name to |
---|
313 | # its integer index. If only <first> is specified, then that one |
---|
314 | # control is deleted. If <last> is specified, then all controls in the |
---|
315 | # range <first> to <last> are deleted. |
---|
316 | # ---------------------------------------------------------------------- |
---|
317 | itcl::body Rappture::Controls::delete {first {last ""}} { |
---|
318 | if {$last == ""} { |
---|
319 | set last $first |
---|
320 | } |
---|
321 | if {![regexp {^[0-9]+|end$} $first]} { |
---|
322 | error "bad index \"$first\": should be integer or \"end\"" |
---|
323 | } |
---|
324 | if {![regexp {^[0-9]+|end$} $last]} { |
---|
325 | error "bad index \"$last\": should be integer or \"end\"" |
---|
326 | } |
---|
327 | |
---|
328 | foreach name [lrange $_controls $first $last] { |
---|
329 | _monitor $name off |
---|
330 | |
---|
331 | if {"" != $_name2info($name-label)} { |
---|
332 | destroy $_name2info($name-label) |
---|
333 | } |
---|
334 | if {"" != $_name2info($name-value)} { |
---|
335 | destroy $_name2info($name-value) |
---|
336 | } |
---|
337 | $_owner widgetfor $_name2info($name-path) "" |
---|
338 | array unset _name2info $name-* |
---|
339 | } |
---|
340 | set _controls [lreplace $_controls $first $last] |
---|
341 | |
---|
342 | $_dispatcher event -idle !layout |
---|
343 | } |
---|
344 | |
---|
345 | # ---------------------------------------------------------------------- |
---|
346 | # USAGE: index <name>|@n |
---|
347 | # |
---|
348 | # Clients use this to convert a control <name> into its corresponding |
---|
349 | # integer index. Returns an error if the <name> is not recognized. |
---|
350 | # ---------------------------------------------------------------------- |
---|
351 | itcl::body Rappture::Controls::index {name} { |
---|
352 | set i [lsearch $_controls $name] |
---|
353 | if {$i >= 0} { |
---|
354 | return $i |
---|
355 | } |
---|
356 | if {[regexp {^@([0-9]+)$} $name match i]} { |
---|
357 | return $i |
---|
358 | } |
---|
359 | if {$name == "end"} { |
---|
360 | return [expr {[llength $_controls]-1}] |
---|
361 | } |
---|
362 | error "bad control name \"$name\": should be @int or one of [join [lsort $_controls] {, }]" |
---|
363 | } |
---|
364 | |
---|
365 | # ---------------------------------------------------------------------- |
---|
366 | # USAGE: control ?-label|-value|-path|-enable? ?<name>|@n? |
---|
367 | # |
---|
368 | # Clients use this to get information about controls. With no args, it |
---|
369 | # returns a list of all control names. Otherwise, it returns the frame |
---|
370 | # associated with a control name. The -label option requests the label |
---|
371 | # widget instead of the value widget. The -path option requests the |
---|
372 | # path within the XML that the control affects. The -enable option |
---|
373 | # requests the enabling condition for this control. |
---|
374 | # ---------------------------------------------------------------------- |
---|
375 | itcl::body Rappture::Controls::control {args} { |
---|
376 | if {[llength $args] == 0} { |
---|
377 | return $_controls |
---|
378 | } |
---|
379 | Rappture::getopts args params { |
---|
380 | flag switch -value default |
---|
381 | flag switch -label |
---|
382 | flag switch -path |
---|
383 | flag switch -enable |
---|
384 | flag switch -disablestyle |
---|
385 | } |
---|
386 | if {[llength $args] == 0} { |
---|
387 | error "missing control name" |
---|
388 | } |
---|
389 | set i [index [lindex $args 0]] |
---|
390 | set name [lindex $_controls $i] |
---|
391 | |
---|
392 | set opt $params(switch) |
---|
393 | return $_name2info($name$opt) |
---|
394 | } |
---|
395 | |
---|
396 | # ---------------------------------------------------------------------- |
---|
397 | # USAGE: refresh |
---|
398 | # |
---|
399 | # Clients use this to refresh the layout of the control panel |
---|
400 | # whenever a widget within the panel changes visibility state. |
---|
401 | # ---------------------------------------------------------------------- |
---|
402 | itcl::body Rappture::Controls::refresh {} { |
---|
403 | $_dispatcher event -idle !layout |
---|
404 | } |
---|
405 | |
---|
406 | # ---------------------------------------------------------------------- |
---|
407 | # USAGE: _layout |
---|
408 | # |
---|
409 | # Used internally to fix the layout of controls whenever controls |
---|
410 | # are added or deleted, or when the control arrangement changes. |
---|
411 | # There are a lot of heuristics here trying to achieve a "good" |
---|
412 | # arrangement of controls. |
---|
413 | # ---------------------------------------------------------------------- |
---|
414 | itcl::body Rappture::Controls::_layout {} { |
---|
415 | # |
---|
416 | # Clear any existing layout |
---|
417 | # |
---|
418 | foreach name $_controls { |
---|
419 | foreach elem {label value} { |
---|
420 | set w $_name2info($name-$elem) |
---|
421 | if {$w != "" && [winfo exists $w]} { |
---|
422 | grid forget $w |
---|
423 | } |
---|
424 | } |
---|
425 | } |
---|
426 | if {[$_tabs size] > 0} { |
---|
427 | $_tabs delete 0 end |
---|
428 | } |
---|
429 | grid forget $_frame.empty |
---|
430 | |
---|
431 | # |
---|
432 | # Decide which widgets should be shown and which should be hidden. |
---|
433 | # |
---|
434 | set hidden "" |
---|
435 | set showing "" |
---|
436 | foreach name $_controls { |
---|
437 | set show 1 |
---|
438 | set cond $_name2info($name-enable) |
---|
439 | if {[string is boolean $cond] && !$cond} { |
---|
440 | # hard-coded "off" -- ignore completely |
---|
441 | } elseif {[catch {expr $cond} show] == 0} { |
---|
442 | set type $_name2info($name-type) |
---|
443 | set disablestyle $_name2info($name-disablestyle) |
---|
444 | set lwidget $_name2info($name-label) |
---|
445 | set vwidget $_name2info($name-value) |
---|
446 | if {[lsearch -exact {group image structure} $type] >= 0 || |
---|
447 | $disablestyle == "hide" } { |
---|
448 | if {$show ne "" && $show} { |
---|
449 | lappend showing $name |
---|
450 | } else { |
---|
451 | lappend hidden $name |
---|
452 | } |
---|
453 | } else { |
---|
454 | # show other objects, but enable/disable them |
---|
455 | lappend showing $name |
---|
456 | if {$show ne "" && $show} { |
---|
457 | if {[winfo exists $vwidget]} { |
---|
458 | $vwidget configure -state normal |
---|
459 | } |
---|
460 | if {[winfo exists $lwidget]} { |
---|
461 | $lwidget configure -foreground \ |
---|
462 | [lindex [$lwidget configure -foreground] 3] |
---|
463 | } |
---|
464 | } else { |
---|
465 | if {[winfo exists $vwidget]} { |
---|
466 | $vwidget configure -state disabled |
---|
467 | } |
---|
468 | if {[winfo exists $lwidget]} { |
---|
469 | $lwidget configure -foreground gray |
---|
470 | } |
---|
471 | } |
---|
472 | } |
---|
473 | } else { |
---|
474 | bgerror "Error in <enable> expression for \"$_name2info($name-path)\":\n $show" |
---|
475 | } |
---|
476 | } |
---|
477 | |
---|
478 | # store the showing tabs in the object so it can be used in _changeTabs |
---|
479 | set _showing $showing |
---|
480 | |
---|
481 | # |
---|
482 | # Decide on a layout scheme: |
---|
483 | # tabs ...... best if all elements within are groups |
---|
484 | # hlabels ... horizontal labels (label: value) |
---|
485 | # |
---|
486 | if {[llength $showing] >= 2} { |
---|
487 | # assume tabs for multiple groups |
---|
488 | set _scheme tabs |
---|
489 | foreach name $showing { |
---|
490 | set w $_name2info($name-value) |
---|
491 | |
---|
492 | if {$w == "--" || [winfo class $w] != "GroupEntry"} { |
---|
493 | # something other than a group? then fall back on hlabels |
---|
494 | set _scheme hlabels |
---|
495 | break |
---|
496 | } |
---|
497 | } |
---|
498 | } else { |
---|
499 | set _scheme hlabels |
---|
500 | } |
---|
501 | |
---|
502 | switch -- $_scheme { |
---|
503 | tabs { |
---|
504 | # |
---|
505 | # SCHEME: tabs |
---|
506 | # put a series of groups into a tabbed notebook |
---|
507 | # |
---|
508 | |
---|
509 | # use inner frame within tabs to show current group |
---|
510 | pack $_tabs -before $_frame -fill x |
---|
511 | |
---|
512 | set gn 1 |
---|
513 | foreach name $showing { |
---|
514 | set wv $_name2info($name-value) |
---|
515 | $wv configure -heading no |
---|
516 | |
---|
517 | set label [$wv component heading cget -text] |
---|
518 | if {"" == $label} { |
---|
519 | set label "Group #$gn" |
---|
520 | } |
---|
521 | set _name2info($name-label) $label |
---|
522 | $_tabs insert end $name -text $label \ |
---|
523 | -activebackground $itk_option(-background) |
---|
524 | |
---|
525 | incr gn |
---|
526 | } |
---|
527 | |
---|
528 | # compute the overall size |
---|
529 | # BE CAREFUL: do this after setting "-heading no" above |
---|
530 | $_dispatcher event -now !resize |
---|
531 | |
---|
532 | grid propagate $_frame off |
---|
533 | grid columnconfigure $_frame 0 -weight 1 |
---|
534 | grid rowconfigure $_frame 0 -weight 1 |
---|
535 | |
---|
536 | $_tabs select 0; _changeTabs |
---|
537 | } |
---|
538 | |
---|
539 | hlabels { |
---|
540 | # |
---|
541 | # SCHEME: hlabels |
---|
542 | # simple "Label: Value" layout |
---|
543 | # |
---|
544 | pack forget $_tabs |
---|
545 | grid propagate $_frame on |
---|
546 | grid columnconfigure $_frame 0 -weight 0 |
---|
547 | grid rowconfigure $_frame 0 -weight 0 |
---|
548 | |
---|
549 | set expand 0 ;# most controls float to top |
---|
550 | set row 0 |
---|
551 | foreach name $showing { |
---|
552 | set wl $_name2info($name-label) |
---|
553 | if {$wl != "" && [winfo exists $wl]} { |
---|
554 | grid $wl -row $row -column 0 -sticky e |
---|
555 | } |
---|
556 | |
---|
557 | set wv $_name2info($name-value) |
---|
558 | if {$wv != "" && [winfo exists $wv]} { |
---|
559 | if {$wl != ""} { |
---|
560 | grid $wv -row $row -column 1 -sticky ew |
---|
561 | } else { |
---|
562 | grid $wv -row $row -column 0 -columnspan 2 -sticky ew |
---|
563 | } |
---|
564 | |
---|
565 | grid rowconfigure $_frame $row -weight 0 |
---|
566 | |
---|
567 | switch -- [winfo class $wv] { |
---|
568 | TextEntry { |
---|
569 | if {[regexp {[0-9]+x[0-9]+} [$wv size]]} { |
---|
570 | grid $wl -sticky n -pady 4 |
---|
571 | grid $wv -sticky nsew |
---|
572 | grid rowconfigure $_frame $row -weight 1 |
---|
573 | grid columnconfigure $_frame 1 -weight 1 |
---|
574 | set expand 1 |
---|
575 | } |
---|
576 | } |
---|
577 | GroupEntry { |
---|
578 | $wv configure -heading yes |
---|
579 | |
---|
580 | # |
---|
581 | # Scan through all children in this group |
---|
582 | # and see if any demand more space. If the |
---|
583 | # group contains a structure or a note, then |
---|
584 | # make sure that the group itself is set to |
---|
585 | # expand/fill. |
---|
586 | # |
---|
587 | set queue [winfo children $wv] |
---|
588 | set expandgroup 0 |
---|
589 | while {[llength $queue] > 0} { |
---|
590 | set w [lindex $queue 0] |
---|
591 | set queue [lrange $queue 1 end] |
---|
592 | set c [winfo class $w] |
---|
593 | if {[lsearch {DeviceEditor Note} $c] >= 0} { |
---|
594 | set expandgroup 1 |
---|
595 | break |
---|
596 | } |
---|
597 | eval lappend queue [winfo children $w] |
---|
598 | } |
---|
599 | if {$expandgroup} { |
---|
600 | set expand 1 |
---|
601 | grid $wv -sticky nsew |
---|
602 | grid rowconfigure $_frame $row -weight 1 |
---|
603 | } |
---|
604 | } |
---|
605 | Note { |
---|
606 | grid $wv -sticky nsew |
---|
607 | grid rowconfigure $_frame $row -weight 1 |
---|
608 | set expand 1 |
---|
609 | } |
---|
610 | } |
---|
611 | grid columnconfigure $_frame 1 -weight 1 |
---|
612 | } elseif {$wv == "--"} { |
---|
613 | grid rowconfigure $_frame $row -minsize 10 |
---|
614 | } |
---|
615 | |
---|
616 | incr row |
---|
617 | grid rowconfigure $_frame $row -minsize $itk_option(-padding) |
---|
618 | incr row |
---|
619 | } |
---|
620 | grid $_frame.empty -row $row |
---|
621 | |
---|
622 | # |
---|
623 | # If there are any hidden items, then make the bottom of |
---|
624 | # this form fill up any extra space, so the form floats |
---|
625 | # to the top. Otherwise, it will jitter around as the |
---|
626 | # hidden items come and go. |
---|
627 | # |
---|
628 | if {[llength $hidden] > 0 && !$expand} { |
---|
629 | grid rowconfigure $_frame 99 -weight 1 |
---|
630 | } else { |
---|
631 | grid rowconfigure $_frame 99 -weight 0 |
---|
632 | } |
---|
633 | } |
---|
634 | } |
---|
635 | } |
---|
636 | |
---|
637 | # ---------------------------------------------------------------------- |
---|
638 | # USAGE: _monitor <name> <state> |
---|
639 | # |
---|
640 | # Used internally to add/remove bindings that cause the widget |
---|
641 | # associated with <name> to notify this controls widget of size |
---|
642 | # changes. Whenever there is a size change, this controls widget |
---|
643 | # should fix its layout. |
---|
644 | # ---------------------------------------------------------------------- |
---|
645 | itcl::body Rappture::Controls::_monitor {name state} { |
---|
646 | set tag "Controls-$this" |
---|
647 | set wv $_name2info($name-value) |
---|
648 | if {$wv == "--" || [catch {bindtags $wv} btags]} { |
---|
649 | return |
---|
650 | } |
---|
651 | set i [lsearch $btags $tag] |
---|
652 | |
---|
653 | if {$state} { |
---|
654 | if {$i < 0} { |
---|
655 | bindtags $wv [linsert $btags 0 $tag] |
---|
656 | } |
---|
657 | } else { |
---|
658 | if {$i >= 0} { |
---|
659 | bindtags $wv [lreplace $btags $i $i] |
---|
660 | } |
---|
661 | } |
---|
662 | } |
---|
663 | |
---|
664 | # ---------------------------------------------------------------------- |
---|
665 | # USAGE: _controlChanged <name> |
---|
666 | # |
---|
667 | # Invoked automatically whenever the value for a control changes. |
---|
668 | # Sends a notification along to the tool controlling this panel. |
---|
669 | # ---------------------------------------------------------------------- |
---|
670 | itcl::body Rappture::Controls::_controlChanged {name} { |
---|
671 | set path $_name2info($name-path) |
---|
672 | |
---|
673 | # |
---|
674 | # Let the owner know that this control changed. |
---|
675 | # |
---|
676 | if {"" != $_owner} { |
---|
677 | $_owner changed $path |
---|
678 | } |
---|
679 | } |
---|
680 | |
---|
681 | # ---------------------------------------------------------------------- |
---|
682 | # USAGE: _controlValue <path> ?<units>? |
---|
683 | # |
---|
684 | # Used internally to get the value of a control with the specified |
---|
685 | # <path>. Returns the current value for the control. |
---|
686 | # ---------------------------------------------------------------------- |
---|
687 | itcl::body Rappture::Controls::_controlValue {path {units ""}} { |
---|
688 | if {"" != $_owner} { |
---|
689 | set val [$_owner valuefor $path] |
---|
690 | if {"" != $units} { |
---|
691 | set val [Rappture::Units::convert $val -to $units -units off] |
---|
692 | } |
---|
693 | return $val |
---|
694 | } |
---|
695 | return "" |
---|
696 | } |
---|
697 | |
---|
698 | # ---------------------------------------------------------------------- |
---|
699 | # USAGE: _formatLabel <string> |
---|
700 | # |
---|
701 | # Used internally to format a label <string>. Trims any excess |
---|
702 | # white space and adds a ":" to the end. That way, all labels |
---|
703 | # have a uniform look. |
---|
704 | # ---------------------------------------------------------------------- |
---|
705 | itcl::body Rappture::Controls::_formatLabel {str} { |
---|
706 | set str [string trim $str] |
---|
707 | if {"" != $str && [string index $str end] != ":"} { |
---|
708 | append str ":" |
---|
709 | } |
---|
710 | return $str |
---|
711 | } |
---|
712 | |
---|
713 | # ---------------------------------------------------------------------- |
---|
714 | # USAGE: _changeTabs ?-user|-program? |
---|
715 | # |
---|
716 | # Used internally to change tabs when the user clicks on a tab |
---|
717 | # in the "tabs" layout mode. This mode is used when the widget |
---|
718 | # contains nothing but groups, as a compact way of representing |
---|
719 | # the groups. |
---|
720 | # ---------------------------------------------------------------------- |
---|
721 | itcl::body Rappture::Controls::_changeTabs {{why -program}} { |
---|
722 | set i [$_tabs index select] |
---|
723 | # we use _showing here instead of _controls because sometimes tabs |
---|
724 | # are disabled, and the index of the choosen tab always matches |
---|
725 | # _showing, even if tabs are disabled. |
---|
726 | set name [lindex $_showing $i] |
---|
727 | if {$name ne ""} { |
---|
728 | foreach w [grid slaves $_frame] { |
---|
729 | grid forget $w |
---|
730 | } |
---|
731 | |
---|
732 | set wv $_name2info($name-value) |
---|
733 | grid $wv -row 0 -column 0 -sticky new |
---|
734 | |
---|
735 | if {$why eq "-user"} { |
---|
736 | Rappture::Logger::log group $_name2info($name-path) |
---|
737 | } |
---|
738 | } |
---|
739 | } |
---|
740 | |
---|
741 | # ---------------------------------------------------------------------- |
---|
742 | # USAGE: _resize |
---|
743 | # |
---|
744 | # Used internally to resize the widget when its contents change. |
---|
745 | # ---------------------------------------------------------------------- |
---|
746 | itcl::body Rappture::Controls::_resize {} { |
---|
747 | switch -- $_scheme { |
---|
748 | tabs { |
---|
749 | # compute the overall size |
---|
750 | # BE CAREFUL: do this after setting "-heading no" above |
---|
751 | set maxw 0 |
---|
752 | set maxh 0 |
---|
753 | update idletasks |
---|
754 | foreach name $_controls { |
---|
755 | set wv $_name2info($name-value) |
---|
756 | set w [winfo reqwidth $wv] |
---|
757 | if {$w > $maxw} { set maxw $w } |
---|
758 | set h [winfo reqheight $wv] |
---|
759 | if {$h > $maxh} { set maxh $h } |
---|
760 | } |
---|
761 | $_frame configure -width $maxw -height $maxh |
---|
762 | } |
---|
763 | hlabels { |
---|
764 | # do nothing |
---|
765 | } |
---|
766 | } |
---|
767 | } |
---|
768 | |
---|
769 | # ---------------------------------------------------------------------- |
---|
770 | # OPTION: -padding |
---|
771 | # ---------------------------------------------------------------------- |
---|
772 | itcl::configbody Rappture::Controls::padding { |
---|
773 | $_dispatcher event -idle !layout |
---|
774 | } |
---|