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 "gtkorientable.h"
33 #include "gtkprivate.h"
49 struct _GtkCellRendererProgressPrivate
60 GtkOrientation orientation;
64 static void gtk_cell_renderer_progress_finalize (GObject *object);
65 static void gtk_cell_renderer_progress_get_property (GObject *object,
69 static void gtk_cell_renderer_progress_set_property (GObject *object,
73 static void gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
75 static void gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
77 static void gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
79 static void compute_dimensions (GtkCellRenderer *cell,
84 static void gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
86 const GdkRectangle *cell_area,
91 static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
94 const GdkRectangle *background_area,
95 const GdkRectangle *cell_area,
96 GtkCellRendererState flags);
99 G_DEFINE_TYPE_WITH_CODE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER,
100 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
103 gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
105 GObjectClass *object_class = G_OBJECT_CLASS (klass);
106 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
108 object_class->finalize = gtk_cell_renderer_progress_finalize;
109 object_class->get_property = gtk_cell_renderer_progress_get_property;
110 object_class->set_property = gtk_cell_renderer_progress_set_property;
112 cell_class->get_size = gtk_cell_renderer_progress_get_size;
113 cell_class->render = gtk_cell_renderer_progress_render;
116 * GtkCellRendererProgress:value:
118 * The "value" property determines the percentage to which the
119 * progress bar will be "filled in".
123 g_object_class_install_property (object_class,
125 g_param_spec_int ("value",
127 P_("Value of the progress bar"),
129 GTK_PARAM_READWRITE));
132 * GtkCellRendererProgress:text:
134 * The "text" property determines the label which will be drawn
135 * over the progress bar. Setting this property to %NULL causes the default
136 * label to be displayed. Setting this property to an empty string causes
137 * no label to be displayed.
141 g_object_class_install_property (object_class,
143 g_param_spec_string ("text",
145 P_("Text on the progress bar"),
147 GTK_PARAM_READWRITE));
150 * GtkCellRendererProgress:pulse:
152 * Setting this to a non-negative value causes the cell renderer to
153 * enter "activity mode", where a block bounces back and forth to
154 * indicate that some progress is made, without specifying exactly how
157 * Each increment of the property causes the block to move by a little
160 * To indicate that the activity has not started yet, set the property
161 * to zero. To indicate completion, set the property to %G_MAXINT.
165 g_object_class_install_property (object_class,
167 g_param_spec_int ("pulse",
169 P_("Set this to positive values to indicate that some progress is made, but you don't know how much."),
171 GTK_PARAM_READWRITE));
174 * GtkCellRendererProgress:text-xalign:
176 * The "text-xalign" property controls the horizontal alignment of the
177 * text in the progress bar. Valid values range from 0 (left) to 1
178 * (right). Reserved for RTL layouts.
182 g_object_class_install_property (object_class,
184 g_param_spec_float ("text-xalign",
185 P_("Text x alignment"),
186 P_("The horizontal text alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
188 GTK_PARAM_READWRITE));
191 * GtkCellRendererProgress:text-yalign:
193 * The "text-yalign" property controls the vertical alignment of the
194 * text in the progress bar. Valid values range from 0 (top) to 1
199 g_object_class_install_property (object_class,
201 g_param_spec_float ("text-yalign",
202 P_("Text y alignment"),
203 P_("The vertical text alignment, from 0 (top) to 1 (bottom)."),
205 GTK_PARAM_READWRITE));
207 g_object_class_override_property (object_class,
211 g_object_class_install_property (object_class,
213 g_param_spec_boolean ("inverted",
215 P_("Invert the direction in which the progress bar grows"),
217 GTK_PARAM_READWRITE));
219 g_type_class_add_private (object_class,
220 sizeof (GtkCellRendererProgressPrivate));
224 gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
226 GtkCellRendererProgressPrivate *priv;
228 cellprogress->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellprogress,
229 GTK_TYPE_CELL_RENDERER_PROGRESS,
230 GtkCellRendererProgressPrivate);
231 priv = cellprogress->priv;
241 priv->text_xalign = 0.5;
242 priv->text_yalign = 0.5;
244 priv->orientation = GTK_ORIENTATION_HORIZONTAL,
245 priv->inverted = FALSE;
250 * gtk_cell_renderer_progress_new:
252 * Creates a new #GtkCellRendererProgress.
254 * Return value: the new cell renderer
259 gtk_cell_renderer_progress_new (void)
261 return g_object_new (GTK_TYPE_CELL_RENDERER_PROGRESS, NULL);
265 gtk_cell_renderer_progress_finalize (GObject *object)
267 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
268 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
271 g_free (priv->label);
273 G_OBJECT_CLASS (gtk_cell_renderer_progress_parent_class)->finalize (object);
277 gtk_cell_renderer_progress_get_property (GObject *object,
282 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
283 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
288 g_value_set_int (value, priv->value);
291 g_value_set_string (value, priv->text);
294 g_value_set_int (value, priv->pulse);
296 case PROP_TEXT_XALIGN:
297 g_value_set_float (value, priv->text_xalign);
299 case PROP_TEXT_YALIGN:
300 g_value_set_float (value, priv->text_yalign);
302 case PROP_ORIENTATION:
303 g_value_set_enum (value, priv->orientation);
306 g_value_set_boolean (value, priv->inverted);
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
314 gtk_cell_renderer_progress_set_property (GObject *object,
319 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
320 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
325 gtk_cell_renderer_progress_set_value (cellprogress,
326 g_value_get_int (value));
329 gtk_cell_renderer_progress_set_text (cellprogress,
330 g_value_get_string (value));
333 gtk_cell_renderer_progress_set_pulse (cellprogress,
334 g_value_get_int (value));
336 case PROP_TEXT_XALIGN:
337 priv->text_xalign = g_value_get_float (value);
339 case PROP_TEXT_YALIGN:
340 priv->text_yalign = g_value_get_float (value);
342 case PROP_ORIENTATION:
343 priv->orientation = g_value_get_enum (value);
346 priv->orientation = g_value_get_boolean (value);
349 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
354 recompute_label (GtkCellRendererProgress *cellprogress)
356 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
360 label = g_strdup (priv->text);
361 else if (priv->pulse < 0)
362 label = g_strdup_printf (C_("progress bar label", "%d %%"), priv->value);
366 g_free (priv->label);
371 gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
374 cellprogress->priv->value = value;
376 recompute_label (cellprogress);
380 gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
385 new_text = g_strdup (text);
386 g_free (cellprogress->priv->text);
387 cellprogress->priv->text = new_text;
389 recompute_label (cellprogress);
393 gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
396 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
398 if (pulse != priv->pulse)
403 priv->offset = pulse;
408 recompute_label (cellprogress);
412 compute_dimensions (GtkCellRenderer *cell,
418 PangoRectangle logical_rect;
422 layout = gtk_widget_create_pango_layout (widget, text);
423 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
425 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
428 *width = logical_rect.width + xpad * 2;
431 *height = logical_rect.height + ypad * 2;
433 g_object_unref (layout);
437 gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
439 const GdkRectangle *cell_area,
445 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
446 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
452 text = g_strdup_printf (C_("progress bar label", "%d %%"), 100);
453 compute_dimensions (cell, widget, text,
459 compute_dimensions (cell, widget, priv->label, &w, &h);
462 *width = MAX (priv->min_w, w);
465 *height = MIN (priv->min_h, h);
467 /* FIXME: at the moment cell_area is only set when we are requesting
468 * the size for drawing the focus rectangle. We now just return
469 * the last size we used for drawing the progress bar, which will
470 * work for now. Not a really nice solution though.
475 *width = cell_area->width;
477 *height = cell_area->height;
480 if (x_offset) *x_offset = 0;
481 if (y_offset) *y_offset = 0;
485 get_bar_size (gint pulse,
492 bar_size = full_size * MAX (0, value) / 100;
495 else if (pulse == G_MAXINT)
496 bar_size = full_size;
498 bar_size = MAX (2, full_size / 5);
504 get_bar_position (gint start,
513 if (pulse < 0 || pulse == 0 || pulse == G_MAXINT)
515 position = is_rtl ? (start + full_size - bar_size) : start;
519 position = (is_rtl ? offset + 12 : offset) % 24;
521 position = 24 - position;
522 position = start + full_size * position / 15;
529 gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
532 const GdkRectangle *background_area,
533 const GdkRectangle *cell_area,
534 GtkCellRendererState flags)
536 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
537 GtkCellRendererProgressPrivate *priv= cellprogress->priv;
540 PangoRectangle logical_rect;
541 gint x, y, w, h, x_pos, y_pos, bar_position, bar_size, start, full_size;
546 style = gtk_widget_get_style (widget);
548 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
550 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
551 x = cell_area->x + xpad;
552 y = cell_area->y + ypad;
553 w = cell_area->width - xpad * 2;
554 h = cell_area->height - ypad * 2;
556 /* FIXME: GtkProgressBar draws the box with "trough" detail,
557 * but some engines don't paint anything with that detail for
558 * non-GtkProgressBar widgets.
560 gtk_paint_box (style,
562 GTK_STATE_NORMAL, GTK_SHADOW_IN,
566 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
574 bar_size = get_bar_size (priv->pulse, priv->value, full_size);
577 bar_position = get_bar_position (start, full_size, bar_size,
578 priv->pulse, priv->offset, is_rtl);
580 bar_position = get_bar_position (start, full_size, bar_size,
581 priv->pulse, priv->offset, !is_rtl);
583 clip.width = bar_size;
584 clip.x = bar_position;
594 bar_size = get_bar_size (priv->pulse, priv->value, full_size);
597 bar_position = get_bar_position (start, full_size, bar_size,
598 priv->pulse, priv->offset, TRUE);
600 bar_position = get_bar_position (start, full_size, bar_size,
601 priv->pulse, priv->offset, FALSE);
603 clip.height = bar_size;
604 clip.y = bar_position;
608 gtk_paint_box (style,
610 GTK_STATE_SELECTED, GTK_SHADOW_OUT,
613 clip.width, clip.height);
619 layout = gtk_widget_create_pango_layout (widget, priv->label);
620 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
622 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
623 text_xalign = 1.0 - priv->text_xalign;
625 text_xalign = priv->text_xalign;
627 x_pos = x + style->xthickness + text_xalign *
628 (w - 2 * style->xthickness - logical_rect.width);
630 y_pos = y + style->ythickness + priv->text_yalign *
631 (h - 2 * style->ythickness - logical_rect.height);
634 gdk_cairo_rectangle (cr, &clip);
637 gtk_paint_layout (style, cr,
639 FALSE, widget, "progressbar",
645 if (bar_position > start)
647 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
650 clip.width = bar_position - x;
655 clip.height = bar_position - y;
659 gdk_cairo_rectangle (cr, &clip);
662 gtk_paint_layout (style, cr,
664 FALSE, widget, "progressbar",
671 if (bar_position + bar_size < start + full_size)
673 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
675 clip.x = bar_position + bar_size;
676 clip.width = x + w - (bar_position + bar_size);
680 clip.y = bar_position + bar_size;
681 clip.height = y + h - (bar_position + bar_size);
685 gdk_cairo_rectangle (cr, &clip);
688 gtk_paint_layout (style, cr,
690 FALSE, widget, "progressbar",
697 g_object_unref (layout);