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"
36 #define GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), \
37 GTK_TYPE_CELL_RENDERER_PROGRESS, \
38 GtkCellRendererProgressPrivate))
51 struct _GtkCellRendererProgressPrivate
62 GtkProgressBarOrientation orientation;
65 static void gtk_cell_renderer_progress_finalize (GObject *object);
66 static void gtk_cell_renderer_progress_get_property (GObject *object,
70 static void gtk_cell_renderer_progress_set_property (GObject *object,
74 static void gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
76 static void gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
78 static void gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
80 static void compute_dimensions (GtkCellRenderer *cell,
85 static void gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
87 GdkRectangle *cell_area,
92 static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
95 GdkRectangle *background_area,
96 GdkRectangle *cell_area,
97 GdkRectangle *expose_area,
101 G_DEFINE_TYPE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER)
104 gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
106 GObjectClass *object_class = G_OBJECT_CLASS (klass);
107 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
109 object_class->finalize = gtk_cell_renderer_progress_finalize;
110 object_class->get_property = gtk_cell_renderer_progress_get_property;
111 object_class->set_property = gtk_cell_renderer_progress_set_property;
113 cell_class->get_size = gtk_cell_renderer_progress_get_size;
114 cell_class->render = gtk_cell_renderer_progress_render;
117 * GtkCellRendererProgress:value:
119 * The "value" property determines the percentage to which the
120 * progress bar will be "filled in".
124 g_object_class_install_property (object_class,
126 g_param_spec_int ("value",
128 P_("Value of the progress bar"),
130 GTK_PARAM_READWRITE));
133 * GtkCellRendererProgress:text:
135 * The "text" property determines the label which will be drawn
136 * over the progress bar. Setting this property to %NULL causes the default
137 * label to be displayed. Setting this property to an empty string causes
138 * no label to be displayed.
142 g_object_class_install_property (object_class,
144 g_param_spec_string ("text",
146 P_("Text on the progress bar"),
148 GTK_PARAM_READWRITE));
151 * GtkCellRendererProgress:pulse:
153 * Setting this to a non-negative value causes the cell renderer to
154 * enter "activity mode", where a block bounces back and forth to
155 * indicate that some progress is made, without specifying exactly how
158 * Each increment of the property causes the block to move by a little
161 * To indicate that the activity has not started yet, set the property
162 * to zero. To indicate completion, set the property to %G_MAXINT.
166 g_object_class_install_property (object_class,
168 g_param_spec_int ("pulse",
170 P_("Set this to positive values to indicate that some progress is made, but you don't know how much."),
172 GTK_PARAM_READWRITE));
175 * GtkCellRendererProgress:text-xalign:
177 * The "text-xalign" property controls the horizontal alignment of the
178 * text in the progress bar. Valid values range from 0 (left) to 1
179 * (right). Reserved for RTL layouts.
183 g_object_class_install_property (object_class,
185 g_param_spec_float ("text-xalign",
186 P_("Text x alignment"),
187 P_("The horizontal text alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
189 GTK_PARAM_READWRITE));
192 * GtkCellRendererProgress:text-yalign:
194 * The "text-yalign" property controls the vertical alignment of the
195 * text in the progress bar. Valid values range from 0 (top) to 1
200 g_object_class_install_property (object_class,
202 g_param_spec_float ("text-yalign",
203 P_("Text y alignment"),
204 P_("The vertical text alignment, from 0 (top) to 1 (bottom)."),
206 GTK_PARAM_READWRITE));
209 * GtkCellRendererProgress:orientation:
211 * The "orientation" property controls the direction and growth
212 * direction of the progress bar (left-to-right, right-to-left,
213 * top-to-bottom or bottom-to-top).
217 g_object_class_install_property (object_class,
219 g_param_spec_enum ("orientation",
221 P_("Orientation and growth direction of the progress bar"),
222 GTK_TYPE_PROGRESS_BAR_ORIENTATION,
223 GTK_PROGRESS_LEFT_TO_RIGHT,
224 GTK_PARAM_READWRITE));
227 g_type_class_add_private (object_class,
228 sizeof (GtkCellRendererProgressPrivate));
232 gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
234 GtkCellRendererProgressPrivate *priv = GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE (cellprogress);
244 priv->text_xalign = 0.5;
245 priv->text_yalign = 0.5;
247 priv->orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
249 cellprogress->priv = priv;
254 * gtk_cell_renderer_progress_new:
256 * Creates a new #GtkCellRendererProgress.
258 * Return value: the new cell renderer
263 gtk_cell_renderer_progress_new (void)
265 return g_object_new (GTK_TYPE_CELL_RENDERER_PROGRESS, NULL);
269 gtk_cell_renderer_progress_finalize (GObject *object)
271 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
272 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
275 g_free (priv->label);
277 G_OBJECT_CLASS (gtk_cell_renderer_progress_parent_class)->finalize (object);
281 gtk_cell_renderer_progress_get_property (GObject *object,
286 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
287 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
292 g_value_set_int (value, priv->value);
295 g_value_set_string (value, priv->text);
298 g_value_set_int (value, priv->pulse);
300 case PROP_TEXT_XALIGN:
301 g_value_set_float (value, priv->text_xalign);
303 case PROP_TEXT_YALIGN:
304 g_value_set_float (value, priv->text_yalign);
306 case PROP_ORIENTATION:
307 g_value_set_enum (value, priv->orientation);
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
315 gtk_cell_renderer_progress_set_property (GObject *object,
320 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
321 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
326 gtk_cell_renderer_progress_set_value (cellprogress,
327 g_value_get_int (value));
330 gtk_cell_renderer_progress_set_text (cellprogress,
331 g_value_get_string (value));
334 gtk_cell_renderer_progress_set_pulse (cellprogress,
335 g_value_get_int (value));
337 case PROP_TEXT_XALIGN:
338 priv->text_xalign = g_value_get_float (value);
340 case PROP_TEXT_YALIGN:
341 priv->text_yalign = g_value_get_float (value);
343 case PROP_ORIENTATION:
344 priv->orientation = g_value_get_enum (value);
347 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
352 recompute_label (GtkCellRendererProgress *cellprogress)
354 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
358 label = g_strdup (priv->text);
359 else if (priv->pulse < 0)
360 label = g_strdup_printf (C_("progress bar label", "%d %%"), priv->value);
364 g_free (priv->label);
369 gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
372 cellprogress->priv->value = value;
374 recompute_label (cellprogress);
378 gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
383 new_text = g_strdup (text);
384 g_free (cellprogress->priv->text);
385 cellprogress->priv->text = new_text;
387 recompute_label (cellprogress);
391 gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
394 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
396 if (pulse != priv->pulse)
401 priv->offset = pulse;
406 recompute_label (cellprogress);
410 compute_dimensions (GtkCellRenderer *cell,
416 PangoRectangle logical_rect;
419 layout = gtk_widget_create_pango_layout (widget, text);
420 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
423 *width = logical_rect.width + cell->xpad * 2;
426 *height = logical_rect.height + cell->ypad * 2;
428 g_object_unref (layout);
432 gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
434 GdkRectangle *cell_area,
440 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
441 GtkCellRendererProgressPrivate *priv = cellprogress->priv;
447 text = g_strdup_printf (C_("progress bar label", "%d %%"), 100);
448 compute_dimensions (cell, widget, text,
454 compute_dimensions (cell, widget, priv->label, &w, &h);
457 *width = MAX (priv->min_w, w);
460 *height = MIN (priv->min_h, h);
462 /* FIXME: at the moment cell_area is only set when we are requesting
463 * the size for drawing the focus rectangle. We now just return
464 * the last size we used for drawing the progress bar, which will
465 * work for now. Not a really nice solution though.
470 *width = cell_area->width;
472 *height = cell_area->height;
475 if (x_offset) *x_offset = 0;
476 if (y_offset) *y_offset = 0;
480 get_bar_size (gint pulse,
487 bar_size = full_size * MAX (0, value) / 100;
490 else if (pulse == G_MAXINT)
491 bar_size = full_size;
493 bar_size = MAX (2, full_size / 5);
499 get_bar_position (gint start,
508 if (pulse < 0 || pulse == 0 || pulse == G_MAXINT)
510 position = is_rtl ? (start + full_size - bar_size) : start;
514 position = (is_rtl ? offset + 12 : offset) % 24;
516 position = 24 - position;
517 position = start + full_size * position / 15;
524 gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
527 GdkRectangle *background_area,
528 GdkRectangle *cell_area,
529 GdkRectangle *expose_area,
532 GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
533 GtkCellRendererProgressPrivate *priv= cellprogress->priv;
535 PangoRectangle logical_rect;
536 gint x, y, w, h, x_pos, y_pos, bar_position, bar_size, start, full_size;
540 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
542 x = cell_area->x + cell->xpad;
543 y = cell_area->y + cell->ypad;
544 w = cell_area->width - cell->xpad * 2;
545 h = cell_area->height - cell->ypad * 2;
547 /* FIXME: GtkProgressBar draws the box with "trough" detail,
548 * but some engines don't paint anything with that detail for
549 * non-GtkProgressBar widgets.
551 gtk_paint_box (widget->style,
553 GTK_STATE_NORMAL, GTK_SHADOW_IN,
557 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
558 || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
566 bar_size = get_bar_size (priv->pulse, priv->value, full_size);
568 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
569 bar_position = get_bar_position (start, full_size, bar_size,
570 priv->pulse, priv->offset, is_rtl);
572 bar_position = get_bar_position (start, full_size, bar_size,
573 priv->pulse, priv->offset, !is_rtl);
575 clip.width = bar_size;
576 clip.x = bar_position;
586 bar_size = get_bar_size (priv->pulse, priv->value, full_size);
588 if (priv->orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
589 bar_position = get_bar_position (start, full_size, bar_size,
590 priv->pulse, priv->offset, TRUE);
592 bar_position = get_bar_position (start, full_size, bar_size,
593 priv->pulse, priv->offset, FALSE);
595 clip.height = bar_size;
596 clip.y = bar_position;
599 gtk_paint_box (widget->style,
601 GTK_STATE_SELECTED, GTK_SHADOW_OUT,
602 &clip, widget, "bar",
604 clip.width, clip.height);
610 layout = gtk_widget_create_pango_layout (widget, priv->label);
611 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
613 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
614 text_xalign = 1.0 - priv->text_xalign;
616 text_xalign = priv->text_xalign;
618 x_pos = x + widget->style->xthickness + text_xalign *
619 (w - 2 * widget->style->xthickness - logical_rect.width);
621 y_pos = y + widget->style->ythickness + priv->text_yalign *
622 (h - 2 * widget->style->ythickness - logical_rect.height);
624 gtk_paint_layout (widget->style, window,
626 FALSE, &clip, widget, "progressbar",
630 if (bar_position > start)
632 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
633 || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
636 clip.width = bar_position - x;
641 clip.height = bar_position - y;
644 gtk_paint_layout (widget->style, window,
646 FALSE, &clip, widget, "progressbar",
651 if (bar_position + bar_size < start + full_size)
653 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
654 || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
656 clip.x = bar_position + bar_size;
657 clip.width = x + w - (bar_position + bar_size);
661 clip.y = bar_position + bar_size;
662 clip.height = y + h - (bar_position + bar_size);
665 gtk_paint_layout (widget->style, window,
667 FALSE, &clip, widget, "progressbar",
672 g_object_unref (layout);