Merge branch 'master' of http://repo.or.cz/r/msysgit into devel
[msysgit/historical-msysgit.git] / mingw / lib / tk8.4 / demos / ctext.tcl
blobfb120a897a79c6e42795353e4283ec5ccda8c3d4
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 # RCS: @(#) $Id: ctext.tcl,v 1.3 2001/06/14 10:56:58 dkf Exp $
8 if {![info exists widgetDemo]} {
9 error "This script should be run from the \"widget\" demo."
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 frame $w.buttons
31 pack $w.buttons -side bottom -fill x -pady 2m
32 button $w.buttons.dismiss -text Dismiss -command "destroy $w"
33 button $w.buttons.code -text "See Code" -command "showCode $w"
34 pack $w.buttons.dismiss $w.buttons.code -side left -expand 1
36 canvas $c -relief flat -borderwidth 0 -width 500 -height 350
37 pack $w.c -side top -expand yes -fill both
39 set textFont {Helvetica 24}
41 $c create rectangle 245 195 255 205 -outline black -fill red
43 # First, create the text item and give it bindings so it can be edited.
45 $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 24} -justify left]
46 $c bind text <1> "textB1Press $c %x %y"
47 $c bind text <B1-Motion> "textB1Move $c %x %y"
48 $c bind text <Shift-1> "$c select adjust current @%x,%y"
49 $c bind text <Shift-B1-Motion> "textB1Move $c %x %y"
50 $c bind text <KeyPress> "textInsert $c %A"
51 $c bind text <Return> "textInsert $c \\n"
52 $c bind text <Control-h> "textBs $c"
53 $c bind text <BackSpace> "textBs $c"
54 $c bind text <Delete> "textDel $c"
55 $c bind text <2> "textPaste $c @%x,%y"
57 # Next, create some items that allow the text's anchor position
58 # to be edited.
60 proc mkTextConfig {w x y option value color} {
61 set item [$w create rect $x $y [expr {$x+30}] [expr {$y+30}] \
62 -outline black -fill $color -width 1]
63 $w bind $item <1> "$w itemconf text $option $value"
64 $w addtag config withtag $item
67 set x 50
68 set y 50
69 set color LightSkyBlue1
70 mkTextConfig $c $x $y -anchor se $color
71 mkTextConfig $c [expr {$x+30}] [expr {$y }] -anchor s $color
72 mkTextConfig $c [expr {$x+60}] [expr {$y }] -anchor sw $color
73 mkTextConfig $c [expr {$x }] [expr {$y+30}] -anchor e $color
74 mkTextConfig $c [expr {$x+30}] [expr {$y+30}] -anchor center $color
75 mkTextConfig $c [expr {$x+60}] [expr {$y+30}] -anchor w $color
76 mkTextConfig $c [expr {$x }] [expr {$y+60}] -anchor ne $color
77 mkTextConfig $c [expr {$x+30}] [expr {$y+60}] -anchor n $color
78 mkTextConfig $c [expr {$x+60}] [expr {$y+60}] -anchor nw $color
79 set item [$c create rect \
80 [expr {$x+40}] [expr {$y+40}] [expr {$x+50}] [expr {$y+50}] \
81 -outline black -fill red]
82 $c bind $item <1> "$c itemconf text -anchor center"
83 $c create text [expr {$x+45}] [expr {$y-5}] \
84 -text {Text Position} -anchor s -font {Times 24} -fill brown
86 # Lastly, create some items that allow the text's justification to be
87 # changed.
89 set x 350
90 set y 50
91 set color SeaGreen2
92 mkTextConfig $c $x $y -justify left $color
93 mkTextConfig $c [expr {$x+30}] $y -justify center $color
94 mkTextConfig $c [expr {$x+60}] $y -justify right $color
95 $c create text [expr {$x+45}] [expr {$y-5}] \
96 -text {Justification} -anchor s -font {Times 24} -fill brown
98 $c bind config <Enter> "textEnter $c"
99 $c bind config <Leave> "$c itemconf current -fill \$textConfigFill"
101 set textConfigFill {}
103 proc textEnter {w} {
104 global textConfigFill
105 set textConfigFill [lindex [$w itemconfig current -fill] 4]
106 $w itemconfig current -fill black
109 proc textInsert {w string} {
110 if {$string == ""} {
111 return
113 catch {$w dchars text sel.first sel.last}
114 $w insert text insert $string
117 proc textPaste {w pos} {
118 catch {
119 $w insert text $pos [selection get]
123 proc textB1Press {w x y} {
124 $w icursor current @$x,$y
125 $w focus current
126 focus $w
127 $w select from current @$x,$y
130 proc textB1Move {w x y} {
131 $w select to current @$x,$y
134 proc textBs {w} {
135 if {![catch {$w dchars text sel.first sel.last}]} {
136 return
138 set char [expr {[$w index text insert] - 1}]
139 if {$char >= 0} {$w dchar text $char}
142 proc textDel {w} {
143 if {![catch {$w dchars text sel.first sel.last}]} {
144 return
146 $w dchars text insert