#!/usr/bin/perl
use warnings;
use strict;
use Goo::Canvas;
use Gtk2 '-init';
use Glib qw(TRUE FALSE);

my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_default_size(640, 600);

my $canvas = Goo::Canvas->new();

my $gray = Gtk2::Gdk::Color->parse('gray');	
$canvas->set( 
	'automatic-bounds' 		=> FALSE,
	'bounds-from-origin' 	=> FALSE,
	'background-color' 		=> sprintf( "#%04x%04x%04x", $gray->red, $gray->green, $gray->blue ), 
);

my $vbox = Gtk2::VBox->new;
$window->add($vbox);
$vbox->add($canvas);

my $root = $canvas->get_root_item();

for (my $i = 0; $i < 3; $i++) {
	boxed_text($root, $i, $i*50, $i*50, $i*50+50, $i*50+50);
	add_button($vbox, $i);
}

$window->show_all();

sub boxed_text {
	my ($root, $char, $x1, $y1, $x2, $y2) = @_;
	my $text = Goo::Canvas::Text->new($root, $char, $x1, $y1, 0, 'nw');

	# clicking text box produces a dialog to edit the text
	$text->signal_connect('button-press-event' => sub {
		my ($widget, $target, $ev) = @_;
		
		print "text $widget button-press-event\n", $widget->get('text'), "\n";
		my $dialog = Gtk2::Dialog -> new ("item: ".$widget->get('text'), $window,
			'modal',
			'gtk-ok' => 'ok',
			'gtk-cancel' => 'cancel');
		$dialog->set_default_response ('ok');
		$dialog->show_all;
		if ($dialog->run eq 'ok') {}
		$dialog->destroy;
		
		#ungrab the pointer, see
		#http://library.gnome.org/devel/goocanvas/unstable/GooCanvas.html#goo-canvas-pointer-ungrab
		$canvas->pointer_ungrab( $widget, $ev->time );
		
		return TRUE;
	});
}

sub add_button {
	my ($vbox, $label) = @_;
	my $button = Gtk2::Button->new($label);
	$vbox->add($button);
	$button->signal_connect('button-press-event' => sub {
		my ($widget, $target, $ev) = @_;
		print "text $widget button-press-event\n", $widget->get('label'), "\n";
		my $dialog = Gtk2::Dialog -> new ('Editing text...', $window,
			'modal',
			'gtk-ok' => 'ok',
			'gtk-cancel' => 'cancel'
		);
		$dialog->set_default_response ('ok');
		$dialog->show_all;
		if ($dialog->run eq 'ok') {}
		$dialog->destroy;
		return TRUE;
	});
}

Gtk2->main;
