gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
3.7KB

  1. # -*-mode: tcl; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: DynTree.tcl,v 1.3 2001/12/09 05:31:07 idiscovery Exp $
  4. #
  5. # Tix Demostration Program
  6. #
  7. # This sample program is structured in such a way so that it can be
  8. # executed from the Tix demo program "widget": it must have a
  9. # procedure called "RunSample". It should also have the "if" statment
  10. # at the end of this file so that it can be run as a standalone
  11. # program using tixwish.
  12. # This file demonstrates how to use the TixTree widget to display
  13. # dynamic hierachical data (the files in the Unix file system)
  14. #
  15. proc RunSample {w} {
  16. # We create the frame and the ScrolledHList widget
  17. # at the top of the dialog box
  18. #
  19. frame $w.top -relief raised -bd 1
  20. # Create a TixTree widget to display the hypothetical DOS disk drive
  21. #
  22. #
  23. tixTree $w.top.a -options {
  24. hlist.separator "/"
  25. hlist.width 35
  26. hlist.height 25
  27. }
  28. pack $w.top.a -expand yes -fill both -padx 10 -pady 10 -side left
  29. set tree $w.top.a
  30. set hlist [$tree subwidget hlist]
  31. $tree config -opencmd "DynTree:OpenDir $tree"
  32. # Add the root directory the TixTree widget
  33. DynTree:AddDir $tree /
  34. # The / directory is added in the "open" mode. The user can open it
  35. # and then browse its subdirectories ...
  36. # Use a ButtonBox to hold the buttons.
  37. #
  38. tixButtonBox $w.box -orientation horizontal
  39. $w.box add ok -text Ok -underline 0 -command "destroy $w" \
  40. -width 6
  41. $w.box add cancel -text Cancel -underline 0 -command "destroy $w" \
  42. -width 6
  43. pack $w.box -side bottom -fill x
  44. pack $w.top -side top -fill both -expand yes
  45. }
  46. proc DynTree:AddDir {tree dir} {
  47. set hlist [$tree subwidget hlist]
  48. if {$dir == "/"} {
  49. set text /
  50. } else {
  51. set text [file tail $dir]
  52. }
  53. $hlist add $dir -itemtype imagetext \
  54. -text $text -image [tix getimage folder]
  55. catch {
  56. # We need a catch here because the directory may not be readable by us
  57. #
  58. $tree setmode $dir none
  59. if {[glob -nocomplain $dir/*] != {}} {
  60. $tree setmode $dir open
  61. }
  62. }
  63. }
  64. # This command is called whenever the user presses the (+) indicator or
  65. # double clicks on a directory whose mode is "open". It loads the files
  66. # inside that directory into the Tree widget.
  67. #
  68. # Note we didn't specify the -closecmd option for the Tree widget, so it
  69. # performs the default action when the user presses the (-) indicator or
  70. # double clicks on a directory whose mode is "close": hide all of its child
  71. # entries
  72. #
  73. proc DynTree:OpenDir {tree dir} {
  74. set PWD [pwd]
  75. set hlist [$tree subwidget hlist]
  76. if {[$hlist info children $dir] != {}} {
  77. # We have already loaded this directory. Let's just
  78. # show all the child entries
  79. #
  80. # Note: since we load the directory only once, it will not be
  81. # refreshed if the you add or remove files from this
  82. # directory.
  83. #
  84. foreach kid [$hlist info children $dir] {
  85. $hlist show entry $kid
  86. }
  87. return
  88. }
  89. if [catch {cd $dir}] {
  90. # We can't read that directory, better not do anything
  91. cd $PWD
  92. return
  93. }
  94. set files [lsort [glob -nocomplain *]]
  95. foreach f $files {
  96. if [file isdirectory $f] {
  97. if {$dir == "/"} {
  98. set subdir /$f
  99. } else {
  100. set subdir $dir/$f
  101. }
  102. DynTree:AddDir $tree $subdir
  103. } else {
  104. if {$dir == "/"} {
  105. set file /$f
  106. } else {
  107. set file $dir/$f
  108. }
  109. $hlist add $file -itemtype imagetext \
  110. -text $f -image [tix getimage file]
  111. }
  112. }
  113. cd $PWD
  114. }
  115. # This "if" statement makes it possible to run this script file inside or
  116. # outside of the main demo program "widget".
  117. #
  118. if {![info exists tix_demo_running]} {
  119. wm withdraw .
  120. set w .demo
  121. toplevel $w; wm transient $w ""
  122. RunSample $w
  123. bind $w <Destroy> exit
  124. }
上海开阖软件有限公司 沪ICP备12045867号-1