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));
183 * GtkProgressBar:show_text:
185 * Sets whether the progressbar will show text superimposed
186 * over the bar. The shown text is either the value of
187 * the #GtkProgressBar::text property or, if that is %NULL,
188 * the #GtkProgressBar::fraction value, as a percentage.
192 g_object_class_install_property (gobject_class,
194 g_param_spec_boolean ("show-text",
196 P_("Whether the progress is shown as text."),
198 GTK_PARAM_READWRITE));
201 * GtkProgressBar:ellipsize:
203 * The preferred place to ellipsize the string, if the progressbar does
204 * not have enough room to display the entire string, specified as a
205 * #PangoEllisizeMode.
207 * Note that setting this property to a value other than
208 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
209 * only enough space to display the ellipsis "...". Another means to set a
210 * progressbar's width is gtk_widget_set_size_request().
214 g_object_class_install_property (gobject_class,
216 g_param_spec_enum ("ellipsize",
218 P_("The preferred place to ellipsize the string, if the progress bar "
219 "does not have enough room to display the entire string, if at all."),
220 PANGO_TYPE_ELLIPSIZE_MODE,
221 PANGO_ELLIPSIZE_NONE,
222 GTK_PARAM_READWRITE));
223 gtk_widget_class_install_style_property (widget_class,
224 g_param_spec_int ("xspacing",
226 P_("Extra spacing applied to the width of a progress bar."),
229 gtk_widget_class_install_style_property (widget_class,
230 g_param_spec_int ("yspacing",
232 P_("Extra spacing applied to the height of a progress bar."),
237 * GtkProgressBar:min-horizontal-bar-width:
239 * The minimum horizontal width of the progress bar.
243 gtk_widget_class_install_style_property (widget_class,
244 g_param_spec_int ("min-horizontal-bar-width",
245 P_("Minimum horizontal bar width"),
246 P_("The minimum horizontal width of the progress bar"),
247 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
250 * GtkProgressBar:min-horizontal-bar-height:
252 * Minimum horizontal height of the progress bar.
256 gtk_widget_class_install_style_property (widget_class,
257 g_param_spec_int ("min-horizontal-bar-height",
258 P_("Minimum horizontal bar height"),
259 P_("Minimum horizontal height of the progress bar"),
260 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
263 * GtkProgressBar:min-vertical-bar-width:
265 * The minimum vertical width of the progress bar.
269 gtk_widget_class_install_style_property (widget_class,
270 g_param_spec_int ("min-vertical-bar-width",
271 P_("Minimum vertical bar width"),
272 P_("The minimum vertical width of the progress bar"),
273 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
276 * GtkProgressBar:min-vertical-bar-height:
278 * The minimum vertical height of the progress bar.
282 gtk_widget_class_install_style_property (widget_class,
283 g_param_spec_int ("min-vertical-bar-height",
284 P_("Minimum vertical bar height"),
285 P_("The minimum vertical height of the progress bar"),
286 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
289 g_type_class_add_private (class, sizeof (GtkProgressBarPrivate));
293 gtk_progress_bar_init (GtkProgressBar *pbar)
295 GtkProgressBarPrivate *priv;
297 pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
298 GTK_TYPE_PROGRESS_BAR,
299 GtkProgressBarPrivate);
304 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
305 priv->inverted = FALSE;
306 priv->pulse_fraction = 0.1;
307 priv->activity_pos = 0;
308 priv->activity_dir = 1;
309 priv->activity_step = 3;
310 priv->activity_blocks = 5;
311 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
312 priv->show_text = FALSE;
315 priv->fraction = 0.0;
317 gtk_widget_set_has_window (GTK_WIDGET (pbar), FALSE);
321 gtk_progress_bar_set_property (GObject *object,
326 GtkProgressBar *pbar;
328 pbar = GTK_PROGRESS_BAR (object);
332 case PROP_ORIENTATION:
333 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
336 gtk_progress_bar_set_inverted (pbar, g_value_get_boolean (value));
339 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
341 case PROP_PULSE_STEP:
342 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
345 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
348 gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
351 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
354 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360 gtk_progress_bar_get_property (GObject *object,
365 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
366 GtkProgressBarPrivate* priv = pbar->priv;
370 case PROP_ORIENTATION:
371 g_value_set_enum (value, priv->orientation);
374 g_value_set_boolean (value, priv->inverted);
377 g_value_set_double (value, priv->fraction);
379 case PROP_PULSE_STEP:
380 g_value_set_double (value, priv->pulse_fraction);
383 g_value_set_string (value, priv->text);
386 g_value_set_boolean (value, priv->show_text);
389 g_value_set_enum (value, priv->ellipsize);
392 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
398 * gtk_progress_bar_new:
400 * Creates a new #GtkProgressBar.
402 * Returns: a #GtkProgressBar.
405 gtk_progress_bar_new (void)
409 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
415 gtk_progress_bar_real_update (GtkProgressBar *pbar)
417 GtkProgressBarPrivate *priv;
420 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
423 widget = GTK_WIDGET (pbar);
425 if (priv->activity_mode)
427 GtkAllocation allocation;
428 GtkStyleContext *context;
433 gtk_widget_get_allocation (widget, &allocation);
434 context = gtk_widget_get_style_context (widget);
435 state = gtk_widget_get_state_flags (widget);
436 gtk_style_context_get_padding (context, state, &padding);
438 /* advance the block */
439 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
441 /* Update our activity step. */
442 priv->activity_step = allocation.width * priv->pulse_fraction;
444 size = MAX (2, allocation.width / priv->activity_blocks);
446 if (priv->activity_dir == 0)
448 priv->activity_pos += priv->activity_step;
449 if (priv->activity_pos + size >= allocation.width - padding.left)
451 priv->activity_pos = allocation.width - padding.left - size;
452 priv->activity_dir = 1;
457 priv->activity_pos -= priv->activity_step;
458 if (priv->activity_pos <= padding.left)
460 priv->activity_pos = padding.left;
461 priv->activity_dir = 0;
467 /* Update our activity step. */
468 priv->activity_step = allocation.height * priv->pulse_fraction;
470 size = MAX (2, allocation.height / priv->activity_blocks);
472 if (priv->activity_dir == 0)
474 priv->activity_pos += priv->activity_step;
475 if (priv->activity_pos + size >= allocation.height - padding.top)
477 priv->activity_pos = allocation.height - padding.top - size;
478 priv->activity_dir = 1;
483 priv->activity_pos -= priv->activity_step;
484 if (priv->activity_pos <= padding.top)
486 priv->activity_pos = padding.top;
487 priv->activity_dir = 0;
492 gtk_widget_queue_draw (widget);
496 gtk_progress_bar_finalize (GObject *object)
498 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
499 GtkProgressBarPrivate *priv = pbar->priv;
503 G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
507 get_current_text (GtkProgressBar *pbar)
509 GtkProgressBarPrivate *priv = pbar->priv;
512 return g_strdup (priv->text);
514 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
518 gtk_progress_bar_get_preferred_width (GtkWidget *widget,
522 GtkProgressBar *pbar;
523 GtkProgressBarPrivate *priv;
524 GtkStyleContext *style_context;
528 PangoRectangle logical_rect;
534 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
536 style_context = gtk_widget_get_style_context (widget);
537 state = gtk_widget_get_state_flags (widget);
538 gtk_style_context_get_padding (style_context, state, &padding);
540 gtk_widget_style_get (widget,
541 "xspacing", &xspacing,
544 pbar = GTK_PROGRESS_BAR (widget);
547 width = padding.left + padding.right + xspacing;
551 buf = get_current_text (pbar);
552 layout = gtk_widget_create_pango_layout (widget, buf);
554 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
558 const PangoFontDescription *font_desc;
559 PangoContext *context;
560 PangoFontMetrics *metrics;
563 /* The minimum size for ellipsized text is ~ 3 chars */
564 context = pango_layout_get_context (layout);
565 font_desc = gtk_style_context_get_font (style_context, state);
566 metrics = pango_context_get_metrics (context, font_desc, pango_context_get_language (context));
568 char_width = pango_font_metrics_get_approximate_char_width (metrics);
569 pango_font_metrics_unref (metrics);
571 width += PANGO_PIXELS (char_width) * 3;
574 width += logical_rect.width;
576 g_object_unref (layout);
580 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
581 gtk_widget_style_get (widget,
582 "min-horizontal-bar-width", &min_width,
585 gtk_widget_style_get (widget,
586 "min-vertical-bar-width", &min_width,
589 *minimum = *natural = MAX (min_width, width);
593 gtk_progress_bar_get_preferred_height (GtkWidget *widget,
597 GtkProgressBar *pbar;
598 GtkProgressBarPrivate *priv;
599 GtkStyleContext *context;
603 PangoRectangle logical_rect;
609 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
611 context = gtk_widget_get_style_context (widget);
612 state = gtk_widget_get_state_flags (widget);
613 gtk_style_context_get_padding (context, state, &padding);
615 gtk_widget_style_get (widget,
616 "yspacing", &yspacing,
619 pbar = GTK_PROGRESS_BAR (widget);
622 height = padding.top + padding.bottom + yspacing;
626 buf = get_current_text (pbar);
627 layout = gtk_widget_create_pango_layout (widget, buf);
629 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
631 height += logical_rect.height;
633 g_object_unref (layout);
637 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
638 gtk_widget_style_get (widget,
639 "min-horizontal-bar-height", &min_height,
642 gtk_widget_style_get (widget,
643 "min-vertical-bar-height", &min_height,
646 *minimum = *natural = MAX (min_height, height);
650 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
652 GtkProgressBarPrivate *priv = pbar->priv;
653 GtkAllocation allocation;
654 GtkStyleContext *context;
657 GtkWidget *widget = GTK_WIDGET (pbar);
658 GtkOrientation orientation;
661 context = gtk_widget_get_style_context (widget);
662 state = gtk_widget_get_state_flags (widget);
663 gtk_style_context_get_padding (context, state, &padding);
665 orientation = priv->orientation;
666 inverted = priv->inverted;
667 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
669 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
670 inverted = !inverted;
673 /* calculate start pos */
675 if (orientation == GTK_ORIENTATION_HORIZONTAL)
679 priv->activity_pos = padding.left;
680 priv->activity_dir = 0;
684 gtk_widget_get_allocation (widget, &allocation);
685 priv->activity_pos = allocation.width - padding.left -
686 (allocation.height - padding.top - padding.bottom);
687 priv->activity_dir = 1;
694 priv->activity_pos = padding.top;
695 priv->activity_dir = 0;
699 gtk_widget_get_allocation (widget, &allocation);
700 priv->activity_pos = allocation.height - padding.top -
701 (allocation.width - padding.left - padding.right);
702 priv->activity_dir = 1;
708 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
709 GtkOrientation orientation,
713 GtkProgressBarPrivate *priv = pbar->priv;
714 GtkAllocation allocation;
715 GtkWidget *widget = GTK_WIDGET (pbar);
717 *offset = priv->activity_pos;
719 gtk_widget_get_allocation (widget, &allocation);
721 if (orientation == GTK_ORIENTATION_HORIZONTAL)
722 *amount = MAX (2, allocation.width / priv->activity_blocks);
724 *amount = MAX (2, allocation.height / priv->activity_blocks);
728 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
730 GtkOrientation orientation,
735 GtkStyleContext *context;
738 GtkWidget *widget = GTK_WIDGET (pbar);
741 context = gtk_widget_get_style_context (widget);
742 state = gtk_widget_get_state_flags (widget);
743 gtk_style_context_get_padding (context, state, &padding);
745 if (orientation == GTK_ORIENTATION_HORIZONTAL)
747 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
748 area.y = padding.top;
749 area.height = height - padding.top - padding.bottom;
753 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
754 area.x = padding.left;
755 area.width = width - padding.left - padding.right;
758 gtk_style_context_save (context);
759 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
761 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
763 gtk_style_context_restore (context);
767 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
770 GtkOrientation orientation,
775 GtkStyleContext *context;
778 GtkWidget *widget = GTK_WIDGET (pbar);
784 context = gtk_widget_get_style_context (widget);
785 state = gtk_widget_get_state_flags (widget);
786 gtk_style_context_get_padding (context, state, &padding);
788 if (orientation == GTK_ORIENTATION_HORIZONTAL)
791 area.height = height - padding.top - padding.bottom;
792 area.y = padding.top;
795 area.x = padding.left;
797 area.x = width - amount - padding.right;
801 area.width = width - padding.left - padding.right;
802 area.height = amount;
803 area.x = padding.left;
806 area.y = padding.top;
808 area.y = height - amount - padding.bottom;
811 gtk_style_context_save (context);
812 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
814 gtk_render_activity (context, cr, area.x, area.y, area.width, area.height);
816 gtk_style_context_restore (context);
820 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
824 GtkOrientation orientation,
829 GtkProgressBarPrivate *priv = pbar->priv;
830 GtkStyleContext *context;
833 GtkWidget *widget = GTK_WIDGET (pbar);
839 PangoRectangle logical_rect;
840 GdkRectangle prelight_clip, start_clip, end_clip;
841 gfloat text_xalign = 0.5;
842 gfloat text_yalign = 0.5;
844 context = gtk_widget_get_style_context (widget);
845 state = gtk_widget_get_state_flags (widget);
846 gtk_style_context_get_padding (context, state, &padding);
848 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
849 text_xalign = 1.0 - text_xalign;
851 buf = get_current_text (pbar);
853 layout = gtk_widget_create_pango_layout (widget, buf);
854 pango_layout_set_ellipsize (layout, priv->ellipsize);
856 pango_layout_set_width (layout, width * PANGO_SCALE);
858 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
860 x = padding.left + 1 + text_xalign * (width - padding.left - padding.right - 2 - logical_rect.width);
861 y = padding.top + 1 + text_yalign * (height - padding.top - padding.bottom - 2 - logical_rect.height);
863 rect.x = padding.left;
864 rect.y = padding.top;
865 rect.width = width - padding.left - padding.right;
866 rect.height = height - padding.top - padding.bottom;
868 prelight_clip = start_clip = end_clip = rect;
870 if (orientation == GTK_ORIENTATION_HORIZONTAL)
875 prelight_clip.x = offset;
876 prelight_clip.width = amount;
877 start_clip.width = prelight_clip.x - start_clip.x;
878 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
879 end_clip.width -= prelight_clip.width + start_clip.width;
884 prelight_clip.x = offset;
886 prelight_clip.x = rect.x + rect.width - amount;
887 prelight_clip.width = amount;
888 start_clip.width = prelight_clip.x - start_clip.x;
889 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
890 end_clip.width -= prelight_clip.width + start_clip.width;
898 prelight_clip.y = offset;
899 prelight_clip.height = amount;
900 start_clip.height = prelight_clip.y - start_clip.y;
901 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
902 end_clip.height -= prelight_clip.height + start_clip.height;
907 prelight_clip.y = offset;
909 prelight_clip.y = rect.y + rect.height - amount;
910 prelight_clip.height = amount;
911 start_clip.height = prelight_clip.y - start_clip.y;
912 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
913 end_clip.height -= prelight_clip.height + start_clip.height;
917 gtk_style_context_save (context);
918 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
920 if (start_clip.width > 0 && start_clip.height > 0)
923 gdk_cairo_rectangle (cr, &start_clip);
926 gtk_render_layout (context, cr, x, y, layout);
930 if (end_clip.width > 0 && end_clip.height > 0)
933 gdk_cairo_rectangle (cr, &end_clip);
936 gtk_render_layout (context, cr, x, y, layout);
940 gtk_style_context_restore (context);
943 gdk_cairo_rectangle (cr, &prelight_clip);
946 gtk_style_context_save (context);
947 gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
949 gtk_render_layout (context, cr, x, y, layout);
951 gtk_style_context_restore (context);
954 g_object_unref (layout);
959 gtk_progress_bar_draw (GtkWidget *widget,
962 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
963 GtkProgressBarPrivate *priv = pbar->priv;
964 GtkOrientation orientation;
966 GtkStyleContext *context;
971 context = gtk_widget_get_style_context (widget);
972 state = gtk_widget_get_state_flags (widget);
974 orientation = priv->orientation;
975 inverted = priv->inverted;
976 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
978 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
979 inverted = !inverted;
981 width = gtk_widget_get_allocated_width (widget);
982 height = gtk_widget_get_allocated_height (widget);
984 gtk_style_context_save (context);
985 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
987 gtk_render_background (context, cr, 0, 0, width, height);
988 gtk_render_frame (context, cr, 0, 0, width, height);
990 gtk_style_context_get_padding (context, state, &padding);
992 gtk_style_context_restore (context);
994 if (priv->activity_mode)
996 gtk_progress_bar_paint_activity (pbar, cr,
997 orientation, inverted,
1000 if (priv->show_text)
1005 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
1006 gtk_progress_bar_paint_text (pbar, cr,
1008 orientation, inverted,
1017 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1018 space = width - padding.left - padding.right;
1020 space = height - padding.top - padding.bottom;
1022 amount = space * gtk_progress_bar_get_fraction (pbar);
1024 gtk_progress_bar_paint_continuous (pbar, cr, amount, orientation, inverted, width, height);
1026 if (priv->show_text)
1027 gtk_progress_bar_paint_text (pbar, cr, -1, amount, orientation, inverted, width, height);
1034 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
1035 gboolean activity_mode)
1037 GtkProgressBarPrivate *priv = pbar->priv;
1039 activity_mode = !!activity_mode;
1041 if (priv->activity_mode != activity_mode)
1043 priv->activity_mode = activity_mode;
1045 if (priv->activity_mode)
1046 gtk_progress_bar_act_mode_enter (pbar);
1048 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1049 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1054 * gtk_progress_bar_set_fraction:
1055 * @pbar: a #GtkProgressBar
1056 * @fraction: fraction of the task that's been completed
1058 * Causes the progress bar to "fill in" the given fraction
1059 * of the bar. The fraction should be between 0.0 and 1.0,
1064 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1067 GtkProgressBarPrivate* priv;
1069 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1073 priv->fraction = fraction;
1074 gtk_progress_bar_set_activity_mode (pbar, FALSE);
1075 gtk_progress_bar_real_update (pbar);
1077 g_object_notify (G_OBJECT (pbar), "fraction");
1081 * gtk_progress_bar_pulse:
1082 * @pbar: a #GtkProgressBar
1084 * Indicates that some progress is made, but you don't know how much.
1085 * Causes the progress bar to enter "activity mode," where a block
1086 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1087 * causes the block to move by a little bit (the amount of movement
1088 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1091 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1093 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1095 gtk_progress_bar_set_activity_mode (pbar, TRUE);
1096 gtk_progress_bar_real_update (pbar);
1100 * gtk_progress_bar_set_text:
1101 * @pbar: a #GtkProgressBar
1102 * @text: (allow-none): a UTF-8 string, or %NULL
1104 * Causes the given @text to appear superimposed on the progress bar.
1107 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1110 GtkProgressBarPrivate *priv;
1112 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1116 g_free (priv->text);
1117 priv->text = text && *text ? g_strdup (text) : NULL;
1119 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1120 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1122 g_object_notify (G_OBJECT (pbar), "text");
1126 * gtk_progress_bar_set_show_text:
1127 * @pbar: a #GtkProgressBar
1128 * @show_text: whether to show superimposed text
1130 * Sets whether the progressbar will show text superimposed
1131 * over the bar. The shown text is either the value of
1132 * the #GtkProgressBar::text property or, if that is %NULL,
1133 * the #GtkProgressBar::fraction value, as a percentage.
1138 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1141 GtkProgressBarPrivate *priv;
1143 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1147 show_text = !!show_text;
1149 if (priv->show_text != show_text)
1151 priv->show_text = show_text;
1153 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1154 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1156 g_object_notify (G_OBJECT (pbar), "show-text");
1161 * gtk_progress_bar_get_show_text:
1162 * @pbar: a #GtkProgressBar
1164 * Gets the value of the #GtkProgressBar::show-text property.
1165 * See gtk_progress_bar_set_show_text().
1167 * Returns: %TRUE if text is shown in the progress bar
1172 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1174 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1176 return pbar->priv->show_text;
1180 * gtk_progress_bar_set_pulse_step:
1181 * @pbar: a #GtkProgressBar
1182 * @fraction: fraction between 0.0 and 1.0
1184 * Sets the fraction of total progress bar length to move the
1185 * bouncing block for each call to gtk_progress_bar_pulse().
1188 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1191 GtkProgressBarPrivate *priv;
1193 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1197 priv->pulse_fraction = fraction;
1199 g_object_notify (G_OBJECT (pbar), "pulse-step");
1203 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1204 GtkOrientation orientation)
1206 GtkProgressBarPrivate *priv = pbar->priv;
1208 if (priv->orientation != orientation)
1210 priv->orientation = orientation;
1211 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (pbar));
1213 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1214 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1219 * gtk_progress_bar_set_inverted:
1220 * @pbar: a #GtkProgressBar
1221 * @inverted: %TRUE to invert the progress bar
1223 * Progress bars normally grow from top to bottom or left to right.
1224 * Inverted progress bars grow in the opposite direction.
1227 gtk_progress_bar_set_inverted (GtkProgressBar *pbar,
1230 GtkProgressBarPrivate *priv;
1232 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1236 if (priv->inverted != inverted)
1238 priv->inverted = inverted;
1240 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1241 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1243 g_object_notify (G_OBJECT (pbar), "inverted");
1248 * gtk_progress_bar_get_text:
1249 * @pbar: a #GtkProgressBar
1251 * Retrieves the text displayed superimposed on the progress bar,
1252 * if any, otherwise %NULL. The return value is a reference
1253 * to the text, not a copy of it, so will become invalid
1254 * if you change the text in the progress bar.
1256 * Return value: text, or %NULL; this string is owned by the widget
1257 * and should not be modified or freed.
1259 G_CONST_RETURN gchar*
1260 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1262 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1264 return pbar->priv->text;
1268 * gtk_progress_bar_get_fraction:
1269 * @pbar: a #GtkProgressBar
1271 * Returns the current fraction of the task that's been completed.
1273 * Return value: a fraction from 0.0 to 1.0
1276 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1278 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1280 return pbar->priv->fraction;
1284 * gtk_progress_bar_get_pulse_step:
1285 * @pbar: a #GtkProgressBar
1287 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1289 * Return value: a fraction from 0.0 to 1.0
1292 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1294 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1296 return pbar->priv->pulse_fraction;
1300 * gtk_progress_bar_get_inverted:
1301 * @pbar: a #GtkProgressBar
1303 * Gets the value set by gtk_progress_bar_set_inverted()
1305 * Return value: %TRUE if the progress bar is inverted
1308 gtk_progress_bar_get_inverted (GtkProgressBar *pbar)
1310 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1312 return pbar->priv->inverted;
1316 * gtk_progress_bar_set_ellipsize:
1317 * @pbar: a #GtkProgressBar
1318 * @mode: a #PangoEllipsizeMode
1320 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1321 * if there is not enough space to render the entire string.
1326 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1327 PangoEllipsizeMode mode)
1329 GtkProgressBarPrivate *priv;
1331 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1332 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1333 mode <= PANGO_ELLIPSIZE_END);
1337 if ((PangoEllipsizeMode)priv->ellipsize != mode)
1339 priv->ellipsize = mode;
1341 g_object_notify (G_OBJECT (pbar), "ellipsize");
1342 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1347 * gtk_progress_bar_get_ellipsize:
1348 * @pbar: a #GtkProgressBar
1350 * Returns the ellipsizing position of the progressbar.
1351 * See gtk_progress_bar_set_ellipsize().
1353 * Return value: #PangoEllipsizeMode
1358 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1360 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1362 return pbar->priv->ellipsize;