Re: Subclassing Gtk2::CellRenderer (reprise)



On Wednesday, October 15, 2003, at 09:50 AM, muppet wrote:

i will post more info about this, and some code, later today.

everything you need is at http://asofyet.org/muppet/software/gtk2-perl/
  Gtk2-CellRenderer-Base-0.03.tar.gz

this requires that you patch Glib and Gtk2:
  Glib-1.00-type_utils.patch
  Gtk2-1.00-get_child_requisition.patch
  Gtk2-1.00-style_ornull.patch

for those working against current cvs, there's also
  cellrendererstuff.patch


the archive includes both of Tortsten's way cool renderers, my original hack at the popup renderer (now new and improved with y'all's patches), and a new hack, fancy.pl.

fancy.pl is very ugly, but illustrates deriving directly from Gtk2::CellRendererText via the new tricks that 0.03 brings in. what are those tricks?

Gtk2::CellRenderer::register_subclass (parent_package, new_package, ...)

does something very similar to Glib::Type::register, but requires that parent_package descends from GtkCellRenderer. the varargs stuff is the same as what you'd pass to Glib::Type::register or the use args of Glib::Object::Subclass.

therefore:

   # derive a new object, named ReallyCool, from Gtk2::CellRendererText.
   # the resulting object has all of the properties of a text renderer,
# plus the new one 'nlines'. for each event, a perl sub will be called.
   Gtk2::CellRendererText->register_subclass ('ReallyCool',
           properties => [
                 Glib::ParamSpec->int ('nlines', 'number of lines',
'the number of lines you want in the cell', 1, 65535, 1, [qw/readable writable/]),
           ]);

   # implement the get_size() virtual method:
   sub on_get_size {
          # we're just extending the parent.  let's call it first,
          # and see what it wants.
          my @results = shift->parent_on_get_size (@_);
          ...
   }

all of this is subject to change, i have designs already on how the chaining may be better done; as it is currently implemented, it is very likely not to work on multiply-derived renderers.

in case you hadn't noticed, this method allows us to avoid the use of Gtk2::CellRenderer::Base altogether (well, it's used under the hood, but), which i think is somewhat cleaner.

comments?  thoughts?  suggestions?


--
muppet <scott at asofyet dot org>




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