STList3.tcl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*-mode: tcl; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: STList3.tcl,v 1.4 2004/03/28 02:44:56 hobbs 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. # Demonstrates the use of DirTree with the TList
  13. #
  14. proc RunSample {w} {
  15. set top [frame $w.f -bd 1 -relief raised]
  16. set box [tixButtonBox $w.b -bd 1 -relief raised]
  17. pack $box -side bottom -fill both
  18. pack $top -side top -fill both -expand yes
  19. # Create the Paned Window to contain the dirtree and scrolled tlist
  20. #
  21. set p [tixPanedWindow $top.p -orient horizontal]
  22. pack $p -expand yes -fill both -padx 4 -pady 4
  23. set p1 [$p add pane1 -expand 1]
  24. set p2 [$p add pane2 -expand 4]
  25. $p1 config -relief flat
  26. $p2 config -relief flat
  27. # Create a DirTree
  28. #
  29. tixDirTree $p1.dirtree -options {
  30. hlist.width 28
  31. }
  32. pack $p1.dirtree -expand yes -fill both -padx 4 -pady 4
  33. # Create a TList
  34. # NOTE: we set the width of the tlist to 60 characters, since we'll have
  35. # quite a few files to display
  36. #
  37. tixScrolledTList $p2.st -options {
  38. tlist.orient vertical
  39. tlist.selectMode single
  40. tlist.width 60
  41. tlist.height 25
  42. }
  43. pack $p2.st -expand yes -fill both -padx 4 -pady 4
  44. set tlist [$p2.st subwidget tlist]
  45. # setup the callbacks: when the user selects a directory, we'll display
  46. # its content in the tlist widget
  47. $p1.dirtree config \
  48. -browsecmd [list TList:listdir $tlist] \
  49. -command [list TList:listdir $tlist]
  50. # List the directory now
  51. #
  52. TList:listdir $tlist [pwd]
  53. # Create the buttons
  54. #
  55. $box add ok -text Ok -command [list destroy $w] -width 6
  56. $box add cancel -text Cancel -command [list destroy $w] -width 6
  57. }
  58. proc TList:listdir {w dir} {
  59. $w delete 0 end
  60. if {[catch {glob -nocomplain -directory $dir *} entries]} {
  61. # The user has entered an invalid directory
  62. # %% todo: prompt error, go back to last succeed directory
  63. return
  64. }
  65. set files ""
  66. foreach fname [lsort -dictionary $entries] {
  67. if {[file isdirectory $fname]} {
  68. set image [tix getimage folder]
  69. } else {
  70. lappend files [file tail $fname]
  71. continue
  72. }
  73. $w insert end -itemtype imagetext \
  74. -text [file tail $fname] -image $image
  75. }
  76. foreach fname $files {
  77. switch -glob -- $fname {
  78. {*.[ch]} { set image [tix getimage srcfile] }
  79. *.tcl -
  80. *.o { set image [tix getimage file] }
  81. default { set image [tix getimage textfile] }
  82. }
  83. $w insert end -itemtype imagetext -text $fname -image $image
  84. }
  85. }
  86. if {![info exists tix_demo_running]} {
  87. wm withdraw .
  88. set w .demo
  89. toplevel $w; wm transient $w ""
  90. RunSample $w
  91. bind $w <Destroy> exit
  92. }