1 /* gtkcellrendererprogress.c
2 * Taken from Gaim 0.77.
4 * Gaim is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* This is taken largely from GtkCellRenderer[Text|Pixbuf|Toggle] by
25 * Jonathon Blandford <jrb@redhat.com> for RedHat, Inc.
28 #include "gtkcellrendererprogress.h"
30 static void gtk_cell_renderer_progress_get_property (GObject *object,
34 static void gtk_cell_renderer_progress_set_property (GObject *object,
38 static void gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress);
39 static void gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *class);
40 static void gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
42 GdkRectangle *cell_area,
47 static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
50 GdkRectangle *background_area,
51 GdkRectangle *cell_area,
52 GdkRectangle *expose_area,
55 static gboolean gtk_cell_renderer_progress_activate (GtkCellRenderer *cell,
59 GdkRectangle *background_area,
60 GdkRectangle *cell_area,
63 static void gtk_cell_renderer_progress_finalize (GObject *gobject);
77 static gpointer parent_class;
78 /* static guint progress_cell_renderer_signals [LAST_SIGNAL]; */
80 GType gtk_cell_renderer_progress_get_type (void)
82 static GType cell_progress_type = 0;
84 if (!cell_progress_type)
86 static const GTypeInfo cell_progress_info =
88 sizeof (GtkCellRendererProgressClass),
90 NULL, /* base_finalize */
91 (GClassInitFunc) gtk_cell_renderer_progress_class_init,
92 NULL, /* class_finalize */
93 NULL, /* class_data */
94 sizeof (GtkCellRendererProgress),
96 (GInstanceInitFunc) gtk_cell_renderer_progress_init,
100 g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererProgress",
101 &cell_progress_info, 0);
104 return cell_progress_type;
108 static void gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
110 GTK_CELL_RENDERER(cellprogress)->mode = GTK_CELL_RENDERER_MODE_INERT;
111 GTK_CELL_RENDERER(cellprogress)->xpad = 2;
112 GTK_CELL_RENDERER(cellprogress)->ypad = 2;
115 static void gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *class)
117 GObjectClass *object_class = G_OBJECT_CLASS(class);
118 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(class);
120 parent_class = g_type_class_peek_parent (class);
121 object_class->finalize = gtk_cell_renderer_progress_finalize;
123 object_class->get_property = gtk_cell_renderer_progress_get_property;
124 object_class->set_property = gtk_cell_renderer_progress_set_property;
126 cell_class->get_size = gtk_cell_renderer_progress_get_size;
127 cell_class->render = gtk_cell_renderer_progress_render;
129 g_object_class_install_property (object_class,
131 g_param_spec_double ("percentage",
133 "The fractional progress to display",
137 g_object_class_install_property (object_class,
139 g_param_spec_string ("text",
141 "Text to overlay over progress bar",
144 g_object_class_install_property(object_class,
146 g_param_spec_string("text_set",
148 "Whether to overlay text on the progress bar",
150 G_PARAM_READABLE | G_PARAM_WRITABLE));
155 static void gtk_cell_renderer_progress_finalize (GObject *object)
158 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS(object);
161 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
164 static void gtk_cell_renderer_progress_get_property (GObject *object,
169 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS(object);
173 case PROP_PERCENTAGE:
174 g_value_set_double(value, cellprogress->progress);
177 g_value_set_string(value, cellprogress->text);
180 g_value_set_boolean(value, cellprogress->text_set);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, psec);
188 static void gtk_cell_renderer_progress_set_property (GObject *object,
193 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
197 case PROP_PERCENTAGE:
198 cellprogress->progress = g_value_get_double(value);
201 if (cellprogress->text)
202 g_free(cellprogress->text);
203 cellprogress->text = g_strdup(g_value_get_string(value));
204 g_object_notify(object, "text");
207 cellprogress->text_set = g_value_get_boolean(value);
210 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
215 GtkCellRenderer *gtk_cell_renderer_progress_new(void)
217 return g_object_new(GTK_TYPE_CELL_RENDERER_PROGRESS, NULL);
220 static void gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
222 GdkRectangle *cell_area,
231 calc_width = (gint) cell->xpad * 2 + 50;
232 calc_height = (gint) cell->ypad * 2 + 12;
238 *height = calc_height;
244 *x_offset = cell->xalign * (cell_area->width - calc_width);
245 *x_offset = MAX (*x_offset, 0);
249 *y_offset = cell->yalign * (cell_area->height - calc_height);
250 *y_offset = MAX (*y_offset, 0);
256 static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
259 GdkRectangle *background_area,
260 GdkRectangle *cell_area,
261 GdkRectangle *expose_area,
264 GtkCellRendererProgress *cellprogress = (GtkCellRendererProgress *) cell;
269 width = cell_area->width;
270 height = cell_area->height;
272 if (GTK_WIDGET_HAS_FOCUS (widget))
273 state = GTK_STATE_ACTIVE;
275 state = GTK_STATE_NORMAL;
279 width -= cell->xpad*2;
280 height -= cell->ypad*2;
282 gtk_paint_box (widget->style,
284 GTK_STATE_NORMAL, GTK_SHADOW_IN,
285 NULL, widget, "trough",
286 cell_area->x + cell->xpad,
287 cell_area->y + cell->ypad,
288 width - 1, height - 1);
289 gtk_paint_box (widget->style,
291 state, GTK_SHADOW_OUT,
293 cell_area->x + cell->xpad + 1,
294 cell_area->y + cell->ypad + 1,
295 (width - 3) * cellprogress->progress,