MkSample.tcl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # -*-mode: tcl; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: MkSample.tcl,v 1.3 2001/12/09 05:34:59 idiscovery Exp $
  4. #
  5. # MkSample.tcl --
  6. #
  7. # This file implements the "Sample" page in the widget demo
  8. #
  9. # This file has not been properly documented. It is NOT intended
  10. # to be used as an introductory demo program about Tix
  11. # programming. For such demos, please see the files in the
  12. # demos/samples directory or go to the "Samples" page in the
  13. # "widget demo"
  14. #
  15. #
  16. # Copyright (c) 1996, Expert Interface Technologies
  17. #
  18. # See the file "license.terms" for information on usage and redistribution
  19. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  20. #
  21. #
  22. #
  23. set tix_demo_running 1
  24. set samples_dir [file join $demo_dir samples]
  25. set sample_filename {}
  26. uplevel #0 source [list [file join $samples_dir AllSampl.tcl]]
  27. proc MkSample {nb page} {
  28. global tixOption
  29. #----------------------------------------------------------------------
  30. set w [$nb subwidget $page]
  31. set pane [tixPanedWindow $w.pane -orient horizontal]
  32. pack $pane -expand yes -fill both
  33. set f1 [$pane add 1 -expand 1]
  34. set f2 [$pane add 2 -expand 3]
  35. $f1 config -relief flat
  36. $f2 config -relief flat
  37. # Left pane: the Tree:
  38. #
  39. set lab [label $f1.lab -text "Select a sample program:" -anchor w]
  40. set tree [tixTree $f1.slb \
  41. -options {
  42. hlist.selectMode single
  43. hlist.width 40
  44. }]
  45. $tree config \
  46. -command "Sample:Action $w $tree run" \
  47. -browsecmd "Sample:Action $w $tree browse"
  48. pack $lab -side top -fill x -padx 5 -pady 5
  49. pack $tree -side top -fill both -expand yes -padx 5
  50. # Right pane: the Text
  51. #
  52. set labe [tixLabelEntry $f2.lab -label "Source:" -options {
  53. label.anchor w
  54. }]
  55. $labe subwidget entry config -state disabled
  56. set stext [tixScrolledText $f2.stext]
  57. set f3 [frame $f2.f3]
  58. set run [button $f3.run -text "Run ..." \
  59. -command "Sample:Action $w $tree run"]
  60. set view [button $f3.view -text "View Source in Browser ..." \
  61. -command "Sample:Action $w $tree view"]
  62. pack $run $view -side left -fill y -pady 2
  63. pack $labe -side top -fill x -padx 7 -pady 2
  64. pack $f3 -side bottom -fill x -padx 7
  65. pack $stext -side top -fill both -expand yes -padx 7
  66. #
  67. # Set up the text subwidget
  68. set text [$stext subwidget text]
  69. bind $text <Up> "%W yview scroll -1 unit"
  70. bind $text <Down> "%W yview scroll 1 unit"
  71. bind $text <Left> "%W xview scroll -1 unit"
  72. bind $text <Right> "%W xview scroll 1 unit"
  73. bind $text <Tab> {focus [tk_focusNext %W]; break}
  74. bindtags $text "$text Text [winfo toplevel $text] all"
  75. $text config -bg [$tree subwidget hlist cget -bg] \
  76. -state disabled -font $tixOption(fixed_font) -wrap none
  77. $run config -state disabled
  78. $view config -state disabled
  79. global demo
  80. set demo(w:run) $run
  81. set demo(w:view) $view
  82. set demo(w:tree) $tree
  83. set demo(w:lab1) $labe
  84. set demo(w:stext) $stext
  85. set hlist [$tree subwidget hlist]
  86. $hlist config -separator "." -width 30 -drawbranch 0 \
  87. -wideselect false
  88. set style [tixDisplayStyle imagetext -refwindow $hlist \
  89. -fg $tixOption(fg) -padx 4]
  90. set file [tix getimage textfile]
  91. set folder [tix getimage openfold]
  92. ForAllSamples root "" \
  93. [list AddSampleToHList $tree $hlist $style $file $folder]
  94. }
  95. # AddSampleToHList --
  96. #
  97. # A callback from ForAllSamples. Add all the possible sample files
  98. # into the Tree widget.
  99. #
  100. proc AddSampleToHList {tree hlist style file folder token type text dest} {
  101. case $type {
  102. d {
  103. return [$hlist addchild $token -itemtype imagetext -style $style \
  104. -image $folder -text $text]
  105. }
  106. done {
  107. if {![tixStrEq $token ""]} {
  108. $tree setmode $token close
  109. $tree close $token
  110. }
  111. }
  112. f {
  113. return [$hlist addchild $token -itemtype imagetext \
  114. -image $file -text $text -data [list $text $dest]]
  115. }
  116. }
  117. }
  118. proc Sample:Action {w slb action args} {
  119. global samples demo_dir demo samples_dir
  120. set hlist [$slb subwidget hlist]
  121. set ent [$hlist info anchor]
  122. if {$ent == ""} {
  123. $demo(w:run) config -state disabled
  124. $demo(w:view) config -state disabled
  125. return
  126. }
  127. if {[$hlist info data $ent] == {}} {
  128. # This is just a comment
  129. $demo(w:run) config -state disabled
  130. $demo(w:view) config -state disabled
  131. return
  132. } else {
  133. $demo(w:run) config -state normal
  134. $demo(w:view) config -state normal
  135. }
  136. set theSample [$hlist info data $ent]
  137. set title [lindex $theSample 0]
  138. set prog [lindex $theSample 1]
  139. case $action {
  140. "run" {
  141. RunProg $title $prog
  142. }
  143. "view" {
  144. LoadFile [file join $samples_dir $prog]
  145. }
  146. "browse" {
  147. # Bring up a short description of the sample program
  148. # in the scrolled text about
  149. set text [$demo(w:stext) subwidget text]
  150. uplevel #0 set sample_filename [list [file join $samples_dir $prog]]
  151. tixWidgetDoWhenIdle ReadFileWhenIdle $text
  152. $demo(w:lab1) subwidget entry config -state normal
  153. $demo(w:lab1) subwidget entry delete 0 end
  154. $demo(w:lab1) subwidget entry insert end [file join $samples_dir $prog]
  155. $demo(w:lab1) subwidget entry xview end
  156. $demo(w:lab1) subwidget entry config -state disabled
  157. }
  158. }
  159. }
  160. proc RunProg {title prog} {
  161. global samples demo_dir demo samples_dir
  162. set w .[lindex [split $prog .] 0]
  163. set w [string tolower $w]
  164. if [winfo exists $w] {
  165. wm deiconify $w
  166. raise $w
  167. return
  168. }
  169. uplevel #0 source [list [file join $samples_dir $prog]]
  170. toplevel $w
  171. wm title $w $title
  172. RunSample $w
  173. }
  174. proc LoadFile {filename} {
  175. global tixOption
  176. set tmp $filename
  177. regsub -all . $filename _ tmp
  178. set w [string tolower .$tmp]
  179. if [winfo exists $w] {
  180. wm deiconify $w
  181. raise $w
  182. return
  183. }
  184. toplevel $w
  185. wm title $w "Source View: $filename"
  186. button $w.b -text Close -command "destroy $w"
  187. set t [tixScrolledText $w.text]
  188. tixForm $w.b -left 0 -bottom -0 -padx 4 -pady 4
  189. tixForm $w.text -left 0 -right -0 -top 0 -bottom $w.b
  190. $t subwidget text config -highlightcolor [$t cget -bg] -bd 2 \
  191. -bg [$t cget -bg] -font $tixOption(fixed_font)
  192. if {$filename == {}} {
  193. return
  194. }
  195. set text [$w.text subwidget text]
  196. $text config -wrap none
  197. ReadFile $text $filename
  198. }
  199. proc ReadFileWhenIdle {text} {
  200. global sample_filename
  201. if ![file isdir $sample_filename] {
  202. ReadFile $text $sample_filename
  203. }
  204. }
  205. proc ReadFile {text filename} {
  206. set oldState [$text cget -state]
  207. $text config -state normal
  208. $text delete 0.0 end
  209. set fd [open $filename {RDONLY}]
  210. $text delete 1.0 end
  211. while {![eof $fd]} {
  212. $text insert end [gets $fd]\n
  213. }
  214. close $fd
  215. $text see 1.0
  216. $text config -state $oldState
  217. }