use subs qw
/twind_create_plot twind_delete_plot twind_restore_bg/;
# Create a top-level window with a text widget that demonstrates the
# use of embedded windows in Text widgets.
-title
=> 'Text Demonstration - Embedded Windows',
# By default, when you create a Scrolled instance of a Perl/Tk widget
# the scrollbars are always displayed; that is, they are required. But
# you can have optional scrollbars as well, specified via the -scrollbars
# specifier. So, assume scrollbars can be postioned 'nsew' (north, south
# east or west), or 'se' for southeast, etcetera. You specify 'required'
# or 'optional' using an 'r' or 'o' character, respectively, preceeding
# the scrollbar position. So the following Scrolled widget has an
# optional scrollbar at the bottom of the text widget and a required
# scrollbar positioned to the right.
# Optional scrollbars are only displayed if they are required, so, the
# the southern scrollbar is displayed IFF -wrap => none.
my $t = $TOP->Scrolled(qw
/Text
-setgrid true
-width
70 -height
35
-wrap word
-highlightthickness
0 -borderwidth
0 -scrollbars osre
$t->tag(qw
/configure center -justify center -spacing1 5m -spacing3 5m/);
$t->tag(qw
/configure buttons
-lmargin1
1c
-lmargin2
1c
-rmargin
1c
-spacing1
3m
-spacing2
0 -spacing3
0/);
-command
=> [$t => qw
/configure -wrap none/],
-cursor
=> 'top_left_arrow',
-command
=> [$t => qw
/configure -wrap word/],
-cursor
=> 'top_left_arrow',
my $t_click = $t->Button(
-command
=> [\
&twind_create_plot
, $t],
-cursor
=> 'top_left_arrow',
my $t_delete = $t->Button(
-command
=> [\
&twind_delete_plot
, $t],
-cursor
=> 'top_left_arrow',
$t->insert('end', "A text widget can contain other widgets embedded ");
$t->insert('end', "in it. These are called ");
$t->insert('end', "\"embedded windows\"");
$t->insert('end', ", and they can consist of arbitrary widgets. ");
$t->insert('end', "For example, here are two embedded button ");
$t->insert('end', "widgets. You can click on the first button to ");
$t->window('create', 'end', -window
=> $t_on);
$t->insert('end', " horizontal scrolling, which also turns off ");
$t->insert('end', "word wrapping. Or, you can click on the second ");
$t->insert('end', "button to\n");
$t->window('create', 'end', -window
=> $t_off);
$t->insert('end', " horizontal scrolling and turn back on word ");
$t->insert('end', "wrapping.\n\n");
$t->insert('end', "Or, here is another example. If you ");
$t->window('create', 'end', -window
=> $t_click);
$t->insert('end', " a canvas displaying an x-y plot will appear ");
$t->insert('end', "right here.");
$t->mark('set', 'plot', 'insert');
$t->mark('gravity', 'plot', 'left');
$t->insert('end', " You can drag the data points around with the ");
$t->insert('end', "mouse, or you can click here to ");
$t->window('create', 'end', -window
=> $t_delete);
$t->insert('end', " the plot again.\n\n");
$t->insert('end', "You may also find it useful to put embedded windows");
$t->insert('end', " in a text without any actual text. In this case ");
$t->insert('end', "the text widget acts like a geometry manager. For ");
$t->insert('end', "example, here is a collection of buttons laid out ");
$t->insert('end', "neatly into rows by the text widget. These buttons");
$t->insert('end', " can be used to change the background color of the ");
$t->insert('end', "text widget (\"Default\" restores the color to ");
$t->insert('end', "its default). If you click on the button labeled ");
$t->insert('end', "\"Short\", it changes to a longer string so that ");
$t->insert('end', "you can see how the text widget automatically ");
$t->insert('end', "changes the layout. Click on the button again ");
$t->insert('end', "to restore the short string.\n");
my $t_default = $t->Button(
-command
=> [\
&twind_restore_bg
, $t],
-cursor
=> 'top_left_arrow',
$t->window('create', 'end', -window
=> $t_default, -padx
=> 3);
my $t_toggle = $t->Checkbutton(
-textvariable
=> \
$embToggle,
-variable
=> \
$embToggle,
-onvalue
=> 'A much longer string',
-cursor
=> 'top_left_arrow',
$t->window('create', 'end', -window
=> $t_toggle,
my($i, $color) = (1, '');
foreach $color (qw(AntiqueWhite3 Bisque1 Bisque2 Bisque3 Bisque4
SlateBlue3 RoyalBlue1 SteelBlue2 DeepSkyBlue3
LightBlue1 DarkSlateGray1 Aquamarine2 DarkSeaGreen2
SeaGreen1 Yellow1 IndianRed1 IndianRed2 Tan1 Tan4)) {
-cursor
=> 'top_left_arrow',
$col->configure(-command
=> sub {
$t->configure(-background
=> $color);
$t->window('create', 'end', -window
=> $col,
$t->tag('add', 'buttons', $t_default, 'end');
# We are required to create a new Plot object everytime since embedded
# widgets are destroyed when their tag is deleted. (Too bad.)
if (not Exists
($twind::plot
)) {
$twind::plot
= $text->Plot(
-inactive_highlight
=> 'Skyblue2',
-active_highlight
=> 'red',
while ($text->get('plot') =~ / |\t|\n/) {
$text->insert('plot', "\n");
$text->window('create', 'plot', -window
=> $twind::plot
);
$text->tag('add', 'center', 'plot');
$text->insert('plot', "\n");
} # end twind_create_plot
if (Exists
($twind::plot
)) {
$text->delete($twind::plot
);
while ($text->get('plot') =~ / |\t|\n/) {
$text->insert('plot', ' ');
} # end twind_delete_plot
$text->configure(-background
=>
($text->Subwidget('text')->configure(-background
))[3]);