10
# Create a window containing a canvas displaying a text string and allowing the string to be edited and re-anchored.
12
$mkCanvText->destroy if Exists($mkCanvText);
13
$mkCanvText = $top->Toplevel();
16
$w->title('Canvas Text Demonstration');
19
my $w_msg = $w->Label(-font => '-Adobe-Times-Medium-R-Normal--*-180-*-*-*-*-*-*', -wraplength => '5i',
20
-justify => 'left', -text => 'This window displays a string of text to demonstrate the text ' .
21
'facilities of canvas widgets. You can point, click, and type. You can also select and then ' .
22
'delete with Control-d. You can copy the selection with Control-v. You can click in the boxes ' .
23
'to adjust the position of the text relative to its positioning point or change its justification.');
24
my $c = $w->Canvas(-relief => 'flat', -bd => 0, -width => '500', -height => '400');
25
my $w_ok = $w->Button(-text => 'OK', -width => 8, -command => ['destroy', $w]);
26
$w_msg->pack(-side => 'top', -fill => 'both');
27
$c->pack(-side => 'top', -expand => 'yes', -fill => 'both');
28
$w_ok->pack(-side => 'bottom', -pady => '5', -anchor => 'center');
30
$font = '-Adobe-helvetica-medium-r-normal--*-240-*-*-*-*-*-*';
32
$c->create(qw(rectangle 245 195 255 205 -outline black -fill red));
34
# First, create the text item and give it bindings so it can be edited.
36
$c->addtag('text', 'withtag', $c->create('text', 250, 200, -text => 'This is just a string of text to demonstrate the ' .
37
'text facilities of canvas widgets. You can point, click, and type. You can ' .
38
'also select and then delete with Control-d.', -width => 440, -anchor => 'n',
39
-font => $font, -justify => 'left'));
40
$c->bind('text', '<1>' => sub {textB1Press(@_)});
41
$c->bind('text', '<B1-Motion>' => sub {textB1Move(@_)});
42
$c->bind('text', '<Shift-1>' => sub {
45
my($x, $y) = ($e->x, $e->y);
46
$c->select('adjust', 'current', "\@$x,$y");
48
$c->bind('text', '<Shift-B1-Motion>' => sub {textB1Move(@_)});
49
$c->bind('text', '<KeyPress>' => sub {
53
$c->insert('text', 'insert', "$A");
55
$c->bind('text', '<Shift-KeyPress>' => sub {
59
$c->insert('text', 'insert', "$A");
61
$c->bind('text', '<Return>' => sub {
64
$c->insert('text', 'insert', "\\n");
66
$c->bind('text', '<Control-h>' => sub {textBs(@_)});
67
$c->bind('text', '<Delete>' => sub {textBs(@_)});
68
$c->bind('text', '<Control-d>' => sub {
71
$c->dchars('text', 'sel.first', 'sel.last');
73
$c->bind('text', '<Control-v>' => sub {
76
$c->insert('text', 'insert', Tk::selection('get'));
79
# Next, create some items that allow the text's anchor position to be edited.
81
my($x, $y, $color) = (50, 50, 'LightSkyBlue1');
82
mkTextConfig $c, $x, $y, -anchor => 'se', $color;
83
mkTextConfig $c, $x+30, $y, -anchor => 's', $color;
84
mkTextConfig $c, $x+60, $y, -anchor => 'sw', $color;
85
mkTextConfig $c, $x, $y+30, -anchor => 'e', $color;
86
mkTextConfig $c, $x+30, $y+30, -anchor => 'center', $color;
87
mkTextConfig $c, $x+60, $y+30, -anchor => 'w', $color;
88
mkTextConfig $c, $x, $y+60, -anchor => 'ne', $color;
89
mkTextConfig $c, $x+30, $y+60, -anchor => 'n', $color;
90
mkTextConfig $c, $x+60, $y+60, -anchor => 'nw', $color;
91
my $item = $c->create('rectangle', $x+40, $y+40, $x+50, $y+50, -outline => 'black', -fill => 'red');
92
$c->bind($item, '<1>' => sub {
95
$c->itemconfigure('text', -anchor => 'center');
97
$c->create('text', $x+45, $y-5, -text => 'Text Position', -anchor => 's',
98
-font => '-Adobe-times-medium-r-normal--*-240-*-*-*-*-*-*', -fill => 'brown');
100
# Lastly, create some items that allow the text's justification to be changed.
102
$x = 350; $y = 50; $color = 'SeaGreen2';
103
mkTextConfig $c, $x, $y, -justify => 'left', $color;
104
mkTextConfig $c, $x+30, $y, -justify => 'center', $color;
105
mkTextConfig $c, $x+60, $y, -justify => 'right', $color;
106
$c->create('text', $x+45, $y-5, -text => 'Justification', -anchor => 's',
107
-font => '-Adobe-times-medium-r-normal--*-240-*-*-*-*-*-*', -fill => 'brown');
109
$c->bind('config', '<Enter>' => sub {textEnter(@_)});
110
$c->bind('config', '<Leave>' => sub {
113
$c->itemconfigure('current', -fill => $mkCanvText::textConfigFill);
121
my($w, $x, $y, $option, $value, $color) = @_;
123
my $item = $w->create('rectangle', $x, $y, $x+30, $y+30, -outline => 'black', -fill => $color, -width => 1);
124
$w->bind($item, '<1>', [sub {
125
my($w, $option, $value, $e) = @_;
128
$w->itemconfigure('text', $option => $value);
129
}, $option, $value]);
130
$w->addtag('config', 'withtag', $item);
134
$mkCanvText::textConfigFill = 'purple';
141
$mkCanvText::textConfigFill = ($w->itemconfigure('current', -fill))[4];
142
$w->itemconfigure('current', -fill => 'black');
152
my($x, $y) = ($e->x, $e->y);
153
$w->icursor('current', "\@$x,$y");
154
$w->focus('current');
156
$w->select('from', 'current', "\@$x,$y");
166
my($x, $y) = ($e->x, $e->y);
167
$w->select('to', 'current', "\@$x,$y");
177
my $char = $w->index('text', 'insert') - 1;
178
$w->dchar('text', $char) if $char >= 0;