source: trunk/gui/scripts/choiceentry.tcl @ 95

Last change on this file since 95 was 22, checked in by mmc, 19 years ago

Lots of changes to support Huckel-IV:

  • Support for embedded <tool> declarations
  • New <integer> entry
  • Support for numbers and structures as output elements
  • Atom numbers/symbols in MoleculeViewer?
File size: 9.0 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: ChoiceEntry - widget for entering a choice of strings
3#
4#  This widget represents a <choice> entry on a control panel.
5#  It is used to choose one of several mutually-exclusive strings.
6# ======================================================================
7#  AUTHOR:  Michael McLennan, Purdue University
8#  Copyright (c) 2004-2005
9#  Purdue Research Foundation, West Lafayette, IN
10# ======================================================================
11package require Itk
12
13itcl::class Rappture::ChoiceEntry {
14    inherit itk::Widget
15
16    constructor {owner path args} { # defined below }
17    destructor { # defined below }
18
19    public method value {args}
20
21    public method label {}
22    public method tooltip {}
23
24    protected method _rebuild {}
25    protected method _newValue {}
26    protected method _tooltip {}
27
28    private variable _owner ""    ;# thing managing this control
29    private variable _path ""     ;# path in XML to this number
30}
31
32itk::usual ChoiceEntry {
33    keep -cursor -font
34    keep -foreground -background
35    keep -textforeground -textbackground
36    keep -selectbackground -selectforeground -selectborderwidth
37}
38
39# ----------------------------------------------------------------------
40# CONSTRUCTOR
41# ----------------------------------------------------------------------
42itcl::body Rappture::ChoiceEntry::constructor {owner path args} {
43    if {[catch {$owner isa Rappture::ControlOwner} valid] != 0 || !$valid} {
44        error "bad object \"$owner\": should be Rappture::ControlOwner"
45    }
46    set _owner $owner
47    set _path $path
48
49    #
50    # Create the widget and configure it properly based on other
51    # hints in the XML.
52    #
53    itk_component add choice {
54        Rappture::Combobox $itk_interior.choice -editable no
55    }
56    pack $itk_component(choice) -expand yes -fill both
57    bind $itk_component(choice) <<Value>> [itcl::code $this _newValue]
58
59    eval itk_initialize $args
60
61    _rebuild
62}
63
64# ----------------------------------------------------------------------
65# DESTRUCTOR
66# ----------------------------------------------------------------------
67itcl::body Rappture::ChoiceEntry::destructor {} {
68    $_owner notify remove $this
69}
70
71# ----------------------------------------------------------------------
72# USAGE: value ?-check? ?<newval>?
73#
74# Clients use this to query/set the value for this widget.  With
75# no args, it returns the current value for the widget.  If the
76# <newval> is specified, it sets the value of the widget and
77# sends a <<Value>> event.  If the -check flag is included, the
78# new value is not actually applied, but just checked for correctness.
79# ----------------------------------------------------------------------
80itcl::body Rappture::ChoiceEntry::value {args} {
81    set onlycheck 0
82    set i [lsearch -exact $args -check]
83    if {$i >= 0} {
84        set onlycheck 1
85        set args [lreplace $args $i $i]
86    }
87
88    if {[llength $args] == 1} {
89        if {$onlycheck} {
90            # someday we may add validation...
91            return
92        }
93        set newval [lindex $args 0]
94        $itk_component(choice) value $newval
95        return $newval
96
97    } elseif {[llength $args] != 0} {
98        error "wrong # args: should be \"value ?-check? ?newval?\""
99    }
100
101    #
102    # Query the value and return.
103    #
104    return [$itk_component(choice) value]
105}
106
107# ----------------------------------------------------------------------
108# USAGE: label
109#
110# Clients use this to query the label associated with this widget.
111# Reaches into the XML and pulls out the appropriate label string.
112# ----------------------------------------------------------------------
113itcl::body Rappture::ChoiceEntry::label {} {
114    set label [$_owner xml get $_path.about.label]
115    if {"" == $label} {
116        set label "Number"
117    }
118    return $label
119}
120
121# ----------------------------------------------------------------------
122# USAGE: tooltip
123#
124# Clients use this to query the tooltip associated with this widget.
125# Reaches into the XML and pulls out the appropriate description
126# string.  Returns the string that should be used with the
127# Rappture::Tooltip facility.
128# ----------------------------------------------------------------------
129itcl::body Rappture::ChoiceEntry::tooltip {} {
130    # query tooltip on-demand based on current choice
131    return "@[itcl::code $this _tooltip]"
132}
133
134# ----------------------------------------------------------------------
135# USAGE: _rebuild
136#
137# Used internally to rebuild the contents of this choice widget
138# whenever something that it depends on changes.  Scans through the
139# information in the XML spec and builds a list of choices for the
140# widget.
141# ----------------------------------------------------------------------
142itcl::body Rappture::ChoiceEntry::_rebuild {} {
143    # get rid of any existing choices
144    $itk_component(choice) choices delete 0 end
145
146    #
147    # Plug in the various options for the choice.
148    #
149    set max 10
150    foreach cname [$_owner xml children -type option $_path] {
151        set path [string trim [$_owner xml get $_path.$cname.path]]
152        if {"" != $path} {
153            # look for the input element controlling this path
154            set found 0
155            foreach cntl [Rappture::entities [$_owner xml object] "input"] {
156                set len [string length $cntl]
157                if {[string equal -length $len $cntl $path]} {
158                    set found 1
159                    break
160                }
161            }
162            if {$found} {
163                #
164                # Choice comes from a list of matching entities at
165                # a particular XML path.  Use the <label> as a template
166                # for each item on the path.
167                #
168                $_owner notify add $this $cntl [itcl::code $this _rebuild]
169
170                set label [string trim [$_owner xml get $_path.$cname.about.label]]
171                if {"" == $label} {
172                    set label "%type #%n"
173                }
174
175                set ppath [Rappture::LibraryObj::path2list $path]
176                set leading [join [lrange $ppath 0 end-1] .]
177                set tail [lindex $ppath end]
178                set n 1
179                foreach ccname [$_owner xml children $leading] {
180                    if {[string match $tail $ccname]} {
181                        set subst(%n) $n
182                        set subst(%type) [$_owner xml element -as type $leading.$ccname]
183                        set subst(%id) [$_owner xml element -as id $leading.$ccname]
184                        foreach detail [$_owner xml children $leading.$ccname] {
185                            set subst(%$detail) [$_owner xml get $leading.$ccname.$detail]
186                        }
187                        set str [string map [array get subst] $label]
188                        $itk_component(choice) choices insert end \
189                            $leading.$ccname $str
190                        incr n
191                    }
192                }
193                $itk_component(choice) value ""
194            } else {
195                puts "can't find controlling entity for path \"$path\""
196            }
197        } else {
198            #
199            # Choice is an ordinary LABEL.
200            # Add the label as-is into the list of choices.
201            #
202            set str [string trim [$_owner xml get $_path.$cname.about.label]]
203            if {"" != $str} {
204                $itk_component(choice) choices insert end $_path.$cname $str
205                set len [string length $str]
206                if {$len > $max} { set max $len }
207            }
208        }
209    }
210    $itk_component(choice) configure -width $max
211
212    #
213    # Assign the default value to this widget, if there is one.
214    #
215    set str [$_owner xml get $_path.default]
216    if {"" != $str} { $itk_component(choice) value $str }
217}
218
219# ----------------------------------------------------------------------
220# USAGE: _newValue
221#
222# Invoked automatically whenever the value in the choice changes.
223# Sends a <<Value>> event to notify clients of the change.
224# ----------------------------------------------------------------------
225itcl::body Rappture::ChoiceEntry::_newValue {} {
226    event generate $itk_component(hull) <<Value>>
227}
228
229# ----------------------------------------------------------------------
230# USAGE: _tooltip
231#
232# Returns the tooltip for this widget, given the current choice in
233# the selector.  This is normally called by the Rappture::Tooltip
234# facility whenever it is about to pop up a tooltip for this widget.
235# ----------------------------------------------------------------------
236itcl::body Rappture::ChoiceEntry::_tooltip {} {
237    set tip [string trim [$_owner xml get $_path.about.description]]
238
239    # get the description for the current choice, if there is one
240    set str [$itk_component(choice) value]
241    set path [$itk_component(choice) translate $str]
242
243    if {"" != $str} {
244        append tip "\n\n$str:"
245
246        if {$path != ""} {
247            set desc [$_owner xml get $path.description]
248            if {[string length $desc] > 0} {
249                append tip "\n$desc"
250            }
251        }
252    }
253    return $tip
254}
Note: See TracBrowser for help on using the repository browser.