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/.
37 #include "gtkprogressbar.h"
38 #include "gtkprivate.h"
43 #define MIN_HORIZONTAL_BAR_WIDTH 150
44 #define MIN_HORIZONTAL_BAR_HEIGHT 20
45 #define MIN_VERTICAL_BAR_WIDTH 22
46 #define MIN_VERTICAL_BAR_HEIGHT 80
47 #define MAX_TEXT_LENGTH 80
67 static void gtk_progress_bar_set_property (GObject *object,
71 static void gtk_progress_bar_get_property (GObject *object,
75 static gboolean gtk_progress_bar_expose (GtkWidget *widget,
76 GdkEventExpose *event);
77 static void gtk_progress_bar_size_request (GtkWidget *widget,
78 GtkRequisition *requisition);
79 static void gtk_progress_bar_style_set (GtkWidget *widget,
81 static void gtk_progress_bar_real_update (GtkProgress *progress);
82 static void gtk_progress_bar_paint (GtkProgress *progress);
83 static void gtk_progress_bar_act_mode_enter (GtkProgress *progress);
85 static void gtk_progress_bar_set_bar_style_internal (GtkProgressBar *pbar,
86 GtkProgressBarStyle style);
87 static void gtk_progress_bar_set_discrete_blocks_internal (GtkProgressBar *pbar,
89 static void gtk_progress_bar_set_activity_step_internal (GtkProgressBar *pbar,
91 static void gtk_progress_bar_set_activity_blocks_internal (GtkProgressBar *pbar,
95 G_DEFINE_TYPE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_PROGRESS)
98 gtk_progress_bar_class_init (GtkProgressBarClass *class)
100 GObjectClass *gobject_class;
101 GtkWidgetClass *widget_class;
102 GtkProgressClass *progress_class;
104 gobject_class = G_OBJECT_CLASS (class);
105 widget_class = (GtkWidgetClass *) class;
106 progress_class = (GtkProgressClass *) class;
108 gobject_class->set_property = gtk_progress_bar_set_property;
109 gobject_class->get_property = gtk_progress_bar_get_property;
111 widget_class->expose_event = gtk_progress_bar_expose;
112 widget_class->size_request = gtk_progress_bar_size_request;
113 widget_class->style_set = gtk_progress_bar_style_set;
115 progress_class->paint = gtk_progress_bar_paint;
116 progress_class->update = gtk_progress_bar_real_update;
117 progress_class->act_mode_enter = gtk_progress_bar_act_mode_enter;
119 g_object_class_install_property (gobject_class,
121 g_param_spec_object ("adjustment",
123 P_("The GtkAdjustment connected to the progress bar (Deprecated)"),
125 GTK_PARAM_READWRITE));
127 g_object_class_install_property (gobject_class,
129 g_param_spec_enum ("orientation",
131 P_("Orientation and growth direction of the progress bar"),
132 GTK_TYPE_PROGRESS_BAR_ORIENTATION,
133 GTK_PROGRESS_LEFT_TO_RIGHT,
134 GTK_PARAM_READWRITE));
136 g_object_class_install_property (gobject_class,
138 g_param_spec_enum ("bar-style",
140 P_("Specifies the visual style of the bar in percentage mode (Deprecated)"),
141 GTK_TYPE_PROGRESS_BAR_STYLE,
142 GTK_PROGRESS_CONTINUOUS,
143 GTK_PARAM_READWRITE));
145 g_object_class_install_property (gobject_class,
147 g_param_spec_uint ("activity-step",
149 P_("The increment used for each iteration in activity mode (Deprecated)"),
151 GTK_PARAM_READWRITE));
153 g_object_class_install_property (gobject_class,
154 PROP_ACTIVITY_BLOCKS,
155 g_param_spec_uint ("activity-blocks",
156 P_("Activity Blocks"),
157 P_("The number of blocks which can fit in the progress bar area in activity mode (Deprecated)"),
159 GTK_PARAM_READWRITE));
161 g_object_class_install_property (gobject_class,
162 PROP_DISCRETE_BLOCKS,
163 g_param_spec_uint ("discrete-blocks",
164 P_("Discrete Blocks"),
165 P_("The number of discrete blocks in a progress bar (when shown in the discrete style)"),
167 GTK_PARAM_READWRITE));
169 g_object_class_install_property (gobject_class,
171 g_param_spec_double ("fraction",
173 P_("The fraction of total work that has been completed"),
175 GTK_PARAM_READWRITE));
177 g_object_class_install_property (gobject_class,
179 g_param_spec_double ("pulse-step",
181 P_("The fraction of total progress to move the bouncing block when pulsed"),
183 GTK_PARAM_READWRITE));
185 g_object_class_install_property (gobject_class,
187 g_param_spec_string ("text",
189 P_("Text to be displayed in the progress bar"),
191 GTK_PARAM_READWRITE));
194 * GtkProgressBar:ellipsize:
196 * The preferred place to ellipsize the string, if the progressbar does
197 * not have enough room to display the entire string, specified as a
198 * #PangoEllisizeMode.
200 * Note that setting this property to a value other than
201 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
202 * only enough space to display the ellipsis "...". Another means to set a
203 * progressbar's width is gtk_widget_set_size_request().
207 g_object_class_install_property (gobject_class,
209 g_param_spec_enum ("ellipsize",
211 P_("The preferred place to ellipsize the string, if the progress bar "
212 "does not have enough room to display the entire string, if at all."),
213 PANGO_TYPE_ELLIPSIZE_MODE,
214 PANGO_ELLIPSIZE_NONE,
215 GTK_PARAM_READWRITE));
216 gtk_widget_class_install_style_property (widget_class,
217 g_param_spec_int ("xspacing",
219 P_("Extra spacing applied to the width of a progress bar."),
222 gtk_widget_class_install_style_property (widget_class,
223 g_param_spec_int ("yspacing",
225 "Extra spacing applied to the height of a progress bar.",
231 gtk_progress_bar_init (GtkProgressBar *pbar)
233 pbar->bar_style = GTK_PROGRESS_CONTINUOUS;
236 pbar->orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
237 pbar->pulse_fraction = 0.1;
238 pbar->activity_pos = 0;
239 pbar->activity_dir = 1;
240 pbar->activity_step = 3;
241 pbar->activity_blocks = 5;
242 pbar->ellipsize = PANGO_ELLIPSIZE_NONE;
246 gtk_progress_bar_set_property (GObject *object,
251 GtkProgressBar *pbar;
253 pbar = GTK_PROGRESS_BAR (object);
257 case PROP_ADJUSTMENT:
258 gtk_progress_set_adjustment (GTK_PROGRESS (pbar),
259 GTK_ADJUSTMENT (g_value_get_object (value)));
261 case PROP_ORIENTATION:
262 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
265 gtk_progress_bar_set_bar_style_internal (pbar, g_value_get_enum (value));
267 case PROP_ACTIVITY_STEP:
268 gtk_progress_bar_set_activity_step_internal (pbar, g_value_get_uint (value));
270 case PROP_ACTIVITY_BLOCKS:
271 gtk_progress_bar_set_activity_blocks_internal (pbar, g_value_get_uint (value));
273 case PROP_DISCRETE_BLOCKS:
274 gtk_progress_bar_set_discrete_blocks_internal (pbar, g_value_get_uint (value));
277 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
279 case PROP_PULSE_STEP:
280 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
283 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
286 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295 gtk_progress_bar_get_property (GObject *object,
300 GtkProgressBar *pbar;
302 pbar = GTK_PROGRESS_BAR (object);
306 case PROP_ADJUSTMENT:
307 g_value_set_object (value, GTK_PROGRESS (pbar)->adjustment);
309 case PROP_ORIENTATION:
310 g_value_set_enum (value, pbar->orientation);
313 g_value_set_enum (value, pbar->bar_style);
315 case PROP_ACTIVITY_STEP:
316 g_value_set_uint (value, pbar->activity_step);
318 case PROP_ACTIVITY_BLOCKS:
319 g_value_set_uint (value, pbar->activity_blocks);
321 case PROP_DISCRETE_BLOCKS:
322 g_value_set_uint (value, pbar->blocks);
325 g_value_set_double (value, gtk_progress_get_current_percentage (GTK_PROGRESS (pbar)));
327 case PROP_PULSE_STEP:
328 g_value_set_double (value, pbar->pulse_fraction);
331 g_value_set_string (value, gtk_progress_bar_get_text (pbar));
334 g_value_set_enum (value, pbar->ellipsize);
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
343 gtk_progress_bar_new (void)
347 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
353 gtk_progress_bar_new_with_adjustment (GtkAdjustment *adjustment)
357 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
359 pbar = g_object_new (GTK_TYPE_PROGRESS_BAR,
360 "adjustment", adjustment,
367 gtk_progress_bar_real_update (GtkProgress *progress)
369 GtkProgressBar *pbar;
372 g_return_if_fail (GTK_IS_PROGRESS (progress));
374 pbar = GTK_PROGRESS_BAR (progress);
375 widget = GTK_WIDGET (progress);
377 if (pbar->bar_style == GTK_PROGRESS_CONTINUOUS ||
378 GTK_PROGRESS (pbar)->activity_mode)
380 if (GTK_PROGRESS (pbar)->activity_mode)
384 /* advance the block */
386 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
387 pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
389 /* Update our activity step. */
391 pbar->activity_step = widget->allocation.width * pbar->pulse_fraction;
393 size = MAX (2, widget->allocation.width / pbar->activity_blocks);
395 if (pbar->activity_dir == 0)
397 pbar->activity_pos += pbar->activity_step;
398 if (pbar->activity_pos + size >=
399 widget->allocation.width -
400 widget->style->xthickness)
402 pbar->activity_pos = widget->allocation.width -
403 widget->style->xthickness - size;
404 pbar->activity_dir = 1;
409 pbar->activity_pos -= pbar->activity_step;
410 if (pbar->activity_pos <= widget->style->xthickness)
412 pbar->activity_pos = widget->style->xthickness;
413 pbar->activity_dir = 0;
419 /* Update our activity step. */
421 pbar->activity_step = widget->allocation.height * pbar->pulse_fraction;
423 size = MAX (2, widget->allocation.height / pbar->activity_blocks);
425 if (pbar->activity_dir == 0)
427 pbar->activity_pos += pbar->activity_step;
428 if (pbar->activity_pos + size >=
429 widget->allocation.height -
430 widget->style->ythickness)
432 pbar->activity_pos = widget->allocation.height -
433 widget->style->ythickness - size;
434 pbar->activity_dir = 1;
439 pbar->activity_pos -= pbar->activity_step;
440 if (pbar->activity_pos <= widget->style->ythickness)
442 pbar->activity_pos = widget->style->ythickness;
443 pbar->activity_dir = 0;
449 gtk_widget_queue_draw (GTK_WIDGET (progress));
455 in_block = -1 + (gint)(gtk_progress_get_current_percentage (progress) *
456 (gdouble)pbar->blocks);
458 if (pbar->in_block != in_block)
460 pbar->in_block = in_block;
462 gtk_widget_queue_draw (GTK_WIDGET (progress));
468 gtk_progress_bar_expose (GtkWidget *widget,
469 GdkEventExpose *event)
471 GtkProgressBar *pbar;
473 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (widget), FALSE);
475 pbar = GTK_PROGRESS_BAR (widget);
477 if (GTK_WIDGET_DRAWABLE (widget) && pbar->dirty)
478 gtk_progress_bar_paint (GTK_PROGRESS (pbar));
480 return GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->expose_event (widget, event);
484 gtk_progress_bar_size_request (GtkWidget *widget,
485 GtkRequisition *requisition)
487 GtkProgress *progress;
488 GtkProgressBar *pbar;
490 PangoRectangle logical_rect;
493 gint xspacing, yspacing;
495 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
496 g_return_if_fail (requisition != NULL);
498 gtk_widget_style_get (widget,
499 "xspacing", &xspacing,
500 "yspacing", &yspacing,
503 progress = GTK_PROGRESS (widget);
504 pbar = GTK_PROGRESS_BAR (widget);
506 width = 2 * widget->style->xthickness + xspacing;
507 height = 2 * widget->style->ythickness + yspacing;
509 if (progress->show_text && pbar->bar_style != GTK_PROGRESS_DISCRETE)
511 if (!progress->adjustment)
512 gtk_progress_set_adjustment (progress, NULL);
514 buf = gtk_progress_get_text_from_value (progress, progress->adjustment->upper);
516 layout = gtk_widget_create_pango_layout (widget, buf);
518 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
522 PangoContext *context;
523 PangoFontMetrics *metrics;
526 /* The minimum size for ellipsized text is ~ 3 chars */
527 context = pango_layout_get_context (layout);
528 metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
530 char_width = pango_font_metrics_get_approximate_char_width (metrics);
531 pango_font_metrics_unref (metrics);
533 width += PANGO_PIXELS (char_width) * 3;
536 width += logical_rect.width;
538 height += logical_rect.height;
540 g_object_unref (layout);
544 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
545 pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
547 requisition->width = MAX (MIN_HORIZONTAL_BAR_WIDTH, width);
548 requisition->height = MAX (MIN_HORIZONTAL_BAR_HEIGHT, height);
552 requisition->width = MAX (MIN_VERTICAL_BAR_WIDTH, width);
553 requisition->height = MAX (MIN_VERTICAL_BAR_HEIGHT, height);
558 gtk_progress_bar_style_set (GtkWidget *widget,
561 GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
565 GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->style_set (widget, previous);
569 gtk_progress_bar_act_mode_enter (GtkProgress *progress)
571 GtkProgressBar *pbar;
573 GtkProgressBarOrientation orientation;
575 pbar = GTK_PROGRESS_BAR (progress);
576 widget = GTK_WIDGET (progress);
578 orientation = pbar->orientation;
579 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
581 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
582 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
583 else if (pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
584 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
587 /* calculate start pos */
589 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
590 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
592 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
594 pbar->activity_pos = widget->style->xthickness;
595 pbar->activity_dir = 0;
599 pbar->activity_pos = widget->allocation.width -
600 widget->style->xthickness - (widget->allocation.height -
601 widget->style->ythickness * 2);
602 pbar->activity_dir = 1;
607 if (orientation == GTK_PROGRESS_TOP_TO_BOTTOM)
609 pbar->activity_pos = widget->style->ythickness;
610 pbar->activity_dir = 0;
614 pbar->activity_pos = widget->allocation.height -
615 widget->style->ythickness - (widget->allocation.width -
616 widget->style->xthickness * 2);
617 pbar->activity_dir = 1;
623 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
624 GtkProgressBarOrientation orientation,
628 GtkWidget *widget = GTK_WIDGET (pbar);
630 *offset = pbar->activity_pos;
634 case GTK_PROGRESS_LEFT_TO_RIGHT:
635 case GTK_PROGRESS_RIGHT_TO_LEFT:
636 *amount = MAX (2, widget->allocation.width / pbar->activity_blocks);
639 case GTK_PROGRESS_TOP_TO_BOTTOM:
640 case GTK_PROGRESS_BOTTOM_TO_TOP:
641 *amount = MAX (2, widget->allocation.height / pbar->activity_blocks);
647 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
648 GtkProgressBarOrientation orientation)
650 GtkWidget *widget = GTK_WIDGET (pbar);
651 GtkProgress *progress = GTK_PROGRESS (pbar);
656 case GTK_PROGRESS_LEFT_TO_RIGHT:
657 case GTK_PROGRESS_RIGHT_TO_LEFT:
658 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
659 area.y = widget->style->ythickness;
660 area.height = widget->allocation.height - 2 * widget->style->ythickness;
663 case GTK_PROGRESS_TOP_TO_BOTTOM:
664 case GTK_PROGRESS_BOTTOM_TO_TOP:
665 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
666 area.x = widget->style->xthickness;
667 area.width = widget->allocation.width - 2 * widget->style->xthickness;
675 gtk_paint_box (widget->style,
676 progress->offscreen_pixmap,
677 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
678 &area, widget, "bar",
679 area.x, area.y, area.width, area.height);
683 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
685 GtkProgressBarOrientation orientation)
688 GtkWidget *widget = GTK_WIDGET (pbar);
695 case GTK_PROGRESS_LEFT_TO_RIGHT:
696 case GTK_PROGRESS_RIGHT_TO_LEFT:
698 area.height = widget->allocation.height - widget->style->ythickness * 2;
699 area.y = widget->style->ythickness;
701 area.x = widget->style->xthickness;
702 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
703 area.x = widget->allocation.width - amount - area.x;
706 case GTK_PROGRESS_TOP_TO_BOTTOM:
707 case GTK_PROGRESS_BOTTOM_TO_TOP:
708 area.width = widget->allocation.width - widget->style->xthickness * 2;
709 area.height = amount;
710 area.x = widget->style->xthickness;
712 area.y = widget->style->ythickness;
713 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
714 area.y = widget->allocation.height - amount - area.y;
722 gtk_paint_box (widget->style,
723 GTK_PROGRESS (pbar)->offscreen_pixmap,
724 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
725 &area, widget, "bar",
726 area.x, area.y, area.width, area.height);
730 gtk_progress_bar_paint_discrete (GtkProgressBar *pbar,
731 GtkProgressBarOrientation orientation)
733 GtkWidget *widget = GTK_WIDGET (pbar);
736 for (i = 0; i <= pbar->in_block; i++)
743 case GTK_PROGRESS_LEFT_TO_RIGHT:
744 case GTK_PROGRESS_RIGHT_TO_LEFT:
745 space = widget->allocation.width - 2 * widget->style->xthickness;
747 area.x = widget->style->xthickness + (i * space) / pbar->blocks;
748 area.y = widget->style->ythickness;
749 area.width = widget->style->xthickness + ((i + 1) * space) / pbar->blocks - area.x;
750 area.height = widget->allocation.height - 2 * widget->style->ythickness;
752 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
753 area.x = widget->allocation.width - area.width - area.x;
756 case GTK_PROGRESS_TOP_TO_BOTTOM:
757 case GTK_PROGRESS_BOTTOM_TO_TOP:
758 space = widget->allocation.height - 2 * widget->style->ythickness;
760 area.x = widget->style->xthickness;
761 area.y = widget->style->ythickness + (i * space) / pbar->blocks;
762 area.width = widget->allocation.width - 2 * widget->style->xthickness;
763 area.height = widget->style->ythickness + ((i + 1) * space) / pbar->blocks - area.y;
765 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
766 area.y = widget->allocation.height - area.height - area.y;
774 gtk_paint_box (widget->style,
775 GTK_PROGRESS (pbar)->offscreen_pixmap,
776 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
777 &area, widget, "bar",
778 area.x, area.y, area.width, area.height);
783 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
786 GtkProgressBarOrientation orientation)
788 GtkProgress *progress = GTK_PROGRESS (pbar);
789 GtkWidget *widget = GTK_WIDGET (pbar);
795 PangoRectangle logical_rect;
796 GdkRectangle prelight_clip, start_clip, end_clip;
797 gfloat text_xalign = progress->x_align;
798 gfloat text_yalign = progress->y_align;
800 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
801 text_xalign = 1.0 - text_xalign;
803 buf = gtk_progress_get_current_text (progress);
805 layout = gtk_widget_create_pango_layout (widget, buf);
806 pango_layout_set_ellipsize (layout, pbar->ellipsize);
808 pango_layout_set_width (layout, widget->allocation.width * PANGO_SCALE);
810 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
812 x = widget->style->xthickness + 1 + text_xalign *
813 (widget->allocation.width - 2 * widget->style->xthickness -
814 2 - logical_rect.width);
816 y = widget->style->ythickness + 1 + text_yalign *
817 (widget->allocation.height - 2 * widget->style->ythickness -
818 2 - logical_rect.height);
820 rect.x = widget->style->xthickness;
821 rect.y = widget->style->ythickness;
822 rect.width = widget->allocation.width - 2 * widget->style->xthickness;
823 rect.height = widget->allocation.height - 2 * widget->style->ythickness;
825 prelight_clip = start_clip = end_clip = rect;
829 case GTK_PROGRESS_LEFT_TO_RIGHT:
831 prelight_clip.x = offset;
832 prelight_clip.width = amount;
833 start_clip.width = prelight_clip.x - start_clip.x;
834 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
835 end_clip.width -= prelight_clip.width + start_clip.width;
838 case GTK_PROGRESS_RIGHT_TO_LEFT:
840 prelight_clip.x = offset;
842 prelight_clip.x = rect.x + rect.width - amount;
843 prelight_clip.width = amount;
844 start_clip.width = prelight_clip.x - start_clip.x;
845 end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
846 end_clip.width -= prelight_clip.width + start_clip.width;
849 case GTK_PROGRESS_TOP_TO_BOTTOM:
851 prelight_clip.y = offset;
852 prelight_clip.height = amount;
853 start_clip.height = prelight_clip.y - start_clip.y;
854 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
855 end_clip.height -= prelight_clip.height + start_clip.height;
858 case GTK_PROGRESS_BOTTOM_TO_TOP:
860 prelight_clip.y = offset;
862 prelight_clip.y = rect.y + rect.height - amount;
863 prelight_clip.height = amount;
864 start_clip.height = prelight_clip.y - start_clip.y;
865 end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
866 end_clip.height -= prelight_clip.height + start_clip.height;
870 if (start_clip.width > 0 && start_clip.height > 0)
871 gtk_paint_layout (widget->style,
872 progress->offscreen_pixmap,
881 if (end_clip.width > 0 && end_clip.height > 0)
882 gtk_paint_layout (widget->style,
883 progress->offscreen_pixmap,
892 gtk_paint_layout (widget->style,
893 progress->offscreen_pixmap,
902 g_object_unref (layout);
907 gtk_progress_bar_paint (GtkProgress *progress)
909 GtkProgressBar *pbar;
912 GtkProgressBarOrientation orientation;
914 g_return_if_fail (GTK_IS_PROGRESS_BAR (progress));
916 pbar = GTK_PROGRESS_BAR (progress);
917 widget = GTK_WIDGET (progress);
919 orientation = pbar->orientation;
920 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
922 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
923 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
924 else if (pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
925 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
928 if (progress->offscreen_pixmap)
930 gtk_paint_box (widget->style,
931 progress->offscreen_pixmap,
932 GTK_STATE_NORMAL, GTK_SHADOW_IN,
933 NULL, widget, "trough",
935 widget->allocation.width,
936 widget->allocation.height);
938 if (progress->activity_mode)
940 gtk_progress_bar_paint_activity (pbar, orientation);
942 if (GTK_PROGRESS (pbar)->show_text)
947 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
948 gtk_progress_bar_paint_text (pbar, offset, amount, orientation);
956 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
957 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
958 space = widget->allocation.width - 2 * widget->style->xthickness;
960 space = widget->allocation.height - 2 * widget->style->ythickness;
963 gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
965 if (pbar->bar_style == GTK_PROGRESS_CONTINUOUS)
967 gtk_progress_bar_paint_continuous (pbar, amount, orientation);
969 if (GTK_PROGRESS (pbar)->show_text)
970 gtk_progress_bar_paint_text (pbar, -1, amount, orientation);
973 gtk_progress_bar_paint_discrete (pbar, orientation);
981 gtk_progress_bar_set_bar_style_internal (GtkProgressBar *pbar,
982 GtkProgressBarStyle bar_style)
984 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
986 if (pbar->bar_style != bar_style)
988 pbar->bar_style = bar_style;
990 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
991 gtk_widget_queue_resize (GTK_WIDGET (pbar));
993 g_object_notify (G_OBJECT (pbar), "bar-style");
998 gtk_progress_bar_set_discrete_blocks_internal (GtkProgressBar *pbar,
1001 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1002 g_return_if_fail (blocks > 1);
1004 if (pbar->blocks != blocks)
1006 pbar->blocks = blocks;
1008 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1009 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1011 g_object_notify (G_OBJECT (pbar), "discrete-blocks");
1016 gtk_progress_bar_set_activity_step_internal (GtkProgressBar *pbar,
1019 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1021 if (pbar->activity_step != step)
1023 pbar->activity_step = step;
1024 g_object_notify (G_OBJECT (pbar), "activity-step");
1029 gtk_progress_bar_set_activity_blocks_internal (GtkProgressBar *pbar,
1032 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1033 g_return_if_fail (blocks > 1);
1035 if (pbar->activity_blocks != blocks)
1037 pbar->activity_blocks = blocks;
1038 g_object_notify (G_OBJECT (pbar), "activity-blocks");
1042 /*******************************************************************/
1045 * gtk_progress_bar_set_fraction:
1046 * @pbar: a #GtkProgressBar
1047 * @fraction: fraction of the task that's been completed
1049 * Causes the progress bar to "fill in" the given fraction
1050 * of the bar. The fraction should be between 0.0 and 1.0,
1055 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1058 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1060 /* If we know the percentage, we don't want activity mode. */
1061 gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), FALSE);
1063 /* We use the deprecated GtkProgress interface internally.
1064 * Once everything's been deprecated for a good long time,
1065 * we can clean up all this code.
1067 gtk_progress_set_percentage (GTK_PROGRESS (pbar), fraction);
1069 g_object_notify (G_OBJECT (pbar), "fraction");
1073 * gtk_progress_bar_pulse:
1074 * @pbar: a #GtkProgressBar
1076 * Indicates that some progress is made, but you don't know how much.
1077 * Causes the progress bar to enter "activity mode," where a block
1078 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1079 * causes the block to move by a little bit (the amount of movement
1080 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1083 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1085 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1087 /* If we don't know the percentage, we must want activity mode. */
1088 gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), TRUE);
1091 gtk_progress_bar_real_update (GTK_PROGRESS (pbar));
1095 * gtk_progress_bar_set_text:
1096 * @pbar: a #GtkProgressBar
1097 * @text: a UTF-8 string, or %NULL
1099 * Causes the given @text to appear superimposed on the progress bar.
1102 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1105 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1107 gtk_progress_set_show_text (GTK_PROGRESS (pbar), text && *text);
1108 gtk_progress_set_format_string (GTK_PROGRESS (pbar), text);
1110 /* We don't support formats in this interface, but turn
1111 * them back on for NULL, which should put us back to
1112 * the initial state.
1114 GTK_PROGRESS (pbar)->use_text_format = (text == NULL);
1116 g_object_notify (G_OBJECT (pbar), "text");
1120 * gtk_progress_bar_set_pulse_step:
1121 * @pbar: a #GtkProgressBar
1122 * @fraction: fraction between 0.0 and 1.0
1124 * Sets the fraction of total progress bar length to move the
1125 * bouncing block for each call to gtk_progress_bar_pulse().
1128 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1131 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1133 pbar->pulse_fraction = fraction;
1135 g_object_notify (G_OBJECT (pbar), "pulse-step");
1139 gtk_progress_bar_update (GtkProgressBar *pbar,
1142 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1144 /* Use of gtk_progress_bar_update() is deprecated !
1145 * Use gtk_progress_bar_set_percentage ()
1148 gtk_progress_set_percentage (GTK_PROGRESS (pbar), percentage);
1152 * gtk_progress_bar_set_orientation:
1153 * @pbar: a #GtkProgressBar
1154 * @orientation: orientation of the progress bar
1156 * Causes the progress bar to switch to a different orientation
1157 * (left-to-right, right-to-left, top-to-bottom, or bottom-to-top).
1160 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1161 GtkProgressBarOrientation orientation)
1163 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1165 if (pbar->orientation != orientation)
1167 pbar->orientation = orientation;
1169 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1170 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1172 g_object_notify (G_OBJECT (pbar), "orientation");
1177 * gtk_progress_bar_get_text:
1178 * @pbar: a #GtkProgressBar
1180 * Retrieves the text displayed superimposed on the progress bar,
1181 * if any, otherwise %NULL. The return value is a reference
1182 * to the text, not a copy of it, so will become invalid
1183 * if you change the text in the progress bar.
1185 * Return value: text, or %NULL; this string is owned by the widget
1186 * and should not be modified or freed.
1188 G_CONST_RETURN gchar*
1189 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1191 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1193 if (GTK_PROGRESS (pbar)->use_text_format)
1196 return GTK_PROGRESS (pbar)->format;
1200 * gtk_progress_bar_get_fraction:
1201 * @pbar: a #GtkProgressBar
1203 * Returns the current fraction of the task that's been completed.
1205 * Return value: a fraction from 0.0 to 1.0
1208 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1210 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1212 return gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
1216 * gtk_progress_bar_get_pulse_step:
1217 * @pbar: a #GtkProgressBar
1219 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1221 * Return value: a fraction from 0.0 to 1.0
1224 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1226 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1228 return pbar->pulse_fraction;
1232 * gtk_progress_bar_get_orientation:
1233 * @pbar: a #GtkProgressBar
1235 * Retrieves the current progress bar orientation.
1237 * Return value: orientation of the progress bar
1239 GtkProgressBarOrientation
1240 gtk_progress_bar_get_orientation (GtkProgressBar *pbar)
1242 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1244 return pbar->orientation;
1248 gtk_progress_bar_set_bar_style (GtkProgressBar *pbar,
1249 GtkProgressBarStyle bar_style)
1251 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1253 gtk_progress_bar_set_bar_style_internal (pbar, bar_style);
1257 gtk_progress_bar_set_discrete_blocks (GtkProgressBar *pbar,
1260 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1261 g_return_if_fail (blocks > 1);
1263 gtk_progress_bar_set_discrete_blocks_internal (pbar, blocks);
1267 gtk_progress_bar_set_activity_step (GtkProgressBar *pbar,
1270 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1272 gtk_progress_bar_set_activity_step_internal (pbar, step);
1276 gtk_progress_bar_set_activity_blocks (GtkProgressBar *pbar,
1279 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1280 g_return_if_fail (blocks > 1);
1282 gtk_progress_bar_set_activity_blocks_internal (pbar, blocks);
1286 * gtk_progress_bar_set_ellipsize:
1287 * @pbar: a #GtkProgressBar
1288 * @mode: a #PangoEllipsizeMode
1290 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1291 * if there is not enough space to render the entire string.
1296 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1297 PangoEllipsizeMode mode)
1299 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1300 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1301 mode <= PANGO_ELLIPSIZE_END);
1303 if ((PangoEllipsizeMode)pbar->ellipsize != mode)
1305 pbar->ellipsize = mode;
1307 g_object_notify (G_OBJECT (pbar), "ellipsize");
1308 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1313 * gtk_progress_bar_get_ellipsize:
1314 * @pbar: a #GtkProgressBar
1316 * Returns the ellipsizing position of the progressbar.
1317 * See gtk_progress_bar_set_ellipsize().
1319 * Return value: #PangoEllipsizeMode
1324 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1326 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1328 return pbar->ellipsize;
1331 #define __GTK_PROGRESS_BAR_C__
1332 #include "gtkaliasdef.c"