tkfbox.tcl 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. # tkfbox.tcl --
  2. #
  3. # Implements the "TK" standard file selection dialog box. This dialog
  4. # box is used on the Unix platforms whenever the tk_strictMotif flag is
  5. # not set.
  6. #
  7. # The "TK" standard file selection dialog box is similar to the file
  8. # selection dialog box on Win95(TM). The user can navigate the
  9. # directories by clicking on the folder icons or by selecting the
  10. # "Directory" option menu. The user can select files by clicking on the
  11. # file icons or by entering a filename in the "Filename:" entry.
  12. #
  13. # Copyright (c) 1994-1998 Sun Microsystems, Inc.
  14. #
  15. # See the file "license.terms" for information on usage and redistribution
  16. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  17. #
  18. namespace eval ::tk::dialog {}
  19. namespace eval ::tk::dialog::file {
  20. namespace import -force ::tk::msgcat::*
  21. variable showHiddenBtn 0
  22. variable showHiddenVar 1
  23. # Create the images if they did not already exist.
  24. if {![info exists ::tk::Priv(updirImage)]} {
  25. set ::tk::Priv(updirImage) [image create photo -data {
  26. iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABmJLR0QA/gD+AP7rGN
  27. SCAAAACXBIWXMAAA3WAAAN1gGQb3mcAAAACXZwQWcAAAAWAAAAFgDcxelYAAAENUlE
  28. QVQ4y7WUbWiVZRjHf/f9POcc9+Kc5bC2aIq5sGG0XnTzNU13zAIlFMNc9CEhTCKwCC
  29. JIgt7AglaR0RcrolAKg14+GBbiGL6xZiYyy63cmzvu7MVznnOe537rw7bDyvlBoT/c
  30. n+6L3/3nf13XLZLJJP+HfICysjKvqqpq+rWKysvLR1tbW+11g+fPn/+bEGIe4KYqCs
  31. Owu66u7oG2trah6wJrrRc0NTVhjME5h7Vj5pxzCCE4duxYZUdHx/aGhoZmgJ+yb+wF
  32. uCO19RmAffv25f8LFslkktraWtvU1CS6u7vRWmOtxVpbAPu+T0tLS04pFU/J34Wd3S
  33. cdFtlfZWeZBU4IcaS5uXn1ZLAEMMY4ay1aa4wx/zpKKYIgoL6+vmjxqoXe5ZLTcsPq
  34. bTyycjODpe1y3WMrvDAMV14jCuW0VhhjiJQpOJ5w7Zwjk8/y9R+vsHHNNq6oFMrkeX
  35. BxI+8d2sktap3YvOPD0lRQrH+Z81fE7t3WB4gihVKazsuaA20aKSUgAG/seQdy2l6W
  36. 37+EyopqTv39I6HJUT2zlnlza2jLdgiTaxwmDov6alLHcZUTzXPGGAauWJbfO4dHl9
  37. bgJs3HyfNf0N4ZsOa+jbT3/ownY/hO09p1kBULtjBw+Tvq7xzwauds4dWPDleAcP5E
  38. xlprgtBRUZRgYCRPTzoHwEi2g6OnX+eFrW/RM9qBE4p43CeTz5ATaU6nDrFm2cPs/+
  39. E1SopqkZ7MFJqntXZaa7IKppckwIEvJbg8LWd28OT6nVihCPQQ8UScWCLGqO4hXuQx
  40. qDtJ204eWrqWb1ufRspwtABWaqx5gRKUFSdwDnxPcuLcyyxbuIyaqntIBV34MY9YzC
  41. Owg+S9YeJFkniRpGPkCLMrZzG3+jbktA/KClMxFoUhiKC0OAbAhd79CO8i6xe/STyW
  42. 4O7KVRgUJ/sP0heeJV4kEVKw/vZd40sFKxat4mLvp6VLdvnb/XHHGGPIKwBBpC1/9n
  43. 3DpfRZnn9/AwCxRII9O79kVPdjvByxuET6Ai8mePeTt4lyheXzhOSpCcdWa00uckTG
  44. kckbGu76nEhbIm2xznH4VB3OWYaiXqQn8GKSWGIMHuXyPL76LBcupmhp69pz4uMnXi
  45. w4VloTGcdQRtGdzmHs1f+RdYZslMZJhzUOHVnceN1ooEiP5JUzdqCQMWCD0JCIeQzn
  46. NNpO+clhrCYf5rC+A2cxWmDUWG2oHEOZMEKIwclgMnnLrTeXUV7sUzpNXgU9DmijWV
  47. v9LEKCkAIhKIBnlvpks6F21qUZ31u/sbExPa9h0/RzwzMov2nGlG5TmW1YOzzlnSfL
  48. mVnyGf19Q7lwZHBp+1fPtflAIgiC7389n9qkihP+lWyeqfUO15ZwQTqlw9H+o2cOvN
  49. QJCAHEgEqgYnI0NyALjAJdyWQy7wMa6AEujUdzo3LjcAXwD/XCTKIRjWytAAAAJXRF
  50. WHRjcmVhdGUtZGF0ZQAyMDA5LTA0LTA2VDIxOjI1OjQxLTAzOjAw8s+uCAAAACV0RV
  51. h0bW9kaWZ5LWRhdGUAMjAwOC0wMS0wM1QxNTowODoyMS0wMjowMJEc/44AAAAZdEVY
  52. dFNvZnR3YXJlAHd3dy5pbmtzY2FwZS5vcmeb7jwaAAAAAElFTkSuQmCC
  53. }]
  54. }
  55. if {![info exists ::tk::Priv(folderImage)]} {
  56. set ::tk::Priv(folderImage) [image create photo -data {
  57. iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiA
  58. AAAAlwSFlzAAAN1wAADdcBQiibeAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBl
  59. Lm9yZ5vuPBoAAAHCSURBVDiNpZAxa5NRFIafc+9XLCni4BC6FBycMnbrLpkcgtDVX6
  60. C70D/g4lZX/4coxLlgxFkpiiSSUGm/JiXfveee45AmNlhawXc53HvPee55X+l2u/yP
  61. qt3d3Tfu/viatwt3fzIYDI5uBJhZr9fr3TMzzAx3B+D09PR+v98/7HQ6z5fNOWdCCG
  62. U4HH6s67oAVDlnV1UmkwmllBUkhMD29nYHeLuEAkyn06qU8qqu64MrgIyqYmZrkHa7
  63. 3drc3KTVahFjJITAaDRiPB4/XFlQVVMtHH5IzJo/P4EA4MyB+erWPQB7++zs7ccYvl
  64. U5Z08pMW2cl88eIXLZeDUpXzsBkNQ5eP1+p0opmaoCTgzw6fjs6gLLsp58FB60t0Dc
  65. K1Ul54yIEIMQ43Uj68pquDmCeJVztpwzuBNE2LgBoMVpslHMCUEAFgDVxQbzVAiA+a
  66. K5uGPmmDtZF3VpoUm2ArhqQaRiUjcMf81p1G60UEVhcjZfAFTVUkrgkS+jc06mDX9n
  67. vq4YhJ9nlxZExMwMEaHJRutOdWuIIsJFUoBSuTvHJ4YIfP46unV4qdlsjsBRZRtb/X
  68. fHd5+C8+P7+J8BIoxFwovfRxYhnhxjpzEAAAAASUVORK5CYII=
  69. }]
  70. }
  71. if {![info exists ::tk::Priv(fileImage)]} {
  72. set ::tk::Priv(fileImage) [image create photo -data {
  73. iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gva
  74. eTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1QQWFA84umAmQgAAANpJREFU
  75. OMutkj1uhDAQhb8HSLtbISGfgZ+zbJkix0HmFhwhUdocBnMBGvqtTIqIFSReWKK8ai
  76. x73nwzHrVt+zEMwwvH9FrX9TsA1trpqKy10+yUzME4jnjvAZB0LzXHkojjmDRNVyh3
  77. A+89zrlVwlKSqKrqVy/J8lAUxSZBSMny4ZLgp54iyPM8UPHGNJ2IomibAKDv+9VlWZ
  78. bABbgB5/0WQgSSkC4PF2JF4JzbHN430c4vhAm0TyCJruuClefph4yCBCGT3T3Isoy/
  79. KDHGfDZNcz2SZIx547/0BVRRX7n8uT/sAAAAAElFTkSuQmCC
  80. }]
  81. }
  82. }
  83. # ::tk::dialog::file:: --
  84. #
  85. # Implements the TK file selection dialog. This dialog is used when the
  86. # tk_strictMotif flag is set to false. This procedure shouldn't be
  87. # called directly. Call tk_getOpenFile or tk_getSaveFile instead.
  88. #
  89. # Arguments:
  90. # type "open" or "save"
  91. # args Options parsed by the procedure.
  92. #
  93. proc ::tk::dialog::file:: {type args} {
  94. variable ::tk::Priv
  95. variable showHiddenBtn
  96. set dataName __tk_filedialog
  97. upvar ::tk::dialog::file::$dataName data
  98. Config $dataName $type $args
  99. if {$data(-parent) eq "."} {
  100. set w .$dataName
  101. } else {
  102. set w $data(-parent).$dataName
  103. }
  104. # (re)create the dialog box if necessary
  105. #
  106. if {![winfo exists $w]} {
  107. Create $w TkFDialog
  108. } elseif {[winfo class $w] ne "TkFDialog"} {
  109. destroy $w
  110. Create $w TkFDialog
  111. } else {
  112. set data(dirMenuBtn) $w.contents.f1.menu
  113. set data(dirMenu) $w.contents.f1.menu.menu
  114. set data(upBtn) $w.contents.f1.up
  115. set data(icons) $w.contents.icons
  116. set data(ent) $w.contents.f2.ent
  117. set data(typeMenuLab) $w.contents.f2.lab2
  118. set data(typeMenuBtn) $w.contents.f2.menu
  119. set data(typeMenu) $data(typeMenuBtn).m
  120. set data(okBtn) $w.contents.f2.ok
  121. set data(cancelBtn) $w.contents.f2.cancel
  122. set data(hiddenBtn) $w.contents.f2.hidden
  123. SetSelectMode $w $data(-multiple)
  124. }
  125. if {$showHiddenBtn} {
  126. $data(hiddenBtn) configure -state normal
  127. grid $data(hiddenBtn)
  128. } else {
  129. $data(hiddenBtn) configure -state disabled
  130. grid remove $data(hiddenBtn)
  131. }
  132. # Make sure subseqent uses of this dialog are independent [Bug 845189]
  133. unset -nocomplain data(extUsed)
  134. # Dialog boxes should be transient with respect to their parent, so that
  135. # they will always stay on top of their parent window. However, some
  136. # window managers will create the window as withdrawn if the parent window
  137. # is withdrawn or iconified. Combined with the grab we put on the window,
  138. # this can hang the entire application. Therefore we only make the dialog
  139. # transient if the parent is viewable.
  140. if {[winfo viewable [winfo toplevel $data(-parent)]]} {
  141. wm transient $w $data(-parent)
  142. }
  143. # Add traces on the selectPath variable
  144. #
  145. trace add variable data(selectPath) write \
  146. [list ::tk::dialog::file::SetPath $w]
  147. $data(dirMenuBtn) configure \
  148. -textvariable ::tk::dialog::file::${dataName}(selectPath)
  149. # Cleanup previous menu
  150. #
  151. $data(typeMenu) delete 0 end
  152. $data(typeMenuBtn) configure -state normal -text ""
  153. # Initialize the file types menu
  154. #
  155. if {[llength $data(-filetypes)]} {
  156. # Default type and name to first entry
  157. set initialtype [lindex $data(-filetypes) 0]
  158. set initialTypeName [lindex $initialtype 0]
  159. if {$data(-typevariable) ne ""} {
  160. upvar #0 $data(-typevariable) typeVariable
  161. if {[info exists typeVariable]} {
  162. set initialTypeName $typeVariable
  163. }
  164. }
  165. foreach type $data(-filetypes) {
  166. set title [lindex $type 0]
  167. set filter [lindex $type 1]
  168. $data(typeMenu) add command -label $title \
  169. -command [list ::tk::dialog::file::SetFilter $w $type]
  170. # [string first] avoids glob-pattern char issues
  171. if {[string first ${initialTypeName} $title] == 0} {
  172. set initialtype $type
  173. }
  174. }
  175. SetFilter $w $initialtype
  176. $data(typeMenuBtn) configure -state normal
  177. $data(typeMenuLab) configure -state normal
  178. } else {
  179. set data(filter) "*"
  180. $data(typeMenuBtn) configure -state disabled -takefocus 0
  181. $data(typeMenuLab) configure -state disabled
  182. }
  183. UpdateWhenIdle $w
  184. # Withdraw the window, then update all the geometry information
  185. # so we know how big it wants to be, then center the window in the
  186. # display (Motif style) and de-iconify it.
  187. ::tk::PlaceWindow $w widget $data(-parent)
  188. wm title $w $data(-title)
  189. # Set a grab and claim the focus too.
  190. ::tk::SetFocusGrab $w $data(ent)
  191. $data(ent) delete 0 end
  192. $data(ent) insert 0 $data(selectFile)
  193. $data(ent) selection range 0 end
  194. $data(ent) icursor end
  195. # Wait for the user to respond, then restore the focus and return the
  196. # index of the selected button. Restore the focus before deleting the
  197. # window, since otherwise the window manager may take the focus away so we
  198. # can't redirect it. Finally, restore any grab that was in effect.
  199. vwait ::tk::Priv(selectFilePath)
  200. ::tk::RestoreFocusGrab $w $data(ent) withdraw
  201. # Cleanup traces on selectPath variable
  202. #
  203. foreach trace [trace info variable data(selectPath)] {
  204. trace remove variable data(selectPath) {*}$trace
  205. }
  206. $data(dirMenuBtn) configure -textvariable {}
  207. return $Priv(selectFilePath)
  208. }
  209. # ::tk::dialog::file::Config --
  210. #
  211. # Configures the TK filedialog according to the argument list
  212. #
  213. proc ::tk::dialog::file::Config {dataName type argList} {
  214. upvar ::tk::dialog::file::$dataName data
  215. set data(type) $type
  216. # 0: Delete all variable that were set on data(selectPath) the
  217. # last time the file dialog is used. The traces may cause troubles
  218. # if the dialog is now used with a different -parent option.
  219. foreach trace [trace info variable data(selectPath)] {
  220. trace remove variable data(selectPath) {*}$trace
  221. }
  222. # 1: the configuration specs
  223. #
  224. set specs {
  225. {-defaultextension "" "" ""}
  226. {-filetypes "" "" ""}
  227. {-initialdir "" "" ""}
  228. {-initialfile "" "" ""}
  229. {-parent "" "" "."}
  230. {-title "" "" ""}
  231. {-typevariable "" "" ""}
  232. }
  233. # The "-multiple" option is only available for the "open" file dialog.
  234. #
  235. if {$type eq "open"} {
  236. lappend specs {-multiple "" "" "0"}
  237. }
  238. # The "-confirmoverwrite" option is only for the "save" file dialog.
  239. #
  240. if {$type eq "save"} {
  241. lappend specs {-confirmoverwrite "" "" "1"}
  242. }
  243. # 2: default values depending on the type of the dialog
  244. #
  245. if {![info exists data(selectPath)]} {
  246. # first time the dialog has been popped up
  247. set data(selectPath) [pwd]
  248. set data(selectFile) ""
  249. }
  250. # 3: parse the arguments
  251. #
  252. tclParseConfigSpec ::tk::dialog::file::$dataName $specs "" $argList
  253. if {$data(-title) eq ""} {
  254. if {$type eq "open"} {
  255. set data(-title) [mc "Open"]
  256. } else {
  257. set data(-title) [mc "Save As"]
  258. }
  259. }
  260. # 4: set the default directory and selection according to the -initial
  261. # settings
  262. #
  263. if {$data(-initialdir) ne ""} {
  264. # Ensure that initialdir is an absolute path name.
  265. if {[file isdirectory $data(-initialdir)]} {
  266. set old [pwd]
  267. cd $data(-initialdir)
  268. set data(selectPath) [pwd]
  269. cd $old
  270. } else {
  271. set data(selectPath) [pwd]
  272. }
  273. }
  274. set data(selectFile) $data(-initialfile)
  275. # 5. Parse the -filetypes option
  276. #
  277. set data(-filetypes) [::tk::FDGetFileTypes $data(-filetypes)]
  278. if {![winfo exists $data(-parent)]} {
  279. return -code error -errorcode [list TK LOOKUP WINDOW $data(-parent)] \
  280. "bad window path name \"$data(-parent)\""
  281. }
  282. # Set -multiple to a one or zero value (not other boolean types like
  283. # "yes") so we can use it in tests more easily.
  284. if {$type eq "save"} {
  285. set data(-multiple) 0
  286. } elseif {$data(-multiple)} {
  287. set data(-multiple) 1
  288. } else {
  289. set data(-multiple) 0
  290. }
  291. }
  292. proc ::tk::dialog::file::Create {w class} {
  293. set dataName [lindex [split $w .] end]
  294. upvar ::tk::dialog::file::$dataName data
  295. variable ::tk::Priv
  296. global tk_library
  297. toplevel $w -class $class
  298. if {[tk windowingsystem] eq "x11"} {wm attributes $w -type dialog}
  299. pack [ttk::frame $w.contents] -expand 1 -fill both
  300. #set w $w.contents
  301. # f1: the frame with the directory option menu
  302. #
  303. set f1 [ttk::frame $w.contents.f1]
  304. bind [::tk::AmpWidget ttk::label $f1.lab -text [mc "&Directory:"]] \
  305. <<AltUnderlined>> [list focus $f1.menu]
  306. set data(dirMenuBtn) $f1.menu
  307. if {![info exists data(selectPath)]} {
  308. set data(selectPath) ""
  309. }
  310. set data(dirMenu) $f1.menu.menu
  311. ttk::menubutton $f1.menu -menu $data(dirMenu) -direction flush \
  312. -textvariable [format %s(selectPath) ::tk::dialog::file::$dataName]
  313. menu $data(dirMenu) -tearoff 0
  314. $data(dirMenu) add radiobutton -label "" -variable \
  315. [format %s(selectPath) ::tk::dialog::file::$dataName]
  316. set data(upBtn) [ttk::button $f1.up]
  317. $data(upBtn) configure -image $Priv(updirImage)
  318. $f1.menu configure -takefocus 1;# -highlightthickness 2
  319. pack $data(upBtn) -side right -padx 4 -fill both
  320. pack $f1.lab -side left -padx 4 -fill both
  321. pack $f1.menu -expand yes -fill both -padx 4
  322. # data(icons): the IconList that list the files and directories.
  323. #
  324. if {$class eq "TkFDialog"} {
  325. if { $data(-multiple) } {
  326. set fNameCaption [mc "File &names:"]
  327. } else {
  328. set fNameCaption [mc "File &name:"]
  329. }
  330. set fTypeCaption [mc "Files of &type:"]
  331. set iconListCommand [list ::tk::dialog::file::OkCmd $w]
  332. } else {
  333. set fNameCaption [mc "&Selection:"]
  334. set iconListCommand [list ::tk::dialog::file::chooseDir::DblClick $w]
  335. }
  336. set data(icons) [::tk::IconList $w.contents.icons \
  337. -command $iconListCommand -multiple $data(-multiple)]
  338. bind $data(icons) <<ListboxSelect>> \
  339. [list ::tk::dialog::file::ListBrowse $w]
  340. # f2: the frame with the OK button, cancel button, "file name" field
  341. # and file types field.
  342. #
  343. set f2 [ttk::frame $w.contents.f2]
  344. bind [::tk::AmpWidget ttk::label $f2.lab -text $fNameCaption -anchor e]\
  345. <<AltUnderlined>> [list focus $f2.ent]
  346. # -pady 0
  347. set data(ent) [ttk::entry $f2.ent]
  348. # The font to use for the icons. The default Canvas font on Unix is just
  349. # deviant.
  350. set ::tk::$w.contents.icons(font) [$data(ent) cget -font]
  351. # Make the file types bits only if this is a File Dialog
  352. if {$class eq "TkFDialog"} {
  353. set data(typeMenuLab) [::tk::AmpWidget ttk::label $f2.lab2 \
  354. -text $fTypeCaption -anchor e]
  355. # -pady [$f2.lab cget -pady]
  356. set data(typeMenuBtn) [ttk::menubutton $f2.menu \
  357. -menu $f2.menu.m]
  358. # -indicatoron 1
  359. set data(typeMenu) [menu $data(typeMenuBtn).m -tearoff 0]
  360. # $data(typeMenuBtn) configure -takefocus 1 -relief raised -anchor w
  361. bind $data(typeMenuLab) <<AltUnderlined>> [list \
  362. focus $data(typeMenuBtn)]
  363. }
  364. # The hidden button is displayed when ::tk::dialog::file::showHiddenBtn is
  365. # true. Create it disabled so the binding doesn't trigger if it isn't
  366. # shown.
  367. if {$class eq "TkFDialog"} {
  368. set text [mc "Show &Hidden Files and Directories"]
  369. } else {
  370. set text [mc "Show &Hidden Directories"]
  371. }
  372. set data(hiddenBtn) [::tk::AmpWidget ttk::checkbutton $f2.hidden \
  373. -text $text -state disabled \
  374. -variable ::tk::dialog::file::showHiddenVar \
  375. -command [list ::tk::dialog::file::UpdateWhenIdle $w]]
  376. # -anchor w -padx 3
  377. # the okBtn is created after the typeMenu so that the keyboard traversal
  378. # is in the right order, and add binding so that we find out when the
  379. # dialog is destroyed by the user (added here instead of to the overall
  380. # window so no confusion about how much <Destroy> gets called; exactly
  381. # once will do). [Bug 987169]
  382. set data(okBtn) [::tk::AmpWidget ttk::button $f2.ok \
  383. -text [mc "&OK"] -default active];# -pady 3]
  384. bind $data(okBtn) <Destroy> [list ::tk::dialog::file::Destroyed $w]
  385. set data(cancelBtn) [::tk::AmpWidget ttk::button $f2.cancel \
  386. -text [mc "&Cancel"] -default normal];# -pady 3]
  387. # grid the widgets in f2
  388. #
  389. grid $f2.lab $f2.ent $data(okBtn) -padx 4 -pady 3 -sticky ew
  390. grid configure $f2.ent -padx 2
  391. if {$class eq "TkFDialog"} {
  392. grid $data(typeMenuLab) $data(typeMenuBtn) $data(cancelBtn) \
  393. -padx 4 -sticky ew
  394. grid configure $data(typeMenuBtn) -padx 0
  395. grid $data(hiddenBtn) -columnspan 2 -padx 4 -sticky ew
  396. } else {
  397. grid $data(hiddenBtn) - $data(cancelBtn) -padx 4 -sticky ew
  398. }
  399. grid columnconfigure $f2 1 -weight 1
  400. # Pack all the frames together. We are done with widget construction.
  401. #
  402. pack $f1 -side top -fill x -pady 4
  403. pack $f2 -side bottom -pady 4 -fill x
  404. pack $data(icons) -expand yes -fill both -padx 4 -pady 1
  405. # Set up the event handlers that are common to Directory and File Dialogs
  406. #
  407. wm protocol $w WM_DELETE_WINDOW [list ::tk::dialog::file::CancelCmd $w]
  408. $data(upBtn) configure -command [list ::tk::dialog::file::UpDirCmd $w]
  409. $data(cancelBtn) configure -command [list ::tk::dialog::file::CancelCmd $w]
  410. bind $w <KeyPress-Escape> [list $data(cancelBtn) invoke]
  411. bind $w <Alt-Key> [list tk::AltKeyInDialog $w %A]
  412. # Set up event handlers specific to File or Directory Dialogs
  413. #
  414. if {$class eq "TkFDialog"} {
  415. bind $data(ent) <Return> [list ::tk::dialog::file::ActivateEnt $w]
  416. $data(okBtn) configure -command [list ::tk::dialog::file::OkCmd $w]
  417. bind $w <Alt-t> [format {
  418. if {[%s cget -state] eq "normal"} {
  419. focus %s
  420. }
  421. } $data(typeMenuBtn) $data(typeMenuBtn)]
  422. } else {
  423. set okCmd [list ::tk::dialog::file::chooseDir::OkCmd $w]
  424. bind $data(ent) <Return> $okCmd
  425. $data(okBtn) configure -command $okCmd
  426. bind $w <Alt-s> [list focus $data(ent)]
  427. bind $w <Alt-o> [list $data(okBtn) invoke]
  428. }
  429. bind $w <Alt-h> [list $data(hiddenBtn) invoke]
  430. bind $data(ent) <Tab> [list ::tk::dialog::file::CompleteEnt $w]
  431. # Build the focus group for all the entries
  432. #
  433. ::tk::FocusGroup_Create $w
  434. ::tk::FocusGroup_BindIn $w $data(ent) [list \
  435. ::tk::dialog::file::EntFocusIn $w]
  436. ::tk::FocusGroup_BindOut $w $data(ent) [list \
  437. ::tk::dialog::file::EntFocusOut $w]
  438. }
  439. # ::tk::dialog::file::SetSelectMode --
  440. #
  441. # Set the select mode of the dialog to single select or multi-select.
  442. #
  443. # Arguments:
  444. # w The dialog path.
  445. # multi 1 if the dialog is multi-select; 0 otherwise.
  446. #
  447. # Results:
  448. # None.
  449. proc ::tk::dialog::file::SetSelectMode {w multi} {
  450. set dataName __tk_filedialog
  451. upvar ::tk::dialog::file::$dataName data
  452. if { $multi } {
  453. set fNameCaption [mc "File &names:"]
  454. } else {
  455. set fNameCaption [mc "File &name:"]
  456. }
  457. set iconListCommand [list ::tk::dialog::file::OkCmd $w]
  458. ::tk::SetAmpText $w.contents.f2.lab $fNameCaption
  459. $data(icons) configure -multiple $multi -command $iconListCommand
  460. return
  461. }
  462. # ::tk::dialog::file::UpdateWhenIdle --
  463. #
  464. # Creates an idle event handler which updates the dialog in idle time.
  465. # This is important because loading the directory may take a long time
  466. # and we don't want to load the same directory for multiple times due to
  467. # multiple concurrent events.
  468. #
  469. proc ::tk::dialog::file::UpdateWhenIdle {w} {
  470. upvar ::tk::dialog::file::[winfo name $w] data
  471. if {[info exists data(updateId)]} {
  472. return
  473. }
  474. set data(updateId) [after idle [list ::tk::dialog::file::Update $w]]
  475. }
  476. # ::tk::dialog::file::Update --
  477. #
  478. # Loads the files and directories into the IconList widget. Also sets up
  479. # the directory option menu for quick access to parent directories.
  480. #
  481. proc ::tk::dialog::file::Update {w} {
  482. # This proc may be called within an idle handler. Make sure that the
  483. # window has not been destroyed before this proc is called
  484. if {![winfo exists $w]} {
  485. return
  486. }
  487. set class [winfo class $w]
  488. if {($class ne "TkFDialog") && ($class ne "TkChooseDir")} {
  489. return
  490. }
  491. set dataName [winfo name $w]
  492. upvar ::tk::dialog::file::$dataName data
  493. variable ::tk::Priv
  494. variable showHiddenVar
  495. global tk_library
  496. unset -nocomplain data(updateId)
  497. set folder $Priv(folderImage)
  498. set file $Priv(fileImage)
  499. set appPWD [pwd]
  500. if {[catch {
  501. cd $data(selectPath)
  502. }]} then {
  503. # We cannot change directory to $data(selectPath). $data(selectPath)
  504. # should have been checked before ::tk::dialog::file::Update is
  505. # called, so we normally won't come to here. Anyways, give an error
  506. # and abort action.
  507. tk_messageBox -type ok -parent $w -icon warning -message [mc \
  508. "Cannot change to the directory \"%1\$s\".\nPermission denied."\
  509. $data(selectPath)]
  510. cd $appPWD
  511. return
  512. }
  513. # Turn on the busy cursor. BUG?? We haven't disabled X events, though,
  514. # so the user may still click and cause havoc ...
  515. #
  516. set entCursor [$data(ent) cget -cursor]
  517. set dlgCursor [$w cget -cursor]
  518. $data(ent) configure -cursor watch
  519. $w configure -cursor watch
  520. update idletasks
  521. $data(icons) deleteall
  522. set showHidden $showHiddenVar
  523. # Make the dir list. Note that using an explicit [pwd] (instead of '.') is
  524. # better in some VFS cases.
  525. $data(icons) add $folder [GlobFiltered [pwd] d 1]
  526. if {$class eq "TkFDialog"} {
  527. # Make the file list if this is a File Dialog, selecting all but
  528. # 'd'irectory type files.
  529. #
  530. $data(icons) add $file [GlobFiltered [pwd] {f b c l p s}]
  531. }
  532. # Update the Directory: option menu
  533. #
  534. set list ""
  535. set dir ""
  536. foreach subdir [file split $data(selectPath)] {
  537. set dir [file join $dir $subdir]
  538. lappend list $dir
  539. }
  540. $data(dirMenu) delete 0 end
  541. set var [format %s(selectPath) ::tk::dialog::file::$dataName]
  542. foreach path $list {
  543. $data(dirMenu) add command -label $path -command [list set $var $path]
  544. }
  545. # Restore the PWD to the application's PWD
  546. #
  547. cd $appPWD
  548. if {$class eq "TkFDialog"} {
  549. # Restore the Open/Save Button if this is a File Dialog
  550. #
  551. if {$data(type) eq "open"} {
  552. ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  553. } else {
  554. ::tk::SetAmpText $data(okBtn) [mc "&Save"]
  555. }
  556. }
  557. # turn off the busy cursor.
  558. #
  559. $data(ent) configure -cursor $entCursor
  560. $w configure -cursor $dlgCursor
  561. }
  562. # ::tk::dialog::file::SetPathSilently --
  563. #
  564. # Sets data(selectPath) without invoking the trace procedure
  565. #
  566. proc ::tk::dialog::file::SetPathSilently {w path} {
  567. upvar ::tk::dialog::file::[winfo name $w] data
  568. set cb [list ::tk::dialog::file::SetPath $w]
  569. trace remove variable data(selectPath) write $cb
  570. set data(selectPath) $path
  571. trace add variable data(selectPath) write $cb
  572. }
  573. # This proc gets called whenever data(selectPath) is set
  574. #
  575. proc ::tk::dialog::file::SetPath {w name1 name2 op} {
  576. if {[winfo exists $w]} {
  577. upvar ::tk::dialog::file::[winfo name $w] data
  578. UpdateWhenIdle $w
  579. # On directory dialogs, we keep the entry in sync with the currentdir.
  580. if {[winfo class $w] eq "TkChooseDir"} {
  581. $data(ent) delete 0 end
  582. $data(ent) insert end $data(selectPath)
  583. }
  584. }
  585. }
  586. # This proc gets called whenever data(filter) is set
  587. #
  588. proc ::tk::dialog::file::SetFilter {w type} {
  589. upvar ::tk::dialog::file::[winfo name $w] data
  590. set data(filterType) $type
  591. set data(filter) [lindex $type 1]
  592. $data(typeMenuBtn) configure -text [lindex $type 0] ;#-indicatoron 1
  593. # If we aren't using a default extension, use the one suppled by the
  594. # filter.
  595. if {![info exists data(extUsed)]} {
  596. if {[string length $data(-defaultextension)]} {
  597. set data(extUsed) 1
  598. } else {
  599. set data(extUsed) 0
  600. }
  601. }
  602. if {!$data(extUsed)} {
  603. # Get the first extension in the list that matches {^\*\.\w+$} and
  604. # remove all * from the filter.
  605. set index [lsearch -regexp $data(filter) {^\*\.\w+$}]
  606. if {$index >= 0} {
  607. set data(-defaultextension) \
  608. [string trimleft [lindex $data(filter) $index] "*"]
  609. } else {
  610. # Couldn't find anything! Reset to a safe default...
  611. set data(-defaultextension) ""
  612. }
  613. }
  614. $data(icons) see 0
  615. UpdateWhenIdle $w
  616. }
  617. # tk::dialog::file::ResolveFile --
  618. #
  619. # Interpret the user's text input in a file selection dialog. Performs:
  620. #
  621. # (1) ~ substitution
  622. # (2) resolve all instances of . and ..
  623. # (3) check for non-existent files/directories
  624. # (4) check for chdir permissions
  625. # (5) conversion of environment variable references to their
  626. # contents (once only)
  627. #
  628. # Arguments:
  629. # context: the current directory you are in
  630. # text: the text entered by the user
  631. # defaultext: the default extension to add to files with no extension
  632. # expandEnv: whether to expand environment variables (yes by default)
  633. #
  634. # Return vaue:
  635. # [list $flag $directory $file]
  636. #
  637. # flag = OK : valid input
  638. # = PATTERN : valid directory/pattern
  639. # = PATH : the directory does not exist
  640. # = FILE : the directory exists by the file doesn't exist
  641. # = CHDIR : Cannot change to the directory
  642. # = ERROR : Invalid entry
  643. #
  644. # directory : valid only if flag = OK or PATTERN or FILE
  645. # file : valid only if flag = OK or PATTERN
  646. #
  647. # directory may not be the same as context, because text may contain a
  648. # subdirectory name
  649. #
  650. proc ::tk::dialog::file::ResolveFile {context text defaultext {expandEnv 1}} {
  651. set appPWD [pwd]
  652. set path [JoinFile $context $text]
  653. # If the file has no extension, append the default. Be careful not to do
  654. # this for directories, otherwise typing a dirname in the box will give
  655. # back "dirname.extension" instead of trying to change dir.
  656. if {
  657. ![file isdirectory $path] && ([file ext $path] eq "") &&
  658. ![string match {$*} [file tail $path]]
  659. } then {
  660. set path "$path$defaultext"
  661. }
  662. if {[catch {file exists $path}]} {
  663. # This "if" block can be safely removed if the following code stop
  664. # generating errors.
  665. #
  666. # file exists ~nonsuchuser
  667. #
  668. return [list ERROR $path ""]
  669. }
  670. if {[file exists $path]} {
  671. if {[file isdirectory $path]} {
  672. if {[catch {cd $path}]} {
  673. return [list CHDIR $path ""]
  674. }
  675. set directory [pwd]
  676. set file ""
  677. set flag OK
  678. cd $appPWD
  679. } else {
  680. if {[catch {cd [file dirname $path]}]} {
  681. return [list CHDIR [file dirname $path] ""]
  682. }
  683. set directory [pwd]
  684. set file [file tail $path]
  685. set flag OK
  686. cd $appPWD
  687. }
  688. } else {
  689. set dirname [file dirname $path]
  690. if {[file exists $dirname]} {
  691. if {[catch {cd $dirname}]} {
  692. return [list CHDIR $dirname ""]
  693. }
  694. set directory [pwd]
  695. cd $appPWD
  696. set file [file tail $path]
  697. # It's nothing else, so check to see if it is an env-reference
  698. if {$expandEnv && [string match {$*} $file]} {
  699. set var [string range $file 1 end]
  700. if {[info exist ::env($var)]} {
  701. return [ResolveFile $context $::env($var) $defaultext 0]
  702. }
  703. }
  704. if {[regexp {[*?]} $file]} {
  705. set flag PATTERN
  706. } else {
  707. set flag FILE
  708. }
  709. } else {
  710. set directory $dirname
  711. set file [file tail $path]
  712. set flag PATH
  713. # It's nothing else, so check to see if it is an env-reference
  714. if {$expandEnv && [string match {$*} $file]} {
  715. set var [string range $file 1 end]
  716. if {[info exist ::env($var)]} {
  717. return [ResolveFile $context $::env($var) $defaultext 0]
  718. }
  719. }
  720. }
  721. }
  722. return [list $flag $directory $file]
  723. }
  724. # Gets called when the entry box gets keyboard focus. We clear the selection
  725. # from the icon list . This way the user can be certain that the input in the
  726. # entry box is the selection.
  727. #
  728. proc ::tk::dialog::file::EntFocusIn {w} {
  729. upvar ::tk::dialog::file::[winfo name $w] data
  730. if {[$data(ent) get] ne ""} {
  731. $data(ent) selection range 0 end
  732. $data(ent) icursor end
  733. } else {
  734. $data(ent) selection clear
  735. }
  736. if {[winfo class $w] eq "TkFDialog"} {
  737. # If this is a File Dialog, make sure the buttons are labeled right.
  738. if {$data(type) eq "open"} {
  739. ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  740. } else {
  741. ::tk::SetAmpText $data(okBtn) [mc "&Save"]
  742. }
  743. }
  744. }
  745. proc ::tk::dialog::file::EntFocusOut {w} {
  746. upvar ::tk::dialog::file::[winfo name $w] data
  747. $data(ent) selection clear
  748. }
  749. # Gets called when user presses Return in the "File name" entry.
  750. #
  751. proc ::tk::dialog::file::ActivateEnt {w} {
  752. upvar ::tk::dialog::file::[winfo name $w] data
  753. set text [$data(ent) get]
  754. if {$data(-multiple)} {
  755. foreach t $text {
  756. VerifyFileName $w $t
  757. }
  758. } else {
  759. VerifyFileName $w $text
  760. }
  761. }
  762. # Verification procedure
  763. #
  764. proc ::tk::dialog::file::VerifyFileName {w filename} {
  765. upvar ::tk::dialog::file::[winfo name $w] data
  766. set list [ResolveFile $data(selectPath) $filename $data(-defaultextension)]
  767. foreach {flag path file} $list {
  768. break
  769. }
  770. switch -- $flag {
  771. OK {
  772. if {$file eq ""} {
  773. # user has entered an existing (sub)directory
  774. set data(selectPath) $path
  775. $data(ent) delete 0 end
  776. } else {
  777. SetPathSilently $w $path
  778. if {$data(-multiple)} {
  779. lappend data(selectFile) $file
  780. } else {
  781. set data(selectFile) $file
  782. }
  783. Done $w
  784. }
  785. }
  786. PATTERN {
  787. set data(selectPath) $path
  788. set data(filter) $file
  789. }
  790. FILE {
  791. if {$data(type) eq "open"} {
  792. tk_messageBox -icon warning -type ok -parent $w \
  793. -message [mc "File \"%1\$s\" does not exist." \
  794. [file join $path $file]]
  795. $data(ent) selection range 0 end
  796. $data(ent) icursor end
  797. } else {
  798. SetPathSilently $w $path
  799. if {$data(-multiple)} {
  800. lappend data(selectFile) $file
  801. } else {
  802. set data(selectFile) $file
  803. }
  804. Done $w
  805. }
  806. }
  807. PATH {
  808. tk_messageBox -icon warning -type ok -parent $w \
  809. -message [mc "Directory \"%1\$s\" does not exist." $path]
  810. $data(ent) selection range 0 end
  811. $data(ent) icursor end
  812. }
  813. CHDIR {
  814. tk_messageBox -type ok -parent $w -icon warning -message \
  815. [mc "Cannot change to the directory\
  816. \"%1\$s\".\nPermission denied." $path]
  817. $data(ent) selection range 0 end
  818. $data(ent) icursor end
  819. }
  820. ERROR {
  821. tk_messageBox -type ok -parent $w -icon warning -message \
  822. [mc "Invalid file name \"%1\$s\"." $path]
  823. $data(ent) selection range 0 end
  824. $data(ent) icursor end
  825. }
  826. }
  827. }
  828. # Gets called when user presses the Alt-s or Alt-o keys.
  829. #
  830. proc ::tk::dialog::file::InvokeBtn {w key} {
  831. upvar ::tk::dialog::file::[winfo name $w] data
  832. if {[$data(okBtn) cget -text] eq $key} {
  833. $data(okBtn) invoke
  834. }
  835. }
  836. # Gets called when user presses the "parent directory" button
  837. #
  838. proc ::tk::dialog::file::UpDirCmd {w} {
  839. upvar ::tk::dialog::file::[winfo name $w] data
  840. if {$data(selectPath) ne "/"} {
  841. set data(selectPath) [file dirname $data(selectPath)]
  842. }
  843. }
  844. # Join a file name to a path name. The "file join" command will break if the
  845. # filename begins with ~
  846. #
  847. proc ::tk::dialog::file::JoinFile {path file} {
  848. if {[string match {~*} $file] && [file exists $path/$file]} {
  849. return [file join $path ./$file]
  850. } else {
  851. return [file join $path $file]
  852. }
  853. }
  854. # Gets called when user presses the "OK" button
  855. #
  856. proc ::tk::dialog::file::OkCmd {w} {
  857. upvar ::tk::dialog::file::[winfo name $w] data
  858. set filenames {}
  859. foreach item [$data(icons) selection get] {
  860. lappend filenames [$data(icons) get $item]
  861. }
  862. if {
  863. ([llength $filenames] && !$data(-multiple)) ||
  864. ($data(-multiple) && ([llength $filenames] == 1))
  865. } then {
  866. set filename [lindex $filenames 0]
  867. set file [JoinFile $data(selectPath) $filename]
  868. if {[file isdirectory $file]} {
  869. ListInvoke $w [list $filename]
  870. return
  871. }
  872. }
  873. ActivateEnt $w
  874. }
  875. # Gets called when user presses the "Cancel" button
  876. #
  877. proc ::tk::dialog::file::CancelCmd {w} {
  878. upvar ::tk::dialog::file::[winfo name $w] data
  879. variable ::tk::Priv
  880. bind $data(okBtn) <Destroy> {}
  881. set Priv(selectFilePath) ""
  882. }
  883. # Gets called when user destroys the dialog directly [Bug 987169]
  884. #
  885. proc ::tk::dialog::file::Destroyed {w} {
  886. upvar ::tk::dialog::file::[winfo name $w] data
  887. variable ::tk::Priv
  888. set Priv(selectFilePath) ""
  889. }
  890. # Gets called when user browses the IconList widget (dragging mouse, arrow
  891. # keys, etc)
  892. #
  893. proc ::tk::dialog::file::ListBrowse {w} {
  894. upvar ::tk::dialog::file::[winfo name $w] data
  895. set text {}
  896. foreach item [$data(icons) selection get] {
  897. lappend text [$data(icons) get $item]
  898. }
  899. if {[llength $text] == 0} {
  900. return
  901. }
  902. if {$data(-multiple)} {
  903. set newtext {}
  904. foreach file $text {
  905. set fullfile [JoinFile $data(selectPath) $file]
  906. if { ![file isdirectory $fullfile] } {
  907. lappend newtext $file
  908. }
  909. }
  910. set text $newtext
  911. set isDir 0
  912. } else {
  913. set text [lindex $text 0]
  914. set file [JoinFile $data(selectPath) $text]
  915. set isDir [file isdirectory $file]
  916. }
  917. if {!$isDir} {
  918. $data(ent) delete 0 end
  919. $data(ent) insert 0 $text
  920. if {[winfo class $w] eq "TkFDialog"} {
  921. if {$data(type) eq "open"} {
  922. ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  923. } else {
  924. ::tk::SetAmpText $data(okBtn) [mc "&Save"]
  925. }
  926. }
  927. } elseif {[winfo class $w] eq "TkFDialog"} {
  928. ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  929. }
  930. }
  931. # Gets called when user invokes the IconList widget (double-click, Return key,
  932. # etc)
  933. #
  934. proc ::tk::dialog::file::ListInvoke {w filenames} {
  935. upvar ::tk::dialog::file::[winfo name $w] data
  936. if {[llength $filenames] == 0} {
  937. return
  938. }
  939. set file [JoinFile $data(selectPath) [lindex $filenames 0]]
  940. set class [winfo class $w]
  941. if {$class eq "TkChooseDir" || [file isdirectory $file]} {
  942. set appPWD [pwd]
  943. if {[catch {cd $file}]} {
  944. tk_messageBox -type ok -parent $w -icon warning -message \
  945. [mc "Cannot change to the directory \"%1\$s\".\nPermission denied." $file]
  946. } else {
  947. cd $appPWD
  948. set data(selectPath) $file
  949. }
  950. } else {
  951. if {$data(-multiple)} {
  952. set data(selectFile) $filenames
  953. } else {
  954. set data(selectFile) $file
  955. }
  956. Done $w
  957. }
  958. }
  959. # ::tk::dialog::file::Done --
  960. #
  961. # Gets called when user has input a valid filename. Pops up a dialog
  962. # box to confirm selection when necessary. Sets the
  963. # tk::Priv(selectFilePath) variable, which will break the "vwait" loop
  964. # in ::tk::dialog::file:: and return the selected filename to the script
  965. # that calls tk_getOpenFile or tk_getSaveFile
  966. #
  967. proc ::tk::dialog::file::Done {w {selectFilePath ""}} {
  968. upvar ::tk::dialog::file::[winfo name $w] data
  969. variable ::tk::Priv
  970. if {$selectFilePath eq ""} {
  971. if {$data(-multiple)} {
  972. set selectFilePath {}
  973. foreach f $data(selectFile) {
  974. lappend selectFilePath [JoinFile $data(selectPath) $f]
  975. }
  976. } else {
  977. set selectFilePath [JoinFile $data(selectPath) $data(selectFile)]
  978. }
  979. set Priv(selectFile) $data(selectFile)
  980. set Priv(selectPath) $data(selectPath)
  981. if {($data(type) eq "save") && $data(-confirmoverwrite) && [file exists $selectFilePath]} {
  982. set reply [tk_messageBox -icon warning -type yesno -parent $w \
  983. -message [mc "File \"%1\$s\" already exists.\nDo you want\
  984. to overwrite it?" $selectFilePath]]
  985. if {$reply eq "no"} {
  986. return
  987. }
  988. }
  989. if {
  990. [info exists data(-typevariable)] && $data(-typevariable) ne ""
  991. && [info exists data(-filetypes)] && [llength $data(-filetypes)]
  992. && [info exists data(filterType)] && $data(filterType) ne ""
  993. } then {
  994. upvar #0 $data(-typevariable) typeVariable
  995. set typeVariable [lindex $data(filterType) 0]
  996. }
  997. }
  998. bind $data(okBtn) <Destroy> {}
  999. set Priv(selectFilePath) $selectFilePath
  1000. }
  1001. # ::tk::dialog::file::GlobFiltered --
  1002. #
  1003. # Gets called to do globbing, returning the results and filtering them
  1004. # according to the current filter (and removing the entries for '.' and
  1005. # '..' which are never shown). Deals with evil cases such as where the
  1006. # user is supplying a filter which is an invalid list or where it has an
  1007. # unbalanced brace. The resulting list will be dictionary sorted.
  1008. #
  1009. # Arguments:
  1010. # dir Which directory to search
  1011. # type List of filetypes to look for ('d' or 'f b c l p s')
  1012. # overrideFilter Whether to ignore the filter for this search.
  1013. #
  1014. # NB: Assumes that the caller has mapped the state variable to 'data'.
  1015. #
  1016. proc ::tk::dialog::file::GlobFiltered {dir type {overrideFilter 0}} {
  1017. variable showHiddenVar
  1018. upvar 1 data(filter) filter
  1019. if {$filter eq "*" || $overrideFilter} {
  1020. set patterns [list *]
  1021. if {$showHiddenVar} {
  1022. lappend patterns .*
  1023. }
  1024. } elseif {[string is list $filter]} {
  1025. set patterns $filter
  1026. } else {
  1027. # Invalid list; assume we can use non-whitespace sequences as words
  1028. set patterns [regexp -inline -all {\S+} $filter]
  1029. }
  1030. set opts [list -tails -directory $dir -type $type -nocomplain]
  1031. set result {}
  1032. catch {
  1033. # We have a catch because we might have a really bad pattern (e.g.,
  1034. # with an unbalanced brace); even [glob -nocomplain] doesn't like it.
  1035. # Using a catch ensures that it just means we match nothing instead of
  1036. # throwing a nasty error at the user...
  1037. foreach f [glob {*}$opts -- {*}$patterns] {
  1038. if {$f eq "." || $f eq ".."} {
  1039. continue
  1040. }
  1041. # See ticket [1641721], $f might be a link pointing to a dir
  1042. if {$type != "d" && [file isdir [file join $dir $f]]} {
  1043. continue
  1044. }
  1045. lappend result $f
  1046. }
  1047. }
  1048. return [lsort -dictionary -unique $result]
  1049. }
  1050. proc ::tk::dialog::file::CompleteEnt {w} {
  1051. upvar ::tk::dialog::file::[winfo name $w] data
  1052. set f [$data(ent) get]
  1053. if {$data(-multiple)} {
  1054. if {![string is list $f] || [llength $f] != 1} {
  1055. return -code break
  1056. }
  1057. set f [lindex $f 0]
  1058. }
  1059. # Get list of matching filenames and dirnames
  1060. set files [if {[winfo class $w] eq "TkFDialog"} {
  1061. GlobFiltered $data(selectPath) {f b c l p s}
  1062. }]
  1063. set dirs2 {}
  1064. foreach d [GlobFiltered $data(selectPath) d] {lappend dirs2 $d/}
  1065. set targets [concat \
  1066. [lsearch -glob -all -inline $files $f*] \
  1067. [lsearch -glob -all -inline $dirs2 $f*]]
  1068. if {[llength $targets] == 1} {
  1069. # We have a winner!
  1070. set f [lindex $targets 0]
  1071. } elseif {$f in $targets || [llength $targets] == 0} {
  1072. if {[string length $f] > 0} {
  1073. bell
  1074. }
  1075. return
  1076. } elseif {[llength $targets] > 1} {
  1077. # Multiple possibles
  1078. if {[string length $f] == 0} {
  1079. return
  1080. }
  1081. set t0 [lindex $targets 0]
  1082. for {set len [string length $t0]} {$len>0} {} {
  1083. set allmatch 1
  1084. foreach s $targets {
  1085. if {![string equal -length $len $s $t0]} {
  1086. set allmatch 0
  1087. break
  1088. }
  1089. }
  1090. incr len -1
  1091. if {$allmatch} break
  1092. }
  1093. set f [string range $t0 0 $len]
  1094. }
  1095. if {$data(-multiple)} {
  1096. set f [list $f]
  1097. }
  1098. $data(ent) delete 0 end
  1099. $data(ent) insert 0 $f
  1100. return -code break
  1101. }