| 1 |
greg |
2.1 |
# SCCSid "$SunId$ LBL" |
| 2 |
|
|
# |
| 3 |
greg |
2.5 |
# Usage: getfile [-win w] [-grab] [-perm] [-glob pattern] [-view proc] [-send proc] |
| 4 |
greg |
2.1 |
# |
| 5 |
|
|
# Create a dialog box (in window w if given) to get file name. |
| 6 |
greg |
2.5 |
# If -grab option is given, then getfile does a local grab on its window. |
| 7 |
greg |
2.1 |
# Normally, a single file name and return as value. |
| 8 |
|
|
# If perm switch is given, keep window up for multiple file entries. |
| 9 |
|
|
# If pattern is given, start with list of all the specified files, |
| 10 |
|
|
# breaking the string into directory and file match parts. |
| 11 |
|
|
# If a view procedure is given, a separate "View" button appears |
| 12 |
|
|
# for allowing the user to preview a selected file. |
| 13 |
|
|
# If a send procedure is given, a file name is sent upon |
| 14 |
|
|
# each double-click, and no File entry is displayed. |
| 15 |
|
|
# |
| 16 |
|
|
|
| 17 |
|
|
proc getfile args { # get filename interactively |
| 18 |
|
|
# Create necessary globals |
| 19 |
|
|
global curdir curfile curpat |
| 20 |
|
|
# Set defaults |
| 21 |
|
|
set w .fpwin |
| 22 |
|
|
set topwin 1 |
| 23 |
greg |
2.5 |
set dograb 0 |
| 24 |
greg |
2.1 |
set curdir . |
| 25 |
|
|
set curpat * |
| 26 |
|
|
set transient 1 |
| 27 |
|
|
# Get options |
| 28 |
|
|
while {[llength $args]} { |
| 29 |
|
|
switch -glob -- [lindex $args 0] { |
| 30 |
|
|
-win* { |
| 31 |
|
|
set w [lindex $args 1] |
| 32 |
|
|
set topwin 0 |
| 33 |
|
|
set args [lreplace $args 1 1] |
| 34 |
|
|
} |
| 35 |
greg |
2.5 |
-grab { |
| 36 |
|
|
set dograb 1 |
| 37 |
|
|
} |
| 38 |
greg |
2.1 |
-perm* { |
| 39 |
|
|
set transient 0 |
| 40 |
|
|
} |
| 41 |
|
|
-glob { |
| 42 |
|
|
set curdir [file dirname [lindex $args 1]] |
| 43 |
|
|
set curpat [file tail [lindex $args 1]] |
| 44 |
|
|
set args [lreplace $args 1 1] |
| 45 |
|
|
} |
| 46 |
|
|
-vi* { |
| 47 |
|
|
set viewproc [lindex $args 1] |
| 48 |
|
|
set args [lreplace $args 1 1] |
| 49 |
|
|
} |
| 50 |
|
|
-send { |
| 51 |
|
|
set sendproc [lindex $args 1] |
| 52 |
|
|
set args [lreplace $args 1 1] |
| 53 |
|
|
} |
| 54 |
|
|
default { error "bad argument to getfile: [lindex $args 0]" } |
| 55 |
|
|
} |
| 56 |
|
|
set args [lreplace $args 0 0] |
| 57 |
|
|
} |
| 58 |
|
|
# Save initial directory |
| 59 |
|
|
set startdir [pwd] |
| 60 |
|
|
# Create widgets |
| 61 |
|
|
catch {destroy $w} |
| 62 |
|
|
if $topwin { |
| 63 |
greg |
2.2 |
toplevel $w -geometry 400x410 |
| 64 |
greg |
2.1 |
wm title $w "File Picker" |
| 65 |
|
|
wm iconname $w "Files" |
| 66 |
greg |
2.2 |
wm minsize $w 400 300 |
| 67 |
greg |
2.1 |
} else { |
| 68 |
greg |
2.2 |
frame $w -geometry 400x410 |
| 69 |
greg |
2.1 |
pack $w |
| 70 |
|
|
} |
| 71 |
greg |
2.5 |
if $dograb { grab $w } |
| 72 |
|
|
$w configure -cursor top_left_arrow |
| 73 |
greg |
2.1 |
label $w.dt -text Directory: |
| 74 |
greg |
2.4 |
place $w.dt -relx .025 -rely .03125 |
| 75 |
greg |
2.1 |
helplink $w.dt file directory intro |
| 76 |
|
|
button $w.ls -text List -command "update_dir $w" |
| 77 |
greg |
2.4 |
place $w.ls -relx .025 -rely .125 -relwidth .15 -relheight .0625 |
| 78 |
greg |
2.1 |
helplink $w.ls file directory list |
| 79 |
|
|
entry $w.de -textvariable curdir -relief sunken |
| 80 |
|
|
place $w.de -relx .25 -rely .03125 -relwidth .6875 -relheight .0625 |
| 81 |
|
|
focus $w.de |
| 82 |
|
|
helplink $w.de file directory intro |
| 83 |
|
|
bind $w.de <Return> "update_dir $w" |
| 84 |
|
|
entry $w.pw -relief sunken -textvariable curpat |
| 85 |
|
|
place $w.pw -relx .25 -rely .125 -relwidth .34375 -relheight .0625 |
| 86 |
|
|
bind $w.pw <Return> "update_dir $w" |
| 87 |
|
|
helplink $w.pw file directory match |
| 88 |
|
|
frame $w.fm |
| 89 |
|
|
scrollbar $w.fm.sb -relief sunken -command "$w.fm.fl yview" |
| 90 |
|
|
listbox $w.fm.fl -relief sunken -yscroll "$w.fm.sb set" |
| 91 |
|
|
pack $w.fm.sb -side right -fill y |
| 92 |
|
|
pack $w.fm.fl -side left -expand yes -fill both |
| 93 |
|
|
place $w.fm -relx .25 -rely .21875 -relwidth .6875 -relheight .625 |
| 94 |
|
|
tk_listboxSingleSelect $w.fm.fl |
| 95 |
|
|
helplink $w.fm.fl file file intro |
| 96 |
|
|
if [info exists sendproc] { |
| 97 |
|
|
bind $w.fm.fl <Double-Button-1> "set_curfile $w $sendproc" |
| 98 |
|
|
if {$transient} { |
| 99 |
|
|
bind $w.fm.fl <Double-Button-1> \ |
| 100 |
|
|
"+if {\$curfile > {}} {destroy $w}" |
| 101 |
|
|
} |
| 102 |
|
|
} else { |
| 103 |
|
|
bind $w.fm.fl <Double-Button-1> "set_curfile $w" |
| 104 |
|
|
label $w.fl -text File: |
| 105 |
greg |
2.4 |
place $w.fl -relx .10625 -rely .875 |
| 106 |
greg |
2.1 |
entry $w.fn -relief sunken -textvariable curfile |
| 107 |
|
|
place $w.fn -relx .25 -rely .875 \ |
| 108 |
|
|
-relwidth .6875 -relheight .0625 |
| 109 |
|
|
focus $w.fn |
| 110 |
|
|
helplink $w.fn file file entry |
| 111 |
|
|
if $transient { |
| 112 |
|
|
bind $w.fn <Return> "destroy $w" |
| 113 |
|
|
} else { |
| 114 |
|
|
bind $w.fn <Return> {} |
| 115 |
|
|
} |
| 116 |
|
|
} |
| 117 |
|
|
if {$transient != [info exists sendproc]} { |
| 118 |
|
|
button $w.ok -text OK -command "destroy $w" |
| 119 |
greg |
2.4 |
place $w.ok -relx .025 -rely .28125 \ |
| 120 |
|
|
-relwidth .15 -relheight .0625 |
| 121 |
greg |
2.1 |
} |
| 122 |
|
|
if {$transient || [info exists sendproc]} { |
| 123 |
|
|
button $w.cancel -text Cancel \ |
| 124 |
|
|
-command "destroy $w; unset curdir" |
| 125 |
greg |
2.4 |
place $w.cancel -relx .025 -rely .375 \ |
| 126 |
|
|
-relwidth .15 -relheight .0625 |
| 127 |
greg |
2.1 |
} |
| 128 |
|
|
if [info exists viewproc] { |
| 129 |
|
|
button $w.vi -text View -state disabled \ |
| 130 |
|
|
-command "$viewproc \[$w.fm.fl get \[$w.fm.fl curselection\]\]" |
| 131 |
greg |
2.4 |
place $w.vi -relx .025 -rely .46875 \ |
| 132 |
|
|
-relwidth .15 -relheight .0625 |
| 133 |
greg |
2.1 |
bind $w.fm.fl <ButtonRelease-1> "+chk_select $w" |
| 134 |
|
|
helplink $w.vi file file view |
| 135 |
|
|
} |
| 136 |
|
|
# Load current directory |
| 137 |
|
|
update_dir $w |
| 138 |
|
|
if $transient { |
| 139 |
|
|
tkwait window $w |
| 140 |
|
|
cd $startdir |
| 141 |
|
|
if [info exists sendproc] { |
| 142 |
|
|
return [info exists curdir] |
| 143 |
|
|
} |
| 144 |
|
|
if [info exists curdir] { |
| 145 |
|
|
return $curdir/$curfile |
| 146 |
|
|
} |
| 147 |
|
|
return |
| 148 |
|
|
} elseif {[info exists sendproc]} { |
| 149 |
|
|
tkwait window $w |
| 150 |
|
|
cd $startdir |
| 151 |
|
|
if [info exists curdir] { |
| 152 |
|
|
return 1 |
| 153 |
|
|
} |
| 154 |
|
|
return 0 |
| 155 |
|
|
} |
| 156 |
|
|
# Else return the dialog window |
| 157 |
|
|
return $w |
| 158 |
|
|
} |
| 159 |
|
|
|
| 160 |
|
|
|
| 161 |
|
|
proc update_dir w { # Update working directory |
| 162 |
|
|
global curdir curpat |
| 163 |
|
|
if {"$curpat" == ""} { |
| 164 |
|
|
set curpat * |
| 165 |
|
|
} |
| 166 |
greg |
2.3 |
if {"$curdir" == {}} {set curdir {~}} |
| 167 |
greg |
2.1 |
if [catch {cd $curdir} curdir] { |
| 168 |
|
|
set oldcol [lindex [$w.de config -foreground] 4] |
| 169 |
|
|
$w.de config -foreground Red |
| 170 |
|
|
update idletasks |
| 171 |
|
|
after 1500 |
| 172 |
|
|
$w.de config -foreground $oldcol |
| 173 |
|
|
} |
| 174 |
|
|
set curdir [pwd] |
| 175 |
greg |
2.7 |
set offset [expr [string length $curdir] - 32] |
| 176 |
|
|
if {$offset > 0} { |
| 177 |
|
|
$w.de view [expr $offset + \ |
| 178 |
|
|
[string first / [string range $curdir $offset end]] \ |
| 179 |
|
|
+ 1] |
| 180 |
|
|
} |
| 181 |
greg |
2.1 |
$w.fm.fl delete 0 end |
| 182 |
|
|
set ls ../ |
| 183 |
greg |
2.6 |
foreach f [glob -nocomplain *] { |
| 184 |
greg |
2.1 |
if [file isdirectory $f] { |
| 185 |
|
|
lappend ls $f/ |
| 186 |
|
|
} |
| 187 |
|
|
} |
| 188 |
|
|
foreach f [eval glob -nocomplain [split $curpat]] { |
| 189 |
|
|
if [file isfile $f] { |
| 190 |
|
|
lappend ls $f |
| 191 |
|
|
} |
| 192 |
|
|
} |
| 193 |
|
|
eval $w.fm.fl insert end [lsort $ls] |
| 194 |
|
|
} |
| 195 |
|
|
|
| 196 |
|
|
|
| 197 |
|
|
proc set_curfile {w {sp ""}} { # change current file selection |
| 198 |
|
|
global curdir curfile |
| 199 |
|
|
set f [$w.fm.fl get [$w.fm.fl curselection]] |
| 200 |
|
|
if [string match */ $f] { |
| 201 |
|
|
set curdir [string range $f 0 [expr [string length $f]-2]] |
| 202 |
|
|
update_dir $w |
| 203 |
|
|
} else { |
| 204 |
|
|
set curfile $f |
| 205 |
|
|
if {"$sp" > ""} { |
| 206 |
|
|
$sp $curdir/$curfile |
| 207 |
|
|
} |
| 208 |
|
|
} |
| 209 |
|
|
} |
| 210 |
|
|
|
| 211 |
|
|
|
| 212 |
|
|
proc chk_select w { # check if current selection is file |
| 213 |
|
|
set s [$w.fm.fl curselection] |
| 214 |
|
|
if {"$s" > "" && [file isfile [$w.fm.fl get $s]]} { |
| 215 |
|
|
$w.vi configure -state normal |
| 216 |
|
|
} else { |
| 217 |
|
|
$w.vi configure -state disabled |
| 218 |
|
|
} |
| 219 |
|
|
} |