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