1 /* gtkcellrendererprogress.c
2 * Copyright (C) 2002 Naba Kumar <kh_naba@users.sourceforge.net>
3 * heavily modified by Jörgen Scheibengruber <mfcn@gmx.de>
4 * heavily modified by Marco Pesenti Gritti <marco@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2004. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkcellrendererprogress.h"
35 #define GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), \
36 GTK_TYPE_CELL_RENDERER_PROGRESS, \
37 GtkCellRendererProgressPrivate))
46 struct _GtkCellRendererProgressPrivate
55 static void gtk_cell_renderer_progress_finalize (GObject *object);
56 static void gtk_cell_renderer_progress_get_property (GObject *object,
60 static void gtk_cell_renderer_progress_set_property (GObject *object,
64 static void gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
66 static void gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
68 static void compute_dimensions (GtkCellRenderer *cell,
73 static void gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
75 GdkRectangle *cell_area,
80 static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
83 GdkRectangle *background_area,
84 GdkRectangle *cell_area,
85 GdkRectangle *expose_area,
89 G_DEFINE_TYPE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER);
92 gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
94 GObjectClass *object_class = G_OBJECT_CLASS (klass);
95 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
97 object_class->finalize = gtk_cell_renderer_progress_finalize;
98 object_class->get_property = gtk_cell_renderer_progress_get_property;
99 object_class->set_property = gtk_cell_renderer_progress_set_property;
101 cell_class->get_size = gtk_cell_renderer_progress_get_size;
102 cell_class->render = gtk_cell_renderer_progress_render;
105 * GtkCellRendererProgress:value:
107 * The "value" property determines the percentage to which the
108 * progress bar will be "filled in".
112 g_object_class_install_property (object_class,
114 g_param_spec_int ("value",
116 P_("Value of the progress bar"),
121 * GtkCellRendererProgress:text:
123 * The "text" property determines the label which will be drawn
124 * over the progress bar. Setting this property to %NULL causes the default
125 * label to be displayed. Setting this property to an empty string causes
126 * no label to be displayed.
130 g_object_class_install_property (object_class,
132 g_param_spec_string ("text",
134 P_("Text on the progress bar"),
138 g_type_class_add_private (object_class,
139 sizeof (GtkCellRendererProgressPrivate));
143 gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
145 cellprogress->priv = GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE (cellprogress);
146 cellprogress->priv->value = 0;
147 cellprogress->priv->text = NULL;
148 cellprogress->priv->label = NULL;
149 cellprogress->priv->min_w = -1;
150 cellprogress->priv->min_h = -1;
155 * gtk_cell_renderer_progress_new:
157 * Creates a new #GtkCellRendererProgress.
159 * Return value: the new cell renderer
164 gtk_cell_renderer_progress_new (void)
166 return g_object_new (GTK_TYPE_CELL_RENDERER_PROGRESS, NULL);
170 gtk_cell_renderer_progress_finalize (GObject *object)
172 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
174 g_free (cellprogress->priv->text);
175 g_free (cellprogress->priv->label);
177 G_OBJECT_CLASS (gtk_cell_renderer_progress_parent_class)->finalize (object);
181 gtk_cell_renderer_progress_get_property (GObject *object,
186 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
191 g_value_set_int (value, cellprogress->priv->value);
194 g_value_set_string (value, cellprogress->priv->text);
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
202 gtk_cell_renderer_progress_set_property (GObject *object,
207 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
212 gtk_cell_renderer_progress_set_value (cellprogress,
213 g_value_get_int (value));
216 gtk_cell_renderer_progress_set_text (cellprogress,
217 g_value_get_string (value));
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
225 gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
230 cellprogress->priv->value = value;
232 if (cellprogress->priv->text)
233 text = g_strdup (cellprogress->priv->text);
235 /* do not translate the part before the | */
236 text = g_strdup_printf (Q_("progress bar label|%d %%"),
237 cellprogress->priv->value);
239 g_free (cellprogress->priv->label);
240 cellprogress->priv->label = text;
244 gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
249 new_text = g_strdup (text);
250 g_free (cellprogress->priv->text);
251 cellprogress->priv->text = new_text;
253 /* Update the label */
254 gtk_cell_renderer_progress_set_value (cellprogress, cellprogress->priv->value);
258 compute_dimensions (GtkCellRenderer *cell,
264 PangoRectangle logical_rect;
267 layout = gtk_widget_create_pango_layout (widget, text);
268 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
271 *width = logical_rect.width + cell->xpad * 2 + widget->style->xthickness * 2;
274 *height = logical_rect.height + cell->ypad * 2 + widget->style->ythickness * 2;
276 g_object_unref (layout);
280 gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
282 GdkRectangle *cell_area,
288 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
292 if (cellprogress->priv->min_w < 0)
294 text = g_strdup_printf (Q_("progress bar label|%d %%"), 100);
295 compute_dimensions (cell, widget, text,
296 &cellprogress->priv->min_w,
297 &cellprogress->priv->min_h);
301 compute_dimensions (cell, widget, cellprogress->priv->label, &w, &h);
304 *width = MAX (cellprogress->priv->min_w, w);
307 *height = MIN (cellprogress->priv->min_h, h);
311 gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
314 GdkRectangle *background_area,
315 GdkRectangle *cell_area,
316 GdkRectangle *expose_area,
319 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
322 PangoRectangle logical_rect;
323 gint x, y, w, h, perc_w, pos;
327 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
329 gc = gdk_gc_new (window);
331 x = cell_area->x + cell->xpad;
332 y = cell_area->y + cell->ypad;
334 w = cell_area->width - cell->xpad * 2;
335 h = cell_area->height - cell->ypad * 2;
337 gdk_gc_set_rgb_fg_color (gc, &widget->style->fg[GTK_STATE_NORMAL]);
338 gdk_draw_rectangle (window, gc, TRUE, x, y, w, h);
340 x += widget->style->xthickness;
341 y += widget->style->ythickness;
342 w -= widget->style->xthickness * 2;
343 h -= widget->style->ythickness * 2;
344 gdk_gc_set_rgb_fg_color (gc, &widget->style->bg[GTK_STATE_NORMAL]);
345 gdk_draw_rectangle (window, gc, TRUE, x, y, w, h);
347 gdk_gc_set_rgb_fg_color (gc, &widget->style->bg[GTK_STATE_SELECTED]);
348 perc_w = w * MAX (0, cellprogress->priv->value) / 100;
349 gdk_draw_rectangle (window, gc, TRUE, is_rtl ? (x + w - perc_w) : x, y, perc_w, h);
351 layout = gtk_widget_create_pango_layout (widget, cellprogress->priv->label);
352 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
354 pos = (w - logical_rect.width)/2;
358 clip.width = is_rtl ? w - perc_w : perc_w;
361 gtk_paint_layout (widget->style, window,
362 is_rtl ? GTK_STATE_NORMAL : GTK_STATE_SELECTED,
363 FALSE, &clip, widget, "progressbar",
364 x + pos, y + (h - logical_rect.height)/2,
367 clip.x = clip.x + clip.width;
368 clip.width = w - clip.width;
370 gtk_paint_layout (widget->style, window,
371 is_rtl ? GTK_STATE_SELECTED : GTK_STATE_NORMAL,
372 FALSE, &clip, widget, "progressbar",
373 x + pos, y + (h - logical_rect.height)/2,
376 g_object_unref (layout);
380 #define __GTK_CELL_RENDERER_PROGRESS_C__
381 #include "gtkaliasdef.c"