3 # Choose directory dialog implementation for Unix/Mac.
5 # Copyright (c) 1998-2000 by Scriptics Corporation.
8 # RCS: @(#) $Id: choosedir.tcl,v 1.20 2007/05/16 18:10:35 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.f1.
menu
47 set data
(dirMenu
) $w.f1.
menu.
menu
48 set data
(upBtn
) $w.f1.up
49 set data
(icons
) $w.icons
50 set data
(ent
) $w.f2.ent
51 set data
(okBtn
) $w.f2.ok
52 set data
(cancelBtn
) $w.f2.cancel
53 set data
(hiddenBtn
) $w.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 # Dialog boxes should be transient with respect to their parent,
64 # so that they will always stay on top of their parent window. However,
65 # some window managers will create the window as withdrawn if the parent
66 # window is withdrawn or iconified. Combined with the grab we put on the
67 # window, this can hang the entire application. Therefore we only make
68 # the dialog transient if the parent is viewable.
70 if {[winfo viewable
[winfo toplevel $data(-parent)]] } {
71 wm transient
$w $data(-parent)
74 trace add
variable data
(selectPath
) write
\
75 [list ::tk::dialog::file::SetPath $w]
76 $data(dirMenuBtn
) configure
\
77 -textvariable ::tk::dialog::file::${dataName
}(selectPath
)
80 set data
(previousEntryText
) ""
81 ::tk::dialog::file::UpdateWhenIdle $w
83 # Withdraw the window, then update all the geometry information
84 # so we know how big it wants to be, then center the window in the
85 # display and de-iconify it.
87 ::tk::PlaceWindow $w widget
$data(-parent)
88 wm title
$w $data(-title)
90 # Set a grab and claim the focus too.
92 ::tk::SetFocusGrab $w $data(ent
)
93 $data(ent
) delete
0 end
94 $data(ent
) insert
0 $data(selectPath
)
95 $data(ent
) selection range
0 end
96 $data(ent
) icursor end
98 # Wait for the user to respond, then restore the focus and
99 # return the index of the selected button. Restore the focus
100 # before deleting the window, since otherwise the window manager
101 # may take the focus away so we can't redirect it. Finally,
102 # restore any grab that was in effect.
104 vwait ::tk::Priv(selectFilePath
)
106 ::tk::RestoreFocusGrab $w $data(ent
) withdraw
108 # Cleanup traces on selectPath variable
111 foreach trace [trace info variable data
(selectPath
)] {
112 trace remove
variable data
(selectPath
) [lindex $trace 0] [lindex $trace 1]
114 $data(dirMenuBtn
) configure
-textvariable {}
116 # Return value to user
119 return $Priv(selectFilePath
)
122 # ::tk::dialog::file::chooseDir::Config --
124 # Configures the Tk choosedir dialog according to the argument list
126 proc ::tk::dialog::file::chooseDir::Config {dataName argList
} {
127 upvar ::tk::dialog::file::$dataName data
129 # 0: Delete all variable that were set on data(selectPath) the
130 # last time the file dialog is used. The traces may cause troubles
131 # if the dialog is now used with a different -parent option.
133 foreach trace [trace info variable data
(selectPath
)] {
134 trace remove
variable data
(selectPath
) [lindex $trace 0] [lindex $trace 1]
137 # 1: the configuration specs
141 {-initialdir "" "" ""}
146 # 2: default values depending on the type of the dialog
148 if {![info exists data
(selectPath
)]} {
149 # first time the dialog has been popped up
150 set data
(selectPath
) [pwd]
153 # 3: parse the arguments
155 tclParseConfigSpec
::tk::dialog::file::$dataName $specs "" $argList
157 if {$data(-title) eq
""} {
158 set data
(-title) "[mc "Choose Directory
"]"
161 # Stub out the -multiple value for the dialog; it doesn't make sense for
162 # choose directory dialogs, but we have to have something there because we
163 # share so much code with the file dialogs.
164 set data
(-multiple) 0
166 # 4: set the default directory and selection according to the -initial
169 if {$data(-initialdir) ne
""} {
170 # Ensure that initialdir is an absolute path name.
171 if {[file isdirectory
$data(-initialdir)]} {
173 cd $data(-initialdir)
174 set data
(selectPath
) [pwd]
177 set data
(selectPath
) [pwd]
181 if {![winfo exists
$data(-parent)]} {
182 error "bad window path name \"$data(-parent)\""
186 # Gets called when user presses Return in the "Selection" entry or presses OK.
188 proc ::tk::dialog::file::chooseDir::OkCmd {w
} {
189 upvar ::tk::dialog::file::[winfo name
$w] data
191 # This is the brains behind selecting non-existant directories. Here's
193 # 1. If the icon list has a selection, join it with the current dir,
194 # and return that value.
195 # 1a. If the icon list does not have a selection ...
196 # 2. If the entry is empty, do nothing.
197 # 3. If the entry contains an invalid directory, then...
198 # 3a. If the value is the same as last time through here, end dialog.
199 # 3b. If the value is different than last time, save it and return.
200 # 4. If entry contains a valid directory, then...
201 # 4a. If the value is the same as the current directory, end dialog.
202 # 4b. If the value is different from the current directory, change to
205 set selection [tk::IconList_CurSelection $data(icons
)]
206 if {[llength $selection] != 0} {
207 set iconText
[tk::IconList_Get $data(icons
) [lindex $selection 0]]
208 set iconText
[file join $data(selectPath
) $iconText]
211 set text [$data(ent
) get
]
215 set text [file join {*}[file split [string trim
$text]]]
216 if {![file exists
$text] ||
![file isdirectory
$text]} {
217 # Entry contains an invalid directory. If it's the same as the
218 # last time they came through here, reset the saved value and end
219 # the dialog. Otherwise, save the value (so we can do this test
221 if {$text eq
$data(previousEntryText
)} {
222 set data
(previousEntryText
) ""
225 set data
(previousEntryText
) $text
228 # Entry contains a valid directory. If it is the same as the
229 # current directory, end the dialog. Otherwise, change to that
231 if {$text eq
$data(selectPath
)} {
234 set data
(selectPath
) $text
241 proc ::tk::dialog::file::chooseDir::DblClick {w
} {
242 upvar ::tk::dialog::file::[winfo name
$w] data
243 set selection [tk::IconList_CurSelection $data(icons
)]
244 if {[llength $selection] != 0} {
245 set filenameFragment
\
246 [tk::IconList_Get $data(icons
) [lindex $selection 0]]
247 set file $data(selectPath
)
248 if {[file isdirectory
$file]} {
249 ::tk::dialog::file::ListInvoke $w [list $filenameFragment]
255 # Gets called when user browses the IconList widget (dragging mouse, arrow
258 proc ::tk::dialog::file::chooseDir::ListBrowse {w
text} {
259 upvar ::tk::dialog::file::[winfo name
$w] data
265 set file [::tk::dialog::file::JoinFile $data(selectPath
) $text]
266 $data(ent
) delete
0 end
267 $data(ent
) insert
0 $file
270 # ::tk::dialog::file::chooseDir::Done --
272 # Gets called when user has input a valid filename. Pops up a
273 # dialog box to confirm selection when necessary. Sets the
274 # Priv(selectFilePath) variable, which will break the "vwait"
275 # loop in tk_chooseDirectory and return the selected filename to the
276 # script that calls tk_getOpenFile or tk_getSaveFile
278 proc ::tk::dialog::file::chooseDir::Done {w
{selectFilePath
""}} {
279 upvar ::tk::dialog::file::[winfo name
$w] data
282 if {$selectFilePath eq
""} {
283 set selectFilePath
$data(selectPath
)
285 if {$data(-mustexist)} {
286 if {![file exists
$selectFilePath] ||
![file isdir
$selectFilePath]} {
290 set Priv
(selectFilePath
) $selectFilePath