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 #undef GTK_DISABLE_DEPRECATED
32 #define __GTK_PROGRESS_BAR_C__
34 #include "gtkprogressbar.h"
35 #include "gtkprivate.h"
40 #define MIN_HORIZONTAL_BAR_WIDTH 150
41 #define MIN_HORIZONTAL_BAR_HEIGHT 20
42 #define MIN_VERTICAL_BAR_WIDTH 22
43 #define MIN_VERTICAL_BAR_HEIGHT 80
63 static void gtk_progress_bar_set_property (GObject *object,
67 static void gtk_progress_bar_get_property (GObject *object,
71 static gboolean gtk_progress_bar_expose (GtkWidget *widget,
72 GdkEventExpose *event);
73 static void gtk_progress_bar_size_request (GtkWidget *widget,
74 GtkRequisition *requisition);
75 static void gtk_progress_bar_style_set (GtkWidget *widget,
77 static void gtk_progress_bar_real_update (GtkProgress *progress);
78 static void gtk_progress_bar_paint (GtkProgress *progress);
79 static void gtk_progress_bar_act_mode_enter (GtkProgress *progress);
81 static void gtk_progress_bar_set_bar_style_internal (GtkProgressBar *pbar,
82 GtkProgressBarStyle style);
83 static void gtk_progress_bar_set_discrete_blocks_internal (GtkProgressBar *pbar,
85 static void gtk_progress_bar_set_activity_step_internal (GtkProgressBar *pbar,
87 static void gtk_progress_bar_set_activity_blocks_internal (GtkProgressBar *pbar,
91 G_DEFINE_TYPE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_PROGRESS)
94 gtk_progress_bar_class_init (GtkProgressBarClass *class)
96 GObjectClass *gobject_class;
97 GtkWidgetClass *widget_class;
98 GtkProgressClass *progress_class;
100 gobject_class = G_OBJECT_CLASS (class);
101 widget_class = (GtkWidgetClass *) class;
102 progress_class = (GtkProgressClass *) class;
104 gobject_class->set_property = gtk_progress_bar_set_property;
105 gobject_class->get_property = gtk_progress_bar_get_property;
107 widget_class->expose_event = gtk_progress_bar_expose;
108 widget_class->size_request = gtk_progress_bar_size_request;
109 widget_class->style_set = gtk_progress_bar_style_set;
111 progress_class->paint = gtk_progress_bar_paint;
112 progress_class->update = gtk_progress_bar_real_update;
113 progress_class->act_mode_enter = gtk_progress_bar_act_mode_enter;
115 g_object_class_install_property (gobject_class,
117 g_param_spec_object ("adjustment",
119 P_("The GtkAdjustment connected to the progress bar (Deprecated)"),
121 GTK_PARAM_READWRITE));
123 g_object_class_install_property (gobject_class,
125 g_param_spec_enum ("orientation",
127 P_("Orientation and growth direction of the progress bar"),
128 GTK_TYPE_PROGRESS_BAR_ORIENTATION,
129 GTK_PROGRESS_LEFT_TO_RIGHT,
130 GTK_PARAM_READWRITE));
132 g_object_class_install_property (gobject_class,
134 g_param_spec_enum ("bar-style",
136 P_("Specifies the visual style of the bar in percentage mode (Deprecated)"),
137 GTK_TYPE_PROGRESS_BAR_STYLE,
138 GTK_PROGRESS_CONTINUOUS,
139 GTK_PARAM_READWRITE));
141 g_object_class_install_property (gobject_class,
143 g_param_spec_uint ("activity-step",
145 P_("The increment used for each iteration in activity mode (Deprecated)"),
147 GTK_PARAM_READWRITE));
149 g_object_class_install_property (gobject_class,
150 PROP_ACTIVITY_BLOCKS,
151 g_param_spec_uint ("activity-blocks",
152 P_("Activity Blocks"),
153 P_("The number of blocks which can fit in the progress bar area in activity mode (Deprecated)"),
155 GTK_PARAM_READWRITE));
157 g_object_class_install_property (gobject_class,
158 PROP_DISCRETE_BLOCKS,
159 g_param_spec_uint ("discrete-blocks",
160 P_("Discrete Blocks"),
161 P_("The number of discrete blocks in a progress bar (when shown in the discrete style)"),
163 GTK_PARAM_READWRITE));
165 g_object_class_install_property (gobject_class,
167 g_param_spec_double ("fraction",
169 P_("The fraction of total work that has been completed"),
171 GTK_PARAM_READWRITE));
173 g_object_class_install_property (gobject_class,
175 g_param_spec_double ("pulse-step",
177 P_("The fraction of total progress to move the bouncing block when pulsed"),
179 GTK_PARAM_READWRITE));
181 g_object_class_install_property (gobject_class,
183 g_param_spec_string ("text",
185 P_("Text to be displayed in the progress bar"),
187 GTK_PARAM_READWRITE));
190 * GtkProgressBar:ellipsize:
192 * The preferred place to ellipsize the string, if the progressbar does
193 * not have enough room to display the entire string, specified as a
194 * #PangoEllisizeMode.
196 * Note that setting this property to a value other than
197 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
198 * only enough space to display the ellipsis "...". Another means to set a
199 * progressbar's width is gtk_widget_set_size_request().
203 g_object_class_install_property (gobject_class,
205 g_param_spec_enum ("ellipsize",
207 P_("The preferred place to ellipsize the string, if the progress bar "
208 "does not have enough room to display the entire string, if at all."),
209 PANGO_TYPE_ELLIPSIZE_MODE,
210 PANGO_ELLIPSIZE_NONE,
211 GTK_PARAM_READWRITE));
212 gtk_widget_class_install_style_property (widget_class,
213 g_param_spec_int ("xspacing",
215 P_("Extra spacing applied to the width of a progress bar."),
218 gtk_widget_class_install_style_property (widget_class,
219 g_param_spec_int ("yspacing",
221 P_("Extra spacing applied to the height of a progress bar."),
226 * GtkProgressBar:min-horizontal-bar-width:
228 * The minimum horizontal width of the progress bar.
232 gtk_widget_class_install_style_property (widget_class,
233 g_param_spec_int ("min-horizontal-bar-width",
234 P_("Min horizontal bar width"),
235 P_("The minimum horizontal width of the progress bar"),
236 1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
239 * GtkProgressBar:min-horizontal-bar-height:
241 * Minimum horizontal height of the progress bar.
245 gtk_widget_class_install_style_property (widget_class,
246 g_param_spec_int ("min-horizontal-bar-height",
247 P_("Min horizontal bar height"),
248 P_("Minimum horizontal height of the progress bar"),
249 1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
252 * GtkProgressBar:min-vertical-bar-width:
254 * The minimum vertical width of the progress bar.
258 gtk_widget_class_install_style_property (widget_class,
259 g_param_spec_int ("min-vertical-bar-width",
260 P_("Min vertical bar width"),
261 P_("The minimum vertical width of the progress bar"),
262 1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
265 * GtkProgressBar:min-vertical-bar-height:
267 * The minimum vertical height of the progress bar.
271 gtk_widget_class_install_style_property (widget_class,
272 g_param_spec_int ("min-vertical-bar-height",
273 P_("Min vertical bar height"),
274 P_("The minimum vertical height of the progress bar"),
275 1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
280 gtk_progress_bar_init (GtkProgressBar *pbar)
282 pbar->bar_style = GTK_PROGRESS_CONTINUOUS;
285 pbar->orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
286 pbar->pulse_fraction = 0.1;
287 pbar->activity_pos = 0;
288 pbar->activity_dir = 1;
289 pbar->activity_step = 3;
290 pbar->activity_blocks = 5;
291 pbar->ellipsize = PANGO_ELLIPSIZE_NONE;
295 gtk_progress_bar_set_property (GObject *object,
300 GtkProgressBar *pbar;
302 pbar = GTK_PROGRESS_BAR (object);
306 case PROP_ADJUSTMENT:
307 gtk_progress_set_adjustment (GTK_PROGRESS (pbar),
308 GTK_ADJUSTMENT (g_value_get_object (value)));
310 case PROP_ORIENTATION:
311 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
314 gtk_progress_bar_set_bar_style_internal (pbar, g_value_get_enum (value));
316 case PROP_ACTIVITY_STEP:
317 gtk_progress_bar_set_activity_step_internal (pbar, g_value_get_uint (value));
319 case PROP_ACTIVITY_BLOCKS:
320 gtk_progress_bar_set_activity_blocks_internal (pbar, g_value_get_uint (value));
322 case PROP_DISCRETE_BLOCKS:
323 gtk_progress_bar_set_discrete_blocks_internal (pbar, g_value_get_uint (value));
326 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
328 case PROP_PULSE_STEP:
329 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
332 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
335 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 gtk_progress_bar_get_property (GObject *object,
349 GtkProgressBar *pbar;
351 pbar = GTK_PROGRESS_BAR (object);
355 case PROP_ADJUSTMENT:
356 g_value_set_object (value, GTK_PROGRESS (pbar)->adjustment);
358 case PROP_ORIENTATION:
359 g_value_set_enum (value, pbar->orientation);
362 g_value_set_enum (value, pbar->bar_style);
364 case PROP_ACTIVITY_STEP:
365 g_value_set_uint (value, pbar->activity_step);
367 case PROP_ACTIVITY_BLOCKS:
368 g_value_set_uint (value, pbar->activity_blocks);
370 case PROP_DISCRETE_BLOCKS:
371 g_value_set_uint (value, pbar->blocks);
374 g_value_set_double (value, gtk_progress_get_current_percentage (GTK_PROGRESS (pbar)));
376 case PROP_PULSE_STEP:
377 g_value_set_double (value, pbar->pulse_fraction);
380 g_value_set_string (value, gtk_progress_bar_get_text (pbar));
383 g_value_set_enum (value, pbar->ellipsize);
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
392 gtk_progress_bar_new (void)
396 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
402 gtk_progress_bar_new_with_adjustment (GtkAdjustment *adjustment)
406 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
408 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR,
409 "adjustment", adjustment,
416 gtk_progress_bar_real_update (GtkProgress *progress)
418 GtkProgressBar *pbar;
421 g_return_if_fail (GTK_IS_PROGRESS (progress));
423 pbar = GTK_PROGRESS_BAR (progress);
424 widget = GTK_WIDGET (progress);
426 if (pbar->bar_style == GTK_PROGRESS_CONTINUOUS ||
427 GTK_PROGRESS (pbar)->activity_mode)
429 if (GTK_PROGRESS (pbar)->activity_mode)
433 /* advance the block */
435 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
436 pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
438 /* Update our activity step. */
440 pbar->activity_step = widget->allocation.width * pbar->pulse_fraction;
442 size = MAX (2, widget->allocation.width / pbar->activity_blocks);
444 if (pbar->activity_dir == 0)
446 pbar->activity_pos += pbar->activity_step;
447 if (pbar->activity_pos + size >=
448 widget->allocation.width -
449 widget->style->xthickness)
451 pbar->activity_pos = widget->allocation.width -
452 widget->style->xthickness - size;
453 pbar->activity_dir = 1;
458 pbar->activity_pos -= pbar->activity_step;
459 if (pbar->activity_pos <= widget->style->xthickness)
461 pbar->activity_pos = widget->style->xthickness;
462 pbar->activity_dir = 0;
468 /* Update our activity step. */
470 pbar->activity_step = widget->allocation.height * pbar->pulse_fraction;
472 size = MAX (2, widget->allocation.height / pbar->activity_blocks);
474 if (pbar->activity_dir == 0)
476 pbar->activity_pos += pbar->activity_step;
477 if (pbar->activity_pos + size >=
478 widget->allocation.height -
479 widget->style->ythickness)
481 pbar->activity_pos = widget->allocation.height -
482 widget->style->ythickness - size;
483 pbar->activity_dir = 1;
488 pbar->activity_pos -= pbar->activity_step;
489 if (pbar->activity_pos <= widget->style->ythickness)
491 pbar->activity_pos = widget->style->ythickness;
492 pbar->activity_dir = 0;
498 gtk_widget_queue_draw (GTK_WIDGET (progress));
504 in_block = -1 + (gint)(gtk_progress_get_current_percentage (progress) *
505 (gdouble)pbar->blocks);
507 if (pbar->in_block != in_block)
509 pbar->in_block = in_block;
511 gtk_widget_queue_draw (GTK_WIDGET (progress));
517 gtk_progress_bar_expose (GtkWidget *widget,
518 GdkEventExpose *event)
520 GtkProgressBar *pbar;
522 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (widget), FALSE);
524 pbar = GTK_PROGRESS_BAR (widget);
526 if (GTK_WIDGET_DRAWABLE (widget) && pbar->dirty)
527 gtk_progress_bar_paint (GTK_PROGRESS (pbar));
529 return GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->expose_event (widget, event);
533 gtk_progress_bar_size_request (GtkWidget *widget,
534 GtkRequisition *requisition)
536 GtkProgress *progress;
537 GtkProgressBar *pbar;
539 PangoRectangle logical_rect;
542 gint xspacing, yspacing;
543 gint min_width, min_height;
545 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
546 g_return_if_fail (requisition != NULL);
548 gtk_widget_style_get (widget,
549 "xspacing", &xspacing,
550 "yspacing", &yspacing,
553 progress = GTK_PROGRESS (widget);
554 pbar = GTK_PROGRESS_BAR (widget);
556 width = 2 * widget->style->xthickness + xspacing;
557 height = 2 * widget->style->ythickness + yspacing;
559 if (progress->show_text && pbar->bar_style != GTK_PROGRESS_DISCRETE)
561 if (!progress->adjustment)
562 gtk_progress_set_adjustment (progress, NULL);
564 buf = gtk_progress_get_text_from_value (progress, progress->adjustment->upper);
566 layout = gtk_widget_create_pango_layout (widget, buf);
568 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
572 PangoContext *context;
573 PangoFontMetrics *metrics;
576 /* The minimum size for ellipsized text is ~ 3 chars */
577 context = pango_layout_get_context (layout);
578 metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
580 char_width = pango_font_metrics_get_approximate_char_width (metrics);
581 pango_font_metrics_unref (metrics);
583 width += PANGO_PIXELS (char_width) * 3;
586 width += logical_rect.width;
588 height += logical_rect.height;
590 g_object_unref (layout);
594 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
595 pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
596 gtk_widget_style_get (widget,
597 "min-horizontal-bar-width", &min_width,
598 "min-horizontal-bar-height", &min_height,
601 gtk_widget_style_get (widget,
602 "min-vertical-bar-width", &min_width,
603 "min-vertical-bar-height", &min_height,
606 requisition->width = MAX (min_width, width);
607 requisition->height = MAX (min_height, height);
611 gtk_progress_bar_style_set (GtkWidget *widget,
614 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
618 GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->style_set (widget, previous);
622 gtk_progress_bar_act_mode_enter (GtkProgress *progress)
624 GtkProgressBar *pbar;
626 GtkProgressBarOrientation orientation;
628 pbar = GTK_PROGRESS_BAR (progress);
629 widget = GTK_WIDGET (progress);
631 orientation = pbar->orientation;
632 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
634 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
635 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
636 else if (pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
637 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
640 /* calculate start pos */
642 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
643 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
645 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
647 pbar->activity_pos = widget->style->xthickness;
648 pbar->activity_dir = 0;
652 pbar->activity_pos = widget->allocation.width -
653 widget->style->xthickness - (widget->allocation.height -
654 widget->style->ythickness * 2);
655 pbar->activity_dir = 1;
660 if (orientation == GTK_PROGRESS_TOP_TO_BOTTOM)
662 pbar->activity_pos = widget->style->ythickness;
663 pbar->activity_dir = 0;
667 pbar->activity_pos = widget->allocation.height -
668 widget->style->ythickness - (widget->allocation.width -
669 widget->style->xthickness * 2);
670 pbar->activity_dir = 1;
676 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
677 GtkProgressBarOrientation orientation,
681 GtkWidget *widget = GTK_WIDGET (pbar);
683 *offset = pbar->activity_pos;
687 case GTK_PROGRESS_LEFT_TO_RIGHT:
688 case GTK_PROGRESS_RIGHT_TO_LEFT:
689 *amount = MAX (2, widget->allocation.width / pbar->activity_blocks);
692 case GTK_PROGRESS_TOP_TO_BOTTOM:
693 case GTK_PROGRESS_BOTTOM_TO_TOP:
694 *amount = MAX (2, widget->allocation.height / pbar->activity_blocks);
700 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
701 GtkProgressBarOrientation orientation)
703 GtkWidget *widget = GTK_WIDGET (pbar);
704 GtkProgress *progress = GTK_PROGRESS (pbar);
709 case GTK_PROGRESS_LEFT_TO_RIGHT:
710 case GTK_PROGRESS_RIGHT_TO_LEFT:
711 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
712 area.y = widget->style->ythickness;
713 area.height = widget->allocation.height - 2 * widget->style->ythickness;
716 case GTK_PROGRESS_TOP_TO_BOTTOM:
717 case GTK_PROGRESS_BOTTOM_TO_TOP:
718 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
719 area.x = widget->style->xthickness;
720 area.width = widget->allocation.width - 2 * widget->style->xthickness;
728 gtk_paint_box (widget->style,
729 progress->offscreen_pixmap,
730 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
731 &area, widget, "bar",
732 area.x, area.y, area.width, area.height);
736 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
738 GtkProgressBarOrientation orientation)
741 GtkWidget *widget = GTK_WIDGET (pbar);
748 case GTK_PROGRESS_LEFT_TO_RIGHT:
749 case GTK_PROGRESS_RIGHT_TO_LEFT:
751 area.height = widget->allocation.height - widget->style->ythickness * 2;
752 area.y = widget->style->ythickness;
754 area.x = widget->style->xthickness;
755 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
756 area.x = widget->allocation.width - amount - area.x;
759 case GTK_PROGRESS_TOP_TO_BOTTOM:
760 case GTK_PROGRESS_BOTTOM_TO_TOP:
761 area.width = widget->allocation.width - widget->style->xthickness * 2;
762 area.height = amount;
763 area.x = widget->style->xthickness;
765 area.y = widget->style->ythickness;
766 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
767 area.y = widget->allocation.height - amount - area.y;
775 gtk_paint_box (widget->style,
776 GTK_PROGRESS (pbar)->offscreen_pixmap,
777 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
778 &area, widget, "bar",
779 area.x, area.y, area.width, area.height);
783 gtk_progress_bar_paint_discrete (GtkProgressBar *pbar,
784 GtkProgressBarOrientation orientation)
786 GtkWidget *widget = GTK_WIDGET (pbar);
789 for (i = 0; i <= pbar->in_block; i++)
796 case GTK_PROGRESS_LEFT_TO_RIGHT:
797 case GTK_PROGRESS_RIGHT_TO_LEFT:
798 space = widget->allocation.width - 2 * widget->style->xthickness;
800 area.x = widget->style->xthickness + (i * space) / pbar->blocks;
801 area.y = widget->style->ythickness;
802 area.width = widget->style->xthickness + ((i + 1) * space) / pbar->blocks - area.x;
803 area.height = widget->allocation.height - 2 * widget->style->ythickness;
805 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
806 area.x = widget->allocation.width - area.width - area.x;
809 case GTK_PROGRESS_TOP_TO_BOTTOM:
810 case GTK_PROGRESS_BOTTOM_TO_TOP:
811 space = widget->allocation.height - 2 * widget->style->ythickness;
813 area.x = widget->style->xthickness;
814 area.y = widget->style->ythickness + (i * space) / pbar->blocks;
815 area.width = widget->allocation.width - 2 * widget->style->xthickness;
816 area.height = widget->style->ythickness + ((i + 1) * space) / pbar->blocks - area.y;
818 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
819 area.y = widget->allocation.height - area.height - area.y;
827 gtk_paint_box (widget->style,
828 GTK_PROGRESS (pbar)->offscreen_pixmap,
829 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
830 &area, widget, "bar",
831 area.x, area.y, area.width, area.height);
836 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
839 GtkProgressBarOrientation orientation)
841 GtkProgress *progress = GTK_PROGRESS (pbar);
842 GtkWidget *widget = GTK_WIDGET (pbar);
848 PangoRectangle logical_rect;
849 GdkRectangle prelight_clip, start_clip, end_clip;
850 gfloat text_xalign = progress->x_align;
851 gfloat text_yalign = progress->y_align;
853 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
854 text_xalign = 1.0 - text_xalign;
856 buf = gtk_progress_get_current_text (progress);
858 layout = gtk_widget_create_pango_layout (widget, buf);
859 pango_layout_set_ellipsize (layout, pbar->ellipsize);
861 pango_layout_set_width (layout, widget->allocation.width * PANGO_SCALE);
863 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
865 x = widget->style->xthickness + 1 + text_xalign *
866 (widget->allocation.width - 2 * widget->style->xthickness -
867 2 - logical_rect.width);
869 y = widget->style->ythickness + 1 + text_yalign *
870 (widget->allocation.height - 2 * widget->style->ythickness -
871 2 - logical_rect.height);
873 rect.x = widget->style->xthickness;
874 rect.y = widget->style->ythickness;
875 rect.width = widget->allocation.width - 2 * widget->style->xthickness;
876 rect.height = widget->allocation.height - 2 * widget->style->ythickness;
878 prelight_clip = start_clip = end_clip = rect;
882 case GTK_PROGRESS_LEFT_TO_RIGHT:
884 prelight_clip.x = offset;
885 prelight_clip.width = amount;
886 start_clip.width = prelight_clip.x - start_clip.x;
887 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
888 end_clip.width -= prelight_clip.width + start_clip.width;
891 case GTK_PROGRESS_RIGHT_TO_LEFT:
893 prelight_clip.x = offset;
895 prelight_clip.x = rect.x + rect.width - amount;
896 prelight_clip.width = amount;
897 start_clip.width = prelight_clip.x - start_clip.x;
898 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
899 end_clip.width -= prelight_clip.width + start_clip.width;
902 case GTK_PROGRESS_TOP_TO_BOTTOM:
904 prelight_clip.y = offset;
905 prelight_clip.height = amount;
906 start_clip.height = prelight_clip.y - start_clip.y;
907 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
908 end_clip.height -= prelight_clip.height + start_clip.height;
911 case GTK_PROGRESS_BOTTOM_TO_TOP:
913 prelight_clip.y = offset;
915 prelight_clip.y = rect.y + rect.height - amount;
916 prelight_clip.height = amount;
917 start_clip.height = prelight_clip.y - start_clip.y;
918 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
919 end_clip.height -= prelight_clip.height + start_clip.height;
923 if (start_clip.width > 0 && start_clip.height > 0)
924 gtk_paint_layout (widget->style,
925 progress->offscreen_pixmap,
934 if (end_clip.width > 0 && end_clip.height > 0)
935 gtk_paint_layout (widget->style,
936 progress->offscreen_pixmap,
945 gtk_paint_layout (widget->style,
946 progress->offscreen_pixmap,
955 g_object_unref (layout);
960 gtk_progress_bar_paint (GtkProgress *progress)
962 GtkProgressBar *pbar;
965 GtkProgressBarOrientation orientation;
967 g_return_if_fail (GTK_IS_PROGRESS_BAR (progress));
969 pbar = GTK_PROGRESS_BAR (progress);
970 widget = GTK_WIDGET (progress);
972 orientation = pbar->orientation;
973 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
975 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
976 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
977 else if (pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
978 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
981 if (progress->offscreen_pixmap)
983 gtk_paint_box (widget->style,
984 progress->offscreen_pixmap,
985 GTK_STATE_NORMAL, GTK_SHADOW_IN,
986 NULL, widget, "trough",
988 widget->allocation.width,
989 widget->allocation.height);
991 if (progress->activity_mode)
993 gtk_progress_bar_paint_activity (pbar, orientation);
995 if (GTK_PROGRESS (pbar)->show_text)
1000 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
1001 gtk_progress_bar_paint_text (pbar, offset, amount, orientation);
1009 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
1010 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
1011 space = widget->allocation.width - 2 * widget->style->xthickness;
1013 space = widget->allocation.height - 2 * widget->style->ythickness;
1016 gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
1018 if (pbar->bar_style == GTK_PROGRESS_CONTINUOUS)
1020 gtk_progress_bar_paint_continuous (pbar, amount, orientation);
1022 if (GTK_PROGRESS (pbar)->show_text)
1023 gtk_progress_bar_paint_text (pbar, -1, amount, orientation);
1026 gtk_progress_bar_paint_discrete (pbar, orientation);
1029 pbar->dirty = FALSE;
1034 gtk_progress_bar_set_bar_style_internal (GtkProgressBar *pbar,
1035 GtkProgressBarStyle bar_style)
1037 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1039 if (pbar->bar_style != bar_style)
1041 pbar->bar_style = bar_style;
1043 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1044 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1046 g_object_notify (G_OBJECT (pbar), "bar-style");
1051 gtk_progress_bar_set_discrete_blocks_internal (GtkProgressBar *pbar,
1054 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1055 g_return_if_fail (blocks > 1);
1057 if (pbar->blocks != blocks)
1059 pbar->blocks = blocks;
1061 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1062 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1064 g_object_notify (G_OBJECT (pbar), "discrete-blocks");
1069 gtk_progress_bar_set_activity_step_internal (GtkProgressBar *pbar,
1072 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1074 if (pbar->activity_step != step)
1076 pbar->activity_step = step;
1077 g_object_notify (G_OBJECT (pbar), "activity-step");
1082 gtk_progress_bar_set_activity_blocks_internal (GtkProgressBar *pbar,
1085 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1086 g_return_if_fail (blocks > 1);
1088 if (pbar->activity_blocks != blocks)
1090 pbar->activity_blocks = blocks;
1091 g_object_notify (G_OBJECT (pbar), "activity-blocks");
1095 /*******************************************************************/
1098 * gtk_progress_bar_set_fraction:
1099 * @pbar: a #GtkProgressBar
1100 * @fraction: fraction of the task that's been completed
1102 * Causes the progress bar to "fill in" the given fraction
1103 * of the bar. The fraction should be between 0.0 and 1.0,
1108 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1111 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1113 /* If we know the percentage, we don't want activity mode. */
1114 gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), FALSE);
1116 /* We use the deprecated GtkProgress interface internally.
1117 * Once everything's been deprecated for a good long time,
1118 * we can clean up all this code.
1120 gtk_progress_set_percentage (GTK_PROGRESS (pbar), fraction);
1122 g_object_notify (G_OBJECT (pbar), "fraction");
1126 * gtk_progress_bar_pulse:
1127 * @pbar: a #GtkProgressBar
1129 * Indicates that some progress is made, but you don't know how much.
1130 * Causes the progress bar to enter "activity mode," where a block
1131 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1132 * causes the block to move by a little bit (the amount of movement
1133 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1136 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1138 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1140 /* If we don't know the percentage, we must want activity mode. */
1141 gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), TRUE);
1144 gtk_progress_bar_real_update (GTK_PROGRESS (pbar));
1148 * gtk_progress_bar_set_text:
1149 * @pbar: a #GtkProgressBar
1150 * @text: a UTF-8 string, or %NULL
1152 * Causes the given @text to appear superimposed on the progress bar.
1155 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1158 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1160 gtk_progress_set_show_text (GTK_PROGRESS (pbar), text && *text);
1161 gtk_progress_set_format_string (GTK_PROGRESS (pbar), text);
1163 /* We don't support formats in this interface, but turn
1164 * them back on for NULL, which should put us back to
1165 * the initial state.
1167 GTK_PROGRESS (pbar)->use_text_format = (text == NULL);
1169 g_object_notify (G_OBJECT (pbar), "text");
1173 * gtk_progress_bar_set_pulse_step:
1174 * @pbar: a #GtkProgressBar
1175 * @fraction: fraction between 0.0 and 1.0
1177 * Sets the fraction of total progress bar length to move the
1178 * bouncing block for each call to gtk_progress_bar_pulse().
1181 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1184 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1186 pbar->pulse_fraction = fraction;
1188 g_object_notify (G_OBJECT (pbar), "pulse-step");
1192 gtk_progress_bar_update (GtkProgressBar *pbar,
1195 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1197 /* Use of gtk_progress_bar_update() is deprecated !
1198 * Use gtk_progress_bar_set_percentage ()
1201 gtk_progress_set_percentage (GTK_PROGRESS (pbar), percentage);
1205 * gtk_progress_bar_set_orientation:
1206 * @pbar: a #GtkProgressBar
1207 * @orientation: orientation of the progress bar
1209 * Causes the progress bar to switch to a different orientation
1210 * (left-to-right, right-to-left, top-to-bottom, or bottom-to-top).
1213 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1214 GtkProgressBarOrientation orientation)
1216 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1218 if (pbar->orientation != orientation)
1220 pbar->orientation = orientation;
1222 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1223 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1225 g_object_notify (G_OBJECT (pbar), "orientation");
1230 * gtk_progress_bar_get_text:
1231 * @pbar: a #GtkProgressBar
1233 * Retrieves the text displayed superimposed on the progress bar,
1234 * if any, otherwise %NULL. The return value is a reference
1235 * to the text, not a copy of it, so will become invalid
1236 * if you change the text in the progress bar.
1238 * Return value: text, or %NULL; this string is owned by the widget
1239 * and should not be modified or freed.
1241 G_CONST_RETURN gchar*
1242 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1244 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1246 if (GTK_PROGRESS (pbar)->use_text_format)
1249 return GTK_PROGRESS (pbar)->format;
1253 * gtk_progress_bar_get_fraction:
1254 * @pbar: a #GtkProgressBar
1256 * Returns the current fraction of the task that's been completed.
1258 * Return value: a fraction from 0.0 to 1.0
1261 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1263 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1265 return gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
1269 * gtk_progress_bar_get_pulse_step:
1270 * @pbar: a #GtkProgressBar
1272 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1274 * Return value: a fraction from 0.0 to 1.0
1277 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1279 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1281 return pbar->pulse_fraction;
1285 * gtk_progress_bar_get_orientation:
1286 * @pbar: a #GtkProgressBar
1288 * Retrieves the current progress bar orientation.
1290 * Return value: orientation of the progress bar
1292 GtkProgressBarOrientation
1293 gtk_progress_bar_get_orientation (GtkProgressBar *pbar)
1295 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1297 return pbar->orientation;
1301 gtk_progress_bar_set_bar_style (GtkProgressBar *pbar,
1302 GtkProgressBarStyle bar_style)
1304 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1306 gtk_progress_bar_set_bar_style_internal (pbar, bar_style);
1310 gtk_progress_bar_set_discrete_blocks (GtkProgressBar *pbar,
1313 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1314 g_return_if_fail (blocks > 1);
1316 gtk_progress_bar_set_discrete_blocks_internal (pbar, blocks);
1320 gtk_progress_bar_set_activity_step (GtkProgressBar *pbar,
1323 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1325 gtk_progress_bar_set_activity_step_internal (pbar, step);
1329 gtk_progress_bar_set_activity_blocks (GtkProgressBar *pbar,
1332 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1333 g_return_if_fail (blocks > 1);
1335 gtk_progress_bar_set_activity_blocks_internal (pbar, blocks);
1339 * gtk_progress_bar_set_ellipsize:
1340 * @pbar: a #GtkProgressBar
1341 * @mode: a #PangoEllipsizeMode
1343 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1344 * if there is not enough space to render the entire string.
1349 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1350 PangoEllipsizeMode mode)
1352 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1353 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1354 mode <= PANGO_ELLIPSIZE_END);
1356 if ((PangoEllipsizeMode)pbar->ellipsize != mode)
1358 pbar->ellipsize = mode;
1360 g_object_notify (G_OBJECT (pbar), "ellipsize");
1361 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1366 * gtk_progress_bar_get_ellipsize:
1367 * @pbar: a #GtkProgressBar
1369 * Returns the ellipsizing position of the progressbar.
1370 * See gtk_progress_bar_set_ellipsize().
1372 * Return value: #PangoEllipsizeMode
1377 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1379 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1381 return pbar->ellipsize;
1384 #include "gtkaliasdef.c"