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