1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: owner - manages Rappture controls |
---|
3 | # |
---|
4 | # This object represents an entity managing Rappture controls. |
---|
5 | # It is typically a Tool, a DeviceEditor, or some other large entity |
---|
6 | # that manages a Rappture XML tree. All controlling widgets are |
---|
7 | # registered with an owner, and the owner propagates notifications |
---|
8 | # out to clients who have an interest in a particular control. |
---|
9 | # ====================================================================== |
---|
10 | # AUTHOR: Michael McLennan, Purdue University |
---|
11 | # Copyright (c) 2004-2005 Purdue Research Foundation |
---|
12 | # |
---|
13 | # See the file "license.terms" for information on usage and |
---|
14 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
15 | # ====================================================================== |
---|
16 | package require Itcl |
---|
17 | |
---|
18 | itcl::class Rappture::ControlOwner { |
---|
19 | constructor {owner} { # defined below } |
---|
20 | |
---|
21 | public method xml {args} |
---|
22 | |
---|
23 | public method load {newobj} |
---|
24 | public method widgetfor {path args} |
---|
25 | public method valuefor {path args} |
---|
26 | public method dependenciesfor {path args} |
---|
27 | public method ownerfor {path {skip ""}} |
---|
28 | public method changed {path} |
---|
29 | public method regularize {path} |
---|
30 | public method notify {option owner args} |
---|
31 | public method sync {} |
---|
32 | public method tool {} |
---|
33 | |
---|
34 | protected method _slave {option args} |
---|
35 | |
---|
36 | protected variable _owner "" ;# ControlOwner containing this one |
---|
37 | protected variable _path "" ;# paths within are relative to this |
---|
38 | protected variable _slaves "" ;# this owner has these slaves |
---|
39 | protected variable _xmlobj "" ;# Rappture XML description |
---|
40 | private variable _path2widget ;# maps path => widget on this page |
---|
41 | private variable _owner2paths ;# for notify: maps owner => interests |
---|
42 | private variable _type2curpath ;# maps type(path) => path's current value |
---|
43 | private variable _callbacks ;# for notify: maps owner/path => callback |
---|
44 | private variable _dependencies ;# maps path => other paths dep on this |
---|
45 | } |
---|
46 | |
---|
47 | # ---------------------------------------------------------------------- |
---|
48 | # CONSTRUCTOR |
---|
49 | # ---------------------------------------------------------------------- |
---|
50 | itcl::body Rappture::ControlOwner::constructor {owner} { |
---|
51 | if {"" != $owner} { |
---|
52 | set parts [split $owner @] |
---|
53 | set _owner [lindex $parts 0] |
---|
54 | set _path [lindex $parts 1] |
---|
55 | $_owner _slave add $this |
---|
56 | } |
---|
57 | |
---|
58 | # we are adding this so notes can be used |
---|
59 | # in coordination with loaders inside the load function |
---|
60 | array set _type2curpath { |
---|
61 | choice current |
---|
62 | boolean current |
---|
63 | image current |
---|
64 | integer current |
---|
65 | loader current |
---|
66 | note contents |
---|
67 | number current |
---|
68 | string current |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | # ---------------------------------------------------------------------- |
---|
73 | # USAGE: xml <subcommand> ?<arg> <arg> ...? |
---|
74 | # USAGE: xml object |
---|
75 | # |
---|
76 | # Used by clients to manipulate the underlying XML data for this |
---|
77 | # tool. The <subcommand> can be any operation supported by a |
---|
78 | # Rappture::library object. Clients can also request the XML object |
---|
79 | # directly by using the "object" subcommand. |
---|
80 | # ---------------------------------------------------------------------- |
---|
81 | itcl::body Rappture::ControlOwner::xml {args} { |
---|
82 | if {"object" == $args} { |
---|
83 | return $_xmlobj |
---|
84 | } |
---|
85 | return [eval $_xmlobj $args] |
---|
86 | } |
---|
87 | |
---|
88 | # ---------------------------------------------------------------------- |
---|
89 | # USAGE: widgetfor <path> ?<widget>? |
---|
90 | # |
---|
91 | # Used by embedded widgets such as a Controls panel to register the |
---|
92 | # various controls associated with this page. That way, this |
---|
93 | # ControlOwner knows what widgets to look at when syncing itself |
---|
94 | # to the underlying XML data. |
---|
95 | # ---------------------------------------------------------------------- |
---|
96 | itcl::body Rappture::ControlOwner::widgetfor {path args} { |
---|
97 | # if this is a query operation, then look for the path |
---|
98 | if {[llength $args] == 0} { |
---|
99 | set owner [ownerfor $path] |
---|
100 | if {$owner != $this && $owner != ""} { |
---|
101 | return [$owner widgetfor $path] |
---|
102 | } |
---|
103 | if {[info exists _path2widget($path)]} { |
---|
104 | return $_path2widget($path) |
---|
105 | } |
---|
106 | return "" |
---|
107 | } |
---|
108 | |
---|
109 | # otherwise, associate the path with the given widget |
---|
110 | set widget [lindex $args 0] |
---|
111 | if {"" != $widget} { |
---|
112 | if {[info exists _path2widget($path)]} { |
---|
113 | error "$path already associated with widget $_path2widget($path)" |
---|
114 | } |
---|
115 | set _path2widget($path) $widget |
---|
116 | } else { |
---|
117 | catch {unset _path2widget($path)} |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | # ---------------------------------------------------------------------- |
---|
122 | # USAGE: valuefor <path> ?<newValue>? |
---|
123 | # |
---|
124 | # Used by embedded widgets such as a Loader to query or set the |
---|
125 | # value of another control. With no extra args, it returns the |
---|
126 | # value of the widget at the <path> in the XML. Otherwise, it |
---|
127 | # sets the value of the widget to <newValue>. |
---|
128 | # ---------------------------------------------------------------------- |
---|
129 | itcl::body Rappture::ControlOwner::valuefor {path args} { |
---|
130 | set owner [ownerfor $path] |
---|
131 | |
---|
132 | # if this is a query operation, then look for the path |
---|
133 | if {[llength $args] == 0} { |
---|
134 | if {$owner != $this && $owner != ""} { |
---|
135 | return [$owner valuefor $path] |
---|
136 | } |
---|
137 | if {[info exists _path2widget($path)]} { |
---|
138 | return [$_path2widget($path) value] |
---|
139 | } |
---|
140 | # can't find the path? try removing the prefix for this owner |
---|
141 | set plen [string length $_path] |
---|
142 | if {[string equal -length $plen $_path $path]} { |
---|
143 | set relpath [string range $path [expr {$plen+1}] end] |
---|
144 | if {[info exists _path2widget($relpath)]} { |
---|
145 | return [$_path2widget($relpath) value] |
---|
146 | } |
---|
147 | } |
---|
148 | return "" |
---|
149 | } |
---|
150 | |
---|
151 | # otherwise, set the value |
---|
152 | if {$owner != $this && $owner != ""} { |
---|
153 | return [eval $owner valuefor $path $args] |
---|
154 | } |
---|
155 | if {[llength $args] > 1} { |
---|
156 | error "wrong # args: should be \"valuefor path ?newValue?\"" |
---|
157 | } |
---|
158 | |
---|
159 | if {[info exists _path2widget($path)]} { |
---|
160 | $_path2widget($path) value [lindex $args 0] |
---|
161 | } else { |
---|
162 | error "bad path \"$path\": should be one of [join [lsort [array names _path2widget]] {, }]" |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | # ---------------------------------------------------------------------- |
---|
167 | # USAGE: dependenciesfor <path> ?<path>...? |
---|
168 | # |
---|
169 | # Used by embedded widgets such as a Controls panel to register the |
---|
170 | # various controls that are dependent on another one. If only one |
---|
171 | # path is specified, then this method returns all known dependencies |
---|
172 | # for the specified <path>. Otherwise, the additional <path>'s are |
---|
173 | # noted as being dependent on the first <path>. |
---|
174 | # ---------------------------------------------------------------------- |
---|
175 | itcl::body Rappture::ControlOwner::dependenciesfor {path args} { |
---|
176 | if {"" != $_owner} { |
---|
177 | # |
---|
178 | # Keep all dependencies at the highest level. |
---|
179 | # That way, a structure can come and go, but the |
---|
180 | # dependencies remain fixed in the topmost tool. |
---|
181 | # |
---|
182 | set plen [string length $_path] |
---|
183 | if {"" != $_path && ![string equal -length $plen $_path $path]} { |
---|
184 | set path $_path.$path |
---|
185 | } |
---|
186 | return [eval $_owner dependenciesfor $path $args] |
---|
187 | } |
---|
188 | |
---|
189 | # if this is a query operation, then look for the path |
---|
190 | if {[llength $args] == 0} { |
---|
191 | if {[info exists _dependencies($path)]} { |
---|
192 | return $_dependencies($path) |
---|
193 | } |
---|
194 | return "" |
---|
195 | } |
---|
196 | |
---|
197 | # add new dependencies |
---|
198 | if {![info exists _dependencies($path)]} { |
---|
199 | set _dependencies($path) "" |
---|
200 | } |
---|
201 | foreach dpath $args { |
---|
202 | set i [lsearch -exact $_dependencies($path) $dpath] |
---|
203 | if {$i < 0} { |
---|
204 | lappend _dependencies($path) $dpath |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | # ---------------------------------------------------------------------- |
---|
210 | # USAGE: ownerfor <path> ?<skip>? |
---|
211 | # |
---|
212 | # Returns the ControlOwner that directly controls the specified <path>. |
---|
213 | # ---------------------------------------------------------------------- |
---|
214 | itcl::body Rappture::ControlOwner::ownerfor {path {skip ""}} { |
---|
215 | if {[info exists _path2widget($path)]} { |
---|
216 | return $this |
---|
217 | } |
---|
218 | |
---|
219 | # can't find the path? try removing the prefix for this owner |
---|
220 | set plen [string length $_path] |
---|
221 | if {[string equal -length $plen $_path $path]} { |
---|
222 | set relpath [string range $path [expr {$plen+1}] end] |
---|
223 | if {[info exists _path2widget($relpath)]} { |
---|
224 | return $this |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | # couldn't find this path? then check all subordinates |
---|
229 | foreach slave $_slaves { |
---|
230 | if {$slave == $skip} { |
---|
231 | continue ;# skip this slave if it's already been searched |
---|
232 | } |
---|
233 | set rval [$slave ownerfor $path $this] |
---|
234 | if {"" != $rval} { |
---|
235 | return $rval |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | # check the owner as a last resort |
---|
240 | if {"" != $_owner && $_owner != $skip} { |
---|
241 | set rval [$_owner ownerfor $path $this] |
---|
242 | if {"" != $rval} { |
---|
243 | return $rval |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | return "" |
---|
248 | } |
---|
249 | |
---|
250 | # ---------------------------------------------------------------------- |
---|
251 | # USAGE: load <xmlobj> |
---|
252 | # |
---|
253 | # Loads the contents of a Rappture <xmlobj> into the controls |
---|
254 | # associated with this tool. |
---|
255 | # ---------------------------------------------------------------------- |
---|
256 | itcl::body Rappture::ControlOwner::load {newobj} { |
---|
257 | if {![Rappture::library isvalid $newobj]} { |
---|
258 | error "\"$newobj\" is not a Rappture::library" |
---|
259 | } |
---|
260 | |
---|
261 | foreach path [array names _path2widget] { |
---|
262 | # the following elements do not accept "current" tags, skip them |
---|
263 | set type [[tool] xml element -as type $path] |
---|
264 | if {[lsearch {group separator control} $type] >= 0} { |
---|
265 | continue |
---|
266 | } |
---|
267 | |
---|
268 | set type [[tool] xml element -as type $path] |
---|
269 | if {[info exists _type2curpath($type)]} { |
---|
270 | set currentpath $path.$_type2curpath($type) |
---|
271 | } else { |
---|
272 | # default incase i forgot an input type in _type2curpath |
---|
273 | set currentpath $path.current |
---|
274 | } |
---|
275 | |
---|
276 | # copy new value to the XML tree |
---|
277 | # we only copy the values if they existed in newobj |
---|
278 | # so we don't overwrite values that were set in previous loads. |
---|
279 | # this is needed for when the users specify copy.from and copy.to |
---|
280 | # in a loader. in this case, _path2widget holds a list of all |
---|
281 | # widgets. if there are two loaders loading two different widgets, |
---|
282 | # and each loader uses the copy from/to functionality, |
---|
283 | # the second load could wipe out the values set in the first load |
---|
284 | # because on the second load, the copied paths from the first load no |
---|
285 | # longer exist in newobj and blanks are copied to the paths |
---|
286 | # in [tool] xml set by the first loader. the solution is to check |
---|
287 | # newobj and see if the path exists. if the path exists, then we copy |
---|
288 | # it over to [tool] xml, otherwise we ignore it. |
---|
289 | if {"" != [$newobj element -as component $currentpath]} { |
---|
290 | [tool] xml copy $currentpath from $newobj $currentpath |
---|
291 | } |
---|
292 | |
---|
293 | # also copy to the widget associated with the tree |
---|
294 | if {"" != [$newobj element -as type $path]} { |
---|
295 | set val [$newobj get $currentpath] |
---|
296 | if {[string length $val] > 0 |
---|
297 | || [llength [$newobj children $currentpath]] == 0} { |
---|
298 | $_path2widget($path) value $val |
---|
299 | } else { |
---|
300 | set obj [$newobj element -as object $currentpath] |
---|
301 | $_path2widget($path) value $obj |
---|
302 | } |
---|
303 | } |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | # ---------------------------------------------------------------------- |
---|
308 | # USAGE: changed <path> |
---|
309 | # |
---|
310 | # Invoked automatically by the various widgets associated with this |
---|
311 | # tool whenever their value changes. Sends notifications to any |
---|
312 | # client that has registered an interest via "notify add". |
---|
313 | # ---------------------------------------------------------------------- |
---|
314 | itcl::body Rappture::ControlOwner::changed {path} { |
---|
315 | if {"" != $_owner} { |
---|
316 | set plen [string length $_path] |
---|
317 | if {"" != $_path && ![string equal -length $plen $_path $path]} { |
---|
318 | set path $_path.$path |
---|
319 | } |
---|
320 | $_owner changed $path |
---|
321 | } else { |
---|
322 | # send out any callback notifications |
---|
323 | foreach owner [array names _owner2paths] { |
---|
324 | foreach pattern $_owner2paths($owner) { |
---|
325 | if {[string match $pattern $path]} { |
---|
326 | uplevel #0 $_callbacks($owner/$pattern) |
---|
327 | break |
---|
328 | } |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | # find the control panel for each dependency, and tell it |
---|
333 | # to update its layout. |
---|
334 | foreach cpath [dependenciesfor $path] { |
---|
335 | set wv [widgetfor $cpath] |
---|
336 | while {"" != $wv} { |
---|
337 | set wv [winfo parent $wv] |
---|
338 | if {[winfo class $wv] == "Controls"} { |
---|
339 | $wv refresh |
---|
340 | break |
---|
341 | } |
---|
342 | } |
---|
343 | } |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | # ---------------------------------------------------------------------- |
---|
348 | # USAGE: regularize <path> |
---|
349 | # |
---|
350 | # Clients use this to get a full, regularized path for the specified |
---|
351 | # <path>, which may be relative to the current owner. |
---|
352 | # ---------------------------------------------------------------------- |
---|
353 | itcl::body Rappture::ControlOwner::regularize {path} { |
---|
354 | set owner [ownerfor $path] |
---|
355 | if {$owner != $this && $owner != ""} { |
---|
356 | return [$owner regularize $path] |
---|
357 | } |
---|
358 | set rpath "" |
---|
359 | if {"" != $_xmlobj} { |
---|
360 | set rpath [$_xmlobj element -as path $path] |
---|
361 | |
---|
362 | # can't find the path? try removing the prefix for this owner |
---|
363 | if {"" == $rpath} { |
---|
364 | set plen [string length $_path] |
---|
365 | if {[string equal -length $plen $_path $path]} { |
---|
366 | set relpath [string range $path [expr {$plen+2}] end] |
---|
367 | set rpath [$_xmlobj element -as path $relpath] |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | if {"" != $rpath && "" != $_path} { |
---|
372 | # return a full name for the path |
---|
373 | set rpath "$_path.$rpath" |
---|
374 | } |
---|
375 | } |
---|
376 | return $rpath |
---|
377 | } |
---|
378 | |
---|
379 | # ---------------------------------------------------------------------- |
---|
380 | # USAGE: notify add <owner> <path> <callback> |
---|
381 | # USAGE: notify info ?<owner>? ?<path>? |
---|
382 | # USAGE: notify remove <owner> ?<path> ...? |
---|
383 | # |
---|
384 | # Clients use this to request notifications about changes to a |
---|
385 | # particular <path> for a control under this tool. Whenever the |
---|
386 | # value associated with <path> changes, the client identified by |
---|
387 | # <owner> is sent a message by invoking its <callback> routine. |
---|
388 | # |
---|
389 | # Notifications can be silenced by calling the "notify remove" |
---|
390 | # function. |
---|
391 | # ---------------------------------------------------------------------- |
---|
392 | itcl::body Rappture::ControlOwner::notify {option args} { |
---|
393 | switch -- $option { |
---|
394 | add { |
---|
395 | if {[llength $args] != 3} { |
---|
396 | error "wrong # args: should be \"notify add owner path callback\"" |
---|
397 | } |
---|
398 | set owner [lindex $args 0] |
---|
399 | set path [lindex $args 1] |
---|
400 | set cb [lindex $args 2] |
---|
401 | |
---|
402 | if {[info exists _owner2paths($owner)]} { |
---|
403 | set plist $_owner2paths($owner) |
---|
404 | } else { |
---|
405 | set plist "" |
---|
406 | } |
---|
407 | |
---|
408 | set i [lsearch -exact $plist $path] |
---|
409 | if {$i < 0} { lappend _owner2paths($owner) $path } |
---|
410 | set _callbacks($owner/$path) $cb |
---|
411 | } |
---|
412 | info { |
---|
413 | if {[llength $args] == 0} { |
---|
414 | # no args? then return all owners |
---|
415 | return [array names _owner2paths] |
---|
416 | } else { |
---|
417 | set owner [lindex $args 0] |
---|
418 | if {[info exists _owner2paths($owner)]} { |
---|
419 | set plist $_owner2paths($owner) |
---|
420 | } else { |
---|
421 | set plist "" |
---|
422 | } |
---|
423 | if {[llength $args] == 1} { |
---|
424 | # 1 arg? then return paths for this owner |
---|
425 | return $plist |
---|
426 | } elseif {[llength $args] == 2} { |
---|
427 | # 2 args? then return callback for this path |
---|
428 | set path [lindex $args 1] |
---|
429 | if {[info exists _callbacks($owner/$path)]} { |
---|
430 | return $_callbacks($owner/$path) |
---|
431 | } |
---|
432 | return "" |
---|
433 | } else { |
---|
434 | error "wrong # args: should be \"notify info ?owner? ?path?\"" |
---|
435 | } |
---|
436 | } |
---|
437 | } |
---|
438 | remove { |
---|
439 | if {[llength $args] < 1} { |
---|
440 | error "wrong # args: should be \"notify remove owner ?path ...?\"" |
---|
441 | } |
---|
442 | set owner [lindex $args 0] |
---|
443 | |
---|
444 | if {[llength $args] == 1} { |
---|
445 | # no args? then delete all paths for this owner |
---|
446 | if {[info exists _owner2paths($owner)]} { |
---|
447 | set plist $_owner2paths($owner) |
---|
448 | } else { |
---|
449 | set plist "" |
---|
450 | } |
---|
451 | } else { |
---|
452 | set plist [lrange $args 1 end] |
---|
453 | } |
---|
454 | |
---|
455 | # forget about the callback for each path |
---|
456 | foreach path $plist { |
---|
457 | catch {unset _callbacks($owner/$path)} |
---|
458 | |
---|
459 | if {[info exists _owner2paths($owner)]} { |
---|
460 | set i [lsearch -exact $_owner2paths($owner) $path] |
---|
461 | if {$i >= 0} { |
---|
462 | set _owner2paths($owner) \ |
---|
463 | [lreplace $_owner2paths($owner) $i $i] |
---|
464 | } |
---|
465 | } |
---|
466 | } |
---|
467 | } |
---|
468 | } |
---|
469 | } |
---|
470 | |
---|
471 | # ---------------------------------------------------------------------- |
---|
472 | # USAGE: sync |
---|
473 | # |
---|
474 | # Used by descendents such as a Controls panel to register the |
---|
475 | # various controls associated with this page. That way, this |
---|
476 | # ControlOwner knows what widgets to look at when syncing itself |
---|
477 | # to the underlying XML data. |
---|
478 | # ---------------------------------------------------------------------- |
---|
479 | itcl::body Rappture::ControlOwner::sync {} { |
---|
480 | # sync all of the widgets under control of this owner |
---|
481 | if {"" != $_xmlobj} { |
---|
482 | foreach path [lsort [array names _path2widget]] { |
---|
483 | set type [$_xmlobj element -as type $path] |
---|
484 | if {[lsearch {group separator control note} $type] >= 0} { |
---|
485 | continue |
---|
486 | } |
---|
487 | $_xmlobj put $path.current [$_path2widget($path) value] |
---|
488 | } |
---|
489 | } |
---|
490 | |
---|
491 | # sync all subordinate slaves as well |
---|
492 | foreach slave $_slaves { |
---|
493 | $slave sync |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | # ---------------------------------------------------------------------- |
---|
498 | # USAGE: tool |
---|
499 | # |
---|
500 | # Clients use this to figure out which tool is associated with |
---|
501 | # this object. If there is no parent ControlOwner, then this |
---|
502 | # must be the top-level tool. |
---|
503 | # ---------------------------------------------------------------------- |
---|
504 | itcl::body Rappture::ControlOwner::tool {} { |
---|
505 | if {"" != $_owner} { |
---|
506 | return [$_owner tool] |
---|
507 | } |
---|
508 | return $this |
---|
509 | } |
---|
510 | |
---|
511 | # ---------------------------------------------------------------------- |
---|
512 | # USAGE: _slave add <newobj>... |
---|
513 | # |
---|
514 | # Used internally to register the parent-child relationship whenever |
---|
515 | # one ControlOwner is registered to another. When the parent syncs, |
---|
516 | # it causes all of its children to sync. When a name is being |
---|
517 | # resolved, it is resolved locally first, then up to the parent for |
---|
518 | # further resolution. |
---|
519 | # ---------------------------------------------------------------------- |
---|
520 | itcl::body Rappture::ControlOwner::_slave {option args} { |
---|
521 | switch -- $option { |
---|
522 | add { |
---|
523 | eval lappend _slaves $args |
---|
524 | } |
---|
525 | default { |
---|
526 | error "bad option \"$option\": should be add" |
---|
527 | } |
---|
528 | } |
---|
529 | } |
---|