]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererprogress.c
Implement activity mode for GtkCellRendererProgress. (#377851, Brad
[~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 "gtkprivate.h"
33 #include "gtkintl.h"
34 #include "gtkalias.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 }; 
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 };
58
59 static void gtk_cell_renderer_progress_finalize     (GObject                 *object);
60 static void gtk_cell_renderer_progress_get_property (GObject                 *object,
61                                                      guint                    param_id,
62                                                      GValue                  *value,
63                                                      GParamSpec              *pspec);
64 static void gtk_cell_renderer_progress_set_property (GObject                 *object,
65                                                      guint                    param_id,
66                                                      const GValue            *value,
67                                                      GParamSpec              *pspec);
68 static void gtk_cell_renderer_progress_set_value    (GtkCellRendererProgress *cellprogress,
69                                                      gint                     value);
70 static void gtk_cell_renderer_progress_set_text     (GtkCellRendererProgress *cellprogress,
71                                                      const gchar             *text);
72 static void gtk_cell_renderer_progress_set_pulse    (GtkCellRendererProgress *cellprogress,
73                                                      gint                     pulse);
74 static void compute_dimensions                      (GtkCellRenderer         *cell,
75                                                      GtkWidget               *widget,
76                                                      const gchar             *text,
77                                                      gint                    *width,
78                                                      gint                    *height);
79 static void gtk_cell_renderer_progress_get_size     (GtkCellRenderer         *cell,
80                                                      GtkWidget               *widget,
81                                                      GdkRectangle            *cell_area,
82                                                      gint                    *x_offset,
83                                                      gint                    *y_offset,
84                                                      gint                    *width,
85                                                      gint                    *height);
86 static void gtk_cell_renderer_progress_render       (GtkCellRenderer         *cell,
87                                                      GdkWindow               *window,
88                                                      GtkWidget               *widget,
89                                                      GdkRectangle            *background_area,
90                                                      GdkRectangle            *cell_area,
91                                                      GdkRectangle            *expose_area,
92                                                      guint                    flags);
93
94      
95 G_DEFINE_TYPE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER)
96
97 static void
98 gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
99 {
100   GObjectClass *object_class = G_OBJECT_CLASS (klass);
101   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
102   
103   object_class->finalize = gtk_cell_renderer_progress_finalize;
104   object_class->get_property = gtk_cell_renderer_progress_get_property;
105   object_class->set_property = gtk_cell_renderer_progress_set_property;
106   
107   cell_class->get_size = gtk_cell_renderer_progress_get_size;
108   cell_class->render = gtk_cell_renderer_progress_render;
109   
110   /**
111    * GtkCellRendererProgress:value:
112    * 
113    * The "value" property determines the percentage to which the
114    * progress bar will be "filled in". 
115    *
116    * Since: 2.6
117    **/
118   g_object_class_install_property (object_class,
119                                    PROP_VALUE,
120                                    g_param_spec_int ("value",
121                                                      P_("Value"),
122                                                      P_("Value of the progress bar"),
123                                                      0, 100, 0,
124                                                      GTK_PARAM_READWRITE));
125
126   /**
127    * GtkCellRendererProgress:text:
128    * 
129    * The "text" property determines the label which will be drawn
130    * over the progress bar. Setting this property to %NULL causes the default 
131    * label to be displayed. Setting this property to an empty string causes 
132    * no label to be displayed.
133    *
134    * Since: 2.6
135    **/
136   g_object_class_install_property (object_class,
137                                    PROP_TEXT,
138                                    g_param_spec_string ("text",
139                                                         P_("Text"),
140                                                         P_("Text on the progress bar"),
141                                                         NULL,
142                                                         GTK_PARAM_READWRITE));
143
144   /**
145    * GtkCellRendererProgress:pulse:
146    * 
147    * Setting this to a non-negative value causes the cell renderer to
148    * enter "activity mode", where a block bounces back and forth to 
149    * indicate that some progress is made, with specifying exactly how
150    * much.
151    *
152    * Each increment of the property causes the block to move by a little 
153    * bit.
154    *
155    * To indicate that the activity has not started yet, set the property
156    * to zero. To indicate completion, set the property to %G_MAXINT.
157    *
158    * Since: 2.12
159    */
160   g_object_class_install_property (object_class,
161                                    PROP_PULSE,
162                                    g_param_spec_int ("pulse",
163                                                      P_("Pulse"),
164                                                      P_("Set this to positive values to indicate that some progress is made, but you don't know how much."),
165                                                      -1, G_MAXINT, -1,
166                                                      GTK_PARAM_READWRITE));
167
168   g_type_class_add_private (object_class, 
169                             sizeof (GtkCellRendererProgressPrivate));
170 }
171
172 static void
173 gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
174 {
175   GtkCellRendererProgressPrivate *priv = GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE (cellprogress);
176
177   priv->value = 0;
178   priv->text = NULL;
179   priv->label = NULL;
180   priv->min_w = -1;
181   priv->min_h = -1;
182   priv->pulse = -1;
183   priv->offset = 0;
184
185   cellprogress->priv = priv;
186 }
187
188
189 /**
190  * gtk_cell_renderer_progress_new:
191  * 
192  * Creates a new #GtkCellRendererProgress. 
193  *
194  * Return value: the new cell renderer
195  *
196  * Since: 2.6
197  **/
198 GtkCellRenderer*
199 gtk_cell_renderer_progress_new (void)
200 {
201   return g_object_new (GTK_TYPE_CELL_RENDERER_PROGRESS, NULL);
202 }
203
204 static void
205 gtk_cell_renderer_progress_finalize (GObject *object)
206 {
207   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
208   GtkCellRendererProgressPrivate *priv = cellprogress->priv;
209   
210   g_free (priv->text);
211   g_free (priv->label);
212   
213   G_OBJECT_CLASS (gtk_cell_renderer_progress_parent_class)->finalize (object);
214 }
215
216 static void
217 gtk_cell_renderer_progress_get_property (GObject *object,
218                                          guint param_id,
219                                          GValue *value,
220                                          GParamSpec *pspec)
221 {
222   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
223   GtkCellRendererProgressPrivate *priv = cellprogress->priv;
224   
225   switch (param_id)
226     {
227     case PROP_VALUE:
228       g_value_set_int (value, priv->value);
229       break;
230     case PROP_TEXT:
231       g_value_set_string (value, priv->text);
232       break;
233     case PROP_PULSE:
234       g_value_set_int (value, priv->pulse);
235       break;
236     default:
237       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
238     }
239 }
240
241 static void
242 gtk_cell_renderer_progress_set_property (GObject *object,
243                                          guint param_id,
244                                          const GValue *value,
245                                          GParamSpec   *pspec)
246 {
247   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
248   
249   switch (param_id)
250     {
251     case PROP_VALUE:
252       gtk_cell_renderer_progress_set_value (cellprogress, 
253                                             g_value_get_int (value));
254       break;
255     case PROP_TEXT:
256       gtk_cell_renderer_progress_set_text (cellprogress,
257                                            g_value_get_string (value));
258       break;
259     case PROP_PULSE:
260       gtk_cell_renderer_progress_set_pulse (cellprogress, 
261                                             g_value_get_int (value));
262       break;
263     default:
264       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
265     }
266 }
267
268 static void
269 recompute_label (GtkCellRendererProgress *cellprogress)
270 {
271   GtkCellRendererProgressPrivate *priv = cellprogress->priv;
272   gchar *label;
273
274   if (priv->text)
275     label = g_strdup (priv->text);
276   else if (priv->pulse < 0)
277     /* do not translate the part before the | */
278     label = g_strdup_printf (Q_("progress bar label|%d %%"), priv->value);
279   else
280     label = NULL;
281  
282   g_free (priv->label);
283   priv->label = label;
284 }
285
286 static void
287 gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress, 
288                                       gint                     value)
289 {
290   cellprogress->priv->value = value;
291
292   recompute_label (cellprogress);
293 }
294
295 static void
296 gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress, 
297                                      const gchar             *text)
298 {
299   gchar *new_text;
300
301   new_text = g_strdup (text);
302   g_free (cellprogress->priv->text);
303   cellprogress->priv->text = new_text;
304
305   recompute_label (cellprogress);
306 }
307
308 static void
309 gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress, 
310                                       gint                     pulse)
311 {
312    GtkCellRendererProgressPrivate *priv = cellprogress->priv;
313
314    if (pulse != priv->pulse)
315      {
316        if (priv->pulse <= 0)
317          priv->offset = 0;
318        else
319          priv->offset++;
320      }
321
322    priv->pulse = pulse;
323
324    recompute_label (cellprogress);
325 }
326
327 static void
328 compute_dimensions (GtkCellRenderer *cell,
329                     GtkWidget       *widget, 
330                     const gchar     *text, 
331                     gint            *width, 
332                     gint            *height)
333 {
334   PangoRectangle logical_rect;
335   PangoLayout *layout;
336   
337   layout = gtk_widget_create_pango_layout (widget, text);
338   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
339   
340   if (width)
341     *width = logical_rect.width + cell->xpad * 2;
342   
343   if (height)
344     *height = logical_rect.height + cell->ypad * 2;
345
346   g_object_unref (layout);
347 }
348
349 static void
350 gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
351                                      GtkWidget       *widget,
352                                      GdkRectangle    *cell_area,
353                                      gint            *x_offset,
354                                      gint            *y_offset,
355                                      gint            *width,
356                                      gint            *height)
357 {
358   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
359   GtkCellRendererProgressPrivate *priv = cellprogress->priv;
360   gint w, h;
361   gchar *text;
362
363   if (priv->min_w < 0)
364     {
365       text = g_strdup_printf (Q_("progress bar label|%d %%"), 100);
366       compute_dimensions (cell, widget, text,
367                           &priv->min_w,
368                           &priv->min_h);
369       g_free (text);
370     }
371   
372   compute_dimensions (cell, widget, priv->label, &w, &h);
373   
374   if (width)
375     *width = MAX (priv->min_w, w);
376   
377   if (height)
378     *height = MIN (priv->min_h, h);
379
380   /* FIXME: at the moment cell_area is only set when we are requesting
381    * the size for drawing the focus rectangle. We now just return
382    * the last size we used for drawing the progress bar, which will
383    * work for now. Not a really nice solution though.
384    */
385   if (cell_area)
386     {
387       if (width)
388         *width = cell_area->width;
389       if (height)
390         *height = cell_area->height;
391     }
392
393   if (x_offset) *x_offset = 0;
394   if (y_offset) *y_offset = 0;
395 }
396
397 static void
398 gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
399                                    GdkWindow       *window,
400                                    GtkWidget       *widget,
401                                    GdkRectangle    *background_area,
402                                    GdkRectangle    *cell_area,
403                                    GdkRectangle    *expose_area,
404                                    guint            flags)
405 {
406   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
407   GtkCellRendererProgressPrivate *priv= cellprogress->priv; 
408   PangoLayout *layout;
409   PangoRectangle logical_rect;
410   gint x, y, w, h, x_pos, y_pos, bar_x, bar_width;
411   GdkRectangle clip;
412   gboolean is_rtl;
413
414   is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
415   
416   x = cell_area->x + cell->xpad;
417   y = cell_area->y + cell->ypad;
418   w = cell_area->width - cell->xpad * 2;
419   h = cell_area->height - cell->ypad * 2;
420
421   /* FIXME: GtkProgressBar draws the box with "trough" detail,
422    * but some engines don't paint anything with that detail for
423    * non-GtkProgressBar widgets.
424    */
425   gtk_paint_box (widget->style,
426                  window,
427                  GTK_STATE_NORMAL, GTK_SHADOW_IN, 
428                  NULL, widget, NULL,
429                  x, y, w, h);
430
431   clip.y = y;
432   clip.height = h;
433   if (priv->pulse < 0)
434     {
435       clip.width = w * MAX (0, priv->value) / 100;
436       clip.x = is_rtl ? (x + w - clip.width) : x;
437     }
438   else if (priv->pulse == 0)
439     {
440       clip.x = x;
441       clip.width = 0;
442     }
443   else if (priv->pulse == G_MAXINT)
444     {
445       clip.x = x;
446       clip.width = w;
447     }
448   else
449     {
450       clip.width = MAX (2, w / 5);
451       x_pos = (is_rtl ? priv->offset + 12 : priv->offset) % 24;
452       if (x_pos > 12)
453         x_pos = 24 - x_pos;
454       clip.x = x + w * x_pos / 15; 
455     }
456
457   gtk_paint_box (widget->style,
458                  window,
459                  GTK_STATE_SELECTED, GTK_SHADOW_OUT,
460                  &clip, widget, "bar",
461                  clip.x, clip.y,
462                  clip.width, clip.height);
463
464   if (priv->label)
465     {
466       layout = gtk_widget_create_pango_layout (widget, priv->label);
467       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
468       
469       x_pos = x + (w - logical_rect.width) / 2;
470       y_pos = y + (h - logical_rect.height) / 2;
471   
472       gtk_paint_layout (widget->style, window, 
473                         GTK_STATE_SELECTED,
474                         FALSE, &clip, widget, "progressbar",
475                         x_pos, y_pos, 
476                         layout);
477
478       bar_x = clip.x;
479       bar_width = clip.width;
480       if (bar_x > x)
481         {
482           clip.x = x;
483           clip.width = bar_x - x;
484
485           gtk_paint_layout (widget->style, window, 
486                             GTK_STATE_NORMAL,
487                             FALSE, &clip, widget, "progressbar",
488                             x_pos, y_pos,
489                             layout);
490         }
491
492       if (bar_x + bar_width < x + w)
493         {
494           clip.x = bar_x + bar_width;
495           clip.width = x + w - (bar_x + bar_width);  
496
497           gtk_paint_layout (widget->style, window, 
498                             GTK_STATE_NORMAL,
499                             FALSE, &clip, widget, "progressbar",
500                             x_pos, y_pos,
501                             layout);
502         }
503
504       g_object_unref (layout);
505     }
506 }
507
508 #define __GTK_CELL_RENDERER_PROGRESS_C__
509 #include "gtkaliasdef.c"