twind.tcl 11 KB

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