]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererprogress.c
gtk/: fully remove gtkalias hacks
[~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 #define GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object),                        \
37                                                                                      GTK_TYPE_CELL_RENDERER_PROGRESS, \
38                                                                                      GtkCellRendererProgressPrivate))
39
40 enum
41 {
42   PROP_0,
43   PROP_VALUE,
44   PROP_TEXT,
45   PROP_PULSE,
46   PROP_TEXT_XALIGN,
47   PROP_TEXT_YALIGN,
48   PROP_ORIENTATION
49 }; 
50
51 struct _GtkCellRendererProgressPrivate
52 {
53   gint value;
54   gchar *text;
55   gchar *label;
56   gint min_h;
57   gint min_w;
58   gint pulse;
59   gint offset;
60   gfloat text_xalign;
61   gfloat text_yalign;
62   GtkProgressBarOrientation orientation;
63 };
64
65 static void gtk_cell_renderer_progress_finalize     (GObject                 *object);
66 static void gtk_cell_renderer_progress_get_property (GObject                 *object,
67                                                      guint                    param_id,
68                                                      GValue                  *value,
69                                                      GParamSpec              *pspec);
70 static void gtk_cell_renderer_progress_set_property (GObject                 *object,
71                                                      guint                    param_id,
72                                                      const GValue            *value,
73                                                      GParamSpec              *pspec);
74 static void gtk_cell_renderer_progress_set_value    (GtkCellRendererProgress *cellprogress,
75                                                      gint                     value);
76 static void gtk_cell_renderer_progress_set_text     (GtkCellRendererProgress *cellprogress,
77                                                      const gchar             *text);
78 static void gtk_cell_renderer_progress_set_pulse    (GtkCellRendererProgress *cellprogress,
79                                                      gint                     pulse);
80 static void compute_dimensions                      (GtkCellRenderer         *cell,
81                                                      GtkWidget               *widget,
82                                                      const gchar             *text,
83                                                      gint                    *width,
84                                                      gint                    *height);
85 static void gtk_cell_renderer_progress_get_size     (GtkCellRenderer         *cell,
86                                                      GtkWidget               *widget,
87                                                      GdkRectangle            *cell_area,
88                                                      gint                    *x_offset,
89                                                      gint                    *y_offset,
90                                                      gint                    *width,
91                                                      gint                    *height);
92 static void gtk_cell_renderer_progress_render       (GtkCellRenderer         *cell,
93                                                      GdkWindow               *window,
94                                                      GtkWidget               *widget,
95                                                      GdkRectangle            *background_area,
96                                                      GdkRectangle            *cell_area,
97                                                      GdkRectangle            *expose_area,
98                                                      guint                    flags);
99
100      
101 G_DEFINE_TYPE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER)
102
103 static void
104 gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
105 {
106   GObjectClass *object_class = G_OBJECT_CLASS (klass);
107   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
108   
109   object_class->finalize = gtk_cell_renderer_progress_finalize;
110   object_class->get_property = gtk_cell_renderer_progress_get_property;
111   object_class->set_property = gtk_cell_renderer_progress_set_property;
112   
113   cell_class->get_size = gtk_cell_renderer_progress_get_size;
114   cell_class->render = gtk_cell_renderer_progress_render;
115   
116   /**
117    * GtkCellRendererProgress:value:
118    * 
119    * The "value" property determines the percentage to which the
120    * progress bar will be "filled in". 
121    *
122    * Since: 2.6
123    **/
124   g_object_class_install_property (object_class,
125                                    PROP_VALUE,
126                                    g_param_spec_int ("value",
127                                                      P_("Value"),
128                                                      P_("Value of the progress bar"),
129                                                      0, 100, 0,
130                                                      GTK_PARAM_READWRITE));
131
132   /**
133    * GtkCellRendererProgress:text:
134    * 
135    * The "text" property determines the label which will be drawn
136    * over the progress bar. Setting this property to %NULL causes the default 
137    * label to be displayed. Setting this property to an empty string causes 
138    * no label to be displayed.
139    *
140    * Since: 2.6
141    **/
142   g_object_class_install_property (object_class,
143                                    PROP_TEXT,
144                                    g_param_spec_string ("text",
145                                                         P_("Text"),
146                                                         P_("Text on the progress bar"),
147                                                         NULL,
148                                                         GTK_PARAM_READWRITE));
149
150   /**
151    * GtkCellRendererProgress:pulse:
152    * 
153    * Setting this to a non-negative value causes the cell renderer to
154    * enter "activity mode", where a block bounces back and forth to 
155    * indicate that some progress is made, without specifying exactly how
156    * much.
157    *
158    * Each increment of the property causes the block to move by a little 
159    * bit.
160    *
161    * To indicate that the activity has not started yet, set the property
162    * to zero. To indicate completion, set the property to %G_MAXINT.
163    *
164    * Since: 2.12
165    */
166   g_object_class_install_property (object_class,
167                                    PROP_PULSE,
168                                    g_param_spec_int ("pulse",
169                                                      P_("Pulse"),
170                                                      P_("Set this to positive values to indicate that some progress is made, but you don't know how much."),
171                                                      -1, G_MAXINT, -1,
172                                                      GTK_PARAM_READWRITE));
173
174   /**
175    * GtkCellRendererProgress:text-xalign:
176    *
177    * The "text-xalign" property controls the horizontal alignment of the
178    * text in the progress bar.  Valid values range from 0 (left) to 1
179    * (right).  Reserved for RTL layouts.
180    *
181    * Since: 2.12
182    */
183   g_object_class_install_property (object_class,
184                                    PROP_TEXT_XALIGN,
185                                    g_param_spec_float ("text-xalign",
186                                                        P_("Text x alignment"),
187                                                        P_("The horizontal text alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
188                                                        0.0, 1.0, 0.5,
189                                                        GTK_PARAM_READWRITE));
190
191   /**
192    * GtkCellRendererProgress:text-yalign:
193    *
194    * The "text-yalign" property controls the vertical alignment of the
195    * text in the progress bar.  Valid values range from 0 (top) to 1
196    * (bottom).
197    *
198    * Since: 2.12
199    */
200   g_object_class_install_property (object_class,
201                                    PROP_TEXT_YALIGN,
202                                    g_param_spec_float ("text-yalign",
203                                                        P_("Text y alignment"),
204                                                        P_("The vertical text alignment, from 0 (top) to 1 (bottom)."),
205                                                        0.0, 1.0, 0.5,
206                                                        GTK_PARAM_READWRITE));
207
208   /**
209    * GtkCellRendererProgress:orientation:
210    *
211    * The "orientation" property controls the direction and growth
212    * direction of the progress bar (left-to-right, right-to-left,
213    * top-to-bottom or bottom-to-top).
214    *
215    * Since: 2.12
216    */
217   g_object_class_install_property (object_class,
218                                    PROP_ORIENTATION,
219                                    g_param_spec_enum ("orientation",
220                                                       P_("Orientation"),
221                                                       P_("Orientation and growth direction of the progress bar"),
222                                                       GTK_TYPE_PROGRESS_BAR_ORIENTATION,
223                                                       GTK_PROGRESS_LEFT_TO_RIGHT,
224                                                       GTK_PARAM_READWRITE));
225
226
227   g_type_class_add_private (object_class, 
228                             sizeof (GtkCellRendererProgressPrivate));
229 }
230
231 static void
232 gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
233 {
234   GtkCellRendererProgressPrivate *priv = GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE (cellprogress);
235
236   priv->value = 0;
237   priv->text = NULL;
238   priv->label = NULL;
239   priv->min_w = -1;
240   priv->min_h = -1;
241   priv->pulse = -1;
242   priv->offset = 0;
243
244   priv->text_xalign = 0.5;
245   priv->text_yalign = 0.5;
246
247   priv->orientation = GTK_PROGRESS_LEFT_TO_RIGHT;
248
249   cellprogress->priv = priv;
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   
419   layout = gtk_widget_create_pango_layout (widget, text);
420   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
421   
422   if (width)
423     *width = logical_rect.width + cell->xpad * 2;
424   
425   if (height)
426     *height = logical_rect.height + cell->ypad * 2;
427
428   g_object_unref (layout);
429 }
430
431 static void
432 gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
433                                      GtkWidget       *widget,
434                                      GdkRectangle    *cell_area,
435                                      gint            *x_offset,
436                                      gint            *y_offset,
437                                      gint            *width,
438                                      gint            *height)
439 {
440   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
441   GtkCellRendererProgressPrivate *priv = cellprogress->priv;
442   gint w, h;
443   gchar *text;
444
445   if (priv->min_w < 0)
446     {
447       text = g_strdup_printf (C_("progress bar label", "%d %%"), 100);
448       compute_dimensions (cell, widget, text,
449                           &priv->min_w,
450                           &priv->min_h);
451       g_free (text);
452     }
453   
454   compute_dimensions (cell, widget, priv->label, &w, &h);
455   
456   if (width)
457     *width = MAX (priv->min_w, w);
458   
459   if (height)
460     *height = MIN (priv->min_h, h);
461
462   /* FIXME: at the moment cell_area is only set when we are requesting
463    * the size for drawing the focus rectangle. We now just return
464    * the last size we used for drawing the progress bar, which will
465    * work for now. Not a really nice solution though.
466    */
467   if (cell_area)
468     {
469       if (width)
470         *width = cell_area->width;
471       if (height)
472         *height = cell_area->height;
473     }
474
475   if (x_offset) *x_offset = 0;
476   if (y_offset) *y_offset = 0;
477 }
478
479 static inline gint
480 get_bar_size (gint pulse,
481               gint value,
482               gint full_size)
483 {
484   gint bar_size;
485
486   if (pulse < 0)
487     bar_size = full_size * MAX (0, value) / 100;
488   else if (pulse == 0)
489     bar_size = 0;
490   else if (pulse == G_MAXINT)
491     bar_size = full_size;
492   else
493     bar_size = MAX (2, full_size / 5);
494
495   return bar_size;
496 }
497
498 static inline gint
499 get_bar_position (gint     start,
500                   gint     full_size,
501                   gint     bar_size,
502                   gint     pulse,
503                   gint     offset,
504                   gboolean is_rtl)
505 {
506   gint position;
507
508   if (pulse < 0 || pulse == 0 || pulse == G_MAXINT)
509     {
510       position = is_rtl ? (start + full_size - bar_size) : start;
511     }
512   else
513     {
514       position = (is_rtl ? offset + 12 : offset) % 24;
515       if (position > 12)
516         position = 24 - position;
517       position = start + full_size * position / 15;
518     }
519
520   return position;
521 }
522
523 static void
524 gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
525                                    GdkWindow       *window,
526                                    GtkWidget       *widget,
527                                    GdkRectangle    *background_area,
528                                    GdkRectangle    *cell_area,
529                                    GdkRectangle    *expose_area,
530                                    guint            flags)
531 {
532   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
533   GtkCellRendererProgressPrivate *priv= cellprogress->priv; 
534   PangoLayout *layout;
535   PangoRectangle logical_rect;
536   gint x, y, w, h, x_pos, y_pos, bar_position, bar_size, start, full_size;
537   GdkRectangle clip;
538   gboolean is_rtl;
539
540   is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
541   
542   x = cell_area->x + cell->xpad;
543   y = cell_area->y + cell->ypad;
544   w = cell_area->width - cell->xpad * 2;
545   h = cell_area->height - cell->ypad * 2;
546
547   /* FIXME: GtkProgressBar draws the box with "trough" detail,
548    * but some engines don't paint anything with that detail for
549    * non-GtkProgressBar widgets.
550    */
551   gtk_paint_box (widget->style,
552                  window,
553                  GTK_STATE_NORMAL, GTK_SHADOW_IN, 
554                  NULL, widget, NULL,
555                  x, y, w, h);
556
557   if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
558       || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
559     {
560       clip.y = y;
561       clip.height = h;
562
563       start = x;
564       full_size = w;
565
566       bar_size = get_bar_size (priv->pulse, priv->value, full_size);
567
568       if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT)
569         bar_position = get_bar_position (start, full_size, bar_size,
570                                          priv->pulse, priv->offset, is_rtl);
571       else
572         bar_position = get_bar_position (start, full_size, bar_size,
573                                          priv->pulse, priv->offset, !is_rtl);
574
575       clip.width = bar_size;
576       clip.x = bar_position;
577     }
578   else
579     {
580       clip.x = x;
581       clip.width = w;
582
583       start = y;
584       full_size = h;
585
586       bar_size = get_bar_size (priv->pulse, priv->value, full_size);
587
588       if (priv->orientation == GTK_PROGRESS_BOTTOM_TO_TOP)
589         bar_position = get_bar_position (start, full_size, bar_size,
590                                          priv->pulse, priv->offset, TRUE);
591       else
592         bar_position = get_bar_position (start, full_size, bar_size,
593                                          priv->pulse, priv->offset, FALSE);
594
595       clip.height = bar_size;
596       clip.y = bar_position;
597     }
598
599   gtk_paint_box (widget->style,
600                  window,
601                  GTK_STATE_SELECTED, GTK_SHADOW_OUT,
602                  &clip, widget, "bar",
603                  clip.x, clip.y,
604                  clip.width, clip.height);
605
606   if (priv->label)
607     {
608       gfloat text_xalign;
609
610       layout = gtk_widget_create_pango_layout (widget, priv->label);
611       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
612
613       if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
614         text_xalign = 1.0 - priv->text_xalign;
615       else
616         text_xalign = priv->text_xalign;
617
618       x_pos = x + widget->style->xthickness + text_xalign *
619         (w - 2 * widget->style->xthickness - logical_rect.width);
620
621       y_pos = y + widget->style->ythickness + priv->text_yalign *
622         (h - 2 * widget->style->ythickness - logical_rect.height);
623   
624       gtk_paint_layout (widget->style, window, 
625                         GTK_STATE_SELECTED,
626                         FALSE, &clip, widget, "progressbar",
627                         x_pos, y_pos, 
628                         layout);
629
630       if (bar_position > start)
631         {
632           if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
633               || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
634             {
635               clip.x = x;
636               clip.width = bar_position - x;
637             }
638           else
639             {
640               clip.y = y;
641               clip.height = bar_position - y;
642             }
643
644           gtk_paint_layout (widget->style, window, 
645                             GTK_STATE_NORMAL,
646                             FALSE, &clip, widget, "progressbar",
647                             x_pos, y_pos,
648                             layout);
649         }
650
651       if (bar_position + bar_size < start + full_size)
652         {
653           if (priv->orientation == GTK_PROGRESS_LEFT_TO_RIGHT
654               || priv->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
655             {
656               clip.x = bar_position + bar_size;
657               clip.width = x + w - (bar_position + bar_size);
658             }
659           else
660             {
661               clip.y = bar_position + bar_size;
662               clip.height = y + h - (bar_position + bar_size);
663             }
664
665           gtk_paint_layout (widget->style, window, 
666                             GTK_STATE_NORMAL,
667                             FALSE, &clip, widget, "progressbar",
668                             x_pos, y_pos,
669                             layout);
670         }
671
672       g_object_unref (layout);
673     }
674 }