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"
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 progressbar 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.
189 g_object_class_install_property (gobject_class,
191 g_param_spec_boolean ("show-text",
193 P_("Whether the progress is shown as text."),
195 GTK_PARAM_READWRITE));
198 * GtkProgressBar:ellipsize:
200 * The preferred place to ellipsize the string, if the progressbar does
201 * not have enough room to display the entire string, specified as a
202 * #PangoEllisizeMode.
204 * Note that setting this property to a value other than
205 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
206 * only enough space to display the ellipsis "...". Another means to set a
207 * progressbar's width is gtk_widget_set_size_request().
211 g_object_class_install_property (gobject_class,
213 g_param_spec_enum ("ellipsize",
215 P_("The preferred place to ellipsize the string, if the progress bar "
216 "does not have enough room to display the entire string, if at all."),
217 PANGO_TYPE_ELLIPSIZE_MODE,
218 PANGO_ELLIPSIZE_NONE,
219 GTK_PARAM_READWRITE));
220 gtk_widget_class_install_style_property (widget_class,
221 g_param_spec_int ("xspacing",
223 P_("Extra spacing applied to the width of a progress bar."),
226 gtk_widget_class_install_style_property (widget_class,
227 g_param_spec_int ("yspacing",
229 P_("Extra spacing applied to the height of a progress bar."),
234 * GtkProgressBar:min-horizontal-bar-width:
236 * The minimum horizontal width of the progress bar.
240 gtk_widget_class_install_style_property (widget_class,
241 g_param_spec_int ("min-horizontal-bar-width",
242 P_("Minimum horizontal bar width"),
243 P_("The minimum horizontal width of the progress bar"),
244 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
247 * GtkProgressBar:min-horizontal-bar-height:
249 * Minimum horizontal height of the progress bar.
253 gtk_widget_class_install_style_property (widget_class,
254 g_param_spec_int ("min-horizontal-bar-height",
255 P_("Minimum horizontal bar height"),
256 P_("Minimum horizontal height of the progress bar"),
257 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
260 * GtkProgressBar:min-vertical-bar-width:
262 * The minimum vertical width of the progress bar.
266 gtk_widget_class_install_style_property (widget_class,
267 g_param_spec_int ("min-vertical-bar-width",
268 P_("Minimum vertical bar width"),
269 P_("The minimum vertical width of the progress bar"),
270 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
273 * GtkProgressBar:min-vertical-bar-height:
275 * The minimum vertical height of the progress bar.
279 gtk_widget_class_install_style_property (widget_class,
280 g_param_spec_int ("min-vertical-bar-height",
281 P_("Minimum vertical bar height"),
282 P_("The minimum vertical height of the progress bar"),
283 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
286 g_type_class_add_private (class, sizeof (GtkProgressBarPrivate));
290 gtk_progress_bar_init (GtkProgressBar *pbar)
292 GtkProgressBarPrivate *priv;
294 pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
295 GTK_TYPE_PROGRESS_BAR,
296 GtkProgressBarPrivate);
299 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
300 priv->inverted = FALSE;
301 priv->pulse_fraction = 0.1;
302 priv->activity_pos = 0;
303 priv->activity_dir = 1;
304 priv->activity_step = 3;
305 priv->activity_blocks = 5;
306 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
307 priv->show_text = FALSE;
310 priv->fraction = 0.0;
312 gtk_widget_set_has_window (GTK_WIDGET (pbar), FALSE);
316 gtk_progress_bar_set_property (GObject *object,
321 GtkProgressBar *pbar;
323 pbar = GTK_PROGRESS_BAR (object);
327 case PROP_ORIENTATION:
328 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
331 gtk_progress_bar_set_inverted (pbar, g_value_get_boolean (value));
334 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
336 case PROP_PULSE_STEP:
337 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
340 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
343 gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
346 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
349 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
355 gtk_progress_bar_get_property (GObject *object,
360 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
361 GtkProgressBarPrivate* priv = pbar->priv;
365 case PROP_ORIENTATION:
366 g_value_set_enum (value, priv->orientation);
369 g_value_set_boolean (value, priv->inverted);
372 g_value_set_double (value, priv->fraction);
374 case PROP_PULSE_STEP:
375 g_value_set_double (value, priv->pulse_fraction);
378 g_value_set_string (value, priv->text);
381 g_value_set_boolean (value, priv->show_text);
384 g_value_set_enum (value, priv->ellipsize);
387 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
393 * gtk_progress_bar_new:
395 * Creates a new #GtkProgressBar.
397 * Returns: a #GtkProgressBar.
400 gtk_progress_bar_new (void)
404 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
410 gtk_progress_bar_real_update (GtkProgressBar *pbar)
412 GtkProgressBarPrivate *priv;
415 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
418 widget = GTK_WIDGET (pbar);
420 if (priv->activity_mode)
422 GtkAllocation allocation;
423 GtkStyleContext *context;
428 gtk_widget_get_allocation (widget, &allocation);
429 context = gtk_widget_get_style_context (widget);
430 state = gtk_widget_get_state_flags (widget);
431 gtk_style_context_get_padding (context, state, &padding);
433 /* advance the block */
434 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
436 /* Update our activity step. */
437 priv->activity_step = allocation.width * priv->pulse_fraction;
439 size = MAX (2, allocation.width / priv->activity_blocks);
441 if (priv->activity_dir == 0)
443 priv->activity_pos += priv->activity_step;
444 if (priv->activity_pos + size >= allocation.width - padding.left)
446 priv->activity_pos = allocation.width - padding.left - size;
447 priv->activity_dir = 1;
452 priv->activity_pos -= priv->activity_step;
453 if (priv->activity_pos <= padding.left)
455 priv->activity_pos = padding.left;
456 priv->activity_dir = 0;
462 /* Update our activity step. */
463 priv->activity_step = allocation.height * priv->pulse_fraction;
465 size = MAX (2, allocation.height / priv->activity_blocks);
467 if (priv->activity_dir == 0)
469 priv->activity_pos += priv->activity_step;
470 if (priv->activity_pos + size >= allocation.height - padding.top)
472 priv->activity_pos = allocation.height - padding.top - size;
473 priv->activity_dir = 1;
478 priv->activity_pos -= priv->activity_step;
479 if (priv->activity_pos <= padding.top)
481 priv->activity_pos = padding.top;
482 priv->activity_dir = 0;
487 gtk_widget_queue_draw (widget);
491 gtk_progress_bar_finalize (GObject *object)
493 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
494 GtkProgressBarPrivate *priv = pbar->priv;
498 G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
502 get_current_text (GtkProgressBar *pbar)
504 GtkProgressBarPrivate *priv = pbar->priv;
507 return g_strdup (priv->text);
509 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
513 gtk_progress_bar_get_preferred_width (GtkWidget *widget,
517 GtkProgressBar *pbar;
518 GtkProgressBarPrivate *priv;
519 GtkStyleContext *style_context;
523 PangoRectangle logical_rect;
529 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
531 style_context = gtk_widget_get_style_context (widget);
532 state = gtk_widget_get_state_flags (widget);
533 gtk_style_context_get_padding (style_context, state, &padding);
535 gtk_widget_style_get (widget,
536 "xspacing", &xspacing,
539 pbar = GTK_PROGRESS_BAR (widget);
542 width = padding.left + padding.right + xspacing;
546 buf = get_current_text (pbar);
547 layout = gtk_widget_create_pango_layout (widget, buf);
549 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
553 const PangoFontDescription *font_desc;
554 PangoContext *context;
555 PangoFontMetrics *metrics;
558 /* The minimum size for ellipsized text is ~ 3 chars */
559 context = pango_layout_get_context (layout);
560 font_desc = gtk_style_context_get_font (style_context, state);
561 metrics = pango_context_get_metrics (context, font_desc, pango_context_get_language (context));
563 char_width = pango_font_metrics_get_approximate_char_width (metrics);
564 pango_font_metrics_unref (metrics);
566 width += PANGO_PIXELS (char_width) * 3;
569 width += logical_rect.width;
571 g_object_unref (layout);
575 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
576 gtk_widget_style_get (widget,
577 "min-horizontal-bar-width", &min_width,
580 gtk_widget_style_get (widget,
581 "min-vertical-bar-width", &min_width,
584 *minimum = *natural = MAX (min_width, width);
588 gtk_progress_bar_get_preferred_height (GtkWidget *widget,
592 GtkProgressBar *pbar;
593 GtkProgressBarPrivate *priv;
594 GtkStyleContext *context;
598 PangoRectangle logical_rect;
604 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
606 context = gtk_widget_get_style_context (widget);
607 state = gtk_widget_get_state_flags (widget);
608 gtk_style_context_get_padding (context, state, &padding);
610 gtk_widget_style_get (widget,
611 "yspacing", &yspacing,
614 pbar = GTK_PROGRESS_BAR (widget);
617 height = padding.top + padding.bottom + yspacing;
621 buf = get_current_text (pbar);
622 layout = gtk_widget_create_pango_layout (widget, buf);
624 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
626 height += logical_rect.height;
628 g_object_unref (layout);
632 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
633 gtk_widget_style_get (widget,
634 "min-horizontal-bar-height", &min_height,
637 gtk_widget_style_get (widget,
638 "min-vertical-bar-height", &min_height,
641 *minimum = *natural = MAX (min_height, height);
645 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
647 GtkProgressBarPrivate *priv = pbar->priv;
648 GtkAllocation allocation;
649 GtkStyleContext *context;
652 GtkWidget *widget = GTK_WIDGET (pbar);
653 GtkOrientation orientation;
656 context = gtk_widget_get_style_context (widget);
657 state = gtk_widget_get_state_flags (widget);
658 gtk_style_context_get_padding (context, state, &padding);
660 orientation = priv->orientation;
661 inverted = priv->inverted;
662 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
664 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
665 inverted = !inverted;
668 /* calculate start pos */
670 if (orientation == GTK_ORIENTATION_HORIZONTAL)
674 priv->activity_pos = padding.left;
675 priv->activity_dir = 0;
679 gtk_widget_get_allocation (widget, &allocation);
680 priv->activity_pos = allocation.width - padding.left -
681 (allocation.height - padding.top - padding.bottom);
682 priv->activity_dir = 1;
689 priv->activity_pos = padding.top;
690 priv->activity_dir = 0;
694 gtk_widget_get_allocation (widget, &allocation);
695 priv->activity_pos = allocation.height - padding.top -
696 (allocation.width - padding.left - padding.right);
697 priv->activity_dir = 1;
703 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
704 GtkOrientation orientation,
708 GtkProgressBarPrivate *priv = pbar->priv;
709 GtkAllocation allocation;
710 GtkWidget *widget = GTK_WIDGET (pbar);
712 *offset = priv->activity_pos;
714 gtk_widget_get_allocation (widget, &allocation);
716 if (orientation == GTK_ORIENTATION_HORIZONTAL)
717 *amount = MAX (2, allocation.width / priv->activity_blocks);
719 *amount = MAX (2, allocation.height / priv->activity_blocks);
723 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
725 GtkOrientation orientation,
730 GtkStyleContext *context;
733 GtkWidget *widget = GTK_WIDGET (pbar);
736 context = gtk_widget_get_style_context (widget);
737 state = gtk_widget_get_state_flags (widget);
738 gtk_style_context_get_padding (context, state, &padding);
740 if (orientation == GTK_ORIENTATION_HORIZONTAL)
742 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
743 area.y = padding.top;
744 area.height = height - padding.top - padding.bottom;
748 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
749 area.x = padding.left;
750 area.width = width - padding.left - padding.right;
753 gtk_style_context_save (context);
754 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
756 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
758 gtk_style_context_restore (context);
762 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
765 GtkOrientation orientation,
770 GtkStyleContext *context;
773 GtkWidget *widget = GTK_WIDGET (pbar);
779 context = gtk_widget_get_style_context (widget);
780 state = gtk_widget_get_state_flags (widget);
781 gtk_style_context_get_padding (context, state, &padding);
783 if (orientation == GTK_ORIENTATION_HORIZONTAL)
786 area.height = height - padding.top - padding.bottom;
787 area.y = padding.top;
790 area.x = padding.left;
792 area.x = width - amount - padding.right;
796 area.width = width - padding.left - padding.right;
797 area.height = amount;
798 area.x = padding.left;
801 area.y = padding.top;
803 area.y = height - amount - padding.bottom;
806 gtk_style_context_save (context);
807 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
809 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
811 gtk_style_context_restore (context);
815 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
819 GtkOrientation orientation,
824 GtkProgressBarPrivate *priv = pbar->priv;
825 GtkStyleContext *context;
828 GtkWidget *widget = GTK_WIDGET (pbar);
834 PangoRectangle logical_rect;
835 GdkRectangle prelight_clip, start_clip, end_clip;
836 gfloat text_xalign = 0.5;
837 gfloat text_yalign = 0.5;
839 context = gtk_widget_get_style_context (widget);
840 state = gtk_widget_get_state_flags (widget);
841 gtk_style_context_get_padding (context, state, &padding);
843 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
844 text_xalign = 1.0 - text_xalign;
846 buf = get_current_text (pbar);
848 layout = gtk_widget_create_pango_layout (widget, buf);
849 pango_layout_set_ellipsize (layout, priv->ellipsize);
851 pango_layout_set_width (layout, width * PANGO_SCALE);
853 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
855 x = padding.left + 1 + text_xalign * (width - padding.left - padding.right - 2 - logical_rect.width);
856 y = padding.top + 1 + text_yalign * (height - padding.top - padding.bottom - 2 - logical_rect.height);
858 rect.x = padding.left;
859 rect.y = padding.top;
860 rect.width = width - padding.left - padding.right;
861 rect.height = height - padding.top - padding.bottom;
863 prelight_clip = start_clip = end_clip = rect;
865 if (orientation == GTK_ORIENTATION_HORIZONTAL)
870 prelight_clip.x = offset;
871 prelight_clip.width = amount;
872 start_clip.width = prelight_clip.x - start_clip.x;
873 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
874 end_clip.width -= prelight_clip.width + start_clip.width;
879 prelight_clip.x = offset;
881 prelight_clip.x = rect.x + rect.width - amount;
882 prelight_clip.width = amount;
883 start_clip.width = prelight_clip.x - start_clip.x;
884 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
885 end_clip.width -= prelight_clip.width + start_clip.width;
893 prelight_clip.y = offset;
894 prelight_clip.height = amount;
895 start_clip.height = prelight_clip.y - start_clip.y;
896 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
897 end_clip.height -= prelight_clip.height + start_clip.height;
902 prelight_clip.y = offset;
904 prelight_clip.y = rect.y + rect.height - amount;
905 prelight_clip.height = amount;
906 start_clip.height = prelight_clip.y - start_clip.y;
907 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
908 end_clip.height -= prelight_clip.height + start_clip.height;
912 gtk_style_context_save (context);
913 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
915 if (start_clip.width > 0 && start_clip.height > 0)
918 gdk_cairo_rectangle (cr, &start_clip);
921 gtk_render_layout (context, cr, x, y, layout);
925 if (end_clip.width > 0 && end_clip.height > 0)
928 gdk_cairo_rectangle (cr, &end_clip);
931 gtk_render_layout (context, cr, x, y, layout);
935 gtk_style_context_restore (context);
938 gdk_cairo_rectangle (cr, &prelight_clip);
941 gtk_style_context_save (context);
942 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
944 gtk_render_layout (context, cr, x, y, layout);
946 gtk_style_context_restore (context);
949 g_object_unref (layout);
954 gtk_progress_bar_draw (GtkWidget *widget,
957 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
958 GtkProgressBarPrivate *priv = pbar->priv;
959 GtkOrientation orientation;
961 GtkStyleContext *context;
966 context = gtk_widget_get_style_context (widget);
967 state = gtk_widget_get_state_flags (widget);
968 gtk_style_context_get_padding (context, state, &padding);
970 orientation = priv->orientation;
971 inverted = priv->inverted;
972 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
974 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
975 inverted = !inverted;
977 width = gtk_widget_get_allocated_width (widget);
978 height = gtk_widget_get_allocated_height (widget);
980 gtk_style_context_save (context);
981 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
983 gtk_render_background (context, cr, 0, 0, width, height);
984 gtk_render_frame (context, cr, 0, 0, width, height);
986 gtk_style_context_restore (context);
988 if (priv->activity_mode)
990 gtk_progress_bar_paint_activity (pbar, cr,
991 orientation, inverted,
999 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
1000 gtk_progress_bar_paint_text (pbar, cr,
1002 orientation, inverted,
1011 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1012 space = width - padding.left - padding.right;
1014 space = height - padding.top - padding.bottom;
1016 amount = space * gtk_progress_bar_get_fraction (pbar);
1018 gtk_progress_bar_paint_continuous (pbar, cr, amount, orientation, inverted, width, height);
1020 if (priv->show_text)
1021 gtk_progress_bar_paint_text (pbar, cr, -1, amount, orientation, inverted, width, height);
1028 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
1029 gboolean activity_mode)
1031 GtkProgressBarPrivate *priv = pbar->priv;
1033 activity_mode = !!activity_mode;
1035 if (priv->activity_mode != activity_mode)
1037 priv->activity_mode = activity_mode;
1039 if (priv->activity_mode)
1040 gtk_progress_bar_act_mode_enter (pbar);
1042 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1047 * gtk_progress_bar_set_fraction:
1048 * @pbar: a #GtkProgressBar
1049 * @fraction: fraction of the task that's been completed
1051 * Causes the progress bar to "fill in" the given fraction
1052 * of the bar. The fraction should be between 0.0 and 1.0,
1057 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1060 GtkProgressBarPrivate* priv;
1062 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1066 priv->fraction = fraction;
1067 gtk_progress_bar_set_activity_mode (pbar, FALSE);
1068 gtk_progress_bar_real_update (pbar);
1070 g_object_notify (G_OBJECT (pbar), "fraction");
1074 * gtk_progress_bar_pulse:
1075 * @pbar: a #GtkProgressBar
1077 * Indicates that some progress is made, but you don't know how much.
1078 * Causes the progress bar to enter "activity mode," where a block
1079 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1080 * causes the block to move by a little bit (the amount of movement
1081 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1084 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1086 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1088 gtk_progress_bar_set_activity_mode (pbar, TRUE);
1089 gtk_progress_bar_real_update (pbar);
1093 * gtk_progress_bar_set_text:
1094 * @pbar: a #GtkProgressBar
1095 * @text: (allow-none): a UTF-8 string, or %NULL
1097 * Causes the given @text to appear superimposed on the progress bar.
1100 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1103 GtkProgressBarPrivate *priv;
1105 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1109 g_free (priv->text);
1110 priv->text = text && *text ? g_strdup (text) : NULL;
1112 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1114 g_object_notify (G_OBJECT (pbar), "text");
1118 * gtk_progress_bar_set_show_text:
1119 * @pbar: a #GtkProgressBar
1120 * @show_text: whether to show superimposed text
1122 * Sets whether the progressbar will show text superimposed
1123 * over the bar. The shown text is either the value of
1124 * the #GtkProgressBar::text property or, if that is %NULL,
1125 * the #GtkProgressBar::fraction value, as a percentage.
1130 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1133 GtkProgressBarPrivate *priv;
1135 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1139 show_text = !!show_text;
1141 if (priv->show_text != show_text)
1143 priv->show_text = show_text;
1145 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1147 g_object_notify (G_OBJECT (pbar), "show-text");
1152 * gtk_progress_bar_get_show_text:
1153 * @pbar: a #GtkProgressBar
1155 * Gets the value of the #GtkProgressBar::show-text property.
1156 * See gtk_progress_bar_set_show_text().
1158 * Returns: %TRUE if text is shown in the progress bar
1163 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1165 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1167 return pbar->priv->show_text;
1171 * gtk_progress_bar_set_pulse_step:
1172 * @pbar: a #GtkProgressBar
1173 * @fraction: fraction between 0.0 and 1.0
1175 * Sets the fraction of total progress bar length to move the
1176 * bouncing block for each call to gtk_progress_bar_pulse().
1179 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1182 GtkProgressBarPrivate *priv;
1184 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1188 priv->pulse_fraction = fraction;
1190 g_object_notify (G_OBJECT (pbar), "pulse-step");
1194 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1195 GtkOrientation orientation)
1197 GtkProgressBarPrivate *priv = pbar->priv;
1199 if (priv->orientation != orientation)
1201 priv->orientation = orientation;
1202 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (pbar));
1204 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1209 * gtk_progress_bar_set_inverted:
1210 * @pbar: a #GtkProgressBar
1211 * @inverted: %TRUE to invert the progress bar
1213 * Progress bars normally grow from top to bottom or left to right.
1214 * Inverted progress bars grow in the opposite direction.
1217 gtk_progress_bar_set_inverted (GtkProgressBar *pbar,
1220 GtkProgressBarPrivate *priv;
1222 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1226 if (priv->inverted != inverted)
1228 priv->inverted = inverted;
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.
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;