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"
37 #define MIN_HORIZONTAL_BAR_WIDTH 150
38 #define MIN_HORIZONTAL_BAR_HEIGHT 20
39 #define MIN_VERTICAL_BAR_WIDTH 22
40 #define MIN_VERTICAL_BAR_HEIGHT 80
60 static void gtk_progress_bar_set_property (GObject *object,
64 static void gtk_progress_bar_get_property (GObject *object,
68 static gboolean gtk_progress_bar_expose (GtkWidget *widget,
69 GdkEventExpose *event);
70 static void gtk_progress_bar_size_request (GtkWidget *widget,
71 GtkRequisition *requisition);
72 static void gtk_progress_bar_style_set (GtkWidget *widget,
74 static void gtk_progress_bar_real_update (GtkProgress *progress);
75 static void gtk_progress_bar_paint (GtkProgress *progress);
76 static void gtk_progress_bar_act_mode_enter (GtkProgress *progress);
78 static void gtk_progress_bar_set_bar_style_internal (GtkProgressBar *pbar,
79 GtkProgressBarStyle style);
80 static void gtk_progress_bar_set_discrete_blocks_internal (GtkProgressBar *pbar,
82 static void gtk_progress_bar_set_activity_step_internal (GtkProgressBar *pbar,
84 static void gtk_progress_bar_set_activity_blocks_internal (GtkProgressBar *pbar,
88 G_DEFINE_TYPE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_PROGRESS)
91 gtk_progress_bar_class_init (GtkProgressBarClass *class)
93 GObjectClass *gobject_class;
94 GtkWidgetClass *widget_class;
95 GtkProgressClass *progress_class;
97 gobject_class = G_OBJECT_CLASS (class);
98 widget_class = (GtkWidgetClass *) class;
99 progress_class = (GtkProgressClass *) class;
101 gobject_class->set_property = gtk_progress_bar_set_property;
102 gobject_class->get_property = gtk_progress_bar_get_property;
104 widget_class->expose_event = gtk_progress_bar_expose;
105 widget_class->size_request = gtk_progress_bar_size_request;
106 widget_class->style_set = gtk_progress_bar_style_set;
108 progress_class->paint = gtk_progress_bar_paint;
109 progress_class->update = gtk_progress_bar_real_update;
110 progress_class->act_mode_enter = gtk_progress_bar_act_mode_enter;
112 g_object_class_install_property (gobject_class,
114 g_param_spec_object ("adjustment",
116 P_("The GtkAdjustment connected to the progress bar (Deprecated)"),
118 GTK_PARAM_READWRITE));
120 g_object_class_install_property (gobject_class,
122 g_param_spec_enum ("orientation",
124 P_("Orientation and growth direction of the progress bar"),
125 GTK_TYPE_PROGRESS_BAR_ORIENTATION,
126 GTK_PROGRESS_LEFT_TO_RIGHT,
127 GTK_PARAM_READWRITE));
129 g_object_class_install_property (gobject_class,
131 g_param_spec_enum ("bar-style",
133 P_("Specifies the visual style of the bar in percentage mode (Deprecated)"),
134 GTK_TYPE_PROGRESS_BAR_STYLE,
135 GTK_PROGRESS_CONTINUOUS,
136 GTK_PARAM_READWRITE));
138 g_object_class_install_property (gobject_class,
140 g_param_spec_uint ("activity-step",
142 P_("The increment used for each iteration in activity mode (Deprecated)"),
144 GTK_PARAM_READWRITE));
146 g_object_class_install_property (gobject_class,
147 PROP_ACTIVITY_BLOCKS,
148 g_param_spec_uint ("activity-blocks",
149 P_("Activity Blocks"),
150 P_("The number of blocks which can fit in the progress bar area in activity mode (Deprecated)"),
152 GTK_PARAM_READWRITE));
154 g_object_class_install_property (gobject_class,
155 PROP_DISCRETE_BLOCKS,
156 g_param_spec_uint ("discrete-blocks",
157 P_("Discrete Blocks"),
158 P_("The number of discrete blocks in a progress bar (when shown in the discrete style)"),
160 GTK_PARAM_READWRITE));
162 g_object_class_install_property (gobject_class,
164 g_param_spec_double ("fraction",
166 P_("The fraction of total work that has been completed"),
168 GTK_PARAM_READWRITE));
170 g_object_class_install_property (gobject_class,
172 g_param_spec_double ("pulse-step",
174 P_("The fraction of total progress to move the bouncing block when pulsed"),
176 GTK_PARAM_READWRITE));
178 g_object_class_install_property (gobject_class,
180 g_param_spec_string ("text",
182 P_("Text to be displayed in the progress bar"),
184 GTK_PARAM_READWRITE));
187 * GtkProgressBar:ellipsize:
189 * The preferred place to ellipsize the string, if the progressbar does
190 * not have enough room to display the entire string, specified as a
191 * #PangoEllisizeMode.
193 * Note that setting this property to a value other than
194 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
195 * only enough space to display the ellipsis "...". Another means to set a
196 * progressbar's width is gtk_widget_set_size_request().
200 g_object_class_install_property (gobject_class,
202 g_param_spec_enum ("ellipsize",
204 P_("The preferred place to ellipsize the string, if the progress bar "
205 "does not have enough room to display the entire string, if at all."),
206 PANGO_TYPE_ELLIPSIZE_MODE,
207 PANGO_ELLIPSIZE_NONE,
208 GTK_PARAM_READWRITE));
209 gtk_widget_class_install_style_property (widget_class,
210 g_param_spec_int ("xspacing",
212 P_("Extra spacing applied to the width of a progress bar."),
215 gtk_widget_class_install_style_property (widget_class,
216 g_param_spec_int ("yspacing",
218 P_("Extra spacing applied to the height of a progress bar."),
223 * GtkProgressBar:min-horizontal-bar-width:
225 * The minimum horizontal width of the progress bar.
229 gtk_widget_class_install_style_property (widget_class,
230 g_param_spec_int ("min-horizontal-bar-width",
231 P_("Min horizontal bar width"),
232 P_("The minimum horizontal width of the progress bar"),
233 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
236 * GtkProgressBar:min-horizontal-bar-height:
238 * Minimum horizontal height of the progress bar.
242 gtk_widget_class_install_style_property (widget_class,
243 g_param_spec_int ("min-horizontal-bar-height",
244 P_("Min horizontal bar height"),
245 P_("Minimum horizontal height of the progress bar"),
246 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
249 * GtkProgressBar:min-vertical-bar-width:
251 * The minimum vertical width of the progress bar.
255 gtk_widget_class_install_style_property (widget_class,
256 g_param_spec_int ("min-vertical-bar-width",
257 P_("Min vertical bar width"),
258 P_("The minimum vertical width of the progress bar"),
259 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
262 * GtkProgressBar:min-vertical-bar-height:
264 * The minimum vertical height of the progress bar.
268 gtk_widget_class_install_style_property (widget_class,
269 g_param_spec_int ("min-vertical-bar-height",
270 P_("Min vertical bar height"),
271 P_("The minimum vertical height of the progress bar"),
272 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
277 gtk_progress_bar_init (GtkProgressBar *pbar)
279 pbar->bar_style = GTK_PROGRESS_CONTINUOUS;
282 pbar->orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
283 pbar->pulse_fraction = 0.1;
284 pbar->activity_pos = 0;
285 pbar->activity_dir = 1;
286 pbar->activity_step = 3;
287 pbar->activity_blocks = 5;
288 pbar->ellipsize = PANGO_ELLIPSIZE_NONE;
292 gtk_progress_bar_set_property (GObject *object,
297 GtkProgressBar *pbar;
299 pbar = GTK_PROGRESS_BAR (object);
303 case PROP_ADJUSTMENT:
304 gtk_progress_set_adjustment (GTK_PROGRESS (pbar),
305 GTK_ADJUSTMENT (g_value_get_object (value)));
307 case PROP_ORIENTATION:
308 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
311 gtk_progress_bar_set_bar_style_internal (pbar, g_value_get_enum (value));
313 case PROP_ACTIVITY_STEP:
314 gtk_progress_bar_set_activity_step_internal (pbar, g_value_get_uint (value));
316 case PROP_ACTIVITY_BLOCKS:
317 gtk_progress_bar_set_activity_blocks_internal (pbar, g_value_get_uint (value));
319 case PROP_DISCRETE_BLOCKS:
320 gtk_progress_bar_set_discrete_blocks_internal (pbar, g_value_get_uint (value));
323 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
325 case PROP_PULSE_STEP:
326 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
329 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
332 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341 gtk_progress_bar_get_property (GObject *object,
346 GtkProgressBar *pbar;
348 pbar = GTK_PROGRESS_BAR (object);
352 case PROP_ADJUSTMENT:
353 g_value_set_object (value, GTK_PROGRESS (pbar)->adjustment);
355 case PROP_ORIENTATION:
356 g_value_set_enum (value, pbar->orientation);
359 g_value_set_enum (value, pbar->bar_style);
361 case PROP_ACTIVITY_STEP:
362 g_value_set_uint (value, pbar->activity_step);
364 case PROP_ACTIVITY_BLOCKS:
365 g_value_set_uint (value, pbar->activity_blocks);
367 case PROP_DISCRETE_BLOCKS:
368 g_value_set_uint (value, pbar->blocks);
371 g_value_set_double (value, gtk_progress_get_current_percentage (GTK_PROGRESS (pbar)));
373 case PROP_PULSE_STEP:
374 g_value_set_double (value, pbar->pulse_fraction);
377 g_value_set_string (value, gtk_progress_bar_get_text (pbar));
380 g_value_set_enum (value, pbar->ellipsize);
383 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
389 gtk_progress_bar_new (void)
393 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
399 gtk_progress_bar_new_with_adjustment (GtkAdjustment *adjustment)
403 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
405 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR,
406 "adjustment", adjustment,
413 gtk_progress_bar_real_update (GtkProgress *progress)
415 GtkProgressBar *pbar;
418 g_return_if_fail (GTK_IS_PROGRESS (progress));
420 pbar = GTK_PROGRESS_BAR (progress);
421 widget = GTK_WIDGET (progress);
423 if (pbar->bar_style == GTK_PROGRESS_CONTINUOUS ||
424 GTK_PROGRESS (pbar)->activity_mode)
426 if (GTK_PROGRESS (pbar)->activity_mode)
430 /* advance the block */
432 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
433 pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
435 /* Update our activity step. */
437 pbar->activity_step = widget->allocation.width * pbar->pulse_fraction;
439 size = MAX (2, widget->allocation.width / pbar->activity_blocks);
441 if (pbar->activity_dir == 0)
443 pbar->activity_pos += pbar->activity_step;
444 if (pbar->activity_pos + size >=
445 widget->allocation.width -
446 widget->style->xthickness)
448 pbar->activity_pos = widget->allocation.width -
449 widget->style->xthickness - size;
450 pbar->activity_dir = 1;
455 pbar->activity_pos -= pbar->activity_step;
456 if (pbar->activity_pos <= widget->style->xthickness)
458 pbar->activity_pos = widget->style->xthickness;
459 pbar->activity_dir = 0;
465 /* Update our activity step. */
467 pbar->activity_step = widget->allocation.height * pbar->pulse_fraction;
469 size = MAX (2, widget->allocation.height / pbar->activity_blocks);
471 if (pbar->activity_dir == 0)
473 pbar->activity_pos += pbar->activity_step;
474 if (pbar->activity_pos + size >=
475 widget->allocation.height -
476 widget->style->ythickness)
478 pbar->activity_pos = widget->allocation.height -
479 widget->style->ythickness - size;
480 pbar->activity_dir = 1;
485 pbar->activity_pos -= pbar->activity_step;
486 if (pbar->activity_pos <= widget->style->ythickness)
488 pbar->activity_pos = widget->style->ythickness;
489 pbar->activity_dir = 0;
495 gtk_widget_queue_draw (GTK_WIDGET (progress));
501 in_block = -1 + (gint)(gtk_progress_get_current_percentage (progress) *
502 (gdouble)pbar->blocks);
504 if (pbar->in_block != in_block)
506 pbar->in_block = in_block;
508 gtk_widget_queue_draw (GTK_WIDGET (progress));
514 gtk_progress_bar_expose (GtkWidget *widget,
515 GdkEventExpose *event)
517 GtkProgressBar *pbar;
519 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (widget), FALSE);
521 pbar = GTK_PROGRESS_BAR (widget);
523 if (GTK_WIDGET_DRAWABLE (widget) && pbar->dirty)
524 gtk_progress_bar_paint (GTK_PROGRESS (pbar));
526 return GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->expose_event (widget, event);
530 gtk_progress_bar_size_request (GtkWidget *widget,
531 GtkRequisition *requisition)
533 GtkProgress *progress;
534 GtkProgressBar *pbar;
536 PangoRectangle logical_rect;
539 gint xspacing, yspacing;
540 gint min_width, min_height;
542 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
543 g_return_if_fail (requisition != NULL);
545 gtk_widget_style_get (widget,
546 "xspacing", &xspacing,
547 "yspacing", &yspacing,
550 progress = GTK_PROGRESS (widget);
551 pbar = GTK_PROGRESS_BAR (widget);
553 width = 2 * widget->style->xthickness + xspacing;
554 height = 2 * widget->style->ythickness + yspacing;
556 if (progress->show_text && pbar->bar_style != GTK_PROGRESS_DISCRETE)
558 if (!progress->adjustment)
559 gtk_progress_set_adjustment (progress, NULL);
561 buf = gtk_progress_get_text_from_value (progress, progress->adjustment->upper);
563 layout = gtk_widget_create_pango_layout (widget, buf);
565 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
569 PangoContext *context;
570 PangoFontMetrics *metrics;
573 /* The minimum size for ellipsized text is ~ 3 chars */
574 context = pango_layout_get_context (layout);
575 metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
577 char_width = pango_font_metrics_get_approximate_char_width (metrics);
578 pango_font_metrics_unref (metrics);
580 width += PANGO_PIXELS (char_width) * 3;
583 width += logical_rect.width;
585 height += logical_rect.height;
587 g_object_unref (layout);
591 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
592 pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
593 gtk_widget_style_get (widget,
594 "min-horizontal-bar-width", &min_width,
595 "min-horizontal-bar-height", &min_height,
598 gtk_widget_style_get (widget,
599 "min-vertical-bar-width", &min_width,
600 "min-vertical-bar-height", &min_height,
603 requisition->width = MAX (min_width, width);
604 requisition->height = MAX (min_height, height);
608 gtk_progress_bar_style_set (GtkWidget *widget,
611 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
615 GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->style_set (widget, previous);
619 gtk_progress_bar_act_mode_enter (GtkProgress *progress)
621 GtkProgressBar *pbar;
623 GtkProgressBarOrientation orientation;
625 pbar = GTK_PROGRESS_BAR (progress);
626 widget = GTK_WIDGET (progress);
628 orientation = pbar->orientation;
629 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
631 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
632 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
633 else if (pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
634 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
637 /* calculate start pos */
639 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
640 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
642 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
644 pbar->activity_pos = widget->style->xthickness;
645 pbar->activity_dir = 0;
649 pbar->activity_pos = widget->allocation.width -
650 widget->style->xthickness - (widget->allocation.height -
651 widget->style->ythickness * 2);
652 pbar->activity_dir = 1;
657 if (orientation == GTK_PROGRESS_TOP_TO_BOTTOM)
659 pbar->activity_pos = widget->style->ythickness;
660 pbar->activity_dir = 0;
664 pbar->activity_pos = widget->allocation.height -
665 widget->style->ythickness - (widget->allocation.width -
666 widget->style->xthickness * 2);
667 pbar->activity_dir = 1;
673 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
674 GtkProgressBarOrientation orientation,
678 GtkWidget *widget = GTK_WIDGET (pbar);
680 *offset = pbar->activity_pos;
684 case GTK_PROGRESS_LEFT_TO_RIGHT:
685 case GTK_PROGRESS_RIGHT_TO_LEFT:
686 *amount = MAX (2, widget->allocation.width / pbar->activity_blocks);
689 case GTK_PROGRESS_TOP_TO_BOTTOM:
690 case GTK_PROGRESS_BOTTOM_TO_TOP:
691 *amount = MAX (2, widget->allocation.height / pbar->activity_blocks);
697 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
698 GtkProgressBarOrientation orientation)
700 GtkWidget *widget = GTK_WIDGET (pbar);
701 GtkProgress *progress = GTK_PROGRESS (pbar);
706 case GTK_PROGRESS_LEFT_TO_RIGHT:
707 case GTK_PROGRESS_RIGHT_TO_LEFT:
708 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
709 area.y = widget->style->ythickness;
710 area.height = widget->allocation.height - 2 * widget->style->ythickness;
713 case GTK_PROGRESS_TOP_TO_BOTTOM:
714 case GTK_PROGRESS_BOTTOM_TO_TOP:
715 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
716 area.x = widget->style->xthickness;
717 area.width = widget->allocation.width - 2 * widget->style->xthickness;
725 gtk_paint_box (widget->style,
726 progress->offscreen_pixmap,
727 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
728 &area, widget, "bar",
729 area.x, area.y, area.width, area.height);
733 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
735 GtkProgressBarOrientation orientation)
738 GtkWidget *widget = GTK_WIDGET (pbar);
745 case GTK_PROGRESS_LEFT_TO_RIGHT:
746 case GTK_PROGRESS_RIGHT_TO_LEFT:
748 area.height = widget->allocation.height - widget->style->ythickness * 2;
749 area.y = widget->style->ythickness;
751 area.x = widget->style->xthickness;
752 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
753 area.x = widget->allocation.width - amount - area.x;
756 case GTK_PROGRESS_TOP_TO_BOTTOM:
757 case GTK_PROGRESS_BOTTOM_TO_TOP:
758 area.width = widget->allocation.width - widget->style->xthickness * 2;
759 area.height = amount;
760 area.x = widget->style->xthickness;
762 area.y = widget->style->ythickness;
763 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
764 area.y = widget->allocation.height - amount - area.y;
772 gtk_paint_box (widget->style,
773 GTK_PROGRESS (pbar)->offscreen_pixmap,
774 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
775 &area, widget, "bar",
776 area.x, area.y, area.width, area.height);
780 gtk_progress_bar_paint_discrete (GtkProgressBar *pbar,
781 GtkProgressBarOrientation orientation)
783 GtkWidget *widget = GTK_WIDGET (pbar);
786 for (i = 0; i <= pbar->in_block; i++)
793 case GTK_PROGRESS_LEFT_TO_RIGHT:
794 case GTK_PROGRESS_RIGHT_TO_LEFT:
795 space = widget->allocation.width - 2 * widget->style->xthickness;
797 area.x = widget->style->xthickness + (i * space) / pbar->blocks;
798 area.y = widget->style->ythickness;
799 area.width = widget->style->xthickness + ((i + 1) * space) / pbar->blocks - area.x;
800 area.height = widget->allocation.height - 2 * widget->style->ythickness;
802 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
803 area.x = widget->allocation.width - area.width - area.x;
806 case GTK_PROGRESS_TOP_TO_BOTTOM:
807 case GTK_PROGRESS_BOTTOM_TO_TOP:
808 space = widget->allocation.height - 2 * widget->style->ythickness;
810 area.x = widget->style->xthickness;
811 area.y = widget->style->ythickness + (i * space) / pbar->blocks;
812 area.width = widget->allocation.width - 2 * widget->style->xthickness;
813 area.height = widget->style->ythickness + ((i + 1) * space) / pbar->blocks - area.y;
815 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
816 area.y = widget->allocation.height - area.height - area.y;
824 gtk_paint_box (widget->style,
825 GTK_PROGRESS (pbar)->offscreen_pixmap,
826 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
827 &area, widget, "bar",
828 area.x, area.y, area.width, area.height);
833 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
836 GtkProgressBarOrientation orientation)
838 GtkProgress *progress = GTK_PROGRESS (pbar);
839 GtkWidget *widget = GTK_WIDGET (pbar);
845 PangoRectangle logical_rect;
846 GdkRectangle prelight_clip, start_clip, end_clip;
847 gfloat text_xalign = progress->x_align;
848 gfloat text_yalign = progress->y_align;
850 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
851 text_xalign = 1.0 - text_xalign;
853 buf = gtk_progress_get_current_text (progress);
855 layout = gtk_widget_create_pango_layout (widget, buf);
856 pango_layout_set_ellipsize (layout, pbar->ellipsize);
858 pango_layout_set_width (layout, widget->allocation.width * PANGO_SCALE);
860 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
862 x = widget->style->xthickness + 1 + text_xalign *
863 (widget->allocation.width - 2 * widget->style->xthickness -
864 2 - logical_rect.width);
866 y = widget->style->ythickness + 1 + text_yalign *
867 (widget->allocation.height - 2 * widget->style->ythickness -
868 2 - logical_rect.height);
870 rect.x = widget->style->xthickness;
871 rect.y = widget->style->ythickness;
872 rect.width = widget->allocation.width - 2 * widget->style->xthickness;
873 rect.height = widget->allocation.height - 2 * widget->style->ythickness;
875 prelight_clip = start_clip = end_clip = rect;
879 case GTK_PROGRESS_LEFT_TO_RIGHT:
881 prelight_clip.x = offset;
882 prelight_clip.width = amount;
883 start_clip.width = prelight_clip.x - start_clip.x;
884 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
885 end_clip.width -= prelight_clip.width + start_clip.width;
888 case GTK_PROGRESS_RIGHT_TO_LEFT:
890 prelight_clip.x = offset;
892 prelight_clip.x = rect.x + rect.width - amount;
893 prelight_clip.width = amount;
894 start_clip.width = prelight_clip.x - start_clip.x;
895 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
896 end_clip.width -= prelight_clip.width + start_clip.width;
899 case GTK_PROGRESS_TOP_TO_BOTTOM:
901 prelight_clip.y = offset;
902 prelight_clip.height = amount;
903 start_clip.height = prelight_clip.y - start_clip.y;
904 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
905 end_clip.height -= prelight_clip.height + start_clip.height;
908 case GTK_PROGRESS_BOTTOM_TO_TOP:
910 prelight_clip.y = offset;
912 prelight_clip.y = rect.y + rect.height - amount;
913 prelight_clip.height = amount;
914 start_clip.height = prelight_clip.y - start_clip.y;
915 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
916 end_clip.height -= prelight_clip.height + start_clip.height;
920 if (start_clip.width > 0 && start_clip.height > 0)
921 gtk_paint_layout (widget->style,
922 progress->offscreen_pixmap,
931 if (end_clip.width > 0 && end_clip.height > 0)
932 gtk_paint_layout (widget->style,
933 progress->offscreen_pixmap,
942 gtk_paint_layout (widget->style,
943 progress->offscreen_pixmap,
952 g_object_unref (layout);
957 gtk_progress_bar_paint (GtkProgress *progress)
959 GtkProgressBar *pbar;
962 GtkProgressBarOrientation orientation;
964 g_return_if_fail (GTK_IS_PROGRESS_BAR (progress));
966 pbar = GTK_PROGRESS_BAR (progress);
967 widget = GTK_WIDGET (progress);
969 orientation = pbar->orientation;
970 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
972 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
973 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
974 else if (pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
975 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
978 if (progress->offscreen_pixmap)
980 gtk_paint_box (widget->style,
981 progress->offscreen_pixmap,
982 GTK_STATE_NORMAL, GTK_SHADOW_IN,
983 NULL, widget, "trough",
985 widget->allocation.width,
986 widget->allocation.height);
988 if (progress->activity_mode)
990 gtk_progress_bar_paint_activity (pbar, orientation);
992 if (GTK_PROGRESS (pbar)->show_text)
997 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
998 gtk_progress_bar_paint_text (pbar, offset, amount, orientation);
1006 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
1007 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
1008 space = widget->allocation.width - 2 * widget->style->xthickness;
1010 space = widget->allocation.height - 2 * widget->style->ythickness;
1013 gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
1015 if (pbar->bar_style == GTK_PROGRESS_CONTINUOUS)
1017 gtk_progress_bar_paint_continuous (pbar, amount, orientation);
1019 if (GTK_PROGRESS (pbar)->show_text)
1020 gtk_progress_bar_paint_text (pbar, -1, amount, orientation);
1023 gtk_progress_bar_paint_discrete (pbar, orientation);
1026 pbar->dirty = FALSE;
1031 gtk_progress_bar_set_bar_style_internal (GtkProgressBar *pbar,
1032 GtkProgressBarStyle bar_style)
1034 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1036 if (pbar->bar_style != bar_style)
1038 pbar->bar_style = bar_style;
1040 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1041 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1043 g_object_notify (G_OBJECT (pbar), "bar-style");
1048 gtk_progress_bar_set_discrete_blocks_internal (GtkProgressBar *pbar,
1051 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1052 g_return_if_fail (blocks > 1);
1054 if (pbar->blocks != blocks)
1056 pbar->blocks = blocks;
1058 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1059 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1061 g_object_notify (G_OBJECT (pbar), "discrete-blocks");
1066 gtk_progress_bar_set_activity_step_internal (GtkProgressBar *pbar,
1069 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1071 if (pbar->activity_step != step)
1073 pbar->activity_step = step;
1074 g_object_notify (G_OBJECT (pbar), "activity-step");
1079 gtk_progress_bar_set_activity_blocks_internal (GtkProgressBar *pbar,
1082 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1083 g_return_if_fail (blocks > 1);
1085 if (pbar->activity_blocks != blocks)
1087 pbar->activity_blocks = blocks;
1088 g_object_notify (G_OBJECT (pbar), "activity-blocks");
1092 /*******************************************************************/
1095 * gtk_progress_bar_set_fraction:
1096 * @pbar: a #GtkProgressBar
1097 * @fraction: fraction of the task that's been completed
1099 * Causes the progress bar to "fill in" the given fraction
1100 * of the bar. The fraction should be between 0.0 and 1.0,
1105 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1108 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1110 /* If we know the percentage, we don't want activity mode. */
1111 gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), FALSE);
1113 /* We use the deprecated GtkProgress interface internally.
1114 * Once everything's been deprecated for a good long time,
1115 * we can clean up all this code.
1117 gtk_progress_set_percentage (GTK_PROGRESS (pbar), fraction);
1119 g_object_notify (G_OBJECT (pbar), "fraction");
1123 * gtk_progress_bar_pulse:
1124 * @pbar: a #GtkProgressBar
1126 * Indicates that some progress is made, but you don't know how much.
1127 * Causes the progress bar to enter "activity mode," where a block
1128 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1129 * causes the block to move by a little bit (the amount of movement
1130 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1133 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1135 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1137 /* If we don't know the percentage, we must want activity mode. */
1138 gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), TRUE);
1141 gtk_progress_bar_real_update (GTK_PROGRESS (pbar));
1145 * gtk_progress_bar_set_text:
1146 * @pbar: a #GtkProgressBar
1147 * @text: a UTF-8 string, or %NULL
1149 * Causes the given @text to appear superimposed on the progress bar.
1152 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1155 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1157 gtk_progress_set_show_text (GTK_PROGRESS (pbar), text && *text);
1158 gtk_progress_set_format_string (GTK_PROGRESS (pbar), text);
1160 /* We don't support formats in this interface, but turn
1161 * them back on for NULL, which should put us back to
1162 * the initial state.
1164 GTK_PROGRESS (pbar)->use_text_format = (text == NULL);
1166 g_object_notify (G_OBJECT (pbar), "text");
1170 * gtk_progress_bar_set_pulse_step:
1171 * @pbar: a #GtkProgressBar
1172 * @fraction: fraction between 0.0 and 1.0
1174 * Sets the fraction of total progress bar length to move the
1175 * bouncing block for each call to gtk_progress_bar_pulse().
1178 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1181 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1183 pbar->pulse_fraction = fraction;
1185 g_object_notify (G_OBJECT (pbar), "pulse-step");
1189 gtk_progress_bar_update (GtkProgressBar *pbar,
1192 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1194 /* Use of gtk_progress_bar_update() is deprecated !
1195 * Use gtk_progress_bar_set_percentage ()
1198 gtk_progress_set_percentage (GTK_PROGRESS (pbar), percentage);
1202 * gtk_progress_bar_set_orientation:
1203 * @pbar: a #GtkProgressBar
1204 * @orientation: orientation of the progress bar
1206 * Causes the progress bar to switch to a different orientation
1207 * (left-to-right, right-to-left, top-to-bottom, or bottom-to-top).
1210 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1211 GtkProgressBarOrientation orientation)
1213 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1215 if (pbar->orientation != orientation)
1217 pbar->orientation = orientation;
1219 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1220 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1222 g_object_notify (G_OBJECT (pbar), "orientation");
1227 * gtk_progress_bar_get_text:
1228 * @pbar: a #GtkProgressBar
1230 * Retrieves the text displayed superimposed on the progress bar,
1231 * if any, otherwise %NULL. The return value is a reference
1232 * to the text, not a copy of it, so will become invalid
1233 * if you change the text in the progress bar.
1235 * Return value: text, or %NULL; this string is owned by the widget
1236 * and should not be modified or freed.
1238 G_CONST_RETURN gchar*
1239 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1241 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1243 if (GTK_PROGRESS (pbar)->use_text_format)
1246 return GTK_PROGRESS (pbar)->format;
1250 * gtk_progress_bar_get_fraction:
1251 * @pbar: a #GtkProgressBar
1253 * Returns the current fraction of the task that's been completed.
1255 * Return value: a fraction from 0.0 to 1.0
1258 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1260 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1262 return gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
1266 * gtk_progress_bar_get_pulse_step:
1267 * @pbar: a #GtkProgressBar
1269 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1271 * Return value: a fraction from 0.0 to 1.0
1274 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1276 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1278 return pbar->pulse_fraction;
1282 * gtk_progress_bar_get_orientation:
1283 * @pbar: a #GtkProgressBar
1285 * Retrieves the current progress bar orientation.
1287 * Return value: orientation of the progress bar
1289 GtkProgressBarOrientation
1290 gtk_progress_bar_get_orientation (GtkProgressBar *pbar)
1292 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1294 return pbar->orientation;
1298 gtk_progress_bar_set_bar_style (GtkProgressBar *pbar,
1299 GtkProgressBarStyle bar_style)
1301 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1303 gtk_progress_bar_set_bar_style_internal (pbar, bar_style);
1307 gtk_progress_bar_set_discrete_blocks (GtkProgressBar *pbar,
1310 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1311 g_return_if_fail (blocks > 1);
1313 gtk_progress_bar_set_discrete_blocks_internal (pbar, blocks);
1317 gtk_progress_bar_set_activity_step (GtkProgressBar *pbar,
1320 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1322 gtk_progress_bar_set_activity_step_internal (pbar, step);
1326 gtk_progress_bar_set_activity_blocks (GtkProgressBar *pbar,
1329 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1330 g_return_if_fail (blocks > 1);
1332 gtk_progress_bar_set_activity_blocks_internal (pbar, blocks);
1336 * gtk_progress_bar_set_ellipsize:
1337 * @pbar: a #GtkProgressBar
1338 * @mode: a #PangoEllipsizeMode
1340 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1341 * if there is not enough space to render the entire string.
1346 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1347 PangoEllipsizeMode mode)
1349 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1350 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1351 mode <= PANGO_ELLIPSIZE_END);
1353 if ((PangoEllipsizeMode)pbar->ellipsize != mode)
1355 pbar->ellipsize = mode;
1357 g_object_notify (G_OBJECT (pbar), "ellipsize");
1358 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1363 * gtk_progress_bar_get_ellipsize:
1364 * @pbar: a #GtkProgressBar
1366 * Returns the ellipsizing position of the progressbar.
1367 * See gtk_progress_bar_set_ellipsize().
1369 * Return value: #PangoEllipsizeMode
1374 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1376 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1378 return pbar->ellipsize;
1381 #define __GTK_PROGRESS_BAR_C__
1382 #include "gtkaliasdef.c"