Re: Gtk::Widget->set_events( ) example won't work [BUG and WORKAROUND]



I may have arrived at the correct answer, but I'm not sure of enough of
the inner workings of the event mechanism to be sure I'm not breaking
something else.

At this point this appears to be a BUG in the Gtk::Widget::set_events()
code.  I suggest a cumbersome workaround.

On Wed, 2002-05-29 at 11:33, Ed Halley wrote:
I'm having trouble with some lines which appear in the Gtk-Perl tutorial
at http://personal.riverusers.com/~swilhelm/gtkperl-tutorial/pixmap.html
[...]
The tutorial states,
[...]
      $window->set_events( [ $window->get_events(),
                           'button_press_mask' ] );
      $window->signal_connect( "button_press_event",
                               sub { Gtk->exit( 0 ); } );

The return from Gtk::Widget::get_events() appears to be a hash.  If no
special events had been set before, it returns an EMPTY hash.

Meanwhile, the Gtk::Widget::set_events() can take a list of elements and
deal with them by mixed type.  The elements should be either strings or
hashes retrieved from get_events().  Thus, the example rightly attempts
to combine the old event hash with one new element with a list
containing both:

        [ hashref_of_new_events, 'new_event_mask_keyword' ]

However, Gtk::Widget::set_events() cannot handle a reference to an EMPTY
hash.  It is thus giving an ugly error message that the hash given is
not one of the expected argument values.

My workaround is to verify that the hash returned by get_events() is not
empty; if it is empty, it is ignored.

        my @ev = ( 'button_press_mask' );
        my $oev = $window->get_events();
        push(@ev, $oev) if scalar keys %{$oev};
        $window->set_events( \ ev );
        $window->signal_connect('button_press_event',
                                sub { Gtk->exit( 0 ); });

There's likely a more compact perl-ish way of performing the same
empty-hash check inline, but this is hopefully somewhat readable.

Subsequent calls to get_events() seems to have merged in the new event
mask keyword as a key in that hash.

-- 
[ e d @ h a l l e y . c c ]



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