source: trunk/gui/scripts/loader.tcl @ 46

Last change on this file since 46 was 25, checked in by mmc, 19 years ago

Added <min>, <max>, and <scale> directives to the output plotter
for <curve> objects. If you specify any of these within an axis
for a curve, they set the default characteristics for the axis.

File size: 7.9 KB
Line 
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# ======================================================================
11package require Itk
12
13option add *Loader.textForeground black widgetDefault
14option add *Loader.textBackground white widgetDefault
15
16itcl::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
35itk::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# ----------------------------------------------------------------------
45itcl::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        foreach fname [lsort [glob -nocomplain $fpath]] {
89            if {[file exists $fname]} {
90                if {[catch {set obj [Rappture::library $fname]} result]} {
91                    puts stderr "WARNING: can't load example file \"$fname\""
92                    puts stderr "  $result"
93                } else {
94                    set label [$obj get about.label]
95                    if {$label == ""} {
96                        set label "Example #$_counter"
97                    }
98
99                    # if this is new, add it
100                    set i [$itk_component(combo) choices index -label $label]
101                    if {$i < 0} {
102                        $itk_component(combo) choices insert end $obj $label
103
104                        if {[string equal $defval [file tail $fname]]} {
105                            $_owner xml put $path.default $label
106                        }
107                    }
108                }
109            } else {
110                puts stderr "WARNING: missing example file \"$fname\""
111            }
112        }
113    }
114
115    #
116    # Assign the default value to this widget, if there is one.
117    #
118    set str [$_owner xml get $path.default]
119    if {$str != ""} { after 1000 [itcl::code $this value $str] }
120}
121
122# ----------------------------------------------------------------------
123# USAGE: value ?-check? ?<newval>?
124#
125# Clients use this to query/set the value for this widget.  With
126# no args, it returns the current value for the widget.  If the
127# <newval> is specified, it sets the value of the widget and
128# sends a <<Value>> event.  If the -check flag is included, the
129# new value is not actually applied, but just checked for correctness.
130# ----------------------------------------------------------------------
131itcl::body Rappture::Loader::value {args} {
132    set onlycheck 0
133    set i [lsearch -exact $args -check]
134    if {$i >= 0} {
135        set onlycheck 1
136        set args [lreplace $args $i $i]
137    }
138
139    if {[llength $args] == 1} {
140        if {$onlycheck} {
141            # someday we may add validation...
142            return
143        }
144        set newval [lindex $args 0]
145        $itk_component(combo) value $newval
146        return $newval
147
148    } elseif {[llength $args] != 0} {
149        error "wrong # args: should be \"value ?-check? ?newval?\""
150    }
151
152    #
153    # Query the value and return.
154    #
155    return [$itk_component(combo) value]
156}
157
158# ----------------------------------------------------------------------
159# USAGE: label
160#
161# Clients use this to query the label associated with this widget.
162# Reaches into the XML and pulls out the appropriate label string.
163# ----------------------------------------------------------------------
164itcl::body Rappture::Loader::label {} {
165    set label [$_owner xml get $_path.about.label]
166    if {"" == $label} {
167        set label "Example"
168    }
169    return $label
170}
171
172# ----------------------------------------------------------------------
173# USAGE: tooltip
174#
175# Clients use this to query the tooltip associated with this widget.
176# Reaches into the XML and pulls out the appropriate description
177# string.  Returns the string that should be used with the
178# Rappture::Tooltip facility.
179# ----------------------------------------------------------------------
180itcl::body Rappture::Loader::tooltip {} {
181    # query tooltip on-demand based on current choice
182    return "@[itcl::code $this _tooltip]"
183}
184
185# ----------------------------------------------------------------------
186# USAGE: _newValue
187#
188# Invoked automatically whenever the value in the combobox changes.
189# Tries to load the selected example into the tool's data structure.
190# Sends a <<Value>> event to notify clients of the change.
191# ----------------------------------------------------------------------
192itcl::body Rappture::Loader::_newValue {} {
193    set newval [$itk_component(combo) value]
194    set obj [$itk_component(combo) translate $newval]
195    if {$obj != "" && $itk_option(-tool) != ""} {
196        $itk_option(-tool) load $obj
197    }
198
199    event generate $itk_component(hull) <<Value>>
200}
201
202# ----------------------------------------------------------------------
203# USAGE: _tooltip
204#
205# Returns the tooltip for this widget, given the current choice in
206# the selector.  This is normally called by the Rappture::Tooltip
207# facility whenever it is about to pop up a tooltip for this widget.
208# ----------------------------------------------------------------------
209itcl::body Rappture::Loader::_tooltip {} {
210    set str [string trim [$_owner xml get $_path.about.description]]
211
212    # get the description for the current choice, if there is one
213    set newval [$itk_component(combo) value]
214    set obj [$itk_component(combo) translate $newval]
215    if {$obj != ""} {
216        set label [$obj get about.label]
217        if {[string length $label] > 0} {
218            append str "\n\n$label"
219        }
220
221        set desc [$obj get about.description]
222        if {[string length $desc] > 0} {
223            if {[string length $label] > 0} {
224                append str ":\n"
225            } else {
226                append str "\n\n"
227            }
228            append str $desc
229        }
230    }
231    return [string trim $str]
232}
233
234# ----------------------------------------------------------------------
235# OPTION: -tool
236# ----------------------------------------------------------------------
237itcl::configbody Rappture::Loader::tool {
238    if {[catch {$itk_option(-tool) isa Rappture::Tool} valid] || !$valid} {
239        error "object \"$itk_option(-tool)\" is not a Rappture Tool"
240    }
241}
Note: See TracBrowser for help on using the repository browser.