]> Pileus Git - ~andy/gtk/blob - gtk/gtkspinner.c
Add GtkSpinner::animation-duration style property
[~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 #include "gtkalias.h"
40
41
42 /**
43  * SECTION:gtkspinner
44  * @Short_description: Show a spinner animation
45  * @Title: GtkSpinner
46  * @See_also: #GtkCellRendererSpinner, #GtkProgressBar
47  *
48  * A GtkSpinner widget displays an icon-size spinning animation.
49  * It is often used as an alternative to a #GtkProgressBar for
50  * displaying indefinite activity, instead of actual progress.
51  *
52  * To start the animation, use gtk_spinner_start(), to stop it
53  * use gtk_spinner_stop().
54  */
55
56
57 #define GTK_SPINNER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SPINNER, GtkSpinnerPrivate))
58
59 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_DRAWING_AREA);
60
61 enum {
62   PROP_0,
63   PROP_ACTIVE
64 };
65
66 struct _GtkSpinnerPrivate
67 {
68   guint current;
69   guint num_steps;
70   guint cycle_duration;
71   guint timeout;
72 };
73
74 static void gtk_spinner_class_init     (GtkSpinnerClass *klass);
75 static void gtk_spinner_init           (GtkSpinner      *spinner);
76 static void gtk_spinner_dispose        (GObject         *gobject);
77 static gboolean gtk_spinner_expose     (GtkWidget       *widget,
78                                         GdkEventExpose  *event);
79 static void gtk_spinner_screen_changed (GtkWidget       *widget,
80                                         GdkScreen       *old_screen);
81 static void gtk_spinner_style_set      (GtkWidget       *widget,
82                                         GtkStyle        *prev_style);
83 static void gtk_spinner_get_property   (GObject         *object,
84                                         guint            param_id,
85                                         GValue          *value,
86                                         GParamSpec      *pspec);
87 static void gtk_spinner_set_property   (GObject         *object,
88                                         guint            param_id,
89                                         const GValue    *value,
90                                         GParamSpec      *pspec);
91
92 static AtkObject *gtk_spinner_get_accessible      (GtkWidget *widget);
93 static GType      gtk_spinner_accessible_get_type (void);
94
95 static void
96 gtk_spinner_class_init (GtkSpinnerClass *klass)
97 {
98   GObjectClass *gobject_class;
99   GtkWidgetClass *widget_class;
100
101   gtk_spinner_parent_class = g_type_class_peek_parent (klass);
102
103   gobject_class = G_OBJECT_CLASS(klass);
104   g_type_class_add_private (gobject_class, sizeof (GtkSpinnerPrivate));
105   gobject_class->dispose = gtk_spinner_dispose;
106   gobject_class->get_property = gtk_spinner_get_property;
107   gobject_class->set_property = gtk_spinner_set_property;
108
109   widget_class = GTK_WIDGET_CLASS(klass);
110   widget_class->expose_event = gtk_spinner_expose;
111   widget_class->screen_changed = gtk_spinner_screen_changed;
112   widget_class->style_set = gtk_spinner_style_set;
113   widget_class->get_accessible = gtk_spinner_get_accessible;
114
115   /* GtkSpinner:active:
116    *
117    * Whether the spinner is active
118    *
119    * Since 2.20
120    */
121   g_object_class_install_property (gobject_class,
122                                    PROP_ACTIVE,
123                                    g_param_spec_boolean ("active",
124                                                          P_("Active"),
125                                                          P_("Whether the spinner is active"),
126                                                          FALSE,
127                                                          G_PARAM_READWRITE));
128   /**
129    * GtkSpinner:num-steps:
130    *
131    * The number of steps for the spinner to complete a full loop.
132    * The animation will complete a full cycle in one second by default
133    * (see the #GtkSpinner:cycle-duration style property).
134    *
135    * Since: 2.20
136    */
137   gtk_widget_class_install_style_property (widget_class,
138                                            g_param_spec_uint ("num-steps",
139                                                              P_("Number of steps"),
140                                                              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)."),
141                                                              1,
142                                                              G_MAXUINT,
143                                                              12,
144                                                              G_PARAM_READABLE));
145
146   /**
147    * GtkSpinner::cycle-duration:
148    *
149    * The duration in milliseconds for the spinner to complete a full cycle.
150    *
151    * Since: 2.20
152    */
153   gtk_widget_class_install_style_property (widget_class,
154                                            g_param_spec_uint ("cycle-duration",
155                                                              P_("Animation duration"),
156                                                              P_("The length of time in milliseconds for the spinner to complete a full loop"),
157                                                              500,
158                                                              G_MAXUINT,
159                                                              1000,
160                                                              G_PARAM_READABLE));
161 }
162
163 static void
164 gtk_spinner_get_property (GObject    *object,
165                           guint       param_id,
166                           GValue     *value,
167                           GParamSpec *pspec)
168 {
169   GtkSpinnerPrivate *priv;
170
171   priv = GTK_SPINNER_GET_PRIVATE (object);
172
173   switch (param_id)
174     {
175       case PROP_ACTIVE:
176         g_value_set_boolean (value, priv->timeout != 0);
177         break;
178       default:
179         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
180     }
181 }
182
183 static void
184 gtk_spinner_set_property (GObject      *object,
185                           guint         param_id,
186                           const GValue *value,
187                           GParamSpec   *pspec)
188 {
189   switch (param_id)
190     {
191       case PROP_ACTIVE:
192         if (g_value_get_boolean (value))
193           gtk_spinner_start (GTK_SPINNER (object));
194         else
195           gtk_spinner_stop (GTK_SPINNER (object));
196         break;
197       default:
198         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
199     }
200 }
201
202 static void
203 gtk_spinner_init (GtkSpinner *spinner)
204 {
205   GtkSpinnerPrivate *priv;
206
207   priv = GTK_SPINNER_GET_PRIVATE (spinner);
208   priv->current = 0;
209   priv->timeout = 0;
210
211   GTK_WIDGET_SET_FLAGS (GTK_WIDGET (spinner), GTK_NO_WINDOW);
212 }
213
214 static gboolean
215 gtk_spinner_expose (GtkWidget *widget, GdkEventExpose *event)
216 {
217   GtkStateType state_type;
218   GtkSpinnerPrivate *priv;
219   int width, height;
220
221   priv = GTK_SPINNER_GET_PRIVATE (widget);
222
223   width = widget->allocation.width;
224   height = widget->allocation.height;
225
226   if ((width < 12) || (height <12))
227     gtk_widget_set_size_request (widget, 12, 12);
228
229   state_type = GTK_STATE_NORMAL;
230   if (!GTK_WIDGET_IS_SENSITIVE (widget))
231    state_type = GTK_STATE_INSENSITIVE;
232
233   gtk_paint_spinner (widget->style,
234                      widget->window,
235                      state_type,
236                      priv->current,
237                      event->area.x, event->area.y,
238                      event->area.width, event->area.height);
239
240   return FALSE;
241 }
242
243 static void
244 gtk_spinner_screen_changed (GtkWidget* widget, GdkScreen* old_screen)
245 {
246   GtkSpinner *spinner;
247   GdkScreen* new_screen;
248   GdkColormap* colormap;
249
250   spinner = GTK_SPINNER(widget);
251
252   new_screen = gtk_widget_get_screen (widget);
253   colormap = gdk_screen_get_rgba_colormap (new_screen);
254
255   if (!colormap)
256     {
257       colormap = gdk_screen_get_rgb_colormap (new_screen);
258     }
259
260   gtk_widget_set_colormap (widget, colormap);
261 }
262
263 static void
264 gtk_spinner_style_set (GtkWidget *widget,
265                        GtkStyle  *prev_style)
266 {
267   GtkSpinnerPrivate *priv;
268
269   priv = GTK_SPINNER_GET_PRIVATE (widget);
270
271   gtk_widget_style_get (GTK_WIDGET (widget),
272                         "num-steps", &(priv->num_steps),
273                         "cycle-duration", &(priv->cycle_duration),
274                         NULL);
275
276   if (priv->current > priv->num_steps)
277     priv->current = 0;
278 }
279
280 static gboolean
281 gtk_spinner_timeout (gpointer data)
282 {
283   GtkSpinnerPrivate *priv;
284
285   priv = GTK_SPINNER_GET_PRIVATE (data);
286
287   if (priv->current + 1 >= priv->num_steps)
288     priv->current = 0;
289   else
290     priv->current++;
291
292   gtk_widget_queue_draw (GTK_WIDGET (data));
293
294   return TRUE;
295 }
296 static void
297 gtk_spinner_dispose (GObject *gobject)
298 {
299   GtkSpinnerPrivate *priv;
300
301   priv = GTK_SPINNER_GET_PRIVATE (gobject);
302
303   if (priv->timeout != 0)
304     {
305       g_source_remove (priv->timeout);
306       priv->timeout = 0;
307     }
308
309   G_OBJECT_CLASS (gtk_spinner_parent_class)->dispose (gobject);
310 }
311
312 static GType
313 gtk_spinner_accessible_factory_get_accessible_type (void)
314 {
315   return gtk_spinner_accessible_get_type ();
316 }
317
318 static AtkObject *
319 gtk_spinner_accessible_new (GObject *obj)
320 {
321   AtkObject *accessible;
322
323   g_return_val_if_fail (GTK_IS_WIDGET (obj), NULL);
324
325   accessible = g_object_new (gtk_spinner_accessible_get_type (), NULL);
326   atk_object_initialize (accessible, obj);
327
328   return accessible;
329 }
330
331 static AtkObject*
332 gtk_spinner_accessible_factory_create_accessible (GObject *obj)
333 {
334   return gtk_spinner_accessible_new (obj);
335 }
336
337 static void
338 gtk_spinner_accessible_factory_class_init (AtkObjectFactoryClass *klass)
339 {
340   klass->create_accessible = gtk_spinner_accessible_factory_create_accessible;
341   klass->get_accessible_type = gtk_spinner_accessible_factory_get_accessible_type;
342 }
343
344 static GType
345 gtk_spinner_accessible_factory_get_type (void)
346 {
347   static GType type = 0;
348
349   if (!type)
350     {
351       const GTypeInfo tinfo =
352       {
353         sizeof (AtkObjectFactoryClass),
354         NULL,           /* base_init */
355         NULL,           /* base_finalize */
356         (GClassInitFunc) gtk_spinner_accessible_factory_class_init,
357         NULL,           /* class_finalize */
358         NULL,           /* class_data */
359         sizeof (AtkObjectFactory),
360         0,             /* n_preallocs */
361         NULL, NULL
362       };
363
364       type = g_type_register_static (ATK_TYPE_OBJECT_FACTORY,
365                                     I_("GtkSpinnerAccessibleFactory"),
366                                     &tinfo, 0);
367     }
368   return type;
369 }
370
371 static AtkObjectClass *a11y_parent_class = NULL;
372
373 static void
374 gtk_spinner_accessible_initialize (AtkObject *accessible,
375                                    gpointer   widget)
376 {
377   atk_object_set_name (accessible, _("Spinner"));
378   atk_object_set_description (accessible, _("Provides visual status"));
379
380   a11y_parent_class->initialize (accessible, widget);
381 }
382
383 static void
384 gtk_spinner_accessible_class_init (AtkObjectClass *klass)
385 {
386   a11y_parent_class = g_type_class_peek_parent (klass);
387
388   klass->initialize = gtk_spinner_accessible_initialize;
389 }
390
391 static void
392 gtk_spinner_accessible_image_get_size (AtkImage *image,
393                                        gint     *width,
394                                        gint     *height)
395 {
396   GtkWidget *widget;
397
398   widget = GTK_ACCESSIBLE (image)->widget;
399   if (!widget)
400     {
401       *width = *height = 0;
402     }
403   else
404     {
405       *width = widget->allocation.width;
406       *height = widget->allocation.height;
407     }
408 }
409
410 static void
411 gtk_spinner_accessible_image_interface_init (AtkImageIface *iface)
412 {
413   iface->get_image_size = gtk_spinner_accessible_image_get_size;
414 }
415
416 static GType
417 gtk_spinner_accessible_get_type (void)
418 {
419   static GType type = 0;
420
421   /* Action interface
422      Name etc. ... */
423   if (G_UNLIKELY (type == 0))
424     {
425       const GInterfaceInfo atk_image_info = {
426               (GInterfaceInitFunc) gtk_spinner_accessible_image_interface_init,
427               (GInterfaceFinalizeFunc) NULL,
428               NULL
429       };
430       GType type;
431       GType parent_atk_type;
432       GTypeInfo tinfo = { 0 };
433       GTypeQuery query;
434       AtkObjectFactory *factory;
435
436       if ((type = g_type_from_name ("GtkSpinnerAccessible")))
437         return type;
438
439       factory = atk_registry_get_factory (atk_get_default_registry (),
440                                           GTK_TYPE_IMAGE);
441       if (!factory)
442         return G_TYPE_INVALID;
443
444       parent_atk_type = atk_object_factory_get_accessible_type (factory);
445       if (!parent_atk_type)
446         return G_TYPE_INVALID;
447
448       /*
449        * Figure out the size of the class and instance
450        * we are deriving from
451        */
452       g_type_query (parent_atk_type, &query);
453
454       tinfo.class_init = (GClassInitFunc) gtk_spinner_accessible_class_init;
455       tinfo.class_size    = query.class_size;
456       tinfo.instance_size = query.instance_size;
457
458       /* Register the type */
459       type = g_type_register_static (parent_atk_type,
460                                      "GtkSpinnerAccessible",
461                                      &tinfo, 0);
462
463       g_type_add_interface_static (type, ATK_TYPE_IMAGE,
464                                    &atk_image_info);
465     }
466
467   return type;
468 }
469
470 static AtkObject *
471 gtk_spinner_get_accessible (GtkWidget *widget)
472 {
473   static gboolean first_time = TRUE;
474
475   if (first_time)
476     {
477       AtkObjectFactory *factory;
478       AtkRegistry *registry;
479       GType derived_type;
480       GType derived_atk_type;
481
482       /*
483        * Figure out whether accessibility is enabled by looking at the
484        * type of the accessible object which would be created for
485        * the parent type of GtkSpinner.
486        */
487       derived_type = g_type_parent (GTK_TYPE_SPINNER);
488
489       registry = atk_get_default_registry ();
490       factory = atk_registry_get_factory (registry,
491                                           derived_type);
492       derived_atk_type = atk_object_factory_get_accessible_type (factory);
493       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
494         atk_registry_set_factory_type (registry,
495                                        GTK_TYPE_SPINNER,
496                                        gtk_spinner_accessible_factory_get_type ());
497       first_time = FALSE;
498     }
499   return GTK_WIDGET_CLASS (gtk_spinner_parent_class)->get_accessible (widget);
500 }
501
502 /**
503  * gtk_spinner_new:
504  *
505  * Returns a new spinner widget. Not yet started.
506  *
507  * Return value: a new #GtkSpinner
508  *
509  * Since: 2.20
510  */
511 GtkWidget *
512 gtk_spinner_new (void)
513 {
514   return g_object_new (GTK_TYPE_SPINNER, NULL);
515 }
516
517 /**
518  * gtk_spinner_start:
519  * @spinner: a #GtkSpinner
520  *
521  * Starts the animation of the spinner.
522  *
523  * Since: 2.20
524  */
525 void
526 gtk_spinner_start (GtkSpinner *spinner)
527 {
528   GtkSpinnerPrivate *priv;
529
530   g_return_if_fail (GTK_IS_SPINNER (spinner));
531
532   priv = GTK_SPINNER_GET_PRIVATE (spinner);
533
534   if (priv->timeout != 0)
535     return;
536
537   priv->timeout = gdk_threads_add_timeout (priv->cycle_duration / priv->num_steps, gtk_spinner_timeout, spinner);
538   g_object_notify (G_OBJECT (spinner), "active");
539 }
540
541 /**
542  * gtk_spinner_stop:
543  * @spinner: a #GtkSpinner
544  *
545  * Stops the animation of the spinner.
546  *
547  * Since: 2.20
548  */
549 void
550 gtk_spinner_stop (GtkSpinner *spinner)
551 {
552   GtkSpinnerPrivate *priv;
553
554   g_return_if_fail (GTK_IS_SPINNER (spinner));
555
556   priv = GTK_SPINNER_GET_PRIVATE (spinner);
557   if (priv->timeout == 0)
558     return;
559
560   g_source_remove (priv->timeout);
561   priv->timeout = 0;
562   g_object_notify (G_OBJECT (spinner), "active");
563 }
564
565 #define __GTK_SPINNER_C__
566 #include "gtkaliasdef.c"