]> Pileus Git - ~andy/gtk/blob - gtk/gtkprogressbar.c
GtkProgressBar: Move public members to private structure
[~andy/gtk] / gtk / gtkprogressbar.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 #include "config.h"
28
29 #include <string.h>
30
31 #include "gtkprogressbar.h"
32 #include "gtkprivate.h"
33 #include "gtkintl.h"
34
35
36 #define MIN_HORIZONTAL_BAR_WIDTH   150
37 #define MIN_HORIZONTAL_BAR_HEIGHT  20
38 #define MIN_VERTICAL_BAR_WIDTH     22
39 #define MIN_VERTICAL_BAR_HEIGHT    80
40
41
42 struct _GtkProgressBarPriv
43 {
44   GtkProgressBarOrientation orientation;
45
46   GdkPixmap     *offscreen_pixmap;
47
48   gchar         *text;
49
50   gdouble        fraction;
51   gdouble        pulse_fraction;
52
53   guint          blocks;
54   gint           in_block;
55
56   gint           activity_pos;
57   guint          activity_blocks;
58   guint          activity_step;
59
60   guint          activity_dir  : 1;
61   guint          activity_mode : 1;
62   guint          dirty         : 1;
63   guint          ellipsize     : 3;
64   guint          show_text     : 1;
65 };
66
67 enum {
68   PROP_0,
69   PROP_FRACTION,
70   PROP_PULSE_STEP,
71   PROP_ORIENTATION,
72   PROP_TEXT,
73   PROP_SHOW_TEXT,
74   PROP_ELLIPSIZE
75 };
76
77 static void gtk_progress_bar_set_property  (GObject             *object,
78                                             guint                prop_id,
79                                             const GValue        *value,
80                                             GParamSpec          *pspec);
81 static void gtk_progress_bar_get_property  (GObject             *object,
82                                             guint                prop_id,
83                                             GValue              *value,
84                                             GParamSpec          *pspec);
85 static gboolean gtk_progress_bar_expose    (GtkWidget           *widget,
86                                             GdkEventExpose      *event);
87 static void gtk_progress_bar_size_request  (GtkWidget           *widget,
88                                             GtkRequisition      *requisition);
89 static void gtk_progress_bar_size_allocate (GtkWidget           *widget,
90                                             GtkAllocation       *allocation);
91 static void gtk_progress_bar_style_set     (GtkWidget           *widget,
92                                             GtkStyle            *previous);
93 static void gtk_progress_bar_real_update   (GtkProgressBar      *progress);
94 static void gtk_progress_bar_paint         (GtkProgressBar      *progress);
95 static void gtk_progress_bar_act_mode_enter (GtkProgressBar     *progress);
96 static void gtk_progress_bar_realize       (GtkWidget           *widget);
97 static void gtk_progress_bar_finalize      (GObject             *object);
98 static void gtk_progress_bar_create_pixmap (GtkProgressBar      *pbar);
99
100
101 G_DEFINE_TYPE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_WIDGET)
102
103 static void
104 gtk_progress_bar_class_init (GtkProgressBarClass *class)
105 {
106   GObjectClass *gobject_class;
107   GtkWidgetClass *widget_class;
108
109   gobject_class = G_OBJECT_CLASS (class);
110   widget_class = (GtkWidgetClass *) class;
111
112   gobject_class->set_property = gtk_progress_bar_set_property;
113   gobject_class->get_property = gtk_progress_bar_get_property;
114   gobject_class->finalize = gtk_progress_bar_finalize;
115
116   widget_class->realize = gtk_progress_bar_realize;
117   widget_class->expose_event = gtk_progress_bar_expose;
118   widget_class->size_request = gtk_progress_bar_size_request;
119   widget_class->size_allocate = gtk_progress_bar_size_allocate;
120   widget_class->style_set = gtk_progress_bar_style_set;
121
122   class->paint = gtk_progress_bar_paint;
123   class->update = gtk_progress_bar_real_update;
124   class->act_mode_enter = gtk_progress_bar_act_mode_enter;
125
126   g_object_class_install_property (gobject_class,
127                                    PROP_ORIENTATION,
128                                    g_param_spec_enum ("orientation",
129                                                       P_("Orientation"),
130                                                       P_("Orientation and growth direction of the progress bar"),
131                                                       GTK_TYPE_PROGRESS_BAR_ORIENTATION,
132                                                       GTK_PROGRESS_LEFT_TO_RIGHT,
133                                                       GTK_PARAM_READWRITE));
134
135   g_object_class_install_property (gobject_class,
136                                    PROP_FRACTION,
137                                    g_param_spec_double ("fraction",
138                                                         P_("Fraction"),
139                                                         P_("The fraction of total work that has been completed"),
140                                                         0.0, 1.0, 0.0,
141                                                         GTK_PARAM_READWRITE));
142
143   g_object_class_install_property (gobject_class,
144                                    PROP_PULSE_STEP,
145                                    g_param_spec_double ("pulse-step",
146                                                         P_("Pulse Step"),
147                                                         P_("The fraction of total progress to move the bouncing block when pulsed"),
148                                                         0.0, 1.0, 0.1,
149                                                         GTK_PARAM_READWRITE));
150
151   g_object_class_install_property (gobject_class,
152                                    PROP_TEXT,
153                                    g_param_spec_string ("text",
154                                                         P_("Text"),
155                                                         P_("Text to be displayed in the progress bar"),
156                                                         NULL,
157                                                         GTK_PARAM_READWRITE));
158
159   g_object_class_install_property (gobject_class,
160                                    PROP_SHOW_TEXT,
161                                    g_param_spec_boolean ("show-text",
162                                                          P_("Show text"),
163                                                          P_("Whether the progress is shown as text."),
164                                                          FALSE,
165                                                          GTK_PARAM_READWRITE));
166
167   /**
168    * GtkProgressBar:ellipsize:
169    *
170    * The preferred place to ellipsize the string, if the progressbar does
171    * not have enough room to display the entire string, specified as a
172    * #PangoEllisizeMode.
173    *
174    * Note that setting this property to a value other than
175    * %PANGO_ELLIPSIZE_NONE has the side-effect that the progressbar requests
176    * only enough space to display the ellipsis "...". Another means to set a
177    * progressbar's width is gtk_widget_set_size_request().
178    *
179    * Since: 2.6
180    */
181   g_object_class_install_property (gobject_class,
182                                    PROP_ELLIPSIZE,
183                                    g_param_spec_enum ("ellipsize",
184                                                       P_("Ellipsize"),
185                                                       P_("The preferred place to ellipsize the string, if the progress bar "
186                                                          "does not have enough room to display the entire string, if at all."),
187                                                       PANGO_TYPE_ELLIPSIZE_MODE,
188                                                       PANGO_ELLIPSIZE_NONE,
189                                                       GTK_PARAM_READWRITE));
190   gtk_widget_class_install_style_property (widget_class,
191                                            g_param_spec_int ("xspacing",
192                                                              P_("XSpacing"),
193                                                              P_("Extra spacing applied to the width of a progress bar."),
194                                                              0, G_MAXINT, 7,
195                                                              G_PARAM_READWRITE));
196   gtk_widget_class_install_style_property (widget_class,
197                                            g_param_spec_int ("yspacing",
198                                                              P_("YSpacing"),
199                                                              P_("Extra spacing applied to the height of a progress bar."),
200                                                              0, G_MAXINT, 7,
201                                                              G_PARAM_READWRITE));
202
203   /**
204    * GtkProgressBar:min-horizontal-bar-width:
205    *
206    * The minimum horizontal width of the progress bar.
207    *
208    * Since: 2.14
209    */
210   gtk_widget_class_install_style_property (widget_class,
211                                            g_param_spec_int ("min-horizontal-bar-width",
212                                                              P_("Min horizontal bar width"),
213                                                              P_("The minimum horizontal width of the progress bar"),
214                                                              1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
215                                                              G_PARAM_READWRITE));
216   /**
217    * GtkProgressBar:min-horizontal-bar-height:
218    *
219    * Minimum horizontal height of the progress bar.
220    *
221    * Since: 2.14
222    */
223   gtk_widget_class_install_style_property (widget_class,
224                                            g_param_spec_int ("min-horizontal-bar-height",
225                                                              P_("Min horizontal bar height"),
226                                                              P_("Minimum horizontal height of the progress bar"),
227                                                              1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
228                                                              G_PARAM_READWRITE));
229   /**
230    * GtkProgressBar:min-vertical-bar-width:
231    *
232    * The minimum vertical width of the progress bar.
233    *
234    * Since: 2.14
235    */
236   gtk_widget_class_install_style_property (widget_class,
237                                            g_param_spec_int ("min-vertical-bar-width",
238                                                              P_("Min vertical bar width"),
239                                                              P_("The minimum vertical width of the progress bar"),
240                                                              1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
241                                                              G_PARAM_READWRITE));
242   /**
243    * GtkProgressBar:min-vertical-bar-height:
244    *
245    * The minimum vertical height of the progress bar.
246    *
247    * Since: 2.14
248    */
249   gtk_widget_class_install_style_property (widget_class,
250                                            g_param_spec_int ("min-vertical-bar-height",
251                                                              P_("Min vertical bar height"),
252                                                              P_("The minimum vertical height of the progress bar"),
253                                                              1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
254                                                              G_PARAM_READWRITE));
255
256   g_type_class_add_private (class, sizeof (GtkProgressBarPriv));
257 }
258
259 static void
260 gtk_progress_bar_init (GtkProgressBar *pbar)
261 {
262   GtkProgressBarPriv *priv;
263
264   pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (pbar,
265                                             GTK_TYPE_PROGRESS_BAR,
266                                             GtkProgressBarPriv);
267   priv = pbar->priv;
268
269   priv->blocks = 10;
270   priv->in_block = -1;
271   priv->orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
272   priv->pulse_fraction = 0.1;
273   priv->activity_pos = 0;
274   priv->activity_dir = 1;
275   priv->activity_step = 3;
276   priv->activity_blocks = 5;
277   priv->ellipsize = PANGO_ELLIPSIZE_NONE;
278   priv->show_text = FALSE;
279
280   priv->text = NULL;
281   priv->fraction = 0.0;
282 }
283
284 static void
285 gtk_progress_bar_realize (GtkWidget *widget)
286 {
287   GdkWindowAttr attributes;
288   gint attributes_mask;
289
290   gtk_widget_set_realized (widget, TRUE);
291
292   attributes.window_type = GDK_WINDOW_CHILD;
293   attributes.x = widget->allocation.x;
294   attributes.y = widget->allocation.y;
295   attributes.width = widget->allocation.width;
296   attributes.height = widget->allocation.height;
297   attributes.wclass = GDK_INPUT_OUTPUT;
298   attributes.visual = gtk_widget_get_visual (widget);
299   attributes.colormap = gtk_widget_get_colormap (widget);
300   attributes.event_mask = gtk_widget_get_events (widget);
301   attributes.event_mask |= GDK_EXPOSURE_MASK;
302
303   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
304   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
305                                    &attributes, attributes_mask);
306   gdk_window_set_user_data (widget->window, widget);
307
308   widget->style = gtk_style_attach (widget->style, widget->window);
309   gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
310
311   gtk_progress_bar_create_pixmap (GTK_PROGRESS_BAR (widget));
312 }
313
314 static void
315 gtk_progress_bar_set_property (GObject      *object,
316                                guint         prop_id,
317                                const GValue *value,
318                                GParamSpec   *pspec)
319 {
320   GtkProgressBar *pbar;
321
322   pbar = GTK_PROGRESS_BAR (object);
323
324   switch (prop_id)
325     {
326     case PROP_ORIENTATION:
327       gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
328       break;
329     case PROP_FRACTION:
330       gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
331       break;
332     case PROP_PULSE_STEP:
333       gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
334       break;
335     case PROP_TEXT:
336       gtk_progress_bar_set_text (pbar, g_value_get_string (value));
337       break;
338     case PROP_SHOW_TEXT:
339       gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
340       break;
341     case PROP_ELLIPSIZE:
342       gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
343       break;
344     default:
345       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346       break;
347     }
348 }
349
350 static void
351 gtk_progress_bar_get_property (GObject      *object,
352                                guint         prop_id,
353                                GValue       *value,
354                                GParamSpec   *pspec)
355 {
356   GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
357   GtkProgressBarPriv* priv = pbar->priv;
358
359   switch (prop_id)
360     {
361     case PROP_ORIENTATION:
362       g_value_set_enum (value, priv->orientation);
363       break;
364     case PROP_FRACTION:
365       g_value_set_double (value, priv->fraction);
366       break;
367     case PROP_PULSE_STEP:
368       g_value_set_double (value, priv->pulse_fraction);
369       break;
370     case PROP_TEXT:
371       g_value_set_string (value, priv->text);
372       break;
373     case PROP_SHOW_TEXT:
374       g_value_set_boolean (value, priv->show_text);
375       break;
376     case PROP_ELLIPSIZE:
377       g_value_set_enum (value, priv->ellipsize);
378       break;
379     default:
380       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381       break;
382     }
383 }
384
385 GtkWidget*
386 gtk_progress_bar_new (void)
387 {
388   GtkWidget *pbar;
389
390   pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
391
392   return pbar;
393 }
394
395 static void
396 gtk_progress_bar_real_update (GtkProgressBar *pbar)
397 {
398   GtkProgressBarPriv *priv;
399   GtkWidget *widget;
400
401   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
402
403   priv = pbar->priv;
404   widget = GTK_WIDGET (pbar);
405
406   if (priv->activity_mode)
407     {
408       guint size;
409
410       /* advance the block */
411       if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
412           priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
413         {
414           /* Update our activity step. */
415           priv->activity_step = widget->allocation.width * priv->pulse_fraction;
416
417           size = MAX (2, widget->allocation.width / priv->activity_blocks);
418
419           if (priv->activity_dir == 0)
420             {
421               priv->activity_pos += priv->activity_step;
422               if (priv->activity_pos + size >= widget->allocation.width - widget->style->xthickness)
423                 {
424                   priv->activity_pos = widget->allocation.width -
425                     widget->style->xthickness - size;
426                   priv->activity_dir = 1;
427                 }
428             }
429           else
430             {
431               priv->activity_pos -= priv->activity_step;
432               if (priv->activity_pos <= widget->style->xthickness)
433                 {
434                   priv->activity_pos = widget->style->xthickness;
435                   priv->activity_dir = 0;
436                 }
437             }
438         }
439       else
440         {
441           /* Update our activity step. */
442           priv->activity_step = widget->allocation.height * priv->pulse_fraction;
443
444           size = MAX (2, widget->allocation.height / priv->activity_blocks);
445
446           if (priv->activity_dir == 0)
447             {
448               priv->activity_pos += priv->activity_step;
449               if (priv->activity_pos + size >= widget->allocation.height - widget->style->ythickness)
450                 {
451                   priv->activity_pos = widget->allocation.height -
452                     widget->style->ythickness - size;
453                   priv->activity_dir = 1;
454                 }
455             }
456           else
457             {
458               priv->activity_pos -= priv->activity_step;
459               if (priv->activity_pos <= widget->style->ythickness)
460                 {
461                   priv->activity_pos = widget->style->ythickness;
462                   priv->activity_dir = 0;
463                 }
464             }
465         }
466     }
467   priv->dirty = TRUE;
468   gtk_widget_queue_draw (widget);
469 }
470
471 static void
472 gtk_progress_bar_finalize (GObject *object)
473 {
474   GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
475   GtkProgressBarPriv *priv = pbar->priv;
476
477   if (priv->offscreen_pixmap)
478     g_object_unref (priv->offscreen_pixmap);
479
480   g_free (priv->text);
481
482   G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
483 }
484
485 static gboolean
486 gtk_progress_bar_expose (GtkWidget      *widget,
487                          GdkEventExpose *event)
488 {
489   GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
490   GtkProgressBarPriv *priv = pbar->priv;
491
492   if (gtk_widget_is_drawable (widget))
493     {
494       if (priv->dirty)
495         gtk_progress_bar_paint (pbar);
496
497       gdk_draw_drawable (widget->window,
498                          widget->style->black_gc,
499                          priv->offscreen_pixmap,
500                          event->area.x, event->area.y,
501                          event->area.x, event->area.y,
502                          event->area.width,
503                          event->area.height);
504     }
505
506   return FALSE;
507 }
508
509 static gchar *
510 get_current_text (GtkProgressBar *pbar)
511 {
512   GtkProgressBarPriv *priv = pbar->priv;
513
514   if (priv->text)
515     return g_strdup (priv->text);
516   else
517     return g_strdup_printf ("%.0f %%", priv->fraction * 100.0);
518 }
519
520 static void
521 gtk_progress_bar_size_request (GtkWidget      *widget,
522                                GtkRequisition *requisition)
523 {
524   GtkProgressBar *pbar;
525   GtkProgressBarPriv *priv;
526   gchar *buf;
527   PangoRectangle logical_rect;
528   PangoLayout *layout;
529   gint width, height;
530   gint xspacing, yspacing;
531   gint min_width, min_height;
532
533   g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
534   g_return_if_fail (requisition != NULL);
535
536   gtk_widget_style_get (widget,
537                         "xspacing", &xspacing,
538                         "yspacing", &yspacing,
539                         NULL);
540
541   pbar = GTK_PROGRESS_BAR (widget);
542   priv = pbar->priv;
543
544   width = 2 * widget->style->xthickness + xspacing;
545   height = 2 * widget->style->ythickness + yspacing;
546
547   if (priv->show_text)
548     {
549       buf = get_current_text (pbar);
550       layout = gtk_widget_create_pango_layout (widget, buf);
551
552       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
553
554       if (priv->ellipsize)
555         {
556           PangoContext *context;
557           PangoFontMetrics *metrics;
558           gint char_width;
559
560           /* The minimum size for ellipsized text is ~ 3 chars */
561           context = pango_layout_get_context (layout);
562           metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context));
563
564           char_width = pango_font_metrics_get_approximate_char_width (metrics);
565           pango_font_metrics_unref (metrics);
566
567           width += PANGO_PIXELS (char_width) * 3;
568         }
569       else
570         width += logical_rect.width;
571
572       height += logical_rect.height;
573
574       g_object_unref (layout);
575       g_free (buf);
576     }
577
578   if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
579       priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
580     gtk_widget_style_get (widget,
581                           "min-horizontal-bar-width", &min_width,
582                           "min-horizontal-bar-height", &min_height,
583                           NULL);
584   else
585     gtk_widget_style_get (widget,
586                           "min-vertical-bar-width", &min_width,
587                           "min-vertical-bar-height", &min_height,
588                           NULL);
589
590   requisition->width = MAX (min_width, width);
591   requisition->height = MAX (min_height, height);
592 }
593
594 static void
595 gtk_progress_bar_size_allocate (GtkWidget     *widget,
596                                 GtkAllocation *allocation)
597 {
598   widget->allocation = *allocation;
599
600   if (gtk_widget_get_realized (widget))
601     {
602       gdk_window_move_resize (widget->window,
603                               allocation->x, allocation->y,
604                               allocation->width, allocation->height);
605
606       gtk_progress_bar_create_pixmap (GTK_PROGRESS_BAR (widget));
607     }
608 }
609
610 static void
611 gtk_progress_bar_create_pixmap (GtkProgressBar *pbar)
612 {
613   GtkProgressBarPriv *priv = pbar->priv;
614   GtkWidget *widget = GTK_WIDGET (pbar);
615
616   if (gtk_widget_get_realized (widget))
617     {
618       if (priv->offscreen_pixmap)
619         g_object_unref (priv->offscreen_pixmap);
620
621       priv->offscreen_pixmap = gdk_pixmap_new (widget->window,
622                                                widget->allocation.width,
623                                                widget->allocation.height,
624                                                -1);
625
626       /* clear the pixmap for transparent themes */
627       gtk_paint_flat_box (widget->style,
628                           priv->offscreen_pixmap,
629                           GTK_STATE_NORMAL,
630                           GTK_SHADOW_NONE,
631                           NULL, widget, "trough", 0, 0, -1, -1);
632
633       GTK_PROGRESS_BAR_GET_CLASS (pbar)->paint (pbar);
634     }
635 }
636
637 static void
638 gtk_progress_bar_style_set (GtkWidget *widget,
639                             GtkStyle  *previous)
640 {
641   GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
642   GtkProgressBarPriv *priv = pbar->priv;
643
644   priv->dirty = TRUE;
645
646   GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->style_set (widget, previous);
647 }
648
649 static void
650 gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
651 {
652   GtkProgressBarPriv *priv = pbar->priv;
653   GtkWidget *widget = GTK_WIDGET (pbar);
654   GtkProgressBarOrientation orientation;
655
656   orientation = priv->orientation;
657   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
658     {
659       if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
660         orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
661       else if (priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
662         orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
663     }
664
665   /* calculate start pos */
666
667   if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
668       orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
669     {
670       if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
671         {
672           priv->activity_pos = widget->style->xthickness;
673           priv->activity_dir = 0;
674         }
675       else
676         {
677           priv->activity_pos = widget->allocation.width -
678             widget->style->xthickness - (widget->allocation.height -
679                 widget->style->ythickness * 2);
680           priv->activity_dir = 1;
681         }
682     }
683   else
684     {
685       if (orientation == GTK_PROGRESS_TOP_TO_BOTTOM)
686         {
687           priv->activity_pos = widget->style->ythickness;
688           priv->activity_dir = 0;
689         }
690       else
691         {
692           priv->activity_pos = widget->allocation.height -
693             widget->style->ythickness - (widget->allocation.width -
694                 widget->style->xthickness * 2);
695           priv->activity_dir = 1;
696         }
697     }
698 }
699
700 static void
701 gtk_progress_bar_get_activity (GtkProgressBar            *pbar,
702                                GtkProgressBarOrientation  orientation,
703                                gint                      *offset,
704                                gint                      *amount)
705 {
706   GtkProgressBarPriv *priv = pbar->priv;
707   GtkWidget *widget = GTK_WIDGET (pbar);
708
709   *offset = priv->activity_pos;
710
711   switch (orientation)
712     {
713     case GTK_PROGRESS_LEFT_TO_RIGHT:
714     case GTK_PROGRESS_RIGHT_TO_LEFT:
715       *amount = MAX (2, widget->allocation.width / priv->activity_blocks);
716       break;
717
718     case GTK_PROGRESS_TOP_TO_BOTTOM:
719     case GTK_PROGRESS_BOTTOM_TO_TOP:
720       *amount = MAX (2, widget->allocation.height / priv->activity_blocks);
721       break;
722     }
723 }
724
725 static void
726 gtk_progress_bar_paint_activity (GtkProgressBar            *pbar,
727                                  GtkProgressBarOrientation  orientation)
728 {
729   GtkProgressBarPriv *priv = pbar->priv;
730   GtkWidget *widget = GTK_WIDGET (pbar);
731   GdkRectangle area;
732
733   switch (orientation)
734     {
735     case GTK_PROGRESS_LEFT_TO_RIGHT:
736     case GTK_PROGRESS_RIGHT_TO_LEFT:
737       gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
738       area.y = widget->style->ythickness;
739       area.height = widget->allocation.height - 2 * widget->style->ythickness;
740       break;
741
742     case GTK_PROGRESS_TOP_TO_BOTTOM:
743     case GTK_PROGRESS_BOTTOM_TO_TOP:
744       gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
745       area.x = widget->style->xthickness;
746       area.width = widget->allocation.width - 2 * widget->style->xthickness;
747       break;
748
749     default:
750       return;
751       break;
752     }
753
754   gtk_paint_box (widget->style,
755                  priv->offscreen_pixmap,
756                  GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
757                  &area, widget, "bar",
758                  area.x, area.y, area.width, area.height);
759 }
760
761 static void
762 gtk_progress_bar_paint_continuous (GtkProgressBar            *pbar,
763                                    gint                       amount,
764                                    GtkProgressBarOrientation  orientation)
765 {
766   GtkProgressBarPriv *priv = pbar->priv;
767   GtkWidget *widget = GTK_WIDGET (pbar);
768   GdkRectangle area;
769
770   if (amount <= 0)
771     return;
772
773   switch (orientation)
774     {
775     case GTK_PROGRESS_LEFT_TO_RIGHT:
776     case GTK_PROGRESS_RIGHT_TO_LEFT:
777       area.width = amount;
778       area.height = widget->allocation.height - widget->style->ythickness * 2;
779       area.y = widget->style->ythickness;
780
781       area.x = widget->style->xthickness;
782       if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
783         area.x = widget->allocation.width - amount - area.x;
784       break;
785
786     case GTK_PROGRESS_TOP_TO_BOTTOM:
787     case GTK_PROGRESS_BOTTOM_TO_TOP:
788       area.width = widget->allocation.width - widget->style->xthickness * 2;
789       area.height = amount;
790       area.x = widget->style->xthickness;
791
792       area.y = widget->style->ythickness;
793       if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
794         area.y = widget->allocation.height - amount - area.y;
795       break;
796
797     default:
798       return;
799       break;
800     }
801
802   gtk_paint_box (widget->style,
803                  priv->offscreen_pixmap,
804                  GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
805                  &area, widget, "bar",
806                  area.x, area.y, area.width, area.height);
807 }
808
809 static void
810 gtk_progress_bar_paint_text (GtkProgressBar            *pbar,
811                              gint                       offset,
812                              gint                       amount,
813                              GtkProgressBarOrientation  orientation)
814 {
815   GtkProgressBarPriv *priv = pbar->priv;
816   GtkWidget *widget = GTK_WIDGET (pbar);
817   gint x;
818   gint y;
819   gchar *buf;
820   GdkRectangle rect;
821   PangoLayout *layout;
822   PangoRectangle logical_rect;
823   GdkRectangle prelight_clip, start_clip, end_clip;
824   gfloat text_xalign = 0.5;
825   gfloat text_yalign = 0.5;
826
827   if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
828     text_xalign = 1.0 - text_xalign;
829
830   buf = get_current_text (pbar);
831
832   layout = gtk_widget_create_pango_layout (widget, buf);
833   pango_layout_set_ellipsize (layout, priv->ellipsize);
834   if (priv->ellipsize)
835     pango_layout_set_width (layout, widget->allocation.width * PANGO_SCALE);
836
837   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
838
839   x = widget->style->xthickness + 1 + text_xalign *
840       (widget->allocation.width - 2 * widget->style->xthickness -
841        2 - logical_rect.width);
842
843   y = widget->style->ythickness + 1 + text_yalign *
844       (widget->allocation.height - 2 * widget->style->ythickness -
845        2 - logical_rect.height);
846
847   rect.x = widget->style->xthickness;
848   rect.y = widget->style->ythickness;
849   rect.width = widget->allocation.width - 2 * widget->style->xthickness;
850   rect.height = widget->allocation.height - 2 * widget->style->ythickness;
851
852   prelight_clip = start_clip = end_clip = rect;
853
854   switch (orientation)
855     {
856     case GTK_PROGRESS_LEFT_TO_RIGHT:
857       if (offset != -1)
858         prelight_clip.x = offset;
859       prelight_clip.width = amount;
860       start_clip.width = prelight_clip.x - start_clip.x;
861       end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
862       end_clip.width -= prelight_clip.width + start_clip.width;
863       break;
864
865     case GTK_PROGRESS_RIGHT_TO_LEFT:
866       if (offset != -1)
867         prelight_clip.x = offset;
868       else
869         prelight_clip.x = rect.x + rect.width - amount;
870       prelight_clip.width = amount;
871       start_clip.width = prelight_clip.x - start_clip.x;
872       end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
873       end_clip.width -= prelight_clip.width + start_clip.width;
874       break;
875
876     case GTK_PROGRESS_TOP_TO_BOTTOM:
877       if (offset != -1)
878         prelight_clip.y = offset;
879       prelight_clip.height = amount;
880       start_clip.height = prelight_clip.y - start_clip.y;
881       end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
882       end_clip.height -= prelight_clip.height + start_clip.height;
883       break;
884
885     case GTK_PROGRESS_BOTTOM_TO_TOP:
886       if (offset != -1)
887         prelight_clip.y = offset;
888       else
889         prelight_clip.y = rect.y + rect.height - amount;
890       prelight_clip.height = amount;
891       start_clip.height = prelight_clip.y - start_clip.y;
892       end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
893       end_clip.height -= prelight_clip.height + start_clip.height;
894       break;
895     }
896
897   if (start_clip.width > 0 && start_clip.height > 0)
898     gtk_paint_layout (widget->style,
899                       priv->offscreen_pixmap,
900                       GTK_STATE_NORMAL,
901                       FALSE,
902                       &start_clip,
903                       widget,
904                       "progressbar",
905                       x, y,
906                       layout);
907
908   if (end_clip.width > 0 && end_clip.height > 0)
909     gtk_paint_layout (widget->style,
910                       priv->offscreen_pixmap,
911                       GTK_STATE_NORMAL,
912                       FALSE,
913                       &end_clip,
914                       widget,
915                       "progressbar",
916                       x, y,
917                       layout);
918
919   gtk_paint_layout (widget->style,
920                     priv->offscreen_pixmap,
921                     GTK_STATE_PRELIGHT,
922                     FALSE,
923                     &prelight_clip,
924                     widget,
925                     "progressbar",
926                     x, y,
927                     layout);
928
929   g_object_unref (layout);
930   g_free (buf);
931 }
932
933 static void
934 gtk_progress_bar_paint (GtkProgressBar *pbar)
935 {
936   GtkProgressBarPriv *priv = pbar->priv;
937   GtkWidget *widget = GTK_WIDGET (pbar);
938   GtkProgressBarOrientation orientation;
939
940   orientation = priv->orientation;
941   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
942     {
943       if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
944         orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
945       else if (priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
946         orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
947     }
948
949   if (priv->offscreen_pixmap)
950     {
951       gtk_paint_box (widget->style,
952                      priv->offscreen_pixmap,
953                      GTK_STATE_NORMAL, GTK_SHADOW_IN,
954                      NULL, widget, "trough",
955                      0, 0,
956                      widget->allocation.width,
957                      widget->allocation.height);
958
959       if (priv->activity_mode)
960         {
961           gtk_progress_bar_paint_activity (pbar, orientation);
962
963           if (priv->show_text)
964             {
965               gint offset;
966               gint amount;
967
968               gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
969               gtk_progress_bar_paint_text (pbar, offset, amount, orientation);
970             }
971         }
972       else
973         {
974           gint amount;
975           gint space;
976
977           if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
978               orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
979             space = widget->allocation.width - 2 * widget->style->xthickness;
980           else
981             space = widget->allocation.height - 2 * widget->style->ythickness;
982
983           amount = space * gtk_progress_bar_get_fraction (pbar);
984
985           gtk_progress_bar_paint_continuous (pbar, amount, orientation);
986
987           if (priv->show_text)
988             gtk_progress_bar_paint_text (pbar, -1, amount, orientation);
989         }
990
991       priv->dirty = FALSE;
992     }
993 }
994
995 static void
996 gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
997                                     gboolean        activity_mode)
998 {
999   GtkProgressBarPriv *priv = pbar->priv;
1000
1001   activity_mode = !!activity_mode;
1002
1003   if (priv->activity_mode != activity_mode)
1004     {
1005       priv->activity_mode = activity_mode;
1006
1007       if (priv->activity_mode)
1008         GTK_PROGRESS_BAR_GET_CLASS (pbar)->act_mode_enter (pbar);
1009
1010       if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1011         gtk_widget_queue_resize (GTK_WIDGET (pbar));
1012     }
1013 }
1014
1015 /**
1016  * gtk_progress_bar_set_fraction:
1017  * @pbar: a #GtkProgressBar
1018  * @fraction: fraction of the task that's been completed
1019  *
1020  * Causes the progress bar to "fill in" the given fraction
1021  * of the bar. The fraction should be between 0.0 and 1.0,
1022  * inclusive.
1023  *
1024  **/
1025 void
1026 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1027                                gdouble         fraction)
1028 {
1029   GtkProgressBarPriv* priv;
1030
1031   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1032
1033   priv = pbar->priv;
1034
1035   priv->fraction = fraction;
1036   gtk_progress_bar_set_activity_mode (pbar, FALSE);
1037   gtk_progress_bar_real_update (pbar);
1038
1039   g_object_notify (G_OBJECT (pbar), "fraction");
1040 }
1041
1042 /**
1043  * gtk_progress_bar_pulse:
1044  * @pbar: a #GtkProgressBar
1045  *
1046  * Indicates that some progress is made, but you don't know how much.
1047  * Causes the progress bar to enter "activity mode," where a block
1048  * bounces back and forth. Each call to gtk_progress_bar_pulse()
1049  * causes the block to move by a little bit (the amount of movement
1050  * per pulse is determined by gtk_progress_bar_set_pulse_step()).
1051  **/
1052 void
1053 gtk_progress_bar_pulse (GtkProgressBar *pbar)
1054 {
1055   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1056
1057   gtk_progress_bar_set_activity_mode (pbar, TRUE);
1058   gtk_progress_bar_real_update (pbar);
1059 }
1060
1061 /**
1062  * gtk_progress_bar_set_text:
1063  * @pbar: a #GtkProgressBar
1064  * @text: (allow-none): a UTF-8 string, or %NULL
1065  *
1066  * Causes the given @text to appear superimposed on the progress bar.
1067  **/
1068 void
1069 gtk_progress_bar_set_text (GtkProgressBar *pbar,
1070                            const gchar    *text)
1071 {
1072   GtkProgressBarPriv *priv;
1073
1074   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1075
1076   priv = pbar->priv;
1077
1078   g_free (priv->text);
1079   priv->text = text && *text ? g_strdup (text) : NULL;
1080
1081   if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1082     gtk_widget_queue_resize (GTK_WIDGET (pbar));
1083
1084   g_object_notify (G_OBJECT (pbar), "text");
1085 }
1086
1087 /**
1088  * gtk_progress_bar_set_show_text:
1089  * @pbar: a #GtkProgressBar
1090  * @show_text: whether to show superimposed text
1091  *
1092  * Sets whether the progressbar will show text superimposed
1093  * over the bar. The shown text is either the value of
1094  * the #GtkProgressBar::text property or, if that is %NULL,
1095  * the #GtkProgressBar::fraction value, as a percentage.
1096  *
1097  * Since: 3.0
1098  */
1099 void
1100 gtk_progress_bar_set_show_text (GtkProgressBar *pbar,
1101                                 gboolean        show_text)
1102 {
1103   GtkProgressBarPriv *priv;
1104
1105   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1106
1107   priv = pbar->priv;
1108
1109   show_text = !!show_text;
1110
1111   if (priv->show_text != show_text)
1112     {
1113       priv->show_text = show_text;
1114
1115       if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1116         gtk_widget_queue_resize (GTK_WIDGET (pbar));
1117
1118       g_object_notify (G_OBJECT (pbar), "show-text");
1119     }
1120 }
1121
1122 /**
1123  * gtk_progress_bar_get_show_text:
1124  * @pbar: a #GtkProgressBar
1125  *
1126  * Gets the value of the #GtkProgressBar::show-text property.
1127  * See gtk_progress_bar_set_show_text().
1128  *
1129  * Returns: %TRUE if text is shown in the progress bar
1130  *
1131  * Since: 3.0
1132  */
1133 gboolean
1134 gtk_progress_bar_get_show_text (GtkProgressBar *pbar)
1135 {
1136   g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), FALSE);
1137
1138   return pbar->priv->show_text;
1139 }
1140
1141 /**
1142  * gtk_progress_bar_set_pulse_step:
1143  * @pbar: a #GtkProgressBar
1144  * @fraction: fraction between 0.0 and 1.0
1145  *
1146  * Sets the fraction of total progress bar length to move the
1147  * bouncing block for each call to gtk_progress_bar_pulse().
1148  **/
1149 void
1150 gtk_progress_bar_set_pulse_step (GtkProgressBar *pbar,
1151                                  gdouble         fraction)
1152 {
1153   GtkProgressBarPriv *priv;
1154
1155   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1156
1157   priv = pbar->priv;
1158
1159   priv->pulse_fraction = fraction;
1160
1161   g_object_notify (G_OBJECT (pbar), "pulse-step");
1162 }
1163
1164 /**
1165  * gtk_progress_bar_set_orientation:
1166  * @pbar: a #GtkProgressBar
1167  * @orientation: orientation of the progress bar
1168  *
1169  * Causes the progress bar to switch to a different orientation
1170  * (left-to-right, right-to-left, top-to-bottom, or bottom-to-top).
1171  **/
1172 void
1173 gtk_progress_bar_set_orientation (GtkProgressBar           *pbar,
1174                                   GtkProgressBarOrientation orientation)
1175 {
1176   GtkProgressBarPriv *priv;
1177
1178   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1179
1180   priv = pbar->priv;
1181
1182   if (priv->orientation != orientation)
1183     {
1184       priv->orientation = orientation;
1185
1186       if (gtk_widget_is_drawable (GTK_WIDGET (pbar)))
1187         gtk_widget_queue_resize (GTK_WIDGET (pbar));
1188
1189       g_object_notify (G_OBJECT (pbar), "orientation");
1190     }
1191 }
1192
1193 /**
1194  * gtk_progress_bar_get_text:
1195  * @pbar: a #GtkProgressBar
1196  *
1197  * Retrieves the text displayed superimposed on the progress bar,
1198  * if any, otherwise %NULL. The return value is a reference
1199  * to the text, not a copy of it, so will become invalid
1200  * if you change the text in the progress bar.
1201  *
1202  * Return value: text, or %NULL; this string is owned by the widget
1203  * and should not be modified or freed.
1204  **/
1205 G_CONST_RETURN gchar*
1206 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1207 {
1208   g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1209
1210   return pbar->priv->text;
1211 }
1212
1213 /**
1214  * gtk_progress_bar_get_fraction:
1215  * @pbar: a #GtkProgressBar
1216  *
1217  * Returns the current fraction of the task that's been completed.
1218  *
1219  * Return value: a fraction from 0.0 to 1.0
1220  **/
1221 gdouble
1222 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1223 {
1224   g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1225
1226   return pbar->priv->fraction;
1227 }
1228
1229 /**
1230  * gtk_progress_bar_get_pulse_step:
1231  * @pbar: a #GtkProgressBar
1232  *
1233  * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1234  *
1235  * Return value: a fraction from 0.0 to 1.0
1236  **/
1237 gdouble
1238 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1239 {
1240   g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1241
1242   return pbar->priv->pulse_fraction;
1243 }
1244
1245 /**
1246  * gtk_progress_bar_get_orientation:
1247  * @pbar: a #GtkProgressBar
1248  *
1249  * Retrieves the current progress bar orientation.
1250  *
1251  * Return value: orientation of the progress bar
1252  **/
1253 GtkProgressBarOrientation
1254 gtk_progress_bar_get_orientation (GtkProgressBar *pbar)
1255 {
1256   g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1257
1258   return pbar->priv->orientation;
1259 }
1260
1261 /**
1262  * gtk_progress_bar_set_ellipsize:
1263  * @pbar: a #GtkProgressBar
1264  * @mode: a #PangoEllipsizeMode
1265  *
1266  * Sets the mode used to ellipsize (add an ellipsis: "...") the text
1267  * if there is not enough space to render the entire string.
1268  *
1269  * Since: 2.6
1270  **/
1271 void
1272 gtk_progress_bar_set_ellipsize (GtkProgressBar     *pbar,
1273                                 PangoEllipsizeMode  mode)
1274 {
1275   GtkProgressBarPriv *priv;
1276
1277   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1278   g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE &&
1279                     mode <= PANGO_ELLIPSIZE_END);
1280
1281   priv = pbar->priv;
1282
1283   if ((PangoEllipsizeMode)priv->ellipsize != mode)
1284     {
1285       priv->ellipsize = mode;
1286
1287       g_object_notify (G_OBJECT (pbar), "ellipsize");
1288       gtk_widget_queue_resize (GTK_WIDGET (pbar));
1289     }
1290 }
1291
1292 /**
1293  * gtk_progress_bar_get_ellipsize:
1294  * @pbar: a #GtkProgressBar
1295  *
1296  * Returns the ellipsizing position of the progressbar.
1297  * See gtk_progress_bar_set_ellipsize().
1298  *
1299  * Return value: #PangoEllipsizeMode
1300  *
1301  * Since: 2.6
1302  **/
1303 PangoEllipsizeMode
1304 gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
1305 {
1306   g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), PANGO_ELLIPSIZE_NONE);
1307
1308   return pbar->priv->ellipsize;
1309 }