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