tkfbox.tcl 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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(origfiletypes) $data(-filetypes)
  278. set data(-filetypes) [::tk::FDGetFileTypes $data(-filetypes)]
  279. if {![winfo exists $data(-parent)]} {
  280. return -code error -errorcode [list TK LOOKUP WINDOW $data(-parent)] \
  281. "bad window path name \"$data(-parent)\""
  282. }
  283. # Set -multiple to a one or zero value (not other boolean types like
  284. # "yes") so we can use it in tests more easily.
  285. if {$type eq "save"} {
  286. set data(-multiple) 0
  287. } elseif {$data(-multiple)} {
  288. set data(-multiple) 1
  289. } else {
  290. set data(-multiple) 0
  291. }
  292. }
  293. proc ::tk::dialog::file::Create {w class} {
  294. set dataName [lindex [split $w .] end]
  295. upvar ::tk::dialog::file::$dataName data
  296. variable ::tk::Priv
  297. global tk_library
  298. toplevel $w -class $class
  299. if {[tk windowingsystem] eq "x11"} {wm attributes $w -type dialog}
  300. pack [ttk::frame $w.contents] -expand 1 -fill both
  301. #set w $w.contents
  302. # f1: the frame with the directory option menu
  303. #
  304. set f1 [ttk::frame $w.contents.f1]
  305. bind [::tk::AmpWidget ttk::label $f1.lab -text [mc "&Directory:"]] \
  306. <<AltUnderlined>> [list focus $f1.menu]
  307. set data(dirMenuBtn) $f1.menu
  308. if {![info exists data(selectPath)]} {
  309. set data(selectPath) ""
  310. }
  311. set data(dirMenu) $f1.menu.menu
  312. ttk::menubutton $f1.menu -menu $data(dirMenu) -direction flush \
  313. -textvariable [format %s(selectPath) ::tk::dialog::file::$dataName]
  314. menu $data(dirMenu) -tearoff 0
  315. $data(dirMenu) add radiobutton -label "" -variable \
  316. [format %s(selectPath) ::tk::dialog::file::$dataName]
  317. set data(upBtn) [ttk::button $f1.up]
  318. $data(upBtn) configure -image $Priv(updirImage)
  319. $f1.menu configure -takefocus 1;# -highlightthickness 2
  320. pack $data(upBtn) -side right -padx 4 -fill both
  321. pack $f1.lab -side left -padx 4 -fill both
  322. pack $f1.menu -expand yes -fill both -padx 4
  323. # data(icons): the IconList that list the files and directories.
  324. #
  325. if {$class eq "TkFDialog"} {
  326. if { $data(-multiple) } {
  327. set fNameCaption [mc "File &names:"]
  328. } else {
  329. set fNameCaption [mc "File &name:"]
  330. }
  331. set fTypeCaption [mc "Files of &type:"]
  332. set iconListCommand [list ::tk::dialog::file::OkCmd $w]
  333. } else {
  334. set fNameCaption [mc "&Selection:"]
  335. set iconListCommand [list ::tk::dialog::file::chooseDir::DblClick $w]
  336. }
  337. set data(icons) [::tk::IconList $w.contents.icons \
  338. -command $iconListCommand -multiple $data(-multiple)]
  339. bind $data(icons) <<ListboxSelect>> \
  340. [list ::tk::dialog::file::ListBrowse $w]
  341. # f2: the frame with the OK button, cancel button, "file name" field
  342. # and file types field.
  343. #
  344. set f2 [ttk::frame $w.contents.f2]
  345. bind [::tk::AmpWidget ttk::label $f2.lab -text $fNameCaption -anchor e]\
  346. <<AltUnderlined>> [list focus $f2.ent]
  347. # -pady 0
  348. set data(ent) [ttk::entry $f2.ent]
  349. # The font to use for the icons. The default Canvas font on Unix is just
  350. # deviant.
  351. set ::tk::$w.contents.icons(font) [$data(ent) cget -font]
  352. # Make the file types bits only if this is a File Dialog
  353. if {$class eq "TkFDialog"} {
  354. set data(typeMenuLab) [::tk::AmpWidget ttk::label $f2.lab2 \
  355. -text $fTypeCaption -anchor e]
  356. # -pady [$f2.lab cget -pady]
  357. set data(typeMenuBtn) [ttk::menubutton $f2.menu \
  358. -menu $f2.menu.m]
  359. # -indicatoron 1
  360. set data(typeMenu) [menu $data(typeMenuBtn).m -tearoff 0]
  361. # $data(typeMenuBtn) configure -takefocus 1 -relief raised -anchor w
  362. bind $data(typeMenuLab) <<AltUnderlined>> [list \
  363. focus $data(typeMenuBtn)]
  364. }
  365. # The hidden button is displayed when ::tk::dialog::file::showHiddenBtn is
  366. # true. Create it disabled so the binding doesn't trigger if it isn't
  367. # shown.
  368. if {$class eq "TkFDialog"} {
  369. set text [mc "Show &Hidden Files and Directories"]
  370. } else {
  371. set text [mc "Show &Hidden Directories"]
  372. }
  373. set data(hiddenBtn) [::tk::AmpWidget ttk::checkbutton $f2.hidden \
  374. -text $text -state disabled \
  375. -variable ::tk::dialog::file::showHiddenVar \
  376. -command [list ::tk::dialog::file::UpdateWhenIdle $w]]
  377. # -anchor w -padx 3
  378. # the okBtn is created after the typeMenu so that the keyboard traversal
  379. # is in the right order, and add binding so that we find out when the
  380. # dialog is destroyed by the user (added here instead of to the overall
  381. # window so no confusion about how much <Destroy> gets called; exactly
  382. # once will do). [Bug 987169]
  383. set data(okBtn) [::tk::AmpWidget ttk::button $f2.ok \
  384. -text [mc "&OK"] -default active];# -pady 3]
  385. bind $data(okBtn) <Destroy> [list ::tk::dialog::file::Destroyed $w]
  386. set data(cancelBtn) [::tk::AmpWidget ttk::button $f2.cancel \
  387. -text [mc "&Cancel"] -default normal];# -pady 3]
  388. # grid the widgets in f2
  389. #
  390. grid $f2.lab $f2.ent $data(okBtn) -padx 4 -pady 3 -sticky ew
  391. grid configure $f2.ent -padx 2
  392. if {$class eq "TkFDialog"} {
  393. grid $data(typeMenuLab) $data(typeMenuBtn) $data(cancelBtn) \
  394. -padx 4 -sticky ew
  395. grid configure $data(typeMenuBtn) -padx 0
  396. grid $data(hiddenBtn) -columnspan 2 -padx 4 -sticky ew
  397. } else {
  398. grid $data(hiddenBtn) - $data(cancelBtn) -padx 4 -sticky ew
  399. }
  400. grid columnconfigure $f2 1 -weight 1
  401. # Pack all the frames together. We are done with widget construction.
  402. #
  403. pack $f1 -side top -fill x -pady 4
  404. pack $f2 -side bottom -pady 4 -fill x
  405. pack $data(icons) -expand yes -fill both -padx 4 -pady 1
  406. # Set up the event handlers that are common to Directory and File Dialogs
  407. #
  408. wm protocol $w WM_DELETE_WINDOW [list ::tk::dialog::file::CancelCmd $w]
  409. $data(upBtn) configure -command [list ::tk::dialog::file::UpDirCmd $w]
  410. $data(cancelBtn) configure -command [list ::tk::dialog::file::CancelCmd $w]
  411. bind $w <KeyPress-Escape> [list $data(cancelBtn) invoke]
  412. bind $w <Alt-Key> [list tk::AltKeyInDialog $w %A]
  413. # Set up event handlers specific to File or Directory Dialogs
  414. #
  415. if {$class eq "TkFDialog"} {
  416. bind $data(ent) <Return> [list ::tk::dialog::file::ActivateEnt $w]
  417. $data(okBtn) configure -command [list ::tk::dialog::file::OkCmd $w]
  418. bind $w <Alt-t> [format {
  419. if {[%s cget -state] eq "normal"} {
  420. focus %s
  421. }
  422. } $data(typeMenuBtn) $data(typeMenuBtn)]
  423. } else {
  424. set okCmd [list ::tk::dialog::file::chooseDir::OkCmd $w]
  425. bind $data(ent) <Return> $okCmd
  426. $data(okBtn) configure -command $okCmd
  427. bind $w <Alt-s> [list focus $data(ent)]
  428. bind $w <Alt-o> [list $data(okBtn) invoke]
  429. }
  430. bind $w <Alt-h> [list $data(hiddenBtn) invoke]
  431. bind $data(ent) <Tab> [list ::tk::dialog::file::CompleteEnt $w]
  432. # Build the focus group for all the entries
  433. #
  434. ::tk::FocusGroup_Create $w
  435. ::tk::FocusGroup_BindIn $w $data(ent) [list \
  436. ::tk::dialog::file::EntFocusIn $w]
  437. ::tk::FocusGroup_BindOut $w $data(ent) [list \
  438. ::tk::dialog::file::EntFocusOut $w]
  439. }
  440. # ::tk::dialog::file::SetSelectMode --
  441. #
  442. # Set the select mode of the dialog to single select or multi-select.
  443. #
  444. # Arguments:
  445. # w The dialog path.
  446. # multi 1 if the dialog is multi-select; 0 otherwise.
  447. #
  448. # Results:
  449. # None.
  450. proc ::tk::dialog::file::SetSelectMode {w multi} {
  451. set dataName __tk_filedialog
  452. upvar ::tk::dialog::file::$dataName data
  453. if { $multi } {
  454. set fNameCaption [mc "File &names:"]
  455. } else {
  456. set fNameCaption [mc "File &name:"]
  457. }
  458. set iconListCommand [list ::tk::dialog::file::OkCmd $w]
  459. ::tk::SetAmpText $w.contents.f2.lab $fNameCaption
  460. $data(icons) configure -multiple $multi -command $iconListCommand
  461. return
  462. }
  463. # ::tk::dialog::file::UpdateWhenIdle --
  464. #
  465. # Creates an idle event handler which updates the dialog in idle time.
  466. # This is important because loading the directory may take a long time
  467. # and we don't want to load the same directory for multiple times due to
  468. # multiple concurrent events.
  469. #
  470. proc ::tk::dialog::file::UpdateWhenIdle {w} {
  471. upvar ::tk::dialog::file::[winfo name $w] data
  472. if {[info exists data(updateId)]} {
  473. return
  474. }
  475. set data(updateId) [after idle [list ::tk::dialog::file::Update $w]]
  476. }
  477. # ::tk::dialog::file::Update --
  478. #
  479. # Loads the files and directories into the IconList widget. Also sets up
  480. # the directory option menu for quick access to parent directories.
  481. #
  482. proc ::tk::dialog::file::Update {w} {
  483. # This proc may be called within an idle handler. Make sure that the
  484. # window has not been destroyed before this proc is called
  485. if {![winfo exists $w]} {
  486. return
  487. }
  488. set class [winfo class $w]
  489. if {($class ne "TkFDialog") && ($class ne "TkChooseDir")} {
  490. return
  491. }
  492. set dataName [winfo name $w]
  493. upvar ::tk::dialog::file::$dataName data
  494. variable ::tk::Priv
  495. variable showHiddenVar
  496. global tk_library
  497. unset -nocomplain data(updateId)
  498. set folder $Priv(folderImage)
  499. set file $Priv(fileImage)
  500. set appPWD [pwd]
  501. if {[catch {
  502. cd $data(selectPath)
  503. }]} then {
  504. # We cannot change directory to $data(selectPath). $data(selectPath)
  505. # should have been checked before ::tk::dialog::file::Update is
  506. # called, so we normally won't come to here. Anyways, give an error
  507. # and abort action.
  508. tk_messageBox -type ok -parent $w -icon warning -message [mc \
  509. "Cannot change to the directory \"%1\$s\".\nPermission denied."\
  510. $data(selectPath)]
  511. cd $appPWD
  512. return
  513. }
  514. # Turn on the busy cursor. BUG?? We haven't disabled X events, though,
  515. # so the user may still click and cause havoc ...
  516. #
  517. set entCursor [$data(ent) cget -cursor]
  518. set dlgCursor [$w cget -cursor]
  519. $data(ent) configure -cursor watch
  520. $w configure -cursor watch
  521. update idletasks
  522. $data(icons) deleteall
  523. set showHidden $showHiddenVar
  524. # Make the dir list. Note that using an explicit [pwd] (instead of '.') is
  525. # better in some VFS cases.
  526. $data(icons) add $folder [GlobFiltered [pwd] d 1]
  527. if {$class eq "TkFDialog"} {
  528. # Make the file list if this is a File Dialog, selecting all but
  529. # 'd'irectory type files.
  530. #
  531. $data(icons) add $file [GlobFiltered [pwd] {f b c l p s}]
  532. }
  533. # Update the Directory: option menu
  534. #
  535. set list ""
  536. set dir ""
  537. foreach subdir [file split $data(selectPath)] {
  538. set dir [file join $dir $subdir]
  539. lappend list $dir
  540. }
  541. $data(dirMenu) delete 0 end
  542. set var [format %s(selectPath) ::tk::dialog::file::$dataName]
  543. foreach path $list {
  544. $data(dirMenu) add command -label $path -command [list set $var $path]
  545. }
  546. # Restore the PWD to the application's PWD
  547. #
  548. cd $appPWD
  549. if {$class eq "TkFDialog"} {
  550. # Restore the Open/Save Button if this is a File Dialog
  551. #
  552. if {$data(type) eq "open"} {
  553. ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  554. } else {
  555. ::tk::SetAmpText $data(okBtn) [mc "&Save"]
  556. }
  557. }
  558. # turn off the busy cursor.
  559. #
  560. $data(ent) configure -cursor $entCursor
  561. $w configure -cursor $dlgCursor
  562. }
  563. # ::tk::dialog::file::SetPathSilently --
  564. #
  565. # Sets data(selectPath) without invoking the trace procedure
  566. #
  567. proc ::tk::dialog::file::SetPathSilently {w path} {
  568. upvar ::tk::dialog::file::[winfo name $w] data
  569. set cb [list ::tk::dialog::file::SetPath $w]
  570. trace remove variable data(selectPath) write $cb
  571. set data(selectPath) $path
  572. trace add variable data(selectPath) write $cb
  573. }
  574. # This proc gets called whenever data(selectPath) is set
  575. #
  576. proc ::tk::dialog::file::SetPath {w name1 name2 op} {
  577. if {[winfo exists $w]} {
  578. upvar ::tk::dialog::file::[winfo name $w] data
  579. UpdateWhenIdle $w
  580. # On directory dialogs, we keep the entry in sync with the currentdir.
  581. if {[winfo class $w] eq "TkChooseDir"} {
  582. $data(ent) delete 0 end
  583. $data(ent) insert end $data(selectPath)
  584. }
  585. }
  586. }
  587. # This proc gets called whenever data(filter) is set
  588. #
  589. proc ::tk::dialog::file::SetFilter {w type} {
  590. upvar ::tk::dialog::file::[winfo name $w] data
  591. set data(filterType) $type
  592. set data(filter) [lindex $type 1]
  593. $data(typeMenuBtn) configure -text [lindex $type 0] ;#-indicatoron 1
  594. # If we aren't using a default extension, use the one suppled by the
  595. # filter.
  596. if {![info exists data(extUsed)]} {
  597. if {[string length $data(-defaultextension)]} {
  598. set data(extUsed) 1
  599. } else {
  600. set data(extUsed) 0
  601. }
  602. }
  603. if {!$data(extUsed)} {
  604. # Get the first extension in the list that matches {^\*\.\w+$} and
  605. # remove all * from the filter.
  606. set index [lsearch -regexp $data(filter) {^\*\.\w+$}]
  607. if {$index >= 0} {
  608. set data(-defaultextension) \
  609. [string trimleft [lindex $data(filter) $index] "*"]
  610. } else {
  611. # Couldn't find anything! Reset to a safe default...
  612. set data(-defaultextension) ""
  613. }
  614. }
  615. $data(icons) see 0
  616. UpdateWhenIdle $w
  617. }
  618. # tk::dialog::file::ResolveFile --
  619. #
  620. # Interpret the user's text input in a file selection dialog. Performs:
  621. #
  622. # (1) ~ substitution
  623. # (2) resolve all instances of . and ..
  624. # (3) check for non-existent files/directories
  625. # (4) check for chdir permissions
  626. # (5) conversion of environment variable references to their
  627. # contents (once only)
  628. #
  629. # Arguments:
  630. # context: the current directory you are in
  631. # text: the text entered by the user
  632. # defaultext: the default extension to add to files with no extension
  633. # expandEnv: whether to expand environment variables (yes by default)
  634. #
  635. # Return vaue:
  636. # [list $flag $directory $file]
  637. #
  638. # flag = OK : valid input
  639. # = PATTERN : valid directory/pattern
  640. # = PATH : the directory does not exist
  641. # = FILE : the directory exists by the file doesn't exist
  642. # = CHDIR : Cannot change to the directory
  643. # = ERROR : Invalid entry
  644. #
  645. # directory : valid only if flag = OK or PATTERN or FILE
  646. # file : valid only if flag = OK or PATTERN
  647. #
  648. # directory may not be the same as context, because text may contain a
  649. # subdirectory name
  650. #
  651. proc ::tk::dialog::file::ResolveFile {context text defaultext {expandEnv 1}} {
  652. set appPWD [pwd]
  653. set path [JoinFile $context $text]
  654. # If the file has no extension, append the default. Be careful not to do
  655. # this for directories, otherwise typing a dirname in the box will give
  656. # back "dirname.extension" instead of trying to change dir.
  657. if {
  658. ![file isdirectory $path] && ([file ext $path] eq "") &&
  659. ![string match {$*} [file tail $path]]
  660. } then {
  661. set path "$path$defaultext"
  662. }
  663. if {[catch {file exists $path}]} {
  664. # This "if" block can be safely removed if the following code stop
  665. # generating errors.
  666. #
  667. # file exists ~nonsuchuser
  668. #
  669. return [list ERROR $path ""]
  670. }
  671. if {[file exists $path]} {
  672. if {[file isdirectory $path]} {
  673. if {[catch {cd $path}]} {
  674. return [list CHDIR $path ""]
  675. }
  676. set directory [pwd]
  677. set file ""
  678. set flag OK
  679. cd $appPWD
  680. } else {
  681. if {[catch {cd [file dirname $path]}]} {
  682. return [list CHDIR [file dirname $path] ""]
  683. }
  684. set directory [pwd]
  685. set file [file tail $path]
  686. set flag OK
  687. cd $appPWD
  688. }
  689. } else {
  690. set dirname [file dirname $path]
  691. if {[file exists $dirname]} {
  692. if {[catch {cd $dirname}]} {
  693. return [list CHDIR $dirname ""]
  694. }
  695. set directory [pwd]
  696. cd $appPWD
  697. set file [file tail $path]
  698. # It's nothing else, so check to see if it is an env-reference
  699. if {$expandEnv && [string match {$*} $file]} {
  700. set var [string range $file 1 end]
  701. if {[info exist ::env($var)]} {
  702. return [ResolveFile $context $::env($var) $defaultext 0]
  703. }
  704. }
  705. if {[regexp {[*?]} $file]} {
  706. set flag PATTERN
  707. } else {
  708. set flag FILE
  709. }
  710. } else {
  711. set directory $dirname
  712. set file [file tail $path]
  713. set flag PATH
  714. # It's nothing else, so check to see if it is an env-reference
  715. if {$expandEnv && [string match {$*} $file]} {
  716. set var [string range $file 1 end]
  717. if {[info exist ::env($var)]} {
  718. return [ResolveFile $context $::env($var) $defaultext 0]
  719. }
  720. }
  721. }
  722. }
  723. return [list $flag $directory $file]
  724. }
  725. # Gets called when the entry box gets keyboard focus. We clear the selection
  726. # from the icon list . This way the user can be certain that the input in the
  727. # entry box is the selection.
  728. #
  729. proc ::tk::dialog::file::EntFocusIn {w} {
  730. upvar ::tk::dialog::file::[winfo name $w] data
  731. if {[$data(ent) get] ne ""} {
  732. $data(ent) selection range 0 end
  733. $data(ent) icursor end
  734. } else {
  735. $data(ent) selection clear
  736. }
  737. if {[winfo class $w] eq "TkFDialog"} {
  738. # If this is a File Dialog, make sure the buttons are labeled right.
  739. if {$data(type) eq "open"} {
  740. ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  741. } else {
  742. ::tk::SetAmpText $data(okBtn) [mc "&Save"]
  743. }
  744. }
  745. }
  746. proc ::tk::dialog::file::EntFocusOut {w} {
  747. upvar ::tk::dialog::file::[winfo name $w] data
  748. $data(ent) selection clear
  749. }
  750. # Gets called when user presses Return in the "File name" entry.
  751. #
  752. proc ::tk::dialog::file::ActivateEnt {w} {
  753. upvar ::tk::dialog::file::[winfo name $w] data
  754. set text [$data(ent) get]
  755. if {$data(-multiple)} {
  756. foreach t $text {
  757. VerifyFileName $w $t
  758. }
  759. } else {
  760. VerifyFileName $w $text
  761. }
  762. }
  763. # Verification procedure
  764. #
  765. proc ::tk::dialog::file::VerifyFileName {w filename} {
  766. upvar ::tk::dialog::file::[winfo name $w] data
  767. set list [ResolveFile $data(selectPath) $filename $data(-defaultextension)]
  768. foreach {flag path file} $list {
  769. break
  770. }
  771. switch -- $flag {
  772. OK {
  773. if {$file eq ""} {
  774. # user has entered an existing (sub)directory
  775. set data(selectPath) $path
  776. $data(ent) delete 0 end
  777. } else {
  778. SetPathSilently $w $path
  779. if {$data(-multiple)} {
  780. lappend data(selectFile) $file
  781. } else {
  782. set data(selectFile) $file
  783. }
  784. Done $w
  785. }
  786. }
  787. PATTERN {
  788. set data(selectPath) $path
  789. set data(filter) $file
  790. }
  791. FILE {
  792. if {$data(type) eq "open"} {
  793. tk_messageBox -icon warning -type ok -parent $w \
  794. -message [mc "File \"%1\$s\" does not exist." \
  795. [file join $path $file]]
  796. $data(ent) selection range 0 end
  797. $data(ent) icursor end
  798. } else {
  799. SetPathSilently $w $path
  800. if {$data(-multiple)} {
  801. lappend data(selectFile) $file
  802. } else {
  803. set data(selectFile) $file
  804. }
  805. Done $w
  806. }
  807. }
  808. PATH {
  809. tk_messageBox -icon warning -type ok -parent $w \
  810. -message [mc "Directory \"%1\$s\" does not exist." $path]
  811. $data(ent) selection range 0 end
  812. $data(ent) icursor end
  813. }
  814. CHDIR {
  815. tk_messageBox -type ok -parent $w -icon warning -message \
  816. [mc "Cannot change to the directory\
  817. \"%1\$s\".\nPermission denied." $path]
  818. $data(ent) selection range 0 end
  819. $data(ent) icursor end
  820. }
  821. ERROR {
  822. tk_messageBox -type ok -parent $w -icon warning -message \
  823. [mc "Invalid file name \"%1\$s\"." $path]
  824. $data(ent) selection range 0 end
  825. $data(ent) icursor end
  826. }
  827. }
  828. }
  829. # Gets called when user presses the Alt-s or Alt-o keys.
  830. #
  831. proc ::tk::dialog::file::InvokeBtn {w key} {
  832. upvar ::tk::dialog::file::[winfo name $w] data
  833. if {[$data(okBtn) cget -text] eq $key} {
  834. $data(okBtn) invoke
  835. }
  836. }
  837. # Gets called when user presses the "parent directory" button
  838. #
  839. proc ::tk::dialog::file::UpDirCmd {w} {
  840. upvar ::tk::dialog::file::[winfo name $w] data
  841. if {$data(selectPath) ne "/"} {
  842. set data(selectPath) [file dirname $data(selectPath)]
  843. }
  844. }
  845. # Join a file name to a path name. The "file join" command will break if the
  846. # filename begins with ~
  847. #
  848. proc ::tk::dialog::file::JoinFile {path file} {
  849. if {[string match {~*} $file] && [file exists $path/$file]} {
  850. return [file join $path ./$file]
  851. } else {
  852. return [file join $path $file]
  853. }
  854. }
  855. # Gets called when user presses the "OK" button
  856. #
  857. proc ::tk::dialog::file::OkCmd {w} {
  858. upvar ::tk::dialog::file::[winfo name $w] data
  859. set filenames {}
  860. foreach item [$data(icons) selection get] {
  861. lappend filenames [$data(icons) get $item]
  862. }
  863. if {
  864. ([llength $filenames] && !$data(-multiple)) ||
  865. ($data(-multiple) && ([llength $filenames] == 1))
  866. } then {
  867. set filename [lindex $filenames 0]
  868. set file [JoinFile $data(selectPath) $filename]
  869. if {[file isdirectory $file]} {
  870. ListInvoke $w [list $filename]
  871. return
  872. }
  873. }
  874. ActivateEnt $w
  875. }
  876. # Gets called when user presses the "Cancel" button
  877. #
  878. proc ::tk::dialog::file::CancelCmd {w} {
  879. upvar ::tk::dialog::file::[winfo name $w] data
  880. variable ::tk::Priv
  881. bind $data(okBtn) <Destroy> {}
  882. set Priv(selectFilePath) ""
  883. }
  884. # Gets called when user destroys the dialog directly [Bug 987169]
  885. #
  886. proc ::tk::dialog::file::Destroyed {w} {
  887. upvar ::tk::dialog::file::[winfo name $w] data
  888. variable ::tk::Priv
  889. set Priv(selectFilePath) ""
  890. }
  891. # Gets called when user browses the IconList widget (dragging mouse, arrow
  892. # keys, etc)
  893. #
  894. proc ::tk::dialog::file::ListBrowse {w} {
  895. upvar ::tk::dialog::file::[winfo name $w] data
  896. set text {}
  897. foreach item [$data(icons) selection get] {
  898. lappend text [$data(icons) get $item]
  899. }
  900. if {[llength $text] == 0} {
  901. return
  902. }
  903. if {$data(-multiple)} {
  904. set newtext {}
  905. foreach file $text {
  906. set fullfile [JoinFile $data(selectPath) $file]
  907. if { ![file isdirectory $fullfile] } {
  908. lappend newtext $file
  909. }
  910. }
  911. set text $newtext
  912. set isDir 0
  913. } else {
  914. set text [lindex $text 0]
  915. set file [JoinFile $data(selectPath) $text]
  916. set isDir [file isdirectory $file]
  917. }
  918. if {!$isDir} {
  919. $data(ent) delete 0 end
  920. $data(ent) insert 0 $text
  921. if {[winfo class $w] eq "TkFDialog"} {
  922. if {$data(type) eq "open"} {
  923. ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  924. } else {
  925. ::tk::SetAmpText $data(okBtn) [mc "&Save"]
  926. }
  927. }
  928. } elseif {[winfo class $w] eq "TkFDialog"} {
  929. ::tk::SetAmpText $data(okBtn) [mc "&Open"]
  930. }
  931. }
  932. # Gets called when user invokes the IconList widget (double-click, Return key,
  933. # etc)
  934. #
  935. proc ::tk::dialog::file::ListInvoke {w filenames} {
  936. upvar ::tk::dialog::file::[winfo name $w] data
  937. if {[llength $filenames] == 0} {
  938. return
  939. }
  940. set file [JoinFile $data(selectPath) [lindex $filenames 0]]
  941. set class [winfo class $w]
  942. if {$class eq "TkChooseDir" || [file isdirectory $file]} {
  943. set appPWD [pwd]
  944. if {[catch {cd $file}]} {
  945. tk_messageBox -type ok -parent $w -icon warning -message \
  946. [mc "Cannot change to the directory \"%1\$s\".\nPermission denied." $file]
  947. } else {
  948. cd $appPWD
  949. set data(selectPath) $file
  950. }
  951. } else {
  952. if {$data(-multiple)} {
  953. set data(selectFile) $filenames
  954. } else {
  955. set data(selectFile) $file
  956. }
  957. Done $w
  958. }
  959. }
  960. # ::tk::dialog::file::Done --
  961. #
  962. # Gets called when user has input a valid filename. Pops up a dialog
  963. # box to confirm selection when necessary. Sets the
  964. # tk::Priv(selectFilePath) variable, which will break the "vwait" loop
  965. # in ::tk::dialog::file:: and return the selected filename to the script
  966. # that calls tk_getOpenFile or tk_getSaveFile
  967. #
  968. proc ::tk::dialog::file::Done {w {selectFilePath ""}} {
  969. upvar ::tk::dialog::file::[winfo name $w] data
  970. variable ::tk::Priv
  971. if {$selectFilePath eq ""} {
  972. if {$data(-multiple)} {
  973. set selectFilePath {}
  974. foreach f $data(selectFile) {
  975. lappend selectFilePath [JoinFile $data(selectPath) $f]
  976. }
  977. } else {
  978. set selectFilePath [JoinFile $data(selectPath) $data(selectFile)]
  979. }
  980. set Priv(selectFile) $data(selectFile)
  981. set Priv(selectPath) $data(selectPath)
  982. if {($data(type) eq "save") && $data(-confirmoverwrite) && [file exists $selectFilePath]} {
  983. set reply [tk_messageBox -icon warning -type yesno -parent $w \
  984. -message [mc "File \"%1\$s\" already exists.\nDo you want\
  985. to overwrite it?" $selectFilePath]]
  986. if {$reply eq "no"} {
  987. return
  988. }
  989. }
  990. if {
  991. [info exists data(-typevariable)] && $data(-typevariable) ne ""
  992. && [info exists data(-filetypes)] && [llength $data(-filetypes)]
  993. && [info exists data(filterType)] && $data(filterType) ne ""
  994. } then {
  995. upvar #0 $data(-typevariable) typeVariable
  996. set typeVariable [lindex $data(origfiletypes) \
  997. [lsearch -exact $data(-filetypes) $data(filterType)] 0]
  998. }
  999. }
  1000. bind $data(okBtn) <Destroy> {}
  1001. set Priv(selectFilePath) $selectFilePath
  1002. }
  1003. # ::tk::dialog::file::GlobFiltered --
  1004. #
  1005. # Gets called to do globbing, returning the results and filtering them
  1006. # according to the current filter (and removing the entries for '.' and
  1007. # '..' which are never shown). Deals with evil cases such as where the
  1008. # user is supplying a filter which is an invalid list or where it has an
  1009. # unbalanced brace. The resulting list will be dictionary sorted.
  1010. #
  1011. # Arguments:
  1012. # dir Which directory to search
  1013. # type List of filetypes to look for ('d' or 'f b c l p s')
  1014. # overrideFilter Whether to ignore the filter for this search.
  1015. #
  1016. # NB: Assumes that the caller has mapped the state variable to 'data'.
  1017. #
  1018. proc ::tk::dialog::file::GlobFiltered {dir type {overrideFilter 0}} {
  1019. variable showHiddenVar
  1020. upvar 1 data(filter) filter
  1021. if {$filter eq "*" || $overrideFilter} {
  1022. set patterns [list *]
  1023. if {$showHiddenVar} {
  1024. lappend patterns .*
  1025. }
  1026. } elseif {[string is list $filter]} {
  1027. set patterns $filter
  1028. } else {
  1029. # Invalid list; assume we can use non-whitespace sequences as words
  1030. set patterns [regexp -inline -all {\S+} $filter]
  1031. }
  1032. set opts [list -tails -directory $dir -type $type -nocomplain]
  1033. set result {}
  1034. catch {
  1035. # We have a catch because we might have a really bad pattern (e.g.,
  1036. # with an unbalanced brace); even [glob -nocomplain] doesn't like it.
  1037. # Using a catch ensures that it just means we match nothing instead of
  1038. # throwing a nasty error at the user...
  1039. foreach f [glob {*}$opts -- {*}$patterns] {
  1040. if {$f eq "." || $f eq ".."} {
  1041. continue
  1042. }
  1043. # See ticket [1641721], $f might be a link pointing to a dir
  1044. if {$type != "d" && [file isdir [file join $dir $f]]} {
  1045. continue
  1046. }
  1047. lappend result $f
  1048. }
  1049. }
  1050. return [lsort -dictionary -unique $result]
  1051. }
  1052. proc ::tk::dialog::file::CompleteEnt {w} {
  1053. upvar ::tk::dialog::file::[winfo name $w] data
  1054. set f [$data(ent) get]
  1055. if {$data(-multiple)} {
  1056. if {![string is list $f] || [llength $f] != 1} {
  1057. return -code break
  1058. }
  1059. set f [lindex $f 0]
  1060. }
  1061. # Get list of matching filenames and dirnames
  1062. set files [if {[winfo class $w] eq "TkFDialog"} {
  1063. GlobFiltered $data(selectPath) {f b c l p s}
  1064. }]
  1065. set dirs2 {}
  1066. foreach d [GlobFiltered $data(selectPath) d] {lappend dirs2 $d/}
  1067. set targets [concat \
  1068. [lsearch -glob -all -inline $files $f*] \
  1069. [lsearch -glob -all -inline $dirs2 $f*]]
  1070. if {[llength $targets] == 1} {
  1071. # We have a winner!
  1072. set f [lindex $targets 0]
  1073. } elseif {$f in $targets || [llength $targets] == 0} {
  1074. if {[string length $f] > 0} {
  1075. bell
  1076. }
  1077. return
  1078. } elseif {[llength $targets] > 1} {
  1079. # Multiple possibles
  1080. if {[string length $f] == 0} {
  1081. return
  1082. }
  1083. set t0 [lindex $targets 0]
  1084. for {set len [string length $t0]} {$len>0} {} {
  1085. set allmatch 1
  1086. foreach s $targets {
  1087. if {![string equal -length $len $s $t0]} {
  1088. set allmatch 0
  1089. break
  1090. }
  1091. }
  1092. incr len -1
  1093. if {$allmatch} break
  1094. }
  1095. set f [string range $t0 0 $len]
  1096. }
  1097. if {$data(-multiple)} {
  1098. set f [list $f]
  1099. }
  1100. $data(ent) delete 0 end
  1101. $data(ent) insert 0 $f
  1102. return -code break
  1103. }