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