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