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 "gtkprivate.h"
36 #define MIN_HORIZONTAL_BAR_WIDTH 150
37 #define MIN_HORIZONTAL_BAR_HEIGHT 20
38 #define MIN_VERTICAL_BAR_WIDTH 22
39 #define MIN_VERTICAL_BAR_HEIGHT 80
42 struct _GtkProgressBarPrivate
44 GtkOrientation orientation;
49 gdouble pulse_fraction;
55 guint activity_blocks;
58 guint activity_dir : 1;
59 guint activity_mode : 1;
76 static void gtk_progress_bar_set_property (GObject *object,
80 static void gtk_progress_bar_get_property (GObject *object,
84 static gboolean gtk_progress_bar_expose (GtkWidget *widget,
85 GdkEventExpose *event);
86 static void gtk_progress_bar_size_request (GtkWidget *widget,
87 GtkRequisition *requisition);
88 static void gtk_progress_bar_size_allocate (GtkWidget *widget,
89 GtkAllocation *allocation);
90 static void gtk_progress_bar_real_update (GtkProgressBar *progress);
91 static void gtk_progress_bar_paint (GtkProgressBar *progress);
92 static void gtk_progress_bar_act_mode_enter (GtkProgressBar *progress);
93 static void gtk_progress_bar_realize (GtkWidget *widget);
94 static void gtk_progress_bar_finalize (GObject *object);
97 G_DEFINE_TYPE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_WIDGET)
100 gtk_progress_bar_class_init (GtkProgressBarClass *class)
102 GObjectClass *gobject_class;
103 GtkWidgetClass *widget_class;
105 gobject_class = G_OBJECT_CLASS (class);
106 widget_class = (GtkWidgetClass *) class;
108 gobject_class->set_property = gtk_progress_bar_set_property;
109 gobject_class->get_property = gtk_progress_bar_get_property;
110 gobject_class->finalize = gtk_progress_bar_finalize;
112 widget_class->realize = gtk_progress_bar_realize;
113 widget_class->expose_event = gtk_progress_bar_expose;
114 widget_class->size_request = gtk_progress_bar_size_request;
115 widget_class->size_allocate = gtk_progress_bar_size_allocate;
117 g_object_class_install_property (gobject_class,
119 g_param_spec_enum ("orientation",
121 P_("Orientation and of the progress bar"),
122 GTK_TYPE_ORIENTATION,
123 GTK_ORIENTATION_HORIZONTAL,
124 GTK_PARAM_READWRITE));
126 g_object_class_install_property (gobject_class,
128 g_param_spec_boolean ("inverted",
130 P_("Invert the direction in which the progress bar grows"),
132 GTK_PARAM_READWRITE));
134 g_object_class_install_property (gobject_class,
136 g_param_spec_double ("fraction",
138 P_("The fraction of total work that has been completed"),
140 GTK_PARAM_READWRITE));
142 g_object_class_install_property (gobject_class,
144 g_param_spec_double ("pulse-step",
146 P_("The fraction of total progress to move the bouncing block when pulsed"),
148 GTK_PARAM_READWRITE));
150 g_object_class_install_property (gobject_class,
152 g_param_spec_string ("text",
154 P_("Text to be displayed in the progress bar"),
156 GTK_PARAM_READWRITE));
158 g_object_class_install_property (gobject_class,
160 g_param_spec_boolean ("show-text",
162 P_("Whether the progress is shown as text."),
164 GTK_PARAM_READWRITE));
167 * GtkProgressBar:ellipsize:
169 * The preferred place to ellipsize the string, if the progressbar does
170 * not have enough room to display the entire string, specified as a
171 * #PangoEllisizeMode.
173 * Note that setting this property to a value other than
174 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
175 * only enough space to display the ellipsis "...". Another means to set a
176 * progressbar's width is gtk_widget_set_size_request().
180 g_object_class_install_property (gobject_class,
182 g_param_spec_enum ("ellipsize",
184 P_("The preferred place to ellipsize the string, if the progress bar "
185 "does not have enough room to display the entire string, if at all."),
186 PANGO_TYPE_ELLIPSIZE_MODE,
187 PANGO_ELLIPSIZE_NONE,
188 GTK_PARAM_READWRITE));
189 gtk_widget_class_install_style_property (widget_class,
190 g_param_spec_int ("xspacing",
192 P_("Extra spacing applied to the width of a progress bar."),
195 gtk_widget_class_install_style_property (widget_class,
196 g_param_spec_int ("yspacing",
198 P_("Extra spacing applied to the height of a progress bar."),
203 * GtkProgressBar:min-horizontal-bar-width:
205 * The minimum horizontal width of the progress bar.
209 gtk_widget_class_install_style_property (widget_class,
210 g_param_spec_int ("min-horizontal-bar-width",
211 P_("Minimum horizontal bar width"),
212 P_("The minimum horizontal width of the progress bar"),
213 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
216 * GtkProgressBar:min-horizontal-bar-height:
218 * Minimum horizontal height of the progress bar.
222 gtk_widget_class_install_style_property (widget_class,
223 g_param_spec_int ("min-horizontal-bar-height",
224 P_("Minimum horizontal bar height"),
225 P_("Minimum horizontal height of the progress bar"),
226 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
229 * GtkProgressBar:min-vertical-bar-width:
231 * The minimum vertical width of the progress bar.
235 gtk_widget_class_install_style_property (widget_class,
236 g_param_spec_int ("min-vertical-bar-width",
237 P_("Minimum vertical bar width"),
238 P_("The minimum vertical width of the progress bar"),
239 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
242 * GtkProgressBar:min-vertical-bar-height:
244 * The minimum vertical height of the progress bar.
248 gtk_widget_class_install_style_property (widget_class,
249 g_param_spec_int ("min-vertical-bar-height",
250 P_("Minimum vertical bar height"),
251 P_("The minimum vertical height of the progress bar"),
252 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
255 g_type_class_add_private (class, sizeof (GtkProgressBarPrivate));
259 gtk_progress_bar_init (GtkProgressBar *pbar)
261 GtkProgressBarPrivate *priv;
263 pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
264 GTK_TYPE_PROGRESS_BAR,
265 GtkProgressBarPrivate);
270 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
271 priv->inverted = FALSE;
272 priv->pulse_fraction = 0.1;
273 priv->activity_pos = 0;
274 priv->activity_dir = 1;
275 priv->activity_step = 3;
276 priv->activity_blocks = 5;
277 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
278 priv->show_text = FALSE;
281 priv->fraction = 0.0;
285 gtk_progress_bar_realize (GtkWidget *widget)
287 GtkAllocation allocation;
289 GdkWindowAttr attributes;
290 gint attributes_mask;
292 gtk_widget_set_realized (widget, TRUE);
294 gtk_widget_get_allocation (widget, &allocation);
296 attributes.window_type = GDK_WINDOW_CHILD;
297 attributes.x = allocation.x;
298 attributes.y = allocation.y;
299 attributes.width = allocation.width;
300 attributes.height = allocation.height;
301 attributes.wclass = GDK_INPUT_OUTPUT;
302 attributes.visual = gtk_widget_get_visual (widget);
303 attributes.colormap = gtk_widget_get_colormap (widget);
304 attributes.event_mask = gtk_widget_get_events (widget);
305 attributes.event_mask |= GDK_EXPOSURE_MASK;
307 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
308 window = gdk_window_new (gtk_widget_get_parent_window (widget),
309 &attributes, attributes_mask);
310 gtk_widget_set_window (widget, window);
311 gdk_window_set_user_data (window, widget);
313 gtk_widget_style_attach (widget);
314 gtk_style_set_background (gtk_widget_get_style (widget),
315 window, GTK_STATE_ACTIVE);
319 gtk_progress_bar_set_property (GObject *object,
324 GtkProgressBar *pbar;
326 pbar = GTK_PROGRESS_BAR (object);
330 case PROP_ORIENTATION:
331 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
334 gtk_progress_bar_set_inverted (pbar, g_value_get_boolean (value));
337 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
339 case PROP_PULSE_STEP:
340 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
343 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
346 gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
349 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
352 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
358 gtk_progress_bar_get_property (GObject *object,
363 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
364 GtkProgressBarPrivate* priv = pbar->priv;
368 case PROP_ORIENTATION:
369 g_value_set_enum (value, priv->orientation);
372 g_value_set_boolean (value, priv->inverted);
375 g_value_set_double (value, priv->fraction);
377 case PROP_PULSE_STEP:
378 g_value_set_double (value, priv->pulse_fraction);
381 g_value_set_string (value, priv->text);
384 g_value_set_boolean (value, priv->show_text);
387 g_value_set_enum (value, priv->ellipsize);
390 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
396 gtk_progress_bar_new (void)
400 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
406 gtk_progress_bar_real_update (GtkProgressBar *pbar)
408 GtkProgressBarPrivate *priv;
411 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
414 widget = GTK_WIDGET (pbar);
416 if (priv->activity_mode)
418 GtkAllocation allocation;
422 gtk_widget_get_allocation (widget, &allocation);
423 style = gtk_widget_get_style (widget);
425 /* advance the block */
426 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
428 /* Update our activity step. */
429 priv->activity_step = allocation.width * priv->pulse_fraction;
431 size = MAX (2, allocation.width / priv->activity_blocks);
433 if (priv->activity_dir == 0)
435 priv->activity_pos += priv->activity_step;
436 if (priv->activity_pos + size >= allocation.width - style->xthickness)
438 priv->activity_pos = allocation.width - style->xthickness - size;
439 priv->activity_dir = 1;
444 priv->activity_pos -= priv->activity_step;
445 if (priv->activity_pos <= style->xthickness)
447 priv->activity_pos = style->xthickness;
448 priv->activity_dir = 0;
454 /* Update our activity step. */
455 priv->activity_step = allocation.height * priv->pulse_fraction;
457 size = MAX (2, allocation.height / priv->activity_blocks);
459 if (priv->activity_dir == 0)
461 priv->activity_pos += priv->activity_step;
462 if (priv->activity_pos + size >= allocation.height - style->ythickness)
464 priv->activity_pos = allocation.height - style->ythickness - size;
465 priv->activity_dir = 1;
470 priv->activity_pos -= priv->activity_step;
471 if (priv->activity_pos <= style->ythickness)
473 priv->activity_pos = style->ythickness;
474 priv->activity_dir = 0;
479 gtk_widget_queue_draw (widget);
483 gtk_progress_bar_finalize (GObject *object)
485 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
486 GtkProgressBarPrivate *priv = pbar->priv;
490 G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
494 gtk_progress_bar_expose (GtkWidget *widget,
495 GdkEventExpose *event)
497 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
499 if (gtk_widget_is_drawable (widget))
500 gtk_progress_bar_paint (pbar);
506 get_current_text (GtkProgressBar *pbar)
508 GtkProgressBarPrivate *priv = pbar->priv;
511 return g_strdup (priv->text);
513 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
517 gtk_progress_bar_size_request (GtkWidget *widget,
518 GtkRequisition *requisition)
520 GtkProgressBar *pbar;
521 GtkProgressBarPrivate *priv;
524 PangoRectangle logical_rect;
527 gint xspacing, yspacing;
528 gint min_width, min_height;
530 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
531 g_return_if_fail (requisition != NULL);
533 style = gtk_widget_get_style (widget);
534 gtk_widget_style_get (widget,
535 "xspacing", &xspacing,
536 "yspacing", &yspacing,
539 pbar = GTK_PROGRESS_BAR (widget);
542 width = 2 * style->xthickness + xspacing;
543 height = 2 * style->ythickness + yspacing;
547 buf = get_current_text (pbar);
548 layout = gtk_widget_create_pango_layout (widget, buf);
550 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
554 PangoContext *context;
555 PangoFontMetrics *metrics;
558 /* The minimum size for ellipsized text is ~ 3 chars */
559 context = pango_layout_get_context (layout);
560 metrics = pango_context_get_metrics (context, style->font_desc, pango_context_get_language (context));
562 char_width = pango_font_metrics_get_approximate_char_width (metrics);
563 pango_font_metrics_unref (metrics);
565 width += PANGO_PIXELS (char_width) * 3;
568 width += logical_rect.width;
570 height += logical_rect.height;
572 g_object_unref (layout);
576 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
577 gtk_widget_style_get (widget,
578 "min-horizontal-bar-width", &min_width,
579 "min-horizontal-bar-height", &min_height,
582 gtk_widget_style_get (widget,
583 "min-vertical-bar-width", &min_width,
584 "min-vertical-bar-height", &min_height,
587 requisition->width = MAX (min_width, width);
588 requisition->height = MAX (min_height, height);
592 gtk_progress_bar_size_allocate (GtkWidget *widget,
593 GtkAllocation *allocation)
595 gtk_widget_set_allocation (widget, allocation);
597 if (gtk_widget_get_realized (widget))
598 gdk_window_move_resize (gtk_widget_get_window (widget),
599 allocation->x, allocation->y,
600 allocation->width, allocation->height);
604 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
606 GtkProgressBarPrivate *priv = pbar->priv;
607 GtkAllocation allocation;
609 GtkWidget *widget = GTK_WIDGET (pbar);
610 GtkOrientation orientation;
613 style = gtk_widget_get_style (widget);
615 orientation = priv->orientation;
616 inverted = priv->inverted;
617 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
619 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
620 inverted = !inverted;
623 /* calculate start pos */
625 if (orientation == GTK_ORIENTATION_HORIZONTAL)
629 priv->activity_pos = style->xthickness;
630 priv->activity_dir = 0;
634 gtk_widget_get_allocation (widget, &allocation);
635 priv->activity_pos = allocation.width - style->xthickness -
636 (allocation.height - style->ythickness * 2);
637 priv->activity_dir = 1;
644 priv->activity_pos = style->ythickness;
645 priv->activity_dir = 0;
649 gtk_widget_get_allocation (widget, &allocation);
650 priv->activity_pos = allocation.height - style->ythickness -
651 (allocation.width - style->xthickness * 2);
652 priv->activity_dir = 1;
658 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
659 GtkOrientation orientation,
663 GtkProgressBarPrivate *priv = pbar->priv;
664 GtkAllocation allocation;
665 GtkWidget *widget = GTK_WIDGET (pbar);
667 *offset = priv->activity_pos;
669 gtk_widget_get_allocation (widget, &allocation);
671 if (orientation == GTK_ORIENTATION_HORIZONTAL)
672 *amount = MAX (2, allocation.width / priv->activity_blocks);
674 *amount = MAX (2, allocation.height / priv->activity_blocks);
678 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
679 GtkOrientation orientation,
682 GtkAllocation allocation;
684 GtkWidget *widget = GTK_WIDGET (pbar);
687 style = gtk_widget_get_style (widget);
688 gtk_widget_get_allocation (widget, &allocation);
690 if (orientation == GTK_ORIENTATION_HORIZONTAL)
692 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
693 area.y = style->ythickness;
694 area.height = allocation.height - 2 * style->ythickness;
698 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
699 area.x = style->xthickness;
700 area.width = allocation.width - 2 * style->xthickness;
703 gtk_paint_box (style,
704 gtk_widget_get_window (widget),
705 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
706 &area, widget, "bar",
707 area.x, area.y, area.width, area.height);
711 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
713 GtkOrientation orientation,
716 GtkAllocation allocation;
718 GtkWidget *widget = GTK_WIDGET (pbar);
724 style = gtk_widget_get_style (widget);
725 gtk_widget_get_allocation (widget, &allocation);
727 if (orientation == GTK_ORIENTATION_HORIZONTAL)
730 area.height = allocation.height - style->ythickness * 2;
731 area.y = style->ythickness;
733 area.x = style->xthickness;
735 area.x = allocation.width - amount - area.x;
739 area.width = allocation.width - style->xthickness * 2;
740 area.height = amount;
741 area.x = style->xthickness;
743 area.y = style->ythickness;
745 area.y = allocation.height - amount - area.y;
748 gtk_paint_box (style,
749 gtk_widget_get_window (widget),
750 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
751 &area, widget, "bar",
752 area.x, area.y, area.width, area.height);
756 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
759 GtkOrientation orientation,
762 GtkProgressBarPrivate *priv = pbar->priv;
763 GtkAllocation allocation;
765 GtkWidget *widget = GTK_WIDGET (pbar);
771 PangoRectangle logical_rect;
772 GdkRectangle prelight_clip, start_clip, end_clip;
773 gfloat text_xalign = 0.5;
774 gfloat text_yalign = 0.5;
776 style = gtk_widget_get_style (widget);
777 gtk_widget_get_allocation (widget, &allocation);
779 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
780 text_xalign = 1.0 - text_xalign;
782 buf = get_current_text (pbar);
784 layout = gtk_widget_create_pango_layout (widget, buf);
785 pango_layout_set_ellipsize (layout, priv->ellipsize);
787 pango_layout_set_width (layout, allocation.width * PANGO_SCALE);
789 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
791 x = style->xthickness + 1 + text_xalign * (allocation.width - 2 * style->xthickness - 2 - logical_rect.width);
793 y = style->ythickness + 1 + text_yalign * (allocation.height - 2 * style->ythickness - 2 - logical_rect.height);
795 rect.x = style->xthickness;
796 rect.y = style->ythickness;
797 rect.width = allocation.width - 2 * style->xthickness;
798 rect.height = allocation.height - 2 * style->ythickness;
800 prelight_clip = start_clip = end_clip = rect;
802 if (orientation == GTK_ORIENTATION_HORIZONTAL)
807 prelight_clip.x = offset;
808 prelight_clip.width = amount;
809 start_clip.width = prelight_clip.x - start_clip.x;
810 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
811 end_clip.width -= prelight_clip.width + start_clip.width;
816 prelight_clip.x = offset;
818 prelight_clip.x = rect.x + rect.width - amount;
819 prelight_clip.width = amount;
820 start_clip.width = prelight_clip.x - start_clip.x;
821 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
822 end_clip.width -= prelight_clip.width + start_clip.width;
830 prelight_clip.y = offset;
831 prelight_clip.height = amount;
832 start_clip.height = prelight_clip.y - start_clip.y;
833 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
834 end_clip.height -= prelight_clip.height + start_clip.height;
839 prelight_clip.y = offset;
841 prelight_clip.y = rect.y + rect.height - amount;
842 prelight_clip.height = amount;
843 start_clip.height = prelight_clip.y - start_clip.y;
844 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
845 end_clip.height -= prelight_clip.height + start_clip.height;
849 if (start_clip.width > 0 && start_clip.height > 0)
850 gtk_paint_layout (style,
851 gtk_widget_get_window (widget),
860 if (end_clip.width > 0 && end_clip.height > 0)
861 gtk_paint_layout (style,
862 gtk_widget_get_window (widget),
871 gtk_paint_layout (style,
872 gtk_widget_get_window (widget),
881 g_object_unref (layout);
886 gtk_progress_bar_paint (GtkProgressBar *pbar)
888 GtkProgressBarPrivate *priv = pbar->priv;
889 GtkAllocation allocation;
890 GtkWidget *widget = GTK_WIDGET (pbar);
891 GtkOrientation orientation;
895 style = gtk_widget_get_style (widget);
896 gtk_widget_get_allocation (widget, &allocation);
898 orientation = priv->orientation;
899 inverted = priv->inverted;
900 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
902 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
903 inverted = !inverted;
906 gtk_paint_box (style,
907 gtk_widget_get_window (widget),
908 GTK_STATE_NORMAL, GTK_SHADOW_IN,
909 NULL, widget, "trough",
914 if (priv->activity_mode)
916 gtk_progress_bar_paint_activity (pbar, orientation, inverted);
923 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
924 gtk_progress_bar_paint_text (pbar, offset, amount, orientation, inverted);
932 if (orientation == GTK_ORIENTATION_HORIZONTAL)
933 space = allocation.width - 2 * style->xthickness;
935 space = allocation.height - 2 * style->ythickness;
937 amount = space * gtk_progress_bar_get_fraction (pbar);
939 gtk_progress_bar_paint_continuous (pbar, amount, orientation, inverted);
942 gtk_progress_bar_paint_text (pbar, -1, amount, orientation, inverted);
947 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
948 gboolean activity_mode)
950 GtkProgressBarPrivate *priv = pbar->priv;
952 activity_mode = !!activity_mode;
954 if (priv->activity_mode != activity_mode)
956 priv->activity_mode = activity_mode;
958 if (priv->activity_mode)
959 gtk_progress_bar_act_mode_enter (pbar);
961 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
962 gtk_widget_queue_resize (GTK_WIDGET (pbar));
967 * gtk_progress_bar_set_fraction:
968 * @pbar: a #GtkProgressBar
969 * @fraction: fraction of the task that's been completed
971 * Causes the progress bar to "fill in" the given fraction
972 * of the bar. The fraction should be between 0.0 and 1.0,
977 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
980 GtkProgressBarPrivate* priv;
982 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
986 priv->fraction = fraction;
987 gtk_progress_bar_set_activity_mode (pbar, FALSE);
988 gtk_progress_bar_real_update (pbar);
990 g_object_notify (G_OBJECT (pbar), "fraction");
994 * gtk_progress_bar_pulse:
995 * @pbar: a #GtkProgressBar
997 * Indicates that some progress is made, but you don't know how much.
998 * Causes the progress bar to enter "activity mode," where a block
999 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1000 * causes the block to move by a little bit (the amount of movement
1001 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1004 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1006 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1008 gtk_progress_bar_set_activity_mode (pbar, TRUE);
1009 gtk_progress_bar_real_update (pbar);
1013 * gtk_progress_bar_set_text:
1014 * @pbar: a #GtkProgressBar
1015 * @text: (allow-none): a UTF-8 string, or %NULL
1017 * Causes the given @text to appear superimposed on the progress bar.
1020 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1023 GtkProgressBarPrivate *priv;
1025 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1029 g_free (priv->text);
1030 priv->text = text && *text ? g_strdup (text) : NULL;
1032 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1033 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1035 g_object_notify (G_OBJECT (pbar), "text");
1039 * gtk_progress_bar_set_show_text:
1040 * @pbar: a #GtkProgressBar
1041 * @show_text: whether to show superimposed text
1043 * Sets whether the progressbar will show text superimposed
1044 * over the bar. The shown text is either the value of
1045 * the #GtkProgressBar::text property or, if that is %NULL,
1046 * the #GtkProgressBar::fraction value, as a percentage.
1051 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1054 GtkProgressBarPrivate *priv;
1056 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1060 show_text = !!show_text;
1062 if (priv->show_text != show_text)
1064 priv->show_text = show_text;
1066 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1067 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1069 g_object_notify (G_OBJECT (pbar), "show-text");
1074 * gtk_progress_bar_get_show_text:
1075 * @pbar: a #GtkProgressBar
1077 * Gets the value of the #GtkProgressBar::show-text property.
1078 * See gtk_progress_bar_set_show_text().
1080 * Returns: %TRUE if text is shown in the progress bar
1085 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1087 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1089 return pbar->priv->show_text;
1093 * gtk_progress_bar_set_pulse_step:
1094 * @pbar: a #GtkProgressBar
1095 * @fraction: fraction between 0.0 and 1.0
1097 * Sets the fraction of total progress bar length to move the
1098 * bouncing block for each call to gtk_progress_bar_pulse().
1101 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1104 GtkProgressBarPrivate *priv;
1106 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1110 priv->pulse_fraction = fraction;
1112 g_object_notify (G_OBJECT (pbar), "pulse-step");
1116 * gtk_progress_bar_set_orientation:
1117 * @pbar: a #GtkProgressBar
1118 * @orientation: orientation of the progress bar
1120 * Causes the progress bar to switch to a different orientation.
1123 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1124 GtkOrientation orientation)
1126 GtkProgressBarPrivate *priv;
1128 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1132 if (priv->orientation != orientation)
1134 priv->orientation = orientation;
1136 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1137 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1139 g_object_notify (G_OBJECT (pbar), "orientation");
1144 * gtk_progress_bar_set_inverted:
1145 * @pbar: a #GtkProgressBar
1146 * @inverted: %TRUE to invert the progress bar
1148 * Progress bars normally grow from top to bottom or left to right.
1149 * Inverted progress bars grow in the opposite direction.
1152 gtk_progress_bar_set_inverted (GtkProgressBar *pbar,
1155 GtkProgressBarPrivate *priv;
1157 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1161 if (priv->inverted != inverted)
1163 priv->inverted = inverted;
1165 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1166 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1168 g_object_notify (G_OBJECT (pbar), "inverted");
1173 * gtk_progress_bar_get_text:
1174 * @pbar: a #GtkProgressBar
1176 * Retrieves the text displayed superimposed on the progress bar,
1177 * if any, otherwise %NULL. The return value is a reference
1178 * to the text, not a copy of it, so will become invalid
1179 * if you change the text in the progress bar.
1181 * Return value: text, or %NULL; this string is owned by the widget
1182 * and should not be modified or freed.
1184 G_CONST_RETURN gchar*
1185 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1187 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1189 return pbar->priv->text;
1193 * gtk_progress_bar_get_fraction:
1194 * @pbar: a #GtkProgressBar
1196 * Returns the current fraction of the task that's been completed.
1198 * Return value: a fraction from 0.0 to 1.0
1201 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1203 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1205 return pbar->priv->fraction;
1209 * gtk_progress_bar_get_pulse_step:
1210 * @pbar: a #GtkProgressBar
1212 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1214 * Return value: a fraction from 0.0 to 1.0
1217 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1219 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1221 return pbar->priv->pulse_fraction;
1225 * gtk_progress_bar_get_orientation:
1226 * @pbar: a #GtkProgressBar
1228 * Retrieves the current progress bar orientation.
1230 * Return value: orientation of the progress bar
1233 gtk_progress_bar_get_orientation (GtkProgressBar *pbar)
1235 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), GTK_ORIENTATION_HORIZONTAL);
1237 return pbar->priv->orientation;
1241 * gtk_progress_bar_get_inverted:
1242 * @pbar: a #GtkProgressBar
1244 * Gets the value set by gtk_progress_bar_set_inverted()
1246 * Return value: %TRUE if the progress bar is inverted
1249 gtk_progress_bar_get_inverted (GtkProgressBar *pbar)
1251 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1253 return pbar->priv->inverted;
1257 * gtk_progress_bar_set_ellipsize:
1258 * @pbar: a #GtkProgressBar
1259 * @mode: a #PangoEllipsizeMode
1261 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1262 * if there is not enough space to render the entire string.
1267 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1268 PangoEllipsizeMode mode)
1270 GtkProgressBarPrivate *priv;
1272 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1273 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1274 mode <= PANGO_ELLIPSIZE_END);
1278 if ((PangoEllipsizeMode)priv->ellipsize != mode)
1280 priv->ellipsize = mode;
1282 g_object_notify (G_OBJECT (pbar), "ellipsize");
1283 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1288 * gtk_progress_bar_get_ellipsize:
1289 * @pbar: a #GtkProgressBar
1291 * Returns the ellipsizing position of the progressbar.
1292 * See gtk_progress_bar_set_ellipsize().
1294 * Return value: #PangoEllipsizeMode
1299 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1301 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1303 return pbar->priv->ellipsize;