choosedir.tcl 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. # choosedir.tcl --
  2. #
  3. # Choose directory dialog implementation for Unix/Mac.
  4. #
  5. # Copyright (c) 1998-2000 by Scriptics Corporation.
  6. # All rights reserved.
  7. # Make sure the tk::dialog namespace, in which all dialogs should live, exists
  8. namespace eval ::tk::dialog {}
  9. namespace eval ::tk::dialog::file {}
  10. # Make the chooseDir namespace inside the dialog namespace
  11. namespace eval ::tk::dialog::file::chooseDir {
  12. namespace import -force ::tk::msgcat::*
  13. }
  14. # ::tk::dialog::file::chooseDir:: --
  15. #
  16. # Implements the TK directory selection dialog.
  17. #
  18. # Arguments:
  19. # args Options parsed by the procedure.
  20. #
  21. proc ::tk::dialog::file::chooseDir:: {args} {
  22. variable ::tk::Priv
  23. set dataName __tk_choosedir
  24. upvar ::tk::dialog::file::$dataName data
  25. Config $dataName $args
  26. if {$data(-parent) eq "."} {
  27. set w .$dataName
  28. } else {
  29. set w $data(-parent).$dataName
  30. }
  31. # (re)create the dialog box if necessary
  32. #
  33. if {![winfo exists $w]} {
  34. ::tk::dialog::file::Create $w TkChooseDir
  35. } elseif {[winfo class $w] ne "TkChooseDir"} {
  36. destroy $w
  37. ::tk::dialog::file::Create $w TkChooseDir
  38. } else {
  39. set data(dirMenuBtn) $w.contents.f1.menu
  40. set data(dirMenu) $w.contents.f1.menu.menu
  41. set data(upBtn) $w.contents.f1.up
  42. set data(icons) $w.contents.icons
  43. set data(ent) $w.contents.f2.ent
  44. set data(okBtn) $w.contents.f2.ok
  45. set data(cancelBtn) $w.contents.f2.cancel
  46. set data(hiddenBtn) $w.contents.f2.hidden
  47. }
  48. if {$::tk::dialog::file::showHiddenBtn} {
  49. $data(hiddenBtn) configure -state normal
  50. grid $data(hiddenBtn)
  51. } else {
  52. $data(hiddenBtn) configure -state disabled
  53. grid remove $data(hiddenBtn)
  54. }
  55. # When using -mustexist, manage the OK button state for validity
  56. $data(okBtn) configure -state normal
  57. if {$data(-mustexist)} {
  58. $data(ent) configure -validate key \
  59. -validatecommand [list ::tk::dialog::file::chooseDir::IsOK? $w %P]
  60. } else {
  61. $data(ent) configure -validate none
  62. }
  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.
  69. if {[winfo viewable [winfo toplevel $data(-parent)]] } {
  70. wm transient $w $data(-parent)
  71. }
  72. trace add variable data(selectPath) write \
  73. [list ::tk::dialog::file::SetPath $w]
  74. $data(dirMenuBtn) configure \
  75. -textvariable ::tk::dialog::file::${dataName}(selectPath)
  76. set data(filter) "*"
  77. set data(previousEntryText) ""
  78. ::tk::dialog::file::UpdateWhenIdle $w
  79. # Withdraw the window, then update all the geometry information
  80. # so we know how big it wants to be, then center the window in the
  81. # display (Motif style) and de-iconify it.
  82. ::tk::PlaceWindow $w widget $data(-parent)
  83. wm title $w $data(-title)
  84. # Set a grab and claim the focus too.
  85. ::tk::SetFocusGrab $w $data(ent)
  86. $data(ent) delete 0 end
  87. $data(ent) insert 0 $data(selectPath)
  88. $data(ent) selection range 0 end
  89. $data(ent) icursor end
  90. # Wait for the user to respond, then restore the focus and
  91. # return the index of the selected button. Restore the focus
  92. # before deleting the window, since otherwise the window manager
  93. # may take the focus away so we can't redirect it. Finally,
  94. # restore any grab that was in effect.
  95. vwait ::tk::Priv(selectFilePath)
  96. ::tk::RestoreFocusGrab $w $data(ent) withdraw
  97. # Cleanup traces on selectPath variable
  98. #
  99. foreach trace [trace info variable data(selectPath)] {
  100. trace remove variable data(selectPath) [lindex $trace 0] [lindex $trace 1]
  101. }
  102. $data(dirMenuBtn) configure -textvariable {}
  103. # Return value to user
  104. #
  105. return $Priv(selectFilePath)
  106. }
  107. # ::tk::dialog::file::chooseDir::Config --
  108. #
  109. # Configures the Tk choosedir dialog according to the argument list
  110. #
  111. proc ::tk::dialog::file::chooseDir::Config {dataName argList} {
  112. upvar ::tk::dialog::file::$dataName data
  113. # 0: Delete all variable that were set on data(selectPath) the
  114. # last time the file dialog is used. The traces may cause troubles
  115. # if the dialog is now used with a different -parent option.
  116. #
  117. foreach trace [trace info variable data(selectPath)] {
  118. trace remove variable data(selectPath) [lindex $trace 0] [lindex $trace 1]
  119. }
  120. # 1: the configuration specs
  121. #
  122. set specs {
  123. {-mustexist "" "" 0}
  124. {-initialdir "" "" ""}
  125. {-parent "" "" "."}
  126. {-title "" "" ""}
  127. }
  128. # 2: default values depending on the type of the dialog
  129. #
  130. if {![info exists data(selectPath)]} {
  131. # first time the dialog has been popped up
  132. set data(selectPath) [pwd]
  133. }
  134. # 3: parse the arguments
  135. #
  136. tclParseConfigSpec ::tk::dialog::file::$dataName $specs "" $argList
  137. if {$data(-title) eq ""} {
  138. set data(-title) "[mc "Choose Directory"]"
  139. }
  140. # Stub out the -multiple value for the dialog; it doesn't make sense for
  141. # choose directory dialogs, but we have to have something there because we
  142. # share so much code with the file dialogs.
  143. set data(-multiple) 0
  144. # 4: set the default directory and selection according to the -initial
  145. # settings
  146. #
  147. if {$data(-initialdir) ne ""} {
  148. # Ensure that initialdir is an absolute path name.
  149. if {[file isdirectory $data(-initialdir)]} {
  150. set old [pwd]
  151. cd $data(-initialdir)
  152. set data(selectPath) [pwd]
  153. cd $old
  154. } else {
  155. set data(selectPath) [pwd]
  156. }
  157. }
  158. if {![winfo exists $data(-parent)]} {
  159. return -code error -errorcode [list TK LOOKUP WINDOW $data(-parent)] \
  160. "bad window path name \"$data(-parent)\""
  161. }
  162. }
  163. # Gets called when user presses Return in the "Selection" entry or presses OK.
  164. #
  165. proc ::tk::dialog::file::chooseDir::OkCmd {w} {
  166. upvar ::tk::dialog::file::[winfo name $w] data
  167. # This is the brains behind selecting non-existant directories. Here's
  168. # the flowchart:
  169. # 1. If the icon list has a selection, join it with the current dir,
  170. # and return that value.
  171. # 1a. If the icon list does not have a selection ...
  172. # 2. If the entry is empty, do nothing.
  173. # 3. If the entry contains an invalid directory, then...
  174. # 3a. If the value is the same as last time through here, end dialog.
  175. # 3b. If the value is different than last time, save it and return.
  176. # 4. If entry contains a valid directory, then...
  177. # 4a. If the value is the same as the current directory, end dialog.
  178. # 4b. If the value is different from the current directory, change to
  179. # that directory.
  180. set selection [$data(icons) selection get]
  181. if {[llength $selection] != 0} {
  182. set iconText [$data(icons) get [lindex $selection 0]]
  183. set iconText [file join $data(selectPath) $iconText]
  184. Done $w $iconText
  185. } else {
  186. set text [$data(ent) get]
  187. if {$text eq ""} {
  188. return
  189. }
  190. set text [file join {*}[file split [string trim $text]]]
  191. if {![file exists $text] || ![file isdirectory $text]} {
  192. # Entry contains an invalid directory. If it's the same as the
  193. # last time they came through here, reset the saved value and end
  194. # the dialog. Otherwise, save the value (so we can do this test
  195. # next time).
  196. if {$text eq $data(previousEntryText)} {
  197. set data(previousEntryText) ""
  198. Done $w $text
  199. } else {
  200. set data(previousEntryText) $text
  201. }
  202. } else {
  203. # Entry contains a valid directory. If it is the same as the
  204. # current directory, end the dialog. Otherwise, change to that
  205. # directory.
  206. if {$text eq $data(selectPath)} {
  207. Done $w $text
  208. } else {
  209. set data(selectPath) $text
  210. }
  211. }
  212. }
  213. return
  214. }
  215. # Change state of OK button to match -mustexist correctness of entry
  216. #
  217. proc ::tk::dialog::file::chooseDir::IsOK? {w text} {
  218. upvar ::tk::dialog::file::[winfo name $w] data
  219. set ok [file isdirectory $text]
  220. $data(okBtn) configure -state [expr {$ok ? "normal" : "disabled"}]
  221. # always return 1
  222. return 1
  223. }
  224. proc ::tk::dialog::file::chooseDir::DblClick {w} {
  225. upvar ::tk::dialog::file::[winfo name $w] data
  226. set selection [$data(icons) selection get]
  227. if {[llength $selection] != 0} {
  228. set filenameFragment [$data(icons) get [lindex $selection 0]]
  229. set file $data(selectPath)
  230. if {[file isdirectory $file]} {
  231. ::tk::dialog::file::ListInvoke $w [list $filenameFragment]
  232. return
  233. }
  234. }
  235. }
  236. # Gets called when user browses the IconList widget (dragging mouse, arrow
  237. # keys, etc)
  238. #
  239. proc ::tk::dialog::file::chooseDir::ListBrowse {w text} {
  240. upvar ::tk::dialog::file::[winfo name $w] data
  241. if {$text eq ""} {
  242. return
  243. }
  244. set file [::tk::dialog::file::JoinFile $data(selectPath) $text]
  245. $data(ent) delete 0 end
  246. $data(ent) insert 0 $file
  247. }
  248. # ::tk::dialog::file::chooseDir::Done --
  249. #
  250. # Gets called when user has input a valid filename. Pops up a
  251. # dialog box to confirm selection when necessary. Sets the
  252. # Priv(selectFilePath) variable, which will break the "vwait"
  253. # loop in tk_chooseDirectory and return the selected filename to the
  254. # script that calls tk_getOpenFile or tk_getSaveFile
  255. #
  256. proc ::tk::dialog::file::chooseDir::Done {w {selectFilePath ""}} {
  257. upvar ::tk::dialog::file::[winfo name $w] data
  258. variable ::tk::Priv
  259. if {$selectFilePath eq ""} {
  260. set selectFilePath $data(selectPath)
  261. }
  262. if {$data(-mustexist) && ![file isdirectory $selectFilePath]} {
  263. return
  264. }
  265. set Priv(selectFilePath) $selectFilePath
  266. }