]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrendererspinner.c
API: Rename gtk_cairo_paint_*() to gtk_paint_*()
[~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 "gtkintl.h"
36
37
38 /**
39  * SECTION:gtkcellrendererspinner
40  * @Short_description: Renders a spinning animation in a cell
41  * @Title: GtkCellRendererSpinner
42  * @See_also: #GtkSpinner, #GtkCellRendererProgress
43  *
44  * GtkCellRendererSpinner renders a spinning animation in a cell, very
45  * similar to #GtkSpinner. It can often be used as an alternative
46  * to a #GtkCellRendererProgress for displaying indefinite activity,
47  * instead of actual progress.
48  *
49  * To start the animation in a cell, set the #GtkCellRendererSpinner:active
50  * property to %TRUE and increment the #GtkCellRendererSpinner:pulse property
51  * at regular intervals. The usual way to set the cell renderer properties
52  * for each cell is to bind them to columns in your tree model using e.g.
53  * gtk_tree_view_column_add_attribute().
54  */
55
56
57 enum {
58   PROP_0,
59   PROP_ACTIVE,
60   PROP_PULSE,
61   PROP_SIZE
62 };
63
64 struct _GtkCellRendererSpinnerPrivate
65 {
66   gboolean active;
67   guint pulse;
68   GtkIconSize icon_size, old_icon_size;
69   gint size;
70 };
71
72
73 static void gtk_cell_renderer_spinner_get_property (GObject         *object,
74                                                     guint            param_id,
75                                                     GValue          *value,
76                                                     GParamSpec      *pspec);
77 static void gtk_cell_renderer_spinner_set_property (GObject         *object,
78                                                     guint            param_id,
79                                                     const GValue    *value,
80                                                     GParamSpec      *pspec);
81 static void gtk_cell_renderer_spinner_get_size     (GtkCellRenderer *cell,
82                                                     GtkWidget       *widget,
83                                                     GdkRectangle    *cell_area,
84                                                     gint            *x_offset,
85                                                     gint            *y_offset,
86                                                     gint            *width,
87                                                     gint            *height);
88 static void gtk_cell_renderer_spinner_render       (GtkCellRenderer      *cell,
89                                                     cairo_t              *cr,
90                                                     GtkWidget            *widget,
91                                                     const GdkRectangle   *background_area,
92                                                     const GdkRectangle   *cell_area,
93                                                     GtkCellRendererState  flags);
94
95 G_DEFINE_TYPE (GtkCellRendererSpinner, gtk_cell_renderer_spinner, GTK_TYPE_CELL_RENDERER)
96
97 static void
98 gtk_cell_renderer_spinner_class_init (GtkCellRendererSpinnerClass *klass)
99 {
100   GObjectClass *object_class = G_OBJECT_CLASS (klass);
101   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
102
103   object_class->get_property = gtk_cell_renderer_spinner_get_property;
104   object_class->set_property = gtk_cell_renderer_spinner_set_property;
105
106   cell_class->get_size = gtk_cell_renderer_spinner_get_size;
107   cell_class->render = gtk_cell_renderer_spinner_render;
108
109   /* GtkCellRendererSpinner:active:
110    *
111    * Whether the spinner is active (ie. shown) in the cell
112    *
113    * Since: 2.20
114    */
115   g_object_class_install_property (object_class,
116                                    PROP_ACTIVE,
117                                    g_param_spec_boolean ("active",
118                                                          P_("Active"),
119                                                          P_("Whether the spinner is active (ie. shown) in the cell"),
120                                                          FALSE,
121                                                          G_PARAM_READWRITE));
122   /**
123    * GtkCellRendererSpinner:pulse:
124    *
125    * Pulse of the spinner. Increment this value to draw the next frame of the
126    * spinner animation. Usually, you would update this value in a timeout.
127    *
128    * The #GtkSpinner widget draws one full cycle of the animation per second by default.
129    * You can learn about the number of frames used by the theme
130    * by looking at the #GtkSpinner:num-steps style property and the duration
131    * of the cycle by looking at #GtkSpinner:cycle-duration.
132    *
133    * Since: 2.20
134    */
135   g_object_class_install_property (object_class,
136                                    PROP_PULSE,
137                                    g_param_spec_uint ("pulse",
138                                                       P_("Pulse"),
139                                                       P_("Pulse of the spinner"),
140                                                       0, G_MAXUINT, 0,
141                                                       G_PARAM_READWRITE));
142   /**
143    * GtkCellRendererSpinner:size:
144    *
145    * The #GtkIconSize value that specifies the size of the rendered spinner.
146    *
147    * Since: 2.20
148    */
149   g_object_class_install_property (object_class,
150                                    PROP_SIZE,
151                                    g_param_spec_enum ("size",
152                                                       P_("Size"),
153                                                       P_("The GtkIconSize value that specifies the size of the rendered spinner"),
154                                                       GTK_TYPE_ICON_SIZE, GTK_ICON_SIZE_MENU,
155                                                       G_PARAM_READWRITE));
156
157
158   g_type_class_add_private (object_class, sizeof (GtkCellRendererSpinnerPrivate));
159 }
160
161 static void
162 gtk_cell_renderer_spinner_init (GtkCellRendererSpinner *cell)
163 {
164   cell->priv = G_TYPE_INSTANCE_GET_PRIVATE (cell,
165                                             GTK_TYPE_CELL_RENDERER_SPINNER,
166                                             GtkCellRendererSpinnerPrivate);
167
168   cell->priv->pulse = 0;
169   cell->priv->old_icon_size = GTK_ICON_SIZE_INVALID;
170   cell->priv->icon_size = GTK_ICON_SIZE_MENU;
171 }
172
173 /**
174  * gtk_cell_renderer_spinner_new
175  *
176  * Returns a new cell renderer which will show a spinner to indicate
177  * activity.
178  *
179  * Return value: a new #GtkCellRenderer
180  *
181  * Since: 2.20
182  */
183 GtkCellRenderer *
184 gtk_cell_renderer_spinner_new (void)
185 {
186   return g_object_new (GTK_TYPE_CELL_RENDERER_SPINNER, NULL);
187 }
188
189 static void
190 gtk_cell_renderer_spinner_update_size (GtkCellRendererSpinner *cell,
191                                        GtkWidget              *widget)
192 {
193   GtkCellRendererSpinnerPrivate *priv = cell->priv;
194   GdkScreen *screen;
195   GtkIconTheme *icon_theme;
196   GtkSettings *settings;
197
198   if (cell->priv->old_icon_size == cell->priv->icon_size)
199     return;
200
201   screen = gtk_widget_get_screen (GTK_WIDGET (widget));
202   icon_theme = gtk_icon_theme_get_for_screen (screen);
203   settings = gtk_settings_get_for_screen (screen);
204
205   if (!gtk_icon_size_lookup_for_settings (settings, priv->icon_size, &priv->size, NULL))
206     {
207       g_warning ("Invalid icon size %u\n", priv->icon_size);
208       priv->size = 24;
209     }
210 }
211
212 static void
213 gtk_cell_renderer_spinner_get_property (GObject    *object,
214                                         guint       param_id,
215                                         GValue     *value,
216                                         GParamSpec *pspec)
217 {
218   GtkCellRendererSpinner *cell = GTK_CELL_RENDERER_SPINNER (object);
219   GtkCellRendererSpinnerPrivate *priv = cell->priv;
220
221   switch (param_id)
222     {
223       case PROP_ACTIVE:
224         g_value_set_boolean (value, priv->active);
225         break;
226       case PROP_PULSE:
227         g_value_set_uint (value, priv->pulse);
228         break;
229       case PROP_SIZE:
230         g_value_set_enum (value, priv->icon_size);
231         break;
232       default:
233         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
234     }
235 }
236
237 static void
238 gtk_cell_renderer_spinner_set_property (GObject      *object,
239                                         guint         param_id,
240                                         const GValue *value,
241                                         GParamSpec   *pspec)
242 {
243   GtkCellRendererSpinner *cell = GTK_CELL_RENDERER_SPINNER (object);
244   GtkCellRendererSpinnerPrivate *priv = cell->priv;
245
246   switch (param_id)
247     {
248       case PROP_ACTIVE:
249         priv->active = g_value_get_boolean (value);
250         break;
251       case PROP_PULSE:
252         priv->pulse = g_value_get_uint (value);
253         break;
254       case PROP_SIZE:
255         priv->old_icon_size = priv->icon_size;
256         priv->icon_size = g_value_get_enum (value);
257         break;
258       default:
259         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
260     }
261 }
262
263 static void
264 gtk_cell_renderer_spinner_get_size (GtkCellRenderer *cellr,
265                                     GtkWidget       *widget,
266                                     GdkRectangle    *cell_area,
267                                     gint            *x_offset,
268                                     gint            *y_offset,
269                                     gint            *width,
270                                     gint            *height)
271 {
272   GtkCellRendererSpinner *cell = GTK_CELL_RENDERER_SPINNER (cellr);
273   GtkCellRendererSpinnerPrivate *priv = cell->priv;
274   gdouble align;
275   gint w, h;
276   gint xpad, ypad;
277   gfloat xalign, yalign;
278   gboolean rtl;
279
280   rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
281
282   gtk_cell_renderer_spinner_update_size (cell, widget);
283
284   g_object_get (cellr,
285                 "xpad", &xpad,
286                 "ypad", &ypad,
287                 "xalign", &xalign,
288                 "yalign", &yalign,
289                 NULL);
290   w = h = priv->size;
291
292   if (cell_area)
293     {
294       if (x_offset)
295         {
296           align = rtl ? 1.0 - xalign : xalign;
297           *x_offset = align * (cell_area->width - w);
298           *x_offset = MAX (*x_offset, 0);
299         }
300       if (y_offset)
301         {
302           align = rtl ? 1.0 - yalign : yalign;
303           *y_offset = align * (cell_area->height - h);
304           *y_offset = MAX (*y_offset, 0);
305         }
306     }
307   else
308     {
309       if (x_offset)
310         *x_offset = 0;
311       if (y_offset)
312         *y_offset = 0;
313     }
314
315   if (width)
316     *width = w;
317   if (height)
318     *height = h;
319 }
320
321 static void
322 gtk_cell_renderer_spinner_render (GtkCellRenderer      *cellr,
323                                   cairo_t              *cr,
324                                   GtkWidget            *widget,
325                                   const GdkRectangle   *background_area,
326                                   const GdkRectangle   *cell_area,
327                                   GtkCellRendererState  flags)
328 {
329   GtkCellRendererSpinner *cell = GTK_CELL_RENDERER_SPINNER (cellr);
330   GtkCellRendererSpinnerPrivate *priv = cell->priv;
331   GtkStateType state;
332   GdkRectangle pix_rect;
333   GdkRectangle draw_rect;
334   gint xpad, ypad;
335
336   if (!priv->active)
337     return;
338
339   gtk_cell_renderer_spinner_get_size (cellr, widget, (GdkRectangle *) cell_area,
340                                       &pix_rect.x, &pix_rect.y,
341                                       &pix_rect.width, &pix_rect.height);
342
343   g_object_get (cellr,
344                 "xpad", &xpad,
345                 "ypad", &ypad,
346                 NULL);
347   pix_rect.x += cell_area->x + xpad;
348   pix_rect.y += cell_area->y + ypad;
349   pix_rect.width -= xpad * 2;
350   pix_rect.height -= ypad * 2;
351
352   if (!gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect))
353     return;
354
355   state = GTK_STATE_NORMAL;
356   if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE ||
357       !gtk_cell_renderer_get_sensitive (cellr))
358     {
359       state = GTK_STATE_INSENSITIVE;
360     }
361   else
362     {
363       if ((flags & GTK_CELL_RENDERER_SELECTED) != 0)
364         {
365           if (gtk_widget_has_focus (widget))
366             state = GTK_STATE_SELECTED;
367           else
368             state = GTK_STATE_ACTIVE;
369         }
370       else
371         state = GTK_STATE_PRELIGHT;
372     }
373
374   gtk_paint_spinner (gtk_widget_get_style (widget),
375                            cr,
376                            state,
377                            widget,
378                            "cell",
379                            priv->pulse,
380                            draw_rect.x, draw_rect.y,
381                            draw_rect.width, draw_rect.height);
382 }