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