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 "gtkorientable.h"
33 #include "gtkprivate.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
73 GtkOrientation orientation;
78 gdouble pulse_fraction;
84 guint activity_blocks;
87 guint activity_dir : 1;
88 guint activity_mode : 1;
105 static void gtk_progress_bar_set_property (GObject *object,
109 static void gtk_progress_bar_get_property (GObject *object,
113 static void gtk_progress_bar_get_preferred_width (GtkWidget *widget,
116 static void gtk_progress_bar_get_preferred_height (GtkWidget *widget,
120 static void gtk_progress_bar_real_update (GtkProgressBar *progress);
121 static gboolean gtk_progress_bar_draw (GtkWidget *widget,
123 static void gtk_progress_bar_act_mode_enter (GtkProgressBar *progress);
124 static void gtk_progress_bar_finalize (GObject *object);
125 static void gtk_progress_bar_set_orientation (GtkProgressBar *progress,
126 GtkOrientation orientation);
128 G_DEFINE_TYPE_WITH_CODE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_WIDGET,
129 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
132 gtk_progress_bar_class_init (GtkProgressBarClass *class)
134 GObjectClass *gobject_class;
135 GtkWidgetClass *widget_class;
137 gobject_class = G_OBJECT_CLASS (class);
138 widget_class = (GtkWidgetClass *) class;
140 gobject_class->set_property = gtk_progress_bar_set_property;
141 gobject_class->get_property = gtk_progress_bar_get_property;
142 gobject_class->finalize = gtk_progress_bar_finalize;
144 widget_class->draw = gtk_progress_bar_draw;
145 widget_class->get_preferred_width = gtk_progress_bar_get_preferred_width;
146 widget_class->get_preferred_height = gtk_progress_bar_get_preferred_height;
148 g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation");
150 g_object_class_install_property (gobject_class,
152 g_param_spec_boolean ("inverted",
154 P_("Invert the direction in which the progress bar grows"),
156 GTK_PARAM_READWRITE));
158 g_object_class_install_property (gobject_class,
160 g_param_spec_double ("fraction",
162 P_("The fraction of total work that has been completed"),
164 GTK_PARAM_READWRITE));
166 g_object_class_install_property (gobject_class,
168 g_param_spec_double ("pulse-step",
170 P_("The fraction of total progress to move the bouncing block when pulsed"),
172 GTK_PARAM_READWRITE));
174 g_object_class_install_property (gobject_class,
176 g_param_spec_string ("text",
178 P_("Text to be displayed in the progress bar"),
180 GTK_PARAM_READWRITE));
182 g_object_class_install_property (gobject_class,
184 g_param_spec_boolean ("show-text",
186 P_("Whether the progress is shown as text."),
188 GTK_PARAM_READWRITE));
191 * GtkProgressBar:ellipsize:
193 * The preferred place to ellipsize the string, if the progressbar does
194 * not have enough room to display the entire string, specified as a
195 * #PangoEllisizeMode.
197 * Note that setting this property to a value other than
198 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
199 * only enough space to display the ellipsis "...". Another means to set a
200 * progressbar's width is gtk_widget_set_size_request().
204 g_object_class_install_property (gobject_class,
206 g_param_spec_enum ("ellipsize",
208 P_("The preferred place to ellipsize the string, if the progress bar "
209 "does not have enough room to display the entire string, if at all."),
210 PANGO_TYPE_ELLIPSIZE_MODE,
211 PANGO_ELLIPSIZE_NONE,
212 GTK_PARAM_READWRITE));
213 gtk_widget_class_install_style_property (widget_class,
214 g_param_spec_int ("xspacing",
216 P_("Extra spacing applied to the width of a progress bar."),
219 gtk_widget_class_install_style_property (widget_class,
220 g_param_spec_int ("yspacing",
222 P_("Extra spacing applied to the height of a progress bar."),
227 * GtkProgressBar:min-horizontal-bar-width:
229 * The minimum horizontal width of the progress bar.
233 gtk_widget_class_install_style_property (widget_class,
234 g_param_spec_int ("min-horizontal-bar-width",
235 P_("Minimum horizontal bar width"),
236 P_("The minimum horizontal width of the progress bar"),
237 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
240 * GtkProgressBar:min-horizontal-bar-height:
242 * Minimum horizontal height of the progress bar.
246 gtk_widget_class_install_style_property (widget_class,
247 g_param_spec_int ("min-horizontal-bar-height",
248 P_("Minimum horizontal bar height"),
249 P_("Minimum horizontal height of the progress bar"),
250 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
253 * GtkProgressBar:min-vertical-bar-width:
255 * The minimum vertical width of the progress bar.
259 gtk_widget_class_install_style_property (widget_class,
260 g_param_spec_int ("min-vertical-bar-width",
261 P_("Minimum vertical bar width"),
262 P_("The minimum vertical width of the progress bar"),
263 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
266 * GtkProgressBar:min-vertical-bar-height:
268 * The minimum vertical height of the progress bar.
272 gtk_widget_class_install_style_property (widget_class,
273 g_param_spec_int ("min-vertical-bar-height",
274 P_("Minimum vertical bar height"),
275 P_("The minimum vertical height of the progress bar"),
276 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
279 g_type_class_add_private (class, sizeof (GtkProgressBarPrivate));
283 gtk_progress_bar_init (GtkProgressBar *pbar)
285 GtkProgressBarPrivate *priv;
287 pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
288 GTK_TYPE_PROGRESS_BAR,
289 GtkProgressBarPrivate);
294 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
295 priv->inverted = FALSE;
296 priv->pulse_fraction = 0.1;
297 priv->activity_pos = 0;
298 priv->activity_dir = 1;
299 priv->activity_step = 3;
300 priv->activity_blocks = 5;
301 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
302 priv->show_text = FALSE;
305 priv->fraction = 0.0;
307 gtk_widget_set_has_window (GTK_WIDGET (pbar), FALSE);
311 gtk_progress_bar_set_property (GObject *object,
316 GtkProgressBar *pbar;
318 pbar = GTK_PROGRESS_BAR (object);
322 case PROP_ORIENTATION:
323 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
326 gtk_progress_bar_set_inverted (pbar, g_value_get_boolean (value));
329 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
331 case PROP_PULSE_STEP:
332 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
335 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
338 gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
341 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
344 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
350 gtk_progress_bar_get_property (GObject *object,
355 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
356 GtkProgressBarPrivate* priv = pbar->priv;
360 case PROP_ORIENTATION:
361 g_value_set_enum (value, priv->orientation);
364 g_value_set_boolean (value, priv->inverted);
367 g_value_set_double (value, priv->fraction);
369 case PROP_PULSE_STEP:
370 g_value_set_double (value, priv->pulse_fraction);
373 g_value_set_string (value, priv->text);
376 g_value_set_boolean (value, priv->show_text);
379 g_value_set_enum (value, priv->ellipsize);
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
388 * gtk_progress_bar_new:
390 * Creates a new #GtkProgressBar.
392 * Returns: a #GtkProgressBar.
395 gtk_progress_bar_new (void)
399 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
405 gtk_progress_bar_real_update (GtkProgressBar *pbar)
407 GtkProgressBarPrivate *priv;
410 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
413 widget = GTK_WIDGET (pbar);
415 if (priv->activity_mode)
417 GtkAllocation allocation;
418 GtkStyleContext *context;
423 gtk_widget_get_allocation (widget, &allocation);
424 context = gtk_widget_get_style_context (widget);
425 state = gtk_widget_get_state_flags (widget);
426 gtk_style_context_get_padding (context, state, &padding);
428 /* advance the block */
429 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
431 /* Update our activity step. */
432 priv->activity_step = allocation.width * priv->pulse_fraction;
434 size = MAX (2, allocation.width / priv->activity_blocks);
436 if (priv->activity_dir == 0)
438 priv->activity_pos += priv->activity_step;
439 if (priv->activity_pos + size >= allocation.width - padding.left)
441 priv->activity_pos = allocation.width - padding.left - size;
442 priv->activity_dir = 1;
447 priv->activity_pos -= priv->activity_step;
448 if (priv->activity_pos <= padding.left)
450 priv->activity_pos = padding.left;
451 priv->activity_dir = 0;
457 /* Update our activity step. */
458 priv->activity_step = allocation.height * priv->pulse_fraction;
460 size = MAX (2, allocation.height / priv->activity_blocks);
462 if (priv->activity_dir == 0)
464 priv->activity_pos += priv->activity_step;
465 if (priv->activity_pos + size >= allocation.height - padding.top)
467 priv->activity_pos = allocation.height - padding.top - size;
468 priv->activity_dir = 1;
473 priv->activity_pos -= priv->activity_step;
474 if (priv->activity_pos <= padding.top)
476 priv->activity_pos = padding.top;
477 priv->activity_dir = 0;
482 gtk_widget_queue_draw (widget);
486 gtk_progress_bar_finalize (GObject *object)
488 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
489 GtkProgressBarPrivate *priv = pbar->priv;
493 G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
497 get_current_text (GtkProgressBar *pbar)
499 GtkProgressBarPrivate *priv = pbar->priv;
502 return g_strdup (priv->text);
504 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
508 gtk_progress_bar_get_preferred_width (GtkWidget *widget,
512 GtkProgressBar *pbar;
513 GtkProgressBarPrivate *priv;
514 GtkStyleContext *style_context;
518 PangoRectangle logical_rect;
524 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
526 style_context = gtk_widget_get_style_context (widget);
527 state = gtk_widget_get_state_flags (widget);
528 gtk_style_context_get_padding (style_context, state, &padding);
530 gtk_widget_style_get (widget,
531 "xspacing", &xspacing,
534 pbar = GTK_PROGRESS_BAR (widget);
537 width = padding.left + padding.right + xspacing;
541 buf = get_current_text (pbar);
542 layout = gtk_widget_create_pango_layout (widget, buf);
544 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
548 const PangoFontDescription *font_desc;
549 PangoContext *context;
550 PangoFontMetrics *metrics;
553 /* The minimum size for ellipsized text is ~ 3 chars */
554 context = pango_layout_get_context (layout);
555 font_desc = gtk_style_context_get_font (style_context, state);
556 metrics = pango_context_get_metrics (context, font_desc, pango_context_get_language (context));
558 char_width = pango_font_metrics_get_approximate_char_width (metrics);
559 pango_font_metrics_unref (metrics);
561 width += PANGO_PIXELS (char_width) * 3;
564 width += logical_rect.width;
566 g_object_unref (layout);
570 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
571 gtk_widget_style_get (widget,
572 "min-horizontal-bar-width", &min_width,
575 gtk_widget_style_get (widget,
576 "min-vertical-bar-width", &min_width,
579 *minimum = *natural = MAX (min_width, width);
583 gtk_progress_bar_get_preferred_height (GtkWidget *widget,
587 GtkProgressBar *pbar;
588 GtkProgressBarPrivate *priv;
589 GtkStyleContext *context;
593 PangoRectangle logical_rect;
599 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
601 context = gtk_widget_get_style_context (widget);
602 state = gtk_widget_get_state_flags (widget);
603 gtk_style_context_get_padding (context, state, &padding);
605 gtk_widget_style_get (widget,
606 "yspacing", &yspacing,
609 pbar = GTK_PROGRESS_BAR (widget);
612 height = padding.top + padding.bottom + yspacing;
616 buf = get_current_text (pbar);
617 layout = gtk_widget_create_pango_layout (widget, buf);
619 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
621 height += logical_rect.height;
623 g_object_unref (layout);
627 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
628 gtk_widget_style_get (widget,
629 "min-horizontal-bar-height", &min_height,
632 gtk_widget_style_get (widget,
633 "min-vertical-bar-height", &min_height,
636 *minimum = *natural = MAX (min_height, height);
640 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
642 GtkProgressBarPrivate *priv = pbar->priv;
643 GtkAllocation allocation;
644 GtkStyleContext *context;
647 GtkWidget *widget = GTK_WIDGET (pbar);
648 GtkOrientation orientation;
651 context = gtk_widget_get_style_context (widget);
652 state = gtk_widget_get_state_flags (widget);
653 gtk_style_context_get_padding (context, state, &padding);
655 orientation = priv->orientation;
656 inverted = priv->inverted;
657 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
659 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
660 inverted = !inverted;
663 /* calculate start pos */
665 if (orientation == GTK_ORIENTATION_HORIZONTAL)
669 priv->activity_pos = padding.left;
670 priv->activity_dir = 0;
674 gtk_widget_get_allocation (widget, &allocation);
675 priv->activity_pos = allocation.width - padding.left -
676 (allocation.height - padding.top - padding.bottom);
677 priv->activity_dir = 1;
684 priv->activity_pos = padding.top;
685 priv->activity_dir = 0;
689 gtk_widget_get_allocation (widget, &allocation);
690 priv->activity_pos = allocation.height - padding.top -
691 (allocation.width - padding.left - padding.right);
692 priv->activity_dir = 1;
698 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
699 GtkOrientation orientation,
703 GtkProgressBarPrivate *priv = pbar->priv;
704 GtkAllocation allocation;
705 GtkWidget *widget = GTK_WIDGET (pbar);
707 *offset = priv->activity_pos;
709 gtk_widget_get_allocation (widget, &allocation);
711 if (orientation == GTK_ORIENTATION_HORIZONTAL)
712 *amount = MAX (2, allocation.width / priv->activity_blocks);
714 *amount = MAX (2, allocation.height / priv->activity_blocks);
718 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
720 GtkOrientation orientation,
725 GtkStyleContext *context;
728 GtkWidget *widget = GTK_WIDGET (pbar);
731 context = gtk_widget_get_style_context (widget);
732 state = gtk_widget_get_state_flags (widget);
733 gtk_style_context_get_padding (context, state, &padding);
735 if (orientation == GTK_ORIENTATION_HORIZONTAL)
737 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
738 area.y = padding.top;
739 area.height = height - padding.top - padding.bottom;
743 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
744 area.x = padding.left;
745 area.width = width - padding.left - padding.right;
748 gtk_style_context_save (context);
749 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
751 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
753 gtk_style_context_restore (context);
757 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
760 GtkOrientation orientation,
765 GtkStyleContext *context;
768 GtkWidget *widget = GTK_WIDGET (pbar);
774 context = gtk_widget_get_style_context (widget);
775 state = gtk_widget_get_state_flags (widget);
776 gtk_style_context_get_padding (context, state, &padding);
778 if (orientation == GTK_ORIENTATION_HORIZONTAL)
781 area.height = height - padding.top - padding.bottom;
782 area.y = padding.top;
785 area.x = padding.left;
787 area.x = width - amount - padding.right;
791 area.width = width - padding.left - padding.right;
792 area.height = amount;
793 area.x = padding.left;
796 area.y = padding.top;
798 area.y = height - amount - padding.bottom;
801 gtk_style_context_save (context);
802 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
804 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
806 gtk_style_context_restore (context);
810 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
814 GtkOrientation orientation,
819 GtkProgressBarPrivate *priv = pbar->priv;
820 GtkStyleContext *context;
823 GtkWidget *widget = GTK_WIDGET (pbar);
829 PangoRectangle logical_rect;
830 GdkRectangle prelight_clip, start_clip, end_clip;
831 gfloat text_xalign = 0.5;
832 gfloat text_yalign = 0.5;
834 context = gtk_widget_get_style_context (widget);
835 state = gtk_widget_get_state_flags (widget);
836 gtk_style_context_get_padding (context, state, &padding);
838 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
839 text_xalign = 1.0 - text_xalign;
841 buf = get_current_text (pbar);
843 layout = gtk_widget_create_pango_layout (widget, buf);
844 pango_layout_set_ellipsize (layout, priv->ellipsize);
846 pango_layout_set_width (layout, width * PANGO_SCALE);
848 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
850 x = padding.left + 1 + text_xalign * (width - padding.left - padding.right - 2 - logical_rect.width);
851 y = padding.top + 1 + text_yalign * (height - padding.top - padding.bottom - 2 - logical_rect.height);
853 rect.x = padding.left;
854 rect.y = padding.top;
855 rect.width = width - padding.left - padding.right;
856 rect.height = height - padding.top - padding.bottom;
858 prelight_clip = start_clip = end_clip = rect;
860 if (orientation == GTK_ORIENTATION_HORIZONTAL)
865 prelight_clip.x = offset;
866 prelight_clip.width = amount;
867 start_clip.width = prelight_clip.x - start_clip.x;
868 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
869 end_clip.width -= prelight_clip.width + start_clip.width;
874 prelight_clip.x = offset;
876 prelight_clip.x = rect.x + rect.width - amount;
877 prelight_clip.width = amount;
878 start_clip.width = prelight_clip.x - start_clip.x;
879 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
880 end_clip.width -= prelight_clip.width + start_clip.width;
888 prelight_clip.y = offset;
889 prelight_clip.height = amount;
890 start_clip.height = prelight_clip.y - start_clip.y;
891 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
892 end_clip.height -= prelight_clip.height + start_clip.height;
897 prelight_clip.y = offset;
899 prelight_clip.y = rect.y + rect.height - amount;
900 prelight_clip.height = amount;
901 start_clip.height = prelight_clip.y - start_clip.y;
902 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
903 end_clip.height -= prelight_clip.height + start_clip.height;
907 gtk_style_context_save (context);
908 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
910 if (start_clip.width > 0 && start_clip.height > 0)
913 gdk_cairo_rectangle (cr, &start_clip);
916 gtk_render_layout (context, cr, x, y, layout);
920 if (end_clip.width > 0 && end_clip.height > 0)
923 gdk_cairo_rectangle (cr, &end_clip);
926 gtk_render_layout (context, cr, x, y, layout);
930 gtk_style_context_restore (context);
933 gdk_cairo_rectangle (cr, &prelight_clip);
936 gtk_style_context_save (context);
937 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
939 gtk_render_layout (context, cr, x, y, layout);
941 gtk_style_context_restore (context);
944 g_object_unref (layout);
949 gtk_progress_bar_draw (GtkWidget *widget,
952 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
953 GtkProgressBarPrivate *priv = pbar->priv;
954 GtkOrientation orientation;
956 GtkStyleContext *context;
961 context = gtk_widget_get_style_context (widget);
962 state = gtk_widget_get_state_flags (widget);
964 orientation = priv->orientation;
965 inverted = priv->inverted;
966 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
968 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
969 inverted = !inverted;
971 width = gtk_widget_get_allocated_width (widget);
972 height = gtk_widget_get_allocated_height (widget);
974 gtk_style_context_save (context);
975 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
977 gtk_render_background (context, cr, 0, 0, width, height);
978 gtk_render_frame (context, cr, 0, 0, width, height);
980 gtk_style_context_get_padding (context, state, &padding);
982 gtk_style_context_restore (context);
984 if (priv->activity_mode)
986 gtk_progress_bar_paint_activity (pbar, cr,
987 orientation, inverted,
995 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
996 gtk_progress_bar_paint_text (pbar, cr,
998 orientation, inverted,
1007 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1008 space = width - padding.left - padding.right;
1010 space = height - padding.top - padding.bottom;
1012 amount = space * gtk_progress_bar_get_fraction (pbar);
1014 gtk_progress_bar_paint_continuous (pbar, cr, amount, orientation, inverted, width, height);
1016 if (priv->show_text)
1017 gtk_progress_bar_paint_text (pbar, cr, -1, amount, orientation, inverted, width, height);
1024 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
1025 gboolean activity_mode)
1027 GtkProgressBarPrivate *priv = pbar->priv;
1029 activity_mode = !!activity_mode;
1031 if (priv->activity_mode != activity_mode)
1033 priv->activity_mode = activity_mode;
1035 if (priv->activity_mode)
1036 gtk_progress_bar_act_mode_enter (pbar);
1038 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1039 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1044 * gtk_progress_bar_set_fraction:
1045 * @pbar: a #GtkProgressBar
1046 * @fraction: fraction of the task that's been completed
1048 * Causes the progress bar to "fill in" the given fraction
1049 * of the bar. The fraction should be between 0.0 and 1.0,
1054 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1057 GtkProgressBarPrivate* priv;
1059 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1063 priv->fraction = fraction;
1064 gtk_progress_bar_set_activity_mode (pbar, FALSE);
1065 gtk_progress_bar_real_update (pbar);
1067 g_object_notify (G_OBJECT (pbar), "fraction");
1071 * gtk_progress_bar_pulse:
1072 * @pbar: a #GtkProgressBar
1074 * Indicates that some progress is made, but you don't know how much.
1075 * Causes the progress bar to enter "activity mode," where a block
1076 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1077 * causes the block to move by a little bit (the amount of movement
1078 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1081 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1083 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1085 gtk_progress_bar_set_activity_mode (pbar, TRUE);
1086 gtk_progress_bar_real_update (pbar);
1090 * gtk_progress_bar_set_text:
1091 * @pbar: a #GtkProgressBar
1092 * @text: (allow-none): a UTF-8 string, or %NULL
1094 * Causes the given @text to appear superimposed on the progress bar.
1097 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1100 GtkProgressBarPrivate *priv;
1102 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1106 g_free (priv->text);
1107 priv->text = text && *text ? g_strdup (text) : NULL;
1109 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1110 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1112 g_object_notify (G_OBJECT (pbar), "text");
1116 * gtk_progress_bar_set_show_text:
1117 * @pbar: a #GtkProgressBar
1118 * @show_text: whether to show superimposed text
1120 * Sets whether the progressbar will show text superimposed
1121 * over the bar. The shown text is either the value of
1122 * the #GtkProgressBar::text property or, if that is %NULL,
1123 * the #GtkProgressBar::fraction value, as a percentage.
1128 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1131 GtkProgressBarPrivate *priv;
1133 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1137 show_text = !!show_text;
1139 if (priv->show_text != show_text)
1141 priv->show_text = show_text;
1143 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1144 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1146 g_object_notify (G_OBJECT (pbar), "show-text");
1151 * gtk_progress_bar_get_show_text:
1152 * @pbar: a #GtkProgressBar
1154 * Gets the value of the #GtkProgressBar::show-text property.
1155 * See gtk_progress_bar_set_show_text().
1157 * Returns: %TRUE if text is shown in the progress bar
1162 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1164 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1166 return pbar->priv->show_text;
1170 * gtk_progress_bar_set_pulse_step:
1171 * @pbar: a #GtkProgressBar
1172 * @fraction: fraction between 0.0 and 1.0
1174 * Sets the fraction of total progress bar length to move the
1175 * bouncing block for each call to gtk_progress_bar_pulse().
1178 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1181 GtkProgressBarPrivate *priv;
1183 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1187 priv->pulse_fraction = fraction;
1189 g_object_notify (G_OBJECT (pbar), "pulse-step");
1193 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1194 GtkOrientation orientation)
1196 GtkProgressBarPrivate *priv = pbar->priv;
1198 if (priv->orientation != orientation)
1200 priv->orientation = orientation;
1202 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1203 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1208 * gtk_progress_bar_set_inverted:
1209 * @pbar: a #GtkProgressBar
1210 * @inverted: %TRUE to invert the progress bar
1212 * Progress bars normally grow from top to bottom or left to right.
1213 * Inverted progress bars grow in the opposite direction.
1216 gtk_progress_bar_set_inverted (GtkProgressBar *pbar,
1219 GtkProgressBarPrivate *priv;
1221 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1225 if (priv->inverted != inverted)
1227 priv->inverted = inverted;
1229 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1230 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1232 g_object_notify (G_OBJECT (pbar), "inverted");
1237 * gtk_progress_bar_get_text:
1238 * @pbar: a #GtkProgressBar
1240 * Retrieves the text displayed superimposed on the progress bar,
1241 * if any, otherwise %NULL. The return value is a reference
1242 * to the text, not a copy of it, so will become invalid
1243 * if you change the text in the progress bar.
1245 * Return value: text, or %NULL; this string is owned by the widget
1246 * and should not be modified or freed.
1248 G_CONST_RETURN gchar*
1249 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1251 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1253 return pbar->priv->text;
1257 * gtk_progress_bar_get_fraction:
1258 * @pbar: a #GtkProgressBar
1260 * Returns the current fraction of the task that's been completed.
1262 * Return value: a fraction from 0.0 to 1.0
1265 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1267 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1269 return pbar->priv->fraction;
1273 * gtk_progress_bar_get_pulse_step:
1274 * @pbar: a #GtkProgressBar
1276 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1278 * Return value: a fraction from 0.0 to 1.0
1281 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1283 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1285 return pbar->priv->pulse_fraction;
1289 * gtk_progress_bar_get_inverted:
1290 * @pbar: a #GtkProgressBar
1292 * Gets the value set by gtk_progress_bar_set_inverted()
1294 * Return value: %TRUE if the progress bar is inverted
1297 gtk_progress_bar_get_inverted (GtkProgressBar *pbar)
1299 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1301 return pbar->priv->inverted;
1305 * gtk_progress_bar_set_ellipsize:
1306 * @pbar: a #GtkProgressBar
1307 * @mode: a #PangoEllipsizeMode
1309 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1310 * if there is not enough space to render the entire string.
1315 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1316 PangoEllipsizeMode mode)
1318 GtkProgressBarPrivate *priv;
1320 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1321 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1322 mode <= PANGO_ELLIPSIZE_END);
1326 if ((PangoEllipsizeMode)priv->ellipsize != mode)
1328 priv->ellipsize = mode;
1330 g_object_notify (G_OBJECT (pbar), "ellipsize");
1331 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1336 * gtk_progress_bar_get_ellipsize:
1337 * @pbar: a #GtkProgressBar
1339 * Returns the ellipsizing position of the progressbar.
1340 * See gtk_progress_bar_set_ellipsize().
1342 * Return value: #PangoEllipsizeMode
1347 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1349 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1351 return pbar->priv->ellipsize;