tkfbox.tcl 37 KB

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