]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererprogress.c
Don't try to keep state in a cell renderer between two paint calls. It
[~andy/gtk] / gtk / gtkcellrendererprogress.c
1 /* gtkcellrendererprogress.c
2  * Copyright (C) 2002 Naba Kumar <kh_naba@users.sourceforge.net>
3  * heavily modified by Jörgen Scheibengruber <mfcn@gmx.de>
4  * heavily modified by Marco Pesenti Gritti <marco@gnome.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 /*
22  * Modified by the GTK+ Team and others 1997-2007.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "config.h"
29 #include <stdlib.h>
30
31 #include "gtkcellrendererprogress.h"
32 #include "gtkprogressbar.h"
33 #include "gtkprivate.h"
34 #include "gtkintl.h"
35 #include "gtkalias.h"
36
37 #define GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object),                        \
38                                                                                      GTK_TYPE_CELL_RENDERER_PROGRESS, \
39                                                                                      GtkCellRendererProgressPrivate))
40
41 enum
42 {
43   PROP_0,
44   PROP_VALUE,
45   PROP_TEXT,
46   PROP_PULSE,
47   PROP_TEXT_XALIGN,
48   PROP_TEXT_YALIGN,
49   PROP_ORIENTATION
50 }; 
51
52 struct _GtkCellRendererProgressPrivate
53 {
54   gint value;
55   gchar *text;
56   gchar *label;
57   gint min_h;
58   gint min_w;
59   gint pulse;
60   gint offset;
61   gfloat text_xalign;
62   gfloat text_yalign;
63   GtkProgressBarOrientation orientation;
64 };
65
66 static void gtk_cell_renderer_progress_finalize     (GObject                 *object);
67 static void gtk_cell_renderer_progress_get_property (GObject                 *object,
68                                                      guint                    param_id,
69                                                      GValue                  *value,
70                                                      GParamSpec              *pspec);
71 static void gtk_cell_renderer_progress_set_property (GObject                 *object,
72                                                      guint                    param_id,
73                                                      const GValue            *value,
74                                                      GParamSpec              *pspec);
75 static void gtk_cell_renderer_progress_set_value    (GtkCellRendererProgress *cellprogress,
76                                                      gint                     value);
77 static void gtk_cell_renderer_progress_set_text     (GtkCellRendererProgress *cellprogress,
78                                                      const gchar             *text);
79 static void gtk_cell_renderer_progress_set_pulse    (GtkCellRendererProgress *cellprogress,
80                                                      gint                     pulse);
81 static void compute_dimensions                      (GtkCellRenderer         *cell,
82                                                      GtkWidget               *widget,
83                                                      const gchar             *text,
84                                                      gint                    *width,
85                                                      gint                    *height);
86 static void gtk_cell_renderer_progress_get_size     (GtkCellRenderer         *cell,
87                                                      GtkWidget               *widget,
88                                                      GdkRectangle            *cell_area,
89                                                      gint                    *x_offset,
90                                                      gint                    *y_offset,
91                                                      gint                    *width,
92                                                      gint                    *height);
93 static void gtk_cell_renderer_progress_render       (GtkCellRenderer         *cell,
94                                                      GdkWindow               *window,
95                                                      GtkWidget               *widget,
96                                                      GdkRectangle            *background_area,
97                                                      GdkRectangle            *cell_area,
98                                                      GdkRectangle            *expose_area,
99                                                      guint                    flags);
100
101      
102 G_DEFINE_TYPE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER)
103
104 static void
105 gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
106 {
107   GObjectClass *object_class = G_OBJECT_CLASS (klass);
108   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
109   
110   object_class->finalize = gtk_cell_renderer_progress_finalize;
111   object_class->get_property = gtk_cell_renderer_progress_get_property;
112   object_class->set_property = gtk_cell_renderer_progress_set_property;
113   
114   cell_class->get_size = gtk_cell_renderer_progress_get_size;
115   cell_class->render = gtk_cell_renderer_progress_render;
116   
117   /**
118    * GtkCellRendererProgress:value:
119    * 
120    * The "value" property determines the percentage to which the
121    * progress bar will be "filled in". 
122    *
123    * Since: 2.6
124    **/
125   g_object_class_install_property (object_class,
126                                    PROP_VALUE,
127                                    g_param_spec_int ("value",
128                                                      P_("Value"),
129                                                      P_("Value of the progress bar"),
130                                                      0, 100, 0,
131                                                      GTK_PARAM_READWRITE));
132
133   /**
134    * GtkCellRendererProgress:text:
135    * 
136    * The "text" property determines the label which will be drawn
137    * over the progress bar. Setting this property to %NULL causes the default 
138    * label to be displayed. Setting this property to an empty string causes 
139    * no label to be displayed.
140    *
141    * Since: 2.6
142    **/
143   g_object_class_install_property (object_class,
144                                    PROP_TEXT,
145                                    g_param_spec_string ("text",
146                                                         P_("Text"),
147                                                         P_("Text on the progress bar"),
148                                                         NULL,
149                                                         GTK_PARAM_READWRITE));
150
151   /**
152    * GtkCellRendererProgress:pulse:
153    * 
154    * Setting this to a non-negative value causes the cell renderer to
155    * enter "activity mode", where a block bounces back and forth to 
156    * indicate that some progress is made, without specifying exactly how
157    * much.
158    *
159    * Each increment of the property causes the block to move by a little 
160    * bit.
161    *
162    * To indicate that the activity has not started yet, set the property
163    * to zero. To indicate completion, set the property to %G_MAXINT.
164    *
165    * Since: 2.12
166    */
167   g_object_class_install_property (object_class,
168                                    PROP_PULSE,
169                                    g_param_spec_int ("pulse",
170                                                      P_("Pulse"),
171                                                      P_("Set this to positive values to indicate that some progress is made, but you don't know how much."),
172                                                      -1, G_MAXINT, -1,
173                                                      GTK_PARAM_READWRITE));
174
175   /**
176    * GtkCellRendererProgress:text-xalign:
177    *
178    * The "text-xalign" property controls the horizontal alignment of the
179    * text in the progress bar.  Valid values range from 0 (left) to 1
180    * (right).  Reserved for RTL layouts.
181    *
182    * Since: 2.12
183    */
184   g_object_class_install_property (object_class,
185                                    PROP_TEXT_XALIGN,
186                                    g_param_spec_float ("text-xalign",
187                                                        P_("Text x alignment"),
188                                                        P_("The horizontal text alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
189                                                        0.0, 1.0, 0.5,
190                                                        GTK_PARAM_READWRITE));
191
192   /**
193    * GtkCellRendererProgress:text-yalign:
194    *
195    * The "text-yalign" property controls the vertical alignment of the
196    * text in the progress bar.  Valid values range from 0 (top) to 1
197    * (bottom).
198    *
199    * Since: 2.12
200    */
201   g_object_class_install_property (object_class,
202                                    PROP_TEXT_YALIGN,
203                                    g_param_spec_float ("text-yalign",
204                                                        P_("Text y alignment"),
205                                                        P_("The vertical text alignment, from 0 (top) to 1 (bottom)."),
206                                                        0.0, 1.0, 0.5,
207                                                        GTK_PARAM_READWRITE));
208
209   /**
210    * GtkCellRendererProgress:orientation:
211    *
212    * The "orientation" property controls the direction and growth
213    * direction of the progress bar (left-to-right, right-to-left,
214    * top-to-bottom or bottom-to-top).
215    *
216    * Since: 2.12
217    */
218   g_object_class_install_property (object_class,
219                                    PROP_ORIENTATION,
220                                    g_param_spec_enum ("orientation",
221                                                       P_("Orientation"),
222                                                       P_("Orientation and growth direction of the progress bar"),
223                                                       GTK_TYPE_PROGRESS_BAR_ORIENTATION,
224                                                       GTK_PROGRESS_LEFT_TO_RIGHT,
225                                                       GTK_PARAM_READWRITE));
226
227
228   g_type_class_add_private (object_class, 
229                             sizeof (GtkCellRendererProgressPrivate));
230 }
231
232 static void
233 gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
234 {
235   GtkCellRendererProgressPrivate *priv = GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE (cellprogress);
236
237   priv->value = 0;
238   priv->text = NULL;
239   priv->label = NULL;
240   priv->min_w = -1;
241   priv->min_h = -1;
242   priv->pulse = -1;
243   priv->offset = 0;
244
245   priv->text_xalign = 0.5;
246   priv->text_yalign = 0.5;
247
248   priv->orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
249
250   cellprogress->priv = priv;
251 }
252
253
254 /**
255  * gtk_cell_renderer_progress_new:
256  * 
257  * Creates a new #GtkCellRendererProgress. 
258  *
259  * Return value: the new cell renderer
260  *
261  * Since: 2.6
262  **/
263 GtkCellRenderer*
264 gtk_cell_renderer_progress_new (void)
265 {
266   return g_object_new (GTK_TYPE_CELL_RENDERER_PROGRESS, NULL);
267 }
268
269 static void
270 gtk_cell_renderer_progress_finalize (GObject *object)
271 {
272   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
273   GtkCellRendererProgressPrivate *priv = cellprogress->priv;
274   
275   g_free (priv->text);
276   g_free (priv->label);
277   
278   G_OBJECT_CLASS (gtk_cell_renderer_progress_parent_class)->finalize (object);
279 }
280
281 static void
282 gtk_cell_renderer_progress_get_property (GObject *object,
283                                          guint param_id,
284                                          GValue *value,
285                                          GParamSpec *pspec)
286 {
287   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
288   GtkCellRendererProgressPrivate *priv = cellprogress->priv;
289   
290   switch (param_id)
291     {
292     case PROP_VALUE:
293       g_value_set_int (value, priv->value);
294       break;
295     case PROP_TEXT:
296       g_value_set_string (value, priv->text);
297       break;
298     case PROP_PULSE:
299       g_value_set_int (value, priv->pulse);
300       break;
301     case PROP_TEXT_XALIGN:
302       g_value_set_float (value, priv->text_xalign);
303       break;
304     case PROP_TEXT_YALIGN:
305       g_value_set_float (value, priv->text_yalign);
306       break;
307     case PROP_ORIENTATION:
308       g_value_set_enum (value, priv->orientation);
309       break;
310     default:
311       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
312     }
313 }
314
315 static void
316 gtk_cell_renderer_progress_set_property (GObject *object,
317                                          guint param_id,
318                                          const GValue *value,
319                                          GParamSpec   *pspec)
320 {
321   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
322   GtkCellRendererProgressPrivate *priv = cellprogress->priv;
323   
324   switch (param_id)
325     {
326     case PROP_VALUE:
327       gtk_cell_renderer_progress_set_value (cellprogress, 
328                                             g_value_get_int (value));
329       break;
330     case PROP_TEXT:
331       gtk_cell_renderer_progress_set_text (cellprogress,
332                                            g_value_get_string (value));
333       break;
334     case PROP_PULSE:
335       gtk_cell_renderer_progress_set_pulse (cellprogress, 
336                                             g_value_get_int (value));
337       break;
338     case PROP_TEXT_XALIGN:
339       priv->text_xalign = g_value_get_float (value);
340       break;
341     case PROP_TEXT_YALIGN:
342       priv->text_yalign = g_value_get_float (value);
343       break;
344     case PROP_ORIENTATION:
345       priv->orientation = g_value_get_enum (value);
346       break;
347     default:
348       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
349     }
350 }
351
352 static void
353 recompute_label (GtkCellRendererProgress *cellprogress)
354 {
355   GtkCellRendererProgressPrivate *priv = cellprogress->priv;
356   gchar *label;
357
358   if (priv->text)
359     label = g_strdup (priv->text);
360   else if (priv->pulse < 0)
361     label = g_strdup_printf (C_("progress bar label", "%d %%"), priv->value);
362   else
363     label = NULL;
364  
365   g_free (priv->label);
366   priv->label = label;
367 }
368
369 static void
370 gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress, 
371                                       gint                     value)
372 {
373   cellprogress->priv->value = value;
374
375   recompute_label (cellprogress);
376 }
377
378 static void
379 gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress, 
380                                      const gchar             *text)
381 {
382   gchar *new_text;
383
384   new_text = g_strdup (text);
385   g_free (cellprogress->priv->text);
386   cellprogress->priv->text = new_text;
387
388   recompute_label (cellprogress);
389 }
390
391 static void
392 gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress, 
393                                       gint                     pulse)
394 {
395    GtkCellRendererProgressPrivate *priv = cellprogress->priv;
396
397    if (pulse != priv->pulse)
398      {
399        if (pulse <= 0)
400          priv->offset = 0;
401        else
402          priv->offset = pulse;
403      }
404
405    priv->pulse = pulse;
406
407    recompute_label (cellprogress);
408 }
409
410 static void
411 compute_dimensions (GtkCellRenderer *cell,
412                     GtkWidget       *widget, 
413                     const gchar     *text, 
414                     gint            *width, 
415                     gint            *height)
416 {
417   PangoRectangle logical_rect;
418   PangoLayout *layout;
419   
420   layout = gtk_widget_create_pango_layout (widget, text);
421   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
422   
423   if (width)
424     *width = logical_rect.width + cell->xpad * 2;
425   
426   if (height)
427     *height = logical_rect.height + cell->ypad * 2;
428
429   g_object_unref (layout);
430 }
431
432 static void
433 gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
434                                      GtkWidget       *widget,
435                                      GdkRectangle    *cell_area,
436                                      gint            *x_offset,
437                                      gint            *y_offset,
438                                      gint            *width,
439                                      gint            *height)
440 {
441   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
442   GtkCellRendererProgressPrivate *priv = cellprogress->priv;
443   gint w, h;
444   gchar *text;
445
446   if (priv->min_w < 0)
447     {
448       text = g_strdup_printf (C_("progress bar label", "%d %%"), 100);
449       compute_dimensions (cell, widget, text,
450                           &priv->min_w,
451                           &priv->min_h);
452       g_free (text);
453     }
454   
455   compute_dimensions (cell, widget, priv->label, &w, &h);
456   
457   if (width)
458     *width = MAX (priv->min_w, w);
459   
460   if (height)
461     *height = MIN (priv->min_h, h);
462
463   /* FIXME: at the moment cell_area is only set when we are requesting
464    * the size for drawing the focus rectangle. We now just return
465    * the last size we used for drawing the progress bar, which will
466    * work for now. Not a really nice solution though.
467    */
468   if (cell_area)
469     {
470       if (width)
471         *width = cell_area->width;
472       if (height)
473         *height = cell_area->height;
474     }
475
476   if (x_offset) *x_offset = 0;
477   if (y_offset) *y_offset = 0;
478 }
479
480 static inline gint
481 get_bar_size (gint pulse,
482               gint value,
483               gint full_size)
484 {
485   gint bar_size;
486
487   if (pulse < 0)
488     bar_size = full_size * MAX (0, value) / 100;
489   else if (pulse == 0)
490     bar_size = 0;
491   else if (pulse == G_MAXINT)
492     bar_size = full_size;
493   else
494     bar_size = MAX (2, full_size / 5);
495
496   return bar_size;
497 }
498
499 static inline gint
500 get_bar_position (gint     start,
501                   gint     full_size,
502                   gint     bar_size,
503                   gint     pulse,
504                   gint     offset,
505                   gboolean is_rtl)
506 {
507   gint position;
508
509   if (pulse < 0 || pulse == 0 || pulse == G_MAXINT)
510     {
511       position = is_rtl ? (start + full_size - bar_size) : start;
512     }
513   else
514     {
515       position = (is_rtl ? offset + 12 : offset) % 24;
516       if (position > 12)
517         position = 24 - position;
518       position = start + full_size * position / 15;
519     }
520
521   return position;
522 }
523
524 static void
525 gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
526                                    GdkWindow       *window,
527                                    GtkWidget       *widget,
528                                    GdkRectangle    *background_area,
529                                    GdkRectangle    *cell_area,
530                                    GdkRectangle    *expose_area,
531                                    guint            flags)
532 {
533   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
534   GtkCellRendererProgressPrivate *priv= cellprogress->priv; 
535   PangoLayout *layout;
536   PangoRectangle logical_rect;
537   gint x, y, w, h, x_pos, y_pos, bar_position, bar_size, start, full_size;
538   GdkRectangle clip;
539   gboolean is_rtl;
540
541   is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
542   
543   x = cell_area->x + cell->xpad;
544   y = cell_area->y + cell->ypad;
545   w = cell_area->width - cell->xpad * 2;
546   h = cell_area->height - cell->ypad * 2;
547
548   /* FIXME: GtkProgressBar draws the box with "trough" detail,
549    * but some engines don't paint anything with that detail for
550    * non-GtkProgressBar widgets.
551    */
552   gtk_paint_box (widget->style,
553                  window,
554                  GTK_STATE_NORMAL, GTK_SHADOW_IN, 
555                  NULL, widget, NULL,
556                  x, y, w, h);
557
558   if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
559       || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
560     {
561       clip.y = y;
562       clip.height = h;
563
564       start = x;
565       full_size = w;
566
567       bar_size = get_bar_size (priv->pulse, priv->value, full_size);
568
569       if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
570         bar_position = get_bar_position (start, full_size, bar_size,
571                                          priv->pulse, priv->offset, is_rtl);
572       else
573         bar_position = get_bar_position (start, full_size, bar_size,
574                                          priv->pulse, priv->offset, !is_rtl);
575
576       clip.width = bar_size;
577       clip.x = bar_position;
578     }
579   else
580     {
581       clip.x = x;
582       clip.width = w;
583
584       start = y;
585       full_size = h;
586
587       bar_size = get_bar_size (priv->pulse, priv->value, full_size);
588
589       if (priv->orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
590         bar_position = get_bar_position (start, full_size, bar_size,
591                                          priv->pulse, priv->offset, TRUE);
592       else
593         bar_position = get_bar_position (start, full_size, bar_size,
594                                          priv->pulse, priv->offset, FALSE);
595
596       clip.height = bar_size;
597       clip.y = bar_position;
598     }
599
600   gtk_paint_box (widget->style,
601                  window,
602                  GTK_STATE_SELECTED, GTK_SHADOW_OUT,
603                  &clip, widget, "bar",
604                  clip.x, clip.y,
605                  clip.width, clip.height);
606
607   if (priv->label)
608     {
609       gfloat text_xalign;
610
611       layout = gtk_widget_create_pango_layout (widget, priv->label);
612       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
613
614       if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
615         text_xalign = 1.0 - priv->text_xalign;
616       else
617         text_xalign = priv->text_xalign;
618
619       x_pos = x + widget->style->xthickness + text_xalign *
620         (w - 2 * widget->style->xthickness - logical_rect.width);
621
622       y_pos = y + widget->style->ythickness + priv->text_yalign *
623         (h - 2 * widget->style->ythickness - logical_rect.height);
624   
625       gtk_paint_layout (widget->style, window, 
626                         GTK_STATE_SELECTED,
627                         FALSE, &clip, widget, "progressbar",
628                         x_pos, y_pos, 
629                         layout);
630
631       if (bar_position > start)
632         {
633           if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
634               || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
635             {
636               clip.x = x;
637               clip.width = bar_position - x;
638             }
639           else
640             {
641               clip.y = y;
642               clip.height = bar_position - y;
643             }
644
645           gtk_paint_layout (widget->style, window, 
646                             GTK_STATE_NORMAL,
647                             FALSE, &clip, widget, "progressbar",
648                             x_pos, y_pos,
649                             layout);
650         }
651
652       if (bar_position + bar_size < start + full_size)
653         {
654           if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
655               || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
656             {
657               clip.x = bar_position + bar_size;
658               clip.width = x + w - (bar_position + bar_size);
659             }
660           else
661             {
662               clip.y = bar_position + bar_size;
663               clip.height = y + h - (bar_position + bar_size);
664             }
665
666           gtk_paint_layout (widget->style, window, 
667                             GTK_STATE_NORMAL,
668                             FALSE, &clip, widget, "progressbar",
669                             x_pos, y_pos,
670                             layout);
671         }
672
673       g_object_unref (layout);
674     }
675 }
676
677 #define __GTK_CELL_RENDERER_PROGRESS_C__
678 #include "gtkaliasdef.c"