OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / ALPHALINUX5 / util / ALPHALINUX5 / lib / tk8.3 / demos / ctext.tcl
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 # RCS: @(#) $Id: ctext.tcl,v 1.2 1998/09/14 18:23:27 stanton Exp $
7
8 if {![info exists widgetDemo]} {
9     error "This script should be run from the \"widget\" demo."
10 }
11
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
19
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
29
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
35
36 canvas $c -relief flat -borderwidth 0 -width 500 -height 350
37 pack $w.c -side top -expand yes -fill both
38
39 set textFont {Helvetica 24}
40
41 $c create rectangle 245 195 255 205 -outline black -fill red
42
43 # First, create the text item and give it bindings so it can be edited.
44
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" 
56
57 # Next, create some items that allow the text's anchor position
58 # to be edited.
59
60 proc mkTextConfig {w x y option value color} {
61     set item [$w create rect [expr $x] [expr $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
65 }
66
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 [expr $x+40] [expr $y+40] [expr $x+50] [expr $y+50] \
80         -outline black -fill red]
81 $c bind $item <1> "$c itemconf text -anchor center"
82 $c create text [expr $x+45] [expr $y-5] -text {Text Position} -anchor s \
83         -font {Times 24} -fill brown
84
85 # Lastly, create some items that allow the text's justification to be
86 # changed.
87
88 set x 350
89 set y 50
90 set color SeaGreen2
91 mkTextConfig $c $x $y -justify left $color
92 mkTextConfig $c [expr $x+30] [expr $y] -justify center $color
93 mkTextConfig $c [expr $x+60] [expr $y] -justify right $color
94 $c create text [expr $x+45] [expr $y-5] -text {Justification} -anchor s \
95         -font {Times 24} -fill brown
96
97 $c bind config <Enter> "textEnter $c"
98 $c bind config <Leave> "$c itemconf current -fill \$textConfigFill"
99
100 set textConfigFill {}
101
102 proc textEnter {w} {
103     global textConfigFill
104     set textConfigFill [lindex [$w itemconfig current -fill] 4]
105     $w itemconfig current -fill black
106 }
107
108 proc textInsert {w string} {
109     if {$string == ""} {
110         return
111     }
112     catch {$w dchars text sel.first sel.last}
113     $w insert text insert $string
114 }
115
116 proc textPaste {w pos} {
117     catch {
118         $w insert text $pos [selection get]
119     }
120 }
121
122 proc textB1Press {w x y} {
123     $w icursor current @$x,$y
124     $w focus current
125     focus $w
126     $w select from current @$x,$y
127 }
128
129 proc textB1Move {w x y} {
130     $w select to current @$x,$y
131 }
132
133 proc textBs {w} {
134     if ![catch {$w dchars text sel.first sel.last}] {
135         return
136     }
137     set char [expr {[$w index text insert] - 1}]
138     if {$char >= 0} {$w dchar text $char}
139 }
140
141 proc textDel {w} {
142     if ![catch {$w dchars text sel.first sel.last}] {
143         return
144     }
145     $w dchars text insert
146 }