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 progress bar 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.
189 * To make a progress bar that is styled and sized suitably for containing
190 * text (even if the actual text is blank), set #GtkProgressBar:show-text to
191 * %TRUE and #GtkProgressBar:text to the empty string (not %NULL).
195 g_object_class_install_property (gobject_class,
197 g_param_spec_boolean ("show-text",
199 P_("Whether the progress is shown as text."),
201 GTK_PARAM_READWRITE));
204 * GtkProgressBar:ellipsize:
206 * The preferred place to ellipsize the string, if the progress bar does
207 * not have enough room to display the entire string, specified as a
208 * #PangoEllisizeMode.
210 * Note that setting this property to a value other than
211 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progress bar requests
212 * only enough space to display the ellipsis ("..."). Another means to set a
213 * progress bar's width is gtk_widget_set_size_request().
217 g_object_class_install_property (gobject_class,
219 g_param_spec_enum ("ellipsize",
221 P_("The preferred place to ellipsize the string, if the progress bar "
222 "does not have enough room to display the entire string, if at all."),
223 PANGO_TYPE_ELLIPSIZE_MODE,
224 PANGO_ELLIPSIZE_NONE,
225 GTK_PARAM_READWRITE));
226 gtk_widget_class_install_style_property (widget_class,
227 g_param_spec_int ("xspacing",
229 P_("Extra spacing applied to the width of a progress bar."),
232 gtk_widget_class_install_style_property (widget_class,
233 g_param_spec_int ("yspacing",
235 P_("Extra spacing applied to the height of a progress bar."),
240 * GtkProgressBar:min-horizontal-bar-width:
242 * The minimum horizontal width of the progress bar.
246 gtk_widget_class_install_style_property (widget_class,
247 g_param_spec_int ("min-horizontal-bar-width",
248 P_("Minimum horizontal bar width"),
249 P_("The minimum horizontal width of the progress bar"),
250 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
253 * GtkProgressBar:min-horizontal-bar-height:
255 * Minimum horizontal height of the progress bar.
259 gtk_widget_class_install_style_property (widget_class,
260 g_param_spec_int ("min-horizontal-bar-height",
261 P_("Minimum horizontal bar height"),
262 P_("Minimum horizontal height of the progress bar"),
263 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
266 * GtkProgressBar:min-vertical-bar-width:
268 * The minimum vertical width of the progress bar.
272 gtk_widget_class_install_style_property (widget_class,
273 g_param_spec_int ("min-vertical-bar-width",
274 P_("Minimum vertical bar width"),
275 P_("The minimum vertical width of the progress bar"),
276 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
279 * GtkProgressBar:min-vertical-bar-height:
281 * The minimum vertical height of the progress bar.
285 gtk_widget_class_install_style_property (widget_class,
286 g_param_spec_int ("min-vertical-bar-height",
287 P_("Minimum vertical bar height"),
288 P_("The minimum vertical height of the progress bar"),
289 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
292 g_type_class_add_private (class, sizeof (GtkProgressBarPrivate));
294 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PROGRESS_BAR_ACCESSIBLE);
298 gtk_progress_bar_init (GtkProgressBar *pbar)
300 GtkProgressBarPrivate *priv;
302 pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
303 GTK_TYPE_PROGRESS_BAR,
304 GtkProgressBarPrivate);
307 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
308 priv->inverted = FALSE;
309 priv->pulse_fraction = 0.1;
310 priv->activity_pos = 0;
311 priv->activity_dir = 1;
312 priv->activity_step = 3;
313 priv->activity_blocks = 5;
314 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
315 priv->show_text = FALSE;
318 priv->fraction = 0.0;
320 gtk_widget_set_has_window (GTK_WIDGET (pbar), FALSE);
324 gtk_progress_bar_set_property (GObject *object,
329 GtkProgressBar *pbar;
331 pbar = GTK_PROGRESS_BAR (object);
335 case PROP_ORIENTATION:
336 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
339 gtk_progress_bar_set_inverted (pbar, g_value_get_boolean (value));
342 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
344 case PROP_PULSE_STEP:
345 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
348 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
351 gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
354 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 gtk_progress_bar_get_property (GObject *object,
368 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
369 GtkProgressBarPrivate* priv = pbar->priv;
373 case PROP_ORIENTATION:
374 g_value_set_enum (value, priv->orientation);
377 g_value_set_boolean (value, priv->inverted);
380 g_value_set_double (value, priv->fraction);
382 case PROP_PULSE_STEP:
383 g_value_set_double (value, priv->pulse_fraction);
386 g_value_set_string (value, priv->text);
389 g_value_set_boolean (value, priv->show_text);
392 g_value_set_enum (value, priv->ellipsize);
395 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
401 * gtk_progress_bar_new:
403 * Creates a new #GtkProgressBar.
405 * Returns: a #GtkProgressBar.
408 gtk_progress_bar_new (void)
412 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
418 gtk_progress_bar_real_update (GtkProgressBar *pbar)
420 GtkProgressBarPrivate *priv;
423 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
426 widget = GTK_WIDGET (pbar);
428 if (priv->activity_mode)
430 GtkAllocation allocation;
431 GtkStyleContext *context;
436 gtk_widget_get_allocation (widget, &allocation);
437 context = gtk_widget_get_style_context (widget);
438 state = gtk_widget_get_state_flags (widget);
439 gtk_style_context_get_padding (context, state, &padding);
441 /* advance the block */
442 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
444 /* Update our activity step. */
445 priv->activity_step = allocation.width * priv->pulse_fraction;
447 size = MAX (2, allocation.width / priv->activity_blocks);
449 if (priv->activity_dir == 0)
451 priv->activity_pos += priv->activity_step;
452 if (priv->activity_pos + size >= allocation.width - padding.left)
454 priv->activity_pos = allocation.width - padding.left - size;
455 priv->activity_dir = 1;
460 priv->activity_pos -= priv->activity_step;
461 if (priv->activity_pos <= padding.left)
463 priv->activity_pos = padding.left;
464 priv->activity_dir = 0;
470 /* Update our activity step. */
471 priv->activity_step = allocation.height * priv->pulse_fraction;
473 size = MAX (2, allocation.height / priv->activity_blocks);
475 if (priv->activity_dir == 0)
477 priv->activity_pos += priv->activity_step;
478 if (priv->activity_pos + size >= allocation.height - padding.top)
480 priv->activity_pos = allocation.height - padding.top - size;
481 priv->activity_dir = 1;
486 priv->activity_pos -= priv->activity_step;
487 if (priv->activity_pos <= padding.top)
489 priv->activity_pos = padding.top;
490 priv->activity_dir = 0;
495 gtk_widget_queue_draw (widget);
499 gtk_progress_bar_finalize (GObject *object)
501 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
502 GtkProgressBarPrivate *priv = pbar->priv;
506 G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
510 get_current_text (GtkProgressBar *pbar)
512 GtkProgressBarPrivate *priv = pbar->priv;
515 return g_strdup (priv->text);
517 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
521 gtk_progress_bar_get_preferred_width (GtkWidget *widget,
525 GtkProgressBar *pbar;
526 GtkProgressBarPrivate *priv;
527 GtkStyleContext *style_context;
531 PangoRectangle logical_rect;
537 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
539 style_context = gtk_widget_get_style_context (widget);
540 state = gtk_widget_get_state_flags (widget);
541 gtk_style_context_get_padding (style_context, state, &padding);
543 gtk_widget_style_get (widget,
544 "xspacing", &xspacing,
547 pbar = GTK_PROGRESS_BAR (widget);
550 width = padding.left + padding.right + xspacing;
554 buf = get_current_text (pbar);
555 layout = gtk_widget_create_pango_layout (widget, buf);
557 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
561 const PangoFontDescription *font_desc;
562 PangoContext *context;
563 PangoFontMetrics *metrics;
566 /* The minimum size for ellipsized text is ~ 3 chars */
567 context = pango_layout_get_context (layout);
568 font_desc = gtk_style_context_get_font (style_context, state);
569 metrics = pango_context_get_metrics (context, font_desc, pango_context_get_language (context));
571 char_width = pango_font_metrics_get_approximate_char_width (metrics);
572 pango_font_metrics_unref (metrics);
574 width += PANGO_PIXELS (char_width) * 3;
577 width += logical_rect.width;
579 g_object_unref (layout);
583 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
584 gtk_widget_style_get (widget,
585 "min-horizontal-bar-width", &min_width,
588 gtk_widget_style_get (widget,
589 "min-vertical-bar-width", &min_width,
592 *minimum = *natural = MAX (min_width, width);
596 gtk_progress_bar_get_preferred_height (GtkWidget *widget,
600 GtkProgressBar *pbar;
601 GtkProgressBarPrivate *priv;
602 GtkStyleContext *context;
606 PangoRectangle logical_rect;
612 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
614 context = gtk_widget_get_style_context (widget);
615 state = gtk_widget_get_state_flags (widget);
616 gtk_style_context_get_padding (context, state, &padding);
618 gtk_widget_style_get (widget,
619 "yspacing", &yspacing,
622 pbar = GTK_PROGRESS_BAR (widget);
625 height = padding.top + padding.bottom + yspacing;
629 buf = get_current_text (pbar);
630 layout = gtk_widget_create_pango_layout (widget, buf);
632 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
634 height += logical_rect.height;
636 g_object_unref (layout);
640 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
641 gtk_widget_style_get (widget,
642 "min-horizontal-bar-height", &min_height,
645 gtk_widget_style_get (widget,
646 "min-vertical-bar-height", &min_height,
649 *minimum = *natural = MAX (min_height, height);
653 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
655 GtkProgressBarPrivate *priv = pbar->priv;
656 GtkAllocation allocation;
657 GtkStyleContext *context;
660 GtkWidget *widget = GTK_WIDGET (pbar);
661 GtkOrientation orientation;
664 context = gtk_widget_get_style_context (widget);
665 state = gtk_widget_get_state_flags (widget);
666 gtk_style_context_get_padding (context, state, &padding);
668 orientation = priv->orientation;
669 inverted = priv->inverted;
670 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
672 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
673 inverted = !inverted;
676 /* calculate start pos */
678 if (orientation == GTK_ORIENTATION_HORIZONTAL)
682 priv->activity_pos = padding.left;
683 priv->activity_dir = 0;
687 gtk_widget_get_allocation (widget, &allocation);
688 priv->activity_pos = allocation.width - padding.left -
689 (allocation.height - padding.top - padding.bottom);
690 priv->activity_dir = 1;
697 priv->activity_pos = padding.top;
698 priv->activity_dir = 0;
702 gtk_widget_get_allocation (widget, &allocation);
703 priv->activity_pos = allocation.height - padding.top -
704 (allocation.width - padding.left - padding.right);
705 priv->activity_dir = 1;
711 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
712 GtkOrientation orientation,
716 GtkProgressBarPrivate *priv = pbar->priv;
717 GtkAllocation allocation;
718 GtkWidget *widget = GTK_WIDGET (pbar);
720 *offset = priv->activity_pos;
722 gtk_widget_get_allocation (widget, &allocation);
724 if (orientation == GTK_ORIENTATION_HORIZONTAL)
725 *amount = MAX (2, allocation.width / priv->activity_blocks);
727 *amount = MAX (2, allocation.height / priv->activity_blocks);
731 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
733 GtkOrientation orientation,
738 GtkStyleContext *context;
741 GtkWidget *widget = GTK_WIDGET (pbar);
744 context = gtk_widget_get_style_context (widget);
745 state = gtk_widget_get_state_flags (widget);
746 gtk_style_context_get_padding (context, state, &padding);
748 if (orientation == GTK_ORIENTATION_HORIZONTAL)
750 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
751 area.y = padding.top;
752 area.height = height - padding.top - padding.bottom;
756 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
757 area.x = padding.left;
758 area.width = width - padding.left - padding.right;
761 gtk_style_context_save (context);
762 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
763 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PULSE);
765 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
767 gtk_style_context_restore (context);
771 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
774 GtkOrientation orientation,
779 GtkStyleContext *context;
782 GtkWidget *widget = GTK_WIDGET (pbar);
788 context = gtk_widget_get_style_context (widget);
789 state = gtk_widget_get_state_flags (widget);
790 gtk_style_context_get_padding (context, state, &padding);
792 if (orientation == GTK_ORIENTATION_HORIZONTAL)
795 area.height = height - padding.top - padding.bottom;
796 area.y = padding.top;
799 area.x = padding.left;
801 area.x = width - amount - padding.right;
805 area.width = width - padding.left - padding.right;
806 area.height = amount;
807 area.x = padding.left;
810 area.y = padding.top;
812 area.y = height - amount - padding.bottom;
815 gtk_style_context_save (context);
816 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
818 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
820 gtk_style_context_restore (context);
824 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
828 GtkOrientation orientation,
833 GtkProgressBarPrivate *priv = pbar->priv;
834 GtkStyleContext *context;
837 GtkWidget *widget = GTK_WIDGET (pbar);
843 PangoRectangle logical_rect;
844 GdkRectangle prelight_clip, start_clip, end_clip;
845 gfloat text_xalign = 0.5;
846 gfloat text_yalign = 0.5;
848 context = gtk_widget_get_style_context (widget);
849 state = gtk_widget_get_state_flags (widget);
850 gtk_style_context_get_padding (context, state, &padding);
852 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
853 text_xalign = 1.0 - text_xalign;
855 buf = get_current_text (pbar);
857 layout = gtk_widget_create_pango_layout (widget, buf);
858 pango_layout_set_ellipsize (layout, priv->ellipsize);
860 pango_layout_set_width (layout, width * PANGO_SCALE);
862 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
864 x = padding.left + 1 + text_xalign * (width - padding.left - padding.right - 2 - logical_rect.width);
865 y = padding.top + 1 + text_yalign * (height - padding.top - padding.bottom - 2 - logical_rect.height);
867 rect.x = padding.left;
868 rect.y = padding.top;
869 rect.width = width - padding.left - padding.right;
870 rect.height = height - padding.top - padding.bottom;
872 prelight_clip = start_clip = end_clip = rect;
874 if (orientation == GTK_ORIENTATION_HORIZONTAL)
879 prelight_clip.x = offset;
880 prelight_clip.width = amount;
881 start_clip.width = prelight_clip.x - start_clip.x;
882 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
883 end_clip.width -= prelight_clip.width + start_clip.width;
888 prelight_clip.x = offset;
890 prelight_clip.x = rect.x + rect.width - amount;
891 prelight_clip.width = amount;
892 start_clip.width = prelight_clip.x - start_clip.x;
893 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
894 end_clip.width -= prelight_clip.width + start_clip.width;
902 prelight_clip.y = offset;
903 prelight_clip.height = amount;
904 start_clip.height = prelight_clip.y - start_clip.y;
905 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
906 end_clip.height -= prelight_clip.height + start_clip.height;
911 prelight_clip.y = offset;
913 prelight_clip.y = rect.y + rect.height - amount;
914 prelight_clip.height = amount;
915 start_clip.height = prelight_clip.y - start_clip.y;
916 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
917 end_clip.height -= prelight_clip.height + start_clip.height;
921 gtk_style_context_save (context);
922 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
924 if (start_clip.width > 0 && start_clip.height > 0)
927 gdk_cairo_rectangle (cr, &start_clip);
930 gtk_render_layout (context, cr, x, y, layout);
934 if (end_clip.width > 0 && end_clip.height > 0)
937 gdk_cairo_rectangle (cr, &end_clip);
940 gtk_render_layout (context, cr, x, y, layout);
944 gtk_style_context_restore (context);
947 gdk_cairo_rectangle (cr, &prelight_clip);
950 gtk_style_context_save (context);
951 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
953 gtk_render_layout (context, cr, x, y, layout);
955 gtk_style_context_restore (context);
958 g_object_unref (layout);
963 gtk_progress_bar_draw (GtkWidget *widget,
966 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
967 GtkProgressBarPrivate *priv = pbar->priv;
968 GtkOrientation orientation;
970 GtkStyleContext *context;
975 context = gtk_widget_get_style_context (widget);
976 state = gtk_widget_get_state_flags (widget);
977 gtk_style_context_get_padding (context, state, &padding);
979 orientation = priv->orientation;
980 inverted = priv->inverted;
981 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
983 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
984 inverted = !inverted;
986 width = gtk_widget_get_allocated_width (widget);
987 height = gtk_widget_get_allocated_height (widget);
989 gtk_style_context_save (context);
990 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
992 gtk_render_background (context, cr, 0, 0, width, height);
993 gtk_render_frame (context, cr, 0, 0, width, height);
995 gtk_style_context_restore (context);
997 if (priv->activity_mode)
999 gtk_progress_bar_paint_activity (pbar, cr,
1000 orientation, inverted,
1003 if (priv->show_text)
1008 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
1009 gtk_progress_bar_paint_text (pbar, cr,
1011 orientation, inverted,
1020 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1021 space = width - padding.left - padding.right;
1023 space = height - padding.top - padding.bottom;
1025 amount = space * gtk_progress_bar_get_fraction (pbar);
1027 gtk_progress_bar_paint_continuous (pbar, cr, amount, orientation, inverted, width, height);
1029 if (priv->show_text)
1030 gtk_progress_bar_paint_text (pbar, cr, -1, amount, orientation, inverted, width, height);
1037 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
1038 gboolean activity_mode)
1040 GtkProgressBarPrivate *priv = pbar->priv;
1042 activity_mode = !!activity_mode;
1044 if (priv->activity_mode != activity_mode)
1046 priv->activity_mode = activity_mode;
1048 if (priv->activity_mode)
1049 gtk_progress_bar_act_mode_enter (pbar);
1051 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1056 * gtk_progress_bar_set_fraction:
1057 * @pbar: a #GtkProgressBar
1058 * @fraction: fraction of the task that's been completed
1060 * Causes the progress bar to "fill in" the given fraction
1061 * of the bar. The fraction should be between 0.0 and 1.0,
1065 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1068 GtkProgressBarPrivate* priv;
1070 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1074 priv->fraction = CLAMP(fraction, 0.0, 1.0);
1075 gtk_progress_bar_set_activity_mode (pbar, FALSE);
1076 gtk_progress_bar_real_update (pbar);
1078 g_object_notify (G_OBJECT (pbar), "fraction");
1082 * gtk_progress_bar_pulse:
1083 * @pbar: a #GtkProgressBar
1085 * Indicates that some progress has been made, but you don't know how much.
1086 * Causes the progress bar to enter "activity mode," where a block
1087 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1088 * causes the block to move by a little bit (the amount of movement
1089 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1092 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1094 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1096 gtk_progress_bar_set_activity_mode (pbar, TRUE);
1097 gtk_progress_bar_real_update (pbar);
1101 * gtk_progress_bar_set_text:
1102 * @pbar: a #GtkProgressBar
1103 * @text: (allow-none): a UTF-8 string, or %NULL
1105 * Causes the given @text to appear superimposed on the progress bar.
1107 * If @text is %NULL and #GtkProgressBar:show-text is %TRUE, the current
1108 * value of #GtkProgressBar:fraction will be displayed as a percentage.
1110 * If @text is non-%NULL and #GtkProgressBar:show-text is %TRUE, the text will
1111 * be displayed. In this case, it will not display the progress percentage.
1112 * If @text is the empty string, the progress bar will still be styled and sized
1113 * suitably for containing text, as long as #GtkProgressBar:show-text is %TRUE.
1116 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1119 GtkProgressBarPrivate *priv;
1121 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1125 /* Don't notify again if nothing's changed. */
1126 if (g_strcmp0 (priv->text, text) == 0)
1129 g_free (priv->text);
1130 priv->text = g_strdup (text);
1132 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1134 g_object_notify (G_OBJECT (pbar), "text");
1138 * gtk_progress_bar_set_show_text:
1139 * @pbar: a #GtkProgressBar
1140 * @show_text: whether to show superimposed text
1142 * Sets whether the progress bar will show text superimposed
1143 * over the bar. The shown text is either the value of
1144 * the #GtkProgressBar:text property or, if that is %NULL,
1145 * the #GtkProgressBar:fraction value, as a percentage.
1147 * To make a progress bar that is styled and sized suitably for containing
1148 * text (even if the actual text is blank), set #GtkProgressBar:show-text to
1149 * %TRUE and #GtkProgressBar:text to the empty string (not %NULL).
1154 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1157 GtkProgressBarPrivate *priv;
1159 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1163 show_text = !!show_text;
1165 if (priv->show_text != show_text)
1167 priv->show_text = show_text;
1169 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1171 g_object_notify (G_OBJECT (pbar), "show-text");
1176 * gtk_progress_bar_get_show_text:
1177 * @pbar: a #GtkProgressBar
1179 * Gets the value of the #GtkProgressBar:show-text property.
1180 * See gtk_progress_bar_set_show_text().
1182 * Returns: %TRUE if text is shown in the progress bar
1187 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1189 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1191 return pbar->priv->show_text;
1195 * gtk_progress_bar_set_pulse_step:
1196 * @pbar: a #GtkProgressBar
1197 * @fraction: fraction between 0.0 and 1.0
1199 * Sets the fraction of total progress bar length to move the
1200 * bouncing block for each call to gtk_progress_bar_pulse().
1203 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1206 GtkProgressBarPrivate *priv;
1208 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1212 priv->pulse_fraction = fraction;
1214 g_object_notify (G_OBJECT (pbar), "pulse-step");
1218 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1219 GtkOrientation orientation)
1221 GtkProgressBarPrivate *priv = pbar->priv;
1223 if (priv->orientation != orientation)
1225 priv->orientation = orientation;
1226 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (pbar));
1228 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1233 * gtk_progress_bar_set_inverted:
1234 * @pbar: a #GtkProgressBar
1235 * @inverted: %TRUE to invert the progress bar
1237 * Progress bars normally grow from top to bottom or left to right.
1238 * Inverted progress bars grow in the opposite direction.
1241 gtk_progress_bar_set_inverted (GtkProgressBar *pbar,
1244 GtkProgressBarPrivate *priv;
1246 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1250 if (priv->inverted != inverted)
1252 priv->inverted = inverted;
1254 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1256 g_object_notify (G_OBJECT (pbar), "inverted");
1261 * gtk_progress_bar_get_text:
1262 * @pbar: a #GtkProgressBar
1264 * Retrieves the text displayed superimposed on the progress bar,
1265 * if any, otherwise %NULL. The return value is a reference
1266 * to the text, not a copy of it, so will become invalid
1267 * if you change the text in the progress bar.
1269 * Return value: text, or %NULL; this string is owned by the widget
1270 * and should not be modified or freed.
1273 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1275 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1277 return pbar->priv->text;
1281 * gtk_progress_bar_get_fraction:
1282 * @pbar: a #GtkProgressBar
1284 * Returns the current fraction of the task that's been completed.
1286 * Return value: a fraction from 0.0 to 1.0
1289 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1291 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1293 return pbar->priv->fraction;
1297 * gtk_progress_bar_get_pulse_step:
1298 * @pbar: a #GtkProgressBar
1300 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step().
1302 * Return value: a fraction from 0.0 to 1.0
1305 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1307 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1309 return pbar->priv->pulse_fraction;
1313 * gtk_progress_bar_get_inverted:
1314 * @pbar: a #GtkProgressBar
1316 * Gets the value set by gtk_progress_bar_set_inverted().
1318 * Return value: %TRUE if the progress bar is inverted
1321 gtk_progress_bar_get_inverted (GtkProgressBar *pbar)
1323 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1325 return pbar->priv->inverted;
1329 * gtk_progress_bar_set_ellipsize:
1330 * @pbar: a #GtkProgressBar
1331 * @mode: a #PangoEllipsizeMode
1333 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1334 * if there is not enough space to render the entire string.
1339 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1340 PangoEllipsizeMode mode)
1342 GtkProgressBarPrivate *priv;
1344 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1345 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1346 mode <= PANGO_ELLIPSIZE_END);
1350 if ((PangoEllipsizeMode)priv->ellipsize != mode)
1352 priv->ellipsize = mode;
1354 g_object_notify (G_OBJECT (pbar), "ellipsize");
1355 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1360 * gtk_progress_bar_get_ellipsize:
1361 * @pbar: a #GtkProgressBar
1363 * Returns the ellipsizing position of the progress bar.
1364 * See gtk_progress_bar_set_ellipsize().
1366 * Return value: #PangoEllipsizeMode
1371 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1373 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1375 return pbar->priv->ellipsize;