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.
191 g_object_class_install_property (gobject_class,
193 g_param_spec_boolean ("show-text",
195 P_("Whether the progress is shown as text."),
197 GTK_PARAM_READWRITE));
200 * GtkProgressBar:ellipsize:
202 * The preferred place to ellipsize the string, if the progressbar does
203 * not have enough room to display the entire string, specified as a
204 * #PangoEllisizeMode.
206 * Note that setting this property to a value other than
207 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
208 * only enough space to display the ellipsis "...". Another means to set a
209 * progressbar's width is gtk_widget_set_size_request().
213 g_object_class_install_property (gobject_class,
215 g_param_spec_enum ("ellipsize",
217 P_("The preferred place to ellipsize the string, if the progress bar "
218 "does not have enough room to display the entire string, if at all."),
219 PANGO_TYPE_ELLIPSIZE_MODE,
220 PANGO_ELLIPSIZE_NONE,
221 GTK_PARAM_READWRITE));
222 gtk_widget_class_install_style_property (widget_class,
223 g_param_spec_int ("xspacing",
225 P_("Extra spacing applied to the width of a progress bar."),
228 gtk_widget_class_install_style_property (widget_class,
229 g_param_spec_int ("yspacing",
231 P_("Extra spacing applied to the height of a progress bar."),
236 * GtkProgressBar:min-horizontal-bar-width:
238 * The minimum horizontal width of the progress bar.
242 gtk_widget_class_install_style_property (widget_class,
243 g_param_spec_int ("min-horizontal-bar-width",
244 P_("Minimum horizontal bar width"),
245 P_("The minimum horizontal width of the progress bar"),
246 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
249 * GtkProgressBar:min-horizontal-bar-height:
251 * Minimum horizontal height of the progress bar.
255 gtk_widget_class_install_style_property (widget_class,
256 g_param_spec_int ("min-horizontal-bar-height",
257 P_("Minimum horizontal bar height"),
258 P_("Minimum horizontal height of the progress bar"),
259 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
262 * GtkProgressBar:min-vertical-bar-width:
264 * The minimum vertical width of the progress bar.
268 gtk_widget_class_install_style_property (widget_class,
269 g_param_spec_int ("min-vertical-bar-width",
270 P_("Minimum vertical bar width"),
271 P_("The minimum vertical width of the progress bar"),
272 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
275 * GtkProgressBar:min-vertical-bar-height:
277 * The minimum vertical height of the progress bar.
281 gtk_widget_class_install_style_property (widget_class,
282 g_param_spec_int ("min-vertical-bar-height",
283 P_("Minimum vertical bar height"),
284 P_("The minimum vertical height of the progress bar"),
285 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
288 g_type_class_add_private (class, sizeof (GtkProgressBarPrivate));
290 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PROGRESS_BAR_ACCESSIBLE);
294 gtk_progress_bar_init (GtkProgressBar *pbar)
296 GtkProgressBarPrivate *priv;
298 pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
299 GTK_TYPE_PROGRESS_BAR,
300 GtkProgressBarPrivate);
303 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
304 priv->inverted = FALSE;
305 priv->pulse_fraction = 0.1;
306 priv->activity_pos = 0;
307 priv->activity_dir = 1;
308 priv->activity_step = 3;
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 GtkAllocation allocation;
427 GtkStyleContext *context;
432 gtk_widget_get_allocation (widget, &allocation);
433 context = gtk_widget_get_style_context (widget);
434 state = gtk_widget_get_state_flags (widget);
435 gtk_style_context_get_padding (context, state, &padding);
437 /* advance the block */
438 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
440 /* Update our activity step. */
441 priv->activity_step = allocation.width * priv->pulse_fraction;
443 size = MAX (2, allocation.width / priv->activity_blocks);
445 if (priv->activity_dir == 0)
447 priv->activity_pos += priv->activity_step;
448 if (priv->activity_pos + size >= allocation.width - padding.left)
450 priv->activity_pos = allocation.width - padding.left - size;
451 priv->activity_dir = 1;
456 priv->activity_pos -= priv->activity_step;
457 if (priv->activity_pos <= padding.left)
459 priv->activity_pos = padding.left;
460 priv->activity_dir = 0;
466 /* Update our activity step. */
467 priv->activity_step = allocation.height * priv->pulse_fraction;
469 size = MAX (2, allocation.height / priv->activity_blocks);
471 if (priv->activity_dir == 0)
473 priv->activity_pos += priv->activity_step;
474 if (priv->activity_pos + size >= allocation.height - padding.top)
476 priv->activity_pos = allocation.height - padding.top - size;
477 priv->activity_dir = 1;
482 priv->activity_pos -= priv->activity_step;
483 if (priv->activity_pos <= padding.top)
485 priv->activity_pos = padding.top;
486 priv->activity_dir = 0;
491 gtk_widget_queue_draw (widget);
495 gtk_progress_bar_finalize (GObject *object)
497 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
498 GtkProgressBarPrivate *priv = pbar->priv;
502 G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
506 get_current_text (GtkProgressBar *pbar)
508 GtkProgressBarPrivate *priv = pbar->priv;
511 return g_strdup (priv->text);
513 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
517 gtk_progress_bar_get_preferred_width (GtkWidget *widget,
521 GtkProgressBar *pbar;
522 GtkProgressBarPrivate *priv;
523 GtkStyleContext *style_context;
527 PangoRectangle logical_rect;
533 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
535 style_context = gtk_widget_get_style_context (widget);
536 state = gtk_widget_get_state_flags (widget);
537 gtk_style_context_get_padding (style_context, state, &padding);
539 gtk_widget_style_get (widget,
540 "xspacing", &xspacing,
543 pbar = GTK_PROGRESS_BAR (widget);
546 width = padding.left + padding.right + xspacing;
550 buf = get_current_text (pbar);
551 layout = gtk_widget_create_pango_layout (widget, buf);
553 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
557 const PangoFontDescription *font_desc;
558 PangoContext *context;
559 PangoFontMetrics *metrics;
562 /* The minimum size for ellipsized text is ~ 3 chars */
563 context = pango_layout_get_context (layout);
564 font_desc = gtk_style_context_get_font (style_context, state);
565 metrics = pango_context_get_metrics (context, font_desc, pango_context_get_language (context));
567 char_width = pango_font_metrics_get_approximate_char_width (metrics);
568 pango_font_metrics_unref (metrics);
570 width += PANGO_PIXELS (char_width) * 3;
573 width += logical_rect.width;
575 g_object_unref (layout);
579 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
580 gtk_widget_style_get (widget,
581 "min-horizontal-bar-width", &min_width,
584 gtk_widget_style_get (widget,
585 "min-vertical-bar-width", &min_width,
588 *minimum = *natural = MAX (min_width, width);
592 gtk_progress_bar_get_preferred_height (GtkWidget *widget,
596 GtkProgressBar *pbar;
597 GtkProgressBarPrivate *priv;
598 GtkStyleContext *context;
602 PangoRectangle logical_rect;
608 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
610 context = gtk_widget_get_style_context (widget);
611 state = gtk_widget_get_state_flags (widget);
612 gtk_style_context_get_padding (context, state, &padding);
614 gtk_widget_style_get (widget,
615 "yspacing", &yspacing,
618 pbar = GTK_PROGRESS_BAR (widget);
621 height = padding.top + padding.bottom + yspacing;
625 buf = get_current_text (pbar);
626 layout = gtk_widget_create_pango_layout (widget, buf);
628 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
630 height += logical_rect.height;
632 g_object_unref (layout);
636 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
637 gtk_widget_style_get (widget,
638 "min-horizontal-bar-height", &min_height,
641 gtk_widget_style_get (widget,
642 "min-vertical-bar-height", &min_height,
645 *minimum = *natural = MAX (min_height, height);
649 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
651 GtkProgressBarPrivate *priv = pbar->priv;
652 GtkAllocation allocation;
653 GtkStyleContext *context;
656 GtkWidget *widget = GTK_WIDGET (pbar);
657 GtkOrientation orientation;
660 context = gtk_widget_get_style_context (widget);
661 state = gtk_widget_get_state_flags (widget);
662 gtk_style_context_get_padding (context, state, &padding);
664 orientation = priv->orientation;
665 inverted = priv->inverted;
666 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
668 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
669 inverted = !inverted;
672 /* calculate start pos */
674 if (orientation == GTK_ORIENTATION_HORIZONTAL)
678 priv->activity_pos = padding.left;
679 priv->activity_dir = 0;
683 gtk_widget_get_allocation (widget, &allocation);
684 priv->activity_pos = allocation.width - padding.left -
685 (allocation.height - padding.top - padding.bottom);
686 priv->activity_dir = 1;
693 priv->activity_pos = padding.top;
694 priv->activity_dir = 0;
698 gtk_widget_get_allocation (widget, &allocation);
699 priv->activity_pos = allocation.height - padding.top -
700 (allocation.width - padding.left - padding.right);
701 priv->activity_dir = 1;
707 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
708 GtkOrientation orientation,
712 GtkProgressBarPrivate *priv = pbar->priv;
713 GtkAllocation allocation;
714 GtkWidget *widget = GTK_WIDGET (pbar);
716 *offset = priv->activity_pos;
718 gtk_widget_get_allocation (widget, &allocation);
720 if (orientation == GTK_ORIENTATION_HORIZONTAL)
721 *amount = MAX (2, allocation.width / priv->activity_blocks);
723 *amount = MAX (2, allocation.height / priv->activity_blocks);
727 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
729 GtkOrientation orientation,
734 GtkStyleContext *context;
737 GtkWidget *widget = GTK_WIDGET (pbar);
740 context = gtk_widget_get_style_context (widget);
741 state = gtk_widget_get_state_flags (widget);
742 gtk_style_context_get_padding (context, state, &padding);
744 if (orientation == GTK_ORIENTATION_HORIZONTAL)
746 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
747 area.y = padding.top;
748 area.height = height - padding.top - padding.bottom;
752 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
753 area.x = padding.left;
754 area.width = width - padding.left - padding.right;
757 gtk_style_context_save (context);
758 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
760 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
762 gtk_style_context_restore (context);
766 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
769 GtkOrientation orientation,
774 GtkStyleContext *context;
777 GtkWidget *widget = GTK_WIDGET (pbar);
783 context = gtk_widget_get_style_context (widget);
784 state = gtk_widget_get_state_flags (widget);
785 gtk_style_context_get_padding (context, state, &padding);
787 if (orientation == GTK_ORIENTATION_HORIZONTAL)
790 area.height = height - padding.top - padding.bottom;
791 area.y = padding.top;
794 area.x = padding.left;
796 area.x = width - amount - padding.right;
800 area.width = width - padding.left - padding.right;
801 area.height = amount;
802 area.x = padding.left;
805 area.y = padding.top;
807 area.y = height - amount - padding.bottom;
810 gtk_style_context_save (context);
811 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
813 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
815 gtk_style_context_restore (context);
819 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
823 GtkOrientation orientation,
828 GtkProgressBarPrivate *priv = pbar->priv;
829 GtkStyleContext *context;
832 GtkWidget *widget = GTK_WIDGET (pbar);
838 PangoRectangle logical_rect;
839 GdkRectangle prelight_clip, start_clip, end_clip;
840 gfloat text_xalign = 0.5;
841 gfloat text_yalign = 0.5;
843 context = gtk_widget_get_style_context (widget);
844 state = gtk_widget_get_state_flags (widget);
845 gtk_style_context_get_padding (context, state, &padding);
847 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
848 text_xalign = 1.0 - text_xalign;
850 buf = get_current_text (pbar);
852 layout = gtk_widget_create_pango_layout (widget, buf);
853 pango_layout_set_ellipsize (layout, priv->ellipsize);
855 pango_layout_set_width (layout, width * PANGO_SCALE);
857 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
859 x = padding.left + 1 + text_xalign * (width - padding.left - padding.right - 2 - logical_rect.width);
860 y = padding.top + 1 + text_yalign * (height - padding.top - padding.bottom - 2 - logical_rect.height);
862 rect.x = padding.left;
863 rect.y = padding.top;
864 rect.width = width - padding.left - padding.right;
865 rect.height = height - padding.top - padding.bottom;
867 prelight_clip = start_clip = end_clip = rect;
869 if (orientation == GTK_ORIENTATION_HORIZONTAL)
874 prelight_clip.x = offset;
875 prelight_clip.width = amount;
876 start_clip.width = prelight_clip.x - start_clip.x;
877 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
878 end_clip.width -= prelight_clip.width + start_clip.width;
883 prelight_clip.x = offset;
885 prelight_clip.x = rect.x + rect.width - amount;
886 prelight_clip.width = amount;
887 start_clip.width = prelight_clip.x - start_clip.x;
888 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
889 end_clip.width -= prelight_clip.width + start_clip.width;
897 prelight_clip.y = offset;
898 prelight_clip.height = amount;
899 start_clip.height = prelight_clip.y - start_clip.y;
900 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
901 end_clip.height -= prelight_clip.height + start_clip.height;
906 prelight_clip.y = offset;
908 prelight_clip.y = rect.y + rect.height - amount;
909 prelight_clip.height = amount;
910 start_clip.height = prelight_clip.y - start_clip.y;
911 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
912 end_clip.height -= prelight_clip.height + start_clip.height;
916 gtk_style_context_save (context);
917 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
919 if (start_clip.width > 0 && start_clip.height > 0)
922 gdk_cairo_rectangle (cr, &start_clip);
925 gtk_render_layout (context, cr, x, y, layout);
929 if (end_clip.width > 0 && end_clip.height > 0)
932 gdk_cairo_rectangle (cr, &end_clip);
935 gtk_render_layout (context, cr, x, y, layout);
939 gtk_style_context_restore (context);
942 gdk_cairo_rectangle (cr, &prelight_clip);
945 gtk_style_context_save (context);
946 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
948 gtk_render_layout (context, cr, x, y, layout);
950 gtk_style_context_restore (context);
953 g_object_unref (layout);
958 gtk_progress_bar_draw (GtkWidget *widget,
961 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
962 GtkProgressBarPrivate *priv = pbar->priv;
963 GtkOrientation orientation;
965 GtkStyleContext *context;
970 context = gtk_widget_get_style_context (widget);
971 state = gtk_widget_get_state_flags (widget);
972 gtk_style_context_get_padding (context, state, &padding);
974 orientation = priv->orientation;
975 inverted = priv->inverted;
976 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
978 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
979 inverted = !inverted;
981 width = gtk_widget_get_allocated_width (widget);
982 height = gtk_widget_get_allocated_height (widget);
984 gtk_style_context_save (context);
985 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
987 gtk_render_background (context, cr, 0, 0, width, height);
988 gtk_render_frame (context, cr, 0, 0, width, height);
990 gtk_style_context_restore (context);
992 if (priv->activity_mode)
994 gtk_progress_bar_paint_activity (pbar, cr,
995 orientation, inverted,
1003 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
1004 gtk_progress_bar_paint_text (pbar, cr,
1006 orientation, inverted,
1015 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1016 space = width - padding.left - padding.right;
1018 space = height - padding.top - padding.bottom;
1020 amount = space * gtk_progress_bar_get_fraction (pbar);
1022 gtk_progress_bar_paint_continuous (pbar, cr, amount, orientation, inverted, width, height);
1024 if (priv->show_text)
1025 gtk_progress_bar_paint_text (pbar, cr, -1, amount, orientation, inverted, width, height);
1032 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
1033 gboolean activity_mode)
1035 GtkProgressBarPrivate *priv = pbar->priv;
1037 activity_mode = !!activity_mode;
1039 if (priv->activity_mode != activity_mode)
1041 priv->activity_mode = activity_mode;
1043 if (priv->activity_mode)
1044 gtk_progress_bar_act_mode_enter (pbar);
1046 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1051 * gtk_progress_bar_set_fraction:
1052 * @pbar: a #GtkProgressBar
1053 * @fraction: fraction of the task that's been completed
1055 * Causes the progress bar to "fill in" the given fraction
1056 * of the bar. The fraction should be between 0.0 and 1.0,
1061 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1064 GtkProgressBarPrivate* priv;
1066 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1070 priv->fraction = fraction;
1071 gtk_progress_bar_set_activity_mode (pbar, FALSE);
1072 gtk_progress_bar_real_update (pbar);
1074 g_object_notify (G_OBJECT (pbar), "fraction");
1078 * gtk_progress_bar_pulse:
1079 * @pbar: a #GtkProgressBar
1081 * Indicates that some progress is made, but you don't know how much.
1082 * Causes the progress bar to enter "activity mode," where a block
1083 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1084 * causes the block to move by a little bit (the amount of movement
1085 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1088 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1090 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1092 gtk_progress_bar_set_activity_mode (pbar, TRUE);
1093 gtk_progress_bar_real_update (pbar);
1097 * gtk_progress_bar_set_text:
1098 * @pbar: a #GtkProgressBar
1099 * @text: (allow-none): a UTF-8 string, or %NULL
1101 * Causes the given @text to appear superimposed on the progress bar.
1104 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1107 GtkProgressBarPrivate *priv;
1109 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1113 g_free (priv->text);
1114 priv->text = text && *text ? g_strdup (text) : NULL;
1116 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1118 g_object_notify (G_OBJECT (pbar), "text");
1122 * gtk_progress_bar_set_show_text:
1123 * @pbar: a #GtkProgressBar
1124 * @show_text: whether to show superimposed text
1126 * Sets whether the progressbar will show text superimposed
1127 * over the bar. The shown text is either the value of
1128 * the #GtkProgressBar::text property or, if that is %NULL,
1129 * the #GtkProgressBar::fraction value, as a percentage.
1134 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1137 GtkProgressBarPrivate *priv;
1139 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1143 show_text = !!show_text;
1145 if (priv->show_text != show_text)
1147 priv->show_text = show_text;
1149 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1151 g_object_notify (G_OBJECT (pbar), "show-text");
1156 * gtk_progress_bar_get_show_text:
1157 * @pbar: a #GtkProgressBar
1159 * Gets the value of the #GtkProgressBar::show-text property.
1160 * See gtk_progress_bar_set_show_text().
1162 * Returns: %TRUE if text is shown in the progress bar
1167 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1169 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1171 return pbar->priv->show_text;
1175 * gtk_progress_bar_set_pulse_step:
1176 * @pbar: a #GtkProgressBar
1177 * @fraction: fraction between 0.0 and 1.0
1179 * Sets the fraction of total progress bar length to move the
1180 * bouncing block for each call to gtk_progress_bar_pulse().
1183 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1186 GtkProgressBarPrivate *priv;
1188 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1192 priv->pulse_fraction = fraction;
1194 g_object_notify (G_OBJECT (pbar), "pulse-step");
1198 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1199 GtkOrientation orientation)
1201 GtkProgressBarPrivate *priv = pbar->priv;
1203 if (priv->orientation != orientation)
1205 priv->orientation = orientation;
1206 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (pbar));
1208 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1213 * gtk_progress_bar_set_inverted:
1214 * @pbar: a #GtkProgressBar
1215 * @inverted: %TRUE to invert the progress bar
1217 * Progress bars normally grow from top to bottom or left to right.
1218 * Inverted progress bars grow in the opposite direction.
1221 gtk_progress_bar_set_inverted (GtkProgressBar *pbar,
1224 GtkProgressBarPrivate *priv;
1226 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1230 if (priv->inverted != inverted)
1232 priv->inverted = inverted;
1234 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1236 g_object_notify (G_OBJECT (pbar), "inverted");
1241 * gtk_progress_bar_get_text:
1242 * @pbar: a #GtkProgressBar
1244 * Retrieves the text displayed superimposed on the progress bar,
1245 * if any, otherwise %NULL. The return value is a reference
1246 * to the text, not a copy of it, so will become invalid
1247 * if you change the text in the progress bar.
1249 * Return value: text, or %NULL; this string is owned by the widget
1250 * and should not be modified or freed.
1253 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1255 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1257 return pbar->priv->text;
1261 * gtk_progress_bar_get_fraction:
1262 * @pbar: a #GtkProgressBar
1264 * Returns the current fraction of the task that's been completed.
1266 * Return value: a fraction from 0.0 to 1.0
1269 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1271 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1273 return pbar->priv->fraction;
1277 * gtk_progress_bar_get_pulse_step:
1278 * @pbar: a #GtkProgressBar
1280 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1282 * Return value: a fraction from 0.0 to 1.0
1285 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1287 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1289 return pbar->priv->pulse_fraction;
1293 * gtk_progress_bar_get_inverted:
1294 * @pbar: a #GtkProgressBar
1296 * Gets the value set by gtk_progress_bar_set_inverted()
1298 * Return value: %TRUE if the progress bar is inverted
1301 gtk_progress_bar_get_inverted (GtkProgressBar *pbar)
1303 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1305 return pbar->priv->inverted;
1309 * gtk_progress_bar_set_ellipsize:
1310 * @pbar: a #GtkProgressBar
1311 * @mode: a #PangoEllipsizeMode
1313 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1314 * if there is not enough space to render the entire string.
1319 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1320 PangoEllipsizeMode mode)
1322 GtkProgressBarPrivate *priv;
1324 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1325 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1326 mode <= PANGO_ELLIPSIZE_END);
1330 if ((PangoEllipsizeMode)priv->ellipsize != mode)
1332 priv->ellipsize = mode;
1334 g_object_notify (G_OBJECT (pbar), "ellipsize");
1335 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1340 * gtk_progress_bar_get_ellipsize:
1341 * @pbar: a #GtkProgressBar
1343 * Returns the ellipsizing position of the progressbar.
1344 * See gtk_progress_bar_set_ellipsize().
1346 * Return value: #PangoEllipsizeMode
1351 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1353 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1355 return pbar->priv->ellipsize;