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);
473 GtkProgressBarPriv *priv = pbar->priv;
475 if (gtk_widget_is_drawable (widget))
476 gtk_progress_bar_paint (pbar);
482 get_current_text (GtkProgressBar *pbar)
484 GtkProgressBarPriv *priv = pbar->priv;
487 return g_strdup (priv->text);
489 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
493 gtk_progress_bar_size_request (GtkWidget *widget,
494 GtkRequisition *requisition)
496 GtkProgressBar *pbar;
497 GtkProgressBarPriv *priv;
499 PangoRectangle logical_rect;
502 gint xspacing, yspacing;
503 gint min_width, min_height;
505 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
506 g_return_if_fail (requisition != NULL);
508 gtk_widget_style_get (widget,
509 "xspacing", &xspacing,
510 "yspacing", &yspacing,
513 pbar = GTK_PROGRESS_BAR (widget);
516 width = 2 * widget->style->xthickness + xspacing;
517 height = 2 * widget->style->ythickness + yspacing;
521 buf = get_current_text (pbar);
522 layout = gtk_widget_create_pango_layout (widget, buf);
524 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
528 PangoContext *context;
529 PangoFontMetrics *metrics;
532 /* The minimum size for ellipsized text is ~ 3 chars */
533 context = pango_layout_get_context (layout);
534 metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
536 char_width = pango_font_metrics_get_approximate_char_width (metrics);
537 pango_font_metrics_unref (metrics);
539 width += PANGO_PIXELS (char_width) * 3;
542 width += logical_rect.width;
544 height += logical_rect.height;
546 g_object_unref (layout);
550 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
551 priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
552 gtk_widget_style_get (widget,
553 "min-horizontal-bar-width", &min_width,
554 "min-horizontal-bar-height", &min_height,
557 gtk_widget_style_get (widget,
558 "min-vertical-bar-width", &min_width,
559 "min-vertical-bar-height", &min_height,
562 requisition->width = MAX (min_width, width);
563 requisition->height = MAX (min_height, height);
567 gtk_progress_bar_size_allocate (GtkWidget *widget,
568 GtkAllocation *allocation)
570 widget->allocation = *allocation;
572 if (gtk_widget_get_realized (widget))
574 gdk_window_move_resize (widget->window,
575 allocation->x, allocation->y,
576 allocation->width, allocation->height);
581 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
583 GtkProgressBarPriv *priv = pbar->priv;
584 GtkWidget *widget = GTK_WIDGET (pbar);
585 GtkProgressBarOrientation orientation;
587 orientation = priv->orientation;
588 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
590 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
591 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
592 else if (priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
593 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
596 /* calculate start pos */
598 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
599 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
601 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
603 priv->activity_pos = widget->style->xthickness;
604 priv->activity_dir = 0;
608 priv->activity_pos = widget->allocation.width -
609 widget->style->xthickness - (widget->allocation.height -
610 widget->style->ythickness * 2);
611 priv->activity_dir = 1;
616 if (orientation == GTK_PROGRESS_TOP_TO_BOTTOM)
618 priv->activity_pos = widget->style->ythickness;
619 priv->activity_dir = 0;
623 priv->activity_pos = widget->allocation.height -
624 widget->style->ythickness - (widget->allocation.width -
625 widget->style->xthickness * 2);
626 priv->activity_dir = 1;
632 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
633 GtkProgressBarOrientation orientation,
637 GtkProgressBarPriv *priv = pbar->priv;
638 GtkWidget *widget = GTK_WIDGET (pbar);
640 *offset = priv->activity_pos;
644 case GTK_PROGRESS_LEFT_TO_RIGHT:
645 case GTK_PROGRESS_RIGHT_TO_LEFT:
646 *amount = MAX (2, widget->allocation.width / priv->activity_blocks);
649 case GTK_PROGRESS_TOP_TO_BOTTOM:
650 case GTK_PROGRESS_BOTTOM_TO_TOP:
651 *amount = MAX (2, widget->allocation.height / priv->activity_blocks);
657 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
658 GtkProgressBarOrientation orientation)
660 GtkProgressBarPriv *priv = pbar->priv;
661 GtkWidget *widget = GTK_WIDGET (pbar);
666 case GTK_PROGRESS_LEFT_TO_RIGHT:
667 case GTK_PROGRESS_RIGHT_TO_LEFT:
668 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
669 area.y = widget->style->ythickness;
670 area.height = widget->allocation.height - 2 * widget->style->ythickness;
673 case GTK_PROGRESS_TOP_TO_BOTTOM:
674 case GTK_PROGRESS_BOTTOM_TO_TOP:
675 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
676 area.x = widget->style->xthickness;
677 area.width = widget->allocation.width - 2 * widget->style->xthickness;
685 gtk_paint_box (widget->style,
686 gtk_widget_get_window (widget),
687 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
688 &area, widget, "bar",
689 area.x, area.y, area.width, area.height);
693 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
695 GtkProgressBarOrientation orientation)
697 GtkProgressBarPriv *priv = pbar->priv;
698 GtkWidget *widget = GTK_WIDGET (pbar);
706 case GTK_PROGRESS_LEFT_TO_RIGHT:
707 case GTK_PROGRESS_RIGHT_TO_LEFT:
709 area.height = widget->allocation.height - widget->style->ythickness * 2;
710 area.y = widget->style->ythickness;
712 area.x = widget->style->xthickness;
713 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
714 area.x = widget->allocation.width - amount - area.x;
717 case GTK_PROGRESS_TOP_TO_BOTTOM:
718 case GTK_PROGRESS_BOTTOM_TO_TOP:
719 area.width = widget->allocation.width - widget->style->xthickness * 2;
720 area.height = amount;
721 area.x = widget->style->xthickness;
723 area.y = widget->style->ythickness;
724 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
725 area.y = widget->allocation.height - amount - area.y;
733 gtk_paint_box (widget->style,
734 gtk_widget_get_window (widget),
735 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
736 &area, widget, "bar",
737 area.x, area.y, area.width, area.height);
741 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
744 GtkProgressBarOrientation orientation)
746 GtkProgressBarPriv *priv = pbar->priv;
747 GtkWidget *widget = GTK_WIDGET (pbar);
753 PangoRectangle logical_rect;
754 GdkRectangle prelight_clip, start_clip, end_clip;
755 gfloat text_xalign = 0.5;
756 gfloat text_yalign = 0.5;
758 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
759 text_xalign = 1.0 - text_xalign;
761 buf = get_current_text (pbar);
763 layout = gtk_widget_create_pango_layout (widget, buf);
764 pango_layout_set_ellipsize (layout, priv->ellipsize);
766 pango_layout_set_width (layout, widget->allocation.width * PANGO_SCALE);
768 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
770 x = widget->style->xthickness + 1 + text_xalign *
771 (widget->allocation.width - 2 * widget->style->xthickness -
772 2 - logical_rect.width);
774 y = widget->style->ythickness + 1 + text_yalign *
775 (widget->allocation.height - 2 * widget->style->ythickness -
776 2 - logical_rect.height);
778 rect.x = widget->style->xthickness;
779 rect.y = widget->style->ythickness;
780 rect.width = widget->allocation.width - 2 * widget->style->xthickness;
781 rect.height = widget->allocation.height - 2 * widget->style->ythickness;
783 prelight_clip = start_clip = end_clip = rect;
787 case GTK_PROGRESS_LEFT_TO_RIGHT:
789 prelight_clip.x = offset;
790 prelight_clip.width = amount;
791 start_clip.width = prelight_clip.x - start_clip.x;
792 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
793 end_clip.width -= prelight_clip.width + start_clip.width;
796 case GTK_PROGRESS_RIGHT_TO_LEFT:
798 prelight_clip.x = offset;
800 prelight_clip.x = rect.x + rect.width - amount;
801 prelight_clip.width = amount;
802 start_clip.width = prelight_clip.x - start_clip.x;
803 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
804 end_clip.width -= prelight_clip.width + start_clip.width;
807 case GTK_PROGRESS_TOP_TO_BOTTOM:
809 prelight_clip.y = offset;
810 prelight_clip.height = amount;
811 start_clip.height = prelight_clip.y - start_clip.y;
812 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
813 end_clip.height -= prelight_clip.height + start_clip.height;
816 case GTK_PROGRESS_BOTTOM_TO_TOP:
818 prelight_clip.y = offset;
820 prelight_clip.y = rect.y + rect.height - amount;
821 prelight_clip.height = amount;
822 start_clip.height = prelight_clip.y - start_clip.y;
823 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
824 end_clip.height -= prelight_clip.height + start_clip.height;
828 if (start_clip.width > 0 && start_clip.height > 0)
829 gtk_paint_layout (widget->style,
830 gtk_widget_get_window (widget),
839 if (end_clip.width > 0 && end_clip.height > 0)
840 gtk_paint_layout (widget->style,
841 gtk_widget_get_window (widget),
850 gtk_paint_layout (widget->style,
851 gtk_widget_get_window (widget),
860 g_object_unref (layout);
865 gtk_progress_bar_paint (GtkProgressBar *pbar)
867 GtkProgressBarPriv *priv = pbar->priv;
868 GtkWidget *widget = GTK_WIDGET (pbar);
869 GtkProgressBarOrientation orientation;
871 orientation = priv->orientation;
872 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
874 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
875 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
876 else if (priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
877 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
880 gtk_paint_box (widget->style,
881 gtk_widget_get_window (widget),
882 GTK_STATE_NORMAL, GTK_SHADOW_IN,
883 NULL, widget, "trough",
885 widget->allocation.width,
886 widget->allocation.height);
888 if (priv->activity_mode)
890 gtk_progress_bar_paint_activity (pbar, orientation);
897 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
898 gtk_progress_bar_paint_text (pbar, offset, amount, orientation);
906 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
907 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
908 space = widget->allocation.width - 2 * widget->style->xthickness;
910 space = widget->allocation.height - 2 * widget->style->ythickness;
912 amount = space * gtk_progress_bar_get_fraction (pbar);
914 gtk_progress_bar_paint_continuous (pbar, amount, orientation);
917 gtk_progress_bar_paint_text (pbar, -1, amount, orientation);
922 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
923 gboolean activity_mode)
925 GtkProgressBarPriv *priv = pbar->priv;
927 activity_mode = !!activity_mode;
929 if (priv->activity_mode != activity_mode)
931 priv->activity_mode = activity_mode;
933 if (priv->activity_mode)
934 gtk_progress_bar_act_mode_enter (pbar);
936 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
937 gtk_widget_queue_resize (GTK_WIDGET (pbar));
942 * gtk_progress_bar_set_fraction:
943 * @pbar: a #GtkProgressBar
944 * @fraction: fraction of the task that's been completed
946 * Causes the progress bar to "fill in" the given fraction
947 * of the bar. The fraction should be between 0.0 and 1.0,
952 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
955 GtkProgressBarPriv* priv;
957 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
961 priv->fraction = fraction;
962 gtk_progress_bar_set_activity_mode (pbar, FALSE);
963 gtk_progress_bar_real_update (pbar);
965 g_object_notify (G_OBJECT (pbar), "fraction");
969 * gtk_progress_bar_pulse:
970 * @pbar: a #GtkProgressBar
972 * Indicates that some progress is made, but you don't know how much.
973 * Causes the progress bar to enter "activity mode," where a block
974 * bounces back and forth. Each call to gtk_progress_bar_pulse()
975 * causes the block to move by a little bit (the amount of movement
976 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
979 gtk_progress_bar_pulse (GtkProgressBar *pbar)
981 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
983 gtk_progress_bar_set_activity_mode (pbar, TRUE);
984 gtk_progress_bar_real_update (pbar);
988 * gtk_progress_bar_set_text:
989 * @pbar: a #GtkProgressBar
990 * @text: (allow-none): a UTF-8 string, or %NULL
992 * Causes the given @text to appear superimposed on the progress bar.
995 gtk_progress_bar_set_text (GtkProgressBar *pbar,
998 GtkProgressBarPriv *priv;
1000 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1004 g_free (priv->text);
1005 priv->text = text && *text ? g_strdup (text) : NULL;
1007 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1008 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1010 g_object_notify (G_OBJECT (pbar), "text");
1014 * gtk_progress_bar_set_show_text:
1015 * @pbar: a #GtkProgressBar
1016 * @show_text: whether to show superimposed text
1018 * Sets whether the progressbar will show text superimposed
1019 * over the bar. The shown text is either the value of
1020 * the #GtkProgressBar::text property or, if that is %NULL,
1021 * the #GtkProgressBar::fraction value, as a percentage.
1026 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1029 GtkProgressBarPriv *priv;
1031 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1035 show_text = !!show_text;
1037 if (priv->show_text != show_text)
1039 priv->show_text = show_text;
1041 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1042 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1044 g_object_notify (G_OBJECT (pbar), "show-text");
1049 * gtk_progress_bar_get_show_text:
1050 * @pbar: a #GtkProgressBar
1052 * Gets the value of the #GtkProgressBar::show-text property.
1053 * See gtk_progress_bar_set_show_text().
1055 * Returns: %TRUE if text is shown in the progress bar
1060 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1062 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1064 return pbar->priv->show_text;
1068 * gtk_progress_bar_set_pulse_step:
1069 * @pbar: a #GtkProgressBar
1070 * @fraction: fraction between 0.0 and 1.0
1072 * Sets the fraction of total progress bar length to move the
1073 * bouncing block for each call to gtk_progress_bar_pulse().
1076 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1079 GtkProgressBarPriv *priv;
1081 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1085 priv->pulse_fraction = fraction;
1087 g_object_notify (G_OBJECT (pbar), "pulse-step");
1091 * gtk_progress_bar_set_orientation:
1092 * @pbar: a #GtkProgressBar
1093 * @orientation: orientation of the progress bar
1095 * Causes the progress bar to switch to a different orientation
1096 * (left-to-right, right-to-left, top-to-bottom, or bottom-to-top).
1099 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1100 GtkProgressBarOrientation orientation)
1102 GtkProgressBarPriv *priv;
1104 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1108 if (priv->orientation != orientation)
1110 priv->orientation = orientation;
1112 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1113 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1115 g_object_notify (G_OBJECT (pbar), "orientation");
1120 * gtk_progress_bar_get_text:
1121 * @pbar: a #GtkProgressBar
1123 * Retrieves the text displayed superimposed on the progress bar,
1124 * if any, otherwise %NULL. The return value is a reference
1125 * to the text, not a copy of it, so will become invalid
1126 * if you change the text in the progress bar.
1128 * Return value: text, or %NULL; this string is owned by the widget
1129 * and should not be modified or freed.
1131 G_CONST_RETURN gchar*
1132 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1134 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1136 return pbar->priv->text;
1140 * gtk_progress_bar_get_fraction:
1141 * @pbar: a #GtkProgressBar
1143 * Returns the current fraction of the task that's been completed.
1145 * Return value: a fraction from 0.0 to 1.0
1148 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1150 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1152 return pbar->priv->fraction;
1156 * gtk_progress_bar_get_pulse_step:
1157 * @pbar: a #GtkProgressBar
1159 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1161 * Return value: a fraction from 0.0 to 1.0
1164 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1166 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1168 return pbar->priv->pulse_fraction;
1172 * gtk_progress_bar_get_orientation:
1173 * @pbar: a #GtkProgressBar
1175 * Retrieves the current progress bar orientation.
1177 * Return value: orientation of the progress bar
1179 GtkProgressBarOrientation
1180 gtk_progress_bar_get_orientation (GtkProgressBar *pbar)
1182 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1184 return pbar->priv->orientation;
1188 * gtk_progress_bar_set_ellipsize:
1189 * @pbar: a #GtkProgressBar
1190 * @mode: a #PangoEllipsizeMode
1192 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1193 * if there is not enough space to render the entire string.
1198 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1199 PangoEllipsizeMode mode)
1201 GtkProgressBarPriv *priv;
1203 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1204 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1205 mode <= PANGO_ELLIPSIZE_END);
1209 if ((PangoEllipsizeMode)priv->ellipsize != mode)
1211 priv->ellipsize = mode;
1213 g_object_notify (G_OBJECT (pbar), "ellipsize");
1214 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1219 * gtk_progress_bar_get_ellipsize:
1220 * @pbar: a #GtkProgressBar
1222 * Returns the ellipsizing position of the progressbar.
1223 * See gtk_progress_bar_set_ellipsize().
1225 * Return value: #PangoEllipsizeMode
1230 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1232 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1234 return pbar->priv->ellipsize;