# Create a window containing a canvas displaying a text string and allowing the string to be edited and re-anchored.
$mkCanvText->destroy if Exists
($mkCanvText);
$mkCanvText = $top->Toplevel();
$w->title('Canvas Text Demonstration');
my $w_msg = $w->Label(-font
=> '-Adobe-Times-Medium-R-Normal--*-180-*-*-*-*-*-*', -wraplength
=> '5i',
-justify
=> 'left', -text
=> 'This window displays a string of text to demonstrate the text ' .
'facilities of canvas widgets. You can point, click, and type. You can also select and then ' .
'delete with Control-d. You can copy the selection with Control-v. You can click in the boxes ' .
'to adjust the position of the text relative to its positioning point or change its justification.');
my $c = $w->Canvas(-relief
=> 'flat', -bd
=> 0, -width
=> '500', -height
=> '400');
my $w_ok = $w->Button(-text
=> 'OK', -width
=> 8, -command
=> ['destroy', $w]);
$w_msg->pack(-side
=> 'top', -fill
=> 'both');
$c->pack(-side
=> 'top', -expand
=> 'yes', -fill
=> 'both');
$w_ok->pack(-side
=> 'bottom', -pady
=> '5', -anchor
=> 'center');
$font = '-Adobe-helvetica-medium-r-normal--*-240-*-*-*-*-*-*';
$c->create(qw(rectangle 245 195 255 205 -outline black -fill red));
# First, create the text item and give it bindings so it can be edited.
$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. You can point, click, and type. You can ' .
'also select and then delete with Control-d.', -width
=> 440, -anchor
=> 'n',
-font
=> $font, -justify
=> 'left'));
$c->bind('text', '<1>' => sub {textB1Press
(@_)});
$c->bind('text', '<B1-Motion>' => sub {textB1Move
(@_)});
$c->bind('text', '<Shift-1>' => sub {
my($x, $y) = ($e->x, $e->y);
$c->select('adjust', 'current', "\@$x,$y");
$c->bind('text', '<Shift-B1-Motion>' => sub {textB1Move
(@_)});
$c->bind('text', '<KeyPress>' => sub {
$c->insert('text', 'insert', "$A");
$c->bind('text', '<Shift-KeyPress>' => sub {
$c->insert('text', 'insert', "$A");
$c->bind('text', '<Return>' => sub {
$c->insert('text', 'insert', "\\n");
$c->bind('text', '<Control-h>' => sub {textBs
(@_)});
$c->bind('text', '<Delete>' => sub {textBs
(@_)});
$c->bind('text', '<Control-d>' => sub {
$c->dchars('text', 'sel.first', 'sel.last');
$c->bind('text', '<Control-v>' => sub {
$c->insert('text', 'insert', Tk
::selection
('get'));
# Next, create some items that allow the text's anchor position to be edited.
my($x, $y, $color) = (50, 50, 'LightSkyBlue1');
mkTextConfig
$c, $x, $y, -anchor
=> 'se', $color;
mkTextConfig
$c, $x+30, $y, -anchor
=> 's', $color;
mkTextConfig
$c, $x+60, $y, -anchor
=> 'sw', $color;
mkTextConfig
$c, $x, $y+30, -anchor
=> 'e', $color;
mkTextConfig
$c, $x+30, $y+30, -anchor
=> 'center', $color;
mkTextConfig
$c, $x+60, $y+30, -anchor
=> 'w', $color;
mkTextConfig
$c, $x, $y+60, -anchor
=> 'ne', $color;
mkTextConfig
$c, $x+30, $y+60, -anchor
=> 'n', $color;
mkTextConfig
$c, $x+60, $y+60, -anchor
=> 'nw', $color;
my $item = $c->create('rectangle', $x+40, $y+40, $x+50, $y+50, -outline
=> 'black', -fill
=> 'red');
$c->bind($item, '<1>' => sub {
$c->itemconfigure('text', -anchor
=> 'center');
$c->create('text', $x+45, $y-5, -text
=> 'Text Position', -anchor
=> 's',
-font
=> '-Adobe-times-medium-r-normal--*-240-*-*-*-*-*-*', -fill
=> 'brown');
# Lastly, create some items that allow the text's justification to be changed.
$x = 350; $y = 50; $color = 'SeaGreen2';
mkTextConfig
$c, $x, $y, -justify
=> 'left', $color;
mkTextConfig
$c, $x+30, $y, -justify
=> 'center', $color;
mkTextConfig
$c, $x+60, $y, -justify
=> 'right', $color;
$c->create('text', $x+45, $y-5, -text
=> 'Justification', -anchor
=> 's',
-font
=> '-Adobe-times-medium-r-normal--*-240-*-*-*-*-*-*', -fill
=> 'brown');
$c->bind('config', '<Enter>' => sub {textEnter
(@_)});
$c->bind('config', '<Leave>' => sub {
$c->itemconfigure('current', -fill
=> $mkCanvText::textConfigFill
);
my($w, $x, $y, $option, $value, $color) = @_;
my $item = $w->create('rectangle', $x, $y, $x+30, $y+30, -outline
=> 'black', -fill
=> $color, -width
=> 1);
$w->bind($item, '<1>', [sub {
my($w, $option, $value, $e) = @_;
$w->itemconfigure('text', $option => $value);
$w->addtag('config', 'withtag', $item);
$mkCanvText::textConfigFill
= 'purple';
$mkCanvText::textConfigFill
= ($w->itemconfigure('current', -fill
))[4];
$w->itemconfigure('current', -fill
=> 'black');
my($x, $y) = ($e->x, $e->y);
$w->icursor('current', "\@$x,$y");
$w->select('from', 'current', "\@$x,$y");
my($x, $y) = ($e->x, $e->y);
$w->select('to', 'current', "\@$x,$y");
my $char = $w->index('text', 'insert') - 1;
$w->dchar('text', $char) if $char >= 0;