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