1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkprogressbar.h"
32 #include "gtkorientableprivate.h"
33 #include "gtkprivate.h"
36 #include "a11y/gtkprogressbaraccessible.h"
39 * SECTION:gtkprogressbar
40 * @Short_description: A widget which indicates progress visually
41 * @Title: GtkProgressBar
43 * The #GtkProgressBar is typically used to display the progress of a long
44 * running operation. It provides a visual clue that processing
45 * is underway. The #GtkProgressBar can be used in two different
46 * modes: percentage mode and activity mode.
48 * When an application can determine how much work needs to take place
49 * (e.g. read a fixed number of bytes from a file) and can monitor its
50 * progress, it can use the #GtkProgressBar in percentage mode and the user
51 * sees a growing bar indicating the percentage of the work that has
52 * been completed. In this mode, the application is required to call
53 * gtk_progress_bar_set_fraction() periodically to update the progress bar.
55 * When an application has no accurate way of knowing the amount of work
56 * to do, it can use the #GtkProgressBar in activity mode, which shows
57 * activity by a block moving back and forth within the progress area. In
58 * this mode, the application is required to call gtk_progress_bar_pulse()
59 * periodically to update the progress bar.
61 * There is quite a bit of flexibility provided to control the appearance
62 * of the #GtkProgressBar. Functions are provided to control the
63 * orientation of the bar, optional text can be displayed along with
64 * the bar, and the step size used in activity mode can be set.
67 #define MIN_HORIZONTAL_BAR_WIDTH 150
68 #define MIN_HORIZONTAL_BAR_HEIGHT 20
69 #define MIN_VERTICAL_BAR_WIDTH 22
70 #define MIN_VERTICAL_BAR_HEIGHT 80
73 struct _GtkProgressBarPrivate
78 gdouble pulse_fraction;
81 guint activity_blocks;
84 GtkOrientation orientation;
86 guint activity_dir : 1;
87 guint activity_mode : 1;
104 static void gtk_progress_bar_set_property (GObject *object,
108 static void gtk_progress_bar_get_property (GObject *object,
112 static void gtk_progress_bar_get_preferred_width (GtkWidget *widget,
115 static void gtk_progress_bar_get_preferred_height (GtkWidget *widget,
119 static void gtk_progress_bar_real_update (GtkProgressBar *progress);
120 static gboolean gtk_progress_bar_draw (GtkWidget *widget,
122 static void gtk_progress_bar_act_mode_enter (GtkProgressBar *progress);
123 static void gtk_progress_bar_finalize (GObject *object);
124 static void gtk_progress_bar_set_orientation (GtkProgressBar *progress,
125 GtkOrientation orientation);
127 G_DEFINE_TYPE_WITH_CODE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_WIDGET,
128 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
131 gtk_progress_bar_class_init (GtkProgressBarClass *class)
133 GObjectClass *gobject_class;
134 GtkWidgetClass *widget_class;
136 gobject_class = G_OBJECT_CLASS (class);
137 widget_class = (GtkWidgetClass *) class;
139 gobject_class->set_property = gtk_progress_bar_set_property;
140 gobject_class->get_property = gtk_progress_bar_get_property;
141 gobject_class->finalize = gtk_progress_bar_finalize;
143 widget_class->draw = gtk_progress_bar_draw;
144 widget_class->get_preferred_width = gtk_progress_bar_get_preferred_width;
145 widget_class->get_preferred_height = gtk_progress_bar_get_preferred_height;
147 g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation");
149 g_object_class_install_property (gobject_class,
151 g_param_spec_boolean ("inverted",
153 P_("Invert the direction in which the progress bar grows"),
155 GTK_PARAM_READWRITE));
157 g_object_class_install_property (gobject_class,
159 g_param_spec_double ("fraction",
161 P_("The fraction of total work that has been completed"),
163 GTK_PARAM_READWRITE));
165 g_object_class_install_property (gobject_class,
167 g_param_spec_double ("pulse-step",
169 P_("The fraction of total progress to move the bouncing block when pulsed"),
171 GTK_PARAM_READWRITE));
173 g_object_class_install_property (gobject_class,
175 g_param_spec_string ("text",
177 P_("Text to be displayed in the progress bar"),
179 GTK_PARAM_READWRITE));
182 * GtkProgressBar:show_text:
184 * Sets whether the progressbar will show text superimposed
185 * over the bar. The shown text is either the value of
186 * the #GtkProgressBar::text property or, if that is %NULL,
187 * the #GtkProgressBar::fraction value, as a percentage.
189 * To make a progress bar that is styled and sized suitably for containing
190 * text (even if the actual text is blank), set #GtkProgressBar:show-text to
191 * %TRUE and #GtkProgressBar:text to the empty string (not %NULL).
195 g_object_class_install_property (gobject_class,
197 g_param_spec_boolean ("show-text",
199 P_("Whether the progress is shown as text."),
201 GTK_PARAM_READWRITE));
204 * GtkProgressBar:ellipsize:
206 * The preferred place to ellipsize the string, if the progressbar does
207 * not have enough room to display the entire string, specified as a
208 * #PangoEllisizeMode.
210 * Note that setting this property to a value other than
211 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
212 * only enough space to display the ellipsis "...". Another means to set a
213 * progressbar's width is gtk_widget_set_size_request().
217 g_object_class_install_property (gobject_class,
219 g_param_spec_enum ("ellipsize",
221 P_("The preferred place to ellipsize the string, if the progress bar "
222 "does not have enough room to display the entire string, if at all."),
223 PANGO_TYPE_ELLIPSIZE_MODE,
224 PANGO_ELLIPSIZE_NONE,
225 GTK_PARAM_READWRITE));
226 gtk_widget_class_install_style_property (widget_class,
227 g_param_spec_int ("xspacing",
229 P_("Extra spacing applied to the width of a progress bar."),
232 gtk_widget_class_install_style_property (widget_class,
233 g_param_spec_int ("yspacing",
235 P_("Extra spacing applied to the height of a progress bar."),
240 * GtkProgressBar:min-horizontal-bar-width:
242 * The minimum horizontal width of the progress bar.
246 gtk_widget_class_install_style_property (widget_class,
247 g_param_spec_int ("min-horizontal-bar-width",
248 P_("Minimum horizontal bar width"),
249 P_("The minimum horizontal width of the progress bar"),
250 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
253 * GtkProgressBar:min-horizontal-bar-height:
255 * Minimum horizontal height of the progress bar.
259 gtk_widget_class_install_style_property (widget_class,
260 g_param_spec_int ("min-horizontal-bar-height",
261 P_("Minimum horizontal bar height"),
262 P_("Minimum horizontal height of the progress bar"),
263 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
266 * GtkProgressBar:min-vertical-bar-width:
268 * The minimum vertical width of the progress bar.
272 gtk_widget_class_install_style_property (widget_class,
273 g_param_spec_int ("min-vertical-bar-width",
274 P_("Minimum vertical bar width"),
275 P_("The minimum vertical width of the progress bar"),
276 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
279 * GtkProgressBar:min-vertical-bar-height:
281 * The minimum vertical height of the progress bar.
285 gtk_widget_class_install_style_property (widget_class,
286 g_param_spec_int ("min-vertical-bar-height",
287 P_("Minimum vertical bar height"),
288 P_("The minimum vertical height of the progress bar"),
289 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
292 g_type_class_add_private (class, sizeof (GtkProgressBarPrivate));
294 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PROGRESS_BAR_ACCESSIBLE);
298 gtk_progress_bar_init (GtkProgressBar *pbar)
300 GtkProgressBarPrivate *priv;
302 pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
303 GTK_TYPE_PROGRESS_BAR,
304 GtkProgressBarPrivate);
307 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
308 priv->inverted = FALSE;
309 priv->pulse_fraction = 0.1;
310 priv->activity_pos = 0;
311 priv->activity_dir = 1;
312 priv->activity_step = 3;
313 priv->activity_blocks = 5;
314 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
315 priv->show_text = FALSE;
318 priv->fraction = 0.0;
320 gtk_widget_set_has_window (GTK_WIDGET (pbar), FALSE);
324 gtk_progress_bar_set_property (GObject *object,
329 GtkProgressBar *pbar;
331 pbar = GTK_PROGRESS_BAR (object);
335 case PROP_ORIENTATION:
336 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
339 gtk_progress_bar_set_inverted (pbar, g_value_get_boolean (value));
342 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
344 case PROP_PULSE_STEP:
345 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
348 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
351 gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
354 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 gtk_progress_bar_get_property (GObject *object,
368 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
369 GtkProgressBarPrivate* priv = pbar->priv;
373 case PROP_ORIENTATION:
374 g_value_set_enum (value, priv->orientation);
377 g_value_set_boolean (value, priv->inverted);
380 g_value_set_double (value, priv->fraction);
382 case PROP_PULSE_STEP:
383 g_value_set_double (value, priv->pulse_fraction);
386 g_value_set_string (value, priv->text);
389 g_value_set_boolean (value, priv->show_text);
392 g_value_set_enum (value, priv->ellipsize);
395 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
401 * gtk_progress_bar_new:
403 * Creates a new #GtkProgressBar.
405 * Returns: a #GtkProgressBar.
408 gtk_progress_bar_new (void)
412 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
418 gtk_progress_bar_real_update (GtkProgressBar *pbar)
420 GtkProgressBarPrivate *priv;
423 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
426 widget = GTK_WIDGET (pbar);
428 if (priv->activity_mode)
430 GtkAllocation allocation;
431 GtkStyleContext *context;
436 gtk_widget_get_allocation (widget, &allocation);
437 context = gtk_widget_get_style_context (widget);
438 state = gtk_widget_get_state_flags (widget);
439 gtk_style_context_get_padding (context, state, &padding);
441 /* advance the block */
442 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
444 /* Update our activity step. */
445 priv->activity_step = allocation.width * priv->pulse_fraction;
447 size = MAX (2, allocation.width / priv->activity_blocks);
449 if (priv->activity_dir == 0)
451 priv->activity_pos += priv->activity_step;
452 if (priv->activity_pos + size >= allocation.width - padding.left)
454 priv->activity_pos = allocation.width - padding.left - size;
455 priv->activity_dir = 1;
460 priv->activity_pos -= priv->activity_step;
461 if (priv->activity_pos <= padding.left)
463 priv->activity_pos = padding.left;
464 priv->activity_dir = 0;
470 /* Update our activity step. */
471 priv->activity_step = allocation.height * priv->pulse_fraction;
473 size = MAX (2, allocation.height / priv->activity_blocks);
475 if (priv->activity_dir == 0)
477 priv->activity_pos += priv->activity_step;
478 if (priv->activity_pos + size >= allocation.height - padding.top)
480 priv->activity_pos = allocation.height - padding.top - size;
481 priv->activity_dir = 1;
486 priv->activity_pos -= priv->activity_step;
487 if (priv->activity_pos <= padding.top)
489 priv->activity_pos = padding.top;
490 priv->activity_dir = 0;
495 gtk_widget_queue_draw (widget);
499 gtk_progress_bar_finalize (GObject *object)
501 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
502 GtkProgressBarPrivate *priv = pbar->priv;
506 G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
510 get_current_text (GtkProgressBar *pbar)
512 GtkProgressBarPrivate *priv = pbar->priv;
515 return g_strdup (priv->text);
517 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
521 gtk_progress_bar_get_preferred_width (GtkWidget *widget,
525 GtkProgressBar *pbar;
526 GtkProgressBarPrivate *priv;
527 GtkStyleContext *style_context;
531 PangoRectangle logical_rect;
537 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
539 style_context = gtk_widget_get_style_context (widget);
540 state = gtk_widget_get_state_flags (widget);
541 gtk_style_context_get_padding (style_context, state, &padding);
543 gtk_widget_style_get (widget,
544 "xspacing", &xspacing,
547 pbar = GTK_PROGRESS_BAR (widget);
550 width = padding.left + padding.right + xspacing;
554 buf = get_current_text (pbar);
555 layout = gtk_widget_create_pango_layout (widget, buf);
557 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
561 const PangoFontDescription *font_desc;
562 PangoContext *context;
563 PangoFontMetrics *metrics;
566 /* The minimum size for ellipsized text is ~ 3 chars */
567 context = pango_layout_get_context (layout);
568 font_desc = gtk_style_context_get_font (style_context, state);
569 metrics = pango_context_get_metrics (context, font_desc, pango_context_get_language (context));
571 char_width = pango_font_metrics_get_approximate_char_width (metrics);
572 pango_font_metrics_unref (metrics);
574 width += PANGO_PIXELS (char_width) * 3;
577 width += logical_rect.width;
579 g_object_unref (layout);
583 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
584 gtk_widget_style_get (widget,
585 "min-horizontal-bar-width", &min_width,
588 gtk_widget_style_get (widget,
589 "min-vertical-bar-width", &min_width,
592 *minimum = *natural = MAX (min_width, width);
596 gtk_progress_bar_get_preferred_height (GtkWidget *widget,
600 GtkProgressBar *pbar;
601 GtkProgressBarPrivate *priv;
602 GtkStyleContext *context;
606 PangoRectangle logical_rect;
612 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
614 context = gtk_widget_get_style_context (widget);
615 state = gtk_widget_get_state_flags (widget);
616 gtk_style_context_get_padding (context, state, &padding);
618 gtk_widget_style_get (widget,
619 "yspacing", &yspacing,
622 pbar = GTK_PROGRESS_BAR (widget);
625 height = padding.top + padding.bottom + yspacing;
629 buf = get_current_text (pbar);
630 layout = gtk_widget_create_pango_layout (widget, buf);
632 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
634 height += logical_rect.height;
636 g_object_unref (layout);
640 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
641 gtk_widget_style_get (widget,
642 "min-horizontal-bar-height", &min_height,
645 gtk_widget_style_get (widget,
646 "min-vertical-bar-height", &min_height,
649 *minimum = *natural = MAX (min_height, height);
653 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
655 GtkProgressBarPrivate *priv = pbar->priv;
656 GtkAllocation allocation;
657 GtkStyleContext *context;
660 GtkWidget *widget = GTK_WIDGET (pbar);
661 GtkOrientation orientation;
664 context = gtk_widget_get_style_context (widget);
665 state = gtk_widget_get_state_flags (widget);
666 gtk_style_context_get_padding (context, state, &padding);
668 orientation = priv->orientation;
669 inverted = priv->inverted;
670 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
672 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
673 inverted = !inverted;
676 /* calculate start pos */
678 if (orientation == GTK_ORIENTATION_HORIZONTAL)
682 priv->activity_pos = padding.left;
683 priv->activity_dir = 0;
687 gtk_widget_get_allocation (widget, &allocation);
688 priv->activity_pos = allocation.width - padding.left -
689 (allocation.height - padding.top - padding.bottom);
690 priv->activity_dir = 1;
697 priv->activity_pos = padding.top;
698 priv->activity_dir = 0;
702 gtk_widget_get_allocation (widget, &allocation);
703 priv->activity_pos = allocation.height - padding.top -
704 (allocation.width - padding.left - padding.right);
705 priv->activity_dir = 1;
711 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
712 GtkOrientation orientation,
716 GtkProgressBarPrivate *priv = pbar->priv;
717 GtkAllocation allocation;
718 GtkWidget *widget = GTK_WIDGET (pbar);
720 *offset = priv->activity_pos;
722 gtk_widget_get_allocation (widget, &allocation);
724 if (orientation == GTK_ORIENTATION_HORIZONTAL)
725 *amount = MAX (2, allocation.width / priv->activity_blocks);
727 *amount = MAX (2, allocation.height / priv->activity_blocks);
731 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
733 GtkOrientation orientation,
738 GtkStyleContext *context;
741 GtkWidget *widget = GTK_WIDGET (pbar);
744 context = gtk_widget_get_style_context (widget);
745 state = gtk_widget_get_state_flags (widget);
746 gtk_style_context_get_padding (context, state, &padding);
748 if (orientation == GTK_ORIENTATION_HORIZONTAL)
750 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
751 area.y = padding.top;
752 area.height = height - padding.top - padding.bottom;
756 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
757 area.x = padding.left;
758 area.width = width - padding.left - padding.right;
761 gtk_style_context_save (context);
762 gtk_style_context_set_state (context, state);
763 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
764 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PULSE);
766 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
768 gtk_style_context_restore (context);
772 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
775 GtkOrientation orientation,
780 GtkStyleContext *context;
783 GtkWidget *widget = GTK_WIDGET (pbar);
789 context = gtk_widget_get_style_context (widget);
790 state = gtk_widget_get_state_flags (widget);
791 gtk_style_context_get_padding (context, state, &padding);
793 if (orientation == GTK_ORIENTATION_HORIZONTAL)
796 area.height = height - padding.top - padding.bottom;
797 area.y = padding.top;
800 area.x = padding.left;
802 area.x = width - amount - padding.right;
806 area.width = width - padding.left - padding.right;
807 area.height = amount;
808 area.x = padding.left;
811 area.y = padding.top;
813 area.y = height - amount - padding.bottom;
816 gtk_style_context_save (context);
817 gtk_style_context_set_state (context, state);
818 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
820 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
822 gtk_style_context_restore (context);
826 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
830 GtkOrientation orientation,
835 GtkProgressBarPrivate *priv = pbar->priv;
836 GtkStyleContext *context;
839 GtkWidget *widget = GTK_WIDGET (pbar);
845 PangoRectangle logical_rect;
846 GdkRectangle prelight_clip, start_clip, end_clip;
847 gfloat text_xalign = 0.5;
848 gfloat text_yalign = 0.5;
850 context = gtk_widget_get_style_context (widget);
851 state = gtk_widget_get_state_flags (widget);
852 gtk_style_context_get_padding (context, state, &padding);
854 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
855 text_xalign = 1.0 - text_xalign;
857 buf = get_current_text (pbar);
859 layout = gtk_widget_create_pango_layout (widget, buf);
860 pango_layout_set_ellipsize (layout, priv->ellipsize);
862 pango_layout_set_width (layout, width * PANGO_SCALE);
864 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
866 x = padding.left + 1 + text_xalign * (width - padding.left - padding.right - 2 - logical_rect.width);
867 y = padding.top + 1 + text_yalign * (height - padding.top - padding.bottom - 2 - logical_rect.height);
869 rect.x = padding.left;
870 rect.y = padding.top;
871 rect.width = width - padding.left - padding.right;
872 rect.height = height - padding.top - padding.bottom;
874 prelight_clip = start_clip = end_clip = rect;
876 if (orientation == GTK_ORIENTATION_HORIZONTAL)
881 prelight_clip.x = offset;
882 prelight_clip.width = amount;
883 start_clip.width = prelight_clip.x - start_clip.x;
884 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
885 end_clip.width -= prelight_clip.width + start_clip.width;
890 prelight_clip.x = offset;
892 prelight_clip.x = rect.x + rect.width - amount;
893 prelight_clip.width = amount;
894 start_clip.width = prelight_clip.x - start_clip.x;
895 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
896 end_clip.width -= prelight_clip.width + start_clip.width;
904 prelight_clip.y = offset;
905 prelight_clip.height = amount;
906 start_clip.height = prelight_clip.y - start_clip.y;
907 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
908 end_clip.height -= prelight_clip.height + start_clip.height;
913 prelight_clip.y = offset;
915 prelight_clip.y = rect.y + rect.height - amount;
916 prelight_clip.height = amount;
917 start_clip.height = prelight_clip.y - start_clip.y;
918 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
919 end_clip.height -= prelight_clip.height + start_clip.height;
923 gtk_style_context_save (context);
924 gtk_style_context_set_state (context, state);
925 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
927 if (start_clip.width > 0 && start_clip.height > 0)
930 gdk_cairo_rectangle (cr, &start_clip);
933 gtk_render_layout (context, cr, x, y, layout);
937 if (end_clip.width > 0 && end_clip.height > 0)
940 gdk_cairo_rectangle (cr, &end_clip);
943 gtk_render_layout (context, cr, x, y, layout);
947 gtk_style_context_restore (context);
950 gdk_cairo_rectangle (cr, &prelight_clip);
953 gtk_style_context_save (context);
954 gtk_style_context_set_state (context, state);
955 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
957 gtk_render_layout (context, cr, x, y, layout);
959 gtk_style_context_restore (context);
962 g_object_unref (layout);
967 gtk_progress_bar_draw (GtkWidget *widget,
970 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
971 GtkProgressBarPrivate *priv = pbar->priv;
972 GtkOrientation orientation;
974 GtkStyleContext *context;
979 context = gtk_widget_get_style_context (widget);
980 state = gtk_widget_get_state_flags (widget);
981 gtk_style_context_get_padding (context, state, &padding);
983 orientation = priv->orientation;
984 inverted = priv->inverted;
985 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
987 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
988 inverted = !inverted;
990 width = gtk_widget_get_allocated_width (widget);
991 height = gtk_widget_get_allocated_height (widget);
993 gtk_style_context_save (context);
994 gtk_style_context_set_state (context, state);
995 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
997 gtk_render_background (context, cr, 0, 0, width, height);
998 gtk_render_frame (context, cr, 0, 0, width, height);
1000 gtk_style_context_restore (context);
1002 if (priv->activity_mode)
1004 gtk_progress_bar_paint_activity (pbar, cr,
1005 orientation, inverted,
1008 if (priv->show_text)
1013 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
1014 gtk_progress_bar_paint_text (pbar, cr,
1016 orientation, inverted,
1025 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1026 space = width - padding.left - padding.right;
1028 space = height - padding.top - padding.bottom;
1030 amount = space * gtk_progress_bar_get_fraction (pbar);
1032 gtk_progress_bar_paint_continuous (pbar, cr, amount, orientation, inverted, width, height);
1034 if (priv->show_text)
1035 gtk_progress_bar_paint_text (pbar, cr, -1, amount, orientation, inverted, width, height);
1042 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
1043 gboolean activity_mode)
1045 GtkProgressBarPrivate *priv = pbar->priv;
1047 activity_mode = !!activity_mode;
1049 if (priv->activity_mode != activity_mode)
1051 priv->activity_mode = activity_mode;
1053 if (priv->activity_mode)
1054 gtk_progress_bar_act_mode_enter (pbar);
1056 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1061 * gtk_progress_bar_set_fraction:
1062 * @pbar: a #GtkProgressBar
1063 * @fraction: fraction of the task that's been completed
1065 * Causes the progress bar to "fill in" the given fraction
1066 * of the bar. The fraction should be between 0.0 and 1.0,
1071 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1074 GtkProgressBarPrivate* priv;
1076 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1080 priv->fraction = CLAMP(fraction, 0.0, 1.0);
1081 gtk_progress_bar_set_activity_mode (pbar, FALSE);
1082 gtk_progress_bar_real_update (pbar);
1084 g_object_notify (G_OBJECT (pbar), "fraction");
1088 * gtk_progress_bar_pulse:
1089 * @pbar: a #GtkProgressBar
1091 * Indicates that some progress is made, but you don't know how much.
1092 * Causes the progress bar to enter "activity mode," where a block
1093 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1094 * causes the block to move by a little bit (the amount of movement
1095 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1098 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1100 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1102 gtk_progress_bar_set_activity_mode (pbar, TRUE);
1103 gtk_progress_bar_real_update (pbar);
1107 * gtk_progress_bar_set_text:
1108 * @pbar: a #GtkProgressBar
1109 * @text: (allow-none): a UTF-8 string, or %NULL
1111 * Causes the given @text to appear superimposed on the progress bar.
1113 * If @text is %NULL and #GtkProgressBar:show-text is %TRUE, the current
1114 * value of #GtkProgressBar:fraction will be displayed as a percentage.
1116 * If @text is non-%NULL and #GtkProgressBar:show-text is %TRUE, the text will
1117 * be displayed. In this case, it will not display the progress percentage.
1118 * If @text is the empty string, the progress bar will still be styled and sized
1119 * suitably for containing text, as long as #GtkProgressBar:show-text is %TRUE.
1122 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1125 GtkProgressBarPrivate *priv;
1127 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1131 /* Don't notify again if nothing's changed. */
1132 if (g_strcmp0 (priv->text, text) == 0)
1135 g_free (priv->text);
1136 priv->text = g_strdup (text);
1138 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1140 g_object_notify (G_OBJECT (pbar), "text");
1144 * gtk_progress_bar_set_show_text:
1145 * @pbar: a #GtkProgressBar
1146 * @show_text: whether to show superimposed text
1148 * Sets whether the progressbar will show text superimposed
1149 * over the bar. The shown text is either the value of
1150 * the #GtkProgressBar::text property or, if that is %NULL,
1151 * the #GtkProgressBar::fraction value, as a percentage.
1153 * To make a progress bar that is styled and sized suitably for containing
1154 * text (even if the actual text is blank), set #GtkProgressBar:show-text to
1155 * %TRUE and #GtkProgressBar:text to the empty string (not %NULL).
1160 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1163 GtkProgressBarPrivate *priv;
1165 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1169 show_text = !!show_text;
1171 if (priv->show_text != show_text)
1173 priv->show_text = show_text;
1175 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1177 g_object_notify (G_OBJECT (pbar), "show-text");
1182 * gtk_progress_bar_get_show_text:
1183 * @pbar: a #GtkProgressBar
1185 * Gets the value of the #GtkProgressBar::show-text property.
1186 * See gtk_progress_bar_set_show_text().
1188 * Returns: %TRUE if text is shown in the progress bar
1193 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1195 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1197 return pbar->priv->show_text;
1201 * gtk_progress_bar_set_pulse_step:
1202 * @pbar: a #GtkProgressBar
1203 * @fraction: fraction between 0.0 and 1.0
1205 * Sets the fraction of total progress bar length to move the
1206 * bouncing block for each call to gtk_progress_bar_pulse().
1209 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1212 GtkProgressBarPrivate *priv;
1214 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1218 priv->pulse_fraction = fraction;
1220 g_object_notify (G_OBJECT (pbar), "pulse-step");
1224 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1225 GtkOrientation orientation)
1227 GtkProgressBarPrivate *priv = pbar->priv;
1229 if (priv->orientation != orientation)
1231 priv->orientation = orientation;
1232 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (pbar));
1234 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1239 * gtk_progress_bar_set_inverted:
1240 * @pbar: a #GtkProgressBar
1241 * @inverted: %TRUE to invert the progress bar
1243 * Progress bars normally grow from top to bottom or left to right.
1244 * Inverted progress bars grow in the opposite direction.
1247 gtk_progress_bar_set_inverted (GtkProgressBar *pbar,
1250 GtkProgressBarPrivate *priv;
1252 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1256 if (priv->inverted != inverted)
1258 priv->inverted = inverted;
1260 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1262 g_object_notify (G_OBJECT (pbar), "inverted");
1267 * gtk_progress_bar_get_text:
1268 * @pbar: a #GtkProgressBar
1270 * Retrieves the text displayed superimposed on the progress bar,
1271 * if any, otherwise %NULL. The return value is a reference
1272 * to the text, not a copy of it, so will become invalid
1273 * if you change the text in the progress bar.
1275 * Return value: text, or %NULL; this string is owned by the widget
1276 * and should not be modified or freed.
1279 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1281 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1283 return pbar->priv->text;
1287 * gtk_progress_bar_get_fraction:
1288 * @pbar: a #GtkProgressBar
1290 * Returns the current fraction of the task that's been completed.
1292 * Return value: a fraction from 0.0 to 1.0
1295 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1297 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1299 return pbar->priv->fraction;
1303 * gtk_progress_bar_get_pulse_step:
1304 * @pbar: a #GtkProgressBar
1306 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1308 * Return value: a fraction from 0.0 to 1.0
1311 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1313 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1315 return pbar->priv->pulse_fraction;
1319 * gtk_progress_bar_get_inverted:
1320 * @pbar: a #GtkProgressBar
1322 * Gets the value set by gtk_progress_bar_set_inverted()
1324 * Return value: %TRUE if the progress bar is inverted
1327 gtk_progress_bar_get_inverted (GtkProgressBar *pbar)
1329 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1331 return pbar->priv->inverted;
1335 * gtk_progress_bar_set_ellipsize:
1336 * @pbar: a #GtkProgressBar
1337 * @mode: a #PangoEllipsizeMode
1339 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1340 * if there is not enough space to render the entire string.
1345 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1346 PangoEllipsizeMode mode)
1348 GtkProgressBarPrivate *priv;
1350 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1351 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1352 mode <= PANGO_ELLIPSIZE_END);
1356 if ((PangoEllipsizeMode)priv->ellipsize != mode)
1358 priv->ellipsize = mode;
1360 g_object_notify (G_OBJECT (pbar), "ellipsize");
1361 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1366 * gtk_progress_bar_get_ellipsize:
1367 * @pbar: a #GtkProgressBar
1369 * Returns the ellipsizing position of the progressbar.
1370 * See gtk_progress_bar_set_ellipsize().
1372 * Return value: #PangoEllipsizeMode
1377 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1379 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1381 return pbar->priv->ellipsize;