]> Pileus Git - ~andy/gtk/blob - gtk/gtkprogressbar.c
don't offset one pixel when calculating the x position of the text.
[~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_continuous (GtkProgressBar            *pbar,
623                                    gint                       amount,
624                                    GtkProgressBarOrientation  orientation)
625 {
626   GtkWidget *widget = GTK_WIDGET (pbar);
627   gint x, y, w, h;
628
629   if (amount <= 0)
630     return;
631
632   switch (orientation)
633     {
634     case GTK_PROGRESS_LEFT_TO_RIGHT:
635     case GTK_PROGRESS_RIGHT_TO_LEFT:
636       w = amount;
637       h = widget->allocation.height - widget->style->ythickness * 2;
638       y = widget->style->ythickness;
639       
640       x = widget->style->xthickness;
641       if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
642         x = widget->allocation.width - amount - x;
643       break;
644       
645     case GTK_PROGRESS_TOP_TO_BOTTOM:
646     case GTK_PROGRESS_BOTTOM_TO_TOP:
647       w = widget->allocation.width - widget->style->xthickness * 2;
648       h = amount;
649       x = widget->style->xthickness;
650       
651       y = widget->style->ythickness;
652       if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
653         y = widget->allocation.height - amount - y;
654       break;
655       
656     default:
657       return;
658       break;
659     }
660   
661   gtk_paint_box (widget->style,
662                  GTK_PROGRESS (pbar)->offscreen_pixmap,
663                  GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
664                  NULL, widget, "bar",
665                  x, y, w, h);
666 }
667
668 static void
669 gtk_progress_bar_paint_discrete (GtkProgressBar            *pbar,
670                                  GtkProgressBarOrientation  orientation)
671 {
672   GtkWidget *widget = GTK_WIDGET (pbar);
673   gint i;
674
675   for (i = 0; i <= pbar->in_block; i++)
676     {
677       gint x, y, w, h, space;
678
679       switch (orientation)
680         {
681         case GTK_PROGRESS_LEFT_TO_RIGHT:
682         case GTK_PROGRESS_RIGHT_TO_LEFT:
683           space = widget->allocation.width - 2 * widget->style->xthickness;
684           
685           x = widget->style->xthickness + (i * space) / pbar->blocks;
686           y = widget->style->ythickness;
687           w = widget->style->xthickness + ((i + 1) * space) / pbar->blocks - x;
688           h = widget->allocation.height - 2 * widget->style->ythickness;
689
690           if (orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
691             x = widget->allocation.width - w - x;
692           break;
693           
694         case GTK_PROGRESS_TOP_TO_BOTTOM:
695         case GTK_PROGRESS_BOTTOM_TO_TOP:
696           space = widget->allocation.height - 2 * widget->style->ythickness;
697           
698           x = widget->style->xthickness;
699           y = widget->style->ythickness + (i * space) / pbar->blocks;
700           w = widget->allocation.width - 2 * widget->style->xthickness;
701           h = widget->style->ythickness + ((i + 1) * space) / pbar->blocks - y;
702           
703           if (orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
704             y = widget->allocation.height - h - y;
705           break;
706
707         default:
708           return;
709           break;
710         }
711       
712       gtk_paint_box (widget->style,
713                      GTK_PROGRESS (pbar)->offscreen_pixmap,
714                      GTK_STATE_PRELIGHT, GTK_SHADOW_OUT,
715                      NULL, widget, "bar",
716                      x, y, w, h);
717     }
718 }
719
720 static void
721 gtk_progress_bar_paint_text (GtkProgressBar            *pbar,
722                              gint                       amount,
723                              GtkProgressBarOrientation  orientation)
724 {
725   GtkProgress *progress = GTK_PROGRESS (pbar);
726   GtkWidget *widget = GTK_WIDGET (pbar);
727   
728   gint x;
729   gint y;
730   gchar *buf;
731   GdkRectangle rect;
732   PangoLayout *layout;
733   PangoRectangle logical_rect;
734   GdkRectangle prelight_clip, normal_clip;
735   
736   buf = gtk_progress_get_current_text (progress);
737   
738   layout = gtk_widget_create_pango_layout (widget, buf);
739   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
740   
741   x = widget->style->xthickness + 1 +
742     (widget->allocation.width - 2 * widget->style->xthickness -
743      2 - logical_rect.width)
744     * progress->x_align; 
745
746   y = widget->style->ythickness + 1 +
747     (widget->allocation.height - 2 * widget->style->ythickness -
748      2 - logical_rect.height)
749     * progress->y_align;
750
751   rect.x = widget->style->xthickness;
752   rect.y = widget->style->ythickness;
753   rect.width = widget->allocation.width - 2 * widget->style->xthickness;
754   rect.height = widget->allocation.height - 2 * widget->style->ythickness;
755
756   prelight_clip = normal_clip = rect;
757
758   switch (orientation)
759     {
760     case GTK_PROGRESS_LEFT_TO_RIGHT:
761       prelight_clip.width = amount;
762       normal_clip.x += amount;
763       normal_clip.width -= amount;
764       break;
765       
766     case GTK_PROGRESS_RIGHT_TO_LEFT:
767       normal_clip.width -= amount;
768       prelight_clip.x += normal_clip.width;
769       prelight_clip.width -= normal_clip.width;
770       break;
771        
772     case GTK_PROGRESS_TOP_TO_BOTTOM:
773       prelight_clip.height = amount;
774       normal_clip.y += amount;
775       normal_clip.height -= amount;
776       break;
777       
778     case GTK_PROGRESS_BOTTOM_TO_TOP:
779       normal_clip.height -= amount;
780       prelight_clip.y += normal_clip.height;
781       prelight_clip.height -= normal_clip.height;
782       break;
783     }
784   
785   gtk_paint_layout (widget->style,
786                     progress->offscreen_pixmap,
787                     GTK_STATE_PRELIGHT,
788                     FALSE,
789                     &prelight_clip,
790                     widget,
791                     "progressbar",
792                     x, y,
793                     layout);
794   
795   gtk_paint_layout (widget->style,
796                     progress->offscreen_pixmap,
797                     GTK_STATE_NORMAL,
798                     FALSE,
799                     &normal_clip,
800                     widget,
801                     "progressbar",
802                     x, y,
803                     layout);
804
805   g_object_unref (layout);
806   g_free (buf);
807 }
808
809 static void
810 gtk_progress_bar_paint (GtkProgress *progress)
811 {
812   GtkProgressBar *pbar;
813   GtkWidget *widget;
814
815   GtkProgressBarOrientation orientation;
816
817   g_return_if_fail (GTK_IS_PROGRESS_BAR (progress));
818
819   pbar = GTK_PROGRESS_BAR (progress);
820   widget = GTK_WIDGET (progress);
821
822   orientation = pbar->orientation;
823   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 
824     {
825       if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
826         orientation = GTK_PROGRESS_RIGHT_TO_LEFT;
827       else if (pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
828         orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
829     }
830  
831   if (progress->offscreen_pixmap)
832     {
833       gtk_paint_box (widget->style,
834                      progress->offscreen_pixmap,
835                      GTK_STATE_NORMAL, GTK_SHADOW_IN, 
836                      NULL, widget, "trough",
837                      0, 0,
838                      widget->allocation.width,
839                      widget->allocation.height);
840       
841       if (progress->activity_mode)
842         {
843           gtk_progress_bar_paint_activity (pbar, orientation);
844         }
845       else
846         {
847           gint amount;
848           gint space;
849           
850           if (orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
851               orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
852             space = widget->allocation.width - 2 * widget->style->xthickness;
853           else
854             space = widget->allocation.height - 2 * widget->style->ythickness;
855           
856           amount = space *
857             gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
858           
859           if (pbar->bar_style == GTK_PROGRESS_CONTINUOUS)
860             {
861               gtk_progress_bar_paint_continuous (pbar, amount, orientation);
862
863               if (GTK_PROGRESS (pbar)->show_text)
864                 gtk_progress_bar_paint_text (pbar, amount, orientation);
865             }
866           else
867             gtk_progress_bar_paint_discrete (pbar, orientation);
868         }
869     }
870 }
871
872 static void
873 gtk_progress_bar_set_bar_style_internal (GtkProgressBar     *pbar,
874                                          GtkProgressBarStyle bar_style)
875 {
876   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
877
878   if (pbar->bar_style != bar_style)
879     {
880       pbar->bar_style = bar_style;
881
882       if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
883         gtk_widget_queue_resize (GTK_WIDGET (pbar));
884
885       g_object_notify (G_OBJECT (pbar), "bar_style");
886     }
887 }
888
889 static void
890 gtk_progress_bar_set_discrete_blocks_internal (GtkProgressBar *pbar,
891                                                guint           blocks)
892 {
893   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
894   g_return_if_fail (blocks > 1);
895
896   if (pbar->blocks != blocks)
897     {
898       pbar->blocks = blocks;
899
900       if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
901         gtk_widget_queue_resize (GTK_WIDGET (pbar));
902
903       g_object_notify (G_OBJECT (pbar), "discrete_blocks");
904     }
905 }
906
907 static void
908 gtk_progress_bar_set_activity_step_internal (GtkProgressBar *pbar,
909                                              guint           step)
910 {
911   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
912
913   if (pbar->activity_step != step)
914     {
915       pbar->activity_step = step;
916       g_object_notify (G_OBJECT (pbar), "activity_step");
917     }
918 }
919
920 static void
921 gtk_progress_bar_set_activity_blocks_internal (GtkProgressBar *pbar,
922                                                guint           blocks)
923 {
924   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
925   g_return_if_fail (blocks > 1);
926
927   if (pbar->activity_blocks != blocks)
928     {
929       pbar->activity_blocks = blocks;
930       g_object_notify (G_OBJECT (pbar), "activity_blocks");
931     }
932 }
933
934 /*******************************************************************/
935
936 /**
937  * gtk_progress_bar_set_fraction:
938  * @pbar: a #GtkProgressBar
939  * @fraction: fraction of the task that's been completed
940  * 
941  * Causes the progress bar to "fill in" the given fraction
942  * of the bar. The fraction should be between 0.0 and 1.0,
943  * inclusive.
944  * 
945  **/
946 void
947 gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
948                                gdouble         fraction)
949 {
950   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
951
952   /* If we know the percentage, we don't want activity mode. */
953   gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), FALSE);
954   
955   /* We use the deprecated GtkProgress interface internally.
956    * Once everything's been deprecated for a good long time,
957    * we can clean up all this code.
958    */
959   gtk_progress_set_percentage (GTK_PROGRESS (pbar), fraction);
960
961   g_object_notify (G_OBJECT (pbar), "fraction");
962 }
963
964 /**
965  * gtk_progress_bar_pulse:
966  * @pbar: a #GtkProgressBar
967  * 
968  * Indicates that some progress is made, but you don't know how much.
969  * Causes the progress bar to enter "activity mode," where a block
970  * bounces back and forth. Each call to gtk_progress_bar_pulse()
971  * causes the block to move by a little bit (the amount of movement
972  * per pulse is determined by gtk_progress_bar_set_pulse_step()).
973  **/
974 void
975 gtk_progress_bar_pulse (GtkProgressBar *pbar)
976 {  
977   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
978
979   /* If we don't know the percentage, we must want activity mode. */
980   gtk_progress_set_activity_mode (GTK_PROGRESS (pbar), TRUE);
981
982   /* Sigh. */
983   gtk_progress_bar_real_update (GTK_PROGRESS (pbar));
984 }
985
986 /**
987  * gtk_progress_bar_set_text:
988  * @pbar: a #GtkProgressBar
989  * @text: a UTF-8 string
990  * 
991  * Causes the given @text to appear superimposed on the progress bar.
992  **/
993 void
994 gtk_progress_bar_set_text (GtkProgressBar *pbar,
995                            const gchar    *text)
996 {
997   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
998   
999   gtk_progress_set_show_text (GTK_PROGRESS (pbar), text && *text);
1000   gtk_progress_set_format_string (GTK_PROGRESS (pbar), text);
1001   
1002   /* We don't support formats in this interface, but turn
1003    * them back on for NULL, which should put us back to
1004    * the initial state.
1005    */
1006   GTK_PROGRESS (pbar)->use_text_format = (text == NULL);
1007   
1008   g_object_notify (G_OBJECT (pbar), "text");
1009 }
1010
1011 /**
1012  * gtk_progress_bar_set_pulse_step:
1013  * @pbar: a #GtkProgressBar
1014  * @fraction: fraction between 0.0 and 1.0
1015  * 
1016  * Sets the fraction of total progress bar length to move the
1017  * bouncing block for each call to gtk_progress_bar_pulse().
1018  **/
1019 void
1020 gtk_progress_bar_set_pulse_step   (GtkProgressBar *pbar,
1021                                    gdouble         fraction)
1022 {
1023   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1024   
1025   pbar->pulse_fraction = fraction;
1026
1027   g_object_notify (G_OBJECT (pbar), "pulse_step");
1028 }
1029
1030 void
1031 gtk_progress_bar_update (GtkProgressBar *pbar,
1032                          gdouble         percentage)
1033 {
1034   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1035
1036   /* Use of gtk_progress_bar_update() is deprecated ! 
1037    * Use gtk_progress_bar_set_percentage ()
1038    */   
1039
1040   gtk_progress_set_percentage (GTK_PROGRESS (pbar), percentage);
1041 }
1042
1043 /**
1044  * gtk_progress_bar_set_orientation:
1045  * @pbar: a #GtkProgressBar
1046  * @orientation: orientation of the progress bar
1047  * 
1048  * Causes the progress bar to switch to a different orientation
1049  * (left-to-right, right-to-left, top-to-bottom, or bottom-to-top). 
1050  **/
1051 void
1052 gtk_progress_bar_set_orientation (GtkProgressBar           *pbar,
1053                                   GtkProgressBarOrientation orientation)
1054 {
1055   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1056
1057   if (pbar->orientation != orientation)
1058     {
1059       pbar->orientation = orientation;
1060
1061       if (GTK_WIDGET_DRAWABLE (GTK_WIDGET (pbar)))
1062         gtk_widget_queue_resize (GTK_WIDGET (pbar));
1063
1064       g_object_notify (G_OBJECT (pbar), "orientation");
1065     }
1066 }
1067
1068 /**
1069  * gtk_progress_bar_get_text:
1070  * @pbar: a #GtkProgressBar
1071  * 
1072  * Retrieves the text displayed superimposed on the progress bar,
1073  * if any, otherwise %NULL. The return value is a reference
1074  * to the text, not a copy of it, so will become invalid
1075  * if you change the text in the progress bar.
1076  * 
1077  * Return value: text, or %NULL; this string is owned by the widget
1078  * and should not be modified or freed.
1079  **/
1080 G_CONST_RETURN gchar*
1081 gtk_progress_bar_get_text (GtkProgressBar *pbar)
1082 {
1083   g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), NULL);
1084
1085   if (GTK_PROGRESS (pbar)->use_text_format)
1086     return NULL;
1087   else
1088     return GTK_PROGRESS (pbar)->format;
1089 }
1090
1091 /**
1092  * gtk_progress_bar_get_fraction:
1093  * @pbar: a #GtkProgressBar
1094  * 
1095  * Returns the current fraction of the task that's been completed.
1096  * 
1097  * Return value: a fraction from 0.0 to 1.0
1098  **/
1099 gdouble
1100 gtk_progress_bar_get_fraction (GtkProgressBar *pbar)
1101 {
1102   g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1103
1104   return gtk_progress_get_current_percentage (GTK_PROGRESS (pbar));
1105 }
1106
1107 /**
1108  * gtk_progress_bar_get_pulse_step:
1109  * @pbar: a #GtkProgressBar
1110  * 
1111  * Retrieves the pulse step set with gtk_progress_bar_set_pulse_step()
1112  * 
1113  * Return value: a fraction from 0.0 to 1.0
1114  **/
1115 gdouble
1116 gtk_progress_bar_get_pulse_step (GtkProgressBar *pbar)
1117 {
1118   g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1119
1120   return pbar->pulse_fraction;
1121 }
1122
1123 /**
1124  * gtk_progress_bar_get_orientation:
1125  * @pbar: a #GtkProgressBar
1126  * 
1127  * Retrieves the current progress bar orientation.
1128  * 
1129  * Return value: orientation of the progress bar
1130  **/
1131 GtkProgressBarOrientation
1132 gtk_progress_bar_get_orientation (GtkProgressBar *pbar)
1133 {
1134   g_return_val_if_fail (GTK_IS_PROGRESS_BAR (pbar), 0);
1135
1136   return pbar->orientation;
1137 }
1138
1139 void
1140 gtk_progress_bar_set_bar_style (GtkProgressBar     *pbar,
1141                                 GtkProgressBarStyle bar_style)
1142 {
1143   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1144
1145   gtk_progress_bar_set_bar_style_internal (pbar, bar_style);
1146 }
1147
1148 void
1149 gtk_progress_bar_set_discrete_blocks (GtkProgressBar *pbar,
1150                                       guint           blocks)
1151 {
1152   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1153   g_return_if_fail (blocks > 1);
1154
1155   gtk_progress_bar_set_discrete_blocks_internal (pbar, blocks);
1156 }
1157
1158 void
1159 gtk_progress_bar_set_activity_step (GtkProgressBar *pbar,
1160                                     guint           step)
1161 {
1162   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1163
1164   gtk_progress_bar_set_activity_step_internal (pbar, step);
1165 }
1166
1167 void
1168 gtk_progress_bar_set_activity_blocks (GtkProgressBar *pbar,
1169                                       guint           blocks)
1170 {
1171   g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));
1172   g_return_if_fail (blocks > 1);
1173
1174   gtk_progress_bar_set_activity_blocks_internal (pbar, blocks);
1175 }