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 _GtkProgressBarPriv
44 GtkProgressBarOrientation orientation;
49 gdouble pulse_fraction;
55 guint activity_blocks;
58 guint activity_dir : 1;
59 guint activity_mode : 1;
74 static void gtk_progress_bar_set_property (GObject *object,
78 static void gtk_progress_bar_get_property (GObject *object,
82 static gboolean gtk_progress_bar_expose (GtkWidget *widget,
83 GdkEventExpose *event);
84 static void gtk_progress_bar_size_request (GtkWidget *widget,
85 GtkRequisition *requisition);
86 static void gtk_progress_bar_size_allocate (GtkWidget *widget,
87 GtkAllocation *allocation);
88 static void gtk_progress_bar_real_update (GtkProgressBar *progress);
89 static void gtk_progress_bar_paint (GtkProgressBar *progress);
90 static void gtk_progress_bar_act_mode_enter (GtkProgressBar *progress);
91 static void gtk_progress_bar_realize (GtkWidget *widget);
92 static void gtk_progress_bar_finalize (GObject *object);
95 G_DEFINE_TYPE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_WIDGET)
98 gtk_progress_bar_class_init (GtkProgressBarClass *class)
100 GObjectClass *gobject_class;
101 GtkWidgetClass *widget_class;
103 gobject_class = G_OBJECT_CLASS (class);
104 widget_class = (GtkWidgetClass *) class;
106 gobject_class->set_property = gtk_progress_bar_set_property;
107 gobject_class->get_property = gtk_progress_bar_get_property;
108 gobject_class->finalize = gtk_progress_bar_finalize;
110 widget_class->realize = gtk_progress_bar_realize;
111 widget_class->expose_event = gtk_progress_bar_expose;
112 widget_class->size_request = gtk_progress_bar_size_request;
113 widget_class->size_allocate = gtk_progress_bar_size_allocate;
115 g_object_class_install_property (gobject_class,
117 g_param_spec_enum ("orientation",
119 P_("Orientation and growth direction of the progress bar"),
120 GTK_TYPE_PROGRESS_BAR_ORIENTATION,
121 GTK_PROGRESS_LEFT_TO_RIGHT,
122 GTK_PARAM_READWRITE));
124 g_object_class_install_property (gobject_class,
126 g_param_spec_double ("fraction",
128 P_("The fraction of total work that has been completed"),
130 GTK_PARAM_READWRITE));
132 g_object_class_install_property (gobject_class,
134 g_param_spec_double ("pulse-step",
136 P_("The fraction of total progress to move the bouncing block when pulsed"),
138 GTK_PARAM_READWRITE));
140 g_object_class_install_property (gobject_class,
142 g_param_spec_string ("text",
144 P_("Text to be displayed in the progress bar"),
146 GTK_PARAM_READWRITE));
148 g_object_class_install_property (gobject_class,
150 g_param_spec_boolean ("show-text",
152 P_("Whether the progress is shown as text."),
154 GTK_PARAM_READWRITE));
157 * GtkProgressBar:ellipsize:
159 * The preferred place to ellipsize the string, if the progressbar does
160 * not have enough room to display the entire string, specified as a
161 * #PangoEllisizeMode.
163 * Note that setting this property to a value other than
164 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
165 * only enough space to display the ellipsis "...". Another means to set a
166 * progressbar's width is gtk_widget_set_size_request().
170 g_object_class_install_property (gobject_class,
172 g_param_spec_enum ("ellipsize",
174 P_("The preferred place to ellipsize the string, if the progress bar "
175 "does not have enough room to display the entire string, if at all."),
176 PANGO_TYPE_ELLIPSIZE_MODE,
177 PANGO_ELLIPSIZE_NONE,
178 GTK_PARAM_READWRITE));
179 gtk_widget_class_install_style_property (widget_class,
180 g_param_spec_int ("xspacing",
182 P_("Extra spacing applied to the width of a progress bar."),
185 gtk_widget_class_install_style_property (widget_class,
186 g_param_spec_int ("yspacing",
188 P_("Extra spacing applied to the height of a progress bar."),
193 * GtkProgressBar:min-horizontal-bar-width:
195 * The minimum horizontal width of the progress bar.
199 gtk_widget_class_install_style_property (widget_class,
200 g_param_spec_int ("min-horizontal-bar-width",
201 P_("Min horizontal bar width"),
202 P_("The minimum horizontal width of the progress bar"),
203 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
206 * GtkProgressBar:min-horizontal-bar-height:
208 * Minimum horizontal height of the progress bar.
212 gtk_widget_class_install_style_property (widget_class,
213 g_param_spec_int ("min-horizontal-bar-height",
214 P_("Min horizontal bar height"),
215 P_("Minimum horizontal height of the progress bar"),
216 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
219 * GtkProgressBar:min-vertical-bar-width:
221 * The minimum vertical width of the progress bar.
225 gtk_widget_class_install_style_property (widget_class,
226 g_param_spec_int ("min-vertical-bar-width",
227 P_("Min vertical bar width"),
228 P_("The minimum vertical width of the progress bar"),
229 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
232 * GtkProgressBar:min-vertical-bar-height:
234 * The minimum vertical height of the progress bar.
238 gtk_widget_class_install_style_property (widget_class,
239 g_param_spec_int ("min-vertical-bar-height",
240 P_("Min vertical bar height"),
241 P_("The minimum vertical height of the progress bar"),
242 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
245 g_type_class_add_private (class, sizeof (GtkProgressBarPriv));
249 gtk_progress_bar_init (GtkProgressBar *pbar)
251 GtkProgressBarPriv *priv;
253 pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
254 GTK_TYPE_PROGRESS_BAR,
260 priv->orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
261 priv->pulse_fraction = 0.1;
262 priv->activity_pos = 0;
263 priv->activity_dir = 1;
264 priv->activity_step = 3;
265 priv->activity_blocks = 5;
266 priv->ellipsize = PANGO_ELLIPSIZE_NONE;
267 priv->show_text = FALSE;
270 priv->fraction = 0.0;
274 gtk_progress_bar_realize (GtkWidget *widget)
276 GdkWindowAttr attributes;
277 gint attributes_mask;
279 gtk_widget_set_realized (widget, TRUE);
281 attributes.window_type = GDK_WINDOW_CHILD;
282 attributes.x = widget->allocation.x;
283 attributes.y = widget->allocation.y;
284 attributes.width = widget->allocation.width;
285 attributes.height = widget->allocation.height;
286 attributes.wclass = GDK_INPUT_OUTPUT;
287 attributes.visual = gtk_widget_get_visual (widget);
288 attributes.colormap = gtk_widget_get_colormap (widget);
289 attributes.event_mask = gtk_widget_get_events (widget);
290 attributes.event_mask |= GDK_EXPOSURE_MASK;
292 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
293 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
294 &attributes, attributes_mask);
295 gdk_window_set_user_data (widget->window, widget);
297 widget->style = gtk_style_attach (widget->style, widget->window);
298 gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
302 gtk_progress_bar_set_property (GObject *object,
307 GtkProgressBar *pbar;
309 pbar = GTK_PROGRESS_BAR (object);
313 case PROP_ORIENTATION:
314 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
317 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
319 case PROP_PULSE_STEP:
320 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
323 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
326 gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
329 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
332 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
338 gtk_progress_bar_get_property (GObject *object,
343 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
344 GtkProgressBarPriv* priv = pbar->priv;
348 case PROP_ORIENTATION:
349 g_value_set_enum (value, priv->orientation);
352 g_value_set_double (value, priv->fraction);
354 case PROP_PULSE_STEP:
355 g_value_set_double (value, priv->pulse_fraction);
358 g_value_set_string (value, priv->text);
361 g_value_set_boolean (value, priv->show_text);
364 g_value_set_enum (value, priv->ellipsize);
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 gtk_progress_bar_new (void)
377 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
383 gtk_progress_bar_real_update (GtkProgressBar *pbar)
385 GtkProgressBarPriv *priv;
388 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
391 widget = GTK_WIDGET (pbar);
393 if (priv->activity_mode)
397 /* advance the block */
398 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
399 priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
401 /* Update our activity step. */
402 priv->activity_step = widget->allocation.width * priv->pulse_fraction;
404 size = MAX (2, widget->allocation.width / priv->activity_blocks);
406 if (priv->activity_dir == 0)
408 priv->activity_pos += priv->activity_step;
409 if (priv->activity_pos + size >= widget->allocation.width - widget->style->xthickness)
411 priv->activity_pos = widget->allocation.width -
412 widget->style->xthickness - size;
413 priv->activity_dir = 1;
418 priv->activity_pos -= priv->activity_step;
419 if (priv->activity_pos <= widget->style->xthickness)
421 priv->activity_pos = widget->style->xthickness;
422 priv->activity_dir = 0;
428 /* Update our activity step. */
429 priv->activity_step = widget->allocation.height * priv->pulse_fraction;
431 size = MAX (2, widget->allocation.height / priv->activity_blocks);
433 if (priv->activity_dir == 0)
435 priv->activity_pos += priv->activity_step;
436 if (priv->activity_pos + size >= widget->allocation.height - widget->style->ythickness)
438 priv->activity_pos = widget->allocation.height -
439 widget->style->ythickness - size;
440 priv->activity_dir = 1;
445 priv->activity_pos -= priv->activity_step;
446 if (priv->activity_pos <= widget->style->ythickness)
448 priv->activity_pos = widget->style->ythickness;
449 priv->activity_dir = 0;
454 gtk_widget_queue_draw (widget);
458 gtk_progress_bar_finalize (GObject *object)
460 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
461 GtkProgressBarPriv *priv = pbar->priv;
465 G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
469 gtk_progress_bar_expose (GtkWidget *widget,
470 GdkEventExpose *event)
472 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
474 if (gtk_widget_is_drawable (widget))
475 gtk_progress_bar_paint (pbar);
481 get_current_text (GtkProgressBar *pbar)
483 GtkProgressBarPriv *priv = pbar->priv;
486 return g_strdup (priv->text);
488 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
492 gtk_progress_bar_size_request (GtkWidget *widget,
493 GtkRequisition *requisition)
495 GtkProgressBar *pbar;
496 GtkProgressBarPriv *priv;
498 PangoRectangle logical_rect;
501 gint xspacing, yspacing;
502 gint min_width, min_height;
504 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
505 g_return_if_fail (requisition != NULL);
507 gtk_widget_style_get (widget,
508 "xspacing", &xspacing,
509 "yspacing", &yspacing,
512 pbar = GTK_PROGRESS_BAR (widget);
515 width = 2 * widget->style->xthickness + xspacing;
516 height = 2 * widget->style->ythickness + yspacing;
520 buf = get_current_text (pbar);
521 layout = gtk_widget_create_pango_layout (widget, buf);
523 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
527 PangoContext *context;
528 PangoFontMetrics *metrics;
531 /* The minimum size for ellipsized text is ~ 3 chars */
532 context = pango_layout_get_context (layout);
533 metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
535 char_width = pango_font_metrics_get_approximate_char_width (metrics);
536 pango_font_metrics_unref (metrics);
538 width += PANGO_PIXELS (char_width) * 3;
541 width += logical_rect.width;
543 height += logical_rect.height;
545 g_object_unref (layout);
549 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
550 priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
551 gtk_widget_style_get (widget,
552 "min-horizontal-bar-width", &min_width,
553 "min-horizontal-bar-height", &min_height,
556 gtk_widget_style_get (widget,
557 "min-vertical-bar-width", &min_width,
558 "min-vertical-bar-height", &min_height,
561 requisition->width = MAX (min_width, width);
562 requisition->height = MAX (min_height, height);
566 gtk_progress_bar_size_allocate (GtkWidget *widget,
567 GtkAllocation *allocation)
569 widget->allocation = *allocation;
571 if (gtk_widget_get_realized (widget))
573 gdk_window_move_resize (widget->window,
574 allocation->x, allocation->y,
575 allocation->width, allocation->height);
580 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
582 GtkProgressBarPriv *priv = pbar->priv;
583 GtkWidget *widget = GTK_WIDGET (pbar);
584 GtkProgressBarOrientation orientation;
586 orientation = priv->orientation;
587 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
589 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
590 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
591 else if (priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
592 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
595 /* calculate start pos */
597 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
598 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
600 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
602 priv->activity_pos = widget->style->xthickness;
603 priv->activity_dir = 0;
607 priv->activity_pos = widget->allocation.width -
608 widget->style->xthickness - (widget->allocation.height -
609 widget->style->ythickness * 2);
610 priv->activity_dir = 1;
615 if (orientation == GTK_PROGRESS_TOP_TO_BOTTOM)
617 priv->activity_pos = widget->style->ythickness;
618 priv->activity_dir = 0;
622 priv->activity_pos = widget->allocation.height -
623 widget->style->ythickness - (widget->allocation.width -
624 widget->style->xthickness * 2);
625 priv->activity_dir = 1;
631 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
632 GtkProgressBarOrientation orientation,
636 GtkProgressBarPriv *priv = pbar->priv;
637 GtkWidget *widget = GTK_WIDGET (pbar);
639 *offset = priv->activity_pos;
643 case GTK_PROGRESS_LEFT_TO_RIGHT:
644 case GTK_PROGRESS_RIGHT_TO_LEFT:
645 *amount = MAX (2, widget->allocation.width / priv->activity_blocks);
648 case GTK_PROGRESS_TOP_TO_BOTTOM:
649 case GTK_PROGRESS_BOTTOM_TO_TOP:
650 *amount = MAX (2, widget->allocation.height / priv->activity_blocks);
656 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
657 GtkProgressBarOrientation orientation)
659 GtkWidget *widget = GTK_WIDGET (pbar);
664 case GTK_PROGRESS_LEFT_TO_RIGHT:
665 case GTK_PROGRESS_RIGHT_TO_LEFT:
666 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
667 area.y = widget->style->ythickness;
668 area.height = widget->allocation.height - 2 * widget->style->ythickness;
671 case GTK_PROGRESS_TOP_TO_BOTTOM:
672 case GTK_PROGRESS_BOTTOM_TO_TOP:
673 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
674 area.x = widget->style->xthickness;
675 area.width = widget->allocation.width - 2 * widget->style->xthickness;
683 gtk_paint_box (widget->style,
684 gtk_widget_get_window (widget),
685 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
686 &area, widget, "bar",
687 area.x, area.y, area.width, area.height);
691 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
693 GtkProgressBarOrientation orientation)
695 GtkWidget *widget = GTK_WIDGET (pbar);
703 case GTK_PROGRESS_LEFT_TO_RIGHT:
704 case GTK_PROGRESS_RIGHT_TO_LEFT:
706 area.height = widget->allocation.height - widget->style->ythickness * 2;
707 area.y = widget->style->ythickness;
709 area.x = widget->style->xthickness;
710 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
711 area.x = widget->allocation.width - amount - area.x;
714 case GTK_PROGRESS_TOP_TO_BOTTOM:
715 case GTK_PROGRESS_BOTTOM_TO_TOP:
716 area.width = widget->allocation.width - widget->style->xthickness * 2;
717 area.height = amount;
718 area.x = widget->style->xthickness;
720 area.y = widget->style->ythickness;
721 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
722 area.y = widget->allocation.height - amount - area.y;
730 gtk_paint_box (widget->style,
731 gtk_widget_get_window (widget),
732 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
733 &area, widget, "bar",
734 area.x, area.y, area.width, area.height);
738 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
741 GtkProgressBarOrientation orientation)
743 GtkProgressBarPriv *priv = pbar->priv;
744 GtkWidget *widget = GTK_WIDGET (pbar);
750 PangoRectangle logical_rect;
751 GdkRectangle prelight_clip, start_clip, end_clip;
752 gfloat text_xalign = 0.5;
753 gfloat text_yalign = 0.5;
755 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
756 text_xalign = 1.0 - text_xalign;
758 buf = get_current_text (pbar);
760 layout = gtk_widget_create_pango_layout (widget, buf);
761 pango_layout_set_ellipsize (layout, priv->ellipsize);
763 pango_layout_set_width (layout, widget->allocation.width * PANGO_SCALE);
765 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
767 x = widget->style->xthickness + 1 + text_xalign *
768 (widget->allocation.width - 2 * widget->style->xthickness -
769 2 - logical_rect.width);
771 y = widget->style->ythickness + 1 + text_yalign *
772 (widget->allocation.height - 2 * widget->style->ythickness -
773 2 - logical_rect.height);
775 rect.x = widget->style->xthickness;
776 rect.y = widget->style->ythickness;
777 rect.width = widget->allocation.width - 2 * widget->style->xthickness;
778 rect.height = widget->allocation.height - 2 * widget->style->ythickness;
780 prelight_clip = start_clip = end_clip = rect;
784 case GTK_PROGRESS_LEFT_TO_RIGHT:
786 prelight_clip.x = offset;
787 prelight_clip.width = amount;
788 start_clip.width = prelight_clip.x - start_clip.x;
789 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
790 end_clip.width -= prelight_clip.width + start_clip.width;
793 case GTK_PROGRESS_RIGHT_TO_LEFT:
795 prelight_clip.x = offset;
797 prelight_clip.x = rect.x + rect.width - amount;
798 prelight_clip.width = amount;
799 start_clip.width = prelight_clip.x - start_clip.x;
800 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
801 end_clip.width -= prelight_clip.width + start_clip.width;
804 case GTK_PROGRESS_TOP_TO_BOTTOM:
806 prelight_clip.y = offset;
807 prelight_clip.height = amount;
808 start_clip.height = prelight_clip.y - start_clip.y;
809 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
810 end_clip.height -= prelight_clip.height + start_clip.height;
813 case GTK_PROGRESS_BOTTOM_TO_TOP:
815 prelight_clip.y = offset;
817 prelight_clip.y = rect.y + rect.height - amount;
818 prelight_clip.height = amount;
819 start_clip.height = prelight_clip.y - start_clip.y;
820 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
821 end_clip.height -= prelight_clip.height + start_clip.height;
825 if (start_clip.width > 0 && start_clip.height > 0)
826 gtk_paint_layout (widget->style,
827 gtk_widget_get_window (widget),
836 if (end_clip.width > 0 && end_clip.height > 0)
837 gtk_paint_layout (widget->style,
838 gtk_widget_get_window (widget),
847 gtk_paint_layout (widget->style,
848 gtk_widget_get_window (widget),
857 g_object_unref (layout);
862 gtk_progress_bar_paint (GtkProgressBar *pbar)
864 GtkProgressBarPriv *priv = pbar->priv;
865 GtkWidget *widget = GTK_WIDGET (pbar);
866 GtkProgressBarOrientation orientation;
868 orientation = priv->orientation;
869 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
871 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
872 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
873 else if (priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
874 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
877 gtk_paint_box (widget->style,
878 gtk_widget_get_window (widget),
879 GTK_STATE_NORMAL, GTK_SHADOW_IN,
880 NULL, widget, "trough",
882 widget->allocation.width,
883 widget->allocation.height);
885 if (priv->activity_mode)
887 gtk_progress_bar_paint_activity (pbar, orientation);
894 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
895 gtk_progress_bar_paint_text (pbar, offset, amount, orientation);
903 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
904 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
905 space = widget->allocation.width - 2 * widget->style->xthickness;
907 space = widget->allocation.height - 2 * widget->style->ythickness;
909 amount = space * gtk_progress_bar_get_fraction (pbar);
911 gtk_progress_bar_paint_continuous (pbar, amount, orientation);
914 gtk_progress_bar_paint_text (pbar, -1, amount, orientation);
919 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
920 gboolean activity_mode)
922 GtkProgressBarPriv *priv = pbar->priv;
924 activity_mode = !!activity_mode;
926 if (priv->activity_mode != activity_mode)
928 priv->activity_mode = activity_mode;
930 if (priv->activity_mode)
931 gtk_progress_bar_act_mode_enter (pbar);
933 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
934 gtk_widget_queue_resize (GTK_WIDGET (pbar));
939 * gtk_progress_bar_set_fraction:
940 * @pbar: a #GtkProgressBar
941 * @fraction: fraction of the task that's been completed
943 * Causes the progress bar to "fill in" the given fraction
944 * of the bar. The fraction should be between 0.0 and 1.0,
949 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
952 GtkProgressBarPriv* priv;
954 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
958 priv->fraction = fraction;
959 gtk_progress_bar_set_activity_mode (pbar, FALSE);
960 gtk_progress_bar_real_update (pbar);
962 g_object_notify (G_OBJECT (pbar), "fraction");
966 * gtk_progress_bar_pulse:
967 * @pbar: a #GtkProgressBar
969 * Indicates that some progress is made, but you don't know how much.
970 * Causes the progress bar to enter "activity mode," where a block
971 * bounces back and forth. Each call to gtk_progress_bar_pulse()
972 * causes the block to move by a little bit (the amount of movement
973 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
976 gtk_progress_bar_pulse (GtkProgressBar *pbar)
978 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
980 gtk_progress_bar_set_activity_mode (pbar, TRUE);
981 gtk_progress_bar_real_update (pbar);
985 * gtk_progress_bar_set_text:
986 * @pbar: a #GtkProgressBar
987 * @text: (allow-none): a UTF-8 string, or %NULL
989 * Causes the given @text to appear superimposed on the progress bar.
992 gtk_progress_bar_set_text (GtkProgressBar *pbar,
995 GtkProgressBarPriv *priv;
997 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1001 g_free (priv->text);
1002 priv->text = text && *text ? g_strdup (text) : NULL;
1004 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1005 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1007 g_object_notify (G_OBJECT (pbar), "text");
1011 * gtk_progress_bar_set_show_text:
1012 * @pbar: a #GtkProgressBar
1013 * @show_text: whether to show superimposed text
1015 * Sets whether the progressbar will show text superimposed
1016 * over the bar. The shown text is either the value of
1017 * the #GtkProgressBar::text property or, if that is %NULL,
1018 * the #GtkProgressBar::fraction value, as a percentage.
1023 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1026 GtkProgressBarPriv *priv;
1028 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1032 show_text = !!show_text;
1034 if (priv->show_text != show_text)
1036 priv->show_text = show_text;
1038 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1039 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1041 g_object_notify (G_OBJECT (pbar), "show-text");
1046 * gtk_progress_bar_get_show_text:
1047 * @pbar: a #GtkProgressBar
1049 * Gets the value of the #GtkProgressBar::show-text property.
1050 * See gtk_progress_bar_set_show_text().
1052 * Returns: %TRUE if text is shown in the progress bar
1057 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1059 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1061 return pbar->priv->show_text;
1065 * gtk_progress_bar_set_pulse_step:
1066 * @pbar: a #GtkProgressBar
1067 * @fraction: fraction between 0.0 and 1.0
1069 * Sets the fraction of total progress bar length to move the
1070 * bouncing block for each call to gtk_progress_bar_pulse().
1073 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1076 GtkProgressBarPriv *priv;
1078 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1082 priv->pulse_fraction = fraction;
1084 g_object_notify (G_OBJECT (pbar), "pulse-step");
1088 * gtk_progress_bar_set_orientation:
1089 * @pbar: a #GtkProgressBar
1090 * @orientation: orientation of the progress bar
1092 * Causes the progress bar to switch to a different orientation
1093 * (left-to-right, right-to-left, top-to-bottom, or bottom-to-top).
1096 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1097 GtkProgressBarOrientation orientation)
1099 GtkProgressBarPriv *priv;
1101 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1105 if (priv->orientation != orientation)
1107 priv->orientation = orientation;
1109 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1110 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1112 g_object_notify (G_OBJECT (pbar), "orientation");
1117 * gtk_progress_bar_get_text:
1118 * @pbar: a #GtkProgressBar
1120 * Retrieves the text displayed superimposed on the progress bar,
1121 * if any, otherwise %NULL. The return value is a reference
1122 * to the text, not a copy of it, so will become invalid
1123 * if you change the text in the progress bar.
1125 * Return value: text, or %NULL; this string is owned by the widget
1126 * and should not be modified or freed.
1128 G_CONST_RETURN gchar*
1129 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1131 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1133 return pbar->priv->text;
1137 * gtk_progress_bar_get_fraction:
1138 * @pbar: a #GtkProgressBar
1140 * Returns the current fraction of the task that's been completed.
1142 * Return value: a fraction from 0.0 to 1.0
1145 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1147 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1149 return pbar->priv->fraction;
1153 * gtk_progress_bar_get_pulse_step:
1154 * @pbar: a #GtkProgressBar
1156 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1158 * Return value: a fraction from 0.0 to 1.0
1161 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1163 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1165 return pbar->priv->pulse_fraction;
1169 * gtk_progress_bar_get_orientation:
1170 * @pbar: a #GtkProgressBar
1172 * Retrieves the current progress bar orientation.
1174 * Return value: orientation of the progress bar
1176 GtkProgressBarOrientation
1177 gtk_progress_bar_get_orientation (GtkProgressBar *pbar)
1179 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1181 return pbar->priv->orientation;
1185 * gtk_progress_bar_set_ellipsize:
1186 * @pbar: a #GtkProgressBar
1187 * @mode: a #PangoEllipsizeMode
1189 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1190 * if there is not enough space to render the entire string.
1195 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1196 PangoEllipsizeMode mode)
1198 GtkProgressBarPriv *priv;
1200 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1201 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1202 mode <= PANGO_ELLIPSIZE_END);
1206 if ((PangoEllipsizeMode)priv->ellipsize != mode)
1208 priv->ellipsize = mode;
1210 g_object_notify (G_OBJECT (pbar), "ellipsize");
1211 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1216 * gtk_progress_bar_get_ellipsize:
1217 * @pbar: a #GtkProgressBar
1219 * Returns the ellipsizing position of the progressbar.
1220 * See gtk_progress_bar_set_ellipsize().
1222 * Return value: #PangoEllipsizeMode
1227 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1229 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1231 return pbar->priv->ellipsize;