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, see <http://www.gnu.org/licenses/>.
20 * Modified by the GTK+ Team and others 1997-2007. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gtkcellrendererprogress.h"
30 #include "gtkorientable.h"
31 #include "gtkprivate.h"
36 * SECTION:gtkcellrendererprogress
37 * @Short_description: Renders numbers as progress bars
38 * @Title: GtkCellRendererProgress
40 * #GtkCellRendererProgress renders a numeric value as a progress par in a cell.
41 * Additionally, it can display a text on top of the progress bar.
43 * The #GtkCellRendererProgress cell renderer was added in GTK+ 2.6.
59 struct _GtkCellRendererProgressPrivate
70 GtkOrientation orientation;
74 static void gtk_cell_renderer_progress_finalize (GObject *object);
75 static void gtk_cell_renderer_progress_get_property (GObject *object,
79 static void gtk_cell_renderer_progress_set_property (GObject *object,
83 static void gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
85 static void gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
87 static void gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
89 static void compute_dimensions (GtkCellRenderer *cell,
94 static void gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
96 const GdkRectangle *cell_area,
101 static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
104 const GdkRectangle *background_area,
105 const GdkRectangle *cell_area,
106 GtkCellRendererState flags);
109 G_DEFINE_TYPE_WITH_CODE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER,
110 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
113 gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
115 GObjectClass *object_class = G_OBJECT_CLASS (klass);
116 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
118 object_class->finalize = gtk_cell_renderer_progress_finalize;
119 object_class->get_property = gtk_cell_renderer_progress_get_property;
120 object_class->set_property = gtk_cell_renderer_progress_set_property;
122 cell_class->get_size = gtk_cell_renderer_progress_get_size;
123 cell_class->render = gtk_cell_renderer_progress_render;
126 * GtkCellRendererProgress:value:
128 * The "value" property determines the percentage to which the
129 * progress bar will be "filled in".
133 g_object_class_install_property (object_class,
135 g_param_spec_int ("value",
137 P_("Value of the progress bar"),
139 GTK_PARAM_READWRITE));
142 * GtkCellRendererProgress:text:
144 * The "text" property determines the label which will be drawn
145 * over the progress bar. Setting this property to %NULL causes the default
146 * label to be displayed. Setting this property to an empty string causes
147 * no label to be displayed.
151 g_object_class_install_property (object_class,
153 g_param_spec_string ("text",
155 P_("Text on the progress bar"),
157 GTK_PARAM_READWRITE));
160 * GtkCellRendererProgress:pulse:
162 * Setting this to a non-negative value causes the cell renderer to
163 * enter "activity mode", where a block bounces back and forth to
164 * indicate that some progress is made, without specifying exactly how
167 * Each increment of the property causes the block to move by a little
170 * To indicate that the activity has not started yet, set the property
171 * to zero. To indicate completion, set the property to %G_MAXINT.
175 g_object_class_install_property (object_class,
177 g_param_spec_int ("pulse",
179 P_("Set this to positive values to indicate that some progress is made, but you don't know how much."),
181 GTK_PARAM_READWRITE));
184 * GtkCellRendererProgress:text-xalign:
186 * The "text-xalign" property controls the horizontal alignment of the
187 * text in the progress bar. Valid values range from 0 (left) to 1
188 * (right). Reserved for RTL layouts.
192 g_object_class_install_property (object_class,
194 g_param_spec_float ("text-xalign",
195 P_("Text x alignment"),
196 P_("The horizontal text alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
198 GTK_PARAM_READWRITE));
201 * GtkCellRendererProgress:text-yalign:
203 * The "text-yalign" property controls the vertical alignment of the
204 * text in the progress bar. Valid values range from 0 (top) to 1
209 g_object_class_install_property (object_class,
211 g_param_spec_float ("text-yalign",
212 P_("Text y alignment"),
213 P_("The vertical text alignment, from 0 (top) to 1 (bottom)."),
215 GTK_PARAM_READWRITE));
217 g_object_class_override_property (object_class,
221 g_object_class_install_property (object_class,
223 g_param_spec_boolean ("inverted",
225 P_("Invert the direction in which the progress bar grows"),
227 GTK_PARAM_READWRITE));
229 g_type_class_add_private (object_class,
230 sizeof (GtkCellRendererProgressPrivate));
234 gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
236 GtkCellRendererProgressPrivate *priv;
238 cellprogress->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellprogress,
239 GTK_TYPE_CELL_RENDERER_PROGRESS,
240 GtkCellRendererProgressPrivate);
241 priv = cellprogress->priv;
251 priv->text_xalign = 0.5;
252 priv->text_yalign = 0.5;
254 priv->orientation = GTK_ORIENTATION_HORIZONTAL,
255 priv->inverted = FALSE;
260 * gtk_cell_renderer_progress_new:
262 * Creates a new #GtkCellRendererProgress.
264 * Return value: the new cell renderer
269 gtk_cell_renderer_progress_new (void)
271 return g_object_new (GTK_TYPE_CELL_RENDERER_PROGRESS, NULL);
275 gtk_cell_renderer_progress_finalize (GObject *object)
277 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
278 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
281 g_free (priv->label);
283 G_OBJECT_CLASS (gtk_cell_renderer_progress_parent_class)->finalize (object);
287 gtk_cell_renderer_progress_get_property (GObject *object,
292 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
293 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
298 g_value_set_int (value, priv->value);
301 g_value_set_string (value, priv->text);
304 g_value_set_int (value, priv->pulse);
306 case PROP_TEXT_XALIGN:
307 g_value_set_float (value, priv->text_xalign);
309 case PROP_TEXT_YALIGN:
310 g_value_set_float (value, priv->text_yalign);
312 case PROP_ORIENTATION:
313 g_value_set_enum (value, priv->orientation);
316 g_value_set_boolean (value, priv->inverted);
319 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
324 gtk_cell_renderer_progress_set_property (GObject *object,
329 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
330 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
335 gtk_cell_renderer_progress_set_value (cellprogress,
336 g_value_get_int (value));
339 gtk_cell_renderer_progress_set_text (cellprogress,
340 g_value_get_string (value));
343 gtk_cell_renderer_progress_set_pulse (cellprogress,
344 g_value_get_int (value));
346 case PROP_TEXT_XALIGN:
347 priv->text_xalign = g_value_get_float (value);
349 case PROP_TEXT_YALIGN:
350 priv->text_yalign = g_value_get_float (value);
352 case PROP_ORIENTATION:
353 priv->orientation = g_value_get_enum (value);
356 priv->inverted = g_value_get_boolean (value);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
364 recompute_label (GtkCellRendererProgress *cellprogress)
366 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
370 label = g_strdup (priv->text);
371 else if (priv->pulse < 0)
372 label = g_strdup_printf (C_("progress bar label", "%d %%"), priv->value);
376 g_free (priv->label);
381 gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
384 cellprogress->priv->value = value;
386 recompute_label (cellprogress);
390 gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
395 new_text = g_strdup (text);
396 g_free (cellprogress->priv->text);
397 cellprogress->priv->text = new_text;
399 recompute_label (cellprogress);
403 gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
406 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
408 if (pulse != priv->pulse)
413 priv->offset = pulse;
418 recompute_label (cellprogress);
422 compute_dimensions (GtkCellRenderer *cell,
428 PangoRectangle logical_rect;
432 layout = gtk_widget_create_pango_layout (widget, text);
433 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
435 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
438 *width = logical_rect.width + xpad * 2;
441 *height = logical_rect.height + ypad * 2;
443 g_object_unref (layout);
447 gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
449 const GdkRectangle *cell_area,
455 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
456 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
462 text = g_strdup_printf (C_("progress bar label", "%d %%"), 100);
463 compute_dimensions (cell, widget, text,
469 compute_dimensions (cell, widget, priv->label, &w, &h);
472 *width = MAX (priv->min_w, w);
475 *height = MIN (priv->min_h, h);
477 /* FIXME: at the moment cell_area is only set when we are requesting
478 * the size for drawing the focus rectangle. We now just return
479 * the last size we used for drawing the progress bar, which will
480 * work for now. Not a really nice solution though.
485 *width = cell_area->width;
487 *height = cell_area->height;
490 if (x_offset) *x_offset = 0;
491 if (y_offset) *y_offset = 0;
495 get_bar_size (gint pulse,
502 bar_size = full_size * MAX (0, value) / 100;
505 else if (pulse == G_MAXINT)
506 bar_size = full_size;
508 bar_size = MAX (2, full_size / 5);
514 get_bar_position (gint start,
523 if (pulse < 0 || pulse == 0 || pulse == G_MAXINT)
525 position = is_rtl ? (start + full_size - bar_size) : start;
529 position = (is_rtl ? offset + 12 : offset) % 24;
531 position = 24 - position;
532 position = start + full_size * position / 15;
539 gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
542 const GdkRectangle *background_area,
543 const GdkRectangle *cell_area,
544 GtkCellRendererState flags)
546 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
547 GtkCellRendererProgressPrivate *priv= cellprogress->priv;
548 GtkStyleContext *context;
551 PangoRectangle logical_rect;
552 gint x, y, w, h, x_pos, y_pos, bar_position, bar_size, start, full_size;
557 context = gtk_widget_get_style_context (widget);
558 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
560 gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
561 x = cell_area->x + xpad;
562 y = cell_area->y + ypad;
563 w = cell_area->width - xpad * 2;
564 h = cell_area->height - ypad * 2;
566 gtk_style_context_save (context);
567 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
569 gtk_render_background (context, cr, x, y, w, h);
570 gtk_render_frame (context, cr, x, y, w, h);
572 gtk_style_context_get_padding (context, GTK_STATE_FLAG_NORMAL, &padding);
576 w -= padding.left + padding.right;
577 h -= padding.top + padding.bottom;
579 gtk_style_context_restore (context);
581 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
589 bar_size = get_bar_size (priv->pulse, priv->value, full_size);
592 bar_position = get_bar_position (start, full_size, bar_size,
593 priv->pulse, priv->offset, is_rtl);
595 bar_position = get_bar_position (start, full_size, bar_size,
596 priv->pulse, priv->offset, !is_rtl);
598 clip.width = bar_size;
599 clip.x = bar_position;
609 bar_size = get_bar_size (priv->pulse, priv->value, full_size);
612 bar_position = get_bar_position (start, full_size, bar_size,
613 priv->pulse, priv->offset, TRUE);
615 bar_position = get_bar_position (start, full_size, bar_size,
616 priv->pulse, priv->offset, FALSE);
618 clip.height = bar_size;
619 clip.y = bar_position;
622 gtk_style_context_save (context);
623 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
626 gtk_render_activity (context, cr,
628 clip.width, clip.height);
630 gtk_style_context_restore (context);
636 layout = gtk_widget_create_pango_layout (widget, priv->label);
637 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
639 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
640 text_xalign = 1.0 - priv->text_xalign;
642 text_xalign = priv->text_xalign;
644 x_pos = x + padding.left + text_xalign *
645 (w - padding.left - padding.right - logical_rect.width);
647 y_pos = y + padding.top + priv->text_yalign *
648 (h - padding.top - padding.bottom - logical_rect.height);
651 gdk_cairo_rectangle (cr, &clip);
654 gtk_style_context_save (context);
655 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
657 gtk_render_layout (context, cr,
661 gtk_style_context_restore (context);
664 gtk_style_context_save (context);
665 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
667 if (bar_position > start)
669 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
672 clip.width = bar_position - x;
677 clip.height = bar_position - y;
681 gdk_cairo_rectangle (cr, &clip);
684 gtk_render_layout (context, cr,
691 if (bar_position + bar_size < start + full_size)
693 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
695 clip.x = bar_position + bar_size;
696 clip.width = x + w - (bar_position + bar_size);
700 clip.y = bar_position + bar_size;
701 clip.height = y + h - (bar_position + bar_size);
705 gdk_cairo_rectangle (cr, &clip);
708 gtk_render_layout (context, cr,
715 gtk_style_context_restore (context);
716 g_object_unref (layout);