ctext.tcl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # ctext.tcl --
  2. #
  3. # This demonstration script creates a canvas widget with a text
  4. # item that can be edited and reconfigured in various ways.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .ctext
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Canvas Text Demonstration"
  13. wm iconname $w "Text"
  14. positionWindow $w
  15. set c $w.c
  16. label $w.msg -font $font -wraplength 5i -justify left -text "This window displays a string of text to demonstrate the text facilities of canvas widgets. You can click in the boxes to adjust the position of the text relative to its positioning point or change its justification, and on a pie slice to change its angle. The text also supports the following simple bindings for editing:
  17. 1. You can point, click, and type.
  18. 2. You can also select with button 1.
  19. 3. You can copy the selection to the mouse position with button 2.
  20. 4. Backspace and Control+h delete the selection if there is one;
  21. otherwise they delete the character just before the insertion cursor.
  22. 5. Delete deletes the selection if there is one; otherwise it deletes
  23. the character just after the insertion cursor."
  24. pack $w.msg -side top
  25. ## See Code / Dismiss buttons
  26. set btns [addSeeDismiss $w.buttons $w]
  27. pack $btns -side bottom -fill x
  28. canvas $c -relief flat -borderwidth 0 -width 500 -height 350
  29. pack $w.c -side top -expand yes -fill both
  30. set textFont {Helvetica 24}
  31. $c create rectangle 245 195 255 205 -outline black -fill red
  32. # First, create the text item and give it bindings so it can be edited.
  33. $c addtag text withtag [$c create text 250 200 -text "This is just a string of text to demonstrate the text facilities of canvas widgets. Bindings have been defined to support editing (see above)." -width 440 -anchor n -font $textFont -justify left]
  34. $c bind text <Button-1> "textB1Press $c %x %y"
  35. $c bind text <B1-Motion> "textB1Move $c %x %y"
  36. $c bind text <Shift-Button-1> "$c select adjust current @%x,%y"
  37. $c bind text <Shift-B1-Motion> "textB1Move $c %x %y"
  38. $c bind text <Key> "textInsert $c %A"
  39. $c bind text <Return> "textInsert $c \\n"
  40. $c bind text <Control-h> "textBs $c"
  41. $c bind text <BackSpace> "textBs $c"
  42. $c bind text <Delete> "textDel $c"
  43. if {[tk windowingsystem] eq "aqua" && ![package vsatisfies [package provide Tk] 8.7-]} {
  44. $c bind text <Button-3> "textPaste $c @%x,%y"
  45. } else {
  46. $c bind text <Button-2> "textPaste $c @%x,%y"
  47. }
  48. # Next, create some items that allow the text's anchor position
  49. # to be edited.
  50. proc mkTextConfigBox {w x y option value color} {
  51. set item [$w create rect $x $y [expr {$x+30}] [expr {$y+30}] \
  52. -outline black -fill $color -width 1]
  53. $w bind $item <Button-1> "$w itemconf text $option $value"
  54. $w addtag config withtag $item
  55. }
  56. proc mkTextConfigPie {w x y a option value color} {
  57. set item [$w create arc $x $y [expr {$x+90}] [expr {$y+90}] \
  58. -start [expr {$a-15}] -extent 30 -outline black -fill $color \
  59. -width 1]
  60. $w bind $item <Button-1> "$w itemconf text $option $value"
  61. $w addtag config withtag $item
  62. }
  63. set x 50
  64. set y 50
  65. set color LightSkyBlue1
  66. mkTextConfigBox $c $x $y -anchor se $color
  67. mkTextConfigBox $c [expr {$x+30}] [expr {$y }] -anchor s $color
  68. mkTextConfigBox $c [expr {$x+60}] [expr {$y }] -anchor sw $color
  69. mkTextConfigBox $c [expr {$x }] [expr {$y+30}] -anchor e $color
  70. mkTextConfigBox $c [expr {$x+30}] [expr {$y+30}] -anchor center $color
  71. mkTextConfigBox $c [expr {$x+60}] [expr {$y+30}] -anchor w $color
  72. mkTextConfigBox $c [expr {$x }] [expr {$y+60}] -anchor ne $color
  73. mkTextConfigBox $c [expr {$x+30}] [expr {$y+60}] -anchor n $color
  74. mkTextConfigBox $c [expr {$x+60}] [expr {$y+60}] -anchor nw $color
  75. set item [$c create rect \
  76. [expr {$x+40}] [expr {$y+40}] [expr {$x+50}] [expr {$y+50}] \
  77. -outline black -fill red]
  78. $c bind $item <Button-1> "$c itemconf text -anchor center"
  79. $c create text [expr {$x+45}] [expr {$y-5}] \
  80. -text {Text Position} -anchor s -font {Times 20} -fill brown
  81. # Now create some items that allow the text's angle to be changed.
  82. set x 205
  83. set y 50
  84. set color Yellow
  85. mkTextConfigPie $c $x $y 0 -angle 90 $color
  86. mkTextConfigPie $c $x $y 30 -angle 120 $color
  87. mkTextConfigPie $c $x $y 60 -angle 150 $color
  88. mkTextConfigPie $c $x $y 90 -angle 180 $color
  89. mkTextConfigPie $c $x $y 120 -angle 210 $color
  90. mkTextConfigPie $c $x $y 150 -angle 240 $color
  91. mkTextConfigPie $c $x $y 180 -angle 270 $color
  92. mkTextConfigPie $c $x $y 210 -angle 300 $color
  93. mkTextConfigPie $c $x $y 240 -angle 330 $color
  94. mkTextConfigPie $c $x $y 270 -angle 0 $color
  95. mkTextConfigPie $c $x $y 300 -angle 30 $color
  96. mkTextConfigPie $c $x $y 330 -angle 60 $color
  97. $c create text [expr {$x+45}] [expr {$y-5}] \
  98. -text {Text Angle} -anchor s -font {Times 20} -fill brown
  99. # Lastly, create some items that allow the text's justification to be
  100. # changed.
  101. set x 350
  102. set y 50
  103. set color SeaGreen2
  104. mkTextConfigBox $c $x $y -justify left $color
  105. mkTextConfigBox $c [expr {$x+30}] $y -justify center $color
  106. mkTextConfigBox $c [expr {$x+60}] $y -justify right $color
  107. $c create text [expr {$x+45}] [expr {$y-5}] \
  108. -text {Justification} -anchor s -font {Times 20} -fill brown
  109. $c bind config <Enter> "textEnter $c"
  110. $c bind config <Leave> "$c itemconf current -fill \$textConfigFill"
  111. set textConfigFill {}
  112. proc textEnter {w} {
  113. global textConfigFill
  114. set textConfigFill [lindex [$w itemconfig current -fill] 4]
  115. $w itemconfig current -fill black
  116. }
  117. proc textInsert {w string} {
  118. if {$string == ""} {
  119. return
  120. }
  121. catch {$w dchars text sel.first sel.last}
  122. $w insert text insert $string
  123. }
  124. proc textPaste {w pos} {
  125. catch {
  126. $w insert text $pos [selection get]
  127. }
  128. }
  129. proc textB1Press {w x y} {
  130. $w icursor current @$x,$y
  131. $w focus current
  132. focus $w
  133. $w select from current @$x,$y
  134. }
  135. proc textB1Move {w x y} {
  136. $w select to current @$x,$y
  137. }
  138. proc textBs {w} {
  139. if {![catch {$w dchars text sel.first sel.last}]} {
  140. return
  141. }
  142. set char [expr {[$w index text insert] - 1}]
  143. if {$char >= 0} {$w dchar text $char}
  144. }
  145. proc textDel {w} {
  146. if {![catch {$w dchars text sel.first sel.last}]} {
  147. return
  148. }
  149. $w dchars text insert
  150. }