#!/usr/bin/perl -w
use strict;

package Goo::Graph;

use Gtk2;
use Glib qw/TRUE FALSE/;
use base 'Goo::Canvas';


sub new {
   my $class  = shift;
   my %params = ( @_ );

    my $self = bless Goo::Canvas->new(), $class;

    # size settings
    $self->set_size_request($params{'width'},$params{'height'} );
    $self->set_bounds(0, 0, $params{'xbound'},$params{'ybound'});
    $self->{'root'} = $self->get_root_item();
    $self->{'scale'} = 1;
  $self->set_color( %params );
  $self->set_fonts( %params );


  $self->set_events([ 'all-events-mask' ]);
  $self->add_events( [ qw(key_press_mask) ]);
$self->add_events([   
'exposure-mask',                                           
'pointer-motion-mask',                               
'pointer-motion-hint-mask',                     
'button-motion-mask',                                 
'button1-motion-mask',                              
'button2-motion-mask',                              
'button3-motion-mask',                              
'button-press-mask',                                   
'button-release-mask',                              
'key-press-mask',                                         
'key-release-mask',                                    
'enter-notify-mask',                                  
'leave-notify-mask',                                  
'focus-change-mask',                                  
'structure-mask',                                        
'property-change-mask',        
'visibility-notify-mask',                        
'proximity-in-mask',                                  
'proximity-out-mask',                                
'substructure-mask',                                  
'scroll-mask',                                              
'all-events-mask']);         
   
   
   $self->signal_connect('button-press-event', \&on_can_button_press);
   $self->can_focus(TRUE);
   $self->grab_focus($self->{'root'});
   $self->signal_connect_after('key_press_event', \&on_key_press );


   $self->signal_connect ('key_press_event' => sub {
		my ($widget, $event) = @_;
		print "@_\n";
		});

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


   return $self;
}

sub set_color {
   my $self   = shift;
   my %params = @_;

    $self->{'bg'} = Gtk2::Gdk::Color->new(  @{$params{'bg'}} );
    $self->modify_base('normal', $self->{'bg'});
    $self->{'black'} = Gtk2::Gdk::Color->new (0x0000,0x0000,0x0000);
    $self->{'white'} = Gtk2::Gdk::Color->new (0xFFFF,0xFFFF,0xFFFF);

}

sub set_fonts {
   my $self   = shift;
   my %params = @_;

   # get font size for height spacing
   my $font  = $params{'font'};
   my $font_desc = Gtk2::Pango::FontDescription->from_string($font);
   my $pangolayout = $self->create_pango_layout("");
   $pangolayout->set_font_description($font_desc);

   my $markup = "<span  font_family ='Arial' foreground = '#000000' 
       size = '14000' weight = 'bold'> 'W' </span>";
   $pangolayout->set_markup( $markup );

   my ($lw,$lh) = $pangolayout->get_pixel_size();
  
   $self->{'lwidth'} = $lw; 
   $self->{'lheight'} = $lh;
  
   print $self->{'lheight'},"\n";


   $markup = "<span font_family ='Arial '
         foreground = '#000000'  
         size = '14000' 
         weight = 'bold'> Goo </span>";

   my $text = Goo::Canvas::Text->new(
    $self->{'root'}, 
    $markup,
    100 , 100 , -1, 
    'center',
    'use markup' => 1,
   );

   my $y = 100 + $self->{'lheight'};

   $text = Goo::Canvas::Text->new(
    $self->{'root'}, 
    $markup,
    100 , $y , -1, 
    'center',
    'use markup' => 1,
   );


return 1;
}


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 ( $self, $event ) = @_;
    # print $event->type,"\n";

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

    return 0;
}

1;

package main;

use Gtk2 -init;

my $window = Gtk2::Window->new;
$window->set_title( 'GooGraph' );
$window->set_border_width( 6 );
$window->signal_connect( delete_event => sub { Gtk2->main_quit; 1 } );
$window->set_size_request(640, 600);

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

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

my $graph = Goo::Graph->new(
   width  => 1000,
   height => 1000,
   xbound => 5000,
   ybound => 5000,
   bg     => [0xee22, 0xee22, 0xff22],
   font   => 'Arial Bold 14',
);

$swin->add($graph);

$vbox->pack_start( $swin, 1, 1, 0 );

$window->show_all;
Gtk2->main;

__END__
