ctext.tcl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. 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 been defined to support editing (see above)." -width 440 -anchor n -font $textFont -justify left]
  34. $c bind text <1> "textB1Press $c %x %y"
  35. $c bind text <B1-Motion> "textB1Move $c %x %y"
  36. $c bind text <Shift-1> "$c select adjust current @%x,%y"
  37. $c bind text <Shift-B1-Motion> "textB1Move $c %x %y"
  38. $c bind text <KeyPress> "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. $c bind text <2> "textPaste $c @%x,%y"
  44. # Next, create some items that allow the text's anchor position
  45. # to be edited.
  46. proc mkTextConfig {w x y option value color} {
  47. set item [$w create rect $x $y [expr {$x+30}] [expr {$y+30}] \
  48. -outline black -fill $color -width 1]
  49. $w bind $item <1> "$w itemconf text $option $value"
  50. $w addtag config withtag $item
  51. }
  52. set x 50
  53. set y 50
  54. set color LightSkyBlue1
  55. mkTextConfig $c $x $y -anchor se $color
  56. mkTextConfig $c [expr {$x+30}] [expr {$y }] -anchor s $color
  57. mkTextConfig $c [expr {$x+60}] [expr {$y }] -anchor sw $color
  58. mkTextConfig $c [expr {$x }] [expr {$y+30}] -anchor e $color
  59. mkTextConfig $c [expr {$x+30}] [expr {$y+30}] -anchor center $color
  60. mkTextConfig $c [expr {$x+60}] [expr {$y+30}] -anchor w $color
  61. mkTextConfig $c [expr {$x }] [expr {$y+60}] -anchor ne $color
  62. mkTextConfig $c [expr {$x+30}] [expr {$y+60}] -anchor n $color
  63. mkTextConfig $c [expr {$x+60}] [expr {$y+60}] -anchor nw $color
  64. set item [$c create rect \
  65. [expr {$x+40}] [expr {$y+40}] [expr {$x+50}] [expr {$y+50}] \
  66. -outline black -fill red]
  67. $c bind $item <1> "$c itemconf text -anchor center"
  68. $c create text [expr {$x+45}] [expr {$y-5}] \
  69. -text {Text Position} -anchor s -font {Times 24} -fill brown
  70. # Lastly, create some items that allow the text's justification to be
  71. # changed.
  72. set x 350
  73. set y 50
  74. set color SeaGreen2
  75. mkTextConfig $c $x $y -justify left $color
  76. mkTextConfig $c [expr {$x+30}] $y -justify center $color
  77. mkTextConfig $c [expr {$x+60}] $y -justify right $color
  78. $c create text [expr {$x+45}] [expr {$y-5}] \
  79. -text {Justification} -anchor s -font {Times 24} -fill brown
  80. $c bind config <Enter> "textEnter $c"
  81. $c bind config <Leave> "$c itemconf current -fill \$textConfigFill"
  82. set textConfigFill {}
  83. proc textEnter {w} {
  84. global textConfigFill
  85. set textConfigFill [lindex [$w itemconfig current -fill] 4]
  86. $w itemconfig current -fill black
  87. }
  88. proc textInsert {w string} {
  89. if {$string == ""} {
  90. return
  91. }
  92. catch {$w dchars text sel.first sel.last}
  93. $w insert text insert $string
  94. }
  95. proc textPaste {w pos} {
  96. catch {
  97. $w insert text $pos [selection get]
  98. }
  99. }
  100. proc textB1Press {w x y} {
  101. $w icursor current @$x,$y
  102. $w focus current
  103. focus $w
  104. $w select from current @$x,$y
  105. }
  106. proc textB1Move {w x y} {
  107. $w select to current @$x,$y
  108. }
  109. proc textBs {w} {
  110. if {![catch {$w dchars text sel.first sel.last}]} {
  111. return
  112. }
  113. set char [expr {[$w index text insert] - 1}]
  114. if {$char >= 0} {$w dchar text $char}
  115. }
  116. proc textDel {w} {
  117. if {![catch {$w dchars text sel.first sel.last}]} {
  118. return
  119. }
  120. $w dchars text insert
  121. }