Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / lib / site_perl / 5.8.0 / sun4-solaris / Tk / demos / widget_lib / ctext.pl
CommitLineData
86530b38
AT
1# ctext.pl
2
3use subs qw/ctext_bs ctext_configure ctext_enter ctext_move ctext_press/;
4use vars qw/$TOP/;
5
6sub ctext {
7
8 # Create a window containing a canvas displaying a text string and
9 # allowing the string to be edited and re-anchored.
10
11 my($demo) = @_;
12 $TOP = $MW->WidgetDemo(
13 -name => $demo,
14 -text => ['This window displays a string of text to demonstrate the text facilities of canvas widgets. You can click in the boxes to adijust the position of the text relative to its positioning point or change its justification. The text also supports the following simple bindings for editing:
15 1. You can point, click, and type.
16 2. You can also select with button 1.
17 3. You can copy the selection to the mouse position with button 2.
18 4. Backspace and Control+h delete the selection if there is one;
19 otherwise they delete the character just before the insertion cursor.
20 5. Delete deletes the selection if there is one; otherwise it deletes
21 the character just after the insertion cursor.', qw/-wraplength 5i/],
22 -title => 'Canvas Text Demonstration',
23 -iconname => 'ctext',
24 );
25
26 my $c = $TOP->Canvas(qw/-relief flat -bd 0 -width 500 -height 350/);
27 $c->pack(qw/-side top -expand yes -fill both/);
28
29 $c->create(qw/rectangle 245 195 255 205 -outline black -fill red/);
30
31 # First, create the text item and give it bindings so it can be edited.
32
33 $c->addtag(qw/text withtag/,
34 $c->create('text', 250, 200,
35 -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)."',
36 qw/-width 440 -anchor n -justify left
37 -font -*-Helvetica-Medium-R-Normal--*-240-*-*-*-*-*-*/
38 ),
39 );
40 $c->bind(qw/text <1>/ => \&ctext_press);
41 $c->bind(qw/text <B1-Motion>/ => \&ctext_move);
42 $c->bind(qw/text <Shift-1>/ => sub {
43 my($c) = @_;
44 my $e = $c->XEvent;
45 my($x, $y) = ($e->x, $e->y);
46 $c->select(qw/adjust current/, "\@$x,$y");
47 });
48 $c->bind(qw/text <Shift-B1-Motion>/ => \&ctext_move);
49 $c->bind(qw/text <KeyPress>/ => sub {
50 my($c) = @_;
51 my $e = $c->XEvent;
52 my $A = $e->A;
53 $c->insert(qw/text insert/, "$A");
54 });
55 $c->bind(qw/text <Return>/ => sub {
56 my($c) = @_;
57 $c->insert(qw/text insert/, "\\n");
58 });
59 $c->bind(qw/text <Control-h>/ => \&ctext_bs);
60 $c->bind(qw/text <BackSpace>/ => \&ctext_bs);
61 $c->bind(qw/text <Delete>/ => sub {
62 my($c) = @_;
63 eval {local $SIG{__DIE__}; $c->dchars(qw/text sel.first sel.last/)};
64 $c->dchars('text', 'insert');
65 });
66 $c->bind(qw/text <2>/ => sub {
67 my($c) = @_;
68 my $e = $c->XEvent;
69 $c->insert('text', $e->xy, $MW->SelectionGet);
70 });
71
72 # Next, create some items that allow the text's anchor position to
73 # be edited.
74
75 my($x, $y, $color) = (50, 50, 'LightSkyBlue1');
76 ctext_configure $c, $x, $y, -anchor => 'se', $color;
77 ctext_configure $c, $x+30, $y, -anchor => 's', $color;
78 ctext_configure $c, $x+60, $y, -anchor => 'sw', $color;
79 ctext_configure $c, $x, $y+30, -anchor => 'e', $color;
80 ctext_configure $c, $x+30, $y+30, -anchor => 'center', $color;
81 ctext_configure $c, $x+60, $y+30, -anchor => 'w', $color;
82 ctext_configure $c, $x, $y+60, -anchor => 'ne', $color;
83 ctext_configure $c, $x+30, $y+60, -anchor => 'n', $color;
84 ctext_configure $c, $x+60, $y+60, -anchor => 'nw', $color;
85 my $item = $c->create('rectangle', $x+40, $y+40, $x+50, $y+50,
86 qw/-outline black -fill red/);
87 $c->bind($item, '<1>' => sub {
88 shift->itemconfigure(qw/text -anchor center/);
89 });
90 $c->create('text', $x+45, $y-5, -text => 'Text Position', qw/-anchor s
91 -font -*-times-medium-r-normal--*-240-*-*-*-*-*-*
92 -fill brown/);
93
94 # Lastly, create some items that allow the text's justification
95 # to be changed.
96
97 $x = 350; $y = 50; $color = 'SeaGreen2';
98 ctext_configure $c, $x, $y, -justify => 'left', $color;
99 ctext_configure $c, $x+30, $y, -justify => 'center', $color;
100 ctext_configure $c, $x+60, $y, -justify => 'right', $color;
101 $c->create('text', $x+45, $y-5, qw/-text Justification -anchor s
102 -font -*-times-medium-r-normal--*-240-*-*-*-*-*-*
103 -fill brown/);
104
105 my $config_fill = '';
106 $c->bind(qw/config <Enter>/ => [\&ctext_enter, \$config_fill]);
107 $c->bind(qw/config <Leave>/ =>
108 sub {$c->itemconfigure('current', -fill => $config_fill)}
109 );
110
111} # end ctext
112
113sub ctext_bs {
114
115 my($c) = @_;
116
117 eval {local $SIG{__DIE__}; $c->dchars(qw/text sel.first sel.last/)};
118 my $char = $c->index(qw/text insert/) - 1;
119 $c->dchars('text', $char) if $char >= 0;
120
121} # end ctext_bs
122
123sub ctext_configure {
124
125 my($w, $x, $y, $option, $value, $color) = @_;
126
127 my $item = $w->create('rectangle', $x, $y, $x+30, $y+30,
128 -outline => 'black', -fill => $color, -width => 1);
129 $w->bind($item, '<1>',
130 sub {$w->itemconfigure('text', $option => $value)}
131 );
132 $w->addtag(qw/config withtag/, $item);
133
134} # end ctext_configure
135
136sub ctext_enter {
137
138 my($w, $config_fill) = @_;
139
140 $$config_fill = ($w->itemconfigure('current', -fill))[4];
141 $w->itemconfigure(qw/current -fill black/);
142
143} # end ctext_enter
144
145sub ctext_move {
146
147 my($w) = @_;
148 my $e = $w->XEvent;
149
150 my($x, $y) = ($e->x, $e->y);
151 $w->select(qw/to current/, "\@$x,$y");
152
153} # end ctext_move
154
155sub ctext_press {
156
157 my($w) = @_;
158 my $e = $w->XEvent;
159
160 my($x, $y) = ($e->x, $e->y);
161 $w->icursor('current', "\@$x,$y");
162 $w->focus('current');
163 $w->CanvasFocus;
164 $w->select(qw/from current/, "\@$x,$y");
165
166} # end ctext_press
167
1681;
169
170