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