3 # ----------------------------------------------------------------------
4 # Implements a radiobuttonbox. Supports adding, inserting, deleting,
5 # selecting, and deselecting of radiobuttons by tag and index.
7 # ----------------------------------------------------------------------
8 # AUTHOR: Michael J. McLennan EMAIL: mmclennan@lucent.com
9 # Mark L. Ulferts EMAIL: mulferts@austin.dsccc.com
12 # ----------------------------------------------------------------------
13 # Copyright (c) 1995 DSC Technologies Corporation
14 # ======================================================================
15 # Permission to use, copy, modify, distribute and license this software
16 # and its documentation for any purpose, and without fee or written
17 # agreement with DSC, is hereby granted, provided that the above copyright
18 # notice appears in all copies and that both the copyright notice and
19 # warranty disclaimer below appear in supporting documentation, and that
20 # the names of DSC Technologies Corporation or DSC Communications
21 # Corporation not be used in advertising or publicity pertaining to the
22 # software without specific, written prior permission.
24 # DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25 # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-
26 # INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE
27 # AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE,
28 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL
29 # DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
30 # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
31 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,
32 # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
34 # ======================================================================
40 keep -background -borderwidth -cursor -disabledforeground \
41 -foreground -labelfont -selectcolor
44 # ------------------------------------------------------------------
46 # ------------------------------------------------------------------
47 class iwidgets::Radiobox {
48 inherit iwidgets::Labeledframe
52 itk_option define -disabledforeground \
53 disabledForeground DisabledForeground {}
54 itk_option define -selectcolor selectColor Background {}
55 itk_option define -command command Command {}
56 itk_option define -orient orient Orient vertical
60 method buttonconfigure {index args}
62 method deselect {index}
66 method insert {index tag args}
70 protected method _command { name1 name2 opt }
73 method gettag {index} ;# Get the tag of the checkbutton associated
74 ;# with a numeric index
76 method _rearrange {} ;# List of radiobutton tags.
77 variable _buttons {} ;# List of radiobutton tags.
78 common _modes ;# Current selection.
79 variable _unique 0 ;# Unique id for choice creation.
84 # Provide a lowercased access method for the Radiobox class.
86 proc ::iwidgets::radiobox {pathName args} {
87 uplevel ::iwidgets::Radiobox $pathName $args
91 # Use option database to override default resources of base classes.
93 option add *Radiobox.labelMargin 10 widgetDefault
94 option add *Radiobox.labelFont \
95 "-Adobe-Helvetica-Bold-R-Normal--*-120-*-*-*-*-*-*" widgetDefault
96 option add *Radiobox.labelPos nw widgetDefault
97 option add *Radiobox.borderWidth 2 widgetDefault
98 option add *Radiobox.relief groove widgetDefault
100 # ------------------------------------------------------------------
102 # ------------------------------------------------------------------
103 body iwidgets::Radiobox::constructor {args} {
104 trace variable [scope _modes($this)] w [code $this _command]
106 grid columnconfigure $itk_component(childsite) 0 -weight 1
108 eval itk_initialize $args
111 # ------------------------------------------------------------------
113 # ------------------------------------------------------------------
115 # ------------------------------------------------------------------
118 # Specifies a command to be evaluated upon change in the radiobox
119 # ------------------------------------------------------------------
120 configbody iwidgets::Radiobox::command {}
122 # ------------------------------------------------------------------
125 # Allows the user to orient the radiobuttons either horizontally
127 # ------------------------------------------------------------------
128 configbody iwidgets::Radiobox::orient {
129 if {$itk_option(-orient) == "horizontal" ||
130 $itk_option(-orient) == "vertical"} {
133 error "Bad orientation: $itk_option(-orient). Should be\
134 \"horizontal\" or \"vertical\"."
138 # ------------------------------------------------------------------
140 # ------------------------------------------------------------------
142 # ------------------------------------------------------------------
143 # METHOD: index index
145 # Searches the radiobutton tags in the radiobox for the one with the
146 # requested tag, numerical index, or keyword "end". Returns the
147 # choices's numerical index if found, otherwise error.
148 # ------------------------------------------------------------------
149 body iwidgets::Radiobox::index {index} {
150 if {[llength $_buttons] > 0} {
151 if {[regexp {(^[0-9]+$)} $index]} {
152 if {$index < [llength $_buttons]} {
155 error "Radiobox index \"$index\" is out of range"
158 } elseif {$index == "end"} {
159 return [expr [llength $_buttons] - 1]
162 if {[set idx [lsearch $_buttons $index]] != -1} {
166 error "bad Radiobox index \"$index\": must be number, end,\
171 error "Radiobox \"$itk_component(hull)\" has no radiobuttons"
175 # ------------------------------------------------------------------
176 # METHOD: add tag ?option value option value ...?
178 # Add a new tagged radiobutton to the radiobox at the end. The method
179 # takes additional options which are passed on to the radiobutton
180 # constructor. These include most of the typical radiobutton
181 # options. The tag is returned.
182 # ------------------------------------------------------------------
183 body iwidgets::Radiobox::add {tag args} {
184 itk_component add $tag {
185 eval radiobutton $itk_component(childsite).rb[incr _unique] \
186 -variable [list [scope _modes($this)]] \
189 -highlightthickness 0 \
193 ignore -highlightthickness -highlightcolor
194 rename -font -labelfont labelFont Font
196 lappend _buttons $tag
197 grid $itk_component($tag)
198 after idle [code $this _rearrange]
203 # ------------------------------------------------------------------
204 # METHOD: insert index tag ?option value option value ...?
206 # Insert the tagged radiobutton in the radiobox just before the
207 # one given by index. Any additional options are passed on to the
208 # radiobutton constructor. These include the typical radiobutton
209 # options. The tag is returned.
210 # ------------------------------------------------------------------
211 body iwidgets::Radiobox::insert {index tag args} {
212 itk_component add $tag {
213 eval radiobutton $itk_component(childsite).rb[incr _unique] \
214 -variable [list [scope _modes($this)]] \
215 -highlightthickness 0 \
221 ignore -highlightthickness -highlightcolor
222 rename -font -labelfont labelFont Font
224 set index [index $index]
225 set before [lindex $_buttons $index]
226 set _buttons [linsert $_buttons $index $tag]
227 grid $itk_component($tag)
228 after idle [code $this _rearrange]
233 # ------------------------------------------------------------------
236 # Rearrange the buttons in the childsite frame using the grid
237 # geometry manager. This method was modified by Chad Smith on 3/9/00
238 # to take into consideration the newly added -orient config option.
239 # ------------------------------------------------------------------
240 body iwidgets::Radiobox::_rearrange {} {
241 if {[set count [llength $_buttons]] > 0} {
242 if {$itk_option(-orient) == "vertical"} {
244 foreach tag $_buttons {
245 grid configure $itk_component($tag) -col 0 -row $row -sticky nw
246 grid rowconfigure $itk_component(childsite) $row -weight 0
249 grid rowconfigure $itk_component(childsite) [expr $count-1] \
253 foreach tag $_buttons {
254 grid configure $itk_component($tag) -col $col -row 0 -sticky nw
255 grid columnconfigure $itk_component(childsite) $col -weight 1
262 # ------------------------------------------------------------------
263 # METHOD: delete index
265 # Delete the specified radiobutton.
266 # ------------------------------------------------------------------
267 body iwidgets::Radiobox::delete {index} {
269 set tag [gettag $index]
270 set index [index $index]
272 destroy $itk_component($tag)
274 set _buttons [lreplace $_buttons $index $index]
276 if {$_modes($this) == $tag} {
279 after idle [code $this _rearrange]
283 # ------------------------------------------------------------------
284 # METHOD: select index
286 # Select the specified radiobutton.
287 # ------------------------------------------------------------------
288 body iwidgets::Radiobox::select {index} {
289 set tag [gettag $index]
290 $itk_component($tag) invoke
293 # ------------------------------------------------------------------
296 # Return the tag of the currently selected radiobutton.
297 # ------------------------------------------------------------------
298 body iwidgets::Radiobox::get {} {
299 return $_modes($this)
302 # ------------------------------------------------------------------
303 # METHOD: deselect index
305 # Deselect the specified radiobutton.
306 # ------------------------------------------------------------------
307 body iwidgets::Radiobox::deselect {index} {
308 set tag [gettag $index]
309 $itk_component($tag) deselect
312 # ------------------------------------------------------------------
313 # METHOD: flash index
315 # Flash the specified radiobutton.
316 # ------------------------------------------------------------------
317 body iwidgets::Radiobox::flash {index} {
318 set tag [gettag $index]
319 $itk_component($tag) flash
322 # ------------------------------------------------------------------
323 # METHOD: buttonconfigure index ?option? ?value option value ...?
325 # Configure a specified radiobutton. This method allows configuration
326 # of radiobuttons from the Radiobox level. The options may have any
327 # of the values accepted by the add method.
328 # ------------------------------------------------------------------
329 body iwidgets::Radiobox::buttonconfigure {index args} {
330 set tag [gettag $index]
331 eval $itk_component($tag) configure $args
334 # ------------------------------------------------------------------
335 # CALLBACK METHOD: _command name1 name2 opt
337 # Tied to the trace on _modes($this). Whenever our -variable for our
338 # radiobuttons change, this method is invoked. It in turn calls
339 # the user specified tcl script given by -command.
340 # ------------------------------------------------------------------
341 body iwidgets::Radiobox::_command { name1 name2 opt } {
342 uplevel #0 $itk_option(-command)
345 # ------------------------------------------------------------------
346 # METHOD: gettag index
348 # Return the tag of the checkbutton associated with a specified
350 # ------------------------------------------------------------------
351 body iwidgets::Radiobox::gettag {index} {
352 return [lindex $_buttons [index $index]]