1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: loader - widget for loading examples and old runs |
---|
3 | # |
---|
4 | # This widget is a glorified combobox that is used to load various |
---|
5 | # example files into the application. |
---|
6 | # ====================================================================== |
---|
7 | # AUTHOR: Michael McLennan, Purdue University |
---|
8 | # Copyright (c) 2004-2005 |
---|
9 | # Purdue Research Foundation, West Lafayette, IN |
---|
10 | # ====================================================================== |
---|
11 | package require Itk |
---|
12 | |
---|
13 | option add *Loader.textForeground black widgetDefault |
---|
14 | option add *Loader.textBackground white widgetDefault |
---|
15 | |
---|
16 | itcl::class Rappture::Loader { |
---|
17 | inherit itk::Widget |
---|
18 | |
---|
19 | itk_option define -tool tool Tool "" |
---|
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 | protected method _tooltip {} |
---|
30 | |
---|
31 | private variable _owner "" ;# thing managing this control |
---|
32 | private variable _path "" ;# path in XML to this loader |
---|
33 | } |
---|
34 | |
---|
35 | itk::usual Loader { |
---|
36 | keep -cursor -font |
---|
37 | keep -foreground -background |
---|
38 | keep -textforeground -textbackground |
---|
39 | keep -selectbackground -selectforeground -selectborderwidth |
---|
40 | } |
---|
41 | |
---|
42 | # ---------------------------------------------------------------------- |
---|
43 | # CONSTRUCTOR |
---|
44 | # ---------------------------------------------------------------------- |
---|
45 | itcl::body Rappture::Loader::constructor {owner path args} { |
---|
46 | if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} { |
---|
47 | error "bad object \"$owner\": should be Rappture::ControlOwner" |
---|
48 | } |
---|
49 | set _owner $owner |
---|
50 | set _path $path |
---|
51 | |
---|
52 | itk_component add combo { |
---|
53 | Rappture::Combobox $itk_interior.combo -editable no |
---|
54 | } { |
---|
55 | usual |
---|
56 | keep -width |
---|
57 | } |
---|
58 | pack $itk_component(combo) -expand yes -fill both |
---|
59 | bind $itk_component(combo) <<Value>> [itcl::code $this _newValue] |
---|
60 | |
---|
61 | eval itk_initialize $args |
---|
62 | |
---|
63 | # |
---|
64 | # Scan through and extract example objects, and load them into |
---|
65 | # the combobox. |
---|
66 | # |
---|
67 | set defval [$_owner xml get $path.default] |
---|
68 | |
---|
69 | set flist "" |
---|
70 | foreach comp [$_owner xml children -type example $path] { |
---|
71 | lappend flist [$_owner xml get $path.$comp] |
---|
72 | } |
---|
73 | |
---|
74 | # if there are no examples, then look for *.xml |
---|
75 | if {[llength $flist] == 0} { |
---|
76 | set flist *.xml |
---|
77 | } |
---|
78 | |
---|
79 | if {$itk_option(-tool) != ""} { |
---|
80 | set fdir [$itk_option(-tool) installdir] |
---|
81 | } else { |
---|
82 | set fdir "." |
---|
83 | } |
---|
84 | |
---|
85 | set _counter 1 |
---|
86 | foreach ftail $flist { |
---|
87 | set fpath [file join $fdir examples $ftail] |
---|
88 | |
---|
89 | catch {unset entries} |
---|
90 | foreach fname [glob -nocomplain $fpath] { |
---|
91 | if {[file exists $fname]} { |
---|
92 | if {[catch {set obj [Rappture::library $fname]} result]} { |
---|
93 | puts stderr "WARNING: can't load example file \"$fname\"" |
---|
94 | puts stderr " $result" |
---|
95 | } else { |
---|
96 | set label [$obj get about.label] |
---|
97 | if {$label == ""} { |
---|
98 | set label "Example #$_counter" |
---|
99 | } |
---|
100 | |
---|
101 | # if this is new, add it |
---|
102 | if {![info exists entries($label)]} { |
---|
103 | set entries($label) $obj |
---|
104 | } |
---|
105 | |
---|
106 | # translate default file name => default label |
---|
107 | if {[string equal $defval [file tail $fname]]} { |
---|
108 | $_owner xml put $path.default $label |
---|
109 | } |
---|
110 | } |
---|
111 | } else { |
---|
112 | puts stderr "WARNING: missing example file \"$fname\"" |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | foreach label [lsort -dictionary [array names entries]] { |
---|
117 | $itk_component(combo) choices insert end $entries($label) $label |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | # |
---|
122 | # Assign the default value to this widget, if there is one. |
---|
123 | # |
---|
124 | set str [$_owner xml get $path.default] |
---|
125 | if {$str != ""} { after 1000 [itcl::code $this value $str] } |
---|
126 | } |
---|
127 | |
---|
128 | # ---------------------------------------------------------------------- |
---|
129 | # USAGE: value ?-check? ?<newval>? |
---|
130 | # |
---|
131 | # Clients use this to query/set the value for this widget. With |
---|
132 | # no args, it returns the current value for the widget. If the |
---|
133 | # <newval> is specified, it sets the value of the widget and |
---|
134 | # sends a <<Value>> event. If the -check flag is included, the |
---|
135 | # new value is not actually applied, but just checked for correctness. |
---|
136 | # ---------------------------------------------------------------------- |
---|
137 | itcl::body Rappture::Loader::value {args} { |
---|
138 | set onlycheck 0 |
---|
139 | set i [lsearch -exact $args -check] |
---|
140 | if {$i >= 0} { |
---|
141 | set onlycheck 1 |
---|
142 | set args [lreplace $args $i $i] |
---|
143 | } |
---|
144 | |
---|
145 | if {[llength $args] == 1} { |
---|
146 | if {$onlycheck} { |
---|
147 | # someday we may add validation... |
---|
148 | return |
---|
149 | } |
---|
150 | set newval [lindex $args 0] |
---|
151 | $itk_component(combo) value $newval |
---|
152 | return $newval |
---|
153 | |
---|
154 | } elseif {[llength $args] != 0} { |
---|
155 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
156 | } |
---|
157 | |
---|
158 | # |
---|
159 | # Query the value and return. |
---|
160 | # |
---|
161 | return [$itk_component(combo) value] |
---|
162 | } |
---|
163 | |
---|
164 | # ---------------------------------------------------------------------- |
---|
165 | # USAGE: label |
---|
166 | # |
---|
167 | # Clients use this to query the label associated with this widget. |
---|
168 | # Reaches into the XML and pulls out the appropriate label string. |
---|
169 | # ---------------------------------------------------------------------- |
---|
170 | itcl::body Rappture::Loader::label {} { |
---|
171 | set label [$_owner xml get $_path.about.label] |
---|
172 | if {"" == $label} { |
---|
173 | set label "Example" |
---|
174 | } |
---|
175 | return $label |
---|
176 | } |
---|
177 | |
---|
178 | # ---------------------------------------------------------------------- |
---|
179 | # USAGE: tooltip |
---|
180 | # |
---|
181 | # Clients use this to query the tooltip associated with this widget. |
---|
182 | # Reaches into the XML and pulls out the appropriate description |
---|
183 | # string. Returns the string that should be used with the |
---|
184 | # Rappture::Tooltip facility. |
---|
185 | # ---------------------------------------------------------------------- |
---|
186 | itcl::body Rappture::Loader::tooltip {} { |
---|
187 | # query tooltip on-demand based on current choice |
---|
188 | return "@[itcl::code $this _tooltip]" |
---|
189 | } |
---|
190 | |
---|
191 | # ---------------------------------------------------------------------- |
---|
192 | # USAGE: _newValue |
---|
193 | # |
---|
194 | # Invoked automatically whenever the value in the combobox changes. |
---|
195 | # Tries to load the selected example into the tool's data structure. |
---|
196 | # Sends a <<Value>> event to notify clients of the change. |
---|
197 | # ---------------------------------------------------------------------- |
---|
198 | itcl::body Rappture::Loader::_newValue {} { |
---|
199 | set newval [$itk_component(combo) value] |
---|
200 | set obj [$itk_component(combo) translate $newval] |
---|
201 | if {$obj != "" && $itk_option(-tool) != ""} { |
---|
202 | $itk_option(-tool) load $obj |
---|
203 | } |
---|
204 | |
---|
205 | event generate $itk_component(hull) <<Value>> |
---|
206 | } |
---|
207 | |
---|
208 | # ---------------------------------------------------------------------- |
---|
209 | # USAGE: _tooltip |
---|
210 | # |
---|
211 | # Returns the tooltip for this widget, given the current choice in |
---|
212 | # the selector. This is normally called by the Rappture::Tooltip |
---|
213 | # facility whenever it is about to pop up a tooltip for this widget. |
---|
214 | # ---------------------------------------------------------------------- |
---|
215 | itcl::body Rappture::Loader::_tooltip {} { |
---|
216 | set str [string trim [$_owner xml get $_path.about.description]] |
---|
217 | |
---|
218 | # get the description for the current choice, if there is one |
---|
219 | set newval [$itk_component(combo) value] |
---|
220 | set obj [$itk_component(combo) translate $newval] |
---|
221 | if {$obj != ""} { |
---|
222 | set label [$obj get about.label] |
---|
223 | if {[string length $label] > 0} { |
---|
224 | append str "\n\n$label" |
---|
225 | } |
---|
226 | |
---|
227 | set desc [$obj get about.description] |
---|
228 | if {[string length $desc] > 0} { |
---|
229 | if {[string length $label] > 0} { |
---|
230 | append str ":\n" |
---|
231 | } else { |
---|
232 | append str "\n\n" |
---|
233 | } |
---|
234 | append str $desc |
---|
235 | } |
---|
236 | } |
---|
237 | return [string trim $str] |
---|
238 | } |
---|
239 | |
---|
240 | # ---------------------------------------------------------------------- |
---|
241 | # OPTION: -tool |
---|
242 | # ---------------------------------------------------------------------- |
---|
243 | itcl::configbody Rappture::Loader::tool { |
---|
244 | if {[catch {$itk_option(-tool) isa Rappture::Tool} valid] || !$valid} { |
---|
245 | error "object \"$itk_option(-tool)\" is not a Rappture Tool" |
---|
246 | } |
---|
247 | } |
---|