Re: [gtk-list] Re: Perl/Gtk: how to get the data from a Gtk::Lis



On Tue, 14 Apr 1998 tile@primenet.com wrote:

> On 15-Apr-98 Kenneth Albanowski wrote:
> > You can use the get_data and set_data methods on the listitems (although I
> > don't believe those methods are much use at the moment, as they'll only
> > let you store gtk_object types).
> > 
> > The Perl approach is simply to store the data in the listitem:
> > 
> >    for $item (@items) {
> >      my $li = new Gtk::ListItem($item);
> >      $li->{mydata} = ++$mydata;
> >      $list->add($li);
> >      $li->show;
> >    }
> >    $list->signal_connect('selection_changed' => \&sel_callback);
> >
> > -- 
> > Kenneth Albanowski (kjahds@kjahds.com, CIS: 70705,126)
> 
> Errg... it's errant to start changing object data, it's much better
> to treat an object as an object, not a hash with methods.  Anyway,
> there are better ways to go about this (read my previous post on
> this thread).

<shrug> GTK objects can store arbitrary data, using the get_data and
set_data calls. PerlGtk objects can store arbitrary data directly
(including references to other Perl & GTK objects) in the object as a hash
value. I'm not sure, offhand, why one would be better or worse, only that
the hash method is far more convenient from Perl. And, in general, Perl
objects are assumed to be able to contain arbitrary data if they are
hashes.

(Note that PerlGtk objects are not tied hashes, so the storing data in
this manner is quite efficient.) 

Now, if the ListItems cannot be used in this manner, that's another issue
altogether. I'm not familiar enough with Lists to say. 

Certainly iterating over the children of the ListItem to get the label,
and read it, should work, but this seems relatively inefficient,
especially if the desired data isn't just the label, but some more complex
data for which the label is merely, well, a label. In that case, storing
data in the ListItem (or the Label, or some other child of the ListItem) 
seems to make the most sense. I can't think of any more reasonable way to
do it from C. 

I suppose one could derive a custom child of Gtk::Object, and insert that
into the ListItem as a non-visible child, but this seems to be going a bit
far.:-)

-- 
Kenneth Albanowski (kjahds@kjahds.com, CIS: 70705,126)





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