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 "gtkprogressbar.h"
33 #include "gtkprivate.h"
37 #define GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), \
38 GTK_TYPE_CELL_RENDERER_PROGRESS, \
39 GtkCellRendererProgressPrivate))
52 struct _GtkCellRendererProgressPrivate
63 GtkProgressBarOrientation orientation;
66 static void gtk_cell_renderer_progress_finalize (GObject *object);
67 static void gtk_cell_renderer_progress_get_property (GObject *object,
71 static void gtk_cell_renderer_progress_set_property (GObject *object,
75 static void gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
77 static void gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
79 static void gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
81 static void compute_dimensions (GtkCellRenderer *cell,
86 static void gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
88 GdkRectangle *cell_area,
93 static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
96 GdkRectangle *background_area,
97 GdkRectangle *cell_area,
98 GdkRectangle *expose_area,
102 G_DEFINE_TYPE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER)
105 gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
107 GObjectClass *object_class = G_OBJECT_CLASS (klass);
108 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
110 object_class->finalize = gtk_cell_renderer_progress_finalize;
111 object_class->get_property = gtk_cell_renderer_progress_get_property;
112 object_class->set_property = gtk_cell_renderer_progress_set_property;
114 cell_class->get_size = gtk_cell_renderer_progress_get_size;
115 cell_class->render = gtk_cell_renderer_progress_render;
118 * GtkCellRendererProgress:value:
120 * The "value" property determines the percentage to which the
121 * progress bar will be "filled in".
125 g_object_class_install_property (object_class,
127 g_param_spec_int ("value",
129 P_("Value of the progress bar"),
131 GTK_PARAM_READWRITE));
134 * GtkCellRendererProgress:text:
136 * The "text" property determines the label which will be drawn
137 * over the progress bar. Setting this property to %NULL causes the default
138 * label to be displayed. Setting this property to an empty string causes
139 * no label to be displayed.
143 g_object_class_install_property (object_class,
145 g_param_spec_string ("text",
147 P_("Text on the progress bar"),
149 GTK_PARAM_READWRITE));
152 * GtkCellRendererProgress:pulse:
154 * Setting this to a non-negative value causes the cell renderer to
155 * enter "activity mode", where a block bounces back and forth to
156 * indicate that some progress is made, without specifying exactly how
159 * Each increment of the property causes the block to move by a little
162 * To indicate that the activity has not started yet, set the property
163 * to zero. To indicate completion, set the property to %G_MAXINT.
167 g_object_class_install_property (object_class,
169 g_param_spec_int ("pulse",
171 P_("Set this to positive values to indicate that some progress is made, but you don't know how much."),
173 GTK_PARAM_READWRITE));
176 * GtkCellRendererProgress:text-xalign:
178 * The "text-xalign" property controls the horizontal alignment of the
179 * text in the progress bar. Valid values range from 0 (left) to 1
180 * (right). Reserved for RTL layouts.
184 g_object_class_install_property (object_class,
186 g_param_spec_float ("text-xalign",
187 P_("Text x alignment"),
188 P_("The horizontal text alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
190 GTK_PARAM_READWRITE));
193 * GtkCellRendererProgress:text-yalign:
195 * The "text-yalign" property controls the vertical alignment of the
196 * text in the progress bar. Valid values range from 0 (top) to 1
201 g_object_class_install_property (object_class,
203 g_param_spec_float ("text-yalign",
204 P_("Text y alignment"),
205 P_("The vertical text alignment, from 0 (top) to 1 (bottom)."),
207 GTK_PARAM_READWRITE));
210 * GtkCellRendererProgress:orientation:
212 * The "orientation" property controls the direction and growth
213 * direction of the progress bar (left-to-right, right-to-left,
214 * top-to-bottom or bottom-to-top).
218 g_object_class_install_property (object_class,
220 g_param_spec_enum ("orientation",
222 P_("Orientation and growth direction of the progress bar"),
223 GTK_TYPE_PROGRESS_BAR_ORIENTATION,
224 GTK_PROGRESS_LEFT_TO_RIGHT,
225 GTK_PARAM_READWRITE));
228 g_type_class_add_private (object_class,
229 sizeof (GtkCellRendererProgressPrivate));
233 gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
235 GtkCellRendererProgressPrivate *priv = GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE (cellprogress);
245 priv->text_xalign = 0.5;
246 priv->text_yalign = 0.5;
248 priv->orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
250 cellprogress->priv = priv;
255 * gtk_cell_renderer_progress_new:
257 * Creates a new #GtkCellRendererProgress.
259 * Return value: the new cell renderer
264 gtk_cell_renderer_progress_new (void)
266 return g_object_new (GTK_TYPE_CELL_RENDERER_PROGRESS, NULL);
270 gtk_cell_renderer_progress_finalize (GObject *object)
272 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
273 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
276 g_free (priv->label);
278 G_OBJECT_CLASS (gtk_cell_renderer_progress_parent_class)->finalize (object);
282 gtk_cell_renderer_progress_get_property (GObject *object,
287 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
288 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
293 g_value_set_int (value, priv->value);
296 g_value_set_string (value, priv->text);
299 g_value_set_int (value, priv->pulse);
301 case PROP_TEXT_XALIGN:
302 g_value_set_float (value, priv->text_xalign);
304 case PROP_TEXT_YALIGN:
305 g_value_set_float (value, priv->text_yalign);
307 case PROP_ORIENTATION:
308 g_value_set_enum (value, priv->orientation);
311 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
316 gtk_cell_renderer_progress_set_property (GObject *object,
321 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
322 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
327 gtk_cell_renderer_progress_set_value (cellprogress,
328 g_value_get_int (value));
331 gtk_cell_renderer_progress_set_text (cellprogress,
332 g_value_get_string (value));
335 gtk_cell_renderer_progress_set_pulse (cellprogress,
336 g_value_get_int (value));
338 case PROP_TEXT_XALIGN:
339 priv->text_xalign = g_value_get_float (value);
341 case PROP_TEXT_YALIGN:
342 priv->text_yalign = g_value_get_float (value);
344 case PROP_ORIENTATION:
345 priv->orientation = g_value_get_enum (value);
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
353 recompute_label (GtkCellRendererProgress *cellprogress)
355 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
359 label = g_strdup (priv->text);
360 else if (priv->pulse < 0)
361 label = g_strdup_printf (C_("progress bar label", "%d %%"), priv->value);
365 g_free (priv->label);
370 gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
373 cellprogress->priv->value = value;
375 recompute_label (cellprogress);
379 gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
384 new_text = g_strdup (text);
385 g_free (cellprogress->priv->text);
386 cellprogress->priv->text = new_text;
388 recompute_label (cellprogress);
392 gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
395 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
397 if (pulse != priv->pulse)
402 priv->offset = pulse;
407 recompute_label (cellprogress);
411 compute_dimensions (GtkCellRenderer *cell,
417 PangoRectangle logical_rect;
420 layout = gtk_widget_create_pango_layout (widget, text);
421 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
424 *width = logical_rect.width + cell->xpad * 2;
427 *height = logical_rect.height + cell->ypad * 2;
429 g_object_unref (layout);
433 gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
435 GdkRectangle *cell_area,
441 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
442 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
448 text = g_strdup_printf (C_("progress bar label", "%d %%"), 100);
449 compute_dimensions (cell, widget, text,
455 compute_dimensions (cell, widget, priv->label, &w, &h);
458 *width = MAX (priv->min_w, w);
461 *height = MIN (priv->min_h, h);
463 /* FIXME: at the moment cell_area is only set when we are requesting
464 * the size for drawing the focus rectangle. We now just return
465 * the last size we used for drawing the progress bar, which will
466 * work for now. Not a really nice solution though.
471 *width = cell_area->width;
473 *height = cell_area->height;
476 if (x_offset) *x_offset = 0;
477 if (y_offset) *y_offset = 0;
481 get_bar_size (gint pulse,
488 bar_size = full_size * MAX (0, value) / 100;
491 else if (pulse == G_MAXINT)
492 bar_size = full_size;
494 bar_size = MAX (2, full_size / 5);
500 get_bar_position (gint start,
509 if (pulse < 0 || pulse == 0 || pulse == G_MAXINT)
511 position = is_rtl ? (start + full_size - bar_size) : start;
515 position = (is_rtl ? offset + 12 : offset) % 24;
517 position = 24 - position;
518 position = start + full_size * position / 15;
525 gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
528 GdkRectangle *background_area,
529 GdkRectangle *cell_area,
530 GdkRectangle *expose_area,
533 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
534 GtkCellRendererProgressPrivate *priv= cellprogress->priv;
536 PangoRectangle logical_rect;
537 gint x, y, w, h, x_pos, y_pos, bar_position, bar_size, start, full_size;
541 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
543 x = cell_area->x + cell->xpad;
544 y = cell_area->y + cell->ypad;
545 w = cell_area->width - cell->xpad * 2;
546 h = cell_area->height - cell->ypad * 2;
548 /* FIXME: GtkProgressBar draws the box with "trough" detail,
549 * but some engines don't paint anything with that detail for
550 * non-GtkProgressBar widgets.
552 gtk_paint_box (widget->style,
554 GTK_STATE_NORMAL, GTK_SHADOW_IN,
558 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
559 || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
567 bar_size = get_bar_size (priv->pulse, priv->value, full_size);
569 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
570 bar_position = get_bar_position (start, full_size, bar_size,
571 priv->pulse, priv->offset, is_rtl);
573 bar_position = get_bar_position (start, full_size, bar_size,
574 priv->pulse, priv->offset, !is_rtl);
576 clip.width = bar_size;
577 clip.x = bar_position;
587 bar_size = get_bar_size (priv->pulse, priv->value, full_size);
589 if (priv->orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
590 bar_position = get_bar_position (start, full_size, bar_size,
591 priv->pulse, priv->offset, TRUE);
593 bar_position = get_bar_position (start, full_size, bar_size,
594 priv->pulse, priv->offset, FALSE);
596 clip.height = bar_size;
597 clip.y = bar_position;
600 gtk_paint_box (widget->style,
602 GTK_STATE_SELECTED, GTK_SHADOW_OUT,
603 &clip, widget, "bar",
605 clip.width, clip.height);
611 layout = gtk_widget_create_pango_layout (widget, priv->label);
612 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
614 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
615 text_xalign = 1.0 - priv->text_xalign;
617 text_xalign = priv->text_xalign;
619 x_pos = x + widget->style->xthickness + text_xalign *
620 (w - 2 * widget->style->xthickness - logical_rect.width);
622 y_pos = y + widget->style->ythickness + priv->text_yalign *
623 (h - 2 * widget->style->ythickness - logical_rect.height);
625 gtk_paint_layout (widget->style, window,
627 FALSE, &clip, widget, "progressbar",
631 if (bar_position > start)
633 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
634 || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
637 clip.width = bar_position - x;
642 clip.height = bar_position - y;
645 gtk_paint_layout (widget->style, window,
647 FALSE, &clip, widget, "progressbar",
652 if (bar_position + bar_size < start + full_size)
654 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
655 || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
657 clip.x = bar_position + bar_size;
658 clip.width = x + w - (bar_position + bar_size);
662 clip.y = bar_position + bar_size;
663 clip.height = y + h - (bar_position + bar_size);
666 gtk_paint_layout (widget->style, window,
668 FALSE, &clip, widget, "progressbar",
673 g_object_unref (layout);
677 #define __GTK_CELL_RENDERER_PROGRESS_C__
678 #include "gtkaliasdef.c"