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