Event Handling

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Event Handling

James Muir-2
Hello All,

I'm having some success with gtk2-perl. I'm liking how this software
works. :-)

I have a question about events. I have some sample code that I took from
one of the examples that hooks up an event handler to a
Gnome2::Canvas::Rect as follows:

$rect->signal_connect (event=>&rectevent);

and then later there's the handler code:

sub rectevent
{
    warn("event: " . $event->type . "/n");
}

Nice, but this handler will receive all events. Is there a way to
indicate to Gnome that my program is only interested in, say,  
"button-press"  events and to call my handler
only for "button-press" events. Or do I have to handle all possible
events in my handler?

-James

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Event Handling

Boštjan Špetič
> I have a question about events. I have some sample code that I took from
> one of the examples that hooks up an event handler to a
> Gnome2::Canvas::Rect as follows:
>
> $rect->signal_connect (event=>&rectevent);
>
> and then later there's the handler code:
>
> sub rectevent
> {
>     warn("event: " . $event->type . "/n");
> }
>
> Nice, but this handler will receive all events. Is there a way to
> indicate to Gnome that my program is only interested in, say,
> "button-press"  events and to call my handler
> only for "button-press" events. Or do I have to handle all possible
> events in my handler?

$rect->signal_connect('button-press' => \&rectevent);

regards, bostjan

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Event Handling

muppet-6
In reply to this post by James Muir-2

James Muir said:
> Nice, but this handler will receive all events. Is there a way to
> indicate to Gnome that my program is only interested in, say,
> "button-press"  events and to call my handler
> only for "button-press" events. Or do I have to handle all possible
> events in my handler?

Unfortunately, no.  GnomeCanvas does its own event routing, and
GnomeCanvasItem gets a small subset of the events that GtkWidget gets.  You
get to dispatch based on $event->type in your event handler.

Something evil i have done in the past is create a table of these things:


  %foo_event_dispatch = (
      'button-press-event' => \&foo_button_press_event,
      'button-release-event' => \&foo_button_release_event,
  );
  sub foo_event {
     my ($item, $event) = @_;
     my $handler = $foo_event_dispatch{$event->type};
     return $handler
          ? $foo_event_dispatch{$event->type}->($item, $event)
          : FALSE;
  }

But depending on how much code you have in each one, that may be more work
than it's worth.

--
muppet <scott at asofyet dot org>

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Event Handling

Jan Hudec
In reply to this post by Boštjan Špetič
On Thu, Aug 04, 2005 at 21:22:52 +0200, Boštjan Špetič wrote:

> >I have a question about events. I have some sample code that I took from
> >one of the examples that hooks up an event handler to a
> >Gnome2::Canvas::Rect as follows:
> >
> >$rect->signal_connect (event=>&rectevent);
> >
> >and then later there's the handler code:
> >
> >sub rectevent
> >{
> >    warn("event: " . $event->type . "/n");
> >}
> >
> >Nice, but this handler will receive all events. Is there a way to
> >indicate to Gnome that my program is only interested in, say,
> >"button-press"  events and to call my handler
> >only for "button-press" events. Or do I have to handle all possible
> >events in my handler?
>
> $rect->signal_connect('button-press' => \&rectevent);
Unfortunately Gnome2::Canvas::Rect is *NOT* a widget. It does *not* have
button-press signal.

-------------------------------------------------------------------------------
                                                 Jan 'Bulb' Hudec <[hidden email]>

_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Event Handling

James Muir-2
In reply to this post by muppet-6
muppet wrote:

>James Muir said:
>  
>
>>Nice, but this handler will receive all events. Is there a way to
>>indicate to Gnome that my program is only interested in, say,
>>"button-press"  events and to call my handler
>>only for "button-press" events. Or do I have to handle all possible
>>events in my handler?
>>    
>>
>
>Unfortunately, no.  GnomeCanvas does its own event routing, and
>GnomeCanvasItem gets a small subset of the events that GtkWidget gets.  You
>get to dispatch based on $event->type in your event handler.
>
>Something evil i have done in the past is create a table of these things:
>
>
>  %foo_event_dispatch = (
>      'button-press-event' => \&foo_button_press_event,
>      'button-release-event' => \&foo_button_release_event,
>  );
>  sub foo_event {
>     my ($item, $event) = @_;
>     my $handler = $foo_event_dispatch{$event->type};
>     return $handler
>          ? $foo_event_dispatch{$event->type}->($item, $event)
>          : FALSE;
>  }
>
>But depending on how much code you have in each one, that may be more work
>than it's worth.
>
>  
>
Thanks for the explanation.
_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Event Handling

Aristotle Pagaltzis
In reply to this post by muppet-6
* muppet <[hidden email]> [2005-08-04 22:35]:
>   sub foo_event {
>      my ($item, $event) = @_;
>      my $handler = $foo_event_dispatch{$event->type};
>      return $handler
>           ? $foo_event_dispatch{$event->type}->($item, $event)
>           : FALSE;
>   }

    sub foo_event {
       my ($item, $event) = @_;
       my $handler = $foo_event_dispatch{$event->type};
       return $handler ? $handler->($item, $event) : FALSE;
    }

Or even cutely hacked:

    sub foo_event {
       my ($item, $event) = @_;
       ($foo_event_dispatch{$event->type} || return FALSE)->($item, $event);
    }

Or taking advantage of the fact that you can `goto` a coderef:

    sub foo_event {
       my ($item, $event) = @_;
       goto &{ $foo_event_dispatch{$event->type} || return FALSE };
    }

Err, okay, I’ll stop now.

Regards,
--
#Aristotle
*AUTOLOAD=*_=sub{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1};
&Just->another->Perl->hacker;
_______________________________________________
gtk-perl-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list