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