Re: GladePerl: Object context



Hi Robert,

Robert Schwebel wrote:
> 
> Hi Dermot, hello Gtk and Glade gurus!
> 
> I run into some problems with the right context of objects. For
> inspection I use something like
>> snipped
> to dump the contents of the object data to the terminal. If you do that
> from an object's constructor you can see that the context is right here
> and it really sees the data from the object itself. But if you press for
> example a button or a menu button you'll see that the handler function
> (still inside the same package) seems to be called with the wrong
> context! Only the data from Gtk::Button becomes dumped.
I have always expected this behaviour, but thinking about it, there is no
way to get from the triggering widget to the object instance.

As you know, the source code generated by Glade-Perl has a constructor new()
for each form (toplevel window/diaLog) that returns an object that is a flat
hash of refs to all the widgets in the form, as well as some shortcuts to 
the toplevel window and so on. When you inspect this you will see all the
widgets.

On the other hand, signal_connect() causes handlers to be called with a
reference to the triggering widget as the first parameter (ie. the class).

You can $class->get_toplevel() to get a ref to the toplevel Gtk window so
that you can hide/destroy it but how do you get to the particular widgets
on this instance of the form?

If you want to get more data passed to the handler, you have to supply this
data to the signal_connect() call in the constructor. 

I guess that the proper way would be to pass a ref to the constructed object
as data to the signal_connect() call that connects the button/menuitem to a
handler. eg. in my example generated BusFrame constructor
    $forms->{'BusFrame'}{'Index'}->signal_connect( 'activate', 
        'on_Index_activate', $self );

This would allow you to use the TOPLEVEL() or FORM() object 
accessor to get at any other widget on this instance of the form so you
could do for instance in Subclass
    sub on_Index_activate {
        my ($class, $data) = @ARG;
        $data->FORM->{'other_widgetname'}->set_text('some text');
    }

Does this achieve what you need? If so, I will change the next version
(0.3.8) will pass the object ref to the signal handler as I described.

Regards, Dermot



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