DirTree.tcl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: DirTree.tcl,v 1.4 2004/03/28 02:44:57 hobbs Exp $
  4. #
  5. # DirTree.tcl --
  6. #
  7. # Implements directory tree for Unix file systems
  8. #
  9. # What the indicators mean:
  10. #
  11. # (+): There are some subdirectories in this directory which are not
  12. # currently visible.
  13. # (-): This directory has some subdirectories and they are all visible
  14. #
  15. # none: The dir has no subdirectori(es).
  16. #
  17. # Copyright (c) 1993-1999 Ioi Kim Lam.
  18. # Copyright (c) 2000-2001 Tix Project Group.
  19. # Copyright (c) 2004 ActiveState
  20. #
  21. # See the file "license.terms" for information on usage and redistribution
  22. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  23. #
  24. ##
  25. ## The tixDirTree require special FS handling due to it's limited
  26. ## separator idea (instead of real tree).
  27. ##
  28. tixWidgetClass tixDirTree {
  29. -classname TixDirTree
  30. -superclass tixVTree
  31. -method {
  32. activate chdir refresh
  33. }
  34. -flag {
  35. -browsecmd -command -directory -disablecallback -showhidden -value
  36. }
  37. -configspec {
  38. {-browsecmd browseCmd BrowseCmd ""}
  39. {-command command Command ""}
  40. {-disablecallback disableCallback DisableCallback 0 tixVerifyBoolean}
  41. {-showhidden showHidden ShowHidden 0 tixVerifyBoolean}
  42. {-value value Value ""}
  43. }
  44. -alias {
  45. {-directory -value}
  46. }
  47. -default {
  48. {.scrollbar auto}
  49. {*Scrollbar.takeFocus 0}
  50. {*borderWidth 1}
  51. {*hlist.indicator 1}
  52. {*hlist.background #c3c3c3}
  53. {*hlist.drawBranch 1}
  54. {*hlist.height 10}
  55. {*hlist.highlightBackground #d9d9d9}
  56. {*hlist.indent 20}
  57. {*hlist.itemType imagetext}
  58. {*hlist.padX 3}
  59. {*hlist.padY 0}
  60. {*hlist.relief sunken}
  61. {*hlist.takeFocus 1}
  62. {*hlist.wideSelection 0}
  63. {*hlist.width 20}
  64. }
  65. }
  66. proc tixDirTree:InitWidgetRec {w} {
  67. upvar #0 $w data
  68. tixChainMethod $w InitWidgetRec
  69. if {$data(-value) == ""} {
  70. set data(-value) [pwd]
  71. }
  72. tixDirTree:SetDir $w [file normalize $data(-value)]
  73. }
  74. proc tixDirTree:ConstructWidget {w} {
  75. upvar #0 $w data
  76. tixChainMethod $w ConstructWidget
  77. tixDoWhenMapped $w [list tixDirTree:StartUp $w]
  78. $data(w:hlist) config -separator [tixFSSep] \
  79. -selectmode "single" -drawbranch 1
  80. # We must creat an extra copy of these images to avoid flashes on
  81. # the screen when user changes directory
  82. #
  83. set data(images) [image create compound -window $data(w:hlist)]
  84. $data(images) add image -image [tix getimage act_fold]
  85. $data(images) add image -image [tix getimage folder]
  86. $data(images) add image -image [tix getimage openfold]
  87. }
  88. proc tixDirTree:SetBindings {w} {
  89. upvar #0 $w data
  90. tixChainMethod $w SetBindings
  91. }
  92. # Add one dir into the node (parent directory), sorted alphabetically
  93. #
  94. proc tixDirTree:AddToList {w fsdir image} {
  95. upvar #0 $w data
  96. set dir [tixFSInternal $fsdir]
  97. if {[$data(w:hlist) info exists $dir]} { return }
  98. set parent [file dirname $fsdir]
  99. if {$fsdir eq $parent} {
  100. # root node
  101. set node ""
  102. } else {
  103. # regular node
  104. set node [tixFSInternal $parent]
  105. }
  106. set added 0
  107. set text [tixFSDisplayFileName $fsdir]
  108. foreach sib [$data(w:hlist) info children $node] {
  109. if {[string compare $dir $sib] < 0} {
  110. $data(w:hlist) add $dir -before $sib -text $text -image $image
  111. set added 1
  112. break
  113. }
  114. }
  115. if {!$added} {
  116. $data(w:hlist) add $dir -text $text -image $image
  117. }
  118. # Check to see if we have children (%% optimize!)
  119. if {[llength [tixFSListDir $fsdir 1 0 0 $data(-showhidden)]]} {
  120. tixVTree:SetMode $w $dir open
  121. }
  122. }
  123. proc tixDirTree:LoadDir {w fsdir {mode toggle}} {
  124. if {![winfo exists $w]} { return }
  125. upvar #0 $w data
  126. # Add the directory and set it to the active directory
  127. #
  128. set fsdir [tixFSNormalize $fsdir]
  129. set dir [tixFSInternal $fsdir]
  130. if {![$data(w:hlist) info exists $dir]} {
  131. # Add $dir and all ancestors of $dir into the HList widget
  132. set fspath ""
  133. set imgopenfold [tix getimage openfold]
  134. foreach part [tixFSAncestors $fsdir] {
  135. set fspath [file join $fspath $part]
  136. tixDirTree:AddToList $w $fspath $imgopenfold
  137. }
  138. }
  139. $data(w:hlist) entryconfig $dir -image [tix getimage act_fold]
  140. if {$mode eq "toggle"} {
  141. if {[llength [$data(w:hlist) info children $dir]]} {
  142. set mode flatten
  143. } else {
  144. set mode expand
  145. }
  146. }
  147. if {$mode eq "expand"} {
  148. # Add all the sub directories of fsdir into the HList widget
  149. tixBusy $w on $data(w:hlist)
  150. set imgfolder [tix getimage folder]
  151. foreach part [tixFSListDir $fsdir 1 0 0 $data(-showhidden)] {
  152. tixDirTree:AddToList $w [file join $fsdir $part] $imgfolder
  153. }
  154. tixWidgetDoWhenIdle tixBusy $w off $data(w:hlist)
  155. # correct indicator to represent children status (added above)
  156. if {[llength [$data(w:hlist) info children $dir]]} {
  157. tixVTree:SetMode $w $dir close
  158. } else {
  159. tixVTree:SetMode $w $dir none
  160. }
  161. } else {
  162. $data(w:hlist) delete offsprings $dir
  163. tixVTree:SetMode $w $dir open
  164. }
  165. }
  166. proc tixDirTree:ToggleDir {w value mode} {
  167. upvar #0 $w data
  168. tixDirTree:LoadDir $w $value $mode
  169. tixDirTree:CallCommand $w
  170. }
  171. proc tixDirTree:CallCommand {w} {
  172. upvar #0 $w data
  173. if {[llength $data(-command)] && !$data(-disablecallback)} {
  174. set bind(specs) {%V}
  175. set bind(%V) $data(-value)
  176. tixEvalCmdBinding $w $data(-command) bind $data(-value)
  177. }
  178. }
  179. proc tixDirTree:CallBrowseCmd {w ent} {
  180. upvar #0 $w data
  181. if {[llength $data(-browsecmd)] && !$data(-disablecallback)} {
  182. set bind(specs) {%V}
  183. set bind(%V) $data(-value)
  184. tixEvalCmdBinding $w $data(-browsecmd) bind [list $data(-value)]
  185. }
  186. }
  187. proc tixDirTree:StartUp {w} {
  188. if {![winfo exists $w]} { return }
  189. upvar #0 $w data
  190. # make sure that all the basic volumes are listed
  191. set imgopenfold [tix getimage openfold]
  192. foreach fspath [tixFSVolumes] {
  193. tixDirTree:AddToList $w $fspath $imgopenfold
  194. }
  195. tixDirTree:LoadDir $w [tixFSExternal $data(i-directory)]
  196. }
  197. proc tixDirTree:ChangeDir {w fsdir {forced 0}} {
  198. upvar #0 $w data
  199. set dir [tixFSInternal $fsdir]
  200. if {!$forced && $data(i-directory) eq $dir} {
  201. return
  202. }
  203. if {!$forced && [$data(w:hlist) info exists $dir]} {
  204. # Set the old directory to "non active"
  205. #
  206. if {[$data(w:hlist) info exists $data(i-directory)]} {
  207. $data(w:hlist) entryconfig $data(i-directory) \
  208. -image [tix getimage folder]
  209. }
  210. $data(w:hlist) entryconfig $dir -image [tix getimage act_fold]
  211. } else {
  212. if {$forced} {
  213. if {[llength [$data(w:hlist) info children $dir]]} {
  214. set mode expand
  215. } else {
  216. set mode flatten
  217. }
  218. } else {
  219. set mode toggle
  220. }
  221. tixDirTree:LoadDir $w $fsdir $mode
  222. tixDirTree:CallCommand $w
  223. }
  224. tixDirTree:SetDir $w $fsdir
  225. }
  226. proc tixDirTree:SetDir {w path} {
  227. upvar #0 $w data
  228. set data(i-directory) [tixFSInternal $path]
  229. set data(-value) [tixFSNativeNorm $path]
  230. }
  231. #----------------------------------------------------------------------
  232. #
  233. # Virtual Methods
  234. #
  235. #----------------------------------------------------------------------
  236. proc tixDirTree:OpenCmd {w ent} {
  237. set fsdir [tixFSExternal $ent]
  238. tixDirTree:ToggleDir $w $fsdir expand
  239. tixDirTree:ChangeDir $w $fsdir
  240. tixDirTree:CallBrowseCmd $w $fsdir
  241. }
  242. proc tixDirTree:CloseCmd {w ent} {
  243. set fsdir [tixFSExternal $ent]
  244. tixDirTree:ToggleDir $w $fsdir flatten
  245. tixDirTree:ChangeDir $w $fsdir
  246. tixDirTree:CallBrowseCmd $w $fsdir
  247. }
  248. proc tixDirTree:Command {w B} {
  249. upvar #0 $w data
  250. upvar $B bind
  251. set ent [tixEvent flag V]
  252. tixChainMethod $w Command $B
  253. if {[llength $data(-command)]} {
  254. set fsdir [tixFSExternal $ent]
  255. tixEvalCmdBinding $w $data(-command) bind $fsdir
  256. }
  257. }
  258. # This is a virtual method
  259. #
  260. proc tixDirTree:BrowseCmd {w B} {
  261. upvar #0 $w data
  262. upvar 1 $B bind
  263. set ent [tixEvent flag V]
  264. set fsdir [tixFSExternal $ent]
  265. # This is a hack because %V may have been modified by callbrowsecmd
  266. set fsdir [file normalize $fsdir]
  267. tixDirTree:ChangeDir $w $fsdir
  268. tixDirTree:CallBrowseCmd $w $fsdir
  269. }
  270. #----------------------------------------------------------------------
  271. #
  272. # Public Methods
  273. #
  274. #----------------------------------------------------------------------
  275. proc tixDirTree:chdir {w value} {
  276. tixDirTree:ChangeDir $w [file normalize $value]
  277. }
  278. proc tixDirTree:refresh {w {dir ""}} {
  279. upvar #0 $w data
  280. if {$dir eq ""} {
  281. set dir $data(-value)
  282. }
  283. set dir [file normalize $dir]
  284. tixDirTree:ChangeDir $w $dir 1
  285. # Delete any stale directories that no longer exist
  286. #
  287. foreach child [$data(w:hlist) info children [tixFSInternal $dir]] {
  288. if {![file exists [tixFSExternal $child]]} {
  289. $data(w:hlist) delete entry $child
  290. }
  291. }
  292. }
  293. proc tixDirTree:config-directory {w value} {
  294. tixDirTree:ChangeDir $w [file normalize $value]
  295. }