]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererspinner.c
Minor documentation improvements
[~andy/gtk] / gtk / gtkcellrendererspinner.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2009 Matthias Clasen <mclasen@redhat.com>
4  * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
5  * Copyright (C) 2009 Bastien Nocera <hadess@hadess.net>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA  02111-1307, USA.
21  */
22
23 /*
24  * Modified by the GTK+ Team and others 2007.  See the AUTHORS
25  * file for a list of people on the GTK+ Team.  See the ChangeLog
26  * files for a list of changes.  These files are distributed with
27  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28  */
29
30 #include "config.h"
31
32 #include "gtkcellrendererspinner.h"
33 #include "gtkiconfactory.h"
34 #include "gtkicontheme.h"
35 #include "gtktypebuiltins.h"
36 #include "gtkintl.h"
37
38
39 /**
40  * SECTION:gtkcellrendererspinner
41  * @Short_description: Renders a spinning animation in a cell
42  * @Title: GtkCellRendererSpinner
43  * @See_also: #GtkSpinner, #GtkCellRendererProgress
44  *
45  * GtkCellRendererSpinner renders a spinning animation in a cell, very
46  * similar to #GtkSpinner. It can often be used as an alternative
47  * to a #GtkCellRendererProgress for displaying indefinite activity,
48  * instead of actual progress.
49  *
50  * To start the animation in a cell, set the #GtkCellRendererSpinner:active
51  * property to %TRUE and increment the #GtkCellRendererSpinner:pulse property
52  * at regular intervals. The usual way to set the cell renderer properties
53  * for each cell is to bind them to columns in your tree model using e.g.
54  * gtk_tree_view_column_add_attribute().
55  */
56
57
58 enum {
59   PROP_0,
60   PROP_ACTIVE,
61   PROP_PULSE,
62   PROP_SIZE
63 };
64
65 struct _GtkCellRendererSpinnerPrivate
66 {
67   gboolean active;
68   guint pulse;
69   GtkIconSize icon_size, old_icon_size;
70   gint size;
71 };
72
73
74 static void gtk_cell_renderer_spinner_get_property (GObject         *object,
75                                                     guint            param_id,
76                                                     GValue          *value,
77                                                     GParamSpec      *pspec);
78 static void gtk_cell_renderer_spinner_set_property (GObject         *object,
79                                                     guint            param_id,
80                                                     const GValue    *value,
81                                                     GParamSpec      *pspec);
82 static void gtk_cell_renderer_spinner_get_size     (GtkCellRenderer *cell,
83                                                     GtkWidget          *widget,
84                                                     const GdkRectangle *cell_area,
85                                                     gint               *x_offset,
86                                                     gint               *y_offset,
87                                                     gint               *width,
88                                                     gint               *height);
89 static void gtk_cell_renderer_spinner_render       (GtkCellRenderer      *cell,
90                                                     cairo_t              *cr,
91                                                     GtkWidget            *widget,
92                                                     const GdkRectangle   *background_area,
93                                                     const GdkRectangle   *cell_area,
94                                                     GtkCellRendererState  flags);
95
96 G_DEFINE_TYPE (GtkCellRendererSpinner, gtk_cell_renderer_spinner, GTK_TYPE_CELL_RENDERER)
97
98 static void
99 gtk_cell_renderer_spinner_class_init (GtkCellRendererSpinnerClass *klass)
100 {
101   GObjectClass *object_class = G_OBJECT_CLASS (klass);
102   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
103
104   object_class->get_property = gtk_cell_renderer_spinner_get_property;
105   object_class->set_property = gtk_cell_renderer_spinner_set_property;
106
107   cell_class->get_size = gtk_cell_renderer_spinner_get_size;
108   cell_class->render = gtk_cell_renderer_spinner_render;
109
110   /* GtkCellRendererSpinner:active:
111    *
112    * Whether the spinner is active (ie. shown) in the cell
113    *
114    * Since: 2.20
115    */
116   g_object_class_install_property (object_class,
117                                    PROP_ACTIVE,
118                                    g_param_spec_boolean ("active",
119                                                          P_("Active"),
120                                                          P_("Whether the spinner is active (ie. shown) in the cell"),
121                                                          FALSE,
122                                                          G_PARAM_READWRITE));
123   /**
124    * GtkCellRendererSpinner:pulse:
125    *
126    * Pulse of the spinner. Increment this value to draw the next frame of the
127    * spinner animation. Usually, you would update this value in a timeout.
128    *
129    * By default, the #GtkSpinner widget draws one full cycle of the animation,
130    * consisting of 12 frames, in 750 milliseconds.
131    *
132    * Since: 2.20
133    */
134   g_object_class_install_property (object_class,
135                                    PROP_PULSE,
136                                    g_param_spec_uint ("pulse",
137                                                       P_("Pulse"),
138                                                       P_("Pulse of the spinner"),
139                                                       0, G_MAXUINT, 0,
140                                                       G_PARAM_READWRITE));
141   /**
142    * GtkCellRendererSpinner:size:
143    *
144    * The #GtkIconSize value that specifies the size of the rendered spinner.
145    *
146    * Since: 2.20
147    */
148   g_object_class_install_property (object_class,
149                                    PROP_SIZE,
150                                    g_param_spec_enum ("size",
151                                                       P_("Size"),
152                                                       P_("The GtkIconSize value that specifies the size of the rendered spinner"),
153                                                       GTK_TYPE_ICON_SIZE, GTK_ICON_SIZE_MENU,
154                                                       G_PARAM_READWRITE));
155
156
157   g_type_class_add_private (object_class, sizeof (GtkCellRendererSpinnerPrivate));
158 }
159
160 static void
161 gtk_cell_renderer_spinner_init (GtkCellRendererSpinner *cell)
162 {
163   cell->priv = G_TYPE_INSTANCE_GET_PRIVATE (cell,
164                                             GTK_TYPE_CELL_RENDERER_SPINNER,
165                                             GtkCellRendererSpinnerPrivate);
166
167   cell->priv->pulse = 0;
168   cell->priv->old_icon_size = GTK_ICON_SIZE_INVALID;
169   cell->priv->icon_size = GTK_ICON_SIZE_MENU;
170 }
171
172 /**
173  * gtk_cell_renderer_spinner_new:
174  *
175  * Returns a new cell renderer which will show a spinner to indicate
176  * activity.
177  *
178  * Return value: a new #GtkCellRenderer
179  *
180  * Since: 2.20
181  */
182 GtkCellRenderer *
183 gtk_cell_renderer_spinner_new (void)
184 {
185   return g_object_new (GTK_TYPE_CELL_RENDERER_SPINNER, NULL);
186 }
187
188 static void
189 gtk_cell_renderer_spinner_update_size (GtkCellRendererSpinner *cell,
190                                        GtkWidget              *widget)
191 {
192   GtkCellRendererSpinnerPrivate *priv = cell->priv;
193   GdkScreen *screen;
194   GtkSettings *settings;
195
196   if (priv->old_icon_size == priv->icon_size)
197     return;
198
199   screen = gtk_widget_get_screen (GTK_WIDGET (widget));
200   settings = gtk_settings_get_for_screen (screen);
201
202   if (!gtk_icon_size_lookup_for_settings (settings, priv->icon_size, &priv->size, NULL))
203     {
204       g_warning ("Invalid icon size %u\n", priv->icon_size);
205       priv->size = 24;
206     }
207 }
208
209 static void
210 gtk_cell_renderer_spinner_get_property (GObject    *object,
211                                         guint       param_id,
212                                         GValue     *value,
213                                         GParamSpec *pspec)
214 {
215   GtkCellRendererSpinner *cell = GTK_CELL_RENDERER_SPINNER (object);
216   GtkCellRendererSpinnerPrivate *priv = cell->priv;
217
218   switch (param_id)
219     {
220       case PROP_ACTIVE:
221         g_value_set_boolean (value, priv->active);
222         break;
223       case PROP_PULSE:
224         g_value_set_uint (value, priv->pulse);
225         break;
226       case PROP_SIZE:
227         g_value_set_enum (value, priv->icon_size);
228         break;
229       default:
230         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
231     }
232 }
233
234 static void
235 gtk_cell_renderer_spinner_set_property (GObject      *object,
236                                         guint         param_id,
237                                         const GValue *value,
238                                         GParamSpec   *pspec)
239 {
240   GtkCellRendererSpinner *cell = GTK_CELL_RENDERER_SPINNER (object);
241   GtkCellRendererSpinnerPrivate *priv = cell->priv;
242
243   switch (param_id)
244     {
245       case PROP_ACTIVE:
246         priv->active = g_value_get_boolean (value);
247         break;
248       case PROP_PULSE:
249         priv->pulse = g_value_get_uint (value);
250         break;
251       case PROP_SIZE:
252         priv->old_icon_size = priv->icon_size;
253         priv->icon_size = g_value_get_enum (value);
254         break;
255       default:
256         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
257     }
258 }
259
260 static void
261 gtk_cell_renderer_spinner_get_size (GtkCellRenderer    *cellr,
262                                     GtkWidget          *widget,
263                                     const GdkRectangle *cell_area,
264                                     gint               *x_offset,
265                                     gint               *y_offset,
266                                     gint               *width,
267                                     gint               *height)
268 {
269   GtkCellRendererSpinner *cell = GTK_CELL_RENDERER_SPINNER (cellr);
270   GtkCellRendererSpinnerPrivate *priv = cell->priv;
271   gdouble align;
272   gint w, h;
273   gint xpad, ypad;
274   gfloat xalign, yalign;
275   gboolean rtl;
276
277   rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
278
279   gtk_cell_renderer_spinner_update_size (cell, widget);
280
281   g_object_get (cellr,
282                 "xpad", &xpad,
283                 "ypad", &ypad,
284                 "xalign", &xalign,
285                 "yalign", &yalign,
286                 NULL);
287   w = h = priv->size;
288
289   if (cell_area)
290     {
291       if (x_offset)
292         {
293           align = rtl ? 1.0 - xalign : xalign;
294           *x_offset = align * (cell_area->width - w);
295           *x_offset = MAX (*x_offset, 0);
296         }
297       if (y_offset)
298         {
299           align = rtl ? 1.0 - yalign : yalign;
300           *y_offset = align * (cell_area->height - h);
301           *y_offset = MAX (*y_offset, 0);
302         }
303     }
304   else
305     {
306       if (x_offset)
307         *x_offset = 0;
308       if (y_offset)
309         *y_offset = 0;
310     }
311
312   if (width)
313     *width = w;
314   if (height)
315     *height = h;
316 }
317
318 static void
319 gtk_cell_renderer_spinner_render (GtkCellRenderer      *cellr,
320                                   cairo_t              *cr,
321                                   GtkWidget            *widget,
322                                   const GdkRectangle   *background_area,
323                                   const GdkRectangle   *cell_area,
324                                   GtkCellRendererState  flags)
325 {
326   GtkCellRendererSpinner *cell = GTK_CELL_RENDERER_SPINNER (cellr);
327   GtkCellRendererSpinnerPrivate *priv = cell->priv;
328   GtkStateType state;
329   GdkRectangle pix_rect;
330   GdkRectangle draw_rect;
331   gint xpad, ypad;
332
333   if (!priv->active)
334     return;
335
336   gtk_cell_renderer_spinner_get_size (cellr, widget, (GdkRectangle *) cell_area,
337                                       &pix_rect.x, &pix_rect.y,
338                                       &pix_rect.width, &pix_rect.height);
339
340   g_object_get (cellr,
341                 "xpad", &xpad,
342                 "ypad", &ypad,
343                 NULL);
344   pix_rect.x += cell_area->x + xpad;
345   pix_rect.y += cell_area->y + ypad;
346   pix_rect.width -= xpad * 2;
347   pix_rect.height -= ypad * 2;
348
349   if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
350     return;
351
352   state = GTK_STATE_NORMAL;
353   if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE ||
354       !gtk_cell_renderer_get_sensitive (cellr))
355     {
356       state = GTK_STATE_INSENSITIVE;
357     }
358   else
359     {
360       if ((flags & GTK_CELL_RENDERER_SELECTED) != 0)
361         {
362           if (gtk_widget_has_focus (widget))
363             state = GTK_STATE_SELECTED;
364           else
365             state = GTK_STATE_ACTIVE;
366         }
367       else
368         state = GTK_STATE_PRELIGHT;
369     }
370
371   cairo_save (cr);
372
373   gdk_cairo_rectangle (cr, cell_area);
374   cairo_clip (cr);
375
376   gtk_paint_spinner (gtk_widget_get_style (widget),
377                            cr,
378                            state,
379                            widget,
380                            "cell",
381                            priv->pulse,
382                            draw_rect.x, draw_rect.y,
383                            draw_rect.width, draw_rect.height);
384
385   cairo_restore (cr);
386 }