]> Pileus Git - ~andy/gtk/blob - gtk/gtkspinner.c
GtkSpinner: Use style context for rendering.
[~andy/gtk] / gtk / gtkspinner.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2007 John Stowers, Neil Jagdish Patel.
4  * Copyright (C) 2009 Bastien Nocera, David Zeuthen
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser 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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser 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  * Code adapted from egg-spinner
22  * by Christian Hergert <christian.hergert@gmail.com>
23  */
24
25 /*
26  * Modified by the GTK+ Team and others 2007.  See the AUTHORS
27  * file for a list of people on the GTK+ Team.  See the ChangeLog
28  * files for a list of changes.  These files are distributed with
29  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30  */
31
32 #include "config.h"
33
34 #include "gtkintl.h"
35 #include "gtkaccessible.h"
36 #include "gtkimage.h"
37 #include "gtkspinner.h"
38 #include "gtkstyle.h"
39
40
41 /**
42  * SECTION:gtkspinner
43  * @Short_description: Show a spinner animation
44  * @Title: GtkSpinner
45  * @See_also: #GtkCellRendererSpinner, #GtkProgressBar
46  *
47  * A GtkSpinner widget displays an icon-size spinning animation.
48  * It is often used as an alternative to a #GtkProgressBar for
49  * displaying indefinite activity, instead of actual progress.
50  *
51  * To start the animation, use gtk_spinner_start(), to stop it
52  * use gtk_spinner_stop().
53  */
54
55
56 #define SPINNER_SIZE 12
57
58 enum {
59   PROP_0,
60   PROP_ACTIVE
61 };
62
63 struct _GtkSpinnerPrivate
64 {
65   gboolean active;
66 };
67
68 static gboolean gtk_spinner_draw       (GtkWidget       *widget,
69                                         cairo_t         *cr);
70 static void gtk_spinner_get_property   (GObject         *object,
71                                         guint            param_id,
72                                         GValue          *value,
73                                         GParamSpec      *pspec);
74 static void gtk_spinner_set_property   (GObject         *object,
75                                         guint            param_id,
76                                         const GValue    *value,
77                                         GParamSpec      *pspec);
78 static void gtk_spinner_set_active     (GtkSpinner      *spinner,
79                                         gboolean         active);
80 static void gtk_spinner_get_preferred_width (GtkWidget  *widget,
81                                         gint            *minimum_size,
82                                         gint            *natural_size);
83 static void gtk_spinner_get_preferred_height (GtkWidget *widget,
84                                         gint            *minimum_size,
85                                         gint            *natural_size);
86
87 static AtkObject *gtk_spinner_get_accessible      (GtkWidget *widget);
88 static GType      gtk_spinner_accessible_get_type (void);
89
90 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET)
91
92 static void
93 gtk_spinner_class_init (GtkSpinnerClass *klass)
94 {
95   GObjectClass *gobject_class;
96   GtkWidgetClass *widget_class;
97
98   gobject_class = G_OBJECT_CLASS(klass);
99   g_type_class_add_private (gobject_class, sizeof (GtkSpinnerPrivate));
100   gobject_class->get_property = gtk_spinner_get_property;
101   gobject_class->set_property = gtk_spinner_set_property;
102
103   widget_class = GTK_WIDGET_CLASS(klass);
104   widget_class->draw = gtk_spinner_draw;
105   widget_class->get_accessible = gtk_spinner_get_accessible;
106   widget_class->get_preferred_width = gtk_spinner_get_preferred_width;
107   widget_class->get_preferred_height = gtk_spinner_get_preferred_height;
108
109   /* GtkSpinner:active:
110    *
111    * Whether the spinner is active
112    *
113    * Since: 2.20
114    */
115   g_object_class_install_property (gobject_class,
116                                    PROP_ACTIVE,
117                                    g_param_spec_boolean ("active",
118                                                          P_("Active"),
119                                                          P_("Whether the spinner is active"),
120                                                          FALSE,
121                                                          G_PARAM_READWRITE));
122   /**
123    * GtkSpinner:num-steps:
124    *
125    * The number of steps for the spinner to complete a full loop.
126    * The animation will complete a full cycle in one second by default
127    * (see the #GtkSpinner:cycle-duration style property).
128    *
129    * Since: 2.20
130    *
131    * Deprecated: 3.0
132    */
133   gtk_widget_class_install_style_property (widget_class,
134                                            g_param_spec_uint ("num-steps",
135                                                              P_("Number of steps"),
136                                                              P_("The number of steps for the spinner to complete a full loop. The animation will complete a full cycle in one second by default (see #GtkSpinner:cycle-duration)."),
137                                                              1,
138                                                              G_MAXUINT,
139                                                              12,
140                                                              G_PARAM_READABLE));
141
142   /**
143    * GtkSpinner:cycle-duration:
144    *
145    * The duration in milliseconds for the spinner to complete a full cycle.
146    *
147    * Since: 2.20
148    *
149    * Deprecated: 3.0
150    */
151   gtk_widget_class_install_style_property (widget_class,
152                                            g_param_spec_uint ("cycle-duration",
153                                                              P_("Animation duration"),
154                                                              P_("The length of time in milliseconds for the spinner to complete a full loop"),
155                                                              500,
156                                                              G_MAXUINT,
157                                                              1000,
158                                                              G_PARAM_READABLE));
159 }
160
161 static void
162 gtk_spinner_get_property (GObject    *object,
163                           guint       param_id,
164                           GValue     *value,
165                           GParamSpec *pspec)
166 {
167   GtkSpinnerPrivate *priv;
168
169   priv = GTK_SPINNER (object)->priv;
170
171   switch (param_id)
172     {
173       case PROP_ACTIVE:
174         g_value_set_boolean (value, priv->active);
175         break;
176       default:
177         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
178     }
179 }
180
181 static void
182 gtk_spinner_set_property (GObject      *object,
183                           guint         param_id,
184                           const GValue *value,
185                           GParamSpec   *pspec)
186 {
187   switch (param_id)
188     {
189       case PROP_ACTIVE:
190         gtk_spinner_set_active (GTK_SPINNER (object), g_value_get_boolean (value));
191         break;
192       default:
193         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
194     }
195 }
196
197 static void
198 gtk_spinner_init (GtkSpinner *spinner)
199 {
200   GtkSpinnerPrivate *priv;
201   GtkStyleContext *context;
202
203   priv = G_TYPE_INSTANCE_GET_PRIVATE (spinner,
204                                       GTK_TYPE_SPINNER,
205                                       GtkSpinnerPrivate);
206   spinner->priv = priv;
207
208   gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
209
210   context = gtk_widget_get_style_context (GTK_WIDGET (spinner));
211   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINNER);
212 }
213
214 static void
215 gtk_spinner_get_preferred_width (GtkWidget *widget,
216                                  gint      *minimum_size,
217                                  gint      *natural_size)
218 {
219   if (minimum_size)
220     *minimum_size = SPINNER_SIZE;
221
222   if (natural_size)
223     *natural_size = SPINNER_SIZE;
224 }
225
226 static void
227 gtk_spinner_get_preferred_height (GtkWidget *widget,
228                                   gint      *minimum_size,
229                                   gint      *natural_size)
230 {
231   if (minimum_size)
232     *minimum_size = SPINNER_SIZE;
233
234   if (natural_size)
235     *natural_size = SPINNER_SIZE;
236 }
237
238 static gboolean
239 gtk_spinner_draw (GtkWidget *widget,
240                   cairo_t   *cr)
241 {
242   GtkSpinnerPrivate *priv;
243   GtkStyleContext *context;
244   GtkStateFlags state;
245
246   priv = GTK_SPINNER (widget)->priv;
247   context = gtk_widget_get_style_context (widget);
248   state = gtk_widget_get_state_flags (widget);
249
250   gtk_style_context_set_state (context, state);
251   gtk_render_activity (context, cr, 0, 0,
252                        gtk_widget_get_allocated_width (widget),
253                        gtk_widget_get_allocated_height (widget));
254
255   return FALSE;
256 }
257
258 static void
259 gtk_spinner_set_active (GtkSpinner *spinner,
260                         gboolean    active)
261 {
262   GtkSpinnerPrivate *priv = spinner->priv;
263
264   active = !!active;
265
266   if (priv->active != active)
267     {
268       priv->active = active;
269
270       g_object_notify (G_OBJECT (spinner), "active");
271
272       if (active)
273         gtk_widget_set_state_flags (GTK_WIDGET (spinner),
274                                     GTK_STATE_FLAG_ACTIVE, FALSE);
275       else
276         gtk_widget_unset_state_flags (GTK_WIDGET (spinner),
277                                       GTK_STATE_FLAG_ACTIVE);
278     }
279 }
280
281 /* accessible implementation */
282
283 static void
284 gtk_spinner_accessible_image_get_size (AtkImage *image,
285                                        gint     *width,
286                                        gint     *height)
287 {
288   GtkAllocation allocation;
289   GtkWidget *widget;
290
291   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
292   if (widget == NULL)
293     {
294       *width = *height = 0;
295     }
296   else
297     {
298       gtk_widget_get_allocation (widget, &allocation);
299       *width = allocation.width;
300       *height = allocation.height;
301     }
302 }
303
304 static void
305 gtk_spinner_accessible_image_iface_init (AtkImageIface *iface)
306 {
307   iface->get_image_size = gtk_spinner_accessible_image_get_size;
308 }
309
310 /* dummy typedef */
311 typedef struct _GtkSpinnerAccessible            GtkSpinnerAccessible;
312 typedef struct _GtkSpinnerAccessibleClass       GtkSpinnerAccessibleClass;
313
314 ATK_DEFINE_TYPE_WITH_CODE (GtkSpinnerAccessible,
315                            gtk_spinner_accessible,
316                            GTK_TYPE_IMAGE,
317                            G_IMPLEMENT_INTERFACE (ATK_TYPE_IMAGE,
318                                                   gtk_spinner_accessible_image_iface_init));
319
320 static void
321 gtk_spinner_accessible_initialize (AtkObject *accessible,
322                                    gpointer   widget)
323 {
324   ATK_OBJECT_CLASS (gtk_spinner_accessible_parent_class)->initialize (accessible, widget);
325
326   atk_object_set_name (accessible, C_("throbbing progress animation widget", "Spinner"));
327   atk_object_set_description (accessible, _("Provides visual indication of progress"));
328 }
329
330 static void
331 gtk_spinner_accessible_class_init (GtkSpinnerAccessibleClass *klass)
332 {
333   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
334
335   atk_class->initialize = gtk_spinner_accessible_initialize;
336 }
337
338 static void
339 gtk_spinner_accessible_init (GtkSpinnerAccessible *self)
340 {
341 }
342
343 /* factory */
344 typedef AtkObjectFactory        GtkSpinnerAccessibleFactory;
345 typedef AtkObjectFactoryClass   GtkSpinnerAccessibleFactoryClass;
346
347 G_DEFINE_TYPE (GtkSpinnerAccessibleFactory,
348                gtk_spinner_accessible_factory,
349                ATK_TYPE_OBJECT_FACTORY);
350
351 static GType
352 gtk_spinner_accessible_factory_get_accessible_type (void)
353 {
354   return gtk_spinner_accessible_get_type ();
355 }
356
357 static AtkObject *
358 gtk_spinner_accessible_factory_create_accessible (GObject *obj)
359 {
360   AtkObject *accessible;
361
362   accessible = g_object_new (gtk_spinner_accessible_get_type (), NULL);
363   atk_object_initialize (accessible, obj);
364
365   return accessible;
366 }
367
368 static void
369 gtk_spinner_accessible_factory_class_init (AtkObjectFactoryClass *klass)
370 {
371   klass->create_accessible = gtk_spinner_accessible_factory_create_accessible;
372   klass->get_accessible_type = gtk_spinner_accessible_factory_get_accessible_type;
373 }
374
375 static void
376 gtk_spinner_accessible_factory_init (AtkObjectFactory *factory)
377 {
378 }
379
380 static AtkObject *
381 gtk_spinner_get_accessible (GtkWidget *widget)
382 {
383   static gboolean first_time = TRUE;
384
385   if (first_time)
386     {
387       AtkObjectFactory *factory;
388       AtkRegistry *registry;
389       GType derived_type;
390       GType derived_atk_type;
391
392       /*
393        * Figure out whether accessibility is enabled by looking at the
394        * type of the accessible object which would be created for
395        * the parent type of GtkSpinner.
396        */
397       derived_type = g_type_parent (GTK_TYPE_SPINNER);
398
399       registry = atk_get_default_registry ();
400       factory = atk_registry_get_factory (registry, derived_type);
401       derived_atk_type = atk_object_factory_get_accessible_type (factory);
402       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
403         atk_registry_set_factory_type (registry,
404                                        GTK_TYPE_SPINNER,
405                                        gtk_spinner_accessible_factory_get_type ());
406       first_time = FALSE;
407     }
408
409   return GTK_WIDGET_CLASS (gtk_spinner_parent_class)->get_accessible (widget);
410 }
411
412 /**
413  * gtk_spinner_new:
414  *
415  * Returns a new spinner widget. Not yet started.
416  *
417  * Return value: a new #GtkSpinner
418  *
419  * Since: 2.20
420  */
421 GtkWidget *
422 gtk_spinner_new (void)
423 {
424   return g_object_new (GTK_TYPE_SPINNER, NULL);
425 }
426
427 /**
428  * gtk_spinner_start:
429  * @spinner: a #GtkSpinner
430  *
431  * Starts the animation of the spinner.
432  *
433  * Since: 2.20
434  */
435 void
436 gtk_spinner_start (GtkSpinner *spinner)
437 {
438   g_return_if_fail (GTK_IS_SPINNER (spinner));
439
440   gtk_spinner_set_active (spinner, TRUE);
441 }
442
443 /**
444  * gtk_spinner_stop:
445  * @spinner: a #GtkSpinner
446  *
447  * Stops the animation of the spinner.
448  *
449  * Since: 2.20
450  */
451 void
452 gtk_spinner_stop (GtkSpinner *spinner)
453 {
454   g_return_if_fail (GTK_IS_SPINNER (spinner));
455
456   gtk_spinner_set_active (spinner, FALSE);
457 }