CellRenderer


Object Hierarchy:

Gtk.CellRenderer Gtk.CellRenderer Gtk.CellRenderer GLib.InitiallyUnowned GLib.InitiallyUnowned GLib.InitiallyUnowned->Gtk.CellRenderer GLib.Object GLib.Object GLib.Object->GLib.InitiallyUnowned

Description:

[ CCode ( type_id = "gtk_cell_renderer_get_type ()" ) ]
public abstract class CellRenderer : InitiallyUnowned

The CellRenderer is a base class of a set of objects used for rendering a cell to a Context.

These objects are used primarily by the TreeView widget, though they aren’t tied to them in any specific way. It is worth noting that CellRenderer is not a Widget and cannot be treated as such.

The primary use of a CellRenderer is for drawing a certain graphical elements on a Context. Typically, one cell renderer is used to draw many cells on the screen. To this extent, it isn’t expected that a CellRenderer keep any permanent state around. Instead, any state is set just prior to use using Objects property system. Then, the cell is measured using get_size. Finally, the cell is rendered in the correct location using render.

There are a number of rules that must be followed when writing a new CellRenderer. First and foremost, it’s important that a certain set of properties will always yield a cell renderer of the same size, barring a Style change. The CellRenderer also has a number of generic properties that are expected to be honored by all children.

Beyond merely rendering a cell, cell renderers can optionally provide active user interface elements. A cell renderer can be “activatable” like CellRendererToggle, which toggles when it gets activated by a mouse click, or it can be “editable” like CellRendererText, which allows the user to edit the text using a widget implementing the CellEditable interface, e.g. Entry. To make a cell renderer activatable or editable, you have to implement the activate or start_editing virtual functions, respectively.

Many properties of CellRenderer and its subclasses have a corresponding “set” property, e.g. “cell-background-set” corresponds to “cell-background”. These “set” properties reflect whether a property has been set or not. You should not set them independently.

Example: CellRenderer:

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

public MyCellRenderer () {
GLib.Object ();
}

// get_size method, always request a 50x50 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)
{
x_offset = 0;
y_offset = 0;
width = 50;
height = 50;
}

// render method
public override void render (Cairo.Context ctx, Gtk.Widget widget,
Gdk.Rectangle background_area,
Gdk.Rectangle cell_area,
Gtk.CellRendererState flags)
{
Gdk.cairo_rectangle (ctx, background_area);
if (icon != null) {
// draw a pixbuf on a cairo context
Gdk.cairo_set_source_pixbuf (ctx, icon,
background_area.x,
background_area.y);
ctx.fill ();
}
}
}

Gdk.Pixbuf open_image (string file) {
try {
return new Gdk.Pixbuf.from_file (file);
} catch (Error e) {
error ("%s", e.message);
}
}

public static int main (string[] args) {
if (args[1] == null) {
print ("Error: Use: ./Gtk.CellRenderer <filename>\n");
return 0;
}

Gtk.init (ref args);

// The TreeView:
Gtk.TreeView tv = new Gtk.TreeView ();
Gtk.ListStore tm = new Gtk.ListStore (2, typeof (Gdk.Pixbuf), typeof (string));
tv.set_model (tm);

Gtk.CellRenderer renderer = new MyCellRenderer ();
Gtk.TreeViewColumn col = new Gtk.TreeViewColumn ();
col.pack_start (renderer, true);
col.set_title ("1st column");
col.add_attribute (renderer, "icon", 0);
tv.append_column (col);

renderer = new Gtk.CellRendererText ();
col = new Gtk.TreeViewColumn ();
col.pack_start (renderer, true);
col.set_title ("2nd column");
col.add_attribute (renderer, "text", 1);
tv.append_column (col);


// Append data to our model:
Gtk.TreeIter ti;
tm.append (out ti);

Gdk.Pixbuf pixbuf = open_image (args[1]);
tm.set (ti, 0, pixbuf, 1, "asd", -1);


// The main window:
Gtk.Window win = new Gtk.Window ();
win.set_default_size (400, 100);
win.destroy.connect (Gtk.main_quit);
win.add (tv);
win.show_all ();

Gtk.main ();
return 0;
}

valac --pkg gtk+-3.0 Gtk.CellRenderer.vala


Namespace: Gtk
Package: gtk+-3.0

Content:

Properties:

Creation methods:

Methods:

Signals:

Inherited Members: