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 / toolbar.itk < prev    next >
Text File  |  2003-09-01  |  32KB  |  984 lines

  1. #
  2. # Toolbar
  3. # ----------------------------------------------------------------------
  4. #
  5. # The Toolbar command creates a new window (given by the pathName 
  6. # argument) and makes it into a Tool Bar widget. Additional options, 
  7. # described above may be specified on the command line or in the 
  8. # option database to configure aspects of the Toolbar such as its 
  9. # colors, font, and orientation. The Toolbar command returns its 
  10. # pathName argument. At the time this command is invoked, there 
  11. # must not exist a window named pathName, but pathName's parent 
  12. # must exist.
  13. # A Toolbar is a widget that displays a collection of widgets arranged 
  14. # either in a row or a column (depending on the value of the -orient 
  15. # option). This collection of widgets is usually for user convenience 
  16. # to give access to a set of commands or settings. Any widget may be 
  17. # placed on a Toolbar. However, command or value-oriented widgets (such 
  18. # as button, radiobutton, etc.) are usually the most useful kind of 
  19. # widgets to appear on a Toolbar.
  20. #
  21. # WISH LIST: 
  22. #   This section lists possible future enhancements.  
  23. #
  24. #    Toggle between text and image/bitmap so that the toolbar could
  25. #     display either all text or all image/bitmaps.
  26. #   Implementation of the -toolbarfile option that allows toolbar
  27. #     add commands to be read in from a file.
  28. # ----------------------------------------------------------------------
  29. #  AUTHOR: Bill W. Scott                 EMAIL: bscott@spd.dsccc.com
  30. #
  31. #  @(#) $Id: toolbar.itk,v 1.5 2001/08/17 19:05:54 smithc Exp $
  32. # ----------------------------------------------------------------------
  33. #            Copyright (c) 1995 DSC Technologies Corporation
  34. # ======================================================================
  35. # Permission to use, copy, modify, distribute and license this software 
  36. # and its documentation for any purpose, and without fee or written 
  37. # agreement with DSC, is hereby granted, provided that the above copyright 
  38. # notice appears in all copies and that both the copyright notice and 
  39. # warranty disclaimer below appear in supporting documentation, and that 
  40. # the names of DSC Technologies Corporation or DSC Communications 
  41. # Corporation not be used in advertising or publicity pertaining to the 
  42. # software without specific, written prior permission.
  43. # DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
  44. # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-
  45. # INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE
  46. # AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, 
  47. # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL 
  48. # DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 
  49. # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
  50. # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,
  51. # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
  52. # SOFTWARE.
  53. # ======================================================================
  54.  
  55. #
  56. # Default resources.
  57. #
  58. option add *Toolbar*padX 5 widgetDefault
  59. option add *Toolbar*padY 5 widgetDefault
  60. option add *Toolbar*orient horizontal widgetDefault
  61. option add *Toolbar*highlightThickness 0 widgetDefault
  62. option add *Toolbar*indicatorOn false widgetDefault
  63. option add *Toolbar*selectColor [. cget -bg] widgetDefault
  64.  
  65. #
  66. # Usual options.
  67. #
  68. itk::usual Toolbar {
  69.     keep -activebackground -activeforeground -background -balloonbackground \
  70.      -balloondelay1 -balloondelay2 -balloonfont -balloonforeground \
  71.      -borderwidth -cursor -disabledforeground -font -foreground \
  72.      -highlightbackground -highlightcolor -highlightthickness \
  73.      -insertbackground -insertforeground -selectbackground \
  74.      -selectborderwidth -selectcolor -selectforeground -troughcolor
  75. }
  76.  
  77. # ------------------------------------------------------------------
  78. #                            TOOLBAR
  79. # ------------------------------------------------------------------
  80. itcl::class iwidgets::Toolbar {
  81.     inherit itk::Widget
  82.     
  83.     constructor {args} {}
  84.     destructor {}
  85.     
  86.     itk_option define -balloonbackground \
  87.         balloonBackground BalloonBackground yellow 
  88.     itk_option define -balloonforeground \
  89.         balloonForeground BalloonForeground black 
  90.     itk_option define -balloonfont balloonFont BalloonFont 6x10 
  91.     itk_option define -balloondelay1 \
  92.         balloonDelay1 BalloonDelay1 1000
  93.     itk_option define -balloondelay2 \
  94.         balloonDelay2 BalloonDelay2 200
  95.     itk_option define -helpvariable helpVariable HelpVariable {} 
  96.     itk_option define -orient orient Orient "horizontal" 
  97.     
  98.     #
  99.     # The following options implement propogated configurations to
  100.     # any widget that might be added to us. The problem is this is
  101.     # not deterministic as someone might add a new kind of widget with
  102.     # and option like -armbackground, so we would not be aware of
  103.     # this kind of option. Anyway we support as many of the obvious
  104.     # ones that we can. They can always configure them with itemconfigures.
  105.     #
  106.     itk_option define -activebackground activeBackground Foreground #c3c3c3
  107.     itk_option define -activeforeground activeForeground Background Black 
  108.     itk_option define -background background Background #d9d9d9 
  109.     itk_option define -borderwidth borderWidth BorderWidth 2 
  110.     itk_option define -cursor cursor Cursor {}
  111.     itk_option define -disabledforeground \
  112.         disabledForeground DisabledForeground #a3a3a3 
  113.     itk_option define -font \
  114.         font Font "-Adobe-Helvetica-Bold-R-Normal--*-120-*-*-*-*-*-*" 
  115.     itk_option define -foreground foreground Foreground #000000000000 
  116.     itk_option define -highlightbackground \
  117.         highlightBackground HighlightBackground #d9d9d9 
  118.     itk_option define -highlightcolor highlightColor HighlightColor Black 
  119.     itk_option define -highlightthickness \
  120.         highlightThickness HighlightThickness 0 
  121.     itk_option define -insertforeground insertForeground Background #c3c3c3 
  122.     itk_option define -insertbackground insertBackground Foreground Black 
  123.     itk_option define -selectbackground selectBackground Foreground #c3c3c3 
  124.     itk_option define -selectborderwidth selectBorderWidth BorderWidth {} 
  125.     itk_option define -selectcolor selectColor Background #b03060 
  126.     itk_option define -selectforeground selectForeground Background Black 
  127.     itk_option define -state state State normal 
  128.     itk_option define -troughcolor troughColor Background #c3c3c3 
  129.     
  130.     public method add {widgetCommand name args} 
  131.     public method delete {args} 
  132.     public method index {index} 
  133.     public method insert {beforeIndex widgetCommand name args} 
  134.     public method itemcget {index args} 
  135.     public method itemconfigure {index args} 
  136.     
  137.     public method _resetBalloonTimer {}
  138.     public method _startBalloonDelay {window}
  139.     public method _stopBalloonDelay {window balloonClick}
  140.  
  141.     private method _deleteWidgets {index1 index2} 
  142.     private method _addWidget {widgetCommand name args}
  143.     private method _index {toolList index} 
  144.     private method _getAttachedOption {optionListName widget args retValue} 
  145.     private method _setAttachedOption {optionListName widget option args} 
  146.     private method _packToolbar {} 
  147.     
  148.     public method hideHelp {} 
  149.     public method showHelp {window} 
  150.     public method showBalloon {window} 
  151.     public method hideBalloon {} 
  152.     
  153.     private variable _balloonTimer 0
  154.     private variable _balloonAfterID 0
  155.     private variable _balloonClick false
  156.     
  157.     private variable _interior {}
  158.     private variable _initialMapping 1   ;# Is this the first mapping?
  159.     private variable _toolList {}        ;# List of all widgets on toolbar
  160.     private variable _opts               ;# New options for child widgets
  161.     private variable _currHelpWidget {}  ;# Widget currently displaying help for
  162.     private variable _hintWindow {}      ;# Balloon help bubble.
  163.     
  164.     # list of options we want to propogate to widgets added to toolbar.
  165.     private common _optionList {
  166.     -activebackground \
  167.         -activeforeground \
  168.         -background \
  169.         -borderwidth \
  170.         -cursor \
  171.         -disabledforeground \
  172.         -font \
  173.         -foreground \
  174.         -highlightbackground \
  175.         -highlightcolor \
  176.         -highlightthickness \
  177.         -insertbackground \
  178.         -insertforeground \
  179.         -selectbackground \
  180.         -selectborderwidth \
  181.         -selectcolor \
  182.         -selectforeground \
  183.         -state \
  184.         -troughcolor \
  185.         }
  186. }
  187.  
  188. # ------------------------------------------------------------------
  189. #                            CONSTRUCTOR 
  190. # ------------------------------------------------------------------
  191. itcl::body iwidgets::Toolbar::constructor {args} {
  192.     component hull configure -borderwidth 0
  193.     set _interior $itk_interior
  194.  
  195.     #
  196.     # Handle configs
  197.     #
  198.     eval itk_initialize $args
  199.  
  200.     # build balloon help window
  201.     set _hintWindow [toplevel $itk_component(hull).balloonHintWindow]
  202.     wm withdraw $_hintWindow
  203.     label $_hintWindow.label \
  204.     -foreground $itk_option(-balloonforeground) \
  205.     -background $itk_option(-balloonbackground) \
  206.     -font $itk_option(-balloonfont) \
  207.     -relief raised \
  208.     -borderwidth 1
  209.     pack $_hintWindow.label
  210.     
  211.     # ... Attach help handler to this widget
  212.     bind toolbar-help-$itk_component(hull) \
  213.         <Enter> "+[itcl::code $this showHelp %W]"
  214.     bind toolbar-help-$itk_component(hull) \
  215.         <Leave> "+[itcl::code $this hideHelp]"
  216.     
  217.     # ... Set up Microsoft style balloon help display.
  218.     set _balloonTimer $itk_option(-balloondelay1)
  219.     bind $_interior \
  220.         <Leave> "+[itcl::code $this _resetBalloonTimer]"
  221.     bind toolbar-balloon-$itk_component(hull) \
  222.         <Enter> "+[itcl::code $this _startBalloonDelay %W]"
  223.     bind toolbar-balloon-$itk_component(hull) \
  224.         <Leave> "+[itcl::code $this _stopBalloonDelay %W false]"
  225.     bind toolbar-balloon-$itk_component(hull) \
  226.         <Button-1> "+[itcl::code $this _stopBalloonDelay %W true]"
  227. }
  228.  
  229. #
  230. # Provide a lowercase access method for the Toolbar class
  231. #
  232. proc ::iwidgets::toolbar {pathName args} {
  233.     uplevel ::iwidgets::Toolbar $pathName $args
  234. }
  235.  
  236. # ------------------------------------------------------------------
  237. #                           DESTURCTOR
  238. # ------------------------------------------------------------------
  239. itcl::body iwidgets::Toolbar::destructor {} {
  240.     if {$_balloonAfterID != 0} {after cancel $_balloonAfterID}
  241. }
  242.  
  243. # ------------------------------------------------------------------
  244. #                            OPTIONS
  245. # ------------------------------------------------------------------
  246.  
  247. # ------------------------------------------------------------------
  248. # OPTION -balloonbackground
  249. # ------------------------------------------------------------------
  250. itcl::configbody iwidgets::Toolbar::balloonbackground {
  251.     if { $_hintWindow != {} } {
  252.     if { $itk_option(-balloonbackground) != {} } {
  253.         $_hintWindow.label configure \
  254.         -background $itk_option(-balloonbackground)
  255.     }
  256.     }
  257. }
  258.  
  259. # ------------------------------------------------------------------
  260. # OPTION -balloonforeground
  261. # ------------------------------------------------------------------
  262. itcl::configbody iwidgets::Toolbar::balloonforeground {
  263.     if { $_hintWindow != {} } {
  264.     if { $itk_option(-balloonforeground) != {} } {
  265.         $_hintWindow.label configure \
  266.         -foreground $itk_option(-balloonforeground)
  267.     }
  268.     }
  269. }
  270.  
  271. # ------------------------------------------------------------------
  272. # OPTION -balloonfont
  273. # ------------------------------------------------------------------
  274. itcl::configbody iwidgets::Toolbar::balloonfont {
  275.     if { $_hintWindow != {} } {
  276.     if { $itk_option(-balloonfont) != {} } {
  277.         $_hintWindow.label configure \
  278.         -font $itk_option(-balloonfont) 
  279.     }
  280.     }
  281. }
  282.  
  283. # ------------------------------------------------------------------
  284. # OPTION: -orient
  285. #
  286. # Position buttons either horizontally or vertically.
  287. # ------------------------------------------------------------------
  288. itcl::configbody iwidgets::Toolbar::orient {
  289.     switch $itk_option(-orient) {
  290.     "horizontal" - "vertical" {
  291.         _packToolbar
  292.     }
  293.     default {error "Invalid orientation. Must be either \
  294.         horizontal or vertical"
  295.         }
  296.     }
  297. }
  298.  
  299. # ------------------------------------------------------------------
  300. #                            METHODS
  301. # ------------------------------------------------------------------
  302.     
  303. # -------------------------------------------------------------
  304. # METHOD: add widgetCommand name ?option value?
  305. #
  306. # Adds a widget with the command widgetCommand whose name is 
  307. # name to the Toolbar.   If widgetCommand is radiobutton 
  308. # or checkbutton, its packing is slightly padded to match the 
  309. # geometry of button widgets.
  310. # -------------------------------------------------------------
  311. itcl::body iwidgets::Toolbar::add { widgetCommand name args } {
  312.     
  313.     eval "_addWidget $widgetCommand $name $args"
  314.     
  315.     lappend _toolList $itk_component($name)
  316.     
  317.     if { $widgetCommand == "radiobutton" || \
  318.         $widgetCommand == "checkbutton" } {
  319.     set iPad 1
  320.     } else {
  321.     set iPad 0
  322.     }
  323.     
  324.     # repack the tool bar
  325.     _packToolbar
  326.     
  327.     return $itk_component($name)
  328.     
  329. }
  330.  
  331. # -------------------------------------------------------------
  332. #
  333. # METHOD: delete index ?index2?
  334. #
  335. # This command deletes all components between index and 
  336. # index2 inclusive. If index2 is omitted then it defaults 
  337. # to index. Returns an empty string
  338. #
  339. # -------------------------------------------------------------
  340. itcl::body iwidgets::Toolbar::delete { args } {
  341.     # empty toolbar
  342.     if { $_toolList == {} } {
  343.     error "can't delete widget, no widgets in the Toolbar \
  344.         \"$itk_component(hull)\""
  345.     }
  346.     
  347.     set len [llength $args]
  348.     switch -- $len {
  349.     1 {
  350.         set fromWidget [_index $_toolList [lindex $args 0]]
  351.         
  352.         if { $fromWidget < 0 || $fromWidget >= [llength $_toolList] } {
  353.         error "bad Toolbar widget index in delete method: \
  354.           should be between 0 and [expr {[llength $_toolList] - 1} ]"
  355.         }
  356.         
  357.         set toWidget $fromWidget
  358.         _deleteWidgets $fromWidget $toWidget
  359.     }
  360.     
  361.     2 {
  362.         set fromWidget [_index $_toolList [lindex $args 0]]
  363.         
  364.         if { $fromWidget < 0 || $fromWidget >= [llength $_toolList] } {
  365.         error "bad Toolbar widget index1 in delete method: \
  366.           should be between 0 and [expr {[llength $_toolList] - 1} ]"
  367.         }
  368.         
  369.         set toWidget [_index $_toolList [lindex $args 1]]
  370.         
  371.         if { $toWidget < 0 || $toWidget >= [llength $_toolList] } {
  372.         error "bad Toolbar widget index2 in delete method: \
  373.           should be between 0 and [expr {[llength $_toolList] - 1} ]"
  374.         }
  375.         
  376.         if { $fromWidget > $toWidget } {
  377.         error "bad Toolbar widget index1 in delete method: \
  378.             index1 is greater than index2"
  379.         }
  380.         
  381.         _deleteWidgets $fromWidget $toWidget
  382.     }
  383.     
  384.     default {
  385.         # ... too few/many parameters passed
  386.         error "wrong # args: should be \
  387.             \"$itk_component(hull) delete index1 ?index2?\""
  388.     }
  389.     }
  390.     
  391.     return {}
  392. }
  393.  
  394.  
  395. # -------------------------------------------------------------
  396. #
  397. # METHOD: index index 
  398. #
  399. # Returns the widget's numerical index for the entry corresponding 
  400. # to index. If index is not found, -1 is returned
  401. #
  402. # -------------------------------------------------------------
  403. itcl::body iwidgets::Toolbar::index { index } {
  404.     
  405.     return [_index $_toolList $index]
  406.     
  407. }
  408.  
  409. # -------------------------------------------------------------
  410. #
  411. # METHOD: insert beforeIndex widgetCommand name ?option value?
  412. #
  413. # Insert a new component named name with the command 
  414. # widgetCommand before the com ponent specified by beforeIndex. 
  415. # If widgetCommand is radiobutton or checkbutton, its packing 
  416. # is slightly padded to match the geometry of button widgets.
  417. #
  418. # -------------------------------------------------------------
  419. itcl::body iwidgets::Toolbar::insert {  beforeIndex widgetCommand name args } {
  420.     
  421.     set beforeIndex [_index $_toolList $beforeIndex]
  422.     
  423.     if {$beforeIndex < 0 || $beforeIndex > [llength $_toolList] } {
  424.     error "bad toolbar entry index $beforeIndex"
  425.     }
  426.     
  427.     eval "_addWidget $widgetCommand $name $args"
  428.     
  429.     # linsert into list
  430.     set _toolList [linsert $_toolList $beforeIndex $itk_component($name)]
  431.     
  432.     # repack the tool bar
  433.     _packToolbar
  434.     
  435.     return $itk_component($name)
  436.     
  437. }
  438.  
  439. # ----------------------------------------------------------------------
  440. # METHOD: itemcget index ?option? 
  441. #
  442. # Returns the value for the option setting of the widget at index $index.
  443. # index can be numeric or widget name
  444. #
  445. # ----------------------------------------------------------------------
  446. itcl::body iwidgets::Toolbar::itemcget { index args} {
  447.     
  448.     return [lindex [eval itemconfigure $index $args] 4]
  449. }
  450.  
  451. # -------------------------------------------------------------
  452. #
  453. # METHOD: itemconfigure index ?option? ?value? ?option value...?
  454. #
  455. # Query or modify the configuration options of the widget of 
  456. # the Toolbar specified by index. If no option is specified, 
  457. # returns a list describing all of the available options for 
  458. # index (see Tk_ConfigureInfo for information on the format 
  459. # of this list). If option is specified with no value, then 
  460. # the command returns a list describing the one named option 
  461. # (this list will be identical to the corresponding sublist 
  462. # of the value returned if no option is specified). If one 
  463. # or more option-value pairs are specified, then the command 
  464. # modifies the given widget option(s) to have the given 
  465. # value(s); in this case the command returns an empty string. 
  466. # The component type of index determines the valid available options.
  467. #
  468. # -------------------------------------------------------------
  469. itcl::body iwidgets::Toolbar::itemconfigure { index args } {
  470.     
  471.     # Get a numeric index.
  472.     set index [_index $_toolList $index]
  473.     
  474.     # Get the tool path
  475.     set toolPath [lindex $_toolList $index]
  476.     
  477.     set len [llength $args]
  478.     
  479.     switch $len {
  480.     0 {
  481.         # show all options
  482.         # ''''''''''''''''
  483.         
  484.         # support display of -helpstr and -balloonstr configs
  485.         set optList [$toolPath configure]
  486.         
  487.         ## @@@ might want to use _getAttachedOption instead...
  488.         if { [info exists _opts($toolPath,-helpstr)] } {
  489.         set value $_opts($toolPath,-helpstr)
  490.         } else {
  491.         set value {}
  492.         }
  493.         lappend optList [list -helpstr helpStr HelpStr {} $value]
  494.         if { [info exists _opts($toolPath,-balloonstr)] } {
  495.         set value $_opts($toolPath,-balloonstr)
  496.         } else {
  497.         set value {}
  498.         }
  499.         lappend optList [list -balloonstr balloonStr BalloonStr {} $value]
  500.         return $optList
  501.     }
  502.     1 {
  503.         # show only option specified
  504.         # ''''''''''''''''''''''''''
  505.         # did we satisfy the option get request?
  506.         
  507.         if { [regexp -- {-helpstr} $args] } {
  508.         if { [info exists _opts($toolPath,-helpstr)] } {
  509.             set value $_opts($toolPath,-helpstr)
  510.         } else {
  511.             set value {}
  512.         }
  513.         return [list -helpstr helpStr HelpStr {} $value]
  514.         } elseif { [regexp -- {-balloonstr} $args] } {
  515.         if { [info exists _opts($toolPath,-balloonstr)] } {
  516.             set value $_opts($toolPath,-balloonstr)
  517.         } else {
  518.             set value {}
  519.         }
  520.         return [list -balloonstr balloonStr BalloonStr {} $value]
  521.         } else {
  522.         return [eval $toolPath configure $args]
  523.         }
  524.         
  525.     }
  526.     default {
  527.         # ... do a normal configure
  528.         
  529.         # first screen for all our child options we are adding
  530.         _setAttachedOption \
  531.             _opts \
  532.             $toolPath \
  533.             "-helpstr" \
  534.             $args
  535.         
  536.         _setAttachedOption \
  537.             _opts \
  538.             $toolPath \
  539.             "-balloonstr" \
  540.             $args 
  541.         
  542.         # with a clean args list do a configure
  543.         
  544.         # if the stripping process brought us down to no options
  545.         # to set, then forget the configure of widget.
  546.         if { [llength $args] != 0 } {
  547.         return [eval $toolPath configure $args]
  548.         } else {
  549.         return ""
  550.         }
  551.     }
  552.     }
  553.     
  554. }
  555.  
  556. # -------------------------------------------------------------
  557. #
  558. # METHOD: _resetBalloonDelay1 
  559. #
  560. # Sets the delay that will occur before a balloon could be popped
  561. # up to balloonDelay1
  562. #
  563. # -------------------------------------------------------------
  564. itcl::body iwidgets::Toolbar::_resetBalloonTimer {} {
  565.     set _balloonTimer $itk_option(-balloondelay1)
  566.     
  567.     # reset the <1> longer delay
  568.     set _balloonClick false
  569. }
  570.  
  571. # -------------------------------------------------------------
  572. #
  573. # METHOD: _startBalloonDelay 
  574. #
  575. # Starts waiting to pop up a balloon id
  576. #
  577. # -------------------------------------------------------------
  578. itcl::body iwidgets::Toolbar::_startBalloonDelay {window} {
  579.     if {$_balloonAfterID != 0} {
  580.     after cancel $_balloonAfterID
  581.     }
  582.     set _balloonAfterID [after $_balloonTimer [itcl::code $this showBalloon $window]]
  583. }
  584.  
  585. # -------------------------------------------------------------
  586. #
  587. # METHOD: _stopBalloonDelay  
  588. #
  589. # This method will stop the timer for a balloon popup if one is
  590. # in progress. If however there is already a balloon window up
  591. # it will hide the balloon window and set timing to delay 2 stage.
  592. #
  593. # -------------------------------------------------------------
  594. itcl::body iwidgets::Toolbar::_stopBalloonDelay { window balloonClick } {
  595.     
  596.     # If <1> then got a click cancel
  597.     if { $balloonClick } {
  598.     set _balloonClick true
  599.     }
  600.     if { $_balloonAfterID != 0 } {
  601.     after cancel $_balloonAfterID
  602.     set _balloonAfterID 0
  603.     } else {
  604.     hideBalloon
  605.     
  606.     # If this was cancelled with a <1> use longer delay.
  607.     if { $_balloonClick } {
  608.         set _balloonTimer $itk_option(-balloondelay1)
  609.     } else {
  610.         set _balloonTimer $itk_option(-balloondelay2)
  611.     }
  612.     }
  613. }
  614.  
  615. # -------------------------------------------------------------
  616. # PRIVATE METHOD: _addWidget
  617. #
  618. # widgetCommand : command to invoke to create the added widget
  619. # name          : name of the new widget to add
  620. # args          : options for the widget create command
  621. #
  622. # Looks for -helpstr, -balloonstr and grabs them, strips from
  623. # args list. Then tries to add a component and keeps based
  624. # on known type. If it fails, it tries to clean up. Then it
  625. # binds handlers for helpstatus and balloon help.
  626. #
  627. # Returns the path of the widget added.
  628. #
  629. # -------------------------------------------------------------
  630. itcl::body iwidgets::Toolbar::_addWidget { widgetCommand name args } {
  631.     
  632.     # ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  633.     # Add the widget to the tool bar
  634.     # '''''''''''''''''''''''''''''''''''''''''''''''''''''
  635.     
  636.     # ... Strip out and save the -helpstr, -balloonstr options from args
  637.     #     and save it in _opts
  638.     _setAttachedOption \
  639.         _opts \
  640.         $_interior.$name \
  641.         -helpstr \
  642.         $args 
  643.     
  644.     _setAttachedOption \
  645.         _opts \
  646.         $_interior.$name \
  647.         -balloonstr \
  648.         $args
  649.     
  650.     
  651.     # ... Add the new widget as a component (catch an error if occurs)
  652.     set createFailed [catch { 
  653.     itk_component add $name {
  654.         eval $widgetCommand $_interior.$name $args
  655.     } {
  656.     }
  657.     } errMsg]
  658.     
  659.     # ... Clean up if the create failed, and exit.
  660.     #     The _opts list if it has -helpstr, -balloonstr just entered for
  661.     #     this, it must be cleaned up.
  662.     if { $createFailed } {
  663.     # clean up
  664.     if {![catch {set _opts($_interior.$name,-helpstr)}]} {
  665.         set lastIndex [\
  666.             expr {[llength \
  667.             $_opts($_interior.$name,-helpstr) ]-1}]
  668.         lreplace $_opts($_interior.$name,-helpstr) \
  669.             $lastIndex $lastIndex ""
  670.     }
  671.     if {![catch {set _opts($_interior.$name,-balloonstr)}]} {
  672.         set lastIndex [\
  673.             expr {[llength \
  674.             $_opts($_interior.$name,-balloonstr) ]-1}]
  675.         lreplace $_opts($_interior.$name,-balloonstr) \
  676.             $lastIndex $lastIndex ""
  677.     }
  678.     error $errMsg
  679.     }
  680.     
  681.     # ... Add in dynamic options that apply from the _optionList
  682.     foreach optionSet [$itk_component($name) configure] {
  683.     set option [lindex $optionSet 0]
  684.     if { [lsearch $_optionList $option] != -1 } {
  685.         itk_option add $name.$option
  686.     }
  687.     }
  688.     
  689.     bindtags $itk_component($name) \
  690.         [linsert [bindtags $itk_component($name)] end \
  691.         toolbar-help-$itk_component(hull)]
  692.     bindtags $itk_component($name) \
  693.         [linsert [bindtags $itk_component($name)] end \
  694.         toolbar-balloon-$itk_component(hull)]
  695.     
  696.     return $itk_component($name)
  697. }
  698.  
  699. # -------------------------------------------------------------
  700. #
  701. # PRIVATE METHOD: _deleteWidgets
  702. #
  703. # deletes widget range by numerical index numbers.
  704. #
  705. # -------------------------------------------------------------
  706. itcl::body iwidgets::Toolbar::_deleteWidgets { index1 index2 } {
  707.     
  708.     for { set index $index1 } { $index <= $index2 } { incr index } {
  709.     
  710.     # kill the widget
  711.     set component [lindex $_toolList $index]
  712.     destroy $component
  713.     
  714.     }
  715.     
  716.     # physically remove the page
  717.     set _toolList [lreplace $_toolList $index1 $index2]
  718.     
  719. }
  720.  
  721. # -------------------------------------------------------------
  722. # PRIVATE METHOD: _index
  723. #
  724. # toolList : list of widget names to search thru if index 
  725. #            is non-numeric
  726. # index    : either number, 'end', 'last', or pattern
  727. #
  728. # _index takes takes the value $index converts it to
  729. # a numeric identifier. If the value is not already
  730. # an integer it looks it up in the $toolList array.
  731. # If it fails it returns -1
  732. #
  733. # -------------------------------------------------------------
  734. itcl::body iwidgets::Toolbar::_index { toolList index } {
  735.     
  736.     switch -- $index {
  737.     end - last {
  738.         set number [expr {[llength $toolList] -1}]
  739.     }
  740.     default {
  741.         # is it a number already? Then just use the number
  742.         if { [regexp {^[0-9]+$} $index] } {
  743.         set number $index
  744.         # check bounds
  745.         if { $number < 0 || $number >= [llength $toolList] } {
  746.             set number -1
  747.         }
  748.         # otherwise it is a widget name
  749.         } else {
  750.         if { [catch { set itk_component($index) } ] } {
  751.             set number -1
  752.         } else {
  753.             set number [lsearch -exact $toolList \
  754.                 $itk_component($index)]
  755.         }
  756.         }
  757.     }
  758.     }
  759.     
  760.     return $number
  761. }
  762.     
  763. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  764. # STATUS HELP for linking to helpVariable
  765. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  766. # -------------------------------------------------------------
  767. # PUBLIC METHOD: hideHelp
  768. #
  769. # Bound to the <Leave> event on a toolbar widget. This clears the
  770. # status widget help area and resets the help entry.
  771. #
  772. # -------------------------------------------------------------
  773. itcl::body iwidgets::Toolbar::hideHelp {} {
  774.     if { $itk_option(-helpvariable) != {} } {
  775.         upvar #0 $itk_option(-helpvariable) helpvar
  776.     set helpvar {}
  777.     }
  778.     set _currHelpWidget {}
  779. }
  780.  
  781. # -------------------------------------------------------------
  782. # PUBLIC METHOD: showHelp
  783. #
  784. # Bound to the <Motion> event on a tool bar widget. This puts the
  785. # help string associated with the tool bar widget into the 
  786. # status widget help area. If no help exists for the current
  787. # entry, the status widget is cleared.
  788. #
  789. # -------------------------------------------------------------
  790. itcl::body iwidgets::Toolbar::showHelp { window } {
  791.     
  792.     set widgetPath $window
  793.     # already on this item?
  794.     if { $window == $_currHelpWidget } {
  795.     return
  796.     }
  797.     
  798.     set _currHelpWidget $window
  799.     
  800.     # Do we have a helpvariable set on the toolbar?
  801.     if { $itk_option(-helpvariable) != {} } {
  802.         upvar #0 $itk_option(-helpvariable) helpvar
  803.     
  804.     # is the -helpstr set for this widget?
  805.     set args "-helpstr"
  806.     if {[_getAttachedOption _opts \
  807.         $window args value]} {
  808.         set helpvar $value.
  809.     } else {
  810.         set helpvar {}
  811.     }
  812.     }
  813. }
  814.  
  815. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  816. # BALLOON HELP for show/hide of hint window
  817. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  818. # -------------------------------------------------------------
  819. # PUBLIC METHOD: showBalloon
  820. #
  821. # -------------------------------------------------------------
  822. itcl::body iwidgets::Toolbar::showBalloon {window} {
  823.     set _balloonClick false
  824.     set _balloonAfterID 0
  825.     # Are we still inside the window?
  826.     set mouseWindow \
  827.         [winfo containing [winfo pointerx .] [winfo pointery .]]
  828.  
  829.     if { [string match $window* $mouseWindow] } {
  830.     # set up the balloonString
  831.     set args "-balloonstr"
  832.     if {[_getAttachedOption _opts \
  833.         $window args hintStr]} {
  834.         # configure the balloon help
  835.         $_hintWindow.label configure -text $hintStr        
  836.         
  837.         # Coordinates of the balloon
  838.         set balloonLeft \
  839.             [expr {[winfo rootx $window] + round(([winfo width $window]/2.0))}]
  840.         set balloonTop \
  841.             [expr {[winfo rooty $window] + [winfo height $window]}]
  842.         
  843.         # put up balloon window
  844.         wm overrideredirect $_hintWindow 0
  845.         wm geometry $_hintWindow "+$balloonLeft+$balloonTop"
  846.         wm overrideredirect $_hintWindow 1
  847.         wm deiconify $_hintWindow
  848.         raise $_hintWindow
  849.     } else {
  850.         #NO BALLOON HELP AVAILABLE
  851.     }
  852.     } else {
  853.     #NOT IN BUTTON
  854.     }
  855.     
  856. }
  857.  
  858. # -------------------------------------------------------------
  859. # PUBLIC METHOD: hideBalloon
  860. #
  861. # -------------------------------------------------------------
  862. itcl::body iwidgets::Toolbar::hideBalloon {} {
  863.     wm withdraw $_hintWindow
  864. }
  865.  
  866. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  867. # OPTION MANAGEMENT for -helpstr, -balloonstr
  868. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  869. # -------------------------------------------------------------
  870. # PRIVATE METHOD: _getAttachedOption
  871. #
  872. # optionListName : the name of the array that holds all attached
  873. #              options. It is indexed via widget,option to get
  874. #              the value.
  875. # widget     : the widget that the option is associated with
  876. # option     : the option whose value we are looking for on 
  877. #              this widget.
  878. #
  879. # expects to be called only if the $option is length 1
  880. # -------------------------------------------------------------
  881. itcl::body iwidgets::Toolbar::_getAttachedOption { optionListName widget args retValue} {
  882.     
  883.     # get a reference to the option, so we can change it.
  884.     upvar $args argsRef
  885.     upvar $retValue retValueRef
  886.     
  887.     set success false
  888.     
  889.     if { ![catch { set retValueRef \
  890.         [eval set [subst [set optionListName]]($widget,$argsRef)]}]} {
  891.     
  892.     # remove the option argument 
  893.     set success true
  894.     set argsRef ""
  895.     }
  896.     
  897.     return $success
  898. }
  899.  
  900. # -------------------------------------------------------------
  901. # PRIVATE METHOD: _setAttachedOption
  902. #
  903. # This method allows us to attach new options to a widget. It
  904. # catches the 'option' to be attached, strips it out of 'args'
  905. # attaches it to the 'widget' by stuffing the value into
  906. # 'optionList(widget,option)'
  907. #
  908. # optionListName:  where to store the option and widget association
  909. # widget: is the widget we want to associate the attached option
  910. # option: is the attached option (unknown to this widget)
  911. # args:   the arg list to search and remove the option from (if found)
  912. #
  913. # Modifies the args parameter.
  914. # Returns boolean indicating the success of the method
  915. #
  916. # -------------------------------------------------------------
  917. itcl::body iwidgets::Toolbar::_setAttachedOption {optionListName widget option args} {
  918.     
  919.     upvar args argsRef
  920.     
  921.     set success false
  922.     
  923.     # check for 'option' in the 'args' list for the 'widget'
  924.     set optPos [eval lsearch $args $option]
  925.     
  926.     # ... found it
  927.     if { $optPos != -1 } {
  928.     # grab a copy of the option from arg list
  929.     set [subst [set optionListName]]($widget,$option) \
  930.         [eval lindex $args [expr {$optPos + 1}]]
  931.     
  932.     # remove the option argument and value from the arg list
  933.     set argsRef [eval lreplace $args $optPos [expr {$optPos + 1}]]
  934.     set success true
  935.     }
  936.     # ... if not found, will leave args alone
  937.     
  938.     return $success
  939. }
  940.  
  941. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  942. # GEOMETRY MANAGEMENT for tool widgets
  943. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  944. # -------------------------------------------------------------
  945. # PRIVATE METHOD: _packToolbar
  946. #
  947. #
  948. #
  949. # -------------------------------------------------------------
  950. itcl::body iwidgets::Toolbar::_packToolbar {} {
  951.  
  952.     # forget the previous locations
  953.     foreach tool $_toolList {
  954.     pack forget $tool
  955.     }
  956.     
  957.     # pack in order of _toolList.
  958.     foreach tool $_toolList {
  959.     # adjust for radios and checks to match buttons
  960.     if { [winfo class $tool] == "Radiobutton" || 
  961.     [winfo class $tool] == "Checkbutton" } {
  962.         set iPad 1
  963.     } else {
  964.         set iPad 0
  965.     }
  966.     
  967.     # pack by horizontal or vertical orientation
  968.     if {$itk_option(-orient) == "horizontal" } {
  969.         pack $tool -side left -fill y \
  970.             -ipadx $iPad -ipady $iPad
  971.     } else {
  972.         pack $tool -side top -fill x \
  973.             -ipadx $iPad -ipady $iPad
  974.     }
  975.     }
  976. }
  977.