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-2007. 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))
48 struct _GtkCellRendererProgressPrivate
59 static void gtk_cell_renderer_progress_finalize (GObject *object);
60 static void gtk_cell_renderer_progress_get_property (GObject *object,
64 static void gtk_cell_renderer_progress_set_property (GObject *object,
68 static void gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
70 static void gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
72 static void gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
74 static void compute_dimensions (GtkCellRenderer *cell,
79 static void gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
81 GdkRectangle *cell_area,
86 static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
89 GdkRectangle *background_area,
90 GdkRectangle *cell_area,
91 GdkRectangle *expose_area,
95 G_DEFINE_TYPE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER)
98 gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
100 GObjectClass *object_class = G_OBJECT_CLASS (klass);
101 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
103 object_class->finalize = gtk_cell_renderer_progress_finalize;
104 object_class->get_property = gtk_cell_renderer_progress_get_property;
105 object_class->set_property = gtk_cell_renderer_progress_set_property;
107 cell_class->get_size = gtk_cell_renderer_progress_get_size;
108 cell_class->render = gtk_cell_renderer_progress_render;
111 * GtkCellRendererProgress:value:
113 * The "value" property determines the percentage to which the
114 * progress bar will be "filled in".
118 g_object_class_install_property (object_class,
120 g_param_spec_int ("value",
122 P_("Value of the progress bar"),
124 GTK_PARAM_READWRITE));
127 * GtkCellRendererProgress:text:
129 * The "text" property determines the label which will be drawn
130 * over the progress bar. Setting this property to %NULL causes the default
131 * label to be displayed. Setting this property to an empty string causes
132 * no label to be displayed.
136 g_object_class_install_property (object_class,
138 g_param_spec_string ("text",
140 P_("Text on the progress bar"),
142 GTK_PARAM_READWRITE));
145 * GtkCellRendererProgress:pulse:
147 * Setting this to a non-negative value causes the cell renderer to
148 * enter "activity mode", where a block bounces back and forth to
149 * indicate that some progress is made, with specifying exactly how
152 * Each increment of the property causes the block to move by a little
155 * To indicate that the activity has not started yet, set the property
156 * to zero. To indicate completion, set the property to %G_MAXINT.
160 g_object_class_install_property (object_class,
162 g_param_spec_int ("pulse",
164 P_("Set this to positive values to indicate that some progress is made, but you don't know how much."),
166 GTK_PARAM_READWRITE));
168 g_type_class_add_private (object_class,
169 sizeof (GtkCellRendererProgressPrivate));
173 gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
175 GtkCellRendererProgressPrivate *priv = GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE (cellprogress);
185 cellprogress->priv = priv;
190 * gtk_cell_renderer_progress_new:
192 * Creates a new #GtkCellRendererProgress.
194 * Return value: the new cell renderer
199 gtk_cell_renderer_progress_new (void)
201 return g_object_new (GTK_TYPE_CELL_RENDERER_PROGRESS, NULL);
205 gtk_cell_renderer_progress_finalize (GObject *object)
207 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
208 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
211 g_free (priv->label);
213 G_OBJECT_CLASS (gtk_cell_renderer_progress_parent_class)->finalize (object);
217 gtk_cell_renderer_progress_get_property (GObject *object,
222 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
223 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
228 g_value_set_int (value, priv->value);
231 g_value_set_string (value, priv->text);
234 g_value_set_int (value, priv->pulse);
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
242 gtk_cell_renderer_progress_set_property (GObject *object,
247 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
252 gtk_cell_renderer_progress_set_value (cellprogress,
253 g_value_get_int (value));
256 gtk_cell_renderer_progress_set_text (cellprogress,
257 g_value_get_string (value));
260 gtk_cell_renderer_progress_set_pulse (cellprogress,
261 g_value_get_int (value));
264 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
269 recompute_label (GtkCellRendererProgress *cellprogress)
271 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
275 label = g_strdup (priv->text);
276 else if (priv->pulse < 0)
277 /* do not translate the part before the | */
278 label = g_strdup_printf (Q_("progress bar label|%d %%"), priv->value);
282 g_free (priv->label);
287 gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
290 cellprogress->priv->value = value;
292 recompute_label (cellprogress);
296 gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
301 new_text = g_strdup (text);
302 g_free (cellprogress->priv->text);
303 cellprogress->priv->text = new_text;
305 recompute_label (cellprogress);
309 gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
312 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
314 if (pulse != priv->pulse)
316 if (priv->pulse <= 0)
324 recompute_label (cellprogress);
328 compute_dimensions (GtkCellRenderer *cell,
334 PangoRectangle logical_rect;
337 layout = gtk_widget_create_pango_layout (widget, text);
338 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
341 *width = logical_rect.width + cell->xpad * 2;
344 *height = logical_rect.height + cell->ypad * 2;
346 g_object_unref (layout);
350 gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
352 GdkRectangle *cell_area,
358 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
359 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
365 text = g_strdup_printf (Q_("progress bar label|%d %%"), 100);
366 compute_dimensions (cell, widget, text,
372 compute_dimensions (cell, widget, priv->label, &w, &h);
375 *width = MAX (priv->min_w, w);
378 *height = MIN (priv->min_h, h);
380 /* FIXME: at the moment cell_area is only set when we are requesting
381 * the size for drawing the focus rectangle. We now just return
382 * the last size we used for drawing the progress bar, which will
383 * work for now. Not a really nice solution though.
388 *width = cell_area->width;
390 *height = cell_area->height;
393 if (x_offset) *x_offset = 0;
394 if (y_offset) *y_offset = 0;
398 gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
401 GdkRectangle *background_area,
402 GdkRectangle *cell_area,
403 GdkRectangle *expose_area,
406 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
407 GtkCellRendererProgressPrivate *priv= cellprogress->priv;
409 PangoRectangle logical_rect;
410 gint x, y, w, h, x_pos, y_pos, bar_x, bar_width;
414 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
416 x = cell_area->x + cell->xpad;
417 y = cell_area->y + cell->ypad;
418 w = cell_area->width - cell->xpad * 2;
419 h = cell_area->height - cell->ypad * 2;
421 /* FIXME: GtkProgressBar draws the box with "trough" detail,
422 * but some engines don't paint anything with that detail for
423 * non-GtkProgressBar widgets.
425 gtk_paint_box (widget->style,
427 GTK_STATE_NORMAL, GTK_SHADOW_IN,
435 clip.width = w * MAX (0, priv->value) / 100;
436 clip.x = is_rtl ? (x + w - clip.width) : x;
438 else if (priv->pulse == 0)
443 else if (priv->pulse == G_MAXINT)
450 clip.width = MAX (2, w / 5);
451 x_pos = (is_rtl ? priv->offset + 12 : priv->offset) % 24;
454 clip.x = x + w * x_pos / 15;
457 gtk_paint_box (widget->style,
459 GTK_STATE_SELECTED, GTK_SHADOW_OUT,
460 &clip, widget, "bar",
462 clip.width, clip.height);
466 layout = gtk_widget_create_pango_layout (widget, priv->label);
467 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
469 x_pos = x + (w - logical_rect.width) / 2;
470 y_pos = y + (h - logical_rect.height) / 2;
472 gtk_paint_layout (widget->style, window,
474 FALSE, &clip, widget, "progressbar",
479 bar_width = clip.width;
483 clip.width = bar_x - x;
485 gtk_paint_layout (widget->style, window,
487 FALSE, &clip, widget, "progressbar",
492 if (bar_x + bar_width < x + w)
494 clip.x = bar_x + bar_width;
495 clip.width = x + w - (bar_x + bar_width);
497 gtk_paint_layout (widget->style, window,
499 FALSE, &clip, widget, "progressbar",
504 g_object_unref (layout);
508 #define __GTK_CELL_RENDERER_PROGRESS_C__
509 #include "gtkaliasdef.c"