GTK+ By Example/Tree View/Custom Cell Renderers

Writing Custom Cell Renderers
The cell renderers that come with Gtk+ should be sufficient for most purposes, but there might be occasions where you want to display something in a tree view that you cannot display with the provided cell renderers, or where you want to derive from one of the provided cell renderers to extend its functionality.

You can do this by writing a new object that derives from GtkCellRenderer (or even one of the other cell renderers if you just want to extend an existing one).

Three things you need to do in the course of that:


 * Register some new properties that your renderer needs with the type system and write your own set_property and get_property functions to set and get your new renderer's properties.
 * Write your own cell_renderer_get_size function and override the parent object's function (usually the parent is of type GtkCellRenderer. Note that you should honour the standard properties for padding and cell alignment of the parent object here.
 * Write your own cell_renderer_render function and override the parent object's function. This function does the actual rendering.

The GObject type system stuff of writing a new cell renderer is similar to what we have done above when writing a custom tree model, and is relatively straight forward in this case. Copy and paste and modify according to your own needs.

Good examples of cell renderer code to look at or even modify are GtkCellRendererPixbuf and GtkCellRendererToggle in the Gtk+ source code tree. Both cases are less than five hundred lines of code to look at and thus should be fairly easy to digest. 12.1. Working Example: a Progress Bar Cell Renderer

In the following we will write a custom cell renderer to render progress bars into a tree view (the code was "heavily inspired" by Sean Egan's progress bar cell renderer implementation in GAIM; note this is merely for demonstrational purposes, Gtk+ has had a progress bar cell renderer for quite a while now: see the GtkCellRendererProgress API documentation for more details).


 * custom-cell-renderer-progressbar.h
 * custom-cell-renderer-progressbar.c
 * main.c

custom-cell-renderer-progressbar.h
The header file consists of the usual GObject type cast and type check defines and our CustomCellRendererProgress structure. As the type of the parent indicates, we derive from GtkCellRenderer. The parent object must always be the first item in the structure (note also that it is not a pointer to an object, but the parent object structure itself embedded in our structure).

Our CustomCellRendererProgress structure is fairly uneventful and contains only a double precision float variable in which we store our new "percentage" property (which will determine how long the progressbar is going to be).

custom-cell-renderer-progressbar.c
The code contains everything as described above, so let's jump right into it:

main.c
And here is a little test that makes use of our new CustomCellRendererProgress:

Cell Renderers Others Have Written
Just in case you are one of those people who do not like to re-invent the wheel, here is a list of custom cell renderers other people have written:


 * Date cell renderer (Planner) (is this one easy to re-use?)
 * List/combo cell renderer (Planner) (is this one easy to re-use?) (FIXME: obsoleted by GtkCellRendererCombo)
 * Pop-up cell renderer (Planner) (what does this do?)
 * Your custom cell renderer here?!