#!/usr/bin/perl
use Gtk2 '-init';
use Gnome2::Canvas;
my($w_top, $w_canvas);
sub create_widgets {
$w_top = Gtk2::Window->new;
$w_top->signal_connect(destroy=> sub { exit });
my $vbox = Gtk2::VBox->new(0,0);
$w_top->add($vbox);
$w_canvas = Gnome2::Canvas->new_aa();
$vbox->pack_start($w_canvas, 1,1,0);
$w_canvas->set_size_request(300,300);
$w_canvas->set_scroll_region (0, 0, 300, 300);
my $quit = Gtk2::Button->new("Quit");
$quit->signal_connect(clicked=> sub { exit });
$vbox->pack_start($quit, 0, 0, 0);
$w_top->show_all();
}
# Callback for moving items on the canvas
my ($dragging, $last_x, $last_y); # item_move static data
sub item_move {
my($item, $event) = @_;
if ($event->type eq "button-press") {
$item->raise_to_top();
$last_x = $event->x;
$last_y = $event->y;
$dragging=1;
}
elsif ($event->type eq "motion-notify") {
if ($dragging) {
my $new_x = $event->x;
my $new_y = $event->y;
$item->move($new_x - $last_x,$new_y-$last_y);
$last_x = $new_x;
$last_y = $new_y;
}
}
elsif ($event->type eq "button-release") {
$dragging = 0;
}
}
sub place_objects_on_canvas {
my $root = $w_canvas->root();
for my $p ([50,50, "green"],
[50,250, "orange"],
[250,50, "yellow"],
[250,250, "blue"]) {
my($x,$y,$color) = @$p;
# Put a circle on the graph
my $item = Gnome2::Canvas::Item->new(
$root, "Gnome2::Canvas::Ellipse",
x1=> $x-16,
y1=> $y-16,
x2=> $x+16,
y2=> $y+16,
fill_color => $color,
outline_color=>"black");
# Make it movable
$item->signal_connect("event", \&item_move);
}
}
create_widgets();
place_objects_on_canvas();
Gtk2->main(); |