twind.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # twind.tcl --
  2. #
  3. # This demonstration script creates a text widget with a bunch of
  4. # embedded windows.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .twind
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Text Demonstration - Embedded Windows and Other Features"
  13. wm iconname $w "Embedded Windows"
  14. positionWindow $w
  15. ## See Code / Dismiss buttons
  16. set btns [addSeeDismiss $w.buttons $w]
  17. pack $btns -side bottom -fill x
  18. frame $w.f -highlightthickness 1 -borderwidth 1 -relief sunken
  19. set t $w.f.text
  20. text $t -yscrollcommand "$w.scroll set" -setgrid true -font $font -width 70 \
  21. -height 35 -wrap word -highlightthickness 0 -borderwidth 0
  22. pack $t -expand yes -fill both
  23. scrollbar $w.scroll -command "$t yview"
  24. pack $w.scroll -side right -fill y
  25. panedwindow $w.pane
  26. pack $w.pane -expand yes -fill both
  27. $w.pane add $w.f
  28. # Import to raise given creation order above
  29. raise $w.f
  30. $t tag configure center -justify center -spacing1 5m -spacing3 5m
  31. $t tag configure buttons -lmargin1 1c -lmargin2 1c -rmargin 1c \
  32. -spacing1 3m -spacing2 0 -spacing3 0
  33. button $t.on -text "Turn On" -command "textWindOn $w" \
  34. -cursor top_left_arrow
  35. button $t.off -text "Turn Off" -command "textWindOff $w" \
  36. -cursor top_left_arrow
  37. $t insert end "A text widget can contain many different kinds of items, "
  38. $t insert end "both active and passive. It can lay these out in various "
  39. $t insert end "ways, with wrapping, tabs, centering, etc. In addition, "
  40. $t insert end "when the contents are too big for the window, smooth "
  41. $t insert end "scrolling in all directions is provided.\n\n"
  42. $t insert end "A text widget can contain other widgets embedded "
  43. $t insert end "it. These are called \"embedded windows\", "
  44. $t insert end "and they can consist of arbitrary widgets. "
  45. $t insert end "For example, here are two embedded button "
  46. $t insert end "widgets. You can click on the first button to "
  47. $t window create end -window $t.on
  48. $t insert end " horizontal scrolling, which also turns off "
  49. $t insert end "word wrapping. Or, you can click on the second "
  50. $t insert end "button to\n"
  51. $t window create end -window $t.off
  52. $t insert end " horizontal scrolling and turn back on word wrapping.\n\n"
  53. $t insert end "Or, here is another example. If you "
  54. $t window create end -create {
  55. button %W.click -text "Click Here" -command "textWindPlot %W" \
  56. -cursor top_left_arrow}
  57. $t insert end " a canvas displaying an x-y plot will appear right here."
  58. $t mark set plot insert
  59. $t mark gravity plot left
  60. $t insert end " You can drag the data points around with the mouse, "
  61. $t insert end "or you can click here to "
  62. $t window create end -create {
  63. button %W.delete -text "Delete" -command "textWindDel %W" \
  64. -cursor top_left_arrow
  65. }
  66. $t insert end " the plot again.\n\n"
  67. $t insert end "You can also create multiple text widgets each of which "
  68. $t insert end "display the same underlying text. Click this button to "
  69. $t window create end \
  70. -create {button %W.peer -text "Make A Peer" -command "textMakePeer %W" \
  71. -cursor top_left_arrow} -padx 3
  72. $t insert end " widget. Notice how peer widgets can have different "
  73. $t insert end "font settings, and by default contain all the images "
  74. $t insert end "of the 'parent', but many of the embedded windows, "
  75. $t insert end "such as buttons will not be there. The easiest way "
  76. $t insert end "to ensure they are in all peers is to use '-create' "
  77. $t insert end "embedded window creation scripts "
  78. $t insert end "(the plot above and the 'Make A Peer' button are "
  79. $t insert end "designed to show up in all peers). A good use of "
  80. $t insert end "peers is for "
  81. $t window create end \
  82. -create {button %W.split -text "Split Windows" -command "textSplitWindow %W" \
  83. -cursor top_left_arrow} -padx 3
  84. $t insert end " \n\n"
  85. $t insert end "Users of previous versions of Tk will also be interested "
  86. $t insert end "to note that now cursor movement is now by visual line by "
  87. $t insert end "default, and that all scrolling of this widget is by pixel.\n\n"
  88. $t insert end "You may also find it useful to put embedded windows in "
  89. $t insert end "a text without any actual text. In this case the "
  90. $t insert end "text widget acts like a geometry manager. For "
  91. $t insert end "example, here is a collection of buttons laid out "
  92. $t insert end "neatly into rows by the text widget. These buttons "
  93. $t insert end "can be used to change the background color of the "
  94. $t insert end "text widget (\"Default\" restores the color to "
  95. $t insert end "its default). If you click on the button labeled "
  96. $t insert end "\"Short\", it changes to a longer string so that "
  97. $t insert end "you can see how the text widget automatically "
  98. $t insert end "changes the layout. Click on the button again "
  99. $t insert end "to restore the short string.\n"
  100. button $t.default -text Default -command "embDefBg $t" \
  101. -cursor top_left_arrow
  102. $t window create end -window $t.default -padx 3
  103. global embToggle
  104. set embToggle Short
  105. checkbutton $t.toggle -textvariable embToggle -indicatoron 0 \
  106. -variable embToggle -onvalue "A much longer string" \
  107. -offvalue "Short" -cursor top_left_arrow -pady 5 -padx 2
  108. $t window create end -window $t.toggle -padx 3 -pady 2
  109. set i 1
  110. foreach color {AntiqueWhite3 Bisque1 Bisque2 Bisque3 Bisque4
  111. SlateBlue3 RoyalBlue1 SteelBlue2 DeepSkyBlue3 LightBlue1
  112. DarkSlateGray1 Aquamarine2 DarkSeaGreen2 SeaGreen1
  113. Yellow1 IndianRed1 IndianRed2 Tan1 Tan4} {
  114. button $t.color$i -text $color -cursor top_left_arrow -command \
  115. "$t configure -bg $color"
  116. $t window create end -window $t.color$i -padx 3 -pady 2
  117. incr i
  118. }
  119. $t tag add buttons $t.default end
  120. button $t.bigB -text "Big borders" -command "textWindBigB $t" \
  121. -cursor top_left_arrow
  122. button $t.smallB -text "Small borders" -command "textWindSmallB $t" \
  123. -cursor top_left_arrow
  124. button $t.bigH -text "Big highlight" -command "textWindBigH $t" \
  125. -cursor top_left_arrow
  126. button $t.smallH -text "Small highlight" -command "textWindSmallH $t" \
  127. -cursor top_left_arrow
  128. button $t.bigP -text "Big pad" -command "textWindBigP $t" \
  129. -cursor top_left_arrow
  130. button $t.smallP -text "Small pad" -command "textWindSmallP $t" \
  131. -cursor top_left_arrow
  132. set text_normal(border) [$t cget -borderwidth]
  133. set text_normal(highlight) [$t cget -highlightthickness]
  134. set text_normal(pad) [$t cget -padx]
  135. $t insert end "\nYou can also change the usual border width and "
  136. $t insert end "highlightthickness and padding.\n"
  137. $t window create end -window $t.bigB
  138. $t window create end -window $t.smallB
  139. $t window create end -window $t.bigH
  140. $t window create end -window $t.smallH
  141. $t window create end -window $t.bigP
  142. $t window create end -window $t.smallP
  143. $t insert end "\n\nFinally, images fit comfortably in text widgets too:"
  144. $t image create end -image \
  145. [image create bitmap -file [file join $tk_demoDirectory images face.xbm]]
  146. proc textWindBigB w {
  147. $w configure -borderwidth 15
  148. }
  149. proc textWindBigH w {
  150. $w configure -highlightthickness 15
  151. }
  152. proc textWindBigP w {
  153. $w configure -padx 15 -pady 15
  154. }
  155. proc textWindSmallB w {
  156. $w configure -borderwidth $::text_normal(border)
  157. }
  158. proc textWindSmallH w {
  159. $w configure -highlightthickness $::text_normal(highlight)
  160. }
  161. proc textWindSmallP w {
  162. $w configure -padx $::text_normal(pad) -pady $::text_normal(pad)
  163. }
  164. proc textWindOn w {
  165. catch {destroy $w.scroll2}
  166. set t $w.f.text
  167. scrollbar $w.scroll2 -orient horizontal -command "$t xview"
  168. pack $w.scroll2 -after $w.buttons -side bottom -fill x
  169. $t configure -xscrollcommand "$w.scroll2 set" -wrap none
  170. }
  171. proc textWindOff w {
  172. catch {destroy $w.scroll2}
  173. set t $w.f.text
  174. $t configure -xscrollcommand {} -wrap word
  175. }
  176. proc textWindPlot t {
  177. set c $t.c
  178. if {[winfo exists $c]} {
  179. return
  180. }
  181. while {[string first [$t get plot] " \t\n"] >= 0} {
  182. $t delete plot
  183. }
  184. $t insert plot "\n"
  185. $t window create plot -create {createPlot %W}
  186. $t tag add center plot
  187. $t insert plot "\n"
  188. }
  189. proc createPlot {t} {
  190. set c $t.c
  191. canvas $c -relief sunken -width 450 -height 300 -cursor top_left_arrow
  192. set font {Helvetica 18}
  193. $c create line 100 250 400 250 -width 2
  194. $c create line 100 250 100 50 -width 2
  195. $c create text 225 20 -text "A Simple Plot" -font $font -fill brown
  196. for {set i 0} {$i <= 10} {incr i} {
  197. set x [expr {100 + ($i*30)}]
  198. $c create line $x 250 $x 245 -width 2
  199. $c create text $x 254 -text [expr {10*$i}] -anchor n -font $font
  200. }
  201. for {set i 0} {$i <= 5} {incr i} {
  202. set y [expr {250 - ($i*40)}]
  203. $c create line 100 $y 105 $y -width 2
  204. $c create text 96 $y -text [expr {$i*50}].0 -anchor e -font $font
  205. }
  206. foreach point {
  207. {12 56} {20 94} {33 98} {32 120} {61 180} {75 160} {98 223}
  208. } {
  209. set x [expr {100 + (3*[lindex $point 0])}]
  210. set y [expr {250 - (4*[lindex $point 1])/5}]
  211. set item [$c create oval [expr {$x-6}] [expr {$y-6}] \
  212. [expr {$x+6}] [expr {$y+6}] -width 1 -outline black \
  213. -fill SkyBlue2]
  214. $c addtag point withtag $item
  215. }
  216. $c bind point <Any-Enter> "$c itemconfig current -fill red"
  217. $c bind point <Any-Leave> "$c itemconfig current -fill SkyBlue2"
  218. $c bind point <1> "embPlotDown $c %x %y"
  219. $c bind point <ButtonRelease-1> "$c dtag selected"
  220. bind $c <B1-Motion> "embPlotMove $c %x %y"
  221. return $c
  222. }
  223. set embPlot(lastX) 0
  224. set embPlot(lastY) 0
  225. proc embPlotDown {w x y} {
  226. global embPlot
  227. $w dtag selected
  228. $w addtag selected withtag current
  229. $w raise current
  230. set embPlot(lastX) $x
  231. set embPlot(lastY) $y
  232. }
  233. proc embPlotMove {w x y} {
  234. global embPlot
  235. $w move selected [expr {$x-$embPlot(lastX)}] [expr {$y-$embPlot(lastY)}]
  236. set embPlot(lastX) $x
  237. set embPlot(lastY) $y
  238. }
  239. proc textWindDel t {
  240. if {[winfo exists $t.c]} {
  241. $t delete $t.c
  242. while {[string first [$t get plot] " \t\n"] >= 0} {
  243. $t delete plot
  244. }
  245. $t insert plot " "
  246. }
  247. }
  248. proc embDefBg t {
  249. $t configure -background [lindex [$t configure -background] 3]
  250. }
  251. proc textMakePeer {parent} {
  252. set n 1
  253. while {[winfo exists .peer$n]} { incr n }
  254. set w [toplevel .peer$n]
  255. wm title $w "Text Peer #$n"
  256. frame $w.f -highlightthickness 1 -borderwidth 1 -relief sunken
  257. set t [$parent peer create $w.f.text -yscrollcommand "$w.scroll set" \
  258. -borderwidth 0 -highlightthickness 0]
  259. pack $t -expand yes -fill both
  260. scrollbar $w.scroll -command "$t yview"
  261. pack $w.scroll -side right -fill y
  262. pack $w.f -expand yes -fill both
  263. }
  264. proc textSplitWindow {textW} {
  265. if {$textW eq ".twind.f.text"} {
  266. if {[winfo exists .twind.peer]} {
  267. destroy .twind.peer
  268. } else {
  269. set parent [winfo parent $textW]
  270. set w [winfo parent $parent]
  271. set t [$textW peer create $w.peer \
  272. -yscrollcommand "$w.scroll set"]
  273. $w.pane add $t
  274. }
  275. } else {
  276. return
  277. }
  278. }