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"
32 #include "gtkprivate.h"
36 #define GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), \
37 GTK_TYPE_CELL_RENDERER_PROGRESS, \
38 GtkCellRendererProgressPrivate))
47 struct _GtkCellRendererProgressPrivate
56 static void gtk_cell_renderer_progress_finalize (GObject *object);
57 static void gtk_cell_renderer_progress_get_property (GObject *object,
61 static void gtk_cell_renderer_progress_set_property (GObject *object,
65 static void gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
67 static void gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
69 static void compute_dimensions (GtkCellRenderer *cell,
74 static void gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
76 GdkRectangle *cell_area,
81 static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
84 GdkRectangle *background_area,
85 GdkRectangle *cell_area,
86 GdkRectangle *expose_area,
90 G_DEFINE_TYPE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER);
93 gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
95 GObjectClass *object_class = G_OBJECT_CLASS (klass);
96 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
98 object_class->finalize = gtk_cell_renderer_progress_finalize;
99 object_class->get_property = gtk_cell_renderer_progress_get_property;
100 object_class->set_property = gtk_cell_renderer_progress_set_property;
102 cell_class->get_size = gtk_cell_renderer_progress_get_size;
103 cell_class->render = gtk_cell_renderer_progress_render;
106 * GtkCellRendererProgress:value:
108 * The "value" property determines the percentage to which the
109 * progress bar will be "filled in".
113 g_object_class_install_property (object_class,
115 g_param_spec_int ("value",
117 P_("Value of the progress bar"),
119 GTK_PARAM_READWRITE));
122 * GtkCellRendererProgress:text:
124 * The "text" property determines the label which will be drawn
125 * over the progress bar. Setting this property to %NULL causes the default
126 * label to be displayed. Setting this property to an empty string causes
127 * no label to be displayed.
131 g_object_class_install_property (object_class,
133 g_param_spec_string ("text",
135 P_("Text on the progress bar"),
137 GTK_PARAM_READWRITE));
139 g_type_class_add_private (object_class,
140 sizeof (GtkCellRendererProgressPrivate));
144 gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
146 cellprogress->priv = GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE (cellprogress);
147 cellprogress->priv->value = 0;
148 cellprogress->priv->text = NULL;
149 cellprogress->priv->label = NULL;
150 cellprogress->priv->min_w = -1;
151 cellprogress->priv->min_h = -1;
156 * gtk_cell_renderer_progress_new:
158 * Creates a new #GtkCellRendererProgress.
160 * Return value: the new cell renderer
165 gtk_cell_renderer_progress_new (void)
167 return g_object_new (GTK_TYPE_CELL_RENDERER_PROGRESS, NULL);
171 gtk_cell_renderer_progress_finalize (GObject *object)
173 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
175 g_free (cellprogress->priv->text);
176 g_free (cellprogress->priv->label);
178 G_OBJECT_CLASS (gtk_cell_renderer_progress_parent_class)->finalize (object);
182 gtk_cell_renderer_progress_get_property (GObject *object,
187 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
192 g_value_set_int (value, cellprogress->priv->value);
195 g_value_set_string (value, cellprogress->priv->text);
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
203 gtk_cell_renderer_progress_set_property (GObject *object,
208 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
213 gtk_cell_renderer_progress_set_value (cellprogress,
214 g_value_get_int (value));
217 gtk_cell_renderer_progress_set_text (cellprogress,
218 g_value_get_string (value));
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
226 gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
231 cellprogress->priv->value = value;
233 if (cellprogress->priv->text)
234 text = g_strdup (cellprogress->priv->text);
236 /* do not translate the part before the | */
237 text = g_strdup_printf (Q_("progress bar label|%d %%"),
238 cellprogress->priv->value);
240 g_free (cellprogress->priv->label);
241 cellprogress->priv->label = text;
245 gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
250 new_text = g_strdup (text);
251 g_free (cellprogress->priv->text);
252 cellprogress->priv->text = new_text;
254 /* Update the label */
255 gtk_cell_renderer_progress_set_value (cellprogress, cellprogress->priv->value);
259 compute_dimensions (GtkCellRenderer *cell,
265 PangoRectangle logical_rect;
268 layout = gtk_widget_create_pango_layout (widget, text);
269 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
272 *width = logical_rect.width + cell->xpad * 2 + widget->style->xthickness * 2;
275 *height = logical_rect.height + cell->ypad * 2 + widget->style->ythickness * 2;
277 g_object_unref (layout);
281 gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
283 GdkRectangle *cell_area,
289 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
293 if (cellprogress->priv->min_w < 0)
295 text = g_strdup_printf (Q_("progress bar label|%d %%"), 100);
296 compute_dimensions (cell, widget, text,
297 &cellprogress->priv->min_w,
298 &cellprogress->priv->min_h);
302 compute_dimensions (cell, widget, cellprogress->priv->label, &w, &h);
305 *width = MAX (cellprogress->priv->min_w, w);
308 *height = MIN (cellprogress->priv->min_h, h);
310 /* FIXME: at the moment cell_area is only set when we are requesting
311 * the size for drawing the focus rectangle. We now just return
312 * the last size we used for drawing the progress bar, which will
313 * work for now. Not a really nice solution though.
318 *width = cell_area->width;
320 *height = cell_area->height;
325 gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
328 GdkRectangle *background_area,
329 GdkRectangle *cell_area,
330 GdkRectangle *expose_area,
333 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
335 PangoRectangle logical_rect;
336 gint x, y, w, h, perc_w, pos;
341 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
343 cr = gdk_cairo_create (window);
345 x = cell_area->x + cell->xpad;
346 y = cell_area->y + cell->ypad;
348 w = cell_area->width - cell->xpad * 2;
349 h = cell_area->height - cell->ypad * 2;
351 cairo_rectangle (cr, x, y, w, h);
352 gdk_cairo_set_source_color (cr, &widget->style->fg[GTK_STATE_NORMAL]);
355 x += widget->style->xthickness;
356 y += widget->style->ythickness;
357 w -= widget->style->xthickness * 2;
358 h -= widget->style->ythickness * 2;
360 cairo_rectangle (cr, x, y, w, h);
361 gdk_cairo_set_source_color (cr, &widget->style->bg[GTK_STATE_NORMAL]);
364 perc_w = w * MAX (0, cellprogress->priv->value) / 100;
366 cairo_rectangle (cr, is_rtl ? (x + w - perc_w) : x, y, perc_w, h);
367 gdk_cairo_set_source_color (cr, &widget->style->bg[GTK_STATE_SELECTED]);
370 layout = gtk_widget_create_pango_layout (widget, cellprogress->priv->label);
371 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
373 pos = (w - logical_rect.width)/2;
377 clip.width = is_rtl ? w - perc_w : perc_w;
380 gtk_paint_layout (widget->style, window,
381 is_rtl ? GTK_STATE_NORMAL : GTK_STATE_SELECTED,
382 FALSE, &clip, widget, "progressbar",
383 x + pos, y + (h - logical_rect.height)/2,
386 clip.x = clip.x + clip.width;
387 clip.width = w - clip.width;
389 gtk_paint_layout (widget->style, window,
390 is_rtl ? GTK_STATE_SELECTED : GTK_STATE_NORMAL,
391 FALSE, &clip, widget, "progressbar",
392 x + pos, y + (h - logical_rect.height)/2,
395 g_object_unref (layout);
399 #define __GTK_CELL_RENDERER_PROGRESS_C__
400 #include "gtkaliasdef.c"