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;
82 GtkOrientation orientation;
84 guint activity_dir : 1;
85 guint activity_mode : 1;
102 static void gtk_progress_bar_set_property (GObject *object,
106 static void gtk_progress_bar_get_property (GObject *object,
110 static void gtk_progress_bar_get_preferred_width (GtkWidget *widget,
113 static void gtk_progress_bar_get_preferred_height (GtkWidget *widget,
117 static void gtk_progress_bar_real_update (GtkProgressBar *progress);
118 static gboolean gtk_progress_bar_draw (GtkWidget *widget,
120 static void gtk_progress_bar_act_mode_enter (GtkProgressBar *progress);
121 static void gtk_progress_bar_finalize (GObject *object);
122 static void gtk_progress_bar_set_orientation (GtkProgressBar *progress,
123 GtkOrientation orientation);
125 G_DEFINE_TYPE_WITH_CODE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_WIDGET,
126 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
129 gtk_progress_bar_class_init (GtkProgressBarClass *class)
131 GObjectClass *gobject_class;
132 GtkWidgetClass *widget_class;
134 gobject_class = G_OBJECT_CLASS (class);
135 widget_class = (GtkWidgetClass *) class;
137 gobject_class->set_property = gtk_progress_bar_set_property;
138 gobject_class->get_property = gtk_progress_bar_get_property;
139 gobject_class->finalize = gtk_progress_bar_finalize;
141 widget_class->draw = gtk_progress_bar_draw;
142 widget_class->get_preferred_width = gtk_progress_bar_get_preferred_width;
143 widget_class->get_preferred_height = gtk_progress_bar_get_preferred_height;
145 g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation");
147 g_object_class_install_property (gobject_class,
149 g_param_spec_boolean ("inverted",
151 P_("Invert the direction in which the progress bar grows"),
153 GTK_PARAM_READWRITE));
155 g_object_class_install_property (gobject_class,
157 g_param_spec_double ("fraction",
159 P_("The fraction of total work that has been completed"),
161 GTK_PARAM_READWRITE));
163 g_object_class_install_property (gobject_class,
165 g_param_spec_double ("pulse-step",
167 P_("The fraction of total progress to move the bouncing block when pulsed"),
169 GTK_PARAM_READWRITE));
171 g_object_class_install_property (gobject_class,
173 g_param_spec_string ("text",
175 P_("Text to be displayed in the progress bar"),
177 GTK_PARAM_READWRITE));
180 * GtkProgressBar:show-text:
182 * Sets whether the progress bar will show text superimposed
183 * over the bar. The shown text is either the value of
184 * the #GtkProgressBar:text property or, if that is %NULL,
185 * the #GtkProgressBar:fraction value, as a percentage.
187 * To make a progress bar that is styled and sized suitably for containing
188 * text (even if the actual text is blank), set #GtkProgressBar:show-text to
189 * %TRUE and #GtkProgressBar:text to the empty string (not %NULL).
193 g_object_class_install_property (gobject_class,
195 g_param_spec_boolean ("show-text",
197 P_("Whether the progress is shown as text."),
199 GTK_PARAM_READWRITE));
202 * GtkProgressBar:ellipsize:
204 * The preferred place to ellipsize the string, if the progress bar does
205 * not have enough room to display the entire string, specified as a
206 * #PangoEllipsizeMode.
208 * Note that setting this property to a value other than
209 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progress bar requests
210 * only enough space to display the ellipsis ("..."). Another means to set a
211 * progress bar's width is gtk_widget_set_size_request().
215 g_object_class_install_property (gobject_class,
217 g_param_spec_enum ("ellipsize",
219 P_("The preferred place to ellipsize the string, if the progress bar "
220 "does not have enough room to display the entire string, if at all."),
221 PANGO_TYPE_ELLIPSIZE_MODE,
222 PANGO_ELLIPSIZE_NONE,
223 GTK_PARAM_READWRITE));
224 gtk_widget_class_install_style_property (widget_class,
225 g_param_spec_int ("xspacing",
227 P_("Extra spacing applied to the width of a progress bar."),
230 gtk_widget_class_install_style_property (widget_class,
231 g_param_spec_int ("yspacing",
233 P_("Extra spacing applied to the height of a progress bar."),
238 * GtkProgressBar:min-horizontal-bar-width:
240 * The minimum horizontal width of the progress bar.
244 gtk_widget_class_install_style_property (widget_class,
245 g_param_spec_int ("min-horizontal-bar-width",
246 P_("Minimum horizontal bar width"),
247 P_("The minimum horizontal width of the progress bar"),
248 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
251 * GtkProgressBar:min-horizontal-bar-height:
253 * Minimum horizontal height of the progress bar.
257 gtk_widget_class_install_style_property (widget_class,
258 g_param_spec_int ("min-horizontal-bar-height",
259 P_("Minimum horizontal bar height"),
260 P_("Minimum horizontal height of the progress bar"),
261 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
264 * GtkProgressBar:min-vertical-bar-width:
266 * The minimum vertical width of the progress bar.
270 gtk_widget_class_install_style_property (widget_class,
271 g_param_spec_int ("min-vertical-bar-width",
272 P_("Minimum vertical bar width"),
273 P_("The minimum vertical width of the progress bar"),
274 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
277 * GtkProgressBar:min-vertical-bar-height:
279 * The minimum vertical height of the progress bar.
283 gtk_widget_class_install_style_property (widget_class,
284 g_param_spec_int ("min-vertical-bar-height",
285 P_("Minimum vertical bar height"),
286 P_("The minimum vertical height of the progress bar"),
287 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
290 g_type_class_add_private (class, sizeof (GtkProgressBarPrivate));
292 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PROGRESS_BAR_ACCESSIBLE);
296 gtk_progress_bar_init (GtkProgressBar *pbar)
298 GtkProgressBarPrivate *priv;
300 pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
301 GTK_TYPE_PROGRESS_BAR,
302 GtkProgressBarPrivate);
305 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
306 priv->inverted = FALSE;
307 priv->pulse_fraction = 0.1;
308 priv->activity_pos = 0;
309 priv->activity_dir = 1;
310 priv->activity_step = 3;
311 priv->activity_blocks = 5;
312 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
313 priv->show_text = FALSE;
316 priv->fraction = 0.0;
318 gtk_widget_set_has_window (GTK_WIDGET (pbar), FALSE);
322 gtk_progress_bar_set_property (GObject *object,
327 GtkProgressBar *pbar;
329 pbar = GTK_PROGRESS_BAR (object);
333 case PROP_ORIENTATION:
334 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
337 gtk_progress_bar_set_inverted (pbar, g_value_get_boolean (value));
340 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
342 case PROP_PULSE_STEP:
343 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
346 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
349 gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
352 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361 gtk_progress_bar_get_property (GObject *object,
366 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
367 GtkProgressBarPrivate* priv = pbar->priv;
371 case PROP_ORIENTATION:
372 g_value_set_enum (value, priv->orientation);
375 g_value_set_boolean (value, priv->inverted);
378 g_value_set_double (value, priv->fraction);
380 case PROP_PULSE_STEP:
381 g_value_set_double (value, priv->pulse_fraction);
384 g_value_set_string (value, priv->text);
387 g_value_set_boolean (value, priv->show_text);
390 g_value_set_enum (value, priv->ellipsize);
393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
399 * gtk_progress_bar_new:
401 * Creates a new #GtkProgressBar.
403 * Returns: a #GtkProgressBar.
406 gtk_progress_bar_new (void)
410 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
416 gtk_progress_bar_real_update (GtkProgressBar *pbar)
418 GtkProgressBarPrivate *priv;
421 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
424 widget = GTK_WIDGET (pbar);
426 if (priv->activity_mode)
428 GtkAllocation allocation;
429 GtkStyleContext *context;
434 gtk_widget_get_allocation (widget, &allocation);
435 context = gtk_widget_get_style_context (widget);
436 state = gtk_widget_get_state_flags (widget);
437 gtk_style_context_get_padding (context, state, &padding);
439 /* advance the block */
440 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
442 /* Update our activity step. */
443 priv->activity_step = allocation.width * priv->pulse_fraction;
445 size = MAX (2, allocation.width / priv->activity_blocks);
447 if (priv->activity_dir == 0)
449 priv->activity_pos += priv->activity_step;
450 if (priv->activity_pos + size >= allocation.width - padding.left)
452 priv->activity_pos = allocation.width - padding.left - size;
453 priv->activity_dir = 1;
458 priv->activity_pos -= priv->activity_step;
459 if (priv->activity_pos <= padding.left)
461 priv->activity_pos = padding.left;
462 priv->activity_dir = 0;
468 /* Update our activity step. */
469 priv->activity_step = allocation.height * priv->pulse_fraction;
471 size = MAX (2, allocation.height / priv->activity_blocks);
473 if (priv->activity_dir == 0)
475 priv->activity_pos += priv->activity_step;
476 if (priv->activity_pos + size >= allocation.height - padding.top)
478 priv->activity_pos = allocation.height - padding.top - size;
479 priv->activity_dir = 1;
484 priv->activity_pos -= priv->activity_step;
485 if (priv->activity_pos <= padding.top)
487 priv->activity_pos = padding.top;
488 priv->activity_dir = 0;
493 gtk_widget_queue_draw (widget);
497 gtk_progress_bar_finalize (GObject *object)
499 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
500 GtkProgressBarPrivate *priv = pbar->priv;
504 G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
508 get_current_text (GtkProgressBar *pbar)
510 GtkProgressBarPrivate *priv = pbar->priv;
513 return g_strdup (priv->text);
515 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
519 gtk_progress_bar_get_preferred_width (GtkWidget *widget,
523 GtkProgressBar *pbar;
524 GtkProgressBarPrivate *priv;
525 GtkStyleContext *style_context;
529 PangoRectangle logical_rect;
535 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
537 style_context = gtk_widget_get_style_context (widget);
538 state = gtk_widget_get_state_flags (widget);
539 gtk_style_context_get_padding (style_context, state, &padding);
541 gtk_widget_style_get (widget,
542 "xspacing", &xspacing,
545 pbar = GTK_PROGRESS_BAR (widget);
548 width = padding.left + padding.right + xspacing;
552 buf = get_current_text (pbar);
553 layout = gtk_widget_create_pango_layout (widget, buf);
555 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
559 PangoContext *context;
560 PangoFontMetrics *metrics;
563 /* The minimum size for ellipsized text is ~ 3 chars */
564 context = pango_layout_get_context (layout);
565 metrics = pango_context_get_metrics (context,
566 pango_context_get_font_description (context),
567 pango_context_get_language (context));
569 char_width = pango_font_metrics_get_approximate_char_width (metrics);
570 pango_font_metrics_unref (metrics);
572 width += PANGO_PIXELS (char_width) * 3;
575 width += logical_rect.width;
577 g_object_unref (layout);
581 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
582 gtk_widget_style_get (widget,
583 "min-horizontal-bar-width", &min_width,
586 gtk_widget_style_get (widget,
587 "min-vertical-bar-width", &min_width,
590 *minimum = *natural = MAX (min_width, width);
594 gtk_progress_bar_get_preferred_height (GtkWidget *widget,
598 GtkProgressBar *pbar;
599 GtkProgressBarPrivate *priv;
600 GtkStyleContext *context;
604 PangoRectangle logical_rect;
610 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
612 context = gtk_widget_get_style_context (widget);
613 state = gtk_widget_get_state_flags (widget);
614 gtk_style_context_get_padding (context, state, &padding);
616 gtk_widget_style_get (widget,
617 "yspacing", &yspacing,
620 pbar = GTK_PROGRESS_BAR (widget);
623 height = padding.top + padding.bottom + yspacing;
627 buf = get_current_text (pbar);
628 layout = gtk_widget_create_pango_layout (widget, buf);
630 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
632 height += logical_rect.height;
634 g_object_unref (layout);
638 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
639 gtk_widget_style_get (widget,
640 "min-horizontal-bar-height", &min_height,
643 gtk_widget_style_get (widget,
644 "min-vertical-bar-height", &min_height,
647 *minimum = *natural = MAX (min_height, height);
651 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
653 GtkProgressBarPrivate *priv = pbar->priv;
654 GtkAllocation allocation;
655 GtkStyleContext *context;
658 GtkWidget *widget = GTK_WIDGET (pbar);
659 GtkOrientation orientation;
662 context = gtk_widget_get_style_context (widget);
663 state = gtk_widget_get_state_flags (widget);
664 gtk_style_context_get_padding (context, state, &padding);
666 orientation = priv->orientation;
667 inverted = priv->inverted;
668 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
670 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
671 inverted = !inverted;
674 /* calculate start pos */
676 if (orientation == GTK_ORIENTATION_HORIZONTAL)
680 priv->activity_pos = padding.left;
681 priv->activity_dir = 0;
685 gtk_widget_get_allocation (widget, &allocation);
686 priv->activity_pos = allocation.width - padding.left -
687 (allocation.height - padding.top - padding.bottom);
688 priv->activity_dir = 1;
695 priv->activity_pos = padding.top;
696 priv->activity_dir = 0;
700 gtk_widget_get_allocation (widget, &allocation);
701 priv->activity_pos = allocation.height - padding.top -
702 (allocation.width - padding.left - padding.right);
703 priv->activity_dir = 1;
709 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
710 GtkOrientation orientation,
714 GtkProgressBarPrivate *priv = pbar->priv;
715 GtkAllocation allocation;
716 GtkWidget *widget = GTK_WIDGET (pbar);
718 *offset = priv->activity_pos;
720 gtk_widget_get_allocation (widget, &allocation);
722 if (orientation == GTK_ORIENTATION_HORIZONTAL)
723 *amount = MAX (2, allocation.width / priv->activity_blocks);
725 *amount = MAX (2, allocation.height / priv->activity_blocks);
729 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
731 GtkOrientation orientation,
736 GtkStyleContext *context;
739 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)
748 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
749 area.y = padding.top;
750 area.height = height - padding.top - padding.bottom;
754 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
755 area.x = padding.left;
756 area.width = width - padding.left - padding.right;
759 gtk_style_context_save (context);
760 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
761 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PULSE);
763 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
765 gtk_style_context_restore (context);
769 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
772 GtkOrientation orientation,
777 GtkStyleContext *context;
780 GtkWidget *widget = GTK_WIDGET (pbar);
786 context = gtk_widget_get_style_context (widget);
787 state = gtk_widget_get_state_flags (widget);
788 gtk_style_context_get_padding (context, state, &padding);
790 if (orientation == GTK_ORIENTATION_HORIZONTAL)
793 area.height = height - padding.top - padding.bottom;
794 area.y = padding.top;
797 area.x = padding.left;
799 area.x = width - amount - padding.right;
803 area.width = width - padding.left - padding.right;
804 area.height = amount;
805 area.x = padding.left;
808 area.y = padding.top;
810 area.y = height - amount - padding.bottom;
813 gtk_style_context_save (context);
814 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
816 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
818 gtk_style_context_restore (context);
822 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
826 GtkOrientation orientation,
831 GtkProgressBarPrivate *priv = pbar->priv;
832 GtkStyleContext *context;
835 GtkWidget *widget = GTK_WIDGET (pbar);
841 PangoRectangle logical_rect;
842 GdkRectangle prelight_clip, start_clip, end_clip;
843 gfloat text_xalign = 0.5;
844 gfloat text_yalign = 0.5;
846 context = gtk_widget_get_style_context (widget);
847 state = gtk_widget_get_state_flags (widget);
848 gtk_style_context_get_padding (context, state, &padding);
850 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
851 text_xalign = 1.0 - text_xalign;
853 buf = get_current_text (pbar);
855 layout = gtk_widget_create_pango_layout (widget, buf);
856 pango_layout_set_ellipsize (layout, priv->ellipsize);
858 pango_layout_set_width (layout, width * PANGO_SCALE);
860 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
862 x = padding.left + 1 + text_xalign * (width - padding.left - padding.right - 2 - logical_rect.width);
863 y = padding.top + 1 + text_yalign * (height - padding.top - padding.bottom - 2 - logical_rect.height);
865 rect.x = padding.left;
866 rect.y = padding.top;
867 rect.width = width - padding.left - padding.right;
868 rect.height = height - padding.top - padding.bottom;
870 prelight_clip = start_clip = end_clip = rect;
872 if (orientation == GTK_ORIENTATION_HORIZONTAL)
877 prelight_clip.x = offset;
878 prelight_clip.width = amount;
879 start_clip.width = prelight_clip.x - start_clip.x;
880 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
881 end_clip.width -= prelight_clip.width + start_clip.width;
886 prelight_clip.x = offset;
888 prelight_clip.x = rect.x + rect.width - amount;
889 prelight_clip.width = amount;
890 start_clip.width = prelight_clip.x - start_clip.x;
891 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
892 end_clip.width -= prelight_clip.width + start_clip.width;
900 prelight_clip.y = offset;
901 prelight_clip.height = amount;
902 start_clip.height = prelight_clip.y - start_clip.y;
903 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
904 end_clip.height -= prelight_clip.height + start_clip.height;
909 prelight_clip.y = offset;
911 prelight_clip.y = rect.y + rect.height - amount;
912 prelight_clip.height = amount;
913 start_clip.height = prelight_clip.y - start_clip.y;
914 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
915 end_clip.height -= prelight_clip.height + start_clip.height;
919 gtk_style_context_save (context);
920 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
922 if (start_clip.width > 0 && start_clip.height > 0)
925 gdk_cairo_rectangle (cr, &start_clip);
928 gtk_render_layout (context, cr, x, y, layout);
932 if (end_clip.width > 0 && end_clip.height > 0)
935 gdk_cairo_rectangle (cr, &end_clip);
938 gtk_render_layout (context, cr, x, y, layout);
942 gtk_style_context_restore (context);
945 gdk_cairo_rectangle (cr, &prelight_clip);
948 gtk_style_context_save (context);
949 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
951 gtk_render_layout (context, cr, x, y, layout);
953 gtk_style_context_restore (context);
956 g_object_unref (layout);
961 gtk_progress_bar_draw (GtkWidget *widget,
964 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
965 GtkProgressBarPrivate *priv = pbar->priv;
966 GtkOrientation orientation;
968 GtkStyleContext *context;
973 context = gtk_widget_get_style_context (widget);
974 state = gtk_widget_get_state_flags (widget);
975 gtk_style_context_get_padding (context, state, &padding);
977 orientation = priv->orientation;
978 inverted = priv->inverted;
979 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
981 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
982 inverted = !inverted;
984 width = gtk_widget_get_allocated_width (widget);
985 height = gtk_widget_get_allocated_height (widget);
987 gtk_style_context_save (context);
988 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
990 gtk_render_background (context, cr, 0, 0, width, height);
991 gtk_render_frame (context, cr, 0, 0, width, height);
993 gtk_style_context_restore (context);
995 if (priv->activity_mode)
997 gtk_progress_bar_paint_activity (pbar, cr,
998 orientation, inverted,
1001 if (priv->show_text)
1006 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
1007 gtk_progress_bar_paint_text (pbar, cr,
1009 orientation, inverted,
1018 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1019 space = width - padding.left - padding.right;
1021 space = height - padding.top - padding.bottom;
1023 amount = space * gtk_progress_bar_get_fraction (pbar);
1025 gtk_progress_bar_paint_continuous (pbar, cr, amount, orientation, inverted, width, height);
1027 if (priv->show_text)
1028 gtk_progress_bar_paint_text (pbar, cr, -1, amount, orientation, inverted, width, height);
1035 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
1036 gboolean activity_mode)
1038 GtkProgressBarPrivate *priv = pbar->priv;
1040 activity_mode = !!activity_mode;
1042 if (priv->activity_mode != activity_mode)
1044 priv->activity_mode = activity_mode;
1046 if (priv->activity_mode)
1047 gtk_progress_bar_act_mode_enter (pbar);
1049 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1054 * gtk_progress_bar_set_fraction:
1055 * @pbar: a #GtkProgressBar
1056 * @fraction: fraction of the task that's been completed
1058 * Causes the progress bar to "fill in" the given fraction
1059 * of the bar. The fraction should be between 0.0 and 1.0,
1063 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1066 GtkProgressBarPrivate* priv;
1068 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1072 priv->fraction = CLAMP(fraction, 0.0, 1.0);
1073 gtk_progress_bar_set_activity_mode (pbar, FALSE);
1074 gtk_progress_bar_real_update (pbar);
1076 g_object_notify (G_OBJECT (pbar), "fraction");
1080 * gtk_progress_bar_pulse:
1081 * @pbar: a #GtkProgressBar
1083 * Indicates that some progress has been made, but you don't know how much.
1084 * Causes the progress bar to enter "activity mode," where a block
1085 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1086 * causes the block to move by a little bit (the amount of movement
1087 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1090 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1092 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1094 gtk_progress_bar_set_activity_mode (pbar, TRUE);
1095 gtk_progress_bar_real_update (pbar);
1099 * gtk_progress_bar_set_text:
1100 * @pbar: a #GtkProgressBar
1101 * @text: (allow-none): a UTF-8 string, or %NULL
1103 * Causes the given @text to appear superimposed on the progress bar.
1105 * If @text is %NULL and #GtkProgressBar:show-text is %TRUE, the current
1106 * value of #GtkProgressBar:fraction will be displayed as a percentage.
1108 * If @text is non-%NULL and #GtkProgressBar:show-text is %TRUE, the text will
1109 * be displayed. In this case, it will not display the progress percentage.
1110 * If @text is the empty string, the progress bar will still be styled and sized
1111 * suitably for containing text, as long as #GtkProgressBar:show-text is %TRUE.
1114 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1117 GtkProgressBarPrivate *priv;
1119 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1123 /* Don't notify again if nothing's changed. */
1124 if (g_strcmp0 (priv->text, text) == 0)
1127 g_free (priv->text);
1128 priv->text = g_strdup (text);
1130 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1132 g_object_notify (G_OBJECT (pbar), "text");
1136 * gtk_progress_bar_set_show_text:
1137 * @pbar: a #GtkProgressBar
1138 * @show_text: whether to show superimposed text
1140 * Sets whether the progress bar will show text superimposed
1141 * over the bar. The shown text is either the value of
1142 * the #GtkProgressBar:text property or, if that is %NULL,
1143 * the #GtkProgressBar:fraction value, as a percentage.
1145 * To make a progress bar that is styled and sized suitably for containing
1146 * text (even if the actual text is blank), set #GtkProgressBar:show-text to
1147 * %TRUE and #GtkProgressBar:text to the empty string (not %NULL).
1152 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1155 GtkProgressBarPrivate *priv;
1157 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1161 show_text = !!show_text;
1163 if (priv->show_text != show_text)
1165 priv->show_text = show_text;
1167 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1169 g_object_notify (G_OBJECT (pbar), "show-text");
1174 * gtk_progress_bar_get_show_text:
1175 * @pbar: a #GtkProgressBar
1177 * Gets the value of the #GtkProgressBar:show-text property.
1178 * See gtk_progress_bar_set_show_text().
1180 * Returns: %TRUE if text is shown in the progress bar
1185 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1187 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1189 return pbar->priv->show_text;
1193 * gtk_progress_bar_set_pulse_step:
1194 * @pbar: a #GtkProgressBar
1195 * @fraction: fraction between 0.0 and 1.0
1197 * Sets the fraction of total progress bar length to move the
1198 * bouncing block for each call to gtk_progress_bar_pulse().
1201 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1204 GtkProgressBarPrivate *priv;
1206 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1210 priv->pulse_fraction = fraction;
1212 g_object_notify (G_OBJECT (pbar), "pulse-step");
1216 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1217 GtkOrientation orientation)
1219 GtkProgressBarPrivate *priv = pbar->priv;
1221 if (priv->orientation != orientation)
1223 priv->orientation = orientation;
1224 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (pbar));
1226 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1231 * gtk_progress_bar_set_inverted:
1232 * @pbar: a #GtkProgressBar
1233 * @inverted: %TRUE to invert the progress bar
1235 * Progress bars normally grow from top to bottom or left to right.
1236 * Inverted progress bars grow in the opposite direction.
1239 gtk_progress_bar_set_inverted (GtkProgressBar *pbar,
1242 GtkProgressBarPrivate *priv;
1244 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1248 if (priv->inverted != inverted)
1250 priv->inverted = inverted;
1252 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1254 g_object_notify (G_OBJECT (pbar), "inverted");
1259 * gtk_progress_bar_get_text:
1260 * @pbar: a #GtkProgressBar
1262 * Retrieves the text displayed superimposed on the progress bar,
1263 * if any, otherwise %NULL. The return value is a reference
1264 * to the text, not a copy of it, so will become invalid
1265 * if you change the text in the progress bar.
1267 * Return value: text, or %NULL; this string is owned by the widget
1268 * and should not be modified or freed.
1271 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1273 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1275 return pbar->priv->text;
1279 * gtk_progress_bar_get_fraction:
1280 * @pbar: a #GtkProgressBar
1282 * Returns the current fraction of the task that's been completed.
1284 * Return value: a fraction from 0.0 to 1.0
1287 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1289 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1291 return pbar->priv->fraction;
1295 * gtk_progress_bar_get_pulse_step:
1296 * @pbar: a #GtkProgressBar
1298 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step().
1300 * Return value: a fraction from 0.0 to 1.0
1303 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1305 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1307 return pbar->priv->pulse_fraction;
1311 * gtk_progress_bar_get_inverted:
1312 * @pbar: a #GtkProgressBar
1314 * Gets the value set by gtk_progress_bar_set_inverted().
1316 * Return value: %TRUE if the progress bar is inverted
1319 gtk_progress_bar_get_inverted (GtkProgressBar *pbar)
1321 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1323 return pbar->priv->inverted;
1327 * gtk_progress_bar_set_ellipsize:
1328 * @pbar: a #GtkProgressBar
1329 * @mode: a #PangoEllipsizeMode
1331 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1332 * if there is not enough space to render the entire string.
1337 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1338 PangoEllipsizeMode mode)
1340 GtkProgressBarPrivate *priv;
1342 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1343 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1344 mode <= PANGO_ELLIPSIZE_END);
1348 if ((PangoEllipsizeMode)priv->ellipsize != mode)
1350 priv->ellipsize = mode;
1352 g_object_notify (G_OBJECT (pbar), "ellipsize");
1353 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1358 * gtk_progress_bar_get_ellipsize:
1359 * @pbar: a #GtkProgressBar
1361 * Returns the ellipsizing position of the progress bar.
1362 * See gtk_progress_bar_set_ellipsize().
1364 * Return value: #PangoEllipsizeMode
1369 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1371 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1373 return pbar->priv->ellipsize;