ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/getfile3.6.tcl
Revision: 2.6
Committed: Wed Nov 30 16:04:39 1994 UTC (29 years, 5 months ago) by greg
Content type: application/x-tcl
Branch: MAIN
Changes since 2.5: +1 -1 lines
Log Message:
fixed error for empty directories

File Contents

# User Rev Content
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     $w.fm.fl delete 0 end
176     set ls ../
177 greg 2.6 foreach f [glob -nocomplain *] {
178 greg 2.1 if [file isdirectory $f] {
179     lappend ls $f/
180     }
181     }
182     foreach f [eval glob -nocomplain [split $curpat]] {
183     if [file isfile $f] {
184     lappend ls $f
185     }
186     }
187     eval $w.fm.fl insert end [lsort $ls]
188     }
189    
190    
191     proc set_curfile {w {sp ""}} { # change current file selection
192     global curdir curfile
193     set f [$w.fm.fl get [$w.fm.fl curselection]]
194     if [string match */ $f] {
195     set curdir [string range $f 0 [expr [string length $f]-2]]
196     update_dir $w
197     } else {
198     set curfile $f
199     if {"$sp" > ""} {
200     $sp $curdir/$curfile
201     }
202     }
203     }
204    
205    
206     proc chk_select w { # check if current selection is file
207     set s [$w.fm.fl curselection]
208     if {"$s" > "" && [file isfile [$w.fm.fl get $s]]} {
209     $w.vi configure -state normal
210     } else {
211     $w.vi configure -state disabled
212     }
213     }