1 /* Taken from the GTK TreeView tutorial on gtk.org.
5 #include "progressbar.h"
7 /* This is based mainly on GtkCellRendererProgress
8 * in GAIM, written and (c) 2002 by Sean Egan
9 * (Licensed under the GPL), which in turn is
10 * based on Gtk's GtkCellRenderer[Text|Toggle|Pixbuf]
11 * implementation by Jonathan Blandford */
13 /* Some boring function declarations: GObject type system stuff */
15 static void custom_cell_renderer_progress_init (CustomCellRendererProgress *cellprogress);
17 static void custom_cell_renderer_progress_class_init (CustomCellRendererProgressClass *klass);
19 static void custom_cell_renderer_progress_get_property (GObject *object,
24 static void custom_cell_renderer_progress_set_property (GObject *object,
29 static void custom_cell_renderer_progress_finalize (GObject *gobject);
32 /* These functions are the heart of our custom cell renderer: */
34 static void custom_cell_renderer_progress_get_size (GtkCellRenderer *cell,
36 GdkRectangle *cell_area,
42 static void custom_cell_renderer_progress_render (GtkCellRenderer *cell,
45 GdkRectangle *background_area,
46 GdkRectangle *cell_area,
47 GdkRectangle *expose_area,
56 static gpointer parent_class;
59 /***************************************************************************
61 * custom_cell_renderer_progress_get_type: here we register our type with
62 * the GObject type system if we
63 * haven't done so yet. Everything
64 * else is done in the callbacks.
66 ***************************************************************************/
69 custom_cell_renderer_progress_get_type (void)
71 static GType cell_progress_type = 0;
73 if (cell_progress_type)
74 return cell_progress_type;
78 static const GTypeInfo cell_progress_info =
80 sizeof (CustomCellRendererProgressClass),
82 NULL, /* base_finalize */
83 (GClassInitFunc) custom_cell_renderer_progress_class_init,
84 NULL, /* class_finalize */
85 NULL, /* class_data */
86 sizeof (CustomCellRendererProgress),
88 (GInstanceInitFunc) custom_cell_renderer_progress_init,
91 /* Derive from GtkCellRenderer */
92 cell_progress_type = g_type_register_static (GTK_TYPE_CELL_RENDERER,
93 "CustomCellRendererProgress",
98 return cell_progress_type;
102 /***************************************************************************
104 * custom_cell_renderer_progress_init: set some default properties of the
105 * parent (GtkCellRenderer).
107 ***************************************************************************/
110 custom_cell_renderer_progress_init (CustomCellRendererProgress *cellrendererprogress)
112 GTK_CELL_RENDERER(cellrendererprogress)->mode = GTK_CELL_RENDERER_MODE_INERT;
113 GTK_CELL_RENDERER(cellrendererprogress)->xpad = 2;
114 GTK_CELL_RENDERER(cellrendererprogress)->ypad = 2;
118 /***************************************************************************
120 * custom_cell_renderer_progress_class_init:
122 * set up our own get_property and set_property functions, and
123 * override the parent's functions that we need to implement.
124 * And make our new "percentage" property known to the type system.
125 * If you want cells that can be activated on their own (ie. not
126 * just the whole row selected) or cells that are editable, you
127 * will need to override 'activate' and 'start_editing' as well.
129 ***************************************************************************/
132 custom_cell_renderer_progress_class_init (CustomCellRendererProgressClass *klass)
134 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(klass);
135 GObjectClass *object_class = G_OBJECT_CLASS(klass);
137 parent_class = g_type_class_peek_parent (klass);
138 object_class->finalize = custom_cell_renderer_progress_finalize;
140 /* Hook up functions to set and get our
141 * custom cell renderer properties */
142 object_class->get_property = custom_cell_renderer_progress_get_property;
143 object_class->set_property = custom_cell_renderer_progress_set_property;
145 /* Override the two crucial functions that are the heart
146 * of a cell renderer in the parent class */
147 cell_class->get_size = custom_cell_renderer_progress_get_size;
148 cell_class->render = custom_cell_renderer_progress_render;
150 /* Install our very own properties */
151 g_object_class_install_property (object_class,
153 g_param_spec_double ("percentage",
155 "The fractional progress to display",
161 /***************************************************************************
163 * custom_cell_renderer_progress_finalize: free any resources here
165 ***************************************************************************/
168 custom_cell_renderer_progress_finalize (GObject *object)
171 CustomCellRendererProgress *cellrendererprogress = CUSTOM_CELL_RENDERER_PROGRESS(object);
174 /* Free any dynamically allocated resources here */
176 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
180 /***************************************************************************
182 * custom_cell_renderer_progress_get_property: as it says
184 ***************************************************************************/
187 custom_cell_renderer_progress_get_property (GObject *object,
192 CustomCellRendererProgress *cellprogress = CUSTOM_CELL_RENDERER_PROGRESS(object);
196 case PROP_PERCENTAGE:
197 g_value_set_double(value, cellprogress->progress);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, psec);
207 /***************************************************************************
209 * custom_cell_renderer_progress_set_property: as it says
211 ***************************************************************************/
214 custom_cell_renderer_progress_set_property (GObject *object,
219 CustomCellRendererProgress *cellprogress = CUSTOM_CELL_RENDERER_PROGRESS (object);
223 case PROP_PERCENTAGE:
224 cellprogress->progress = g_value_get_double(value);
228 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
233 /***************************************************************************
235 * custom_cell_renderer_progress_new: return a new cell renderer instance
237 ***************************************************************************/
240 custom_cell_renderer_progress_new (void)
242 return g_object_new(CUSTOM_TYPE_CELL_RENDERER_PROGRESS, NULL);
246 /***************************************************************************
248 * custom_cell_renderer_progress_get_size: crucial - calculate the size
249 * of our cell, taking into account
250 * padding and alignment properties
253 ***************************************************************************/
255 #define FIXED_WIDTH 100
256 #define FIXED_HEIGHT 12
259 custom_cell_renderer_progress_get_size (GtkCellRenderer *cell,
261 GdkRectangle *cell_area,
270 calc_width = (gint) cell->xpad * 2 + FIXED_WIDTH;
271 calc_height = (gint) cell->ypad * 2 + FIXED_HEIGHT;
277 *height = calc_height;
283 *x_offset = cell->xalign * (cell_area->width - calc_width);
284 *x_offset = MAX (*x_offset, 0);
289 *y_offset = cell->yalign * (cell_area->height - calc_height);
290 *y_offset = MAX (*y_offset, 0);
296 /***************************************************************************
298 * custom_cell_renderer_progress_render: crucial - do the rendering.
300 ***************************************************************************/
303 custom_cell_renderer_progress_render (GtkCellRenderer *cell,
306 GdkRectangle *background_area,
307 GdkRectangle *cell_area,
308 GdkRectangle *expose_area,
311 CustomCellRendererProgress *cellprogress = CUSTOM_CELL_RENDERER_PROGRESS (cell);
314 gint x_offset, y_offset;
316 custom_cell_renderer_progress_get_size (cell, widget, cell_area,
317 &x_offset, &y_offset,
320 if (GTK_WIDGET_HAS_FOCUS (widget))
321 state = GTK_STATE_ACTIVE;
323 state = GTK_STATE_NORMAL;
325 width -= cell->xpad*2;
326 height -= cell->ypad*2;
328 gtk_paint_box (widget->style,
330 GTK_STATE_NORMAL, GTK_SHADOW_IN,
331 NULL, widget, "trough",
332 cell_area->x + x_offset + cell->xpad,
333 cell_area->y + y_offset + cell->ypad,
334 width - 1, height - 1);
336 gtk_paint_box (widget->style,
338 state, GTK_SHADOW_OUT,
340 cell_area->x + x_offset + cell->xpad,
341 cell_area->y + y_offset + cell->ypad,
342 width * cellprogress->progress,