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