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

my $scale = 1;

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

my $swin = Gtk2::ScrolledWindow->new;
$swin->set_shadow_type('in');
$window->add($swin);

my ($cwidth,$cheight)= (2400,2400);
my ($cx,$cy) = ($cwidth/2,$cheight/2);
my $canvas = Goo::Canvas->new();

my ($cw,$ch) = (600,400);
$canvas->set_size_request($cw, $ch);

$canvas->set_bounds(0,0, $cwidth, $cheight);
#$canvas->set_bounds( 0, 0,2*$cwidth,2* $cheight);
my $black = Gtk2::Gdk::Color->new (0x0000,0x0000,0x0000);
my $white = Gtk2::Gdk::Color->new (0xFFFF,0xFFFF,0xFFFF);
$canvas->modify_base('normal',$white );
$swin->add($canvas);

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

my $ha1 = $swin->get_hadjustment;
my $va1 = $swin->get_vadjustment;

$canvas->scroll_to( $cx - $cw/(2*$scale) ,$cy - $ch/(2*$scale) );

my @points1 = (0,$cy,$cwidth,$cy);
my $poly1 = Goo::Canvas::Polyline->new(
        $root, FALSE,
         \@points1, 
        'stroke-color' => 'black',
        'line-width' => 1,
       );

my @points2 = ($cx,0,$cx,$cheight);
my $poly2 = Goo::Canvas::Polyline->new(
        $root, FALSE,
         \@points2, 
        'stroke-color' => 'black',
        'line-width' => 1,
       );


$canvas->signal_connect('button-press-event',
                      \&on_can_button_press);


$canvas->signal_connect_after('key_press_event', \&on_key_press);
$canvas->can_focus(TRUE);
$canvas->grab_focus($root);

$window->signal_connect(event_after => \&event_after); 

$window->show_all();

Gtk2->main;

sub on_can_button_press {
     #print "@_\n";
     my ($widget, $event ) = @_;
     #print $widget ,' ',$event->type, '  ','button',' ',$event->button,"\n";
     my ($x,$y) = ($event->x,$event->y);
      print "$x  $y\n";
    return 0;
}


sub on_key_press {
    # print "@_\n";
     my ( $canvas, $event ) = @_;
    # print $event->type,"\n";

    if ( $event->keyval == $Gtk2::Gdk::Keysyms{Z} ) {
        $scale += .1;
        $canvas->set_scale($scale);
        print "$scale\n";
    }

    if ( $event->keyval == $Gtk2::Gdk::Keysyms{z} ) {
        $scale -= .1;
        $canvas->set_scale($scale);
        }

    if ( $event->keyval == $Gtk2::Gdk::Keysyms{s} ) {
        write_pdf($canvas);
    }

    if ( $event->keyval == $Gtk2::Gdk::Keysyms{c} ) {
    
        $canvas->scroll_to( $cx - $cw/(2*$scale) , $cy - $ch/(2*$scale) );
 }    

#    print "key was ", chr( $event->keyval ), "\n";

    return 0;
}


sub write_pdf {
    #print "@_\n";
    my $canvas = shift;
    print "Write PDF...\n";

    my $scale = $canvas->get_scale;
    print "scale->$scale\n"; 
    
    my $surface = Cairo::PdfSurface->create("$0-$scale.pdf", 
                    $scale*$cwidth, $scale*$cheight);
    
    my $cr = Cairo::Context->create($surface);

    # needed to save scaled version
    $cr->scale($scale, $scale);

    $canvas->render($cr, undef, 1);
    $cr->show_page;
    print "done\n";
    return TRUE;
}

sub event_after {
  my ($mw, $event) = @_;
  #  print "\t",'canvas ',$event->type,"\n";
  
  # get new canvas size after a window resize
  # needed to center scroll region exactly
  my $rect = $canvas->allocation;
  $cw = $rect->width;
  $ch = $rect->height;
 # print "$cw $ch\n"; 
  return FALSE;
}

__END__
