choosedir.tcl 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. error "bad window path name \"$data(-parent)\""
  160. }
  161. }
  162. # Gets called when user presses Return in the "Selection" entry or presses OK.
  163. #
  164. proc ::tk::dialog::file::chooseDir::OkCmd {w} {
  165. upvar ::tk::dialog::file::[winfo name $w] data
  166. # This is the brains behind selecting non-existant directories. Here's
  167. # the flowchart:
  168. # 1. If the icon list has a selection, join it with the current dir,
  169. # and return that value.
  170. # 1a. If the icon list does not have a selection ...
  171. # 2. If the entry is empty, do nothing.
  172. # 3. If the entry contains an invalid directory, then...
  173. # 3a. If the value is the same as last time through here, end dialog.
  174. # 3b. If the value is different than last time, save it and return.
  175. # 4. If entry contains a valid directory, then...
  176. # 4a. If the value is the same as the current directory, end dialog.
  177. # 4b. If the value is different from the current directory, change to
  178. # that directory.
  179. set selection [tk::IconList_CurSelection $data(icons)]
  180. if {[llength $selection] != 0} {
  181. set iconText [tk::IconList_Get $data(icons) [lindex $selection 0]]
  182. set iconText [file join $data(selectPath) $iconText]
  183. Done $w $iconText
  184. } else {
  185. set text [$data(ent) get]
  186. if {$text eq ""} {
  187. return
  188. }
  189. set text [file join {*}[file split [string trim $text]]]
  190. if {![file exists $text] || ![file isdirectory $text]} {
  191. # Entry contains an invalid directory. If it's the same as the
  192. # last time they came through here, reset the saved value and end
  193. # the dialog. Otherwise, save the value (so we can do this test
  194. # next time).
  195. if {$text eq $data(previousEntryText)} {
  196. set data(previousEntryText) ""
  197. Done $w $text
  198. } else {
  199. set data(previousEntryText) $text
  200. }
  201. } else {
  202. # Entry contains a valid directory. If it is the same as the
  203. # current directory, end the dialog. Otherwise, change to that
  204. # directory.
  205. if {$text eq $data(selectPath)} {
  206. Done $w $text
  207. } else {
  208. set data(selectPath) $text
  209. }
  210. }
  211. }
  212. return
  213. }
  214. # Change state of OK button to match -mustexist correctness of entry
  215. #
  216. proc ::tk::dialog::file::chooseDir::IsOK? {w text} {
  217. upvar ::tk::dialog::file::[winfo name $w] data
  218. set ok [file isdirectory $text]
  219. $data(okBtn) configure -state [expr {$ok ? "normal" : "disabled"}]
  220. # always return 1
  221. return 1
  222. }
  223. proc ::tk::dialog::file::chooseDir::DblClick {w} {
  224. upvar ::tk::dialog::file::[winfo name $w] data
  225. set selection [tk::IconList_CurSelection $data(icons)]
  226. if {[llength $selection] != 0} {
  227. set filenameFragment \
  228. [tk::IconList_Get $data(icons) [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. }