# # Radiobox # ---------------------------------------------------------------------- # Implements a radiobuttonbox. Supports adding, inserting, deleting, # selecting, and deselecting of radiobuttons by tag and index. # # ---------------------------------------------------------------------- # AUTHOR: Michael J. McLennan EMAIL: michael.mclennan@att.com # Mark L. Ulferts EMAIL: mulferts@spd.dsccc.com # # @(#) $Id: radiobox.itk,v 1.3 2006/09/11 20:35:55 irby Exp $ # ---------------------------------------------------------------------- # Copyright (c) 1995 DSC Technologies Corporation # ====================================================================== # Permission to use, copy, modify, distribute and license this software # and its documentation for any purpose, and without fee or written # agreement with DSC, is hereby granted, provided that the above copyright # notice appears in all copies and that both the copyright notice and # warranty disclaimer below appear in supporting documentation, and that # the names of DSC Technologies Corporation or DSC Communications # Corporation not be used in advertising or publicity pertaining to the # software without specific, written prior permission. # # DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON- # INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE # AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL # DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION, # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS # SOFTWARE. # ====================================================================== # # Default resources. # option add *Radiobox.borderWidth 2 widgetDefault option add *Radiobox.relief groove widgetDefault option add *Radiobox.labelPos n widgetDefault # # Usual options. # itk::usual Radiobox { keep -background -borderwidth -cursor -foreground -labelfont } # ------------------------------------------------------------------ # RADIOBOX # ------------------------------------------------------------------ class iwidgets::Radiobox { inherit iwidgets::Labeledwidget constructor {args} {} itk_option define -command command Command {} method index {index} method add {tag args} method insert {index tag args} method delete {index} method select {index} method deselect {index} method get {} method flash {index} method buttonconfigure {index args} method _command { name1 name2 opt } private variable _unique 0 ;# Unique id for choice creation. private variable _buttons {} ;# List of radiobutton tags. private common _modes ;# Current selection. } # # Provide a lowercased access method for the Radiobox class. # proc ::iwidgets::radiobox {pathName args} { uplevel ::iwidgets::Radiobox $pathName $args } # ------------------------------------------------------------------ # CONSTRUCTOR # ------------------------------------------------------------------ body iwidgets::Radiobox::constructor {args} { component hull configure -borderwidth 0 itk_component add border { frame $itk_interior.border } { keep -background -cursor -borderwidth -relief } pack $itk_component(border) -expand yes -fill both trace variable [scope _modes($this)] w [code $this _command] eval itk_initialize $args } # ------------------------------------------------------------------ # OPTIONS # ------------------------------------------------------------------ # ------------------------------------------------------------------ # OPTION: -command # # Specifies a command to be evaluated upon change in the radiobox # ------------------------------------------------------------------ configbody iwidgets::Radiobox::command {} # ------------------------------------------------------------------ # METHODS # ------------------------------------------------------------------ # ------------------------------------------------------------------ # METHOD: index index # # Searches the radiobutton tags in the radiobox for the one with the # requested tag, numerical index, or keyword "end". Returns the # choices's numerical index if found, otherwise error. # ------------------------------------------------------------------ body iwidgets::Radiobox::index {index} { if {[llength $_buttons] > 0} { if {[regexp {(^[0-9]+$)} $index]} { if {$index < [llength $_buttons]} { return $index } else { error "Radiobox index \"$index\" is out of range" } } elseif {$index == "end"} { return [expr [llength $_buttons] - 1] } else { if {[set idx [lsearch $_buttons $index]] != -1} { return $idx } error "bad Radiobox index \"$index\": must be number, end,\ or pattern" } } else { error "Radiobox \"$itk_component(hull)\" has no radiobuttons" } } # ------------------------------------------------------------------ # METHOD: add tag ?option value option value ...? # # Add a new tagged radiobutton to the radiobox at the end. The method # takes additional options which are passed on to the radiobutton # constructor. These include most of the typical radiobutton # options. The tag is returned. # ------------------------------------------------------------------ body iwidgets::Radiobox::add {tag args} { itk_component add $tag { eval radiobutton $itk_component(border).rb[incr _unique] \ -variable [list [scope _modes($this)]] -value $tag $args } { keep -background -foreground -cursor -font -text \ -activebackground -activeforeground -selectcolor \ -indicatoron -selectimage -state -highlightcolor \ -highlightthickness rename -highlightbackground -background background Background } pack $itk_component($tag) -anchor w -padx 4 lappend _buttons $tag return $tag } # ------------------------------------------------------------------ # METHOD: insert index tag ?option value option value ...? # # Insert the tagged radiobutton in the radiobox just before the # one given by index. Any additional options are passed on to the # radiobutton constructor. These include the typical radiobutton # options. The tag is returned. # ------------------------------------------------------------------ body iwidgets::Radiobox::insert {index tag args} { itk_component add $tag { eval radiobutton $itk_component(border).rb[incr _unique] \ -variable [list [scope _modes($this)]] -value $tag $args } { keep -background -foreground -cursor -font -text \ -activebackground -activeforeground -selectcolor \ -indicatoron -selectimage -state -highlightcolor \ -highlightthickness rename -highlightbackground -background background Background } set index [index $index] set before [lindex $_buttons $index] set _buttons [linsert $_buttons $index $tag] pack $itk_component($tag) -anchor w -padx 4 -before $itk_component($before) return $tag } # ------------------------------------------------------------------ # METHOD: delete index # # Delete the specified radiobutton. # ------------------------------------------------------------------ body iwidgets::Radiobox::delete {index} { set index [index $index] set tag [lindex $_buttons $index] destroy $itk_component($tag) set _buttons [lreplace $_buttons $index $index] if {$_modes($this) == $tag} { set _modes($this) {} } return } # ------------------------------------------------------------------ # METHOD: select index # # Select the specified radiobutton. # ------------------------------------------------------------------ body iwidgets::Radiobox::select {index} { set index [index $index] set tag [lindex $_buttons $index] $itk_component($tag) invoke } # ------------------------------------------------------------------ # METHOD: get # # Return the tag of the currently selected radiobutton. # ------------------------------------------------------------------ body iwidgets::Radiobox::get {} { return $_modes($this) } # ------------------------------------------------------------------ # METHOD: deselect index # # Deselect the specified radiobutton. # ------------------------------------------------------------------ body iwidgets::Radiobox::deselect {index} { set index [index $index] set tag [lindex $_buttons $index] $itk_component($tag) deselect } # ------------------------------------------------------------------ # METHOD: flash index # # Flash the specified radiobutton. # ------------------------------------------------------------------ body iwidgets::Radiobox::flash {index} { set index [index $index] set tag [lindex $_buttons $index] $itk_component($tag) flash } # ------------------------------------------------------------------ # METHOD: buttonconfigure index ?option? ?value option value ...? # # Configure a specified radiobutton. This method allows configuration # of radiobuttons from the Radiobox level. The options may have any # of the values accepted by the add method. # ------------------------------------------------------------------ body iwidgets::Radiobox::buttonconfigure {index args} { set index [index $index] set tag [lindex $_buttons $index] eval $itk_component($tag) configure $args } # ------------------------------------------------------------------ # CALLBACK METHOD: _command name1 name2 opt # # Tied to the trace on _modes($this). Whenever our -variable for our # radiobuttons change, this method is invoked. It in turn calls # the user specified tcl script given by -command. # ------------------------------------------------------------------ body iwidgets::Radiobox::_command { name1 name2 opt } { uplevel #0 $itk_option(-command) }