3 # Choose directory dialog implementation for Unix/Mac.
5 # Copyright (c) 1998-2000 by Scriptics Corporation.
8 # RCS: @(#) $Id: choosedir.tcl,v 1.23 2007/12/13 15:26:27 dgp Exp $
10 # Make sure the tk::dialog namespace, in which all dialogs should live, exists
11 namespace eval ::tk::dialog {}
12 namespace eval ::tk::dialog::file {}
14 # Make the chooseDir namespace inside the dialog namespace
15 namespace eval ::tk::dialog::file::chooseDir {
16 namespace import
-force ::tk::msgcat::*
19 # ::tk::dialog::file::chooseDir:: --
21 # Implements the TK directory selection dialog.
24 # args Options parsed by the procedure.
26 proc ::tk::dialog::file::chooseDir:: {args
} {
28 set dataName __tk_choosedir
29 upvar ::tk::dialog::file::$dataName data
30 Config
$dataName $args
32 if {$data(-parent) eq
"."} {
35 set w
$data(-parent).
$dataName
38 # (re)create the dialog box if necessary
40 if {![winfo exists
$w]} {
41 ::tk::dialog::file::Create $w TkChooseDir
42 } elseif
{[winfo class
$w] ne
"TkChooseDir"} {
44 ::tk::dialog::file::Create $w TkChooseDir
46 set data
(dirMenuBtn
) $w.contents.f1.
menu
47 set data
(dirMenu
) $w.contents.f1.
menu.
menu
48 set data
(upBtn
) $w.contents.f1.up
49 set data
(icons
) $w.contents.icons
50 set data
(ent
) $w.contents.f2.ent
51 set data
(okBtn
) $w.contents.f2.ok
52 set data
(cancelBtn
) $w.contents.f2.cancel
53 set data
(hiddenBtn
) $w.contents.f2.hidden
55 if {$::tk::dialog::file::showHiddenBtn} {
56 $data(hiddenBtn
) configure
-state normal
59 $data(hiddenBtn
) configure
-state disabled
60 grid remove
$data(hiddenBtn
)
63 # When using -mustexist, manage the OK button state for validity
64 $data(okBtn
) configure
-state normal
65 if {$data(-mustexist)} {
66 $data(ent
) configure
-validate key
\
67 -validatecommand [list ::tk::dialog::file::chooseDir::IsOK?
$w %P
]
69 $data(ent
) configure
-validate none
72 # Dialog boxes should be transient with respect to their parent,
73 # so that they will always stay on top of their parent window. However,
74 # some window managers will create the window as withdrawn if the parent
75 # window is withdrawn or iconified. Combined with the grab we put on the
76 # window, this can hang the entire application. Therefore we only make
77 # the dialog transient if the parent is viewable.
79 if {[winfo viewable
[winfo toplevel $data(-parent)]] } {
80 wm transient
$w $data(-parent)
83 trace add
variable data
(selectPath
) write
\
84 [list ::tk::dialog::file::SetPath $w]
85 $data(dirMenuBtn
) configure
\
86 -textvariable ::tk::dialog::file::${dataName
}(selectPath
)
89 set data
(previousEntryText
) ""
90 ::tk::dialog::file::UpdateWhenIdle $w
92 # Withdraw the window, then update all the geometry information
93 # so we know how big it wants to be, then center the window in the
94 # display and de-iconify it.
96 ::tk::PlaceWindow $w widget
$data(-parent)
97 wm title
$w $data(-title)
99 # Set a grab and claim the focus too.
101 ::tk::SetFocusGrab $w $data(ent
)
102 $data(ent
) delete
0 end
103 $data(ent
) insert
0 $data(selectPath
)
104 $data(ent
) selection range
0 end
105 $data(ent
) icursor end
107 # Wait for the user to respond, then restore the focus and
108 # return the index of the selected button. Restore the focus
109 # before deleting the window, since otherwise the window manager
110 # may take the focus away so we can't redirect it. Finally,
111 # restore any grab that was in effect.
113 vwait ::tk::Priv(selectFilePath
)
115 ::tk::RestoreFocusGrab $w $data(ent
) withdraw
117 # Cleanup traces on selectPath variable
120 foreach trace [trace info variable data
(selectPath
)] {
121 trace remove
variable data
(selectPath
) [lindex $trace 0] [lindex $trace 1]
123 $data(dirMenuBtn
) configure
-textvariable {}
125 # Return value to user
128 return $Priv(selectFilePath
)
131 # ::tk::dialog::file::chooseDir::Config --
133 # Configures the Tk choosedir dialog according to the argument list
135 proc ::tk::dialog::file::chooseDir::Config {dataName argList
} {
136 upvar ::tk::dialog::file::$dataName data
138 # 0: Delete all variable that were set on data(selectPath) the
139 # last time the file dialog is used. The traces may cause troubles
140 # if the dialog is now used with a different -parent option.
142 foreach trace [trace info variable data
(selectPath
)] {
143 trace remove
variable data
(selectPath
) [lindex $trace 0] [lindex $trace 1]
146 # 1: the configuration specs
150 {-initialdir "" "" ""}
155 # 2: default values depending on the type of the dialog
157 if {![info exists data
(selectPath
)]} {
158 # first time the dialog has been popped up
159 set data
(selectPath
) [pwd]
162 # 3: parse the arguments
164 tclParseConfigSpec
::tk::dialog::file::$dataName $specs "" $argList
166 if {$data(-title) eq
""} {
167 set data
(-title) "[mc "Choose Directory
"]"
170 # Stub out the -multiple value for the dialog; it doesn't make sense for
171 # choose directory dialogs, but we have to have something there because we
172 # share so much code with the file dialogs.
173 set data
(-multiple) 0
175 # 4: set the default directory and selection according to the -initial
178 if {$data(-initialdir) ne
""} {
179 # Ensure that initialdir is an absolute path name.
180 if {[file isdirectory
$data(-initialdir)]} {
182 cd $data(-initialdir)
183 set data
(selectPath
) [pwd]
186 set data
(selectPath
) [pwd]
190 if {![winfo exists
$data(-parent)]} {
191 error "bad window path name \"$data(-parent)\""
195 # Gets called when user presses Return in the "Selection" entry or presses OK.
197 proc ::tk::dialog::file::chooseDir::OkCmd {w
} {
198 upvar ::tk::dialog::file::[winfo name
$w] data
200 # This is the brains behind selecting non-existant directories. Here's
202 # 1. If the icon list has a selection, join it with the current dir,
203 # and return that value.
204 # 1a. If the icon list does not have a selection ...
205 # 2. If the entry is empty, do nothing.
206 # 3. If the entry contains an invalid directory, then...
207 # 3a. If the value is the same as last time through here, end dialog.
208 # 3b. If the value is different than last time, save it and return.
209 # 4. If entry contains a valid directory, then...
210 # 4a. If the value is the same as the current directory, end dialog.
211 # 4b. If the value is different from the current directory, change to
214 set selection [tk::IconList_CurSelection $data(icons
)]
215 if {[llength $selection] != 0} {
216 set iconText
[tk::IconList_Get $data(icons
) [lindex $selection 0]]
217 set iconText
[file join $data(selectPath
) $iconText]
220 set text [$data(ent
) get
]
224 set text [file join {*}[file split [string trim
$text]]]
225 if {![file exists
$text] ||
![file isdirectory
$text]} {
226 # Entry contains an invalid directory. If it's the same as the
227 # last time they came through here, reset the saved value and end
228 # the dialog. Otherwise, save the value (so we can do this test
230 if {$text eq
$data(previousEntryText
)} {
231 set data
(previousEntryText
) ""
234 set data
(previousEntryText
) $text
237 # Entry contains a valid directory. If it is the same as the
238 # current directory, end the dialog. Otherwise, change to that
240 if {$text eq
$data(selectPath
)} {
243 set data
(selectPath
) $text
250 # Change state of OK button to match -mustexist correctness of entry
252 proc ::tk::dialog::file::chooseDir::IsOK?
{w
text} {
253 upvar ::tk::dialog::file::[winfo name
$w] data
255 set ok
[file isdirectory
$text]
256 $data(okBtn
) configure
-state [expr {$ok ?
"normal" : "disabled"}]
262 proc ::tk::dialog::file::chooseDir::DblClick {w
} {
263 upvar ::tk::dialog::file::[winfo name
$w] data
264 set selection [tk::IconList_CurSelection $data(icons
)]
265 if {[llength $selection] != 0} {
266 set filenameFragment
\
267 [tk::IconList_Get $data(icons
) [lindex $selection 0]]
268 set file $data(selectPath
)
269 if {[file isdirectory
$file]} {
270 ::tk::dialog::file::ListInvoke $w [list $filenameFragment]
276 # Gets called when user browses the IconList widget (dragging mouse, arrow
279 proc ::tk::dialog::file::chooseDir::ListBrowse {w
text} {
280 upvar ::tk::dialog::file::[winfo name
$w] data
286 set file [::tk::dialog::file::JoinFile $data(selectPath
) $text]
287 $data(ent
) delete
0 end
288 $data(ent
) insert
0 $file
291 # ::tk::dialog::file::chooseDir::Done --
293 # Gets called when user has input a valid filename. Pops up a
294 # dialog box to confirm selection when necessary. Sets the
295 # Priv(selectFilePath) variable, which will break the "vwait"
296 # loop in tk_chooseDirectory and return the selected filename to the
297 # script that calls tk_getOpenFile or tk_getSaveFile
299 proc ::tk::dialog::file::chooseDir::Done {w
{selectFilePath
""}} {
300 upvar ::tk::dialog::file::[winfo name
$w] data
303 if {$selectFilePath eq
""} {
304 set selectFilePath
$data(selectPath
)
306 if {$data(-mustexist) && ![file isdirectory
$selectFilePath]} {
309 set Priv
(selectFilePath
) $selectFilePath