3 # Choose directory dialog implementation for Unix/Mac.
5 # Copyright (c) 1998-2000 by Scriptics Corporation.
8 # Make sure the tk::dialog namespace, in which all dialogs should live, exists
9 namespace eval ::tk::dialog {}
10 namespace eval ::tk::dialog::file {}
12 # Make the chooseDir namespace inside the dialog namespace
13 namespace eval ::tk::dialog::file::chooseDir {
14 namespace import
-force ::tk::msgcat::*
17 # ::tk::dialog::file::chooseDir:: --
19 # Implements the TK directory selection dialog.
22 # args Options parsed by the procedure.
24 proc ::tk::dialog::file::chooseDir:: {args
} {
26 set dataName __tk_choosedir
27 upvar ::tk::dialog::file::$dataName data
28 Config
$dataName $args
30 if {$data(-parent) eq
"."} {
33 set w
$data(-parent).
$dataName
36 # (re)create the dialog box if necessary
38 if {![winfo exists
$w]} {
39 ::tk::dialog::file::Create $w TkChooseDir
40 } elseif
{[winfo class
$w] ne
"TkChooseDir"} {
42 ::tk::dialog::file::Create $w TkChooseDir
44 set data
(dirMenuBtn
) $w.contents.f1.
menu
45 set data
(dirMenu
) $w.contents.f1.
menu.
menu
46 set data
(upBtn
) $w.contents.f1.up
47 set data
(icons
) $w.contents.icons
48 set data
(ent
) $w.contents.f2.ent
49 set data
(okBtn
) $w.contents.f2.ok
50 set data
(cancelBtn
) $w.contents.f2.cancel
51 set data
(hiddenBtn
) $w.contents.f2.hidden
53 if {$::tk::dialog::file::showHiddenBtn} {
54 $data(hiddenBtn
) configure
-state normal
57 $data(hiddenBtn
) configure
-state disabled
58 grid remove
$data(hiddenBtn
)
61 # When using -mustexist, manage the OK button state for validity
62 $data(okBtn
) configure
-state normal
63 if {$data(-mustexist)} {
64 $data(ent
) configure
-validate key
\
65 -validatecommand [list ::tk::dialog::file::chooseDir::IsOK?
$w %P
]
67 $data(ent
) configure
-validate none
70 # Dialog boxes should be transient with respect to their parent,
71 # so that they will always stay on top of their parent window. However,
72 # some window managers will create the window as withdrawn if the parent
73 # window is withdrawn or iconified. Combined with the grab we put on the
74 # window, this can hang the entire application. Therefore we only make
75 # the dialog transient if the parent is viewable.
77 if {[winfo viewable
[winfo toplevel $data(-parent)]] } {
78 wm transient
$w $data(-parent)
81 trace add
variable data
(selectPath
) write
\
82 [list ::tk::dialog::file::SetPath $w]
83 $data(dirMenuBtn
) configure
\
84 -textvariable ::tk::dialog::file::${dataName
}(selectPath
)
87 set data
(previousEntryText
) ""
88 ::tk::dialog::file::UpdateWhenIdle $w
90 # Withdraw the window, then update all the geometry information
91 # so we know how big it wants to be, then center the window in the
92 # display and de-iconify it.
94 ::tk::PlaceWindow $w widget
$data(-parent)
95 wm title
$w $data(-title)
97 # Set a grab and claim the focus too.
99 ::tk::SetFocusGrab $w $data(ent
)
100 $data(ent
) delete
0 end
101 $data(ent
) insert
0 $data(selectPath
)
102 $data(ent
) selection range
0 end
103 $data(ent
) icursor end
105 # Wait for the user to respond, then restore the focus and
106 # return the index of the selected button. Restore the focus
107 # before deleting the window, since otherwise the window manager
108 # may take the focus away so we can't redirect it. Finally,
109 # restore any grab that was in effect.
111 vwait ::tk::Priv(selectFilePath
)
113 ::tk::RestoreFocusGrab $w $data(ent
) withdraw
115 # Cleanup traces on selectPath variable
118 foreach trace [trace info variable data
(selectPath
)] {
119 trace remove
variable data
(selectPath
) [lindex $trace 0] [lindex $trace 1]
121 $data(dirMenuBtn
) configure
-textvariable {}
123 # Return value to user
126 return $Priv(selectFilePath
)
129 # ::tk::dialog::file::chooseDir::Config --
131 # Configures the Tk choosedir dialog according to the argument list
133 proc ::tk::dialog::file::chooseDir::Config {dataName argList
} {
134 upvar ::tk::dialog::file::$dataName data
136 # 0: Delete all variable that were set on data(selectPath) the
137 # last time the file dialog is used. The traces may cause troubles
138 # if the dialog is now used with a different -parent option.
140 foreach trace [trace info variable data
(selectPath
)] {
141 trace remove
variable data
(selectPath
) [lindex $trace 0] [lindex $trace 1]
144 # 1: the configuration specs
148 {-initialdir "" "" ""}
153 # 2: default values depending on the type of the dialog
155 if {![info exists data
(selectPath
)]} {
156 # first time the dialog has been popped up
157 set data
(selectPath
) [pwd]
160 # 3: parse the arguments
162 tclParseConfigSpec
::tk::dialog::file::$dataName $specs "" $argList
164 if {$data(-title) eq
""} {
165 set data
(-title) "[mc "Choose Directory
"]"
168 # Stub out the -multiple value for the dialog; it doesn't make sense for
169 # choose directory dialogs, but we have to have something there because we
170 # share so much code with the file dialogs.
171 set data
(-multiple) 0
173 # 4: set the default directory and selection according to the -initial
176 if {$data(-initialdir) ne
""} {
177 # Ensure that initialdir is an absolute path name.
178 if {[file isdirectory
$data(-initialdir)]} {
180 cd $data(-initialdir)
181 set data
(selectPath
) [pwd]
184 set data
(selectPath
) [pwd]
188 if {![winfo exists
$data(-parent)]} {
189 error "bad window path name \"$data(-parent)\""
193 # Gets called when user presses Return in the "Selection" entry or presses OK.
195 proc ::tk::dialog::file::chooseDir::OkCmd {w
} {
196 upvar ::tk::dialog::file::[winfo name
$w] data
198 # This is the brains behind selecting non-existant directories. Here's
200 # 1. If the icon list has a selection, join it with the current dir,
201 # and return that value.
202 # 1a. If the icon list does not have a selection ...
203 # 2. If the entry is empty, do nothing.
204 # 3. If the entry contains an invalid directory, then...
205 # 3a. If the value is the same as last time through here, end dialog.
206 # 3b. If the value is different than last time, save it and return.
207 # 4. If entry contains a valid directory, then...
208 # 4a. If the value is the same as the current directory, end dialog.
209 # 4b. If the value is different from the current directory, change to
212 set selection [tk::IconList_CurSelection $data(icons
)]
213 if {[llength $selection] != 0} {
214 set iconText
[tk::IconList_Get $data(icons
) [lindex $selection 0]]
215 set iconText
[file join $data(selectPath
) $iconText]
218 set text [$data(ent
) get
]
222 set text [file join {*}[file split [string trim
$text]]]
223 if {![file exists
$text] ||
![file isdirectory
$text]} {
224 # Entry contains an invalid directory. If it's the same as the
225 # last time they came through here, reset the saved value and end
226 # the dialog. Otherwise, save the value (so we can do this test
228 if {$text eq
$data(previousEntryText
)} {
229 set data
(previousEntryText
) ""
232 set data
(previousEntryText
) $text
235 # Entry contains a valid directory. If it is the same as the
236 # current directory, end the dialog. Otherwise, change to that
238 if {$text eq
$data(selectPath
)} {
241 set data
(selectPath
) $text
248 # Change state of OK button to match -mustexist correctness of entry
250 proc ::tk::dialog::file::chooseDir::IsOK?
{w
text} {
251 upvar ::tk::dialog::file::[winfo name
$w] data
253 set ok
[file isdirectory
$text]
254 $data(okBtn
) configure
-state [expr {$ok ?
"normal" : "disabled"}]
260 proc ::tk::dialog::file::chooseDir::DblClick {w
} {
261 upvar ::tk::dialog::file::[winfo name
$w] data
262 set selection [tk::IconList_CurSelection $data(icons
)]
263 if {[llength $selection] != 0} {
264 set filenameFragment
\
265 [tk::IconList_Get $data(icons
) [lindex $selection 0]]
266 set file $data(selectPath
)
267 if {[file isdirectory
$file]} {
268 ::tk::dialog::file::ListInvoke $w [list $filenameFragment]
274 # Gets called when user browses the IconList widget (dragging mouse, arrow
277 proc ::tk::dialog::file::chooseDir::ListBrowse {w
text} {
278 upvar ::tk::dialog::file::[winfo name
$w] data
284 set file [::tk::dialog::file::JoinFile $data(selectPath
) $text]
285 $data(ent
) delete
0 end
286 $data(ent
) insert
0 $file
289 # ::tk::dialog::file::chooseDir::Done --
291 # Gets called when user has input a valid filename. Pops up a
292 # dialog box to confirm selection when necessary. Sets the
293 # Priv(selectFilePath) variable, which will break the "vwait"
294 # loop in tk_chooseDirectory and return the selected filename to the
295 # script that calls tk_getOpenFile or tk_getSaveFile
297 proc ::tk::dialog::file::chooseDir::Done {w
{selectFilePath
""}} {
298 upvar ::tk::dialog::file::[winfo name
$w] data
301 if {$selectFilePath eq
""} {
302 set selectFilePath
$data(selectPath
)
304 if {$data(-mustexist) && ![file isdirectory
$selectFilePath]} {
307 set Priv
(selectFilePath
) $selectFilePath