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