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