Re: keyboard events in subclassed Goo::Canvas



On Fri, 15 Aug 2008 14:50:49 -0400
zentara <zentara1 sbcglobal net> wrote:

Hi, I worked out keypress events on the Goo::Canvas, and shown in 
attached keypress.pl.

In attempting to put the Goo::Canvas into a package, the mouse
events come thru, but not the keyboard. 
As shown in keypress-packaged.pl 

Anyone know why, or how to make it work?

Never mind, I figured it out. I tried putting the focus and grab
after the window->show_all in package main, and it worked.

$window->show_all;
# this caused it to work from main package
#$graph->can_focus(1);                                                                    
#$graph->grab_focus($graph->get_root_item());   

So I figured I would put the can_focus and grab_focus in an
on_expose callback in the package, and it works.


#!/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);

   return $self;
}


sub on_expose_event(){

    my ($self,$event) = @_;

#put the keyboard grab stuff here

   $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 = "<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;

#$graph->can_focus(1);
#$graph->grab_focus($graph->get_root_item());

Gtk2->main;

__END__


zentara





-- 
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]