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;
46 GdkPixmap *offscreen_pixmap;
51 gdouble pulse_fraction;
57 guint activity_blocks;
60 guint activity_dir : 1;
61 guint activity_mode : 1;
77 static void gtk_progress_bar_set_property (GObject *object,
81 static void gtk_progress_bar_get_property (GObject *object,
85 static gboolean gtk_progress_bar_expose (GtkWidget *widget,
86 GdkEventExpose *event);
87 static void gtk_progress_bar_size_request (GtkWidget *widget,
88 GtkRequisition *requisition);
89 static void gtk_progress_bar_size_allocate (GtkWidget *widget,
90 GtkAllocation *allocation);
91 static void gtk_progress_bar_style_set (GtkWidget *widget,
93 static void gtk_progress_bar_real_update (GtkProgressBar *progress);
94 static void gtk_progress_bar_paint (GtkProgressBar *progress);
95 static void gtk_progress_bar_act_mode_enter (GtkProgressBar *progress);
96 static void gtk_progress_bar_realize (GtkWidget *widget);
97 static void gtk_progress_bar_finalize (GObject *object);
98 static void gtk_progress_bar_create_pixmap (GtkProgressBar *pbar);
101 G_DEFINE_TYPE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_WIDGET)
104 gtk_progress_bar_class_init (GtkProgressBarClass *class)
106 GObjectClass *gobject_class;
107 GtkWidgetClass *widget_class;
109 gobject_class = G_OBJECT_CLASS (class);
110 widget_class = (GtkWidgetClass *) class;
112 gobject_class->set_property = gtk_progress_bar_set_property;
113 gobject_class->get_property = gtk_progress_bar_get_property;
114 gobject_class->finalize = gtk_progress_bar_finalize;
116 widget_class->realize = gtk_progress_bar_realize;
117 widget_class->expose_event = gtk_progress_bar_expose;
118 widget_class->size_request = gtk_progress_bar_size_request;
119 widget_class->size_allocate = gtk_progress_bar_size_allocate;
120 widget_class->style_set = gtk_progress_bar_style_set;
122 class->paint = gtk_progress_bar_paint;
123 class->update = gtk_progress_bar_real_update;
124 class->act_mode_enter = gtk_progress_bar_act_mode_enter;
126 g_object_class_install_property (gobject_class,
128 g_param_spec_enum ("orientation",
130 P_("Orientation and growth direction of the progress bar"),
131 GTK_TYPE_PROGRESS_BAR_ORIENTATION,
132 GTK_PROGRESS_LEFT_TO_RIGHT,
133 GTK_PARAM_READWRITE));
135 g_object_class_install_property (gobject_class,
137 g_param_spec_double ("fraction",
139 P_("The fraction of total work that has been completed"),
141 GTK_PARAM_READWRITE));
143 g_object_class_install_property (gobject_class,
145 g_param_spec_double ("pulse-step",
147 P_("The fraction of total progress to move the bouncing block when pulsed"),
149 GTK_PARAM_READWRITE));
151 g_object_class_install_property (gobject_class,
153 g_param_spec_string ("text",
155 P_("Text to be displayed in the progress bar"),
157 GTK_PARAM_READWRITE));
159 g_object_class_install_property (gobject_class,
161 g_param_spec_boolean ("show-text",
163 P_("Whether the progress is shown as text."),
165 GTK_PARAM_READWRITE));
168 * GtkProgressBar:ellipsize:
170 * The preferred place to ellipsize the string, if the progressbar does
171 * not have enough room to display the entire string, specified as a
172 * #PangoEllisizeMode.
174 * Note that setting this property to a value other than
175 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
176 * only enough space to display the ellipsis "...". Another means to set a
177 * progressbar's width is gtk_widget_set_size_request().
181 g_object_class_install_property (gobject_class,
183 g_param_spec_enum ("ellipsize",
185 P_("The preferred place to ellipsize the string, if the progress bar "
186 "does not have enough room to display the entire string, if at all."),
187 PANGO_TYPE_ELLIPSIZE_MODE,
188 PANGO_ELLIPSIZE_NONE,
189 GTK_PARAM_READWRITE));
190 gtk_widget_class_install_style_property (widget_class,
191 g_param_spec_int ("xspacing",
193 P_("Extra spacing applied to the width of a progress bar."),
196 gtk_widget_class_install_style_property (widget_class,
197 g_param_spec_int ("yspacing",
199 P_("Extra spacing applied to the height of a progress bar."),
204 * GtkProgressBar:min-horizontal-bar-width:
206 * The minimum horizontal width of the progress bar.
210 gtk_widget_class_install_style_property (widget_class,
211 g_param_spec_int ("min-horizontal-bar-width",
212 P_("Min horizontal bar width"),
213 P_("The minimum horizontal width of the progress bar"),
214 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
217 * GtkProgressBar:min-horizontal-bar-height:
219 * Minimum horizontal height of the progress bar.
223 gtk_widget_class_install_style_property (widget_class,
224 g_param_spec_int ("min-horizontal-bar-height",
225 P_("Min horizontal bar height"),
226 P_("Minimum horizontal height of the progress bar"),
227 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
230 * GtkProgressBar:min-vertical-bar-width:
232 * The minimum vertical width of the progress bar.
236 gtk_widget_class_install_style_property (widget_class,
237 g_param_spec_int ("min-vertical-bar-width",
238 P_("Min vertical bar width"),
239 P_("The minimum vertical width of the progress bar"),
240 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
243 * GtkProgressBar:min-vertical-bar-height:
245 * The minimum vertical height of the progress bar.
249 gtk_widget_class_install_style_property (widget_class,
250 g_param_spec_int ("min-vertical-bar-height",
251 P_("Min vertical bar height"),
252 P_("The minimum vertical height of the progress bar"),
253 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
256 g_type_class_add_private (class, sizeof (GtkProgressBarPriv));
260 gtk_progress_bar_init (GtkProgressBar *pbar)
262 GtkProgressBarPriv *priv;
264 pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
265 GTK_TYPE_PROGRESS_BAR,
271 priv->orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
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 GdkWindowAttr attributes;
288 gint attributes_mask;
290 gtk_widget_set_realized (widget, TRUE);
292 attributes.window_type = GDK_WINDOW_CHILD;
293 attributes.x = widget->allocation.x;
294 attributes.y = widget->allocation.y;
295 attributes.width = widget->allocation.width;
296 attributes.height = widget->allocation.height;
297 attributes.wclass = GDK_INPUT_OUTPUT;
298 attributes.visual = gtk_widget_get_visual (widget);
299 attributes.colormap = gtk_widget_get_colormap (widget);
300 attributes.event_mask = gtk_widget_get_events (widget);
301 attributes.event_mask |= GDK_EXPOSURE_MASK;
303 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
304 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
305 &attributes, attributes_mask);
306 gdk_window_set_user_data (widget->window, widget);
308 widget->style = gtk_style_attach (widget->style, widget->window);
309 gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
311 gtk_progress_bar_create_pixmap (GTK_PROGRESS_BAR (widget));
315 gtk_progress_bar_set_property (GObject *object,
320 GtkProgressBar *pbar;
322 pbar = GTK_PROGRESS_BAR (object);
326 case PROP_ORIENTATION:
327 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
330 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
332 case PROP_PULSE_STEP:
333 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
336 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
339 gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
342 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351 gtk_progress_bar_get_property (GObject *object,
356 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
357 GtkProgressBarPriv* priv = pbar->priv;
361 case PROP_ORIENTATION:
362 g_value_set_enum (value, priv->orientation);
365 g_value_set_double (value, priv->fraction);
367 case PROP_PULSE_STEP:
368 g_value_set_double (value, priv->pulse_fraction);
371 g_value_set_string (value, priv->text);
374 g_value_set_boolean (value, priv->show_text);
377 g_value_set_enum (value, priv->ellipsize);
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
386 gtk_progress_bar_new (void)
390 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
396 gtk_progress_bar_real_update (GtkProgressBar *pbar)
398 GtkProgressBarPriv *priv;
401 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
404 widget = GTK_WIDGET (pbar);
406 if (priv->activity_mode)
410 /* advance the block */
411 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
412 priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
414 /* Update our activity step. */
415 priv->activity_step = widget->allocation.width * priv->pulse_fraction;
417 size = MAX (2, widget->allocation.width / priv->activity_blocks);
419 if (priv->activity_dir == 0)
421 priv->activity_pos += priv->activity_step;
422 if (priv->activity_pos + size >= widget->allocation.width - widget->style->xthickness)
424 priv->activity_pos = widget->allocation.width -
425 widget->style->xthickness - size;
426 priv->activity_dir = 1;
431 priv->activity_pos -= priv->activity_step;
432 if (priv->activity_pos <= widget->style->xthickness)
434 priv->activity_pos = widget->style->xthickness;
435 priv->activity_dir = 0;
441 /* Update our activity step. */
442 priv->activity_step = widget->allocation.height * priv->pulse_fraction;
444 size = MAX (2, widget->allocation.height / priv->activity_blocks);
446 if (priv->activity_dir == 0)
448 priv->activity_pos += priv->activity_step;
449 if (priv->activity_pos + size >= widget->allocation.height - widget->style->ythickness)
451 priv->activity_pos = widget->allocation.height -
452 widget->style->ythickness - size;
453 priv->activity_dir = 1;
458 priv->activity_pos -= priv->activity_step;
459 if (priv->activity_pos <= widget->style->ythickness)
461 priv->activity_pos = widget->style->ythickness;
462 priv->activity_dir = 0;
468 gtk_widget_queue_draw (widget);
472 gtk_progress_bar_finalize (GObject *object)
474 GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
475 GtkProgressBarPriv *priv = pbar->priv;
477 if (priv->offscreen_pixmap)
478 g_object_unref (priv->offscreen_pixmap);
482 G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
486 gtk_progress_bar_expose (GtkWidget *widget,
487 GdkEventExpose *event)
489 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
490 GtkProgressBarPriv *priv = pbar->priv;
492 if (gtk_widget_is_drawable (widget))
495 gtk_progress_bar_paint (pbar);
497 gdk_draw_drawable (widget->window,
498 widget->style->black_gc,
499 priv->offscreen_pixmap,
500 event->area.x, event->area.y,
501 event->area.x, event->area.y,
510 get_current_text (GtkProgressBar *pbar)
512 GtkProgressBarPriv *priv = pbar->priv;
515 return g_strdup (priv->text);
517 return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
521 gtk_progress_bar_size_request (GtkWidget *widget,
522 GtkRequisition *requisition)
524 GtkProgressBar *pbar;
525 GtkProgressBarPriv *priv;
527 PangoRectangle logical_rect;
530 gint xspacing, yspacing;
531 gint min_width, min_height;
533 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
534 g_return_if_fail (requisition != NULL);
536 gtk_widget_style_get (widget,
537 "xspacing", &xspacing,
538 "yspacing", &yspacing,
541 pbar = GTK_PROGRESS_BAR (widget);
544 width = 2 * widget->style->xthickness + xspacing;
545 height = 2 * widget->style->ythickness + yspacing;
549 buf = get_current_text (pbar);
550 layout = gtk_widget_create_pango_layout (widget, buf);
552 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
556 PangoContext *context;
557 PangoFontMetrics *metrics;
560 /* The minimum size for ellipsized text is ~ 3 chars */
561 context = pango_layout_get_context (layout);
562 metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
564 char_width = pango_font_metrics_get_approximate_char_width (metrics);
565 pango_font_metrics_unref (metrics);
567 width += PANGO_PIXELS (char_width) * 3;
570 width += logical_rect.width;
572 height += logical_rect.height;
574 g_object_unref (layout);
578 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
579 priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
580 gtk_widget_style_get (widget,
581 "min-horizontal-bar-width", &min_width,
582 "min-horizontal-bar-height", &min_height,
585 gtk_widget_style_get (widget,
586 "min-vertical-bar-width", &min_width,
587 "min-vertical-bar-height", &min_height,
590 requisition->width = MAX (min_width, width);
591 requisition->height = MAX (min_height, height);
595 gtk_progress_bar_size_allocate (GtkWidget *widget,
596 GtkAllocation *allocation)
598 widget->allocation = *allocation;
600 if (gtk_widget_get_realized (widget))
602 gdk_window_move_resize (widget->window,
603 allocation->x, allocation->y,
604 allocation->width, allocation->height);
606 gtk_progress_bar_create_pixmap (GTK_PROGRESS_BAR (widget));
611 gtk_progress_bar_create_pixmap (GtkProgressBar *pbar)
613 GtkProgressBarPriv *priv = pbar->priv;
614 GtkWidget *widget = GTK_WIDGET (pbar);
616 if (gtk_widget_get_realized (widget))
618 if (priv->offscreen_pixmap)
619 g_object_unref (priv->offscreen_pixmap);
621 priv->offscreen_pixmap = gdk_pixmap_new (widget->window,
622 widget->allocation.width,
623 widget->allocation.height,
626 /* clear the pixmap for transparent themes */
627 gtk_paint_flat_box (widget->style,
628 priv->offscreen_pixmap,
631 NULL, widget, "trough", 0, 0, -1, -1);
633 GTK_PROGRESS_BAR_GET_CLASS (pbar)->paint (pbar);
638 gtk_progress_bar_style_set (GtkWidget *widget,
641 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
642 GtkProgressBarPriv *priv = pbar->priv;
646 GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->style_set (widget, previous);
650 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
652 GtkProgressBarPriv *priv = pbar->priv;
653 GtkWidget *widget = GTK_WIDGET (pbar);
654 GtkProgressBarOrientation orientation;
656 orientation = priv->orientation;
657 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
659 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
660 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
661 else if (priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
662 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
665 /* calculate start pos */
667 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
668 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
670 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
672 priv->activity_pos = widget->style->xthickness;
673 priv->activity_dir = 0;
677 priv->activity_pos = widget->allocation.width -
678 widget->style->xthickness - (widget->allocation.height -
679 widget->style->ythickness * 2);
680 priv->activity_dir = 1;
685 if (orientation == GTK_PROGRESS_TOP_TO_BOTTOM)
687 priv->activity_pos = widget->style->ythickness;
688 priv->activity_dir = 0;
692 priv->activity_pos = widget->allocation.height -
693 widget->style->ythickness - (widget->allocation.width -
694 widget->style->xthickness * 2);
695 priv->activity_dir = 1;
701 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
702 GtkProgressBarOrientation orientation,
706 GtkProgressBarPriv *priv = pbar->priv;
707 GtkWidget *widget = GTK_WIDGET (pbar);
709 *offset = priv->activity_pos;
713 case GTK_PROGRESS_LEFT_TO_RIGHT:
714 case GTK_PROGRESS_RIGHT_TO_LEFT:
715 *amount = MAX (2, widget->allocation.width / priv->activity_blocks);
718 case GTK_PROGRESS_TOP_TO_BOTTOM:
719 case GTK_PROGRESS_BOTTOM_TO_TOP:
720 *amount = MAX (2, widget->allocation.height / priv->activity_blocks);
726 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
727 GtkProgressBarOrientation orientation)
729 GtkProgressBarPriv *priv = pbar->priv;
730 GtkWidget *widget = GTK_WIDGET (pbar);
735 case GTK_PROGRESS_LEFT_TO_RIGHT:
736 case GTK_PROGRESS_RIGHT_TO_LEFT:
737 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
738 area.y = widget->style->ythickness;
739 area.height = widget->allocation.height - 2 * widget->style->ythickness;
742 case GTK_PROGRESS_TOP_TO_BOTTOM:
743 case GTK_PROGRESS_BOTTOM_TO_TOP:
744 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
745 area.x = widget->style->xthickness;
746 area.width = widget->allocation.width - 2 * widget->style->xthickness;
754 gtk_paint_box (widget->style,
755 priv->offscreen_pixmap,
756 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
757 &area, widget, "bar",
758 area.x, area.y, area.width, area.height);
762 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
764 GtkProgressBarOrientation orientation)
766 GtkProgressBarPriv *priv = pbar->priv;
767 GtkWidget *widget = GTK_WIDGET (pbar);
775 case GTK_PROGRESS_LEFT_TO_RIGHT:
776 case GTK_PROGRESS_RIGHT_TO_LEFT:
778 area.height = widget->allocation.height - widget->style->ythickness * 2;
779 area.y = widget->style->ythickness;
781 area.x = widget->style->xthickness;
782 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
783 area.x = widget->allocation.width - amount - area.x;
786 case GTK_PROGRESS_TOP_TO_BOTTOM:
787 case GTK_PROGRESS_BOTTOM_TO_TOP:
788 area.width = widget->allocation.width - widget->style->xthickness * 2;
789 area.height = amount;
790 area.x = widget->style->xthickness;
792 area.y = widget->style->ythickness;
793 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
794 area.y = widget->allocation.height - amount - area.y;
802 gtk_paint_box (widget->style,
803 priv->offscreen_pixmap,
804 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
805 &area, widget, "bar",
806 area.x, area.y, area.width, area.height);
810 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
813 GtkProgressBarOrientation orientation)
815 GtkProgressBarPriv *priv = pbar->priv;
816 GtkWidget *widget = GTK_WIDGET (pbar);
822 PangoRectangle logical_rect;
823 GdkRectangle prelight_clip, start_clip, end_clip;
824 gfloat text_xalign = 0.5;
825 gfloat text_yalign = 0.5;
827 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
828 text_xalign = 1.0 - text_xalign;
830 buf = get_current_text (pbar);
832 layout = gtk_widget_create_pango_layout (widget, buf);
833 pango_layout_set_ellipsize (layout, priv->ellipsize);
835 pango_layout_set_width (layout, widget->allocation.width * PANGO_SCALE);
837 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
839 x = widget->style->xthickness + 1 + text_xalign *
840 (widget->allocation.width - 2 * widget->style->xthickness -
841 2 - logical_rect.width);
843 y = widget->style->ythickness + 1 + text_yalign *
844 (widget->allocation.height - 2 * widget->style->ythickness -
845 2 - logical_rect.height);
847 rect.x = widget->style->xthickness;
848 rect.y = widget->style->ythickness;
849 rect.width = widget->allocation.width - 2 * widget->style->xthickness;
850 rect.height = widget->allocation.height - 2 * widget->style->ythickness;
852 prelight_clip = start_clip = end_clip = rect;
856 case GTK_PROGRESS_LEFT_TO_RIGHT:
858 prelight_clip.x = offset;
859 prelight_clip.width = amount;
860 start_clip.width = prelight_clip.x - start_clip.x;
861 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
862 end_clip.width -= prelight_clip.width + start_clip.width;
865 case GTK_PROGRESS_RIGHT_TO_LEFT:
867 prelight_clip.x = offset;
869 prelight_clip.x = rect.x + rect.width - amount;
870 prelight_clip.width = amount;
871 start_clip.width = prelight_clip.x - start_clip.x;
872 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
873 end_clip.width -= prelight_clip.width + start_clip.width;
876 case GTK_PROGRESS_TOP_TO_BOTTOM:
878 prelight_clip.y = offset;
879 prelight_clip.height = amount;
880 start_clip.height = prelight_clip.y - start_clip.y;
881 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
882 end_clip.height -= prelight_clip.height + start_clip.height;
885 case GTK_PROGRESS_BOTTOM_TO_TOP:
887 prelight_clip.y = offset;
889 prelight_clip.y = rect.y + rect.height - amount;
890 prelight_clip.height = amount;
891 start_clip.height = prelight_clip.y - start_clip.y;
892 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
893 end_clip.height -= prelight_clip.height + start_clip.height;
897 if (start_clip.width > 0 && start_clip.height > 0)
898 gtk_paint_layout (widget->style,
899 priv->offscreen_pixmap,
908 if (end_clip.width > 0 && end_clip.height > 0)
909 gtk_paint_layout (widget->style,
910 priv->offscreen_pixmap,
919 gtk_paint_layout (widget->style,
920 priv->offscreen_pixmap,
929 g_object_unref (layout);
934 gtk_progress_bar_paint (GtkProgressBar *pbar)
936 GtkProgressBarPriv *priv = pbar->priv;
937 GtkWidget *widget = GTK_WIDGET (pbar);
938 GtkProgressBarOrientation orientation;
940 orientation = priv->orientation;
941 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
943 if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
944 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
945 else if (priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
946 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
949 if (priv->offscreen_pixmap)
951 gtk_paint_box (widget->style,
952 priv->offscreen_pixmap,
953 GTK_STATE_NORMAL, GTK_SHADOW_IN,
954 NULL, widget, "trough",
956 widget->allocation.width,
957 widget->allocation.height);
959 if (priv->activity_mode)
961 gtk_progress_bar_paint_activity (pbar, orientation);
968 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
969 gtk_progress_bar_paint_text (pbar, offset, amount, orientation);
977 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
978 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
979 space = widget->allocation.width - 2 * widget->style->xthickness;
981 space = widget->allocation.height - 2 * widget->style->ythickness;
983 amount = space * gtk_progress_bar_get_fraction (pbar);
985 gtk_progress_bar_paint_continuous (pbar, amount, orientation);
988 gtk_progress_bar_paint_text (pbar, -1, amount, orientation);
996 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
997 gboolean activity_mode)
999 GtkProgressBarPriv *priv = pbar->priv;
1001 activity_mode = !!activity_mode;
1003 if (priv->activity_mode != activity_mode)
1005 priv->activity_mode = activity_mode;
1007 if (priv->activity_mode)
1008 GTK_PROGRESS_BAR_GET_CLASS (pbar)->act_mode_enter (pbar);
1010 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1011 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1016 * gtk_progress_bar_set_fraction:
1017 * @pbar: a #GtkProgressBar
1018 * @fraction: fraction of the task that's been completed
1020 * Causes the progress bar to "fill in" the given fraction
1021 * of the bar. The fraction should be between 0.0 and 1.0,
1026 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1029 GtkProgressBarPriv* priv;
1031 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1035 priv->fraction = fraction;
1036 gtk_progress_bar_set_activity_mode (pbar, FALSE);
1037 gtk_progress_bar_real_update (pbar);
1039 g_object_notify (G_OBJECT (pbar), "fraction");
1043 * gtk_progress_bar_pulse:
1044 * @pbar: a #GtkProgressBar
1046 * Indicates that some progress is made, but you don't know how much.
1047 * Causes the progress bar to enter "activity mode," where a block
1048 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1049 * causes the block to move by a little bit (the amount of movement
1050 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1053 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1055 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1057 gtk_progress_bar_set_activity_mode (pbar, TRUE);
1058 gtk_progress_bar_real_update (pbar);
1062 * gtk_progress_bar_set_text:
1063 * @pbar: a #GtkProgressBar
1064 * @text: (allow-none): a UTF-8 string, or %NULL
1066 * Causes the given @text to appear superimposed on the progress bar.
1069 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1072 GtkProgressBarPriv *priv;
1074 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1078 g_free (priv->text);
1079 priv->text = text && *text ? g_strdup (text) : NULL;
1081 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1082 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1084 g_object_notify (G_OBJECT (pbar), "text");
1088 * gtk_progress_bar_set_show_text:
1089 * @pbar: a #GtkProgressBar
1090 * @show_text: whether to show superimposed text
1092 * Sets whether the progressbar will show text superimposed
1093 * over the bar. The shown text is either the value of
1094 * the #GtkProgressBar::text property or, if that is %NULL,
1095 * the #GtkProgressBar::fraction value, as a percentage.
1100 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1103 GtkProgressBarPriv *priv;
1105 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1109 show_text = !!show_text;
1111 if (priv->show_text != show_text)
1113 priv->show_text = show_text;
1115 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1116 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1118 g_object_notify (G_OBJECT (pbar), "show-text");
1123 * gtk_progress_bar_get_show_text:
1124 * @pbar: a #GtkProgressBar
1126 * Gets the value of the #GtkProgressBar::show-text property.
1127 * See gtk_progress_bar_set_show_text().
1129 * Returns: %TRUE if text is shown in the progress bar
1134 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1136 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1138 return pbar->priv->show_text;
1142 * gtk_progress_bar_set_pulse_step:
1143 * @pbar: a #GtkProgressBar
1144 * @fraction: fraction between 0.0 and 1.0
1146 * Sets the fraction of total progress bar length to move the
1147 * bouncing block for each call to gtk_progress_bar_pulse().
1150 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1153 GtkProgressBarPriv *priv;
1155 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1159 priv->pulse_fraction = fraction;
1161 g_object_notify (G_OBJECT (pbar), "pulse-step");
1165 * gtk_progress_bar_set_orientation:
1166 * @pbar: a #GtkProgressBar
1167 * @orientation: orientation of the progress bar
1169 * Causes the progress bar to switch to a different orientation
1170 * (left-to-right, right-to-left, top-to-bottom, or bottom-to-top).
1173 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1174 GtkProgressBarOrientation orientation)
1176 GtkProgressBarPriv *priv;
1178 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1182 if (priv->orientation != orientation)
1184 priv->orientation = orientation;
1186 if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1187 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1189 g_object_notify (G_OBJECT (pbar), "orientation");
1194 * gtk_progress_bar_get_text:
1195 * @pbar: a #GtkProgressBar
1197 * Retrieves the text displayed superimposed on the progress bar,
1198 * if any, otherwise %NULL. The return value is a reference
1199 * to the text, not a copy of it, so will become invalid
1200 * if you change the text in the progress bar.
1202 * Return value: text, or %NULL; this string is owned by the widget
1203 * and should not be modified or freed.
1205 G_CONST_RETURN gchar*
1206 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1208 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1210 return pbar->priv->text;
1214 * gtk_progress_bar_get_fraction:
1215 * @pbar: a #GtkProgressBar
1217 * Returns the current fraction of the task that's been completed.
1219 * Return value: a fraction from 0.0 to 1.0
1222 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1224 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1226 return pbar->priv->fraction;
1230 * gtk_progress_bar_get_pulse_step:
1231 * @pbar: a #GtkProgressBar
1233 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1235 * Return value: a fraction from 0.0 to 1.0
1238 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1240 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1242 return pbar->priv->pulse_fraction;
1246 * gtk_progress_bar_get_orientation:
1247 * @pbar: a #GtkProgressBar
1249 * Retrieves the current progress bar orientation.
1251 * Return value: orientation of the progress bar
1253 GtkProgressBarOrientation
1254 gtk_progress_bar_get_orientation (GtkProgressBar *pbar)
1256 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1258 return pbar->priv->orientation;
1262 * gtk_progress_bar_set_ellipsize:
1263 * @pbar: a #GtkProgressBar
1264 * @mode: a #PangoEllipsizeMode
1266 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1267 * if there is not enough space to render the entire string.
1272 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1273 PangoEllipsizeMode mode)
1275 GtkProgressBarPriv *priv;
1277 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1278 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1279 mode <= PANGO_ELLIPSIZE_END);
1283 if ((PangoEllipsizeMode)priv->ellipsize != mode)
1285 priv->ellipsize = mode;
1287 g_object_notify (G_OBJECT (pbar), "ellipsize");
1288 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1293 * gtk_progress_bar_get_ellipsize:
1294 * @pbar: a #GtkProgressBar
1296 * Returns the ellipsizing position of the progressbar.
1297 * See gtk_progress_bar_set_ellipsize().
1299 * Return value: #PangoEllipsizeMode
1304 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1306 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1308 return pbar->priv->ellipsize;