Gtk+ CellRender in Vala

How much code does it takes to implement a CellRendererPixbuf-alike CellRenderer for Gtk+ in Vala?

For the very basic stuff, around 40 lines of code actually:

using Gtk;
using Gdk;
class MyCellRenderer : Gtk.CellRenderer
  /* icon property set by the tree column */
  public Gdk.Pixbuf icon { get; set; }

  /* dumb constructor */
  public MyCellRenderer () {}

  /* get_size method, always request a 50×50 area */
  public override void get_size (Gtk.Widget widget,
                                 Gdk.Rectangle? cell_area,
                                 out int x_offset,
                                 out int y_offset,
                                 out int width,
                                 out int height)
    /* The bindings miss the nullable property, so we need to check if the
     * values are null.
    if (&x_offset != null) x_offset = 0;
    if (&y_offset != null) y_offset = 0;
    if (&width != null) width = 50;
    if (&height != null) height = 50;
  /* render method */
  public override void render (Gdk.Window    window,
                               Gtk.Widget    widget,
                               Gdk.Rectangle background_area,
                               Gdk.Rectangle cell_area,
                               Gdk.Rectangle expose_area,
                               Gtk.CellRendererState flags)
    var ctx = Gdk.cairo_create (window);

    if (&expose_area != null)
      Gdk.cairo_rectangle (ctx, expose_area);

    Gdk.cairo_rectangle (ctx, background_area);
    if (icon != null)
      Gdk.cairo_set_source_pixbuf (ctx, icon,

.x, background_area.y); /* draws a pixbuf on a cairo context */

This works with Vala 0.5.6, check the sample in the Vala site for a working test case.

Using a high level language to generate reusable stuff is priceless 🙂

13 thoughts on “Gtk+ CellRender in Vala

  1. Hello Alberto
    This is a great example of valas features and easyness. I am learning vala for a while now, but until now I didn’t know about that ‘&’ operator before the out vars in the ‘get_size’ method. (until now I thought vala would use the reference from the parameter without any more work???)
    I think that also is not documented anywhere. Please add a few lines to the reference manual or the tutorial. That would be great! Thanks


  2. I don’t think you need that “&” there. You need to declared the parameters nullable and then you can just do:
    if (expose_area != null)
    unless i missed something?


  3. like this?
    public override void get_size (Gtk.Widget widget, Gdk.Rectangle? cell_area, out int? x_offset, out int? y_offset, out int? width, out int? height)


  4. That’s why it has been possible to do that THAT EASILY with C++ and Gtkmm for quite a while. Before Vala was even thought about.
    Gotta love those hex-wheels you reinvented.


  5. Hub!
    C++ is a rather low-level language compared to Vala so the comparison isn’t really a good one.
    However you could make the same argument in favor of C#, Java and Python but one advantage Vala has over them is that it doesn’t introduce any run-time dependency and/or performance overhead.


  6. Just to be aware, the code at
    produces a warning with the new valac 0.7.6
    CellRendererSample.vala:10.3-10.23: warning: unable to chain up to private base constructor
    public MyCellRenderer () {}
    /home/mjc/CellRendererSample.vala.c: In function ‘my_cell_renderer_class_init’:
    /home/mjc/CellRendererSample.vala.c:182: warning: assignment from incompatible pointer type
    /home/mjc/CellRendererSample.vala.c:183: warning: assignment from incompatible pointer type
    Compilation succeeded – 1 warning(s)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s