Update tk to version 8.5.11
[git/jnareb-git.git] / mingw / lib / tk8.5 / demos / ctext.tcl
blobe894bc2fca55f290056f4189656192ef06ff306d
1 # ctext.tcl --
3 # This demonstration script creates a canvas widget with a text
4 # item that can be edited and reconfigured in various ways.
6 if {![info exists widgetDemo]} {
7 error "This script should be run from the \"widget\" demo."
10 package require Tk
12 set w .ctext
13 catch {destroy $w}
14 toplevel $w
15 wm title $w "Canvas Text Demonstration"
16 wm iconname $w "Text"
17 positionWindow $w
18 set c $w.c
20 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:
21 1. You can point, click, and type.
22 2. You can also select with button 1.
23 3. You can copy the selection to the mouse position with button 2.
24 4. Backspace and Control+h delete the selection if there is one;
25 otherwise they delete the character just before the insertion cursor.
26 5. Delete deletes the selection if there is one; otherwise it deletes
27 the character just after the insertion cursor."
28 pack $w.msg -side top
30 ## See Code / Dismiss buttons
31 set btns [addSeeDismiss $w.buttons $w]
32 pack $btns -side bottom -fill x
34 canvas $c -relief flat -borderwidth 0 -width 500 -height 350
35 pack $w.c -side top -expand yes -fill both
37 set textFont {Helvetica 24}
39 $c create rectangle 245 195 255 205 -outline black -fill red
41 # First, create the text item and give it bindings so it can be edited.
43 $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]
44 $c bind text <1> "textB1Press $c %x %y"
45 $c bind text <B1-Motion> "textB1Move $c %x %y"
46 $c bind text <Shift-1> "$c select adjust current @%x,%y"
47 $c bind text <Shift-B1-Motion> "textB1Move $c %x %y"
48 $c bind text <KeyPress> "textInsert $c %A"
49 $c bind text <Return> "textInsert $c \\n"
50 $c bind text <Control-h> "textBs $c"
51 $c bind text <BackSpace> "textBs $c"
52 $c bind text <Delete> "textDel $c"
53 $c bind text <2> "textPaste $c @%x,%y"
55 # Next, create some items that allow the text's anchor position
56 # to be edited.
58 proc mkTextConfig {w x y option value color} {
59 set item [$w create rect $x $y [expr {$x+30}] [expr {$y+30}] \
60 -outline black -fill $color -width 1]
61 $w bind $item <1> "$w itemconf text $option $value"
62 $w addtag config withtag $item
65 set x 50
66 set y 50
67 set color LightSkyBlue1
68 mkTextConfig $c $x $y -anchor se $color
69 mkTextConfig $c [expr {$x+30}] [expr {$y }] -anchor s $color
70 mkTextConfig $c [expr {$x+60}] [expr {$y }] -anchor sw $color
71 mkTextConfig $c [expr {$x }] [expr {$y+30}] -anchor e $color
72 mkTextConfig $c [expr {$x+30}] [expr {$y+30}] -anchor center $color
73 mkTextConfig $c [expr {$x+60}] [expr {$y+30}] -anchor w $color
74 mkTextConfig $c [expr {$x }] [expr {$y+60}] -anchor ne $color
75 mkTextConfig $c [expr {$x+30}] [expr {$y+60}] -anchor n $color
76 mkTextConfig $c [expr {$x+60}] [expr {$y+60}] -anchor nw $color
77 set item [$c create rect \
78 [expr {$x+40}] [expr {$y+40}] [expr {$x+50}] [expr {$y+50}] \
79 -outline black -fill red]
80 $c bind $item <1> "$c itemconf text -anchor center"
81 $c create text [expr {$x+45}] [expr {$y-5}] \
82 -text {Text Position} -anchor s -font {Times 24} -fill brown
84 # Lastly, create some items that allow the text's justification to be
85 # changed.
87 set x 350
88 set y 50
89 set color SeaGreen2
90 mkTextConfig $c $x $y -justify left $color
91 mkTextConfig $c [expr {$x+30}] $y -justify center $color
92 mkTextConfig $c [expr {$x+60}] $y -justify right $color
93 $c create text [expr {$x+45}] [expr {$y-5}] \
94 -text {Justification} -anchor s -font {Times 24} -fill brown
96 $c bind config <Enter> "textEnter $c"
97 $c bind config <Leave> "$c itemconf current -fill \$textConfigFill"
99 set textConfigFill {}
101 proc textEnter {w} {
102 global textConfigFill
103 set textConfigFill [lindex [$w itemconfig current -fill] 4]
104 $w itemconfig current -fill black
107 proc textInsert {w string} {
108 if {$string == ""} {
109 return
111 catch {$w dchars text sel.first sel.last}
112 $w insert text insert $string
115 proc textPaste {w pos} {
116 catch {
117 $w insert text $pos [selection get]
121 proc textB1Press {w x y} {
122 $w icursor current @$x,$y
123 $w focus current
124 focus $w
125 $w select from current @$x,$y
128 proc textB1Move {w x y} {
129 $w select to current @$x,$y
132 proc textBs {w} {
133 if {![catch {$w dchars text sel.first sel.last}]} {
134 return
136 set char [expr {[$w index text insert] - 1}]
137 if {$char >= 0} {$w dchar text $char}
140 proc textDel {w} {
141 if {![catch {$w dchars text sel.first sel.last}]} {
142 return
144 $w dchars text insert