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