]> Pileus Git - ~andy/gtk/blob - gtk/gtkspinner.c
docs: Fix building with latest GDK changes
[~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
40
41 /**
42  * SECTION:gtkspinner
43  * @Short_description: Show a spinner animation
44  * @Title: GtkSpinner
45  * @See_also: #GtkCellRendererSpinner, #GtkProgressBar
46  *
47  * A GtkSpinner widget displays an icon-size spinning animation.
48  * It is often used as an alternative to a #GtkProgressBar for
49  * displaying indefinite activity, instead of actual progress.
50  *
51  * To start the animation, use gtk_spinner_start(), to stop it
52  * use gtk_spinner_stop().
53  */
54
55
56 #define SPINNER_SIZE 12
57
58 enum {
59   PROP_0,
60   PROP_ACTIVE
61 };
62
63 struct _GtkSpinnerPrivate
64 {
65   guint current;
66   guint num_steps;
67   guint cycle_duration;
68   gboolean active;
69   guint timeout;
70 };
71
72 static void gtk_spinner_dispose        (GObject         *gobject);
73 static void gtk_spinner_map            (GtkWidget       *widget);
74 static void gtk_spinner_unmap          (GtkWidget       *widget);
75 static gboolean gtk_spinner_draw       (GtkWidget       *widget,
76                                         cairo_t         *cr);
77 static void gtk_spinner_style_set      (GtkWidget       *widget,
78                                         GtkStyle        *prev_style);
79 static void gtk_spinner_get_property   (GObject         *object,
80                                         guint            param_id,
81                                         GValue          *value,
82                                         GParamSpec      *pspec);
83 static void gtk_spinner_set_property   (GObject         *object,
84                                         guint            param_id,
85                                         const GValue    *value,
86                                         GParamSpec      *pspec);
87 static void gtk_spinner_set_active     (GtkSpinner      *spinner,
88                                         gboolean         active);
89 static void gtk_spinner_get_preferred_width (GtkWidget  *widget,
90                                         gint            *minimum_size,
91                                         gint            *natural_size);
92 static void gtk_spinner_get_preferred_height (GtkWidget *widget,
93                                         gint            *minimum_size,
94                                         gint            *natural_size);
95
96 static AtkObject *gtk_spinner_get_accessible      (GtkWidget *widget);
97 static GType      gtk_spinner_accessible_get_type (void);
98
99 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET)
100
101 static void
102 gtk_spinner_class_init (GtkSpinnerClass *klass)
103 {
104   GObjectClass *gobject_class;
105   GtkWidgetClass *widget_class;
106
107   gobject_class = G_OBJECT_CLASS(klass);
108   g_type_class_add_private (gobject_class, sizeof (GtkSpinnerPrivate));
109   gobject_class->dispose = gtk_spinner_dispose;
110   gobject_class->get_property = gtk_spinner_get_property;
111   gobject_class->set_property = gtk_spinner_set_property;
112
113   widget_class = GTK_WIDGET_CLASS(klass);
114   widget_class->map = gtk_spinner_map;
115   widget_class->unmap = gtk_spinner_unmap;
116   widget_class->draw = gtk_spinner_draw;
117   widget_class->style_set = gtk_spinner_style_set;
118   widget_class->get_accessible = gtk_spinner_get_accessible;
119   widget_class->get_preferred_width = gtk_spinner_get_preferred_width;
120   widget_class->get_preferred_height = gtk_spinner_get_preferred_height;
121
122   /* GtkSpinner:active:
123    *
124    * Whether the spinner is active
125    *
126    * Since: 2.20
127    */
128   g_object_class_install_property (gobject_class,
129                                    PROP_ACTIVE,
130                                    g_param_spec_boolean ("active",
131                                                          P_("Active"),
132                                                          P_("Whether the spinner is active"),
133                                                          FALSE,
134                                                          G_PARAM_READWRITE));
135   /**
136    * GtkSpinner:num-steps:
137    *
138    * The number of steps for the spinner to complete a full loop.
139    * The animation will complete a full cycle in one second by default
140    * (see the #GtkSpinner:cycle-duration style property).
141    *
142    * Since: 2.20
143    */
144   gtk_widget_class_install_style_property (widget_class,
145                                            g_param_spec_uint ("num-steps",
146                                                              P_("Number of steps"),
147                                                              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)."),
148                                                              1,
149                                                              G_MAXUINT,
150                                                              12,
151                                                              G_PARAM_READABLE));
152
153   /**
154    * GtkSpinner:cycle-duration:
155    *
156    * The duration in milliseconds for the spinner to complete a full cycle.
157    *
158    * Since: 2.20
159    */
160   gtk_widget_class_install_style_property (widget_class,
161                                            g_param_spec_uint ("cycle-duration",
162                                                              P_("Animation duration"),
163                                                              P_("The length of time in milliseconds for the spinner to complete a full loop"),
164                                                              500,
165                                                              G_MAXUINT,
166                                                              1000,
167                                                              G_PARAM_READABLE));
168 }
169
170 static void
171 gtk_spinner_get_property (GObject    *object,
172                           guint       param_id,
173                           GValue     *value,
174                           GParamSpec *pspec)
175 {
176   GtkSpinnerPrivate *priv;
177
178   priv = GTK_SPINNER (object)->priv;
179
180   switch (param_id)
181     {
182       case PROP_ACTIVE:
183         g_value_set_boolean (value, priv->active);
184         break;
185       default:
186         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
187     }
188 }
189
190 static void
191 gtk_spinner_set_property (GObject      *object,
192                           guint         param_id,
193                           const GValue *value,
194                           GParamSpec   *pspec)
195 {
196   switch (param_id)
197     {
198       case PROP_ACTIVE:
199         gtk_spinner_set_active (GTK_SPINNER (object), g_value_get_boolean (value));
200         break;
201       default:
202         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
203     }
204 }
205
206 static void
207 gtk_spinner_init (GtkSpinner *spinner)
208 {
209   GtkSpinnerPrivate *priv;
210
211   priv = G_TYPE_INSTANCE_GET_PRIVATE (spinner,
212                                       GTK_TYPE_SPINNER,
213                                       GtkSpinnerPrivate);
214   priv->current = 0;
215   priv->timeout = 0;
216
217   spinner->priv = priv;
218
219   gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
220 }
221
222 static void
223 gtk_spinner_get_preferred_width (GtkWidget *widget,
224                                  gint      *minimum_size,
225                                  gint      *natural_size)
226 {
227   if (minimum_size)
228     *minimum_size = SPINNER_SIZE;
229
230   if (natural_size)
231     *natural_size = SPINNER_SIZE;
232 }
233
234 static void
235 gtk_spinner_get_preferred_height (GtkWidget *widget,
236                                   gint      *minimum_size,
237                                   gint      *natural_size)
238 {
239   if (minimum_size)
240     *minimum_size = SPINNER_SIZE;
241
242   if (natural_size)
243     *natural_size = SPINNER_SIZE;
244 }
245
246 static gboolean
247 gtk_spinner_draw (GtkWidget *widget,
248                   cairo_t   *cr)
249 {
250   GtkStateType state_type;
251   GtkSpinnerPrivate *priv;
252
253   priv = GTK_SPINNER (widget)->priv;
254
255   state_type = GTK_STATE_NORMAL;
256   if (!gtk_widget_is_sensitive (widget))
257    state_type = GTK_STATE_INSENSITIVE;
258
259   gtk_paint_spinner (gtk_widget_get_style (widget),
260                      cr,
261                      state_type,
262                      widget,
263                      "spinner",
264                      priv->current,
265                      0, 0,
266                      gtk_widget_get_allocated_width (widget),
267                      gtk_widget_get_allocated_height (widget));
268
269   return FALSE;
270 }
271
272 static gboolean
273 gtk_spinner_timeout (gpointer data)
274 {
275   GtkSpinnerPrivate *priv;
276
277   priv = GTK_SPINNER (data)->priv;
278
279   if (priv->current + 1 >= priv->num_steps)
280     priv->current = 0;
281   else
282     priv->current++;
283
284   gtk_widget_queue_draw (GTK_WIDGET (data));
285
286   return TRUE;
287 }
288
289 static void
290 gtk_spinner_add_timeout (GtkSpinner *spinner)
291 {
292   GtkSpinnerPrivate *priv;
293
294   priv = spinner->priv;
295
296   g_assert (priv->timeout == 0);
297   priv->timeout = gdk_threads_add_timeout ((guint) priv->cycle_duration / priv->num_steps, gtk_spinner_timeout, spinner);
298 }
299
300 static void
301 gtk_spinner_remove_timeout (GtkSpinner *spinner)
302 {
303   GtkSpinnerPrivate *priv;
304
305   priv = spinner->priv;
306
307   g_source_remove (priv->timeout);
308   priv->timeout = 0;
309 }
310
311 static void
312 gtk_spinner_map (GtkWidget *widget)
313 {
314   GtkSpinner *spinner = GTK_SPINNER (widget);
315   GtkSpinnerPrivate *priv = spinner->priv;
316
317   GTK_WIDGET_CLASS (gtk_spinner_parent_class)->map (widget);
318
319   if (priv->active && priv->timeout == 0)
320     gtk_spinner_add_timeout (spinner);
321 }
322
323 static void
324 gtk_spinner_unmap (GtkWidget *widget)
325 {
326   GtkSpinner *spinner = GTK_SPINNER (widget);
327   GtkSpinnerPrivate *priv = spinner->priv;
328
329   if (priv->timeout != 0)
330     gtk_spinner_remove_timeout (spinner);
331
332   GTK_WIDGET_CLASS (gtk_spinner_parent_class)->unmap (widget);
333 }
334
335 static void
336 gtk_spinner_style_set (GtkWidget *widget,
337                        GtkStyle  *prev_style)
338 {
339   GtkSpinnerPrivate *priv;
340
341   priv = GTK_SPINNER (widget)->priv;
342
343   gtk_widget_style_get (GTK_WIDGET (widget),
344                         "num-steps", &(priv->num_steps),
345                         "cycle-duration", &(priv->cycle_duration),
346                         NULL);
347
348   if (priv->current > priv->num_steps)
349     priv->current = 0;
350 }
351
352 static void
353 gtk_spinner_dispose (GObject *gobject)
354 {
355   GtkSpinnerPrivate *priv;
356
357   priv = GTK_SPINNER (gobject)->priv;
358
359   if (priv->timeout != 0)
360     {
361       gtk_spinner_remove_timeout (GTK_SPINNER (gobject));
362     }
363
364   G_OBJECT_CLASS (gtk_spinner_parent_class)->dispose (gobject);
365 }
366
367 static void
368 gtk_spinner_set_active (GtkSpinner *spinner,
369                         gboolean    active)
370 {
371   GtkSpinnerPrivate *priv = spinner->priv;
372
373   active = !!active;
374
375   if (priv->active != active)
376     {
377       priv->active = active;
378
379       g_object_notify (G_OBJECT (spinner), "active");
380
381       if (active &&
382           gtk_widget_get_realized (GTK_WIDGET (spinner)) &&
383           priv->timeout == 0)
384         {
385           gtk_spinner_add_timeout (spinner);
386         }
387       else if (!active && priv->timeout != 0)
388         {
389           gtk_spinner_remove_timeout (spinner);
390         }
391     }
392 }
393
394 /* accessible implementation */
395
396 static void
397 gtk_spinner_accessible_image_get_size (AtkImage *image,
398                                        gint     *width,
399                                        gint     *height)
400 {
401   GtkAllocation allocation;
402   GtkWidget *widget;
403
404   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
405   if (widget == NULL)
406     {
407       *width = *height = 0;
408     }
409   else
410     {
411       gtk_widget_get_allocation (widget, &allocation);
412       *width = allocation.width;
413       *height = allocation.height;
414     }
415 }
416
417 static void
418 gtk_spinner_accessible_image_iface_init (AtkImageIface *iface)
419 {
420   iface->get_image_size = gtk_spinner_accessible_image_get_size;
421 }
422
423 /* dummy typedef */
424 typedef struct _GtkSpinnerAccessible            GtkSpinnerAccessible;
425 typedef struct _GtkSpinnerAccessibleClass       GtkSpinnerAccessibleClass;
426
427 ATK_DEFINE_TYPE_WITH_CODE (GtkSpinnerAccessible,
428                            gtk_spinner_accessible,
429                            GTK_TYPE_IMAGE,
430                            G_IMPLEMENT_INTERFACE (ATK_TYPE_IMAGE,
431                                                   gtk_spinner_accessible_image_iface_init));
432
433 static void
434 gtk_spinner_accessible_initialize (AtkObject *accessible,
435                                    gpointer   widget)
436 {
437   ATK_OBJECT_CLASS (gtk_spinner_accessible_parent_class)->initialize (accessible, widget);
438
439   atk_object_set_name (accessible, C_("throbbing progress animation widget", "Spinner"));
440   atk_object_set_description (accessible, _("Provides visual indication of progress"));
441 }
442
443 static void
444 gtk_spinner_accessible_class_init (GtkSpinnerAccessibleClass *klass)
445 {
446   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
447
448   atk_class->initialize = gtk_spinner_accessible_initialize;
449 }
450
451 static void
452 gtk_spinner_accessible_init (GtkSpinnerAccessible *self)
453 {
454 }
455
456 /* factory */
457 typedef AtkObjectFactory        GtkSpinnerAccessibleFactory;
458 typedef AtkObjectFactoryClass   GtkSpinnerAccessibleFactoryClass;
459
460 G_DEFINE_TYPE (GtkSpinnerAccessibleFactory,
461                gtk_spinner_accessible_factory,
462                ATK_TYPE_OBJECT_FACTORY);
463
464 static GType
465 gtk_spinner_accessible_factory_get_accessible_type (void)
466 {
467   return gtk_spinner_accessible_get_type ();
468 }
469
470 static AtkObject *
471 gtk_spinner_accessible_factory_create_accessible (GObject *obj)
472 {
473   AtkObject *accessible;
474
475   accessible = g_object_new (gtk_spinner_accessible_get_type (), NULL);
476   atk_object_initialize (accessible, obj);
477
478   return accessible;
479 }
480
481 static void
482 gtk_spinner_accessible_factory_class_init (AtkObjectFactoryClass *klass)
483 {
484   klass->create_accessible = gtk_spinner_accessible_factory_create_accessible;
485   klass->get_accessible_type = gtk_spinner_accessible_factory_get_accessible_type;
486 }
487
488 static void
489 gtk_spinner_accessible_factory_init (AtkObjectFactory *factory)
490 {
491 }
492
493 static AtkObject *
494 gtk_spinner_get_accessible (GtkWidget *widget)
495 {
496   static gboolean first_time = TRUE;
497
498   if (first_time)
499     {
500       AtkObjectFactory *factory;
501       AtkRegistry *registry;
502       GType derived_type;
503       GType derived_atk_type;
504
505       /*
506        * Figure out whether accessibility is enabled by looking at the
507        * type of the accessible object which would be created for
508        * the parent type of GtkSpinner.
509        */
510       derived_type = g_type_parent (GTK_TYPE_SPINNER);
511
512       registry = atk_get_default_registry ();
513       factory = atk_registry_get_factory (registry, derived_type);
514       derived_atk_type = atk_object_factory_get_accessible_type (factory);
515       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
516         atk_registry_set_factory_type (registry,
517                                        GTK_TYPE_SPINNER,
518                                        gtk_spinner_accessible_factory_get_type ());
519       first_time = FALSE;
520     }
521
522   return GTK_WIDGET_CLASS (gtk_spinner_parent_class)->get_accessible (widget);
523 }
524
525 /**
526  * gtk_spinner_new:
527  *
528  * Returns a new spinner widget. Not yet started.
529  *
530  * Return value: a new #GtkSpinner
531  *
532  * Since: 2.20
533  */
534 GtkWidget *
535 gtk_spinner_new (void)
536 {
537   return g_object_new (GTK_TYPE_SPINNER, NULL);
538 }
539
540 /**
541  * gtk_spinner_start:
542  * @spinner: a #GtkSpinner
543  *
544  * Starts the animation of the spinner.
545  *
546  * Since: 2.20
547  */
548 void
549 gtk_spinner_start (GtkSpinner *spinner)
550 {
551   g_return_if_fail (GTK_IS_SPINNER (spinner));
552
553   gtk_spinner_set_active (spinner, TRUE);
554 }
555
556 /**
557  * gtk_spinner_stop:
558  * @spinner: a #GtkSpinner
559  *
560  * Stops the animation of the spinner.
561  *
562  * Since: 2.20
563  */
564 void
565 gtk_spinner_stop (GtkSpinner *spinner)
566 {
567   g_return_if_fail (GTK_IS_SPINNER (spinner));
568
569   gtk_spinner_set_active (spinner, FALSE);
570 }