twind.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. ttk::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 that the embedded windows, "
  75. $t insert end "such as buttons may not appear in the peer. To ensure "
  76. $t insert end "that embedded windows appear in all peers you can set the "
  77. $t insert end "'-create' option to a script or a string containing %W. "
  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. $t insert end "\nNOTE: these buttons will not appear in peers!\n" "peer_warning"
  101. button $t.default -text Default -command "embDefBg $t" \
  102. -cursor top_left_arrow
  103. $t window create end -window $t.default -padx 3
  104. global embToggle
  105. set embToggle Short
  106. checkbutton $t.toggle -textvariable embToggle -indicatoron 0 \
  107. -variable embToggle -onvalue "A much longer string" \
  108. -offvalue "Short" -cursor top_left_arrow -pady 5 -padx 2
  109. $t window create end -window $t.toggle -padx 3 -pady 2
  110. set i 1
  111. foreach color {AntiqueWhite3 Bisque1 Bisque2 Bisque3 Bisque4
  112. SlateBlue3 RoyalBlue1 SteelBlue2 DeepSkyBlue3 LightBlue1
  113. DarkSlateGray1 Aquamarine2 DarkSeaGreen2 SeaGreen1
  114. Yellow1 IndianRed1 IndianRed2 Tan1 Tan4} {
  115. button $t.color$i -text $color -cursor top_left_arrow -command \
  116. "$t configure -bg $color"
  117. $t window create end -window $t.color$i -padx 3 -pady 2
  118. incr i
  119. }
  120. $t tag add buttons $t.default end
  121. button $t.bigB -text "Big borders" -command "textWindBigB $t" \
  122. -cursor top_left_arrow
  123. button $t.smallB -text "Small borders" -command "textWindSmallB $t" \
  124. -cursor top_left_arrow
  125. button $t.bigH -text "Big highlight" -command "textWindBigH $t" \
  126. -cursor top_left_arrow
  127. button $t.smallH -text "Small highlight" -command "textWindSmallH $t" \
  128. -cursor top_left_arrow
  129. button $t.bigP -text "Big pad" -command "textWindBigP $t" \
  130. -cursor top_left_arrow
  131. button $t.smallP -text "Small pad" -command "textWindSmallP $t" \
  132. -cursor top_left_arrow
  133. set text_normal(border) [$t cget -borderwidth]
  134. set text_normal(highlight) [$t cget -highlightthickness]
  135. set text_normal(pad) [$t cget -padx]
  136. $t insert end "\nYou can also change the usual border width and "
  137. $t insert end "highlightthickness and padding.\n"
  138. $t window create end -window $t.bigB
  139. $t window create end -window $t.smallB
  140. $t window create end -window $t.bigH
  141. $t window create end -window $t.smallH
  142. $t window create end -window $t.bigP
  143. $t window create end -window $t.smallP
  144. $t insert end "\n\nFinally, images fit comfortably in text widgets too:"
  145. $t image create end -image \
  146. [image create photo -file [file join $tk_demoDirectory images ouster.png]]
  147. proc textWindBigB w {
  148. $w configure -borderwidth 15
  149. }
  150. proc textWindBigH w {
  151. $w configure -highlightthickness 15
  152. }
  153. proc textWindBigP w {
  154. $w configure -padx 15 -pady 15
  155. }
  156. proc textWindSmallB w {
  157. $w configure -borderwidth $::text_normal(border)
  158. }
  159. proc textWindSmallH w {
  160. $w configure -highlightthickness $::text_normal(highlight)
  161. }
  162. proc textWindSmallP w {
  163. $w configure -padx $::text_normal(pad) -pady $::text_normal(pad)
  164. }
  165. proc textWindOn w {
  166. catch {destroy $w.scroll2}
  167. set t $w.f.text
  168. ttk::scrollbar $w.scroll2 -orient horizontal -command "$t xview"
  169. pack $w.scroll2 -after $w.buttons -side bottom -fill x
  170. $t configure -xscrollcommand "$w.scroll2 set" -wrap none
  171. }
  172. proc textWindOff w {
  173. catch {destroy $w.scroll2}
  174. set t $w.f.text
  175. $t configure -xscrollcommand {} -wrap word
  176. }
  177. proc textWindPlot t {
  178. set c $t.c
  179. if {[winfo exists $c]} {
  180. return
  181. }
  182. while {[string first [$t get plot] " \t\n"] >= 0} {
  183. $t delete plot
  184. }
  185. $t insert plot "\n"
  186. $t window create plot -create {createPlot %W}
  187. $t tag add center plot
  188. $t insert plot "\n"
  189. }
  190. proc createPlot {t} {
  191. set c $t.c
  192. canvas $c -relief sunken -width 450 -height 300 -cursor top_left_arrow
  193. set font {Helvetica 18}
  194. $c create line 100 250 400 250 -width 2
  195. $c create line 100 250 100 50 -width 2
  196. $c create text 225 20 -text "A Simple Plot" -font $font -fill brown
  197. for {set i 0} {$i <= 10} {incr i} {
  198. set x [expr {100 + ($i*30)}]
  199. $c create line $x 250 $x 245 -width 2
  200. $c create text $x 254 -text [expr {10*$i}] -anchor n -font $font
  201. }
  202. for {set i 0} {$i <= 5} {incr i} {
  203. set y [expr {250 - ($i*40)}]
  204. $c create line 100 $y 105 $y -width 2
  205. $c create text 96 $y -text [expr {$i*50}].0 -anchor e -font $font
  206. }
  207. foreach point {
  208. {12 56} {20 94} {33 98} {32 120} {61 180} {75 160} {98 223}
  209. } {
  210. set x [expr {100 + (3*[lindex $point 0])}]
  211. set y [expr {250 - (4*[lindex $point 1])/5}]
  212. set item [$c create oval [expr {$x-6}] [expr {$y-6}] \
  213. [expr {$x+6}] [expr {$y+6}] -width 1 -outline black \
  214. -fill SkyBlue2]
  215. $c addtag point withtag $item
  216. }
  217. $c bind point <Any-Enter> "$c itemconfig current -fill red"
  218. $c bind point <Any-Leave> "$c itemconfig current -fill SkyBlue2"
  219. $c bind point <1> "embPlotDown $c %x %y"
  220. $c bind point <ButtonRelease-1> "$c dtag selected"
  221. bind $c <B1-Motion> "embPlotMove $c %x %y"
  222. return $c
  223. }
  224. set embPlot(lastX) 0
  225. set embPlot(lastY) 0
  226. proc embPlotDown {w x y} {
  227. global embPlot
  228. $w dtag selected
  229. $w addtag selected withtag current
  230. $w raise current
  231. set embPlot(lastX) $x
  232. set embPlot(lastY) $y
  233. }
  234. proc embPlotMove {w x y} {
  235. global embPlot
  236. $w move selected [expr {$x-$embPlot(lastX)}] [expr {$y-$embPlot(lastY)}]
  237. set embPlot(lastX) $x
  238. set embPlot(lastY) $y
  239. }
  240. proc textWindDel t {
  241. if {[winfo exists $t.c]} {
  242. $t delete $t.c
  243. while {[string first [$t get plot] " \t\n"] >= 0} {
  244. $t delete plot
  245. }
  246. $t insert plot " "
  247. }
  248. }
  249. proc embDefBg t {
  250. $t configure -background [lindex [$t configure -background] 3]
  251. }
  252. proc textMakePeer {parent} {
  253. set n 1
  254. while {[winfo exists .peer$n]} { incr n }
  255. set w [toplevel .peer$n]
  256. wm title $w "Text Peer #$n"
  257. frame $w.f -highlightthickness 1 -borderwidth 1 -relief sunken
  258. set t [$parent peer create $w.f.text -yscrollcommand "$w.scroll set" \
  259. -borderwidth 0 -highlightthickness 0]
  260. $t tag configure peer_warning -font boldFont
  261. pack $t -expand yes -fill both
  262. ttk::scrollbar $w.scroll -command "$t yview"
  263. pack $w.scroll -side right -fill y
  264. pack $w.f -expand yes -fill both
  265. }
  266. proc textSplitWindow {textW} {
  267. if {$textW eq ".twind.f.text"} {
  268. if {[winfo exists .twind.peer]} {
  269. destroy .twind.peer
  270. } else {
  271. set parent [winfo parent $textW]
  272. set w [winfo parent $parent]
  273. set t [$textW peer create $w.peer \
  274. -yscrollcommand "$w.scroll set"]
  275. $t tag configure peer_warning -font boldFont
  276. $w.pane add $t
  277. }
  278. } else {
  279. return
  280. }
  281. }