home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / pythonwin / python.exe / TKFBOX.TCL < prev    next >
Encoding:
Text File  |  2003-05-15  |  47.8 KB  |  1,785 lines

  1. # tkfbox.tcl --
  2. #
  3. #    Implements the "TK" standard file selection dialog box. This
  4. #    dialog box is used on the Unix platforms whenever the tk_strictMotif
  5. #    flag is not set.
  6. #
  7. #    The "TK" standard file selection dialog box is similar to the
  8. #    file selection dialog box on Win95(TM). The user can navigate
  9. #    the directories by clicking on the folder icons or by
  10. #    selecting the "Directory" option menu. The user can select
  11. #    files by clicking on the file icons or by entering a filename
  12. #    in the "Filename:" entry.
  13. #
  14. # RCS: @(#) $Id: tkfbox.tcl,v 1.38 2003/02/21 14:13:26 dkf Exp $
  15. #
  16. # Copyright (c) 1994-1998 Sun Microsystems, Inc.
  17. #
  18. # See the file "license.terms" for information on usage and redistribution
  19. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  20. #
  21.  
  22. #----------------------------------------------------------------------
  23. #
  24. #              I C O N   L I S T
  25. #
  26. # This is a pseudo-widget that implements the icon list inside the 
  27. # ::tk::dialog::file:: dialog box.
  28. #
  29. #----------------------------------------------------------------------
  30.  
  31. # ::tk::IconList --
  32. #
  33. #    Creates an IconList widget.
  34. #
  35. proc ::tk::IconList {w args} {
  36.     IconList_Config $w $args
  37.     IconList_Create $w
  38. }
  39.  
  40. proc ::tk::IconList_Index {w i} {
  41.     upvar #0 ::tk::$w data
  42.     upvar #0 ::tk::$w:itemList itemList
  43.     if {![info exists data(list)]} {set data(list) {}}
  44.     switch -regexp -- $i {
  45.     "^-?[0-9]+$" {
  46.         if { $i < 0 } {
  47.         set i 0
  48.         }
  49.         if { $i >= [llength $data(list)] } {
  50.         set i [expr {[llength $data(list)] - 1}]
  51.         }
  52.         return $i
  53.     }
  54.     "^active$" {
  55.         return $data(index,active)
  56.     }
  57.     "^anchor$" {
  58.         return $data(index,anchor)
  59.     }
  60.     "^end$" {
  61.         return [llength $data(list)]
  62.     }
  63.     "@-?[0-9]+,-?[0-9]+" {
  64.         foreach {x y} [scan $i "@%d,%d"] {
  65.         break
  66.         }
  67.         set item [$data(canvas) find closest $x $y]
  68.         return [lindex [$data(canvas) itemcget $item -tags] 1]
  69.     }
  70.     }
  71. }
  72.  
  73. proc ::tk::IconList_Selection {w op args} {
  74.     upvar ::tk::$w data
  75.     switch -exact -- $op {
  76.     "anchor" {
  77.         if { [llength $args] == 1 } {
  78.         set data(index,anchor) [tk::IconList_Index $w [lindex $args 0]]
  79.         } else {
  80.         return $data(index,anchor)
  81.         }
  82.     }
  83.     "clear" {
  84.         if { [llength $args] == 2 } {
  85.         foreach {first last} $args {
  86.             break
  87.         }
  88.         } elseif { [llength $args] == 1 } {
  89.         set first [set last [lindex $args 0]]
  90.         } else {
  91.         error "wrong # args: should be [lindex [info level 0] 0] path\
  92.             clear first ?last?"
  93.         }
  94.         set first [IconList_Index $w $first]
  95.         set last [IconList_Index $w $last]
  96.         if { $first > $last } {
  97.         set tmp $first
  98.         set first $last
  99.         set last $tmp
  100.         }
  101.         set ind 0
  102.         foreach item $data(selection) {
  103.         if { $item >= $first } {
  104.             set first $ind
  105.             break
  106.         }
  107.         }
  108.         set ind [expr {[llength $data(selection)] - 1}]
  109.         for {} {$ind >= 0} {incr ind -1} {
  110.         set item [lindex $data(selection) $ind]
  111.         if { $item <= $last } {
  112.             set last $ind
  113.             break
  114.         }
  115.         }
  116.  
  117.         if { $first > $last } {
  118.         return
  119.         }
  120.         set data(selection) [lreplace $data(selection) $first $last]
  121.         event generate $w <<ListboxSelect>>
  122.         IconList_DrawSelection $w
  123.     }
  124.     "includes" {
  125.         set index [lsearch -exact $data(selection) [lindex $args 0]]
  126.         return [expr {$index != -1}]
  127.     }
  128.     "set" {
  129.         if { [llength $args] == 2 } {
  130.         foreach {first last} $args {
  131.             break
  132.         }
  133.         } elseif { [llength $args] == 1 } {
  134.         set last [set first [lindex $args 0]]
  135.         } else {
  136.         error "wrong # args: should be [lindex [info level 0] 0] path\
  137.             set first ?last?"
  138.         }
  139.  
  140.         set first [IconList_Index $w $first]
  141.         set last [IconList_Index $w $last]
  142.         if { $first > $last } {
  143.         set tmp $first
  144.         set first $last
  145.         set last $tmp
  146.         }
  147.         for {set i $first} {$i <= $last} {incr i} {
  148.         lappend data(selection) $i
  149.         }
  150.         set data(selection) [lsort -integer -unique $data(selection)]
  151.         event generate $w <<ListboxSelect>>
  152.         IconList_DrawSelection $w
  153.     }
  154.     }
  155. }
  156.  
  157. proc ::tk::IconList_Curselection {w} {
  158.     upvar ::tk::$w data
  159.     return $data(selection)
  160. }
  161.  
  162. proc ::tk::IconList_DrawSelection {w} {
  163.     upvar ::tk::$w data
  164.     upvar ::tk::$w:itemList itemList
  165.  
  166.     $data(canvas) delete selection
  167.     foreach item $data(selection) {
  168.     set rTag [lindex [lindex $data(list) $item] 2]
  169.     foreach {iTag tTag text serial} $itemList($rTag) {
  170.         break
  171.     }
  172.  
  173.     set bbox [$data(canvas) bbox $tTag]
  174.         $data(canvas) create rect $bbox -fill \#a0a0ff -outline \#a0a0ff \
  175.         -tags selection
  176.     }
  177.     $data(canvas) lower selection
  178.     return
  179. }
  180.  
  181. proc ::tk::IconList_Get {w item} {
  182.     upvar ::tk::$w data
  183.     upvar ::tk::$w:itemList itemList
  184.     set rTag [lindex [lindex $data(list) $item] 2]
  185.     foreach {iTag tTag text serial} $itemList($rTag) {
  186.     break
  187.     }
  188.     return $text
  189. }
  190.  
  191. # ::tk::IconList_Config --
  192. #
  193. #    Configure the widget variables of IconList, according to the command
  194. #    line arguments.
  195. #
  196. proc ::tk::IconList_Config {w argList} {
  197.  
  198.     # 1: the configuration specs
  199.     #
  200.     set specs {
  201.     {-command "" "" ""}
  202.     {-multiple "" "" "0"}
  203.     }
  204.  
  205.     # 2: parse the arguments
  206.     #
  207.     tclParseConfigSpec ::tk::$w $specs "" $argList
  208. }
  209.  
  210. # ::tk::IconList_Create --
  211. #
  212. #    Creates an IconList widget by assembling a canvas widget and a
  213. #    scrollbar widget. Sets all the bindings necessary for the IconList's
  214. #    operations.
  215. #
  216. proc ::tk::IconList_Create {w} {
  217.     upvar ::tk::$w data
  218.  
  219.     frame $w
  220.     set data(sbar)   [scrollbar $w.sbar -orient horizontal \
  221.         -highlightthickness 0 -takefocus 0]
  222.     set data(canvas) [canvas $w.canvas -bd 2 -relief sunken \
  223.         -width 400 -height 120 -takefocus 1]
  224.     pack $data(sbar) -side bottom -fill x -padx 2
  225.     pack $data(canvas) -expand yes -fill both
  226.  
  227.     $data(sbar) config -command [list $data(canvas) xview]
  228.     $data(canvas) config -xscrollcommand [list $data(sbar) set]
  229.  
  230.     # Initializes the max icon/text width and height and other variables
  231.     #
  232.     set data(maxIW) 1
  233.     set data(maxIH) 1
  234.     set data(maxTW) 1
  235.     set data(maxTH) 1
  236.     set data(numItems) 0
  237.     set data(curItem)  {}
  238.     set data(noScroll) 1
  239.     set data(selection) {}
  240.     set data(index,anchor) ""
  241.  
  242.     # Creates the event bindings.
  243.     #
  244.     bind $data(canvas) <Configure>    [list tk::IconList_Arrange $w]
  245.  
  246.     bind $data(canvas) <1>        [list tk::IconList_Btn1 $w %x %y]
  247.     bind $data(canvas) <B1-Motion>    [list tk::IconList_Motion1 $w %x %y]
  248.     bind $data(canvas) <B1-Leave>    [list tk::IconList_Leave1 $w %x %y]
  249.     bind $data(canvas) <Control-1>    [list tk::IconList_CtrlBtn1 $w %x %y]
  250.     bind $data(canvas) <Shift-1>    [list tk::IconList_ShiftBtn1 $w %x %y]
  251.     bind $data(canvas) <B1-Enter>    [list tk::CancelRepeat]
  252.     bind $data(canvas) <ButtonRelease-1> [list tk::CancelRepeat]
  253.     bind $data(canvas) <Double-ButtonRelease-1> \
  254.         [list tk::IconList_Double1 $w %x %y]
  255.  
  256.     bind $data(canvas) <Up>        [list tk::IconList_UpDown $w -1]
  257.     bind $data(canvas) <Down>        [list tk::IconList_UpDown $w  1]
  258.     bind $data(canvas) <Left>        [list tk::IconList_LeftRight $w -1]
  259.     bind $data(canvas) <Right>        [list tk::IconList_LeftRight $w  1]
  260.     bind $data(canvas) <Return>        [list tk::IconList_ReturnKey $w]
  261.     bind $data(canvas) <KeyPress>    [list tk::IconList_KeyPress $w %A]
  262.     bind $data(canvas) <Control-KeyPress> ";"
  263.     bind $data(canvas) <Alt-KeyPress>    ";"
  264.  
  265.     bind $data(canvas) <FocusIn>    [list tk::IconList_FocusIn $w]
  266.     bind $data(canvas) <FocusOut>    [list tk::IconList_FocusOut $w]
  267.  
  268.     return $w
  269. }
  270.  
  271. # ::tk::IconList_AutoScan --
  272. #
  273. # This procedure is invoked when the mouse leaves an entry window
  274. # with button 1 down.  It scrolls the window up, down, left, or
  275. # right, depending on where the mouse left the window, and reschedules
  276. # itself as an "after" command so that the window continues to scroll until
  277. # the mouse moves back into the window or the mouse button is released.
  278. #
  279. # Arguments:
  280. # w -        The IconList window.
  281. #
  282. proc ::tk::IconList_AutoScan {w} {
  283.     upvar ::tk::$w data
  284.     variable ::tk::Priv
  285.  
  286.     if {![winfo exists $w]} return
  287.     set x $Priv(x)
  288.     set y $Priv(y)
  289.  
  290.     if {$data(noScroll)} {
  291.     return
  292.     }
  293.     if {$x >= [winfo width $data(canvas)]} {
  294.     $data(canvas) xview scroll 1 units
  295.     } elseif {$x < 0} {
  296.     $data(canvas) xview scroll -1 units
  297.     } elseif {$y >= [winfo height $data(canvas)]} {
  298.     # do nothing
  299.     } elseif {$y < 0} {
  300.     # do nothing
  301.     } else {
  302.     return
  303.     }
  304.  
  305.     IconList_Motion1 $w $x $y
  306.     set Priv(afterId) [after 50 [list tk::IconList_AutoScan $w]]
  307. }
  308.  
  309. # Deletes all the items inside the canvas subwidget and reset the IconList's
  310. # state.
  311. #
  312. proc ::tk::IconList_DeleteAll {w} {
  313.     upvar ::tk::$w data
  314.     upvar ::tk::$w:itemList itemList
  315.  
  316.     $data(canvas) delete all
  317.     catch {unset data(selected)}
  318.     catch {unset data(rect)}
  319.     catch {unset data(list)}
  320.     catch {unset itemList}
  321.     set data(maxIW) 1
  322.     set data(maxIH) 1
  323.     set data(maxTW) 1
  324.     set data(maxTH) 1
  325.     set data(numItems) 0
  326.     set data(curItem)  {}
  327.     set data(noScroll) 1
  328.     set data(selection) {}
  329.     set data(index,anchor) ""
  330.     $data(sbar) set 0.0 1.0
  331.     $data(canvas) xview moveto 0
  332. }
  333.  
  334. # Adds an icon into the IconList with the designated image and text
  335. #
  336. proc ::tk::IconList_Add {w image items} {
  337.     upvar ::tk::$w data
  338.     upvar ::tk::$w:itemList itemList
  339.     upvar ::tk::$w:textList textList
  340.  
  341.     foreach text $items {
  342.     set iTag [$data(canvas) create image 0 0 -image $image -anchor nw \
  343.         -tags [list icon $data(numItems) item$data(numItems)]]
  344.     set tTag [$data(canvas) create text  0 0 -text  $text  -anchor nw \
  345.         -font $data(font) \
  346.         -tags [list text $data(numItems) item$data(numItems)]]
  347.     set rTag [$data(canvas) create rect  0 0 0 0 -fill "" -outline "" \
  348.         -tags [list rect $data(numItems) item$data(numItems)]]
  349.     
  350.     foreach {x1 y1 x2 y2} [$data(canvas) bbox $iTag] {
  351.         break
  352.     }
  353.     set iW [expr {$x2 - $x1}]
  354.     set iH [expr {$y2 - $y1}]
  355.     if {$data(maxIW) < $iW} {
  356.         set data(maxIW) $iW
  357.     }
  358.     if {$data(maxIH) < $iH} {
  359.         set data(maxIH) $iH
  360.     }
  361.     
  362.     foreach {x1 y1 x2 y2} [$data(canvas) bbox $tTag] {
  363.         break
  364.     }
  365.     set tW [expr {$x2 - $x1}]
  366.     set tH [expr {$y2 - $y1}]
  367.     if {$data(maxTW) < $tW} {
  368.         set data(maxTW) $tW
  369.     }
  370.     if {$data(maxTH) < $tH} {
  371.         set data(maxTH) $tH
  372.     }
  373.     
  374.     lappend data(list) [list $iTag $tTag $rTag $iW $iH $tW \
  375.         $tH $data(numItems)]
  376.     set itemList($rTag) [list $iTag $tTag $text $data(numItems)]
  377.     set textList($data(numItems)) [string tolower $text]
  378.     incr data(numItems)
  379.     }
  380. }
  381.  
  382. # Places the icons in a column-major arrangement.
  383. #
  384. proc ::tk::IconList_Arrange {w} {
  385.     upvar ::tk::$w data
  386.  
  387.     if {![info exists data(list)]} {
  388.     if {[info exists data(canvas)] && [winfo exists $data(canvas)]} {
  389.         set data(noScroll) 1
  390.         $data(sbar) config -command ""
  391.     }
  392.     return
  393.     }
  394.  
  395.     set W [winfo width  $data(canvas)]
  396.     set H [winfo height $data(canvas)]
  397.     set pad [expr {[$data(canvas) cget -highlightthickness] + \
  398.         [$data(canvas) cget -bd]}]
  399.     if {$pad < 2} {
  400.     set pad 2
  401.     }
  402.  
  403.     incr W -[expr {$pad*2}]
  404.     incr H -[expr {$pad*2}]
  405.  
  406.     set dx [expr {$data(maxIW) + $data(maxTW) + 8}]
  407.     if {$data(maxTH) > $data(maxIH)} {
  408.     set dy $data(maxTH)
  409.     } else {
  410.     set dy $data(maxIH)
  411.     }
  412.     incr dy 2
  413.     set shift [expr {$data(maxIW) + 4}]
  414.  
  415.     set x [expr {$pad * 2}]
  416.     set y [expr {$pad * 1}] ; # Why * 1 ?
  417.     set usedColumn 0
  418.     foreach sublist $data(list) {
  419.     set usedColumn 1
  420.     foreach {iTag tTag rTag iW iH tW tH} $sublist {
  421.         break
  422.     }
  423.  
  424.     set i_dy [expr {($dy - $iH)/2}]
  425.     set t_dy [expr {($dy - $tH)/2}]
  426.  
  427.     $data(canvas) coords $iTag $x                    [expr {$y + $i_dy}]
  428.     $data(canvas) coords $tTag [expr {$x + $shift}]  [expr {$y + $t_dy}]
  429.     $data(canvas) coords $rTag $x $y [expr {$x+$dx}] [expr {$y+$dy}]
  430.  
  431.     incr y $dy
  432.     if {($y + $dy) > $H} {
  433.         set y [expr {$pad * 1}] ; # *1 ?
  434.         incr x $dx
  435.         set usedColumn 0
  436.     }
  437.     }
  438.  
  439.     if {$usedColumn} {
  440.     set sW [expr {$x + $dx}]
  441.     } else {
  442.     set sW $x
  443.     }
  444.  
  445.     if {$sW < $W} {
  446.     $data(canvas) config -scrollregion [list $pad $pad $sW $H]
  447.     $data(sbar) config -command ""
  448.     $data(canvas) xview moveto 0
  449.     set data(noScroll) 1
  450.     } else {
  451.     $data(canvas) config -scrollregion [list $pad $pad $sW $H]
  452.     $data(sbar) config -command [list $data(canvas) xview]
  453.     set data(noScroll) 0
  454.     }
  455.  
  456.     set data(itemsPerColumn) [expr {($H-$pad)/$dy}]
  457.     if {$data(itemsPerColumn) < 1} {
  458.     set data(itemsPerColumn) 1
  459.     }
  460.  
  461.     if {$data(curItem) != ""} {
  462.     IconList_Select $w [lindex [lindex $data(list) $data(curItem)] 2] 0
  463.     }
  464. }
  465.  
  466. # Gets called when the user invokes the IconList (usually by double-clicking
  467. # or pressing the Return key).
  468. #
  469. proc ::tk::IconList_Invoke {w} {
  470.     upvar ::tk::$w data
  471.  
  472.     if {$data(-command) != "" && [llength $data(selection)]} {
  473.     uplevel #0 $data(-command)
  474.     }
  475. }
  476.  
  477. # ::tk::IconList_See --
  478. #
  479. #    If the item is not (completely) visible, scroll the canvas so that
  480. #    it becomes visible.
  481. proc ::tk::IconList_See {w rTag} {
  482.     upvar ::tk::$w data
  483.     upvar ::tk::$w:itemList itemList
  484.  
  485.     if {$data(noScroll)} {
  486.     return
  487.     }
  488.     set sRegion [$data(canvas) cget -scrollregion]
  489.     if {[string equal $sRegion {}]} {
  490.     return
  491.     }
  492.  
  493.     if { $rTag < 0 || $rTag >= [llength $data(list)] } {
  494.     return
  495.     }
  496.  
  497.     set bbox [$data(canvas) bbox item$rTag]
  498.     set pad [expr {[$data(canvas) cget -highlightthickness] + \
  499.         [$data(canvas) cget -bd]}]
  500.  
  501.     set x1 [lindex $bbox 0]
  502.     set x2 [lindex $bbox 2]
  503.     incr x1 -[expr {$pad * 2}]
  504.     incr x2 -[expr {$pad * 1}] ; # *1 ?
  505.  
  506.     set cW [expr {[winfo width $data(canvas)] - $pad*2}]
  507.  
  508.     set scrollW [expr {[lindex $sRegion 2]-[lindex $sRegion 0]+1}]
  509.     set dispX [expr {int([lindex [$data(canvas) xview] 0]*$scrollW)}]
  510.     set oldDispX $dispX
  511.  
  512.     # check if out of the right edge
  513.     #
  514.     if {($x2 - $dispX) >= $cW} {
  515.     set dispX [expr {$x2 - $cW}]
  516.     }
  517.     # check if out of the left edge
  518.     #
  519.     if {($x1 - $dispX) < 0} {
  520.     set dispX $x1
  521.     }
  522.  
  523.     if {$oldDispX != $dispX} {
  524.     set fraction [expr {double($dispX)/double($scrollW)}]
  525.     $data(canvas) xview moveto $fraction
  526.     }
  527. }
  528.  
  529. proc ::tk::IconList_Btn1 {w x y} {
  530.     upvar ::tk::$w data
  531.  
  532.     focus $data(canvas)
  533.     set x [expr {int([$data(canvas) canvasx $x])}]
  534.     set y [expr {int([$data(canvas) canvasy $y])}]
  535.     set i [IconList_Index $w @${x},${y}]
  536.     if {$i==""} return
  537.     IconList_Selection $w clear 0 end
  538.     IconList_Selection $w set $i
  539.     IconList_Selection $w anchor $i
  540. }
  541.  
  542. proc ::tk::IconList_CtrlBtn1 {w x y} {
  543.     upvar ::tk::$w data
  544.     
  545.     if { $data(-multiple) } {
  546.     focus $data(canvas)
  547.     set x [expr {int([$data(canvas) canvasx $x])}]
  548.     set y [expr {int([$data(canvas) canvasy $y])}]
  549.     set i [IconList_Index $w @${x},${y}]
  550.     if {$i==""} return
  551.     if { [IconList_Selection $w includes $i] } {
  552.         IconList_Selection $w clear $i
  553.     } else {
  554.         IconList_Selection $w set $i
  555.         IconList_Selection $w anchor $i
  556.     }
  557.     }
  558. }
  559.  
  560. proc ::tk::IconList_ShiftBtn1 {w x y} {
  561.     upvar ::tk::$w data
  562.     
  563.     if { $data(-multiple) } {
  564.     focus $data(canvas)
  565.     set x [expr {int([$data(canvas) canvasx $x])}]
  566.     set y [expr {int([$data(canvas) canvasy $y])}]
  567.     set i [IconList_Index $w @${x},${y}]
  568.     if {$i==""} return
  569.     set a [IconList_Index $w anchor]
  570.     if { [string equal $a ""] } {
  571.         set a $i
  572.     }
  573.     IconList_Selection $w clear 0 end
  574.     IconList_Selection $w set $a $i
  575.     }
  576. }
  577.  
  578. # Gets called on button-1 motions
  579. #
  580. proc ::tk::IconList_Motion1 {w x y} {
  581.     upvar ::tk::$w data
  582.     variable ::tk::Priv
  583.     set Priv(x) $x
  584.     set Priv(y) $y
  585.     set x [expr {int([$data(canvas) canvasx $x])}]
  586.     set y [expr {int([$data(canvas) canvasy $y])}]
  587.     set i [IconList_Index $w @${x},${y}]
  588.     if {$i==""} return
  589.     IconList_Selection $w clear 0 end
  590.     IconList_Selection $w set $i
  591. }
  592.  
  593. proc ::tk::IconList_Double1 {w x y} {
  594.     upvar ::tk::$w data
  595.  
  596.     if {[llength $data(selection)]} {
  597.     IconList_Invoke $w
  598.     }
  599. }
  600.  
  601. proc ::tk::IconList_ReturnKey {w} {
  602.     IconList_Invoke $w
  603. }
  604.  
  605. proc ::tk::IconList_Leave1 {w x y} {
  606.     variable ::tk::Priv
  607.  
  608.     set Priv(x) $x
  609.     set Priv(y) $y
  610.     IconList_AutoScan $w
  611. }
  612.  
  613. proc ::tk::IconList_FocusIn {w} {
  614.     upvar ::tk::$w data
  615.  
  616.     if {![info exists data(list)]} {
  617.     return
  618.     }
  619.  
  620.     if {[llength $data(selection)]} {
  621.     IconList_DrawSelection $w
  622.     }
  623. }
  624.  
  625. proc ::tk::IconList_FocusOut {w} {
  626.     IconList_Selection $w clear 0 end
  627. }
  628.  
  629. # ::tk::IconList_UpDown --
  630. #
  631. # Moves the active element up or down by one element
  632. #
  633. # Arguments:
  634. # w -        The IconList widget.
  635. # amount -    +1 to move down one item, -1 to move back one item.
  636. #
  637. proc ::tk::IconList_UpDown {w amount} {
  638.     upvar ::tk::$w data
  639.  
  640.     if {![info exists data(list)]} {
  641.     return
  642.     }
  643.  
  644.     set curr [tk::IconList_Curselection $w]
  645.     if { [llength $curr] == 0 } {
  646.     set i 0
  647.     } else {
  648.     set i [tk::IconList_Index $w anchor]
  649.     if {$i==""} return
  650.     incr i $amount
  651.     }
  652.     IconList_Selection $w clear 0 end
  653.     IconList_Selection $w set $i
  654.     IconList_Selection $w anchor $i
  655.     IconList_See $w $i
  656. }
  657.  
  658. # ::tk::IconList_LeftRight --
  659. #
  660. # Moves the active element left or right by one column
  661. #
  662. # Arguments:
  663. # w -        The IconList widget.
  664. # amount -    +1 to move right one column, -1 to move left one column.
  665. #
  666. proc ::tk::IconList_LeftRight {w amount} {
  667.     upvar ::tk::$w data
  668.  
  669.     if {![info exists data(list)]} {
  670.     return
  671.     }
  672.  
  673.     set curr [IconList_Curselection $w]
  674.     if { [llength $curr] == 0 } {
  675.     set i 0
  676.     } else {
  677.     set i [IconList_Index $w anchor]
  678.     if {$i==""} return
  679.     incr i [expr {$amount*$data(itemsPerColumn)}]
  680.     }
  681.     IconList_Selection $w clear 0 end
  682.     IconList_Selection $w set $i
  683.     IconList_Selection $w anchor $i
  684.     IconList_See $w $i
  685. }
  686.  
  687. #----------------------------------------------------------------------
  688. #        Accelerator key bindings
  689. #----------------------------------------------------------------------
  690.  
  691. # ::tk::IconList_KeyPress --
  692. #
  693. #    Gets called when user enters an arbitrary key in the listbox.
  694. #
  695. proc ::tk::IconList_KeyPress {w key} {
  696.     variable ::tk::Priv
  697.  
  698.     append Priv(ILAccel,$w) $key
  699.     IconList_Goto $w $Priv(ILAccel,$w)
  700.     catch {
  701.     after cancel $Priv(ILAccel,$w,afterId)
  702.     }
  703.     set Priv(ILAccel,$w,afterId) [after 500 [list tk::IconList_Reset $w]]
  704. }
  705.  
  706. proc ::tk::IconList_Goto {w text} {
  707.     upvar ::tk::$w data
  708.     upvar ::tk::$w:textList textList
  709.     
  710.     if {![info exists data(list)]} {
  711.     return
  712.     }
  713.  
  714.     if {[string equal {} $text]} {
  715.     return
  716.     }
  717.  
  718.     if {$data(curItem) == "" || $data(curItem) == 0} {
  719.     set start  0
  720.     } else {
  721.     set start  $data(curItem)
  722.     }
  723.  
  724.     set text [string tolower $text]
  725.     set theIndex -1
  726.     set less 0
  727.     set len [string length $text]
  728.     set len0 [expr {$len-1}]
  729.     set i $start
  730.  
  731.     # Search forward until we find a filename whose prefix is an exact match
  732.     # with $text
  733.     while {1} {
  734.     set sub [string range $textList($i) 0 $len0]
  735.     if {[string equal $text $sub]} {
  736.         set theIndex $i
  737.         break
  738.     }
  739.     incr i
  740.     if {$i == $data(numItems)} {
  741.         set i 0
  742.     }
  743.     if {$i == $start} {
  744.         break
  745.     }
  746.     }
  747.  
  748.     if {$theIndex > -1} {
  749.     IconList_Selection $w clear 0 end
  750.     IconList_Selection $w set $theIndex
  751.     IconList_Selection $w anchor $theIndex
  752.     IconList_See $w $theIndex
  753.     }
  754. }
  755.  
  756. proc ::tk::IconList_Reset {w} {
  757.     variable ::tk::Priv
  758.  
  759.     catch {unset Priv(ILAccel,$w)}
  760. }
  761.  
  762. #----------------------------------------------------------------------
  763. #
  764. #              F I L E   D I A L O G
  765. #
  766. #----------------------------------------------------------------------
  767.  
  768. namespace eval ::tk::dialog {}
  769. namespace eval ::tk::dialog::file {
  770.     namespace import ::tk::msgcat::*
  771. }
  772.  
  773. # ::tk::dialog::file:: --
  774. #
  775. #    Implements the TK file selection dialog. This dialog is used when
  776. #    the tk_strictMotif flag is set to false. This procedure shouldn't
  777. #    be called directly. Call tk_getOpenFile or tk_getSaveFile instead.
  778. #
  779. # Arguments:
  780. #    type        "open" or "save"
  781. #    args        Options parsed by the procedure.
  782. #
  783.  
  784. proc ::tk::dialog::file:: {type args} {
  785.     variable ::tk::Priv
  786.     set dataName __tk_filedialog
  787.     upvar ::tk::dialog::file::$dataName data
  788.  
  789.     ::tk::dialog::file::Config $dataName $type $args
  790.  
  791.     if {[string equal $data(-parent) .]} {
  792.         set w .$dataName
  793.     } else {
  794.         set w $data(-parent).$dataName
  795.     }
  796.  
  797.     # (re)create the dialog box if necessary
  798.     #
  799.     if {![winfo exists $w]} {
  800.     ::tk::dialog::file::Create $w TkFDialog
  801.     } elseif {[string compare [winfo class $w] TkFDialog]} {
  802.     destroy $w
  803.     ::tk::dialog::file::Create $w TkFDialog
  804.     } else {
  805.     set data(dirMenuBtn) $w.f1.menu
  806.     set data(dirMenu) $w.f1.menu.menu
  807.     set data(upBtn) $w.f1.up
  808.     set data(icons) $w.icons
  809.     set data(ent) $w.f2.ent
  810.     set data(typeMenuLab) $w.f2.lab
  811.     set data(typeMenuBtn) $w.f2.menu
  812.     set data(typeMenu) $data(typeMenuBtn).m
  813.     set data(okBtn) $w.f2.ok
  814.     set data(cancelBtn) $w.f2.cancel
  815.     ::tk::dialog::file::SetSelectMode $w $data(-multiple)
  816.     }
  817.  
  818.     # Dialog boxes should be transient with respect to their parent,
  819.     # so that they will always stay on top of their parent window.  However,
  820.     # some window managers will create the window as withdrawn if the parent
  821.     # window is withdrawn or iconified.  Combined with the grab we put on the
  822.     # window, this can hang the entire application.  Therefore we only make
  823.     # the dialog transient if the parent is viewable.
  824.  
  825.     if {[winfo viewable [winfo toplevel $data(-parent)]] } {
  826.     wm transient $w $data(-parent)
  827.     }
  828.  
  829.     # Add traces on the selectPath variable
  830.     #
  831.  
  832.     trace variable data(selectPath) w "::tk::dialog::file::SetPath $w"
  833.     $data(dirMenuBtn) configure \
  834.         -textvariable ::tk::dialog::file::${dataName}(selectPath)
  835.  
  836.     # Initialize the file types menu
  837.     #
  838.     if {[llength $data(-filetypes)]} {
  839.     $data(typeMenu) delete 0 end
  840.     foreach type $data(-filetypes) {
  841.         set title  [lindex $type 0]
  842.         set filter [lindex $type 1]
  843.         $data(typeMenu) add command -label $title \
  844.         -command [list ::tk::dialog::file::SetFilter $w $type]
  845.     }
  846.     ::tk::dialog::file::SetFilter $w [lindex $data(-filetypes) 0]
  847.     $data(typeMenuBtn) config -state normal
  848.     $data(typeMenuLab) config -state normal
  849.     } else {
  850.     set data(filter) "*"
  851.     $data(typeMenuBtn) config -state disabled -takefocus 0
  852.     $data(typeMenuLab) config -state disabled
  853.     }
  854.     ::tk::dialog::file::UpdateWhenIdle $w
  855.  
  856.     # Withdraw the window, then update all the geometry information
  857.     # so we know how big it wants to be, then center the window in the
  858.     # display and de-iconify it.
  859.  
  860.     ::tk::PlaceWindow $w widget $data(-parent)
  861.     wm title $w $data(-title)
  862.  
  863.     # Set a grab and claim the focus too.
  864.  
  865.     ::tk::SetFocusGrab $w $data(ent)
  866.     $data(ent) delete 0 end
  867.     $data(ent) insert 0 $data(selectFile)
  868.     $data(ent) selection range 0 end
  869.     $data(ent) icursor end
  870.  
  871.     # Wait for the user to respond, then restore the focus and
  872.     # return the index of the selected button.  Restore the focus
  873.     # before deleting the window, since otherwise the window manager
  874.     # may take the focus away so we can't redirect it.  Finally,
  875.     # restore any grab that was in effect.
  876.  
  877.     vwait ::tk::Priv(selectFilePath)
  878.  
  879.     ::tk::RestoreFocusGrab $w $data(ent) withdraw
  880.  
  881.     # Cleanup traces on selectPath variable
  882.     #
  883.  
  884.     foreach trace [trace vinfo data(selectPath)] {
  885.     trace vdelete data(selectPath) [lindex $trace 0] [lindex $trace 1]
  886.     }
  887.     $data(dirMenuBtn) configure -textvariable {}
  888.  
  889.     return $Priv(selectFilePath)
  890. }
  891.  
  892. # ::tk::dialog::file::Config --
  893. #
  894. #    Configures the TK filedialog according to the argument list
  895. #
  896. proc ::tk::dialog::file::Config {dataName type argList} {
  897.     upvar ::tk::dialog::file::$dataName data
  898.  
  899.     set data(type) $type
  900.  
  901.     # 0: Delete all variable that were set on data(selectPath) the
  902.     # last time the file dialog is used. The traces may cause troubles
  903.     # if the dialog is now used with a different -parent option.
  904.  
  905.     foreach trace [trace vinfo data(selectPath)] {
  906.     trace vdelete data(selectPath) [lindex $trace 0] [lindex $trace 1]
  907.     }
  908.  
  909.     # 1: the configuration specs
  910.     #
  911.     set specs {
  912.     {-defaultextension "" "" ""}
  913.     {-filetypes "" "" ""}
  914.     {-initialdir "" "" ""}
  915.     {-initialfile "" "" ""}
  916.     {-parent "" "" "."}
  917.     {-title "" "" ""}
  918.     }
  919.  
  920.     # The "-multiple" option is only available for the "open" file dialog.
  921.     #
  922.     if { [string equal $type "open"] } {
  923.     lappend specs {-multiple "" "" "0"}
  924.     }
  925.  
  926.     # 2: default values depending on the type of the dialog
  927.     #
  928.     if {![info exists data(selectPath)]} {
  929.     # first time the dialog has been popped up
  930.     set data(selectPath) [pwd]
  931.     set data(selectFile) ""
  932.     }
  933.  
  934.     # 3: parse the arguments
  935.     #
  936.     tclParseConfigSpec ::tk::dialog::file::$dataName $specs "" $argList
  937.  
  938.     if {$data(-title) == ""} {
  939.     if {[string equal $type "open"]} {
  940.         set data(-title) "[mc "Open"]"
  941.     } else {
  942.         set data(-title) "[mc "Save As"]"
  943.     }
  944.     }
  945.  
  946.     # 4: set the default directory and selection according to the -initial
  947.     #    settings
  948.     #
  949.     if {$data(-initialdir) != ""} {
  950.     # Ensure that initialdir is an absolute path name.
  951.     if {[file isdirectory $data(-initialdir)]} {
  952.         set old [pwd]
  953.         cd $data(-initialdir)
  954.         set data(selectPath) [pwd]
  955.         cd $old
  956.     } else {
  957.         set data(selectPath) [pwd]
  958.     }
  959.     }
  960.     set data(selectFile) $data(-initialfile)
  961.  
  962.     # 5. Parse the -filetypes option
  963.     #
  964.     set data(-filetypes) [::tk::FDGetFileTypes $data(-filetypes)]
  965.  
  966.     if {![winfo exists $data(-parent)]} {
  967.     error "bad window path name \"$data(-parent)\""
  968.     }
  969.  
  970.     # Set -multiple to a one or zero value (not other boolean types
  971.     # like "yes") so we can use it in tests more easily.
  972.     if {![string compare $type save]} {
  973.     set data(-multiple) 0
  974.     } elseif {$data(-multiple)} { 
  975.     set data(-multiple) 1 
  976.     } else {
  977.     set data(-multiple) 0
  978.     }
  979. }
  980.  
  981. proc ::tk::dialog::file::Create {w class} {
  982.     set dataName [lindex [split $w .] end]
  983.     upvar ::tk::dialog::file::$dataName data
  984.     variable ::tk::Priv
  985.     global tk_library
  986.  
  987.     toplevel $w -class $class
  988.  
  989.     # f1: the frame with the directory option menu
  990.     #
  991.     set f1 [frame $w.f1]
  992.     bind [::tk::AmpWidget label $f1.lab -text "[mc "&Directory:"]" ] \
  993.     <<AltUnderlined>> [list focus $f1.menu]
  994.     
  995.     set data(dirMenuBtn) $f1.menu
  996.     set data(dirMenu) [tk_optionMenu $f1.menu [format %s(selectPath) ::tk::dialog::file::$dataName] ""]
  997.     set data(upBtn) [button $f1.up]
  998.     if {![info exists Priv(updirImage)]} {
  999.     set Priv(updirImage) [image create bitmap -data {
  1000. #define updir_width 28
  1001. #define updir_height 16
  1002. static char updir_bits[] = {
  1003.    0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00,
  1004.    0x20, 0x40, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x10, 0x00, 0x00, 0x01,
  1005.    0x10, 0x02, 0x00, 0x01, 0x10, 0x07, 0x00, 0x01, 0x90, 0x0f, 0x00, 0x01,
  1006.    0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01, 0x10, 0x02, 0x00, 0x01,
  1007.    0x10, 0xfe, 0x07, 0x01, 0x10, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x01,
  1008.    0xf0, 0xff, 0xff, 0x01};}]
  1009.     }
  1010.     $data(upBtn) config -image $Priv(updirImage)
  1011.  
  1012.     $f1.menu config -takefocus 1 -highlightthickness 2
  1013.  
  1014.     pack $data(upBtn) -side right -padx 4 -fill both
  1015.     pack $f1.lab -side left -padx 4 -fill both
  1016.     pack $f1.menu -expand yes -fill both -padx 4
  1017.  
  1018.     # data(icons): the IconList that list the files and directories.
  1019.     #
  1020.     if { [string equal $class TkFDialog] } {
  1021.     if { $data(-multiple) } {
  1022.         set fNameCaption [mc "File &names:"]
  1023.     } else {
  1024.         set fNameCaption [mc "File &name:"]
  1025.     }
  1026.     set fTypeCaption [mc "Files of &type:"]
  1027.     set iconListCommand [list ::tk::dialog::file::OkCmd $w]
  1028.     } else {
  1029.     set fNameCaption [mc "&Selection:"]
  1030.     set iconListCommand [list ::tk::dialog::file::chooseDir::DblClick $w]
  1031.     }
  1032.     set data(icons) [::tk::IconList $w.icons \
  1033.         -command    $iconListCommand \
  1034.         -multiple    $data(-multiple)]
  1035.     bind $data(icons) <<ListboxSelect>> \
  1036.         [list ::tk::dialog::file::ListBrowse $w]
  1037.  
  1038.     # f2: the frame with the OK button, cancel button, "file name" field
  1039.     #     and file types field.
  1040.     #
  1041.     set f2 [frame $w.f2 -bd 0]
  1042.     bind [::tk::AmpWidget label $f2.lab -text $fNameCaption -anchor e -pady 0]\
  1043.         <<AltUnderlined>> [list focus $f2.ent]
  1044.     set data(ent) [entry $f2.ent]
  1045.  
  1046.     # The font to use for the icons. The default Canvas font on Unix
  1047.     # is just deviant.
  1048.     set ::tk::$w.icons(font) [$data(ent) cget -font]
  1049.  
  1050.     # Make the file types bits only if this is a File Dialog
  1051.     if { [string equal $class TkFDialog] } {
  1052.     # The "File of types:" label needs to be grayed-out when
  1053.     # -filetypes are not specified. The label widget does not support
  1054.     # grayed-out text on monochrome displays. Therefore, we have to
  1055.     # use a button widget to emulate a label widget (by setting its
  1056.     # bindtags)
  1057.  
  1058.     set data(typeMenuLab) [::tk::AmpWidget button $f2.lab2 \
  1059.         -text $fTypeCaption  -anchor e  -bd [$f2.lab cget -bd] \
  1060.         -highlightthickness [$f2.lab cget -highlightthickness] \
  1061.         -relief [$f2.lab cget -relief] \
  1062.         -padx [$f2.lab cget -padx] \
  1063.         -pady [$f2.lab cget -pady]]
  1064.     bindtags $data(typeMenuLab) [list $data(typeMenuLab) Label \
  1065.         [winfo toplevel $data(typeMenuLab)] all]
  1066.     set data(typeMenuBtn) [menubutton $f2.menu -indicatoron 1 \
  1067.         -menu $f2.menu.m]
  1068.     set data(typeMenu) [menu $data(typeMenuBtn).m -tearoff 0]
  1069.     $data(typeMenuBtn) config -takefocus 1 -highlightthickness 2 \
  1070.         -relief raised -bd 2 -anchor w
  1071.         bind $data(typeMenuLab) <<AltUnderlined>> [list \
  1072.         focus $data(typeMenuBtn)]
  1073.     }
  1074.  
  1075.     # the okBtn is created after the typeMenu so that the keyboard traversal
  1076.     # is in the right order
  1077.     set data(okBtn)     [::tk::AmpWidget button $f2.ok \
  1078.         -text "[mc "&OK"]"     -default active -pady 3]
  1079.     set data(cancelBtn) [::tk::AmpWidget button $f2.cancel \
  1080.         -text "[mc "&Cancel"]" -default normal -pady 3]
  1081.  
  1082.     # grid the widgets in f2
  1083.     #
  1084.     grid $f2.lab $f2.ent $data(okBtn) -padx 4 -sticky ew
  1085.     grid configure $f2.ent -padx 2
  1086.     if { [string equal $class TkFDialog] } {
  1087.     grid $data(typeMenuLab) $data(typeMenuBtn) $data(cancelBtn) \
  1088.         -padx 4 -sticky ew
  1089.     grid configure $data(typeMenuBtn) -padx 0
  1090.     } else {
  1091.     grid x x $data(cancelBtn) -padx 4 -sticky ew
  1092.     }
  1093.     grid columnconfigure $f2 1 -weight 1
  1094.  
  1095.     # Pack all the frames together. We are done with widget construction.
  1096.     #
  1097.     pack $f1 -side top -fill x -pady 4
  1098.     pack $f2 -side bottom -fill x
  1099.     pack $data(icons) -expand yes -fill both -padx 4 -pady 1
  1100.  
  1101.     # Set up the event handlers that are common to Directory and File Dialogs
  1102.     #
  1103.  
  1104.     wm protocol $w WM_DELETE_WINDOW [list ::tk::dialog::file::CancelCmd $w]
  1105.     $data(upBtn)     config -command [list ::tk::dialog::file::UpDirCmd $w]
  1106.     $data(cancelBtn) config -command [list ::tk::dialog::file::CancelCmd $w]
  1107.     bind $w <KeyPress-Escape> [list tk::ButtonInvoke $data(cancelBtn)]
  1108.     bind $w <Alt-Key> [list tk::AltKeyInDialog $w %A]
  1109.     # Set up event handlers specific to File or Directory Dialogs
  1110.     #
  1111.  
  1112.     if { [string equal $class TkFDialog] } {
  1113.     bind $data(ent) <Return> [list ::tk::dialog::file::ActivateEnt $w]
  1114.     $data(okBtn)     config -command [list ::tk::dialog::file::OkCmd $w]
  1115.     bind $w <Alt-t> [format {
  1116.         if {[string equal [%s cget -state] "normal"]} {
  1117.         focus %s
  1118.         }
  1119.     } $data(typeMenuBtn) $data(typeMenuBtn)]
  1120.     } else {
  1121.     set okCmd [list ::tk::dialog::file::chooseDir::OkCmd $w]
  1122.     bind $data(ent) <Return> $okCmd
  1123.     $data(okBtn) config -command $okCmd
  1124.     bind $w <Alt-s> [list focus $data(ent)]
  1125.     bind $w <Alt-o> [list tk::ButtonInvoke $data(okBtn)]
  1126.     }
  1127.  
  1128.     # Build the focus group for all the entries
  1129.     #
  1130.     ::tk::FocusGroup_Create $w
  1131.     ::tk::FocusGroup_BindIn $w  $data(ent) [list ::tk::dialog::file::EntFocusIn $w]
  1132.     ::tk::FocusGroup_BindOut $w $data(ent) [list ::tk::dialog::file::EntFocusOut $w]
  1133. }
  1134.  
  1135. # ::tk::dialog::file::SetSelectMode --
  1136. #
  1137. #    Set the select mode of the dialog to single select or multi-select.
  1138. #
  1139. # Arguments:
  1140. #    w        The dialog path.
  1141. #    multi        1 if the dialog is multi-select; 0 otherwise.
  1142. #
  1143. # Results:
  1144. #    None.
  1145.  
  1146. proc ::tk::dialog::file::SetSelectMode {w multi} {
  1147.     set dataName __tk_filedialog
  1148.     upvar ::tk::dialog::file::$dataName data
  1149.     if { $multi } {
  1150.     set fNameCaption "[mc {File &names:}]"
  1151.     } else {
  1152.     set fNameCaption "[mc {File &name:}]"
  1153.     }
  1154.     set iconListCommand [list ::tk::dialog::file::OkCmd $w]
  1155.     ::tk::SetAmpText $w.f2.lab $fNameCaption 
  1156.     ::tk::IconList_Config $data(icons) \
  1157.         [list -multiple $multi -command $iconListCommand]
  1158.     return
  1159. }
  1160.  
  1161. # ::tk::dialog::file::UpdateWhenIdle --
  1162. #
  1163. #    Creates an idle event handler which updates the dialog in idle
  1164. #    time. This is important because loading the directory may take a long
  1165. #    time and we don't want to load the same directory for multiple times
  1166. #    due to multiple concurrent events.
  1167. #
  1168. proc ::tk::dialog::file::UpdateWhenIdle {w} {
  1169.     upvar ::tk::dialog::file::[winfo name $w] data
  1170.  
  1171.     if {[info exists data(updateId)]} {
  1172.     return
  1173.     } else {
  1174.     set data(updateId) [after idle [list ::tk::dialog::file::Update $w]]
  1175.     }
  1176. }
  1177.  
  1178. # ::tk::dialog::file::Update --
  1179. #
  1180. #    Loads the files and directories into the IconList widget. Also
  1181. #    sets up the directory option menu for quick access to parent
  1182. #    directories.
  1183. #
  1184. proc ::tk::dialog::file::Update {w} {
  1185.  
  1186.     # This proc may be called within an idle handler. Make sure that the
  1187.     # window has not been destroyed before this proc is called
  1188.     if {![winfo exists $w]} {
  1189.     return
  1190.     }
  1191.     set class [winfo class $w]
  1192.     if { [string compare $class TkFDialog] && \
  1193.         [string compare $class TkChooseDir] } {
  1194.     return
  1195.     }
  1196.  
  1197.     set dataName [winfo name $w]
  1198.     upvar ::tk::dialog::file::$dataName data
  1199.     variable ::tk::Priv
  1200.     global tk_library
  1201.     catch {unset data(updateId)}
  1202.  
  1203.     if {![info exists Priv(folderImage)]} {
  1204.     set Priv(folderImage) [image create photo -data {
  1205. R0lGODlhEAAMAKEAAAD//wAAAPD/gAAAACH5BAEAAAAALAAAAAAQAAwAAAIghINhyycvVFsB
  1206. QtmS3rjaH1Hg141WaT5ouprt2HHcUgAAOw==}]
  1207.     set Priv(fileImage)   [image create photo -data {
  1208. R0lGODlhDAAMAKEAALLA3AAAAP//8wAAACH5BAEAAAAALAAAAAAMAAwAAAIgRI4Ha+IfWHsO
  1209. rSASvJTGhnhcV3EJlo3kh53ltF5nAhQAOw==}]
  1210.     }
  1211.     set folder $Priv(folderImage)
  1212.     set file   $Priv(fileImage)
  1213.  
  1214.     set appPWD [pwd]
  1215.     if {[catch {
  1216.     cd $data(selectPath)
  1217.     }]} {
  1218.     # We cannot change directory to $data(selectPath). $data(selectPath)
  1219.     # should have been checked before ::tk::dialog::file::Update is called, so
  1220.     # we normally won't come to here. Anyways, give an error and abort
  1221.     # action.
  1222.     tk_messageBox -type ok -parent $w -message \
  1223.         "[mc "Cannot change to the directory \"%1\$s\".\nPermission denied." $data(selectPath)]"\
  1224.         -icon warning
  1225.     cd $appPWD
  1226.     return
  1227.     }
  1228.  
  1229.     # Turn on the busy cursor. BUG?? We haven't disabled X events, though,
  1230.     # so the user may still click and cause havoc ...
  1231.     #
  1232.     set entCursor [$data(ent) cget -cursor]
  1233.     set dlgCursor [$w         cget -cursor]
  1234.     $data(ent) config -cursor watch
  1235.     $w         config -cursor watch
  1236.     update idletasks
  1237.     
  1238.     ::tk::IconList_DeleteAll $data(icons)
  1239.  
  1240.     # Make the dir list
  1241.     #
  1242.     set completeFileList [lsort -dictionary -unique [glob -nocomplain .* *]]
  1243.     set dirList {}
  1244.     foreach f $completeFileList {
  1245.     if {[string equal $f .]} {
  1246.         continue
  1247.     }
  1248.     if {[string equal $f ..]} {
  1249.         continue
  1250.     }
  1251.     if {[file isdir ./$f]} {
  1252.         lappend dirList $f
  1253.     }
  1254.     }
  1255.     ::tk::IconList_Add $data(icons) $folder $dirList
  1256.     if { [string equal $class TkFDialog] } {
  1257.     # Make the file list if this is a File Dialog
  1258.     #
  1259.     if {[string equal $data(filter) *]} {
  1260.         set files $completeFileList
  1261.     } else {
  1262.         set files {}
  1263.         foreach f $completeFileList {
  1264.         foreach pat $data(filter) {
  1265.             if { [string match $pat $f] } {
  1266.             lappend files $f
  1267.             break
  1268.             }
  1269.         }
  1270.         }
  1271.     }
  1272.     set fileList {}
  1273.     foreach f $files {
  1274.         if {![file isdir ./$f]} {
  1275.         lappend fileList $f
  1276.         }
  1277.     }
  1278.     ::tk::IconList_Add $data(icons) $file $fileList
  1279.     }
  1280.  
  1281.     ::tk::IconList_Arrange $data(icons)
  1282.  
  1283.     # Update the Directory: option menu
  1284.     #
  1285.     set list ""
  1286.     set dir ""
  1287.     foreach subdir [file split $data(selectPath)] {
  1288.     set dir [file join $dir $subdir]
  1289.     lappend list $dir
  1290.     }
  1291.  
  1292.     $data(dirMenu) delete 0 end
  1293.     set var [format %s(selectPath) ::tk::dialog::file::$dataName]
  1294.     foreach path $list {
  1295.     $data(dirMenu) add command -label $path -command [list set $var $path]
  1296.     }
  1297.  
  1298.     # Restore the PWD to the application's PWD
  1299.     #
  1300.     cd $appPWD
  1301.  
  1302.     if { [string equal $class TkFDialog] } {
  1303.     # Restore the Open/Save Button if this is a File Dialog
  1304.     #
  1305.     if {[string equal $data(type) open]} {
  1306.         ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  1307.     } else {
  1308.         ::tk::SetAmpText $data(okBtn) [mc "&Save"]
  1309.     }
  1310.     }
  1311.  
  1312.     # turn off the busy cursor.
  1313.     #
  1314.     $data(ent) config -cursor $entCursor
  1315.     $w         config -cursor $dlgCursor
  1316. }
  1317.  
  1318. # ::tk::dialog::file::SetPathSilently --
  1319. #
  1320. #     Sets data(selectPath) without invoking the trace procedure
  1321. #
  1322. proc ::tk::dialog::file::SetPathSilently {w path} {
  1323.     upvar ::tk::dialog::file::[winfo name $w] data
  1324.     
  1325.     trace vdelete  data(selectPath) w [list ::tk::dialog::file::SetPath $w]
  1326.     set data(selectPath) $path
  1327.     trace variable data(selectPath) w [list ::tk::dialog::file::SetPath $w]
  1328. }
  1329.  
  1330.  
  1331. # This proc gets called whenever data(selectPath) is set
  1332. #
  1333. proc ::tk::dialog::file::SetPath {w name1 name2 op} {
  1334.     if {[winfo exists $w]} {
  1335.     upvar ::tk::dialog::file::[winfo name $w] data
  1336.     ::tk::dialog::file::UpdateWhenIdle $w
  1337.     # On directory dialogs, we keep the entry in sync with the currentdir.
  1338.     if { [string equal [winfo class $w] TkChooseDir] } {
  1339.         $data(ent) delete 0 end
  1340.         $data(ent) insert end $data(selectPath)
  1341.     }
  1342.     }
  1343. }
  1344.  
  1345. # This proc gets called whenever data(filter) is set
  1346. #
  1347. proc ::tk::dialog::file::SetFilter {w type} {
  1348.     upvar ::tk::dialog::file::[winfo name $w] data
  1349.     upvar ::tk::$data(icons) icons
  1350.  
  1351.     set data(filter) [lindex $type 1]
  1352.     $data(typeMenuBtn) config -text [lindex $type 0] -indicatoron 1
  1353.  
  1354.     # If we aren't using a default extension, use the one suppled
  1355.     # by the filter.
  1356.     if {![info exists data(extUsed)]} {
  1357.     if {[string length $data(-defaultextension)]} {
  1358.         set data(extUsed) 1
  1359.     } else {
  1360.         set data(extUsed) 0
  1361.     }
  1362.     }
  1363.  
  1364.     if {!$data(extUsed)} {
  1365.     # Get the first extension in the list that matches {^\*\.\w+$}
  1366.     # and remove all * from the filter.
  1367.     set index [lsearch -regexp $data(filter) {^\*\.\w+$}]
  1368.     if {$index >= 0} {
  1369.         set data(-defaultextension) \
  1370.             [string trimleft [lindex $data(filter) $index] "*"]
  1371.     } else {
  1372.         # Couldn't find anything!  Reset to a safe default...
  1373.         set data(-defaultextension) ""
  1374.     }
  1375.     }
  1376.  
  1377.     $icons(sbar) set 0.0 0.0
  1378.     
  1379.     ::tk::dialog::file::UpdateWhenIdle $w
  1380. }
  1381.  
  1382. # tk::dialog::file::ResolveFile --
  1383. #
  1384. #    Interpret the user's text input in a file selection dialog.
  1385. #    Performs:
  1386. #
  1387. #    (1) ~ substitution
  1388. #    (2) resolve all instances of . and ..
  1389. #    (3) check for non-existent files/directories
  1390. #    (4) check for chdir permissions
  1391. #
  1392. # Arguments:
  1393. #    context:  the current directory you are in
  1394. #    text:      the text entered by the user
  1395. #    defaultext: the default extension to add to files with no extension
  1396. #
  1397. # Return vaue:
  1398. #    [list $flag $directory $file]
  1399. #
  1400. #     flag = OK    : valid input
  1401. #          = PATTERN    : valid directory/pattern
  1402. #          = PATH    : the directory does not exist
  1403. #          = FILE    : the directory exists by the file doesn't
  1404. #              exist
  1405. #          = CHDIR    : Cannot change to the directory
  1406. #          = ERROR    : Invalid entry
  1407. #
  1408. #     directory      : valid only if flag = OK or PATTERN or FILE
  1409. #     file           : valid only if flag = OK or PATTERN
  1410. #
  1411. #    directory may not be the same as context, because text may contain
  1412. #    a subdirectory name
  1413. #
  1414. proc ::tk::dialog::file::ResolveFile {context text defaultext} {
  1415.  
  1416.     set appPWD [pwd]
  1417.  
  1418.     set path [::tk::dialog::file::JoinFile $context $text]
  1419.  
  1420.     # If the file has no extension, append the default.  Be careful not
  1421.     # to do this for directories, otherwise typing a dirname in the box
  1422.     # will give back "dirname.extension" instead of trying to change dir.
  1423.     if {![file isdirectory $path] && [string equal [file ext $path] ""]} {
  1424.     set path "$path$defaultext"
  1425.     }
  1426.  
  1427.  
  1428.     if {[catch {file exists $path}]} {
  1429.     # This "if" block can be safely removed if the following code
  1430.     # stop generating errors.
  1431.     #
  1432.     #    file exists ~nonsuchuser
  1433.     #
  1434.     return [list ERROR $path ""]
  1435.     }
  1436.  
  1437.     if {[file exists $path]} {
  1438.     if {[file isdirectory $path]} {
  1439.         if {[catch {cd $path}]} {
  1440.         return [list CHDIR $path ""]
  1441.         }
  1442.         set directory [pwd]
  1443.         set file ""
  1444.         set flag OK
  1445.         cd $appPWD
  1446.     } else {
  1447.         if {[catch {cd [file dirname $path]}]} {
  1448.         return [list CHDIR [file dirname $path] ""]
  1449.         }
  1450.         set directory [pwd]
  1451.         set file [file tail $path]
  1452.         set flag OK
  1453.         cd $appPWD
  1454.     }
  1455.     } else {
  1456.     set dirname [file dirname $path]
  1457.     if {[file exists $dirname]} {
  1458.         if {[catch {cd $dirname}]} {
  1459.         return [list CHDIR $dirname ""]
  1460.         }
  1461.         set directory [pwd]
  1462.         set file [file tail $path]
  1463.         if {[regexp {[*]|[?]} $file]} {
  1464.         set flag PATTERN
  1465.         } else {
  1466.         set flag FILE
  1467.         }
  1468.         cd $appPWD
  1469.     } else {
  1470.         set directory $dirname
  1471.         set file [file tail $path]
  1472.         set flag PATH
  1473.     }
  1474.     }
  1475.  
  1476.     return [list $flag $directory $file]
  1477. }
  1478.  
  1479.  
  1480. # Gets called when the entry box gets keyboard focus. We clear the selection
  1481. # from the icon list . This way the user can be certain that the input in the 
  1482. # entry box is the selection.
  1483. #
  1484. proc ::tk::dialog::file::EntFocusIn {w} {
  1485.     upvar ::tk::dialog::file::[winfo name $w] data
  1486.  
  1487.     if {[string compare [$data(ent) get] ""]} {
  1488.     $data(ent) selection range 0 end
  1489.     $data(ent) icursor end
  1490.     } else {
  1491.     $data(ent) selection clear
  1492.     }
  1493.  
  1494.     if { [string equal [winfo class $w] TkFDialog] } {
  1495.     # If this is a File Dialog, make sure the buttons are labeled right.
  1496.     if {[string equal $data(type) open]} {
  1497.         ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  1498.     } else {
  1499.         ::tk::SetAmpText $data(okBtn) [mc "&Save"]
  1500.     }
  1501.     }
  1502. }
  1503.  
  1504. proc ::tk::dialog::file::EntFocusOut {w} {
  1505.     upvar ::tk::dialog::file::[winfo name $w] data
  1506.  
  1507.     $data(ent) selection clear
  1508. }
  1509.  
  1510.  
  1511. # Gets called when user presses Return in the "File name" entry.
  1512. #
  1513. proc ::tk::dialog::file::ActivateEnt {w} {
  1514.     upvar ::tk::dialog::file::[winfo name $w] data
  1515.  
  1516.     set text [$data(ent) get]
  1517.     if {$data(-multiple)} {
  1518.     # For the multiple case we have to be careful to get the file
  1519.     # names as a true list, watching out for a single file with a
  1520.     # space in the name.  Thus we query the IconList directly.
  1521.  
  1522.     set data(selectFile) ""
  1523.     foreach item [::tk::IconList_Curselection $data(icons)] {
  1524.         ::tk::dialog::file::VerifyFileName $w \
  1525.             [::tk::IconList_Get $data(icons) $item]
  1526.     }
  1527.     } else {
  1528.     ::tk::dialog::file::VerifyFileName $w $text
  1529.     }
  1530. }
  1531.  
  1532. # Verification procedure
  1533. #
  1534. proc ::tk::dialog::file::VerifyFileName {w filename} {
  1535.     upvar ::tk::dialog::file::[winfo name $w] data
  1536.  
  1537.     set list [::tk::dialog::file::ResolveFile $data(selectPath) $filename \
  1538.         $data(-defaultextension)]
  1539.     foreach {flag path file} $list {
  1540.     break
  1541.     }
  1542.  
  1543.     switch -- $flag {
  1544.     OK {
  1545.         if {[string equal $file ""]} {
  1546.         # user has entered an existing (sub)directory
  1547.         set data(selectPath) $path
  1548.         $data(ent) delete 0 end
  1549.         } else {
  1550.         ::tk::dialog::file::SetPathSilently $w $path
  1551.         if {$data(-multiple)} {
  1552.             lappend data(selectFile) $file
  1553.         } else {
  1554.             set data(selectFile) $file
  1555.         }
  1556.         ::tk::dialog::file::Done $w
  1557.         }
  1558.     }
  1559.     PATTERN {
  1560.         set data(selectPath) $path
  1561.         set data(filter) $file
  1562.     }
  1563.     FILE {
  1564.         if {[string equal $data(type) open]} {
  1565.         tk_messageBox -icon warning -type ok -parent $w \
  1566.             -message "[mc "File \"%1\$s\"  does not exist." [file join $path $file]]"
  1567.         $data(ent) selection range 0 end
  1568.         $data(ent) icursor end
  1569.         } else {
  1570.         ::tk::dialog::file::SetPathSilently $w $path
  1571.         if {$data(-multiple)} {
  1572.             lappend data(selectFile) $file
  1573.         } else {
  1574.             set data(selectFile) $file
  1575.         }
  1576.         ::tk::dialog::file::Done $w
  1577.         }
  1578.     }
  1579.     PATH {
  1580.         tk_messageBox -icon warning -type ok -parent $w \
  1581.         -message "[mc "Directory \"%1\$s\" does not exist." $path]"
  1582.         $data(ent) selection range 0 end
  1583.         $data(ent) icursor end
  1584.     }
  1585.     CHDIR {
  1586.         tk_messageBox -type ok -parent $w -message \
  1587.            "[mc "Cannot change to the directory \"%1\$s\".\nPermission denied." $path]"\
  1588.         -icon warning
  1589.         $data(ent) selection range 0 end
  1590.         $data(ent) icursor end
  1591.     }
  1592.     ERROR {
  1593.         tk_messageBox -type ok -parent $w -message \
  1594.            "[mc "Invalid file name \"%1\$s\"." $path]"\
  1595.         -icon warning
  1596.         $data(ent) selection range 0 end
  1597.         $data(ent) icursor end
  1598.     }
  1599.     }
  1600. }
  1601.  
  1602. # Gets called when user presses the Alt-s or Alt-o keys.
  1603. #
  1604. proc ::tk::dialog::file::InvokeBtn {w key} {
  1605.     upvar ::tk::dialog::file::[winfo name $w] data
  1606.  
  1607.     if {[string equal [$data(okBtn) cget -text] $key]} {
  1608.     ::tk::ButtonInvoke $data(okBtn)
  1609.     }
  1610. }
  1611.  
  1612. # Gets called when user presses the "parent directory" button
  1613. #
  1614. proc ::tk::dialog::file::UpDirCmd {w} {
  1615.     upvar ::tk::dialog::file::[winfo name $w] data
  1616.  
  1617.     if {[string compare $data(selectPath) "/"]} {
  1618.     set data(selectPath) [file dirname $data(selectPath)]
  1619.     }
  1620. }
  1621.  
  1622. # Join a file name to a path name. The "file join" command will break
  1623. # if the filename begins with ~
  1624. #
  1625. proc ::tk::dialog::file::JoinFile {path file} {
  1626.     if {[string match {~*} $file] && [file exists $path/$file]} {
  1627.     return [file join $path ./$file]
  1628.     } else {
  1629.     return [file join $path $file]
  1630.     }
  1631. }
  1632.  
  1633. # Gets called when user presses the "OK" button
  1634. #
  1635. proc ::tk::dialog::file::OkCmd {w} {
  1636.     upvar ::tk::dialog::file::[winfo name $w] data
  1637.  
  1638.     set filenames {}
  1639.     foreach item [::tk::IconList_Curselection $data(icons)] {
  1640.     lappend filenames [::tk::IconList_Get $data(icons) $item]
  1641.     }
  1642.  
  1643.     if {([llength $filenames] && !$data(-multiple)) || \
  1644.         ($data(-multiple) && ([llength $filenames] == 1))} {
  1645.     set filename [lindex $filenames 0]
  1646.     set file [::tk::dialog::file::JoinFile $data(selectPath) $filename]
  1647.     if {[file isdirectory $file]} {
  1648.         ::tk::dialog::file::ListInvoke $w [list $filename]
  1649.         return
  1650.     }
  1651.     }
  1652.  
  1653.     ::tk::dialog::file::ActivateEnt $w
  1654. }
  1655.  
  1656. # Gets called when user presses the "Cancel" button
  1657. #
  1658. proc ::tk::dialog::file::CancelCmd {w} {
  1659.     upvar ::tk::dialog::file::[winfo name $w] data
  1660.     variable ::tk::Priv
  1661.  
  1662.     set Priv(selectFilePath) ""
  1663. }
  1664.  
  1665. # Gets called when user browses the IconList widget (dragging mouse, arrow
  1666. # keys, etc)
  1667. #
  1668. proc ::tk::dialog::file::ListBrowse {w} {
  1669.     upvar ::tk::dialog::file::[winfo name $w] data
  1670.  
  1671.     set text {}
  1672.     foreach item [::tk::IconList_Curselection $data(icons)] {
  1673.     lappend text [::tk::IconList_Get $data(icons) $item]
  1674.     }
  1675.     if {[llength $text] == 0} {
  1676.     return
  1677.     }
  1678.     if { [llength $text] > 1 } {
  1679.     set newtext {}
  1680.     foreach file $text {
  1681.         set fullfile [::tk::dialog::file::JoinFile $data(selectPath) $file]
  1682.         if { ![file isdirectory $fullfile] } {
  1683.         lappend newtext $file
  1684.         }
  1685.     }
  1686.     set text $newtext
  1687.     set isDir 0
  1688.     } else {
  1689.     set text [lindex $text 0]
  1690.     set file [::tk::dialog::file::JoinFile $data(selectPath) $text]
  1691.     set isDir [file isdirectory $file]
  1692.     }
  1693.     if {!$isDir} {
  1694.     $data(ent) delete 0 end
  1695.     $data(ent) insert 0 $text
  1696.  
  1697.     if { [string equal [winfo class $w] TkFDialog] } {
  1698.         if {[string equal $data(type) open]} {
  1699.         ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  1700.         } else {
  1701.         ::tk::SetAmpText $data(okBtn) [mc "&Save"]
  1702.         }
  1703.     }
  1704.     } else {
  1705.     if { [string equal [winfo class $w] TkFDialog] } {
  1706.         ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  1707.     }
  1708.     }
  1709. }
  1710.  
  1711. # Gets called when user invokes the IconList widget (double-click, 
  1712. # Return key, etc)
  1713. #
  1714. proc ::tk::dialog::file::ListInvoke {w filenames} {
  1715.     upvar ::tk::dialog::file::[winfo name $w] data
  1716.  
  1717.     if {[llength $filenames] == 0} {
  1718.     return
  1719.     }
  1720.  
  1721.     set file [::tk::dialog::file::JoinFile $data(selectPath) \
  1722.         [lindex $filenames 0]]
  1723.     
  1724.     set class [winfo class $w]
  1725.     if {[string equal $class TkChooseDir] || [file isdirectory $file]} {
  1726.     set appPWD [pwd]
  1727.     if {[catch {cd $file}]} {
  1728.         tk_messageBox -type ok -parent $w -message \
  1729.            "[mc "Cannot change to the directory \"%1\$s\".\nPermission denied." $file]"\
  1730.         -icon warning
  1731.     } else {
  1732.         cd $appPWD
  1733.         set data(selectPath) $file
  1734.     }
  1735.     } else {
  1736.     if {$data(-multiple)} {
  1737.         set data(selectFile) $filenames
  1738.     } else {
  1739.         set data(selectFile) $file
  1740.     }
  1741.     ::tk::dialog::file::Done $w
  1742.     }
  1743. }
  1744.  
  1745. # ::tk::dialog::file::Done --
  1746. #
  1747. #    Gets called when user has input a valid filename.  Pops up a
  1748. #    dialog box to confirm selection when necessary. Sets the
  1749. #    tk::Priv(selectFilePath) variable, which will break the "vwait"
  1750. #    loop in ::tk::dialog::file:: and return the selected filename to the
  1751. #    script that calls tk_getOpenFile or tk_getSaveFile
  1752. #
  1753. proc ::tk::dialog::file::Done {w {selectFilePath ""}} {
  1754.     upvar ::tk::dialog::file::[winfo name $w] data
  1755.     variable ::tk::Priv
  1756.  
  1757.     if {[string equal $selectFilePath ""]} {
  1758.     if {$data(-multiple)} {
  1759.         set selectFilePath {}
  1760.         foreach f $data(selectFile) {
  1761.         lappend selectFilePath [::tk::dialog::file::JoinFile \
  1762.             $data(selectPath) $f]
  1763.         }
  1764.     } else {
  1765.         set selectFilePath [::tk::dialog::file::JoinFile \
  1766.             $data(selectPath) $data(selectFile)]
  1767.     }
  1768.     
  1769.     set Priv(selectFile)     $data(selectFile)
  1770.     set Priv(selectPath)     $data(selectPath)
  1771.  
  1772.     if {[string equal $data(type) save]} {
  1773.         if {[file exists $selectFilePath]} {
  1774.         set reply [tk_messageBox -icon warning -type yesno\
  1775.             -parent $w -message \
  1776.             "[mc "File \"%1\$s\" already exists.\nDo you want to overwrite it?" $selectFilePath]"]
  1777.         if {[string equal $reply "no"]} {
  1778.         return
  1779.         }
  1780.         }
  1781.     }
  1782.     }
  1783.     set Priv(selectFilePath) $selectFilePath
  1784. }
  1785.