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 | # ====================================================================== |
---|
11 | package require Itk |
---|
12 | |
---|
13 | itcl::class Rappture::ChoiceEntry { |
---|
14 | inherit itk::Widget |
---|
15 | |
---|
16 | constructor {xmlobj path args} { # defined below } |
---|
17 | |
---|
18 | public method value {args} |
---|
19 | |
---|
20 | public method label {} |
---|
21 | public method tooltip {} |
---|
22 | |
---|
23 | protected method _newValue {} |
---|
24 | protected method _tooltip {} |
---|
25 | |
---|
26 | private variable _xmlobj "" ;# XML containing description |
---|
27 | private variable _path "" ;# path in XML to this number |
---|
28 | } |
---|
29 | |
---|
30 | itk::usual ChoiceEntry { |
---|
31 | keep -cursor -font |
---|
32 | keep -foreground -background |
---|
33 | keep -textforeground -textbackground |
---|
34 | keep -selectbackground -selectforeground -selectborderwidth |
---|
35 | } |
---|
36 | |
---|
37 | # ---------------------------------------------------------------------- |
---|
38 | # CONSTRUCTOR |
---|
39 | # ---------------------------------------------------------------------- |
---|
40 | itcl::body Rappture::ChoiceEntry::constructor {xmlobj path args} { |
---|
41 | if {![Rappture::library isvalid $xmlobj]} { |
---|
42 | error "bad value \"$xmlobj\": should be Rappture::library" |
---|
43 | } |
---|
44 | set _xmlobj $xmlobj |
---|
45 | set _path $path |
---|
46 | |
---|
47 | # |
---|
48 | # Create the widget and configure it properly based on other |
---|
49 | # hints in the XML. |
---|
50 | # |
---|
51 | itk_component add choice { |
---|
52 | Rappture::Combobox $itk_interior.choice -editable no |
---|
53 | } |
---|
54 | pack $itk_component(choice) -expand yes -fill both |
---|
55 | bind $itk_component(choice) <<Value>> [itcl::code $this _newValue] |
---|
56 | |
---|
57 | eval itk_initialize $args |
---|
58 | |
---|
59 | # |
---|
60 | # Plug in the various choices for this widget. |
---|
61 | # |
---|
62 | # plug in the various options for the choice |
---|
63 | set max 10 |
---|
64 | foreach cname [$xmlobj children -type option $path] { |
---|
65 | set str [string trim [$xmlobj get $path.$cname.label]] |
---|
66 | if {"" != $str} { |
---|
67 | $itk_component(choice) choices insert end $path.$cname $str |
---|
68 | set len [string length $str] |
---|
69 | if {$len > $max} { set max $len } |
---|
70 | } |
---|
71 | } |
---|
72 | $itk_component(choice) configure -width $max |
---|
73 | |
---|
74 | # |
---|
75 | # Assign the default value to this widget, if there is one. |
---|
76 | # |
---|
77 | set str [$xmlobj get $path.default] |
---|
78 | if {"" != $str != ""} { $itk_component(choice) value $str } |
---|
79 | } |
---|
80 | |
---|
81 | # ---------------------------------------------------------------------- |
---|
82 | # USAGE: value ?-check? ?<newval>? |
---|
83 | # |
---|
84 | # Clients use this to query/set the value for this widget. With |
---|
85 | # no args, it returns the current value for the widget. If the |
---|
86 | # <newval> is specified, it sets the value of the widget and |
---|
87 | # sends a <<Value>> event. If the -check flag is included, the |
---|
88 | # new value is not actually applied, but just checked for correctness. |
---|
89 | # ---------------------------------------------------------------------- |
---|
90 | itcl::body Rappture::ChoiceEntry::value {args} { |
---|
91 | set onlycheck 0 |
---|
92 | set i [lsearch -exact $args -check] |
---|
93 | if {$i >= 0} { |
---|
94 | set onlycheck 1 |
---|
95 | set args [lreplace $args $i $i] |
---|
96 | } |
---|
97 | |
---|
98 | if {[llength $args] == 1} { |
---|
99 | if {$onlycheck} { |
---|
100 | # someday we may add validation... |
---|
101 | return |
---|
102 | } |
---|
103 | set newval [lindex $args 0] |
---|
104 | $itk_component(choice) value $newval |
---|
105 | return $newval |
---|
106 | |
---|
107 | } elseif {[llength $args] != 0} { |
---|
108 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
109 | } |
---|
110 | |
---|
111 | # |
---|
112 | # Query the value and return. |
---|
113 | # |
---|
114 | return [$itk_component(choice) value] |
---|
115 | } |
---|
116 | |
---|
117 | # ---------------------------------------------------------------------- |
---|
118 | # USAGE: label |
---|
119 | # |
---|
120 | # Clients use this to query the label associated with this widget. |
---|
121 | # Reaches into the XML and pulls out the appropriate label string. |
---|
122 | # ---------------------------------------------------------------------- |
---|
123 | itcl::body Rappture::ChoiceEntry::label {} { |
---|
124 | set label [$_xmlobj get $_path.about.label] |
---|
125 | if {"" == $label} { |
---|
126 | set label "Number" |
---|
127 | } |
---|
128 | return $label |
---|
129 | } |
---|
130 | |
---|
131 | # ---------------------------------------------------------------------- |
---|
132 | # USAGE: tooltip |
---|
133 | # |
---|
134 | # Clients use this to query the tooltip associated with this widget. |
---|
135 | # Reaches into the XML and pulls out the appropriate description |
---|
136 | # string. Returns the string that should be used with the |
---|
137 | # Rappture::Tooltip facility. |
---|
138 | # ---------------------------------------------------------------------- |
---|
139 | itcl::body Rappture::ChoiceEntry::tooltip {} { |
---|
140 | # query tooltip on-demand based on current choice |
---|
141 | return "@[itcl::code $this _tooltip]" |
---|
142 | } |
---|
143 | |
---|
144 | # ---------------------------------------------------------------------- |
---|
145 | # USAGE: _newValue |
---|
146 | # |
---|
147 | # Invoked automatically whenever the value in the choice changes. |
---|
148 | # Sends a <<Value>> event to notify clients of the change. |
---|
149 | # ---------------------------------------------------------------------- |
---|
150 | itcl::body Rappture::ChoiceEntry::_newValue {} { |
---|
151 | event generate $itk_component(hull) <<Value>> |
---|
152 | } |
---|
153 | |
---|
154 | # ---------------------------------------------------------------------- |
---|
155 | # USAGE: _tooltip |
---|
156 | # |
---|
157 | # Returns the tooltip for this widget, given the current choice in |
---|
158 | # the selector. This is normally called by the Rappture::Tooltip |
---|
159 | # facility whenever it is about to pop up a tooltip for this widget. |
---|
160 | # ---------------------------------------------------------------------- |
---|
161 | itcl::body Rappture::ChoiceEntry::_tooltip {} { |
---|
162 | set tip [string trim [$_xmlobj get $_path.about.description]] |
---|
163 | |
---|
164 | # get the description for the current choice, if there is one |
---|
165 | set str [$itk_component(choice) value] |
---|
166 | set path [$itk_component(choice) translate $str] |
---|
167 | |
---|
168 | if {"" != $str} { |
---|
169 | append tip "\n\n$str:" |
---|
170 | |
---|
171 | if {$path != ""} { |
---|
172 | set desc [$_xmlobj get $path.description] |
---|
173 | if {[string length $desc] > 0} { |
---|
174 | append tip "\n$desc" |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | return $tip |
---|
179 | } |
---|