Subclassing Gtk2::CellRenderer



Background: I want to create a custom cell renderer for TreeView to allow cells to be editable by using something that might look like an OptionMenu or a Combo.

Since I'm new to Gtk, I (try to) start off by doing nothing, i.e. implement my own renderer that inherits everything from an existing renderer. The point is to get to grips with the basics of GObject inheritance etc.

So, I made a few small changes to the (1.0rc3) example "simplelist.pl" (please see attached diff), and implemented a basically empty class (please see attached "MyRenderer.pm"). So far so good. The printf's seem to indicate that stuff is routed through my class and since the example still works, it looks as if I'm on the correct track.

Next step is to try to override a method, and just delegate the handling to the parent class. The Gtk documentation shows that a "GtkCellRenderer" class (the superclass of "GtkCellRendererText", which again is the class that "MyRenderer" inherits from) should have a "gtk_cell_renderer_render" method, called whenever the cell needs to be rendered. Looks good as a test case, this method will obviously be the crux of the matter when going back to what I eventually would like to do.

A method invocation in GObject is just a signal, right? So I should override the proper signal name in order to let my own routine handle this method.

However, when I try to install an overridden method (uncommenting line 7 in "MyRenderer.pm") the way I think method overriding is to be done, I get an error message saying "can't override class closure for unknown signal render at ...". Where am I going wrong?

Bjarne
*** /usr/src/Gtk2-perl/Gtk2-1.00rc3/examples/simplelist.pl      Fri Sep 26 01:26:33 2003
--- simplelist.pl       Wed Oct  1 15:03:23 2003
***************
*** 29,30 ****
--- 29,32 ----
  
+ use MyRenderer;
+ 
  # add a new type of column that reverses the text that's in a scalar
***************
*** 33,35 ****
                type     => 'Glib::Scalar',      
!               renderer => 'Gtk2::CellRendererText',   
                attr     => sub {
--- 35,37 ----
                type     => 'Glib::Scalar',      
!               renderer => 'MyRenderer',   
                attr     => sub {
***************
*** 84,85 ****
--- 86,91 ----
  $scwin->add ($slist);
+ printf STDERR "Renderer for column 5 is %s\n",
+     $slist->get_column (5)->get_cell_renderers;
+ printf STDERR "Renderer for column 6 is %s\n",
+     $slist->get_column (6)->get_cell_renderers;
  
package Gtk2::CellRendererText::MyRenderer;
use strict;
use Gtk2;

use Glib::Object::Subclass Gtk2::CellRendererText::,
    signals => {
        render => \&do_render,
#       get_size => \&do_get_size,
    },
    properties => [ ],
;

# Overridden render
sub do_render {
    my $self = shift;
    print STDERR "MyRenderer::render called\n";
    $self->signal_chain_from_overridden;
}

sub do_get_size {
    my $self = shift;
    print STDERR "MyRenderer::get_size called\n";
    $self->signal_chain_from_overridden;
}

sub INIT_INSTANCE {
    my $self = shift;
    print STDERR "MyRenderer::INIT_INSTANCE called\n";
    printf STDERR "Type of self = %s\n", $self;
}

1;


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