#!/usr/bin/perl -w use strict; package Goo::Graph; use Gtk2; use Glib qw/TRUE FALSE/; use base 'Goo::Canvas'; use Gtk2::Gdk::Keysyms; 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->signal_connect ('expose_event' => \&on_expose_event); # $window->signal_connect(event_after => \&event_after); return $self; } sub on_expose_event(){ my ($self,$event) = @_; $self->can_focus(TRUE); $self->grab_focus($self->{'root'}); $self->signal_connect('button-press-event', \&on_can_button_press); $self->signal_connect_after('key_press_event', \&on_key_press ); return FALSE; } 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 = " 'W' "; $pangolayout->set_markup( $markup ); my ($lw,$lh) = $pangolayout->get_pixel_size(); $self->{'lwidth'} = $lw; $self->{'lheight'} = $lh; print $self->{'lheight'},"\n"; $markup = " Goo "; 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; #$graph->can_focus(1); #$graph->grab_focus($graph->get_root_item()); Gtk2->main; __END__