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