[Gtk-sharp-list] custom cell renderer

Michael Quinn mikeq@wam.umd.edu
Thu, 06 Jan 2005 12:10:01 -0500


I'm trying to write just a simple custom CellRenderer that displays both
an icon and some text.  I've extended a CellRendererText, and within
that class it holds a CellRendererPixbuf object.  Then I override the
GetSize and Render methods so that the two render right next to each
other in a row.  Here's the relevant code:

public override void GetSize (Widget widget, ref Rectangle cell_area,
out int x_offset, out int y_offset, out int width, out int height)
{		
   int text_x, text_y, text_w, text_h;
   base.GetSize(widget, ref cell_area, out text_x, out text_y, out
text_w, out text_h);
				
   int pix_x, pix_y, pix_w, pix_h;
   pixCell.GetSize(widget, ref cell_area, out pix_x, out pix_y, out
pix_w, out pix_h);

   if(text_w != 0 && pix_w != 0)
      pix_w += (int)this.Xpad;

   x_offset = 0;
   y_offset = 0;
   width = text_w + pix_w;
   height = Math.Max(text_h, pix_h);
}

protected override void Render (Drawable window, Widget widget,
Rectangle background_area, Rectangle cell_area, Rectangle expose_area,
CellRendererState flags)
{
   Gdk.Window gdkWindow = (Gdk.Window)window;
		
   int pix_x, pix_y, pix_w, pix_h;
   pixCell.GetSize(widget, ref cell_area, out pix_x, out pix_y, out
pix_w, out pix_h);
		
   Rectangle pixCellArea = cell_area;
   pixCellArea.Width = pix_w;

   pixCell.Render(gdkWindow, widget, background_area, pixCellArea,
expose_area, flags);

   Rectangle textCellArea = cell_area;

   if (pix_w != 0)
   {
      textCellArea.X = textCellArea.X + pix_w + (int)this.Xpad;
      textCellArea.Width = textCellArea.Width - pix_w - (int)this.Xpad;
   }
		
   base.Render(window, widget, background_area, textCellArea,
expose_area, flags);
}

pixCell is the CellRendererPixbuf contained inside my custom renderer.
This code has been ported almost line by line from a custom cell
renderer used in Coaster.  Some problems I'm having are:

the final base.Render call always throws a NullReferenceException (even
though neither window nor widget is null).  If I comment out this line,
though, the renderer still wont render the PixBuf.

Also, in the GetSize method, when I call pixCell.GetSize, pix_* are
always set to zero (which explains why it later won't render).

So, is there anybody who could offer any help? Or, is there a custom
cell renderer already out there that I could use that does this already?
Any help is greatly appreciated.  Oh, and yes, I'm sure that the
CellRendererPixbuf has its Pixbuf property properly assigned to a valid
Pixbuf.

-- 
Michael Quinn <mikeq@wam.umd.edu>