Re: Question about Gtk2-1.042/examples/customlist.pl



Niel Das said:
So I began by simply adding a simple new constructor just above the
INIT_INSTANCE function, as follows:

sub new {
    my $class = shift;
    my $self = {};
    bless($self, $class);
    $self->INIT_INSTANCE();
    print "Hello there from new!\n";
    return $self;
}

You didn't create a Glib::Object!  Glib::Object derivatives are special,
because of the magic underlying that ties them to the underlying C-level
GObject.

Also, *never* call INIT_INSTANCE like that.  It is called by the real
Glib::Object constructor.

Your code should look like this:

  sub new {
      my $class = shift;
      my $self = Glib::Object::new ($class);
      return $self;
  }

Glib::Object::new() will do the actual creation work and return the object
already blessed into the descendent class.

If you look at Glib::Object::Subclass::new(), you'll see that that's about it.
 In general you don't want your constructor to do a lot of work, because that
will make it difficult to derive a new object; thus,
Glib::Object::Subclass::new() simply passes the options through to
Glib::Object::new(), which uses them the way Glib::Object::set() would.  In
fact, most of the constructors actually in the GTK+ API are really just
convenience constructors, which pass their arguments to g_object_new().


TreeModels are a slightly different story.  The TreeModel interface just
defines how to access the data, not anything else.  So, for the API you were
discussing,

my $customlist = CustomList->new($n_columns_scalar, $arrayref_column_types);

you could do that a couple of different ways...


  # suppose you have a construct-only object property columns, which
  # is a reference to an array of columns:
  sub new {
      my ($class, @columns) = @_;
      return Glib::Object::new ($class, columns => \ columns);
  }

  # or you pass some important data that only the constructor can mangle,
  # because you can't change it after the model is created.
  sub new {
      my ($class, @column_descriptors) = @_;
      my $self = Glib::Object::new ($class);
      # note that INIT_INSTANCE has already run at this point.
      $self->_mangle_descriptors_into_columns (@column_descriptors);
      return $self;
  }


Also, I was wondering if there are any additional resources are
available to refer to about implementing custom GtkTreeModels with
Gtk2-Perl besides the customrender.pl and customlist.pl examples, and
the GTK+ 2.0 Tree View Tutorial (http://scentric.net/tutorial/)?

That's the only one of which i know.



#
#  here we register our new type and its interfaces with the type system.
#  If you want to implement additional interfaces like GtkTreeSortable,
#  you will need to do it here.
#

use Glib::Object::Subclass
    Glib::Object::,
    interfaces => [ Gtk2::TreeModel:: ],
    ;

# This method of implementation does not work -- Niel comment
#use Glib::Object::Subclass (Glib::Object::);
#Gtk2::TreeModel->add_interface(__PACKAGE__);

That was an experimental method that was abandoned before the GInterface stuff
stabilized.  The "interfaces" key to Glib::Type::register_object() and
Glib::Object::Subclass is the officially-sanctioned way to do that.


-- 
muppet <scott at asofyet dot org>



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