Subclassing Gtk2::CellRenderer (reprise)



I had a brilliant idea in the shower this morning and couldn't let it drop
until i'd tried it.  After a good bit of messing around, i have little sandbox
module that can implement a cell renderer in perl.  If this works out, it
could be folded into the main Gtk2 module.


Since GtkCellRenderer declares no signals for the virtual methods get_size,
render, activate, and start_editing, it's impossible to derive this class with
normal methods.  In the bindings we have a couple of different approaches we
could take:

1. create signals for the methods and add them to the GtkCellRendererClass.
   this seems attractive at first glance, but there's a chicken and egg
problem, and what's worse, we'd be modifying somebody else's object. a big
no-no.

2. create a special subclass of GtkCellRenderer that does enable us to
   override its methods, and use that as the base class for Perl-derived
objects.  The biggest drawback of this is that we can't inherit an existing
renderer; for showing text, this is a major PITA.

As it turns out, pygtk already does method 2 successfully, which makes my
great idea seem a little less great, rather like the Professor finding out
somebody had already invented the Frisbee[1].

Nevertheless, the attached archive implements a new object, the
Gtk2::CellRenderer::Base, intended for use as a base class for Perl-derived
cell renderers.  An example program is included.


Of note: this base class does *not* create new signals for the virtual
methods.  There was a chicken and egg problem, which mostly would result in
the default handler being called twice.  (I can't remember now how i came to
that conclusion so it may not be correct.)  At any rate, the overriding factor
in the decision was the fact that cellrenderer methods are called *really*
*often*, and need to be very fast.  Thus, this module has some custom
marshalling for each Perl callback, which is treated as a method (i.e. called
with call_method()); this may cause problems, but this is an alpha release for
comments.


Basically, you just provide "on_get_size", "on_render", "on_activate", and
"on_start_editing" in your package, and they will be called automagically. 
The rest of the derivation for the widget is like other Glib::Object
subclasses.

Just play with the example program and you'll figure it out eventually.  It's
not very pretty... in fact there's no indication that you can click on it, and
the menu doesn't position itself properly, and the text isn't customizable,
and the implementation isn't perfect...  but it works and i think it's pretty
cool.


[1] if you know what this is a reference to, then you, too, saw too much tv as
a kid.


-- 
muppet <scott at asofyet dot org>

Attachment: Gtk2-CellRenderer-Base-0.01.tar.gz
Description: Unix tar archive



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