home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Freeware 31 / FreelogHS31.iso / Texte / scribus / scribus-1.3.3.9-win32-install.exe / tcl / tix8.1 / EFileBox.tcl < prev    next >
Text File  |  2001-11-03  |  12KB  |  462 lines

  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. #    $Id: EFileBox.tcl,v 1.3.2.1 2001/11/03 07:29:54 idiscovery Exp $
  4. #
  5. # EFileBox.tcl --
  6. #
  7. #    Implements the Extended File Selection Box widget.
  8. #
  9. # Copyright (c) 1993-1999 Ioi Kim Lam.
  10. # Copyright (c) 2000-2001 Tix Project Group.
  11. #
  12. # See the file "license.terms" for information on usage and redistribution
  13. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14. #
  15.  
  16.  
  17. #
  18. # ToDo
  19. #   (1)    If user has entered an invalid directory, give an error dialog
  20. #
  21.  
  22. tixWidgetClass tixExFileSelectBox {
  23.     -classname TixExFileSelectBox
  24.     -superclass tixPrimitive
  25.     -method {
  26.     filter invoke
  27.     }
  28.     -flag {
  29.     -browsecmd -command -dialog -dir -dircmd -directory 
  30.     -disablecallback -filetypes -pattern -selection -showhidden -value
  31.     }
  32.     -forcecall {
  33.     -filetypes
  34.     }
  35.     -configspec {
  36.     {-browsecmd browseCmd BrowseCmd ""}
  37.     {-command command Command ""}
  38.     {-dialog dialog Dialog ""}
  39.     {-dircmd dirCmd DirCmd ""}
  40.     {-directory directory Directory ""}
  41.     {-disablecallback disableCallback DisableCallback 0 tixVerifyBoolean}
  42.     {-filetypes fileTypes FileTypes ""}
  43.     {-pattern pattern Pattern *}
  44.     {-showhidden showHidden ShowHidden 0 tixVerifyBoolean}
  45.     {-value value Value ""}
  46.     }
  47.     -alias {
  48.     {-dir -directory}
  49.     {-selection -value}
  50.     }
  51.  
  52.     -default {
  53.     {*dir.label             {Directories:}}
  54.     {*dir.editable             true}
  55.     {*dir.history             true}
  56.     {*dir*listbox.height         5}
  57.     {*file.label              Files:}
  58.     {*file.editable         true}
  59.     {*file.history             false}
  60.     {*file*listbox.height         5}
  61.     {*types.label             {List Files of Type:}}
  62.     {*types*listbox.height         3}
  63.     {*TixComboBox.labelSide     top}
  64.     {*TixComboBox*Label.anchor     w}
  65.     {*dir.label.underline         0}
  66.     {*file.label.underline        0}
  67.     {*types.label.underline     14}
  68.     {*TixComboBox.anchor         e}
  69.     {*TixHList.height         7}
  70.     {*filelist*listbox.height     7}
  71.     {*hidden.wrapLength         3c}
  72.     {*hidden.justify         left}
  73.     }
  74. }
  75.  
  76. proc tixExFileSelectBox:InitWidgetRec {w} {
  77.     upvar #0 $w data
  78.     global env
  79.  
  80.     tixChainMethod $w InitWidgetRec
  81.  
  82.     if {$data(-directory) == ""} {
  83.     global env
  84.  
  85.     if {[info exists env(PWD)]} {
  86.         set data(-directory) $env(PWD)
  87.     } else {
  88.         set data(-directory) [pwd]
  89.     }
  90.     }
  91.     set data(oldDir)    ""
  92.     set data(flag)      0
  93. }
  94.  
  95.  
  96. #----------------------------------------------------------------------
  97. #        Construct widget
  98. #----------------------------------------------------------------------
  99. proc tixExFileSelectBox:ConstructWidget {w} {
  100.     upvar #0 $w data
  101.  
  102.     tixChainMethod $w ConstructWidget
  103.  
  104.     # listbox frame
  105.     set lf [frame $w.lf]
  106.  
  107.     # The pane that contains the two listboxes
  108.     #
  109.     set pane  [tixPanedWindow $lf.pane -orientation horizontal]
  110.     set dpane [$pane add 1 -size 160]
  111.     set fpane [$pane add 2 -size 160]
  112.  
  113.     $dpane config -relief flat
  114.     $fpane config -relief flat
  115.  
  116.     # The File List Pane
  117.     #
  118.     set data(w:file)  [tixComboBox $fpane.file\
  119.     -command "tixExFileSelectBox:Cmd-FileCombo $w"\
  120.     -prunehistory true \
  121.     -options { \
  122.         label.anchor w \
  123.     }]
  124.     set data(w:filelist) [tixScrolledListBox $fpane.filelist \
  125.     -command "tixExFileSelectBox:Cmd-FileList $w 1"\
  126.     -browsecmd "tixExFileSelectBox:Cmd-FileList $w 0"]
  127.     pack $data(w:file)  -padx 8 -pady 4 -side top -fill x
  128.     pack $data(w:filelist) -padx 8 -pady 4 -side top -fill both -expand yes
  129.  
  130.     # The Directory Pane
  131.     #
  132.     set data(w:dir)   [tixComboBox $dpane.dir \
  133.     -command "tixExFileSelectBox:Cmd-DirCombo $w"\
  134.     -prunehistory true \
  135.     -options { \
  136.         label.anchor w \
  137.     }]
  138.     set data(w:dirlist) [tixDirList  $dpane.dirlist \
  139.     -command "tixExFileSelectBox:Cmd-DirList $w"\
  140.     -browsecmd "tixExFileSelectBox:Browse-DirList $w"]
  141.     pack $data(w:dir)   -padx 8 -pady 4 -side top -fill x
  142.     pack $data(w:dirlist) -padx 8 -pady 4 -side top -fill both -expand yes
  143.  
  144.     # The file types listbox
  145.     #
  146.     set data(w:types) [tixComboBox $lf.types\
  147.     -command "tixExFileSelectBox:Cmd-TypeCombo $w" \
  148.     -options { \
  149.         label.anchor w \
  150.     }]
  151.  
  152.     pack $data(w:types)  -padx 12 -pady 4 -side bottom -fill x -anchor w
  153.     pack $pane -side top -padx 4 -pady 4 -expand yes -fill both
  154.  
  155.     # Buttons to the right
  156.     #
  157.     set bf [frame $w.bf]
  158.     set data(w:ok)     [button $bf.ok -text Ok -width 6 \
  159.     -underline 0 -command [list tixExFileSelectBox:Ok $w]]
  160.     set data(w:cancel) [button $bf.cancel -text Cancel -width 6 \
  161.     -underline 0 -command [list tixExFileSelectBox:Cancel $w]]
  162.     set data(w:hidden) [checkbutton $bf.hidden -text "Show Hidden Files"\
  163.     -underline 0\
  164.            -variable [format %s(-showhidden) $w] -onvalue 1 -offvalue 0\
  165.     -command [list tixExFileSelectBox:SetShowHidden $w]]
  166.  
  167.     pack $data(w:ok) $data(w:cancel) $data(w:hidden)\
  168.     -side top -fill x -padx 6 -pady 3
  169.  
  170.     pack $bf -side right -fill y -pady 6
  171.     pack $lf -side left -expand yes -fill both
  172.  
  173.     tixDoWhenMapped $w "tixExFileSelectBox:Map $w"
  174.  
  175.     if {$data(-filetypes) == ""} {
  176.     $data(w:types) config -state disabled
  177.     }
  178. }
  179.  
  180.  
  181. #----------------------------------------------------------------------
  182. # Configuration
  183. #----------------------------------------------------------------------
  184. proc tixExFileSelectBox:config-showhidden {w value} {
  185.     upvar #0 $w data
  186.  
  187.     set data(-showhidden) $value
  188.     tixExFileSelectBox:SetShowHidden $w
  189. }
  190.  
  191. # Update both DirList and {file list and dir combo}
  192. #
  193. #
  194. proc tixExFileSelectBox:config-directory {w value} {
  195.     upvar #0 $w data
  196.  
  197.     if {![tixIsAbsPath $value]} {
  198.     return $data(-directory)
  199.     }
  200.  
  201.     set data(-directory) [tixFSAbsPath $value]
  202.     tixSetSilent $data(w:dirlist) $data(-directory) 
  203.     tixSetSilent $data(w:dir) $data(-directory) 
  204.     tixWidgetDoWhenIdle tixExFileSelectBox:LoadFiles $w reload
  205.  
  206.     return $data(-directory) 
  207. }
  208.  
  209. proc tixExFileSelectBox:config-filetypes {w value} {
  210.     upvar #0 $w data
  211.  
  212.     $data(w:types) subwidget listbox delete 0 end
  213.  
  214.     foreach name [array names data] {
  215.     if {[string match type,* $name]} {
  216.         catch {unset data($name)}
  217.     }
  218.     }
  219.  
  220.     if {$value == ""} {
  221.     $data(w:types) config -state disabled
  222.     } else {
  223.     $data(w:types) config -state normal
  224.     
  225.     foreach type $value {
  226.         $data(w:types) insert end [lindex $type 1]
  227.         set data(type,[lindex $type 1]) [lindex $type 0]
  228.     }
  229.     tixSetSilent $data(w:types) ""
  230.     }
  231. }
  232.  
  233. #----------------------------------------------------------------------
  234. # MISC Methods
  235. #----------------------------------------------------------------------
  236. proc tixExFileSelectBox:SetShowHidden {w} {
  237.     upvar #0 $w data
  238.  
  239.     $data(w:dirlist) config -showhidden $data(-showhidden)
  240.  
  241.     tixWidgetDoWhenIdle tixExFileSelectBox:LoadFiles $w reload
  242. }
  243.  
  244. # User activates the dir combobox
  245. #
  246. #
  247. proc tixExFileSelectBox:Cmd-DirCombo {w args} {
  248.     upvar #0 $w data
  249.  
  250.     set dir [tixEvent flag V]
  251.     if {![tixIsAbsPath $dir]} {
  252.     return
  253.     }
  254.     set dir [tixFSAbsPath $dir]
  255.  
  256.     if {![file isdirectory $dir]} {
  257.     return
  258.     }
  259.  
  260.     $data(w:dirlist) config -value $dir
  261.     set data(-directory) $dir
  262. }
  263.  
  264. # User activates the dir list
  265. #
  266. #
  267. proc tixExFileSelectBox:Cmd-DirList {w args} {
  268.     upvar #0 $w data
  269.  
  270.     set dir $data(-directory)
  271.     catch {
  272.     set dir [tixEvent flag V]
  273.     }
  274.     set dir [tixFSAbsPath $dir]
  275.  
  276.     tixSetSilent $data(w:dir) $dir
  277.     set data(-directory) $dir
  278.  
  279.     tixWidgetDoWhenIdle tixExFileSelectBox:LoadFiles $w noreload
  280. }
  281.  
  282. # User activates the dir list
  283. #
  284. #
  285. proc tixExFileSelectBox:Browse-DirList {w args} {
  286.     upvar #0 $w data
  287.  
  288.     set dir [tixEvent flag V]
  289.     tixExFileSelectBox:Cmd-DirList $w $dir
  290. }
  291.  
  292. proc tixExFileSelectBox:IsPattern {w string} {
  293.     foreach char [split $string ""] {
  294.     if {$char == "*" || $char == "?" || $char == "\{"  || $char == "\[" } {
  295.         return 1
  296.     }
  297.     }
  298.     return 0
  299. }
  300.  
  301. proc tixExFileSelectBox:Cmd-FileCombo {w value} {
  302.     upvar #0 $w data
  303.  
  304.     if {[tixEvent type] == "<Return>"} {
  305.     tixExFileSelectBox:Ok $w
  306.     }
  307. }
  308.  
  309. proc tixExFileSelectBox:Ok {w} {
  310.     upvar #0 $w data
  311.  
  312.     set value [string trim [$data(w:file) subwidget entry get]]
  313.     if {$value == ""} {
  314.     set value $data(-pattern)
  315.     }
  316.     tixSetSilent $data(w:file) $value
  317.  
  318.     if {[tixExFileSelectBox:IsPattern $w $value]} {
  319.     set data(-pattern) $value
  320.     tixWidgetDoWhenIdle tixExFileSelectBox:LoadFiles $w reload
  321.     } else {
  322.     if {[tixIsAbsPath $value]} {
  323.         set intName [tixFileIntName $value]
  324.     } else {
  325.         set intName [tixSubFolder [tixFileIntName $data(-directory)] \
  326.             [tixFileIntName $value]]
  327.     }
  328.     set data(-value) [tixNativeName $intName]
  329.     tixExFileSelectBox:Invoke $w
  330.     }
  331. }
  332.  
  333. proc tixExFileSelectBox:Cancel {w} {
  334.     upvar #0 $w data
  335.  
  336.     if {$data(-dialog) != ""} {
  337.     eval $data(-dialog) popdown
  338.     }
  339. }
  340.  
  341. proc tixExFileSelectBox:Invoke {w} {
  342.     upvar #0 $w data
  343.  
  344.     # Save some old history
  345.     #
  346.     $data(w:dir)  addhistory [$data(w:dir) cget -value]
  347.     $data(w:file) addhistory $data(-pattern)
  348.     $data(w:file) addhistory $data(-value)
  349.     if {$data(-dialog) != ""} {
  350.     eval $data(-dialog) popdown
  351.     }
  352.     if {$data(-command) != "" && !$data(-disablecallback)} {
  353.     set bind(specs) "%V"
  354.     set bind(%V) $data(-value)
  355.     tixEvalCmdBinding $w $data(-command) bind $data(-value)
  356.     }
  357. }
  358.  
  359. proc tixExFileSelectBox:Cmd-FileList {w invoke args} {
  360.     upvar #0 $w data
  361.  
  362.     set index [lindex [$data(w:filelist) subwidget listbox curselection] 0]
  363.     if {$index == ""} {
  364.     set index 0
  365.     }
  366.  
  367.     set file [$data(w:filelist) subwidget listbox get $index]
  368.     tixSetSilent $data(w:file) $file
  369.  
  370.     set data(-value) [tixNativeName [tixSubFolder \
  371.     [tixFileIntName $data(-directory)] [tixFileIntName $file]]]
  372.  
  373.     if {$invoke == 1} {
  374.     tixExFileSelectBox:Invoke $w
  375.     } else {
  376.     if {$data(-browsecmd) != ""} {
  377.         tixEvalCmdBinding $w $data(-browsecmd) "" $data(-value)
  378.     }
  379.     }
  380. }
  381.  
  382. proc tixExFileSelectBox:Cmd-TypeCombo {w args} {
  383.     upvar #0 $w data
  384.  
  385.     set value [tixEvent flag V]
  386.  
  387.     if {[info exists data(type,$value)]} {
  388.     set data(-pattern) $data(type,$value)
  389.     tixSetSilent $data(w:file) $data(-pattern)
  390.     tixWidgetDoWhenIdle tixExFileSelectBox:LoadFiles $w reload
  391.     }
  392. }
  393.  
  394. proc tixExFileSelectBox:LoadFiles {w flag} {
  395.     upvar #0 $w data
  396.  
  397.     if {$flag != "reload" && $data(-directory) == $data(oldDir)} {
  398.     return
  399.     }
  400.  
  401.     if {![winfo ismapped [winfo toplevel $w]]} {
  402.     tixDoWhenMapped [winfo toplevel $w] \
  403.         "tixExFileSelectBox:LoadFiles $w $flag"
  404.     return
  405.     }
  406.  
  407.     set listbox [$data(w:filelist) subwidget listbox]
  408.     $listbox delete 0 end
  409.  
  410.     set data(-value) ""
  411.  
  412.     tixBusy $w on [$data(w:dirlist) subwidget hlist]
  413.  
  414.     # wrap in a catch so you can't get stuck in a Busy state
  415.     if {[catch {
  416.     set intDir [tixFileIntName $data(-directory)]
  417.     foreach name [tixListDir $intDir 0 1 0 \
  418.         $data(-showhidden) $data(-pattern)] {
  419.         
  420.         $listbox insert end [tixFileDisplayName [tixSubFolder $intDir $name]]
  421.     }
  422.  
  423.     if {$data(oldDir) != $data(-directory)} {
  424.         # Otherwise if the user has already selected a file and then presses
  425.         # "show hidden", the selection won't be wiped out.
  426.         tixSetSilent $data(w:file) $data(-pattern)
  427.     }
  428.     } err]} {
  429.     tixDebug "tixExFileSelectBox:LoadFiles error for $w\n$err"
  430.     }
  431.     set data(oldDir) $data(-directory)
  432.  
  433.     tixWidgetDoWhenIdle tixBusy $w off [$data(w:dirlist) subwidget hlist]
  434. }
  435.  
  436. #
  437. # Called when thd listbox is first mapped
  438. proc tixExFileSelectBox:Map {w} {
  439.     if {![winfo exists $w]} {
  440.     return
  441.     }
  442.     upvar #0 $w data
  443.  
  444.     set bind(specs) "%V"
  445.     set bind(%V) $data(-value)
  446.     tixEvalCmdBinding $w bind \
  447.     "tixExFileSelectBox:Cmd-DirList $w" $data(-directory)
  448. }
  449.  
  450. #----------------------------------------------------------------------
  451. # Public commands
  452. #
  453. #----------------------------------------------------------------------
  454. proc tixExFileSelectBox:invoke {w} {
  455.     tixExFileSelectBox:Invoke $w
  456. }
  457.  
  458. proc tixExFileSelectBox:filter {w} {
  459.     tixExFileSelectBox:LoadFiles $w reload
  460. }
  461.  
  462.