#!/usr/bin/perl
package canvas;
use strict;
use diagnostics;
use warnings;
use Gtk2 '-init';
use Glib qw(TRUE FALSE);
use Gnome2::Canvas;
# Canvas size doesn't matter that much, as long as it's a bit bigger than the window
my $size = 600;
my $flipFlag = FALSE;
# Create the window
my $window = Gtk2::Window->new();
$window->set_title('Canvas');
$window->set_position('center');
$window->set_default_size($size, $size);
$window->set_border_width(5);
$window->signal_connect (destroy => sub { Gtk2->main_quit; });
# Add a VBox containing a Gnome2::Canvas
my $vBox = Gtk2::VBox->new(FALSE, 0);
my $canvasFrame = Gtk2::Frame->new(undef);
$canvasFrame->set_border_width(3);
my $canvasScroller = Gtk2::ScrolledWindow->new();
$canvasScroller->set_border_width(3);
$canvasScroller->set_policy('always','always');
my $canvas = Gnome2::Canvas->new();
$canvas->set_scroll_region(0, 0, $size, $size);
$canvas->set_center_scroll_region(1);
$canvas->set_pixels_per_unit(1);
my $canvasRoot = $canvas->root();
$canvasScroller->add($canvas);
$canvasFrame->add($canvasScroller);
$vBox->pack_start($canvasFrame, TRUE, TRUE, 0);
$window->add($vBox);
# Draw a white background
my $bg = Gnome2::Canvas::Item->new (
$canvasRoot,
'Gnome2::Canvas::Rect',
x1 => 0,
y1 => 0,
x2 => $size,
y2 => $size,
fill_color => '#FFFFFF',
outline_color => '#FFFFFF',
);
$bg->lower_to_bottom();
# Draw some squares above the background
for (my $count = 0; $count < 5; $count++) {
my ($square, $posn, $newColour);
$posn = ($count * 100);
$square = Gnome2::Canvas::Item->new(
$canvasRoot,
'Gnome2::Canvas::Rect',
x1 => ($posn + 10),
y1 => ($posn + 10),
x2 => ($posn + 60),
y2 => ($posn + 60),
outline_color => '#000000',
fill_color => '#FF0000',
);
$square->lower_to_bottom();
$square->raise(1);
# Click on a red square? Then destroy it, and replace it with a yellow one!
$square->signal_connect (event => sub {
my ($widget, $event) = @_;
my $replaceSquare;
if ($event->type eq 'button-press') {
$square->destroy();
$replaceSquare = Gnome2::Canvas::Item->new(
$canvasRoot,
'Gnome2::Canvas::Rect',
x1 => ($posn + 10),
y1 => ($posn + 10),
x2 => ($posn + 60),
y2 => ($posn + 60),
outline_color => '#000000',
fill_color => '#FFFF00',
);
$replaceSquare->lower_to_bottom();
$replaceSquare->raise(1);
}
});
}
# Draw some blue text above the squares
for (my $count = 0; $count < 5; $count++) {
my $text = Gnome2::Canvas::Item->new(
$canvasRoot,
'Gnome2::Canvas::Text',
x => (($count * 100) + 25),
y => (($count * 100) + 25),
fill_color => '#0000FF',
font => 'Sans',
size => 10000,
anchor => 'GTK_ANCHOR_W',
text => 'Hello world!',
);
$text->lower_to_bottom();
$text->raise(2);
}
# Open the window
$window->show_all();
Gtk2->main();