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_real_update (GtkProgress *progress);
80 static void gtk_progress_bar_paint (GtkProgress *progress);
81 static void gtk_progress_bar_act_mode_enter (GtkProgress *progress);
83 static void gtk_progress_bar_set_bar_style_internal (GtkProgressBar *pbar,
84 GtkProgressBarStyle style);
85 static void gtk_progress_bar_set_discrete_blocks_internal (GtkProgressBar *pbar,
87 static void gtk_progress_bar_set_activity_step_internal (GtkProgressBar *pbar,
89 static void gtk_progress_bar_set_activity_blocks_internal (GtkProgressBar *pbar,
93 G_DEFINE_TYPE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_PROGRESS)
96 gtk_progress_bar_class_init (GtkProgressBarClass *class)
98 GObjectClass *gobject_class;
99 GtkWidgetClass *widget_class;
100 GtkProgressClass *progress_class;
102 gobject_class = G_OBJECT_CLASS (class);
103 widget_class = (GtkWidgetClass *) class;
104 progress_class = (GtkProgressClass *) class;
106 gobject_class->set_property = gtk_progress_bar_set_property;
107 gobject_class->get_property = gtk_progress_bar_get_property;
109 widget_class->expose_event = gtk_progress_bar_expose;
110 widget_class->size_request = gtk_progress_bar_size_request;
112 progress_class->paint = gtk_progress_bar_paint;
113 progress_class->update = gtk_progress_bar_real_update;
114 progress_class->act_mode_enter = gtk_progress_bar_act_mode_enter;
116 g_object_class_install_property (gobject_class,
118 g_param_spec_object ("adjustment",
120 P_("The GtkAdjustment connected to the progress bar (Deprecated)"),
122 GTK_PARAM_READWRITE));
124 g_object_class_install_property (gobject_class,
126 g_param_spec_enum ("orientation",
128 P_("Orientation and growth direction of the progress bar"),
129 GTK_TYPE_PROGRESS_BAR_ORIENTATION,
130 GTK_PROGRESS_LEFT_TO_RIGHT,
131 GTK_PARAM_READWRITE));
133 g_object_class_install_property (gobject_class,
135 g_param_spec_enum ("bar-style",
137 P_("Specifies the visual style of the bar in percentage mode (Deprecated)"),
138 GTK_TYPE_PROGRESS_BAR_STYLE,
139 GTK_PROGRESS_CONTINUOUS,
140 GTK_PARAM_READWRITE));
142 g_object_class_install_property (gobject_class,
144 g_param_spec_uint ("activity-step",
146 P_("The increment used for each iteration in activity mode (Deprecated)"),
148 GTK_PARAM_READWRITE));
150 g_object_class_install_property (gobject_class,
151 PROP_ACTIVITY_BLOCKS,
152 g_param_spec_uint ("activity-blocks",
153 P_("Activity Blocks"),
154 P_("The number of blocks which can fit in the progress bar area in activity mode (Deprecated)"),
156 GTK_PARAM_READWRITE));
158 g_object_class_install_property (gobject_class,
159 PROP_DISCRETE_BLOCKS,
160 g_param_spec_uint ("discrete-blocks",
161 P_("Discrete Blocks"),
162 P_("The number of discrete blocks in a progress bar (when shown in the discrete style)"),
164 GTK_PARAM_READWRITE));
166 g_object_class_install_property (gobject_class,
168 g_param_spec_double ("fraction",
170 P_("The fraction of total work that has been completed"),
172 GTK_PARAM_READWRITE));
174 g_object_class_install_property (gobject_class,
176 g_param_spec_double ("pulse-step",
178 P_("The fraction of total progress to move the bouncing block when pulsed"),
180 GTK_PARAM_READWRITE));
182 g_object_class_install_property (gobject_class,
184 g_param_spec_string ("text",
186 P_("Text to be displayed in the progress bar"),
188 GTK_PARAM_READWRITE));
191 * GtkProgressBar:ellipsize:
193 * The preferred place to ellipsize the string, if the progressbar does
194 * not have enough room to display the entire string, specified as a
195 * #PangoEllisizeMode.
197 * Note that setting this property to a value other than
198 * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
199 * only enough space to display the ellipsis "...". Another means to set a
200 * progressbar's width is gtk_widget_set_size_request().
204 g_object_class_install_property (gobject_class,
206 g_param_spec_enum ("ellipsize",
208 P_("The preferred place to ellipsize the string, if the progress bar "
209 "does not have enough room to display the entire string, if at all."),
210 PANGO_TYPE_ELLIPSIZE_MODE,
211 PANGO_ELLIPSIZE_NONE,
212 GTK_PARAM_READWRITE));
213 gtk_widget_class_install_style_property (widget_class,
214 g_param_spec_int ("xspacing",
216 P_("Extra spacing applied to the width of a progress bar."),
219 gtk_widget_class_install_style_property (widget_class,
220 g_param_spec_int ("yspacing",
222 "Extra spacing applied to the height of a progress bar.",
228 gtk_progress_bar_init (GtkProgressBar *pbar)
230 pbar->bar_style = GTK_PROGRESS_CONTINUOUS;
233 pbar->orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
234 pbar->pulse_fraction = 0.1;
235 pbar->activity_pos = 0;
236 pbar->activity_dir = 1;
237 pbar->activity_step = 3;
238 pbar->activity_blocks = 5;
239 pbar->ellipsize = PANGO_ELLIPSIZE_NONE;
243 gtk_progress_bar_set_property (GObject *object,
248 GtkProgressBar *pbar;
250 pbar = GTK_PROGRESS_BAR (object);
254 case PROP_ADJUSTMENT:
255 gtk_progress_set_adjustment (GTK_PROGRESS (pbar),
256 GTK_ADJUSTMENT (g_value_get_object (value)));
258 case PROP_ORIENTATION:
259 gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
262 gtk_progress_bar_set_bar_style_internal (pbar, g_value_get_enum (value));
264 case PROP_ACTIVITY_STEP:
265 gtk_progress_bar_set_activity_step_internal (pbar, g_value_get_uint (value));
267 case PROP_ACTIVITY_BLOCKS:
268 gtk_progress_bar_set_activity_blocks_internal (pbar, g_value_get_uint (value));
270 case PROP_DISCRETE_BLOCKS:
271 gtk_progress_bar_set_discrete_blocks_internal (pbar, g_value_get_uint (value));
274 gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
276 case PROP_PULSE_STEP:
277 gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
280 gtk_progress_bar_set_text (pbar, g_value_get_string (value));
283 gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
286 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292 gtk_progress_bar_get_property (GObject *object,
297 GtkProgressBar *pbar;
299 pbar = GTK_PROGRESS_BAR (object);
303 case PROP_ADJUSTMENT:
304 g_value_set_object (value, GTK_PROGRESS (pbar)->adjustment);
306 case PROP_ORIENTATION:
307 g_value_set_enum (value, pbar->orientation);
310 g_value_set_enum (value, pbar->bar_style);
312 case PROP_ACTIVITY_STEP:
313 g_value_set_uint (value, pbar->activity_step);
315 case PROP_ACTIVITY_BLOCKS:
316 g_value_set_uint (value, pbar->activity_blocks);
318 case PROP_DISCRETE_BLOCKS:
319 g_value_set_uint (value, pbar->blocks);
322 g_value_set_double (value, gtk_progress_get_current_percentage (GTK_PROGRESS (pbar)));
324 case PROP_PULSE_STEP:
325 g_value_set_double (value, pbar->pulse_fraction);
328 g_value_set_string (value, gtk_progress_bar_get_text (pbar));
331 g_value_set_enum (value, pbar->ellipsize);
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
340 gtk_progress_bar_new (void)
344 pbar = gtk_widget_new (GTK_TYPE_PROGRESS_BAR, NULL);
350 gtk_progress_bar_new_with_adjustment (GtkAdjustment *adjustment)
354 g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
356 pbar = gtk_widget_new (GTK_TYPE_PROGRESS_BAR,
357 "adjustment", adjustment,
364 gtk_progress_bar_real_update (GtkProgress *progress)
366 GtkProgressBar *pbar;
369 g_return_if_fail (GTK_IS_PROGRESS (progress));
371 pbar = GTK_PROGRESS_BAR (progress);
372 widget = GTK_WIDGET (progress);
374 if (pbar->bar_style == GTK_PROGRESS_CONTINUOUS ||
375 GTK_PROGRESS (pbar)->activity_mode)
377 if (GTK_PROGRESS (pbar)->activity_mode)
381 /* advance the block */
383 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
384 pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
386 /* Update our activity step. */
388 pbar->activity_step = widget->allocation.width * pbar->pulse_fraction;
390 size = MAX (2, widget->allocation.width / pbar->activity_blocks);
392 if (pbar->activity_dir == 0)
394 pbar->activity_pos += pbar->activity_step;
395 if (pbar->activity_pos + size >=
396 widget->allocation.width -
397 widget->style->xthickness)
399 pbar->activity_pos = widget->allocation.width -
400 widget->style->xthickness - size;
401 pbar->activity_dir = 1;
406 pbar->activity_pos -= pbar->activity_step;
407 if (pbar->activity_pos <= widget->style->xthickness)
409 pbar->activity_pos = widget->style->xthickness;
410 pbar->activity_dir = 0;
416 /* Update our activity step. */
418 pbar->activity_step = widget->allocation.height * pbar->pulse_fraction;
420 size = MAX (2, widget->allocation.height / pbar->activity_blocks);
422 if (pbar->activity_dir == 0)
424 pbar->activity_pos += pbar->activity_step;
425 if (pbar->activity_pos + size >=
426 widget->allocation.height -
427 widget->style->ythickness)
429 pbar->activity_pos = widget->allocation.height -
430 widget->style->ythickness - size;
431 pbar->activity_dir = 1;
436 pbar->activity_pos -= pbar->activity_step;
437 if (pbar->activity_pos <= widget->style->ythickness)
439 pbar->activity_pos = widget->style->ythickness;
440 pbar->activity_dir = 0;
446 gtk_widget_queue_draw (GTK_WIDGET (progress));
452 in_block = -1 + (gint)(gtk_progress_get_current_percentage (progress) *
453 (gdouble)pbar->blocks);
455 if (pbar->in_block != in_block)
457 pbar->in_block = in_block;
459 gtk_widget_queue_draw (GTK_WIDGET (progress));
465 gtk_progress_bar_expose (GtkWidget *widget,
466 GdkEventExpose *event)
468 GtkProgressBar *pbar;
470 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (widget), FALSE);
472 pbar = GTK_PROGRESS_BAR (widget);
474 if (GTK_WIDGET_DRAWABLE (widget) && pbar->dirty)
475 gtk_progress_bar_paint (GTK_PROGRESS (pbar));
477 return GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->expose_event (widget, event);
481 gtk_progress_bar_size_request (GtkWidget *widget,
482 GtkRequisition *requisition)
484 GtkProgress *progress;
485 GtkProgressBar *pbar;
487 PangoRectangle logical_rect;
490 gint xspacing, yspacing;
492 g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
493 g_return_if_fail (requisition != NULL);
495 gtk_widget_style_get (widget,
496 "xspacing", &xspacing,
497 "yspacing", &yspacing,
500 progress = GTK_PROGRESS (widget);
501 pbar = GTK_PROGRESS_BAR (widget);
503 width = 2 * widget->style->xthickness + xspacing;
504 height = 2 * widget->style->ythickness + yspacing;
506 if (progress->show_text && pbar->bar_style != GTK_PROGRESS_DISCRETE)
508 if (!progress->adjustment)
509 gtk_progress_set_adjustment (progress, NULL);
511 buf = gtk_progress_get_text_from_value (progress, progress->adjustment->upper);
513 layout = gtk_widget_create_pango_layout (widget, buf);
515 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
519 PangoContext *context;
520 PangoFontMetrics *metrics;
523 /* The minimum size for ellipsized text is ~ 3 chars */
524 context = pango_layout_get_context (layout);
525 metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
527 char_width = pango_font_metrics_get_approximate_char_width (metrics);
528 pango_font_metrics_unref (metrics);
530 width += PANGO_PIXELS (char_width) * 3;
533 width += logical_rect.width;
535 height += logical_rect.height;
537 g_object_unref (layout);
541 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
542 pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
544 requisition->width = MAX (MIN_HORIZONTAL_BAR_WIDTH, width);
545 requisition->height = MAX (MIN_HORIZONTAL_BAR_HEIGHT, height);
549 requisition->width = MAX (MIN_VERTICAL_BAR_WIDTH, width);
550 requisition->height = MAX (MIN_VERTICAL_BAR_HEIGHT, height);
555 gtk_progress_bar_act_mode_enter (GtkProgress *progress)
557 GtkProgressBar *pbar;
559 GtkProgressBarOrientation orientation;
561 pbar = GTK_PROGRESS_BAR (progress);
562 widget = GTK_WIDGET (progress);
564 orientation = pbar->orientation;
565 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
567 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
568 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
569 else if (pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
570 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
573 /* calculate start pos */
575 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
576 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
578 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
580 pbar->activity_pos = widget->style->xthickness;
581 pbar->activity_dir = 0;
585 pbar->activity_pos = widget->allocation.width -
586 widget->style->xthickness - (widget->allocation.height -
587 widget->style->ythickness * 2);
588 pbar->activity_dir = 1;
593 if (orientation == GTK_PROGRESS_TOP_TO_BOTTOM)
595 pbar->activity_pos = widget->style->ythickness;
596 pbar->activity_dir = 0;
600 pbar->activity_pos = widget->allocation.height -
601 widget->style->ythickness - (widget->allocation.width -
602 widget->style->xthickness * 2);
603 pbar->activity_dir = 1;
609 gtk_progress_bar_get_activity (GtkProgressBar *pbar,
610 GtkProgressBarOrientation orientation,
614 GtkWidget *widget = GTK_WIDGET (pbar);
616 *offset = pbar->activity_pos;
620 case GTK_PROGRESS_LEFT_TO_RIGHT:
621 case GTK_PROGRESS_RIGHT_TO_LEFT:
622 *amount = MAX (2, widget->allocation.width / pbar->activity_blocks);
625 case GTK_PROGRESS_TOP_TO_BOTTOM:
626 case GTK_PROGRESS_BOTTOM_TO_TOP:
627 *amount = MAX (2, widget->allocation.height / pbar->activity_blocks);
633 gtk_progress_bar_paint_activity (GtkProgressBar *pbar,
634 GtkProgressBarOrientation orientation)
636 GtkWidget *widget = GTK_WIDGET (pbar);
637 GtkProgress *progress = GTK_PROGRESS (pbar);
642 case GTK_PROGRESS_LEFT_TO_RIGHT:
643 case GTK_PROGRESS_RIGHT_TO_LEFT:
644 gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
645 area.y = widget->style->ythickness;
646 area.height = widget->allocation.height - 2 * widget->style->ythickness;
649 case GTK_PROGRESS_TOP_TO_BOTTOM:
650 case GTK_PROGRESS_BOTTOM_TO_TOP:
651 gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
652 area.x = widget->style->xthickness;
653 area.width = widget->allocation.width - 2 * widget->style->xthickness;
661 gtk_paint_box (widget->style,
662 progress->offscreen_pixmap,
663 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
664 &area, widget, "bar",
665 area.x, area.y, area.width, area.height);
669 gtk_progress_bar_paint_continuous (GtkProgressBar *pbar,
671 GtkProgressBarOrientation orientation)
674 GtkWidget *widget = GTK_WIDGET (pbar);
681 case GTK_PROGRESS_LEFT_TO_RIGHT:
682 case GTK_PROGRESS_RIGHT_TO_LEFT:
684 area.height = widget->allocation.height - widget->style->ythickness * 2;
685 area.y = widget->style->ythickness;
687 area.x = widget->style->xthickness;
688 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
689 area.x = widget->allocation.width - amount - area.x;
692 case GTK_PROGRESS_TOP_TO_BOTTOM:
693 case GTK_PROGRESS_BOTTOM_TO_TOP:
694 area.width = widget->allocation.width - widget->style->xthickness * 2;
695 area.height = amount;
696 area.x = widget->style->xthickness;
698 area.y = widget->style->ythickness;
699 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
700 area.y = widget->allocation.height - amount - area.y;
708 gtk_paint_box (widget->style,
709 GTK_PROGRESS (pbar)->offscreen_pixmap,
710 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
711 &area, widget, "bar",
712 area.x, area.y, area.width, area.height);
716 gtk_progress_bar_paint_discrete (GtkProgressBar *pbar,
717 GtkProgressBarOrientation orientation)
719 GtkWidget *widget = GTK_WIDGET (pbar);
722 for (i = 0; i <= pbar->in_block; i++)
729 case GTK_PROGRESS_LEFT_TO_RIGHT:
730 case GTK_PROGRESS_RIGHT_TO_LEFT:
731 space = widget->allocation.width - 2 * widget->style->xthickness;
733 area.x = widget->style->xthickness + (i * space) / pbar->blocks;
734 area.y = widget->style->ythickness;
735 area.width = widget->style->xthickness + ((i + 1) * space) / pbar->blocks - area.x;
736 area.height = widget->allocation.height - 2 * widget->style->ythickness;
738 if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
739 area.x = widget->allocation.width - area.width - area.x;
742 case GTK_PROGRESS_TOP_TO_BOTTOM:
743 case GTK_PROGRESS_BOTTOM_TO_TOP:
744 space = widget->allocation.height - 2 * widget->style->ythickness;
746 area.x = widget->style->xthickness;
747 area.y = widget->style->ythickness + (i * space) / pbar->blocks;
748 area.width = widget->allocation.width - 2 * widget->style->xthickness;
749 area.height = widget->style->ythickness + ((i + 1) * space) / pbar->blocks - area.y;
751 if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
752 area.y = widget->allocation.height - area.height - area.y;
760 gtk_paint_box (widget->style,
761 GTK_PROGRESS (pbar)->offscreen_pixmap,
762 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
763 &area, widget, "bar",
764 area.x, area.y, area.width, area.height);
769 gtk_progress_bar_paint_text (GtkProgressBar *pbar,
772 GtkProgressBarOrientation orientation)
774 GtkProgress *progress = GTK_PROGRESS (pbar);
775 GtkWidget *widget = GTK_WIDGET (pbar);
781 PangoRectangle logical_rect;
782 GdkRectangle prelight_clip, normal_clip;
783 gfloat text_xalign = progress->x_align;
784 gfloat text_yalign = progress->y_align;
786 if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
787 text_xalign = 1.0 - text_xalign;
789 buf = gtk_progress_get_current_text (progress);
791 layout = gtk_widget_create_pango_layout (widget, buf);
792 pango_layout_set_ellipsize (layout, pbar->ellipsize);
794 pango_layout_set_width (layout, widget->allocation.width * PANGO_SCALE);
796 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
798 x = widget->style->xthickness + 1 + text_xalign *
799 (widget->allocation.width - 2 * widget->style->xthickness -
800 2 - logical_rect.width);
802 y = widget->style->ythickness + 1 + text_yalign *
803 (widget->allocation.height - 2 * widget->style->ythickness -
804 2 - logical_rect.height);
806 rect.x = widget->style->xthickness;
807 rect.y = widget->style->ythickness;
808 rect.width = widget->allocation.width - 2 * widget->style->xthickness;
809 rect.height = widget->allocation.height - 2 * widget->style->ythickness;
811 prelight_clip = normal_clip = rect;
815 case GTK_PROGRESS_LEFT_TO_RIGHT:
817 prelight_clip.x = offset;
818 prelight_clip.width = amount;
821 case GTK_PROGRESS_RIGHT_TO_LEFT:
823 prelight_clip.x = offset;
825 prelight_clip.x = rect.x + rect.width - amount;
826 prelight_clip.width = amount;
829 case GTK_PROGRESS_TOP_TO_BOTTOM:
831 prelight_clip.y = offset;
832 prelight_clip.height = amount;
835 case GTK_PROGRESS_BOTTOM_TO_TOP:
837 prelight_clip.y = offset;
839 prelight_clip.y = rect.y + rect.height - amount;
840 prelight_clip.height = amount;
844 gtk_paint_layout (widget->style,
845 progress->offscreen_pixmap,
854 gtk_paint_layout (widget->style,
855 progress->offscreen_pixmap,
864 g_object_unref (layout);
869 gtk_progress_bar_paint (GtkProgress *progress)
871 GtkProgressBar *pbar;
874 GtkProgressBarOrientation orientation;
876 g_return_if_fail (GTK_IS_PROGRESS_BAR (progress));
878 pbar = GTK_PROGRESS_BAR (progress);
879 widget = GTK_WIDGET (progress);
881 orientation = pbar->orientation;
882 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
884 if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
885 orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
886 else if (pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
887 orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
890 if (progress->offscreen_pixmap)
892 gtk_paint_box (widget->style,
893 progress->offscreen_pixmap,
894 GTK_STATE_NORMAL, GTK_SHADOW_IN,
895 NULL, widget, "trough",
897 widget->allocation.width,
898 widget->allocation.height);
900 if (progress->activity_mode)
902 gtk_progress_bar_paint_activity (pbar, orientation);
904 if (GTK_PROGRESS (pbar)->show_text)
909 gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
910 gtk_progress_bar_paint_text (pbar, offset, amount, orientation);
918 if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
919 orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
920 space = widget->allocation.width - 2 * widget->style->xthickness;
922 space = widget->allocation.height - 2 * widget->style->ythickness;
925 gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
927 if (pbar->bar_style == GTK_PROGRESS_CONTINUOUS)
929 gtk_progress_bar_paint_continuous (pbar, amount, orientation);
931 if (GTK_PROGRESS (pbar)->show_text)
932 gtk_progress_bar_paint_text (pbar, -1, amount, orientation);
935 gtk_progress_bar_paint_discrete (pbar, orientation);
943 gtk_progress_bar_set_bar_style_internal (GtkProgressBar *pbar,
944 GtkProgressBarStyle bar_style)
946 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
948 if (pbar->bar_style != bar_style)
950 pbar->bar_style = bar_style;
952 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
953 gtk_widget_queue_resize (GTK_WIDGET (pbar));
955 g_object_notify (G_OBJECT (pbar), "bar-style");
960 gtk_progress_bar_set_discrete_blocks_internal (GtkProgressBar *pbar,
963 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
964 g_return_if_fail (blocks > 1);
966 if (pbar->blocks != blocks)
968 pbar->blocks = blocks;
970 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
971 gtk_widget_queue_resize (GTK_WIDGET (pbar));
973 g_object_notify (G_OBJECT (pbar), "discrete-blocks");
978 gtk_progress_bar_set_activity_step_internal (GtkProgressBar *pbar,
981 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
983 if (pbar->activity_step != step)
985 pbar->activity_step = step;
986 g_object_notify (G_OBJECT (pbar), "activity-step");
991 gtk_progress_bar_set_activity_blocks_internal (GtkProgressBar *pbar,
994 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
995 g_return_if_fail (blocks > 1);
997 if (pbar->activity_blocks != blocks)
999 pbar->activity_blocks = blocks;
1000 g_object_notify (G_OBJECT (pbar), "activity-blocks");
1004 /*******************************************************************/
1007 * gtk_progress_bar_set_fraction:
1008 * @pbar: a #GtkProgressBar
1009 * @fraction: fraction of the task that's been completed
1011 * Causes the progress bar to "fill in" the given fraction
1012 * of the bar. The fraction should be between 0.0 and 1.0,
1017 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1020 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1022 /* If we know the percentage, we don't want activity mode. */
1023 gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), FALSE);
1025 /* We use the deprecated GtkProgress interface internally.
1026 * Once everything's been deprecated for a good long time,
1027 * we can clean up all this code.
1029 gtk_progress_set_percentage (GTK_PROGRESS (pbar), fraction);
1031 g_object_notify (G_OBJECT (pbar), "fraction");
1035 * gtk_progress_bar_pulse:
1036 * @pbar: a #GtkProgressBar
1038 * Indicates that some progress is made, but you don't know how much.
1039 * Causes the progress bar to enter "activity mode," where a block
1040 * bounces back and forth. Each call to gtk_progress_bar_pulse()
1041 * causes the block to move by a little bit (the amount of movement
1042 * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1045 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1047 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1049 /* If we don't know the percentage, we must want activity mode. */
1050 gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), TRUE);
1053 gtk_progress_bar_real_update (GTK_PROGRESS (pbar));
1057 * gtk_progress_bar_set_text:
1058 * @pbar: a #GtkProgressBar
1059 * @text: a UTF-8 string, or %NULL
1061 * Causes the given @text to appear superimposed on the progress bar.
1064 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1067 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1069 gtk_progress_set_show_text (GTK_PROGRESS (pbar), text && *text);
1070 gtk_progress_set_format_string (GTK_PROGRESS (pbar), text);
1072 /* We don't support formats in this interface, but turn
1073 * them back on for NULL, which should put us back to
1074 * the initial state.
1076 GTK_PROGRESS (pbar)->use_text_format = (text == NULL);
1078 g_object_notify (G_OBJECT (pbar), "text");
1082 * gtk_progress_bar_set_pulse_step:
1083 * @pbar: a #GtkProgressBar
1084 * @fraction: fraction between 0.0 and 1.0
1086 * Sets the fraction of total progress bar length to move the
1087 * bouncing block for each call to gtk_progress_bar_pulse().
1090 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1093 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1095 pbar->pulse_fraction = fraction;
1097 g_object_notify (G_OBJECT (pbar), "pulse-step");
1101 gtk_progress_bar_update (GtkProgressBar *pbar,
1104 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1106 /* Use of gtk_progress_bar_update() is deprecated !
1107 * Use gtk_progress_bar_set_percentage ()
1110 gtk_progress_set_percentage (GTK_PROGRESS (pbar), percentage);
1114 * gtk_progress_bar_set_orientation:
1115 * @pbar: a #GtkProgressBar
1116 * @orientation: orientation of the progress bar
1118 * Causes the progress bar to switch to a different orientation
1119 * (left-to-right, right-to-left, top-to-bottom, or bottom-to-top).
1122 gtk_progress_bar_set_orientation (GtkProgressBar *pbar,
1123 GtkProgressBarOrientation orientation)
1125 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1127 if (pbar->orientation != orientation)
1129 pbar->orientation = orientation;
1131 if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1132 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1134 g_object_notify (G_OBJECT (pbar), "orientation");
1139 * gtk_progress_bar_get_text:
1140 * @pbar: a #GtkProgressBar
1142 * Retrieves the text displayed superimposed on the progress bar,
1143 * if any, otherwise %NULL. The return value is a reference
1144 * to the text, not a copy of it, so will become invalid
1145 * if you change the text in the progress bar.
1147 * Return value: text, or %NULL; this string is owned by the widget
1148 * and should not be modified or freed.
1150 G_CONST_RETURN gchar*
1151 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1153 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1155 if (GTK_PROGRESS (pbar)->use_text_format)
1158 return GTK_PROGRESS (pbar)->format;
1162 * gtk_progress_bar_get_fraction:
1163 * @pbar: a #GtkProgressBar
1165 * Returns the current fraction of the task that's been completed.
1167 * Return value: a fraction from 0.0 to 1.0
1170 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1172 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1174 return gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
1178 * gtk_progress_bar_get_pulse_step:
1179 * @pbar: a #GtkProgressBar
1181 * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1183 * Return value: a fraction from 0.0 to 1.0
1186 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1188 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1190 return pbar->pulse_fraction;
1194 * gtk_progress_bar_get_orientation:
1195 * @pbar: a #GtkProgressBar
1197 * Retrieves the current progress bar orientation.
1199 * Return value: orientation of the progress bar
1201 GtkProgressBarOrientation
1202 gtk_progress_bar_get_orientation (GtkProgressBar *pbar)
1204 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1206 return pbar->orientation;
1210 gtk_progress_bar_set_bar_style (GtkProgressBar *pbar,
1211 GtkProgressBarStyle bar_style)
1213 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1215 gtk_progress_bar_set_bar_style_internal (pbar, bar_style);
1219 gtk_progress_bar_set_discrete_blocks (GtkProgressBar *pbar,
1222 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1223 g_return_if_fail (blocks > 1);
1225 gtk_progress_bar_set_discrete_blocks_internal (pbar, blocks);
1229 gtk_progress_bar_set_activity_step (GtkProgressBar *pbar,
1232 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1234 gtk_progress_bar_set_activity_step_internal (pbar, step);
1238 gtk_progress_bar_set_activity_blocks (GtkProgressBar *pbar,
1241 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1242 g_return_if_fail (blocks > 1);
1244 gtk_progress_bar_set_activity_blocks_internal (pbar, blocks);
1248 * gtk_progress_bar_set_ellipsize:
1249 * @pbar: a #GtkProgressBar
1250 * @mode: a #PangoEllipsizeMode
1252 * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1253 * if there is not enough space to render the entire string.
1258 gtk_progress_bar_set_ellipsize (GtkProgressBar *pbar,
1259 PangoEllipsizeMode mode)
1261 g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1262 g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1263 mode <= PANGO_ELLIPSIZE_END);
1265 if ((PangoEllipsizeMode)pbar->ellipsize != mode)
1267 pbar->ellipsize = mode;
1269 g_object_notify (G_OBJECT (pbar), "ellipsize");
1270 gtk_widget_queue_resize (GTK_WIDGET (pbar));
1275 * gtk_progress_bar_get_ellipsize:
1276 * @pbar: a #GtkProgressBar
1278 * Returns the ellipsizing position of the progressbar.
1279 * See gtk_progress_bar_set_ellipsize().
1281 * Return value: #PangoEllipsizeMode
1286 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1288 g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1290 return pbar->ellipsize;
1293 #define __GTK_PROGRESS_BAR_C__
1294 #include "gtkaliasdef.c"