OSDN Git Service

touched all sources to ease next import
[pf3gnuchains/pf3gnuchains3x.git] / itcl / iwidgets3.0.0 / generic / radiobox.itk
1 #
2 # Radiobox
3 # ----------------------------------------------------------------------
4 # Implements a radiobuttonbox.  Supports adding, inserting, deleting,
5 # selecting, and deselecting of radiobuttons by tag and index.
6 #
7 # ----------------------------------------------------------------------
8 #  AUTHOR: Michael J. McLennan          EMAIL: mmclennan@lucent.com
9 #          Mark L. Ulferts              EMAIL: mulferts@austin.dsccc.com
10 #
11 #  @(#) $Id$
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.
23
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 
33 # SOFTWARE.
34 # ======================================================================
35
36 #
37 # Usual options.
38 #
39 itk::usual Radiobox {
40     keep -background -borderwidth -cursor -disabledforeground \
41         -foreground -labelfont -selectcolor
42 }
43
44 # ------------------------------------------------------------------
45 #                            RADIOBOX
46 # ------------------------------------------------------------------
47 class iwidgets::Radiobox {
48     inherit iwidgets::Labeledframe
49
50     constructor {args} {}
51
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
57
58     public {
59       method add {tag args}
60       method buttonconfigure {index args}
61       method delete {index}
62       method deselect {index}
63       method flash {index}
64       method get {}
65       method index {index}
66       method insert {index tag args}
67       method select {index}
68     }
69
70     protected method _command { name1 name2 opt }
71
72     private {
73       method gettag {index}      ;# Get the tag of the checkbutton associated
74                                  ;# with a numeric index
75
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.
80     }
81 }
82
83 #
84 # Provide a lowercased access method for the Radiobox class.
85 #
86 proc ::iwidgets::radiobox {pathName args} {
87     uplevel ::iwidgets::Radiobox $pathName $args
88 }
89
90 #
91 # Use option database to override default resources of base classes.
92 #
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
99
100 # ------------------------------------------------------------------
101 #                        CONSTRUCTOR
102 # ------------------------------------------------------------------
103 body iwidgets::Radiobox::constructor {args} {
104     trace variable [scope _modes($this)] w [code $this _command]
105
106     grid columnconfigure $itk_component(childsite) 0 -weight 1
107
108     eval itk_initialize $args
109 }
110
111 # ------------------------------------------------------------------
112 #                            OPTIONS
113 # ------------------------------------------------------------------
114
115 # ------------------------------------------------------------------
116 # OPTION: -command
117 #
118 # Specifies a command to be evaluated upon change in the radiobox
119 # ------------------------------------------------------------------
120 configbody iwidgets::Radiobox::command {}
121
122 # ------------------------------------------------------------------
123 # OPTION: -orient
124 #
125 # Allows the user to orient the radiobuttons either horizontally
126 # or vertically.
127 # ------------------------------------------------------------------
128 configbody iwidgets::Radiobox::orient {
129   if {$itk_option(-orient) == "horizontal" ||
130       $itk_option(-orient) == "vertical"} {
131     _rearrange
132   } else {
133     error "Bad orientation: $itk_option(-orient).  Should be\
134       \"horizontal\" or \"vertical\"."
135   }
136 }
137
138 # ------------------------------------------------------------------
139 #                            METHODS
140 # ------------------------------------------------------------------
141
142 # ------------------------------------------------------------------
143 # METHOD: index index
144 #
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]} {
153                 return $index
154             } else {
155                 error "Radiobox index \"$index\" is out of range"
156             }
157
158         } elseif {$index == "end"} {
159             return [expr [llength $_buttons] - 1]
160
161         } else {
162             if {[set idx [lsearch $_buttons $index]] != -1} {
163                 return $idx
164             }
165
166             error "bad Radiobox index \"$index\": must be number, end,\
167                     or pattern"
168         }
169
170     } else {
171         error "Radiobox \"$itk_component(hull)\" has no radiobuttons"
172     }
173 }
174
175 # ------------------------------------------------------------------
176 # METHOD: add tag ?option value option value ...?
177 #
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)]] \
187             -anchor w \
188             -justify left \
189             -highlightthickness 0 \
190             -value $tag $args
191     } { 
192       usual
193       ignore -highlightthickness -highlightcolor
194       rename -font -labelfont labelFont Font
195     }
196     lappend _buttons $tag
197     grid $itk_component($tag)
198     after idle [code $this _rearrange]
199
200     return $tag
201 }
202
203 # ------------------------------------------------------------------
204 # METHOD: insert index tag ?option value option value ...?
205 #
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 \
216             -anchor w \
217             -justify left \
218             -value $tag $args
219     } { 
220       usual
221       ignore -highlightthickness -highlightcolor
222       rename -font -labelfont labelFont Font
223     }
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]
229
230     return $tag
231 }
232
233 # ------------------------------------------------------------------
234 # METHOD: _rearrange
235 #
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"} {
243             set row 0
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
247                 incr row
248             }
249             grid rowconfigure $itk_component(childsite) [expr $count-1] \
250               -weight 1
251         } else {
252             set col 0
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
256                 incr col
257             }
258         }
259     }
260 }
261
262 # ------------------------------------------------------------------
263 # METHOD: delete index
264 #
265 # Delete the specified radiobutton.
266 # ------------------------------------------------------------------
267 body iwidgets::Radiobox::delete {index} {
268
269     set tag [gettag $index]
270     set index [index $index]
271
272     destroy $itk_component($tag)
273
274     set _buttons [lreplace $_buttons $index $index]
275
276     if {$_modes($this) == $tag} {
277         set _modes($this) {}
278     }
279     after idle [code $this _rearrange]
280     return
281 }
282
283 # ------------------------------------------------------------------
284 # METHOD: select index
285 #
286 # Select the specified radiobutton.
287 # ------------------------------------------------------------------
288 body iwidgets::Radiobox::select {index} {
289     set tag [gettag $index]
290     $itk_component($tag) invoke
291 }
292
293 # ------------------------------------------------------------------
294 # METHOD: get
295 #
296 # Return the tag of the currently selected radiobutton.
297 # ------------------------------------------------------------------
298 body iwidgets::Radiobox::get {} {
299     return $_modes($this)
300 }
301
302 # ------------------------------------------------------------------
303 # METHOD: deselect index
304 #
305 # Deselect the specified radiobutton.
306 # ------------------------------------------------------------------
307 body iwidgets::Radiobox::deselect {index} {
308     set tag [gettag $index]
309     $itk_component($tag) deselect
310 }
311
312 # ------------------------------------------------------------------
313 # METHOD: flash index
314 #
315 # Flash the specified radiobutton.
316 # ------------------------------------------------------------------
317 body iwidgets::Radiobox::flash {index} {
318     set tag [gettag $index]
319     $itk_component($tag) flash  
320 }
321
322 # ------------------------------------------------------------------
323 # METHOD: buttonconfigure index ?option? ?value option value ...?
324 #
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
332 }
333
334 # ------------------------------------------------------------------
335 # CALLBACK METHOD: _command name1 name2 opt 
336 #
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)
343 }
344
345 # ------------------------------------------------------------------
346 # METHOD: gettag index
347 #
348 # Return the tag of the checkbutton associated with a specified
349 # numeric index
350 # ------------------------------------------------------------------
351 body iwidgets::Radiobox::gettag {index} {
352     return [lindex $_buttons [index $index]]
353 }
354