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