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:
403 * @adjustment: (allow-none):
405 * Creates a new #GtkProgressBar with an associated #GtkAdjustment.
407 * Returns: (transfer none): a #GtkProgressBar.
410 gtk_progress_bar_new_with_adjustment (GtkAdjustment *adjustment)
414 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
416 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR,
417 "adjustment", adjustment,
424 gtk_progress_bar_real_update (GtkProgress *progress)
426 GtkProgressBar *pbar;
429 g_return_if_fail (GTK_IS_PROGRESS (progress));
431 pbar = GTK_PROGRESS_BAR (progress);
432 widget = GTK_WIDGET (progress);
434 if (pbar->bar_style == GTK_PROGRESS_CONTINUOUS ||
435 GTK_PROGRESS (pbar)->activity_mode)
437 if (GTK_PROGRESS (pbar)->activity_mode)
441 /* advance the block */
443 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
444 pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
446 /* Update our activity step. */
448 pbar->activity_step = widget->allocation.width * pbar->pulse_fraction;
450 size = MAX (2, widget->allocation.width / pbar->activity_blocks);
452 if (pbar->activity_dir == 0)
454 pbar->activity_pos += pbar->activity_step;
455 if (pbar->activity_pos + size >=
456 widget->allocation.width -
457 widget->style->xthickness)
459 pbar->activity_pos = widget->allocation.width -
460 widget->style->xthickness - size;
461 pbar->activity_dir = 1;
466 pbar->activity_pos -= pbar->activity_step;
467 if (pbar->activity_pos <= widget->style->xthickness)
469 pbar->activity_pos = widget->style->xthickness;
470 pbar->activity_dir = 0;
476 /* Update our activity step. */
478 pbar->activity_step = widget->allocation.height * pbar->pulse_fraction;
480 size = MAX (2, widget->allocation.height / pbar->activity_blocks);
482 if (pbar->activity_dir == 0)
484 pbar->activity_pos += pbar->activity_step;
485 if (pbar->activity_pos + size >=
486 widget->allocation.height -
487 widget->style->ythickness)
489 pbar->activity_pos = widget->allocation.height -
490 widget->style->ythickness - size;
491 pbar->activity_dir = 1;
496 pbar->activity_pos -= pbar->activity_step;
497 if (pbar->activity_pos <= widget->style->ythickness)
499 pbar->activity_pos = widget->style->ythickness;
500 pbar->activity_dir = 0;
506 gtk_widget_queue_draw (GTK_WIDGET (progress));
512 in_block = -1 + (gint)(gtk_progress_get_current_percentage (progress) *
513 (gdouble)pbar->blocks);
515 if (pbar->in_block != in_block)
517 pbar->in_block = in_block;
519 gtk_widget_queue_draw (GTK_WIDGET (progress));
525 gtk_progress_bar_expose (GtkWidget *widget,
526 GdkEventExpose *event)
528 GtkProgressBar *pbar;
530 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (widget), FALSE);
532 pbar = GTK_PROGRESS_BAR (widget);
534 if (GTK_WIDGET_DRAWABLE (widget) && pbar->dirty)
535 gtk_progress_bar_paint (GTK_PROGRESS (pbar));
537 return GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->expose_event (widget, event);
541 gtk_progress_bar_size_request (GtkWidget *widget,
542 GtkRequisition *requisition)
544 GtkProgress *progress;
545 GtkProgressBar *pbar;
547 PangoRectangle logical_rect;
550 gint xspacing, yspacing;
551 gint min_width, min_height;
553 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
554 g_return_if_fail (requisition != NULL);
556 gtk_widget_style_get (widget,
557 "xspacing", &xspacing,
558 "yspacing", &yspacing,
561 progress = GTK_PROGRESS (widget);
562 pbar = GTK_PROGRESS_BAR (widget);
564 width = 2 * widget->style->xthickness + xspacing;
565 height = 2 * widget->style->ythickness + yspacing;
567 if (progress->show_text && pbar->bar_style != GTK_PROGRESS_DISCRETE)
569 if (!progress->adjustment)
570 gtk_progress_set_adjustment (progress, NULL);
572 buf = gtk_progress_get_text_from_value (progress, progress->adjustment->upper);
574 layout = gtk_widget_create_pango_layout (widget, buf);
576 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
580 PangoContext *context;
581 PangoFontMetrics *metrics;
584 /* The minimum size for ellipsized text is ~ 3 chars */
585 context = pango_layout_get_context (layout);
586 metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
588 char_width = pango_font_metrics_get_approximate_char_width (metrics);
589 pango_font_metrics_unref (metrics);
591 width += PANGO_PIXELS (char_width) * 3;
594 width += logical_rect.width;
596 height += logical_rect.height;
598 g_object_unref (layout);
602 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
603 pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
604 gtk_widget_style_get (widget,
605 "min-horizontal-bar-width", &min_width,
606 "min-horizontal-bar-height", &min_height,
609 gtk_widget_style_get (widget,
610 "min-vertical-bar-width", &min_width,
611 "min-vertical-bar-height", &min_height,
614 requisition->width = MAX (min_width, width);
615 requisition->height = MAX (min_height, height);
619 gtk_progress_bar_style_set (GtkWidget *widget,
622 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
626 GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->style_set (widget, previous);
630 gtk_progress_bar_act_mode_enter (GtkProgress *progress)
632 GtkProgressBar *pbar;
634 GtkProgressBarOrientation orientation;
636 pbar = GTK_PROGRESS_BAR (progress);
637 widget = GTK_WIDGET (progress);
639 orientation = pbar->orientation;
640 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
642 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
643 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
644 else if (pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
645 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
648 /* calculate start pos */
650 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
651 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
653 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
655 pbar->activity_pos = widget->style->xthickness;
656 pbar->activity_dir = 0;
660 pbar->activity_pos = widget->allocation.width -
661 widget->style->xthickness - (widget->allocation.height -
662 widget->style->ythickness * 2);
663 pbar->activity_dir = 1;
668 if (orientation == GTK_PROGRESS_TOP_TO_BOTTOM)
670 pbar->activity_pos = widget->style->ythickness;
671 pbar->activity_dir = 0;
675 pbar->activity_pos = widget->allocation.height -
676 widget->style->ythickness - (widget->allocation.width -
677 widget->style->xthickness * 2);
678 pbar->activity_dir = 1;
684 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
685 GtkProgressBarOrientation orientation,
689 GtkWidget *widget = GTK_WIDGET (pbar);
691 *offset = pbar->activity_pos;
695 case GTK_PROGRESS_LEFT_TO_RIGHT:
696 case GTK_PROGRESS_RIGHT_TO_LEFT:
697 *amount = MAX (2, widget->allocation.width / pbar->activity_blocks);
700 case GTK_PROGRESS_TOP_TO_BOTTOM:
701 case GTK_PROGRESS_BOTTOM_TO_TOP:
702 *amount = MAX (2, widget->allocation.height / pbar->activity_blocks);
708 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
709 GtkProgressBarOrientation orientation)
711 GtkWidget *widget = GTK_WIDGET (pbar);
712 GtkProgress *progress = GTK_PROGRESS (pbar);
717 case GTK_PROGRESS_LEFT_TO_RIGHT:
718 case GTK_PROGRESS_RIGHT_TO_LEFT:
719 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
720 area.y = widget->style->ythickness;
721 area.height = widget->allocation.height - 2 * widget->style->ythickness;
724 case GTK_PROGRESS_TOP_TO_BOTTOM:
725 case GTK_PROGRESS_BOTTOM_TO_TOP:
726 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
727 area.x = widget->style->xthickness;
728 area.width = widget->allocation.width - 2 * widget->style->xthickness;
736 gtk_paint_box (widget->style,
737 progress->offscreen_pixmap,
738 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
739 &area, widget, "bar",
740 area.x, area.y, area.width, area.height);
744 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
746 GtkProgressBarOrientation orientation)
749 GtkWidget *widget = GTK_WIDGET (pbar);
756 case GTK_PROGRESS_LEFT_TO_RIGHT:
757 case GTK_PROGRESS_RIGHT_TO_LEFT:
759 area.height = widget->allocation.height - widget->style->ythickness * 2;
760 area.y = widget->style->ythickness;
762 area.x = widget->style->xthickness;
763 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
764 area.x = widget->allocation.width - amount - area.x;
767 case GTK_PROGRESS_TOP_TO_BOTTOM:
768 case GTK_PROGRESS_BOTTOM_TO_TOP:
769 area.width = widget->allocation.width - widget->style->xthickness * 2;
770 area.height = amount;
771 area.x = widget->style->xthickness;
773 area.y = widget->style->ythickness;
774 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
775 area.y = widget->allocation.height - amount - area.y;
783 gtk_paint_box (widget->style,
784 GTK_PROGRESS (pbar)->offscreen_pixmap,
785 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
786 &area, widget, "bar",
787 area.x, area.y, area.width, area.height);
791 gtk_progress_bar_paint_discrete (GtkProgressBar *pbar,
792 GtkProgressBarOrientation orientation)
794 GtkWidget *widget = GTK_WIDGET (pbar);
797 for (i = 0; i <= pbar->in_block; i++)
804 case GTK_PROGRESS_LEFT_TO_RIGHT:
805 case GTK_PROGRESS_RIGHT_TO_LEFT:
806 space = widget->allocation.width - 2 * widget->style->xthickness;
808 area.x = widget->style->xthickness + (i * space) / pbar->blocks;
809 area.y = widget->style->ythickness;
810 area.width = widget->style->xthickness + ((i + 1) * space) / pbar->blocks - area.x;
811 area.height = widget->allocation.height - 2 * widget->style->ythickness;
813 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
814 area.x = widget->allocation.width - area.width - area.x;
817 case GTK_PROGRESS_TOP_TO_BOTTOM:
818 case GTK_PROGRESS_BOTTOM_TO_TOP:
819 space = widget->allocation.height - 2 * widget->style->ythickness;
821 area.x = widget->style->xthickness;
822 area.y = widget->style->ythickness + (i * space) / pbar->blocks;
823 area.width = widget->allocation.width - 2 * widget->style->xthickness;
824 area.height = widget->style->ythickness + ((i + 1) * space) / pbar->blocks - area.y;
826 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
827 area.y = widget->allocation.height - area.height - area.y;
835 gtk_paint_box (widget->style,
836 GTK_PROGRESS (pbar)->offscreen_pixmap,
837 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
838 &area, widget, "bar",
839 area.x, area.y, area.width, area.height);
844 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
847 GtkProgressBarOrientation orientation)
849 GtkProgress *progress = GTK_PROGRESS (pbar);
850 GtkWidget *widget = GTK_WIDGET (pbar);
856 PangoRectangle logical_rect;
857 GdkRectangle prelight_clip, start_clip, end_clip;
858 gfloat text_xalign = progress->x_align;
859 gfloat text_yalign = progress->y_align;
861 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
862 text_xalign = 1.0 - text_xalign;
864 buf = gtk_progress_get_current_text (progress);
866 layout = gtk_widget_create_pango_layout (widget, buf);
867 pango_layout_set_ellipsize (layout, pbar->ellipsize);
869 pango_layout_set_width (layout, widget->allocation.width * PANGO_SCALE);
871 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
873 x = widget->style->xthickness + 1 + text_xalign *
874 (widget->allocation.width - 2 * widget->style->xthickness -
875 2 - logical_rect.width);
877 y = widget->style->ythickness + 1 + text_yalign *
878 (widget->allocation.height - 2 * widget->style->ythickness -
879 2 - logical_rect.height);
881 rect.x = widget->style->xthickness;
882 rect.y = widget->style->ythickness;
883 rect.width = widget->allocation.width - 2 * widget->style->xthickness;
884 rect.height = widget->allocation.height - 2 * widget->style->ythickness;
886 prelight_clip = start_clip = end_clip = rect;
890 case GTK_PROGRESS_LEFT_TO_RIGHT:
892 prelight_clip.x = offset;
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_RIGHT_TO_LEFT:
901 prelight_clip.x = offset;
903 prelight_clip.x = rect.x + rect.width - amount;
904 prelight_clip.width = amount;
905 start_clip.width = prelight_clip.x - start_clip.x;
906 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
907 end_clip.width -= prelight_clip.width + start_clip.width;
910 case GTK_PROGRESS_TOP_TO_BOTTOM:
912 prelight_clip.y = offset;
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;
919 case GTK_PROGRESS_BOTTOM_TO_TOP:
921 prelight_clip.y = offset;
923 prelight_clip.y = rect.y + rect.height - amount;
924 prelight_clip.height = amount;
925 start_clip.height = prelight_clip.y - start_clip.y;
926 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
927 end_clip.height -= prelight_clip.height + start_clip.height;
931 if (start_clip.width > 0 && start_clip.height > 0)
932 gtk_paint_layout (widget->style,
933 progress->offscreen_pixmap,
942 if (end_clip.width > 0 && end_clip.height > 0)
943 gtk_paint_layout (widget->style,
944 progress->offscreen_pixmap,
953 gtk_paint_layout (widget->style,
954 progress->offscreen_pixmap,
963 g_object_unref (layout);
968 gtk_progress_bar_paint (GtkProgress *progress)
970 GtkProgressBar *pbar;
973 GtkProgressBarOrientation orientation;
975 g_return_if_fail (GTK_IS_PROGRESS_BAR (progress));
977 pbar = GTK_PROGRESS_BAR (progress);
978 widget = GTK_WIDGET (progress);
980 orientation = pbar->orientation;
981 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
983 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
984 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
985 else if (pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
986 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
989 if (progress->offscreen_pixmap)
991 gtk_paint_box (widget->style,
992 progress->offscreen_pixmap,
993 GTK_STATE_NORMAL, GTK_SHADOW_IN,
994 NULL, widget, "trough",
996 widget->allocation.width,
997 widget->allocation.height);
999 if (progress->activity_mode)
1001 gtk_progress_bar_paint_activity (pbar, orientation);
1003 if (GTK_PROGRESS (pbar)->show_text)
1008 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
1009 gtk_progress_bar_paint_text (pbar, offset, amount, orientation);
1017 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
1018 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
1019 space = widget->allocation.width - 2 * widget->style->xthickness;
1021 space = widget->allocation.height - 2 * widget->style->ythickness;
1024 gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
1026 if (pbar->bar_style == GTK_PROGRESS_CONTINUOUS)
1028 gtk_progress_bar_paint_continuous (pbar, amount, orientation);
1030 if (GTK_PROGRESS (pbar)->show_text)
1031 gtk_progress_bar_paint_text (pbar, -1, amount, orientation);
1034 gtk_progress_bar_paint_discrete (pbar, orientation);
1037 pbar->dirty = FALSE;
1042 gtk_progress_bar_set_bar_style_internal (GtkProgressBar *pbar,
1043 GtkProgressBarStyle bar_style)
1045 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1047 if (pbar->bar_style != bar_style)
1049 pbar->bar_style = bar_style;
1051 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1052 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1054 g_object_notify (G_OBJECT (pbar), "bar-style");
1059 gtk_progress_bar_set_discrete_blocks_internal (GtkProgressBar *pbar,
1062 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1063 g_return_if_fail (blocks > 1);
1065 if (pbar->blocks != blocks)
1067 pbar->blocks = blocks;
1069 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1070 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1072 g_object_notify (G_OBJECT (pbar), "discrete-blocks");
1077 gtk_progress_bar_set_activity_step_internal (GtkProgressBar *pbar,
1080 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1082 if (pbar->activity_step != step)
1084 pbar->activity_step = step;
1085 g_object_notify (G_OBJECT (pbar), "activity-step");
1090 gtk_progress_bar_set_activity_blocks_internal (GtkProgressBar *pbar,
1093 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1094 g_return_if_fail (blocks > 1);
1096 if (pbar->activity_blocks != blocks)
1098 pbar->activity_blocks = blocks;
1099 g_object_notify (G_OBJECT (pbar), "activity-blocks");
1103 /*******************************************************************/
1106 * gtk_progress_bar_set_fraction:
1107 * @pbar: a #GtkProgressBar
1108 * @fraction: fraction of the task that's been completed
1110 * Causes the progress bar to "fill in" the given fraction
1111 * of the bar. The fraction should be between 0.0 and 1.0,
1116 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1119 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1121 /* If we know the percentage, we don't want activity mode. */
1122 gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), FALSE);
1124 /* We use the deprecated GtkProgress interface internally.
1125 * Once everything's been deprecated for a good long time,
1126 * we can clean up all this code.
1128 gtk_progress_set_percentage (GTK_PROGRESS (pbar), fraction);
1130 g_object_notify (G_OBJECT (pbar), "fraction");
1134 * gtk_progress_bar_pulse:
1135 * @pbar: a #GtkProgressBar
1137 * Indicates that some progress is made, but you don't know how much.
1138 * Causes the progress bar to enter "activity mode," where a block
1139 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1140 * causes the block to move by a little bit (the amount of movement
1141 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1144 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1146 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1148 /* If we don't know the percentage, we must want activity mode. */
1149 gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), TRUE);
1152 gtk_progress_bar_real_update (GTK_PROGRESS (pbar));
1156 * gtk_progress_bar_set_text:
1157 * @pbar: a #GtkProgressBar
1158 * @text: (allow-none): a UTF-8 string, or %NULL
1160 * Causes the given @text to appear superimposed on the progress bar.
1163 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1166 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1168 gtk_progress_set_show_text (GTK_PROGRESS (pbar), text && *text);
1169 gtk_progress_set_format_string (GTK_PROGRESS (pbar), text);
1171 /* We don't support formats in this interface, but turn
1172 * them back on for NULL, which should put us back to
1173 * the initial state.
1175 GTK_PROGRESS (pbar)->use_text_format = (text == NULL);
1177 g_object_notify (G_OBJECT (pbar), "text");
1181 * gtk_progress_bar_set_pulse_step:
1182 * @pbar: a #GtkProgressBar
1183 * @fraction: fraction between 0.0 and 1.0
1185 * Sets the fraction of total progress bar length to move the
1186 * bouncing block for each call to gtk_progress_bar_pulse().
1189 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1192 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1194 pbar->pulse_fraction = fraction;
1196 g_object_notify (G_OBJECT (pbar), "pulse-step");
1200 gtk_progress_bar_update (GtkProgressBar *pbar,
1203 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1205 /* Use of gtk_progress_bar_update() is deprecated !
1206 * Use gtk_progress_bar_set_percentage ()
1209 gtk_progress_set_percentage (GTK_PROGRESS (pbar), percentage);
1213 * gtk_progress_bar_set_orientation:
1214 * @pbar: a #GtkProgressBar
1215 * @orientation: orientation of the progress bar
1217 * Causes the progress bar to switch to a different orientation
1218 * (left-to-right, right-to-left, top-to-bottom, or bottom-to-top).
1221 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1222 GtkProgressBarOrientation orientation)
1224 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1226 if (pbar->orientation != orientation)
1228 pbar->orientation = orientation;
1230 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1231 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1233 g_object_notify (G_OBJECT (pbar), "orientation");
1238 * gtk_progress_bar_get_text:
1239 * @pbar: a #GtkProgressBar
1241 * Retrieves the text displayed superimposed on the progress bar,
1242 * if any, otherwise %NULL. The return value is a reference
1243 * to the text, not a copy of it, so will become invalid
1244 * if you change the text in the progress bar.
1246 * Return value: text, or %NULL; this string is owned by the widget
1247 * and should not be modified or freed.
1249 G_CONST_RETURN gchar*
1250 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1252 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1254 if (GTK_PROGRESS (pbar)->use_text_format)
1257 return GTK_PROGRESS (pbar)->format;
1261 * gtk_progress_bar_get_fraction:
1262 * @pbar: a #GtkProgressBar
1264 * Returns the current fraction of the task that's been completed.
1266 * Return value: a fraction from 0.0 to 1.0
1269 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1271 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1273 return gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
1277 * gtk_progress_bar_get_pulse_step:
1278 * @pbar: a #GtkProgressBar
1280 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1282 * Return value: a fraction from 0.0 to 1.0
1285 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1287 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1289 return pbar->pulse_fraction;
1293 * gtk_progress_bar_get_orientation:
1294 * @pbar: a #GtkProgressBar
1296 * Retrieves the current progress bar orientation.
1298 * Return value: orientation of the progress bar
1300 GtkProgressBarOrientation
1301 gtk_progress_bar_get_orientation (GtkProgressBar *pbar)
1303 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1305 return pbar->orientation;
1309 gtk_progress_bar_set_bar_style (GtkProgressBar *pbar,
1310 GtkProgressBarStyle bar_style)
1312 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1314 gtk_progress_bar_set_bar_style_internal (pbar, bar_style);
1318 gtk_progress_bar_set_discrete_blocks (GtkProgressBar *pbar,
1321 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1322 g_return_if_fail (blocks > 1);
1324 gtk_progress_bar_set_discrete_blocks_internal (pbar, blocks);
1328 gtk_progress_bar_set_activity_step (GtkProgressBar *pbar,
1331 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1333 gtk_progress_bar_set_activity_step_internal (pbar, step);
1337 gtk_progress_bar_set_activity_blocks (GtkProgressBar *pbar,
1340 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1341 g_return_if_fail (blocks > 1);
1343 gtk_progress_bar_set_activity_blocks_internal (pbar, blocks);
1347 * gtk_progress_bar_set_ellipsize:
1348 * @pbar: a #GtkProgressBar
1349 * @mode: a #PangoEllipsizeMode
1351 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1352 * if there is not enough space to render the entire string.
1357 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1358 PangoEllipsizeMode mode)
1360 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1361 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1362 mode <= PANGO_ELLIPSIZE_END);
1364 if ((PangoEllipsizeMode)pbar->ellipsize != mode)
1366 pbar->ellipsize = mode;
1368 g_object_notify (G_OBJECT (pbar), "ellipsize");
1369 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1374 * gtk_progress_bar_get_ellipsize:
1375 * @pbar: a #GtkProgressBar
1377 * Returns the ellipsizing position of the progressbar.
1378 * See gtk_progress_bar_set_ellipsize().
1380 * Return value: #PangoEllipsizeMode
1385 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1387 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1389 return pbar->ellipsize;
1392 #include "gtkaliasdef.c"