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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gtkprogressbar.h"
30 #include "gtkorientableprivate.h"
31 #include "gtkprivate.h"
34 #include "a11y/gtkprogressbaraccessible.h"
37 * SECTION:gtkprogressbar
38 * @Short_description: A widget which indicates progress visually
39 * @Title: GtkProgressBar
41 * The #GtkProgressBar is typically used to display the progress of a long
42 * running operation. It provides a visual clue that processing
43 * is underway. The #GtkProgressBar can be used in two different
44 * modes: percentage mode and activity mode.
46 * When an application can determine how much work needs to take place
47 * (e.g. read a fixed number of bytes from a file) and can monitor its
48 * progress, it can use the #GtkProgressBar in percentage mode and the user
49 * sees a growing bar indicating the percentage of the work that has
50 * been completed. In this mode, the application is required to call
51 * gtk_progress_bar_set_fraction() periodically to update the progress bar.
53 * When an application has no accurate way of knowing the amount of work
54 * to do, it can use the #GtkProgressBar in activity mode, which shows
55 * activity by a block moving back and forth within the progress area. In
56 * this mode, the application is required to call gtk_progress_bar_pulse()
57 * periodically to update the progress bar.
59 * There is quite a bit of flexibility provided to control the appearance
60 * of the #GtkProgressBar. Functions are provided to control the
61 * orientation of the bar, optional text can be displayed along with
62 * the bar, and the step size used in activity mode can be set.
65 #define MIN_HORIZONTAL_BAR_WIDTH 150
66 #define MIN_HORIZONTAL_BAR_HEIGHT 20
67 #define MIN_VERTICAL_BAR_WIDTH 22
68 #define MIN_VERTICAL_BAR_HEIGHT 80
71 struct _GtkProgressBarPrivate
76 gdouble pulse_fraction;
79 guint activity_blocks;
81 GtkOrientation orientation;
83 guint activity_dir : 1;
84 guint activity_mode : 1;
101 static void gtk_progress_bar_set_property (GObject *object,
105 static void gtk_progress_bar_get_property (GObject *object,
109 static void gtk_progress_bar_get_preferred_width (GtkWidget *widget,
112 static void gtk_progress_bar_get_preferred_height (GtkWidget *widget,
116 static void gtk_progress_bar_real_update (GtkProgressBar *progress);
117 static gboolean gtk_progress_bar_draw (GtkWidget *widget,
119 static void gtk_progress_bar_act_mode_enter (GtkProgressBar *progress);
120 static void gtk_progress_bar_finalize (GObject *object);
121 static void gtk_progress_bar_set_orientation (GtkProgressBar *progress,
122 GtkOrientation orientation);
124 G_DEFINE_TYPE_WITH_CODE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_WIDGET,
125 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
128 gtk_progress_bar_class_init (GtkProgressBarClass *class)
130 GObjectClass *gobject_class;
131 GtkWidgetClass *widget_class;
133 gobject_class = G_OBJECT_CLASS (class);
134 widget_class = (GtkWidgetClass *) class;
136 gobject_class->set_property = gtk_progress_bar_set_property;
137 gobject_class->get_property = gtk_progress_bar_get_property;
138 gobject_class->finalize = gtk_progress_bar_finalize;
140 widget_class->draw = gtk_progress_bar_draw;
141 widget_class->get_preferred_width = gtk_progress_bar_get_preferred_width;
142 widget_class->get_preferred_height = gtk_progress_bar_get_preferred_height;
144 g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation");
146 g_object_class_install_property (gobject_class,
148 g_param_spec_boolean ("inverted",
150 P_("Invert the direction in which the progress bar grows"),
152 GTK_PARAM_READWRITE));
154 g_object_class_install_property (gobject_class,
156 g_param_spec_double ("fraction",
158 P_("The fraction of total work that has been completed"),
160 GTK_PARAM_READWRITE));
162 g_object_class_install_property (gobject_class,
164 g_param_spec_double ("pulse-step",
166 P_("The fraction of total progress to move the bouncing block when pulsed"),
168 GTK_PARAM_READWRITE));
170 g_object_class_install_property (gobject_class,
172 g_param_spec_string ("text",
174 P_("Text to be displayed in the progress bar"),
176 GTK_PARAM_READWRITE));
179 * GtkProgressBar:show-text:
181 * Sets whether the progress bar will show text superimposed
182 * over the bar. The shown text is either the value of
183 * the #GtkProgressBar:text property or, if that is %NULL,
184 * the #GtkProgressBar:fraction value, as a percentage.
186 * To make a progress bar that is styled and sized suitably for containing
187 * text (even if the actual text is blank), set #GtkProgressBar:show-text to
188 * %TRUE and #GtkProgressBar:text to the empty string (not %NULL).
192 g_object_class_install_property (gobject_class,
194 g_param_spec_boolean ("show-text",
196 P_("Whether the progress is shown as text."),
198 GTK_PARAM_READWRITE));
201 * GtkProgressBar:ellipsize:
203 * The preferred place to ellipsize the string, if the progress bar does
204 * not have enough room to display the entire string, specified as a
205 * #PangoEllipsizeMode.
207 * Note that setting this property to a value other than
208 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progress bar requests
209 * only enough space to display the ellipsis ("..."). Another means to set a
210 * progress bar's width is gtk_widget_set_size_request().
214 g_object_class_install_property (gobject_class,
216 g_param_spec_enum ("ellipsize",
218 P_("The preferred place to ellipsize the string, if the progress bar "
219 "does not have enough room to display the entire string, if at all."),
220 PANGO_TYPE_ELLIPSIZE_MODE,
221 PANGO_ELLIPSIZE_NONE,
222 GTK_PARAM_READWRITE));
223 gtk_widget_class_install_style_property (widget_class,
224 g_param_spec_int ("xspacing",
226 P_("Extra spacing applied to the width of a progress bar."),
229 gtk_widget_class_install_style_property (widget_class,
230 g_param_spec_int ("yspacing",
232 P_("Extra spacing applied to the height of a progress bar."),
237 * GtkProgressBar:min-horizontal-bar-width:
239 * The minimum horizontal width of the progress bar.
243 gtk_widget_class_install_style_property (widget_class,
244 g_param_spec_int ("min-horizontal-bar-width",
245 P_("Minimum horizontal bar width"),
246 P_("The minimum horizontal width of the progress bar"),
247 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
250 * GtkProgressBar:min-horizontal-bar-height:
252 * Minimum horizontal height of the progress bar.
256 gtk_widget_class_install_style_property (widget_class,
257 g_param_spec_int ("min-horizontal-bar-height",
258 P_("Minimum horizontal bar height"),
259 P_("Minimum horizontal height of the progress bar"),
260 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
263 * GtkProgressBar:min-vertical-bar-width:
265 * The minimum vertical width of the progress bar.
269 gtk_widget_class_install_style_property (widget_class,
270 g_param_spec_int ("min-vertical-bar-width",
271 P_("Minimum vertical bar width"),
272 P_("The minimum vertical width of the progress bar"),
273 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
276 * GtkProgressBar:min-vertical-bar-height:
278 * The minimum vertical height of the progress bar.
282 gtk_widget_class_install_style_property (widget_class,
283 g_param_spec_int ("min-vertical-bar-height",
284 P_("Minimum vertical bar height"),
285 P_("The minimum vertical height of the progress bar"),
286 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
289 g_type_class_add_private (class, sizeof (GtkProgressBarPrivate));
291 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PROGRESS_BAR_ACCESSIBLE);
295 gtk_progress_bar_init (GtkProgressBar *pbar)
297 GtkProgressBarPrivate *priv;
299 pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
300 GTK_TYPE_PROGRESS_BAR,
301 GtkProgressBarPrivate);
304 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
305 priv->inverted = FALSE;
306 priv->pulse_fraction = 0.1;
307 priv->activity_pos = 0;
308 priv->activity_dir = 1;
309 priv->activity_blocks = 5;
310 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
311 priv->show_text = FALSE;
314 priv->fraction = 0.0;
316 gtk_widget_set_has_window (GTK_WIDGET (pbar), FALSE);
320 gtk_progress_bar_set_property (GObject *object,
325 GtkProgressBar *pbar;
327 pbar = GTK_PROGRESS_BAR (object);
331 case PROP_ORIENTATION:
332 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
335 gtk_progress_bar_set_inverted (pbar, g_value_get_boolean (value));
338 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
340 case PROP_PULSE_STEP:
341 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
344 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
347 gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
350 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
359 gtk_progress_bar_get_property (GObject *object,
364 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
365 GtkProgressBarPrivate* priv = pbar->priv;
369 case PROP_ORIENTATION:
370 g_value_set_enum (value, priv->orientation);
373 g_value_set_boolean (value, priv->inverted);
376 g_value_set_double (value, priv->fraction);
378 case PROP_PULSE_STEP:
379 g_value_set_double (value, priv->pulse_fraction);
382 g_value_set_string (value, priv->text);
385 g_value_set_boolean (value, priv->show_text);
388 g_value_set_enum (value, priv->ellipsize);
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
397 * gtk_progress_bar_new:
399 * Creates a new #GtkProgressBar.
401 * Returns: a #GtkProgressBar.
404 gtk_progress_bar_new (void)
408 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
414 gtk_progress_bar_real_update (GtkProgressBar *pbar)
416 GtkProgressBarPrivate *priv;
419 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
422 widget = GTK_WIDGET (pbar);
424 if (priv->activity_mode)
426 /* advance the block */
427 if (priv->activity_dir == 0)
429 priv->activity_pos += priv->pulse_fraction;
430 if (priv->activity_pos > 1.0)
432 priv->activity_pos = 1.0;
433 priv->activity_dir = 1;
438 priv->activity_pos -= priv->pulse_fraction;
439 if (priv->activity_pos <= 0)
441 priv->activity_pos = 0;
442 priv->activity_dir = 0;
446 gtk_widget_queue_draw (widget);
450 gtk_progress_bar_finalize (GObject *object)
452 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
453 GtkProgressBarPrivate *priv = pbar->priv;
457 G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
461 get_current_text (GtkProgressBar *pbar)
463 GtkProgressBarPrivate *priv = pbar->priv;
466 return g_strdup (priv->text);
468 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
472 gtk_progress_bar_get_preferred_width (GtkWidget *widget,
476 GtkProgressBar *pbar;
477 GtkProgressBarPrivate *priv;
478 GtkStyleContext *style_context;
482 PangoRectangle logical_rect;
488 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
490 style_context = gtk_widget_get_style_context (widget);
491 state = gtk_widget_get_state_flags (widget);
492 gtk_style_context_get_padding (style_context, state, &padding);
494 gtk_widget_style_get (widget,
495 "xspacing", &xspacing,
498 pbar = GTK_PROGRESS_BAR (widget);
501 width = padding.left + padding.right + xspacing;
505 buf = get_current_text (pbar);
506 layout = gtk_widget_create_pango_layout (widget, buf);
508 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
512 PangoContext *context;
513 PangoFontMetrics *metrics;
516 /* The minimum size for ellipsized text is ~ 3 chars */
517 context = pango_layout_get_context (layout);
518 metrics = pango_context_get_metrics (context,
519 pango_context_get_font_description (context),
520 pango_context_get_language (context));
522 char_width = pango_font_metrics_get_approximate_char_width (metrics);
523 pango_font_metrics_unref (metrics);
525 width += PANGO_PIXELS (char_width) * 3;
528 width += logical_rect.width;
530 g_object_unref (layout);
534 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
535 gtk_widget_style_get (widget,
536 "min-horizontal-bar-width", &min_width,
539 gtk_widget_style_get (widget,
540 "min-vertical-bar-width", &min_width,
543 *minimum = *natural = MAX (min_width, width);
547 gtk_progress_bar_get_preferred_height (GtkWidget *widget,
551 GtkProgressBar *pbar;
552 GtkProgressBarPrivate *priv;
553 GtkStyleContext *context;
557 PangoRectangle logical_rect;
563 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
565 context = gtk_widget_get_style_context (widget);
566 state = gtk_widget_get_state_flags (widget);
567 gtk_style_context_get_padding (context, state, &padding);
569 gtk_widget_style_get (widget,
570 "yspacing", &yspacing,
573 pbar = GTK_PROGRESS_BAR (widget);
576 height = padding.top + padding.bottom + yspacing;
580 buf = get_current_text (pbar);
581 layout = gtk_widget_create_pango_layout (widget, buf);
583 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
585 height += logical_rect.height;
587 g_object_unref (layout);
591 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
592 gtk_widget_style_get (widget,
593 "min-horizontal-bar-height", &min_height,
596 gtk_widget_style_get (widget,
597 "min-vertical-bar-height", &min_height,
600 *minimum = *natural = MAX (min_height, height);
604 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
606 GtkProgressBarPrivate *priv = pbar->priv;
607 GtkStyleContext *context;
610 GtkWidget *widget = GTK_WIDGET (pbar);
611 GtkOrientation orientation;
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 orientation = priv->orientation;
619 inverted = priv->inverted;
620 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
622 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
623 inverted = !inverted;
626 /* calculate start pos */
628 if (orientation == GTK_ORIENTATION_HORIZONTAL)
632 priv->activity_pos = 0.0;
633 priv->activity_dir = 0;
637 priv->activity_pos = 1.0;
638 priv->activity_dir = 1;
645 priv->activity_pos = 0.0;
646 priv->activity_dir = 0;
650 priv->activity_pos = 1.0;
651 priv->activity_dir = 1;
657 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
658 GtkOrientation orientation,
662 GtkProgressBarPrivate *priv = pbar->priv;
663 GtkWidget *widget = GTK_WIDGET (pbar);
664 GtkStyleContext *context;
665 GtkAllocation allocation;
670 context = gtk_widget_get_style_context (widget);
671 state = gtk_style_context_get_state (context);
672 gtk_widget_get_allocation (widget, &allocation);
673 gtk_style_context_get_padding (context, state, &padding);
675 if (orientation == GTK_ORIENTATION_HORIZONTAL)
676 size = allocation.width - padding.left - padding.top;
678 size = allocation.height - padding.left - padding.top;
680 *amount = MAX (2, size / priv->activity_blocks);
681 *offset = priv->activity_pos * (size - *amount);
685 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
687 GtkOrientation orientation,
692 GtkStyleContext *context;
695 GtkWidget *widget = GTK_WIDGET (pbar);
698 context = gtk_widget_get_style_context (widget);
699 state = gtk_widget_get_state_flags (widget);
700 gtk_style_context_get_padding (context, state, &padding);
702 if (orientation == GTK_ORIENTATION_HORIZONTAL)
704 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
705 area.y = padding.top;
706 area.height = height - padding.top - padding.bottom;
710 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
711 area.x = padding.left;
712 area.width = width - padding.left - padding.right;
715 gtk_style_context_save (context);
716 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
717 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PULSE);
719 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
721 gtk_style_context_restore (context);
725 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
728 GtkOrientation orientation,
733 GtkStyleContext *context;
736 GtkWidget *widget = GTK_WIDGET (pbar);
742 context = gtk_widget_get_style_context (widget);
743 state = gtk_widget_get_state_flags (widget);
744 gtk_style_context_get_padding (context, state, &padding);
746 if (orientation == GTK_ORIENTATION_HORIZONTAL)
749 area.height = height - padding.top - padding.bottom;
750 area.y = padding.top;
753 area.x = padding.left;
755 area.x = width - amount - padding.right;
759 area.width = width - padding.left - padding.right;
760 area.height = amount;
761 area.x = padding.left;
764 area.y = padding.top;
766 area.y = height - amount - padding.bottom;
769 gtk_style_context_save (context);
770 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
772 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
774 gtk_style_context_restore (context);
778 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
782 GtkOrientation orientation,
787 GtkProgressBarPrivate *priv = pbar->priv;
788 GtkStyleContext *context;
791 GtkWidget *widget = GTK_WIDGET (pbar);
797 PangoRectangle logical_rect;
798 GdkRectangle prelight_clip, start_clip, end_clip;
799 gfloat text_xalign = 0.5;
800 gfloat text_yalign = 0.5;
802 context = gtk_widget_get_style_context (widget);
803 state = gtk_widget_get_state_flags (widget);
804 gtk_style_context_get_padding (context, state, &padding);
806 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
807 text_xalign = 1.0 - text_xalign;
809 buf = get_current_text (pbar);
811 layout = gtk_widget_create_pango_layout (widget, buf);
812 pango_layout_set_ellipsize (layout, priv->ellipsize);
814 pango_layout_set_width (layout, width * PANGO_SCALE);
816 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
818 x = padding.left + 1 + text_xalign * (width - padding.left - padding.right - 2 - logical_rect.width);
819 y = padding.top + 1 + text_yalign * (height - padding.top - padding.bottom - 2 - logical_rect.height);
821 rect.x = padding.left;
822 rect.y = padding.top;
823 rect.width = width - padding.left - padding.right;
824 rect.height = height - padding.top - padding.bottom;
826 prelight_clip = start_clip = end_clip = rect;
828 if (orientation == GTK_ORIENTATION_HORIZONTAL)
833 prelight_clip.x = offset;
834 prelight_clip.width = amount;
835 start_clip.width = prelight_clip.x - start_clip.x;
836 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
837 end_clip.width -= prelight_clip.width + start_clip.width;
842 prelight_clip.x = offset;
844 prelight_clip.x = rect.x + rect.width - amount;
845 prelight_clip.width = amount;
846 start_clip.width = prelight_clip.x - start_clip.x;
847 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
848 end_clip.width -= prelight_clip.width + start_clip.width;
856 prelight_clip.y = offset;
857 prelight_clip.height = amount;
858 start_clip.height = prelight_clip.y - start_clip.y;
859 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
860 end_clip.height -= prelight_clip.height + start_clip.height;
865 prelight_clip.y = offset;
867 prelight_clip.y = rect.y + rect.height - amount;
868 prelight_clip.height = amount;
869 start_clip.height = prelight_clip.y - start_clip.y;
870 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
871 end_clip.height -= prelight_clip.height + start_clip.height;
875 gtk_style_context_save (context);
876 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
878 if (start_clip.width > 0 && start_clip.height > 0)
881 gdk_cairo_rectangle (cr, &start_clip);
884 gtk_render_layout (context, cr, x, y, layout);
888 if (end_clip.width > 0 && end_clip.height > 0)
891 gdk_cairo_rectangle (cr, &end_clip);
894 gtk_render_layout (context, cr, x, y, layout);
898 gtk_style_context_restore (context);
901 gdk_cairo_rectangle (cr, &prelight_clip);
904 gtk_style_context_save (context);
905 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
907 gtk_render_layout (context, cr, x, y, layout);
909 gtk_style_context_restore (context);
912 g_object_unref (layout);
917 gtk_progress_bar_draw (GtkWidget *widget,
920 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
921 GtkProgressBarPrivate *priv = pbar->priv;
922 GtkOrientation orientation;
924 GtkStyleContext *context;
929 context = gtk_widget_get_style_context (widget);
930 state = gtk_widget_get_state_flags (widget);
931 gtk_style_context_get_padding (context, state, &padding);
933 orientation = priv->orientation;
934 inverted = priv->inverted;
935 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
937 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
938 inverted = !inverted;
940 width = gtk_widget_get_allocated_width (widget);
941 height = gtk_widget_get_allocated_height (widget);
943 gtk_style_context_save (context);
944 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
946 gtk_render_background (context, cr, 0, 0, width, height);
947 gtk_render_frame (context, cr, 0, 0, width, height);
949 gtk_style_context_restore (context);
951 if (priv->activity_mode)
953 gtk_progress_bar_paint_activity (pbar, cr,
954 orientation, inverted,
962 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
963 gtk_progress_bar_paint_text (pbar, cr,
965 orientation, inverted,
974 if (orientation == GTK_ORIENTATION_HORIZONTAL)
975 space = width - padding.left - padding.right;
977 space = height - padding.top - padding.bottom;
979 amount = space * gtk_progress_bar_get_fraction (pbar);
981 gtk_progress_bar_paint_continuous (pbar, cr, amount, orientation, inverted, width, height);
984 gtk_progress_bar_paint_text (pbar, cr, -1, amount, orientation, inverted, width, height);
991 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
992 gboolean activity_mode)
994 GtkProgressBarPrivate *priv = pbar->priv;
996 activity_mode = !!activity_mode;
998 if (priv->activity_mode != activity_mode)
1000 priv->activity_mode = activity_mode;
1002 if (priv->activity_mode)
1003 gtk_progress_bar_act_mode_enter (pbar);
1005 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1010 * gtk_progress_bar_set_fraction:
1011 * @pbar: a #GtkProgressBar
1012 * @fraction: fraction of the task that's been completed
1014 * Causes the progress bar to "fill in" the given fraction
1015 * of the bar. The fraction should be between 0.0 and 1.0,
1019 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1022 GtkProgressBarPrivate* priv;
1024 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1028 priv->fraction = CLAMP(fraction, 0.0, 1.0);
1029 gtk_progress_bar_set_activity_mode (pbar, FALSE);
1030 gtk_progress_bar_real_update (pbar);
1032 g_object_notify (G_OBJECT (pbar), "fraction");
1036 * gtk_progress_bar_pulse:
1037 * @pbar: a #GtkProgressBar
1039 * Indicates that some progress has been made, but you don't know how much.
1040 * Causes the progress bar to enter "activity mode," where a block
1041 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1042 * causes the block to move by a little bit (the amount of movement
1043 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1046 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1048 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1050 gtk_progress_bar_set_activity_mode (pbar, TRUE);
1051 gtk_progress_bar_real_update (pbar);
1055 * gtk_progress_bar_set_text:
1056 * @pbar: a #GtkProgressBar
1057 * @text: (allow-none): a UTF-8 string, or %NULL
1059 * Causes the given @text to appear superimposed on the progress bar.
1061 * If @text is %NULL and #GtkProgressBar:show-text is %TRUE, the current
1062 * value of #GtkProgressBar:fraction will be displayed as a percentage.
1064 * If @text is non-%NULL and #GtkProgressBar:show-text is %TRUE, the text will
1065 * be displayed. In this case, it will not display the progress percentage.
1066 * If @text is the empty string, the progress bar will still be styled and sized
1067 * suitably for containing text, as long as #GtkProgressBar:show-text is %TRUE.
1070 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1073 GtkProgressBarPrivate *priv;
1075 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1079 /* Don't notify again if nothing's changed. */
1080 if (g_strcmp0 (priv->text, text) == 0)
1083 g_free (priv->text);
1084 priv->text = g_strdup (text);
1086 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1088 g_object_notify (G_OBJECT (pbar), "text");
1092 * gtk_progress_bar_set_show_text:
1093 * @pbar: a #GtkProgressBar
1094 * @show_text: whether to show superimposed text
1096 * Sets whether the progress bar will show text superimposed
1097 * over the bar. The shown text is either the value of
1098 * the #GtkProgressBar:text property or, if that is %NULL,
1099 * the #GtkProgressBar:fraction value, as a percentage.
1101 * To make a progress bar that is styled and sized suitably for containing
1102 * text (even if the actual text is blank), set #GtkProgressBar:show-text to
1103 * %TRUE and #GtkProgressBar:text to the empty string (not %NULL).
1108 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1111 GtkProgressBarPrivate *priv;
1113 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1117 show_text = !!show_text;
1119 if (priv->show_text != show_text)
1121 priv->show_text = show_text;
1123 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1125 g_object_notify (G_OBJECT (pbar), "show-text");
1130 * gtk_progress_bar_get_show_text:
1131 * @pbar: a #GtkProgressBar
1133 * Gets the value of the #GtkProgressBar:show-text property.
1134 * See gtk_progress_bar_set_show_text().
1136 * Returns: %TRUE if text is shown in the progress bar
1141 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1143 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1145 return pbar->priv->show_text;
1149 * gtk_progress_bar_set_pulse_step:
1150 * @pbar: a #GtkProgressBar
1151 * @fraction: fraction between 0.0 and 1.0
1153 * Sets the fraction of total progress bar length to move the
1154 * bouncing block for each call to gtk_progress_bar_pulse().
1157 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1160 GtkProgressBarPrivate *priv;
1162 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1166 priv->pulse_fraction = fraction;
1168 g_object_notify (G_OBJECT (pbar), "pulse-step");
1172 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1173 GtkOrientation orientation)
1175 GtkProgressBarPrivate *priv = pbar->priv;
1177 if (priv->orientation != orientation)
1179 priv->orientation = orientation;
1180 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (pbar));
1182 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1187 * gtk_progress_bar_set_inverted:
1188 * @pbar: a #GtkProgressBar
1189 * @inverted: %TRUE to invert the progress bar
1191 * Progress bars normally grow from top to bottom or left to right.
1192 * Inverted progress bars grow in the opposite direction.
1195 gtk_progress_bar_set_inverted (GtkProgressBar *pbar,
1198 GtkProgressBarPrivate *priv;
1200 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1204 if (priv->inverted != inverted)
1206 priv->inverted = inverted;
1208 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1210 g_object_notify (G_OBJECT (pbar), "inverted");
1215 * gtk_progress_bar_get_text:
1216 * @pbar: a #GtkProgressBar
1218 * Retrieves the text displayed superimposed on the progress bar,
1219 * if any, otherwise %NULL. The return value is a reference
1220 * to the text, not a copy of it, so will become invalid
1221 * if you change the text in the progress bar.
1223 * Return value: text, or %NULL; this string is owned by the widget
1224 * and should not be modified or freed.
1227 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1229 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1231 return pbar->priv->text;
1235 * gtk_progress_bar_get_fraction:
1236 * @pbar: a #GtkProgressBar
1238 * Returns the current fraction of the task that's been completed.
1240 * Return value: a fraction from 0.0 to 1.0
1243 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1245 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1247 return pbar->priv->fraction;
1251 * gtk_progress_bar_get_pulse_step:
1252 * @pbar: a #GtkProgressBar
1254 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step().
1256 * Return value: a fraction from 0.0 to 1.0
1259 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1261 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1263 return pbar->priv->pulse_fraction;
1267 * gtk_progress_bar_get_inverted:
1268 * @pbar: a #GtkProgressBar
1270 * Gets the value set by gtk_progress_bar_set_inverted().
1272 * Return value: %TRUE if the progress bar is inverted
1275 gtk_progress_bar_get_inverted (GtkProgressBar *pbar)
1277 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1279 return pbar->priv->inverted;
1283 * gtk_progress_bar_set_ellipsize:
1284 * @pbar: a #GtkProgressBar
1285 * @mode: a #PangoEllipsizeMode
1287 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1288 * if there is not enough space to render the entire string.
1293 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1294 PangoEllipsizeMode mode)
1296 GtkProgressBarPrivate *priv;
1298 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1299 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1300 mode <= PANGO_ELLIPSIZE_END);
1304 if ((PangoEllipsizeMode)priv->ellipsize != mode)
1306 priv->ellipsize = mode;
1308 g_object_notify (G_OBJECT (pbar), "ellipsize");
1309 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1314 * gtk_progress_bar_get_ellipsize:
1315 * @pbar: a #GtkProgressBar
1317 * Returns the ellipsizing position of the progress bar.
1318 * See gtk_progress_bar_set_ellipsize().
1320 * Return value: #PangoEllipsizeMode
1325 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1327 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1329 return pbar->priv->ellipsize;