DirList.tcl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: DirList.tcl,v 1.5 2004/03/28 02:44:57 hobbs Exp $
  4. #
  5. # DirList.tcl --
  6. #
  7. # Implements the tixDirList widget.
  8. #
  9. # - overrides the -browsecmd and -command options of the
  10. # HList subwidget
  11. #
  12. # Copyright (c) 1993-1999 Ioi Kim Lam.
  13. # Copyright (c) 2000-2001 Tix Project Group.
  14. # Copyright (c) 2004 ActiveState
  15. #
  16. # See the file "license.terms" for information on usage and redistribution
  17. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  18. #
  19. tixWidgetClass tixDirList {
  20. -classname TixDirList
  21. -superclass tixScrolledHList
  22. -method {
  23. chdir
  24. }
  25. -flag {
  26. -browsecmd -command -dircmd -disablecallback
  27. -root -rootname -showhidden -value
  28. }
  29. -configspec {
  30. {-browsecmd browseCmd BrowseCmd ""}
  31. {-command command Command ""}
  32. {-dircmd dirCmd DirCmd ""}
  33. {-disablecallback disableCallback DisableCallback 0 tixVerifyBoolean}
  34. {-root root Root ""}
  35. {-rootname rootName RootName ""}
  36. {-showhidden showHidden ShowHidden 0 tixVerifyBoolean}
  37. {-value value Value ""}
  38. }
  39. -default {
  40. {.scrollbar auto}
  41. {*borderWidth 1}
  42. {*hlist.background #c3c3c3}
  43. {*hlist.indent 7}
  44. {*hlist.relief sunken}
  45. {*hlist.height 10}
  46. {*hlist.width 20}
  47. {*hlist.padX 2}
  48. {*hlist.padY 0}
  49. {*hlist.wideSelection 0}
  50. {*hlist.drawBranch 0}
  51. {*hlist.highlightBackground #d9d9d9}
  52. {*hlist.itemType imagetext}
  53. {*hlist.takeFocus 1}
  54. }
  55. -forcecall {
  56. -value
  57. }
  58. }
  59. # Important data members:
  60. #
  61. # data(vpath)
  62. # The currently selected vpath. This internal variable is useful on
  63. # the Win95 platform, where an directory may correspond to more than
  64. # one node in the hierarchy. For example, C:\Windows\Desktop\Foo
  65. # can appead as "Desktop\Foo" and
  66. # "Desktop\My Computer\C:\Windows\Desktop\Foo". This variable tells us
  67. # which icon should we show given the same DOS pathname.
  68. #
  69. proc tixDirList:InitWidgetRec {w} {
  70. upvar #0 $w data
  71. tixChainMethod $w InitWidgetRec
  72. }
  73. proc tixDirList:ConstructWidget {w} {
  74. upvar #0 $w data
  75. tixChainMethod $w ConstructWidget
  76. $data(w:hlist) config -separator [tixFSSep] -selectmode "single"
  77. }
  78. proc tixDirList:SetBindings {w} {
  79. upvar #0 $w data
  80. tixChainMethod $w SetBindings
  81. $data(w:hlist) config \
  82. -browsecmd [list tixDirList:Browse $w] \
  83. -command [list tixDirList:Command $w]
  84. if {$data(-value) eq ""
  85. || [catch {set data(-value) [tixFSNormalize $data(-value)]}]} {
  86. set data(-value) [pwd]
  87. }
  88. set data(vpath) [tixFSInternal $data(-value)]
  89. }
  90. #----------------------------------------------------------------------
  91. # Incoming-Events
  92. #----------------------------------------------------------------------
  93. proc tixDirList:Browse {w args} {
  94. upvar #0 $w data
  95. set vpath [tixEvent flag V]
  96. set value [$data(w:hlist) info data $vpath]
  97. tixDirList:HighLight $w $vpath
  98. set data(vpath) $vpath
  99. set data(-value) [tixFSExternal $value]
  100. tixDirList:CallBrowseCmd $w $data(-value)
  101. }
  102. proc tixDirList:Command {w args} {
  103. upvar #0 $w data
  104. set vpath [tixEvent value]
  105. set value [$data(w:hlist) info data $vpath]
  106. set data(-value) [tixFSExternal $value]
  107. tixDirList:LoadDir $w [tixFSNativeNorm $value] $vpath
  108. tixDirList:HighLight $w $vpath
  109. set data(vpath) $vpath
  110. tixDirList:CallCommand $w $data(-value)
  111. }
  112. #----------------------------------------------------------------------
  113. # Outgoing-Events
  114. #----------------------------------------------------------------------
  115. proc tixDirList:CallBrowseCmd {w npath} {
  116. upvar #0 $w data
  117. if {[llength $data(-browsecmd)]} {
  118. set bind(specs) "%V"
  119. set bind(%V) $npath
  120. tixEvalCmdBinding $w $data(-browsecmd) bind $npath
  121. }
  122. }
  123. proc tixDirList:CallCommand {w npath} {
  124. upvar #0 $w data
  125. if {[llength $data(-command)] && !$data(-disablecallback)} {
  126. set bind(specs) "%V"
  127. set bind(%V) $npath
  128. tixEvalCmdBinding $w $data(-command) bind $npath
  129. }
  130. }
  131. #----------------------------------------------------------------------
  132. # Directory loading
  133. #----------------------------------------------------------------------
  134. proc tixDirList:LoadDir {w {npath ""} {vpath ""}} {
  135. upvar #0 $w data
  136. tixBusy $w on $data(w:hlist)
  137. $data(w:hlist) delete all
  138. if {$npath eq ""} {
  139. set npath [tixFSNativeNorm $data(-value)]
  140. set vpath [tixFSInternal $npath]
  141. }
  142. tixDirList:ListHierachy $w $npath $vpath
  143. tixDirList:ListSubDirs $w $npath $vpath
  144. tixWidgetDoWhenIdle tixBusy $w off $data(w:hlist)
  145. }
  146. proc tixDirList:ListHierachy {w npath vpath} {
  147. upvar #0 $w data
  148. set img [tix getimage openfold]
  149. set curpath ""
  150. foreach part [tixFSAncestors $npath] {
  151. set curpath [file join $curpath $part]
  152. set text [tixFSDisplayFileName $curpath]
  153. set vpath [tixFSInternal $curpath]
  154. $data(w:hlist) add $vpath -text $text -data $curpath -image $img
  155. }
  156. }
  157. proc tixDirList:ListSubDirs {w npath vpath} {
  158. upvar #0 $w data
  159. $data(w:hlist) entryconfig $vpath -image [tix getimage act_fold]
  160. set img [tix getimage folder]
  161. foreach ent [tixFSListDir $npath 1 0 0 $data(-showhidden)] {
  162. set curpath [file join $npath $ent]
  163. set vp [tixFSInternal $curpath]
  164. if {![$data(w:hlist) info exists $vp]} {
  165. $data(w:hlist) add $vp -text $ent -data $curpath -image $img
  166. }
  167. }
  168. }
  169. proc tixDirList:SetValue {w npath vpath {flag ""}} {
  170. upvar #0 $w data
  171. if {$flag eq "reload" || ![$data(w:hlist) info exists $vpath]} {
  172. tixDirList:LoadDir $w $npath $vpath
  173. }
  174. tixDirList:HighLight $w $vpath
  175. set data(-value) [tixFSNativeNorm $npath]
  176. set data(vpath) $vpath
  177. tixDirList:CallCommand $w $data(-value)
  178. }
  179. proc tixDirList:HighLight {w vpath} {
  180. upvar #0 $w data
  181. if {$data(vpath) ne $vpath} {
  182. set old $data(vpath)
  183. if {[$data(w:hlist) info exists $old]} {
  184. # Un-highlight the originally selected entry by changing its
  185. # folder image
  186. if {[llength [$data(w:hlist) info children $old]]} {
  187. set img [tix getimage openfold]
  188. } else {
  189. set img [tix getimage folder]
  190. }
  191. $data(w:hlist) entryconfig $old -image $img
  192. }
  193. }
  194. # Highlight the newly selected entry
  195. #
  196. $data(w:hlist) entryconfig $vpath -image [tix getimage act_fold]
  197. $data(w:hlist) anchor set $vpath
  198. $data(w:hlist) select clear
  199. $data(w:hlist) select set $vpath
  200. $data(w:hlist) see $vpath
  201. }
  202. #----------------------------------------------------------------------
  203. # Config options
  204. #----------------------------------------------------------------------
  205. proc tixDirList:config-value {w value} {
  206. upvar #0 $w data
  207. tixDirList:chdir $w $value
  208. return $data(-value)
  209. }
  210. proc tixDirList:config-showhidden {w value} {
  211. upvar #0 $w data
  212. tixWidgetDoWhenIdle tixDirList:LoadDir $w
  213. }
  214. #----------------------------------------------------------------------
  215. # Public methods
  216. #----------------------------------------------------------------------
  217. proc tixDirList:chdir {w value} {
  218. upvar #0 $w data
  219. set npath [tixFSNativeNorm $value]
  220. tixDirList:SetValue $w $npath [tixFSInternal $npath]
  221. }