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