home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09962.iso / vrml / cp2b2x.exe / DATA.Z / ctext.tcl < prev    next >
Text File  |  1996-04-23  |  5KB  |  143 lines

  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. #
  6. # @(#) ctext.tcl 1.2 95/07/18 08:31:13
  7.  
  8. set w .ctext
  9. catch {destroy $w}
  10. toplevel $w
  11. wm title $w "Canvas Text Demonstration"
  12. wm iconname $w "Text"
  13. positionWindow $w
  14. set c $w.c
  15.  
  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.  
  26. frame $w.buttons
  27. pack  $w.buttons -side bottom -expand y -fill x -pady 2m
  28. button $w.buttons.dismiss -text Dismiss -command "destroy $w"
  29. button $w.buttons.code -text "See Code" -command "showCode $w"
  30. pack $w.buttons.dismiss $w.buttons.code -side left -expand 1
  31.  
  32. canvas $c -relief flat -borderwidth 0 -width 500 -height 350
  33. pack $w.c -side top -expand yes -fill both
  34.  
  35. set textFont -*-Helvetica-Medium-R-Normal--*-240-*-*-*-*-*-*
  36.  
  37. $c create rectangle 245 195 255 205 -outline black -fill red
  38.  
  39. # First, create the text item and give it bindings so it can be edited.
  40.  
  41. $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 -*-Helvetica-Medium-R-Normal--*-240-*-*-*-*-*-* -justify left]
  42. $c bind text <1> "textB1Press $c %x %y"
  43. $c bind text <B1-Motion> "textB1Move $c %x %y"
  44. $c bind text <Shift-1> "$c select adjust current @%x,%y"
  45. $c bind text <Shift-B1-Motion> "textB1Move $c %x %y"
  46. $c bind text <KeyPress> "textInsert $c %A"
  47. $c bind text <Return> "textInsert $c \\n"
  48. $c bind text <Control-h> "textBs $c"
  49. $c bind text <BackSpace> "textBs $c"
  50. $c bind text <Delete> "textDel $c"
  51. $c bind text <2> "textPaste $c @%x,%y" 
  52.  
  53. # Next, create some items that allow the text's anchor position
  54. # to be edited.
  55.  
  56. proc mkTextConfig {w x y option value color} {
  57.     set item [$w create rect [expr $x] [expr $y] [expr $x+30] [expr $y+30] \
  58.         -outline black -fill $color -width 1]
  59.     $w bind $item <1> "$w itemconf text $option $value"
  60.     $w addtag config withtag $item
  61. }
  62.  
  63. set x 50
  64. set y 50
  65. set color LightSkyBlue1
  66. mkTextConfig $c $x $y -anchor se $color
  67. mkTextConfig $c [expr $x+30] [expr $y] -anchor s $color
  68. mkTextConfig $c [expr $x+60] [expr $y] -anchor sw $color
  69. mkTextConfig $c [expr $x] [expr $y+30] -anchor e $color
  70. mkTextConfig $c [expr $x+30] [expr $y+30] -anchor center $color
  71. mkTextConfig $c [expr $x+60] [expr $y+30] -anchor w $color
  72. mkTextConfig $c [expr $x] [expr $y+60] -anchor ne $color
  73. mkTextConfig $c [expr $x+30] [expr $y+60] -anchor n $color
  74. mkTextConfig $c [expr $x+60] [expr $y+60] -anchor nw $color
  75. set item [$c create rect [expr $x+40] [expr $y+40] [expr $x+50] [expr $y+50] \
  76.     -outline black -fill red]
  77. $c bind $item <1> "$c itemconf text -anchor center"
  78. $c create text [expr $x+45] [expr $y-5] -text {Text Position} -anchor s \
  79.     -font -*-times-medium-r-normal--*-240-*-*-*-*-*-* -fill brown
  80.  
  81. # Lastly, create some items that allow the text's justification to be
  82. # changed.
  83.  
  84. set x 350
  85. set y 50
  86. set color SeaGreen2
  87. mkTextConfig $c $x $y -justify left $color
  88. mkTextConfig $c [expr $x+30] [expr $y] -justify center $color
  89. mkTextConfig $c [expr $x+60] [expr $y] -justify right $color
  90. $c create text [expr $x+45] [expr $y-5] -text {Justification} -anchor s \
  91.     -font -*-times-medium-r-normal--*-240-*-*-*-*-*-* -fill brown
  92.  
  93. $c bind config <Enter> "textEnter $c"
  94. $c bind config <Leave> "$c itemconf current -fill \$textConfigFill"
  95.  
  96. set textConfigFill {}
  97.  
  98. proc textEnter {w} {
  99.     global textConfigFill
  100.     set textConfigFill [lindex [$w itemconfig current -fill] 4]
  101.     $w itemconfig current -fill black
  102. }
  103.  
  104. proc textInsert {w string} {
  105.     if {$string == ""} {
  106.     return
  107.     }
  108.     catch {$w dchars text sel.first sel.last}
  109.     $w insert text insert $string
  110. }
  111.  
  112. proc textPaste {w pos} {
  113.     catch {
  114.     $w insert text $pos [selection get]
  115.     }
  116. }
  117.  
  118. proc textB1Press {w x y} {
  119.     $w icursor current @$x,$y
  120.     $w focus current
  121.     focus $w
  122.     $w select from current @$x,$y
  123. }
  124.  
  125. proc textB1Move {w x y} {
  126.     $w select to current @$x,$y
  127. }
  128.  
  129. proc textBs {w} {
  130.     if ![catch {$w dchars text sel.first sel.last}] {
  131.     return
  132.     }
  133.     set char [expr {[$w index text insert] - 1}]
  134.     if {$char >= 0} {$w dchar text $char}
  135. }
  136.  
  137. proc textDel {w} {
  138.     if ![catch {$w dchars text sel.first sel.last}] {
  139.     return
  140.     }
  141.     $w dchars text insert
  142. }
  143.