home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / checkbox.itk < prev    next >
Text File  |  2003-09-01  |  12KB  |  342 lines

  1. #
  2. # Checkbox
  3. # ----------------------------------------------------------------------
  4. # Implements a checkbuttonbox.  Supports adding, inserting, deleting,
  5. # selecting, and deselecting of checkbuttons by tag and index.
  6. #
  7. # ----------------------------------------------------------------------
  8. #  AUTHOR: John A. Tucker                EMAIL: jatucker@spd.dsccc.com
  9. #
  10. # ----------------------------------------------------------------------
  11. #            Copyright (c) 1997 DSC Technologies Corporation
  12. # ======================================================================
  13. # Permission to use, copy, modify, distribute and license this software 
  14. # and its documentation for any purpose, and without fee or written 
  15. # agreement with DSC, is hereby granted, provided that the above copyright 
  16. # notice appears in all copies and that both the copyright notice and 
  17. # warranty disclaimer below appear in supporting documentation, and that 
  18. # the names of DSC Technologies Corporation or DSC Communications 
  19. # Corporation not be used in advertising or publicity pertaining to the 
  20. # software without specific, written prior permission.
  21. # DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
  22. # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-
  23. # INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE
  24. # AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, 
  25. # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL 
  26. # DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 
  27. # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
  28. # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,
  29. # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
  30. # SOFTWARE.
  31. # ======================================================================
  32.  
  33.  
  34. #
  35. # Use option database to override default resources of base classes.
  36. #
  37. option add *Checkbox.labelMargin    10    widgetDefault
  38. option add *Checkbox.labelFont     \
  39.       "-Adobe-Helvetica-Bold-R-Normal--*-120-*-*-*-*-*-*"  widgetDefault
  40. option add *Checkbox.labelPos        nw    widgetDefault
  41. option add *Checkbox.borderWidth    2    widgetDefault
  42. option add *Checkbox.relief        groove    widgetDefault
  43.  
  44. #
  45. # Usual options.
  46. #
  47. itk::usual Checkbox {
  48.     keep -background -borderwidth -cursor -foreground -labelfont
  49. }
  50.  
  51. # ------------------------------------------------------------------
  52. #                            CHECKBOX
  53. # ------------------------------------------------------------------
  54. itcl::class iwidgets::Checkbox {
  55.     inherit iwidgets::Labeledframe
  56.  
  57.     constructor {args} {}
  58.  
  59.     itk_option define -orient orient Orient vertical
  60.  
  61.     public {
  62.       method add {tag args}
  63.       method insert {index tag args}
  64.       method delete {index}
  65.       method get {{index ""}}
  66.       method index {index}
  67.       method select {index}
  68.       method deselect {index}
  69.       method flash {index}
  70.       method toggle {index}
  71.       method buttonconfigure {index args}
  72.   }
  73.  
  74.   private {
  75.  
  76.       method gettag {index}      ;# Get the tag of the checkbutton associated
  77.                                  ;# with a numeric index
  78.  
  79.       variable _unique 0         ;# Unique id for choice creation.
  80.       variable _buttons {}       ;# List of checkbutton tags.
  81.       common buttonVar           ;# Array of checkbutton "-variables"
  82.   }
  83. }
  84.  
  85. #
  86. # Provide a lowercased access method for the Checkbox class.
  87. #
  88. proc ::iwidgets::checkbox {pathName args} {
  89.     uplevel ::iwidgets::Checkbox $pathName $args
  90. }
  91.  
  92. # ------------------------------------------------------------------
  93. #                        CONSTRUCTOR
  94. # ------------------------------------------------------------------
  95. itcl::body iwidgets::Checkbox::constructor {args} {
  96.  
  97.     eval itk_initialize $args
  98. }
  99.  
  100. # ------------------------------------------------------------------
  101. #                            OPTIONS
  102. # ------------------------------------------------------------------
  103.  
  104. # ------------------------------------------------------------------
  105. # OPTION: -orient
  106. #
  107. # Allows the user to orient the checkbuttons either horizontally
  108. # or vertically.  Added by Chad Smith (csmith@adc.com) 3/10/00.
  109. # ------------------------------------------------------------------
  110. itcl::configbody iwidgets::Checkbox::orient {
  111.   if {$itk_option(-orient) == "horizontal"} {
  112.     foreach tag $_buttons {
  113.       pack $itk_component($tag) -side left -anchor nw -padx 4 -expand 1
  114.     }
  115.   } elseif {$itk_option(-orient) == "vertical"} {
  116.     foreach tag $_buttons {
  117.       pack $itk_component($tag) -side top -anchor w -padx 4 -expand 0
  118.     }
  119.   } else {
  120.     error "Bad orientation: $itk_option(-orient).  Should be\
  121.       \"horizontal\" or \"vertical\"."
  122.   }
  123. }
  124.  
  125.  
  126. # ------------------------------------------------------------------
  127. #                            METHODS
  128. # ------------------------------------------------------------------
  129.  
  130. # ------------------------------------------------------------------
  131. # METHOD: index index
  132. #
  133. # Searches the checkbutton tags in the checkbox for the one with the
  134. # requested tag, numerical index, or keyword "end".  Returns the 
  135. # choices's numerical index if found, otherwise error.
  136. # ------------------------------------------------------------------
  137. itcl::body iwidgets::Checkbox::index {index} {
  138.     if {[llength $_buttons] > 0} {
  139.         if {[regexp {(^[0-9]+$)} $index]} {
  140.             if {$index < [llength $_buttons]} {
  141.                 return $index
  142.             } else {
  143.                 error "Checkbox index \"$index\" is out of range"
  144.             }
  145.  
  146.         } elseif {$index == "end"} {
  147.             return [expr {[llength $_buttons] - 1}]
  148.  
  149.         } else {
  150.             if {[set idx [lsearch $_buttons $index]] != -1} {
  151.                 return $idx
  152.             }
  153.  
  154.             error "bad Checkbox index \"$index\": must be number, end,\
  155.                     or pattern"
  156.         }
  157.  
  158.     } else {
  159.         error "Checkbox \"$itk_component(hull)\" has no checkbuttons"
  160.     }
  161. }
  162.  
  163. # ------------------------------------------------------------------
  164. # METHOD: add tag ?option value option value ...?
  165. #
  166. # Add a new tagged checkbutton to the checkbox at the end.  The method 
  167. # takes additional options which are passed on to the checkbutton
  168. # constructor.  These include most of the typical checkbutton 
  169. # options.  The tag is returned.
  170. # ------------------------------------------------------------------
  171. itcl::body iwidgets::Checkbox::add {tag args} {
  172.     itk_component add $tag {
  173.         eval checkbutton $itk_component(childsite).cb[incr _unique] \
  174.             -variable [list [itcl::scope buttonVar($this,$tag)]] \
  175.             -anchor w \
  176.             -justify left \
  177.             -highlightthickness 0 \
  178.             $args
  179.     } { 
  180.       usual
  181.       keep -command -disabledforeground -selectcolor -state
  182.       ignore -highlightthickness -highlightcolor
  183.       rename -font -labelfont labelFont Font
  184.     }
  185.  
  186.     # Redraw the buttons with the proper orientation.
  187.     if {$itk_option(-orient) == "vertical"} {
  188.       pack $itk_component($tag) -side top -anchor w -padx 4 -expand 0
  189.     } else {
  190.       pack $itk_component($tag) -side left -anchor nw -expand 1
  191.     }
  192.  
  193.     lappend _buttons $tag
  194.  
  195.     return $tag
  196. }
  197.  
  198. # ------------------------------------------------------------------
  199. # METHOD: insert index tag ?option value option value ...?
  200. #
  201. # Insert the tagged checkbutton in the checkbox just before the 
  202. # one given by index.  Any additional options are passed on to the
  203. # checkbutton constructor.  These include the typical checkbutton
  204. # options.  The tag is returned.
  205. # ------------------------------------------------------------------
  206. itcl::body iwidgets::Checkbox::insert {index tag args} {
  207.     itk_component add $tag {
  208.         eval checkbutton $itk_component(childsite).cb[incr _unique] \
  209.             -variable [list [itcl::scope buttonVar($this,$tag)]] \
  210.             -anchor w \
  211.             -justify left \
  212.             -highlightthickness 0 \
  213.             $args
  214.     }  { 
  215.       usual
  216.       ignore -highlightthickness -highlightcolor
  217.       rename -font -labelfont labelFont Font
  218.     }
  219.  
  220.     set index [index $index]
  221.     set before [lindex $_buttons $index]
  222.     set _buttons [linsert $_buttons $index $tag]
  223.  
  224.     pack $itk_component($tag) -anchor w -padx 4 -before $itk_component($before)
  225.  
  226.     return $tag
  227. }
  228.  
  229. # ------------------------------------------------------------------
  230. # METHOD: delete index
  231. #
  232. # Delete the specified checkbutton.
  233. # ------------------------------------------------------------------
  234. itcl::body iwidgets::Checkbox::delete {index} {
  235.  
  236.     set tag [gettag $index]
  237.     set index [index $index]
  238.     destroy $itk_component($tag)
  239.     set _buttons [lreplace $_buttons $index $index]
  240.  
  241.     if { [info exists buttonVar($this,$tag)] == 1 } {
  242.     unset buttonVar($this,$tag)
  243.     }
  244. }
  245.  
  246. # ------------------------------------------------------------------
  247. # METHOD: select index
  248. #
  249. # Select the specified checkbutton.
  250. # ------------------------------------------------------------------
  251. itcl::body iwidgets::Checkbox::select {index} {
  252.     set tag [gettag $index]
  253.     #-----------------------------------------------------------
  254.     # BUG FIX: csmith (Chad Smith: csmith@adc.com), 3/30/99
  255.     #-----------------------------------------------------------
  256.     # This method should only invoke the checkbutton if it's not
  257.     # already selected.  Check its associated variable, and if
  258.     # it's set, then just ignore and return.
  259.     #-----------------------------------------------------------
  260.     if {[set [itcl::scope buttonVar($this,$tag)]] == 
  261.     [[component $tag] cget -onvalue]} {
  262.       return
  263.     }
  264.     $itk_component($tag) invoke
  265. }
  266.  
  267. # ------------------------------------------------------------------
  268. # METHOD: toggle index
  269. #
  270. # Toggle a specified checkbutton between selected and unselected
  271. # ------------------------------------------------------------------
  272. itcl::body iwidgets::Checkbox::toggle {index} {
  273.     set tag [gettag $index]
  274.     $itk_component($tag) toggle
  275. }
  276.  
  277. # ------------------------------------------------------------------
  278. # METHOD: get
  279. #
  280. # Return the value of the checkbutton with the given index, or a
  281. # list of all checkbutton values in increasing order by index.
  282. # ------------------------------------------------------------------
  283. itcl::body iwidgets::Checkbox::get {{index ""}} {
  284.     set result {}
  285.  
  286.     if {$index == ""} {
  287.     foreach tag $_buttons {
  288.         if {$buttonVar($this,$tag)} {
  289.         lappend result $tag
  290.         }
  291.     }
  292.     } else {
  293.         set tag [gettag $index]
  294.     set result $buttonVar($this,$tag)
  295.     }
  296.  
  297.     return $result
  298. }
  299.  
  300. # ------------------------------------------------------------------
  301. # METHOD: deselect index
  302. #
  303. # Deselect the specified checkbutton.
  304. # ------------------------------------------------------------------
  305. itcl::body iwidgets::Checkbox::deselect {index} {
  306.     set tag [gettag $index]
  307.     $itk_component($tag) deselect
  308. }
  309.  
  310. # ------------------------------------------------------------------
  311. # METHOD: flash index
  312. #
  313. # Flash the specified checkbutton.
  314. # ------------------------------------------------------------------
  315. itcl::body iwidgets::Checkbox::flash {index} {
  316.     set tag [gettag $index]
  317.     $itk_component($tag) flash  
  318. }
  319.  
  320. # ------------------------------------------------------------------
  321. # METHOD: buttonconfigure index ?option? ?value option value ...?
  322. #
  323. # Configure a specified checkbutton.  This method allows configuration 
  324. # of checkbuttons from the Checkbox level.  The options may have any 
  325. # of the values accepted by the add method.
  326. # ------------------------------------------------------------------
  327. itcl::body iwidgets::Checkbox::buttonconfigure {index args} { 
  328.     set tag [gettag $index]
  329.     eval $itk_component($tag) configure $args
  330. }
  331.  
  332. # ------------------------------------------------------------------
  333. # METHOD: gettag index
  334. #
  335. # Return the tag of the checkbutton associated with a specified
  336. # numeric index
  337. # ------------------------------------------------------------------
  338. itcl::body iwidgets::Checkbox::gettag {index} {
  339.     return [lindex $_buttons [index $index]]
  340. }
  341.