1 /* GTK - The GIMP Toolkit
3 * Copyright (C) 2007 John Stowers, Neil Jagdish Patel.
4 * Copyright (C) 2009 Bastien Nocera, David Zeuthen
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.
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.
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.
21 * Code adapted from egg-spinner
22 * by Christian Hergert <christian.hergert@gmail.com>
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/.
35 #include "gtkaccessible.h"
37 #include "gtksizerequest.h"
38 #include "gtkspinner.h"
44 * @Short_description: Show a spinner animation
46 * @See_also: #GtkCellRendererSpinner, #GtkProgressBar
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.
52 * To start the animation, use gtk_spinner_start(), to stop it
53 * use gtk_spinner_stop().
57 #define SPINNER_SIZE 12
64 struct _GtkSpinnerPrivate
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,
84 static void gtk_spinner_set_property (GObject *object,
88 static void gtk_spinner_set_active (GtkSpinner *spinner,
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);
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))
99 gtk_spinner_class_init (GtkSpinnerClass *klass)
101 GObjectClass *gobject_class;
102 GtkWidgetClass *widget_class;
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;
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;
117 /* GtkSpinner:active:
119 * Whether the spinner is active
123 g_object_class_install_property (gobject_class,
125 g_param_spec_boolean ("active",
127 P_("Whether the spinner is active"),
131 * GtkSpinner:num-steps:
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).
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)."),
149 * GtkSpinner:cycle-duration:
151 * The duration in milliseconds for the spinner to complete a full cycle.
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"),
166 gtk_spinner_get_property (GObject *object,
171 GtkSpinnerPrivate *priv;
173 priv = GTK_SPINNER (object)->priv;
178 g_value_set_boolean (value, priv->active);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
186 gtk_spinner_set_property (GObject *object,
194 gtk_spinner_set_active (GTK_SPINNER (object), g_value_get_boolean (value));
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
202 gtk_spinner_init (GtkSpinner *spinner)
204 GtkSpinnerPrivate *priv;
206 priv = G_TYPE_INSTANCE_GET_PRIVATE (spinner,
212 spinner->priv = priv;
214 gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
218 gtk_spinner_get_width (GtkSizeRequest *widget,
223 *minimum_size = SPINNER_SIZE;
226 *natural_size = SPINNER_SIZE;
230 gtk_spinner_get_height (GtkSizeRequest *widget,
235 *minimum_size = SPINNER_SIZE;
238 *natural_size = SPINNER_SIZE;
242 gtk_spinner_size_request_init (GtkSizeRequestIface *iface)
244 iface->get_width = gtk_spinner_get_width;
245 iface->get_height = gtk_spinner_get_height;
250 gtk_spinner_expose (GtkWidget *widget,
251 GdkEventExpose *event)
253 GtkAllocation allocation;
254 GtkStateType state_type;
255 GtkSpinnerPrivate *priv;
258 priv = GTK_SPINNER (widget)->priv;
260 gtk_widget_get_allocation (widget, &allocation);
261 width = allocation.width;
262 height = allocation.height;
264 state_type = GTK_STATE_NORMAL;
265 if (!gtk_widget_is_sensitive (widget))
266 state_type = GTK_STATE_INSENSITIVE;
268 gtk_paint_spinner (gtk_widget_get_style (widget),
269 gtk_widget_get_window (widget),
275 event->area.x, event->area.y,
276 event->area.width, event->area.height);
282 gtk_spinner_timeout (gpointer data)
284 GtkSpinnerPrivate *priv;
286 priv = GTK_SPINNER (data)->priv;
288 if (priv->current + 1 >= priv->num_steps)
293 gtk_widget_queue_draw (GTK_WIDGET (data));
299 gtk_spinner_add_timeout (GtkSpinner *spinner)
301 GtkSpinnerPrivate *priv;
303 priv = spinner->priv;
305 priv->timeout = gdk_threads_add_timeout ((guint) priv->cycle_duration / priv->num_steps, gtk_spinner_timeout, spinner);
309 gtk_spinner_remove_timeout (GtkSpinner *spinner)
311 GtkSpinnerPrivate *priv;
313 priv = spinner->priv;
315 g_source_remove (priv->timeout);
320 gtk_spinner_map (GtkWidget *widget)
322 GtkSpinner *spinner = GTK_SPINNER (widget);
323 GtkSpinnerPrivate *priv = spinner->priv;
325 GTK_WIDGET_CLASS (gtk_spinner_parent_class)->map (widget);
328 gtk_spinner_add_timeout (spinner);
332 gtk_spinner_unmap (GtkWidget *widget)
334 GtkSpinner *spinner = GTK_SPINNER (widget);
335 GtkSpinnerPrivate *priv = spinner->priv;
337 if (priv->timeout != 0)
338 gtk_spinner_remove_timeout (spinner);
340 GTK_WIDGET_CLASS (gtk_spinner_parent_class)->unmap (widget);
344 gtk_spinner_style_set (GtkWidget *widget,
345 GtkStyle *prev_style)
347 GtkSpinnerPrivate *priv;
349 priv = GTK_SPINNER (widget)->priv;
351 gtk_widget_style_get (GTK_WIDGET (widget),
352 "num-steps", &(priv->num_steps),
353 "cycle-duration", &(priv->cycle_duration),
356 if (priv->current > priv->num_steps)
361 gtk_spinner_dispose (GObject *gobject)
363 GtkSpinnerPrivate *priv;
365 priv = GTK_SPINNER (gobject)->priv;
367 if (priv->timeout != 0)
369 gtk_spinner_remove_timeout (GTK_SPINNER (gobject));
372 G_OBJECT_CLASS (gtk_spinner_parent_class)->dispose (gobject);
376 gtk_spinner_set_active (GtkSpinner *spinner, gboolean active)
378 GtkSpinnerPrivate *priv;
380 active = active != FALSE;
382 priv = GTK_SPINNER (spinner)->priv;
384 if (priv->active != active)
386 priv->active = active;
387 g_object_notify (G_OBJECT (spinner), "active");
389 if (active && gtk_widget_get_realized (GTK_WIDGET (spinner)) && priv->timeout == 0)
391 gtk_spinner_add_timeout (spinner);
393 else if (!active && priv->timeout != 0)
395 gtk_spinner_remove_timeout (spinner);
401 gtk_spinner_accessible_factory_get_accessible_type (void)
403 return gtk_spinner_accessible_get_type ();
407 gtk_spinner_accessible_new (GObject *obj)
409 AtkObject *accessible;
411 g_return_val_if_fail (GTK_IS_WIDGET (obj), NULL);
413 accessible = g_object_new (gtk_spinner_accessible_get_type (), NULL);
414 atk_object_initialize (accessible, obj);
420 gtk_spinner_accessible_factory_create_accessible (GObject *obj)
422 return gtk_spinner_accessible_new (obj);
426 gtk_spinner_accessible_factory_class_init (AtkObjectFactoryClass *klass)
428 klass->create_accessible = gtk_spinner_accessible_factory_create_accessible;
429 klass->get_accessible_type = gtk_spinner_accessible_factory_get_accessible_type;
433 gtk_spinner_accessible_factory_get_type (void)
435 static GType type = 0;
439 const GTypeInfo tinfo =
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),
452 type = g_type_register_static (ATK_TYPE_OBJECT_FACTORY,
453 I_("GtkSpinnerAccessibleFactory"),
459 static AtkObjectClass *a11y_parent_class = NULL;
462 gtk_spinner_accessible_initialize (AtkObject *accessible,
465 atk_object_set_name (accessible, C_("throbbing progress animation widget", "Spinner"));
466 atk_object_set_description (accessible, _("Provides visual indication of progress"));
468 a11y_parent_class->initialize (accessible, widget);
472 gtk_spinner_accessible_class_init (AtkObjectClass *klass)
474 a11y_parent_class = g_type_class_peek_parent (klass);
476 klass->initialize = gtk_spinner_accessible_initialize;
480 gtk_spinner_accessible_image_get_size (AtkImage *image,
484 GtkAllocation allocation;
487 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
490 *width = *height = 0;
494 gtk_widget_get_allocation (widget, &allocation);
495 *width = allocation.width;
496 *height = allocation.height;
501 gtk_spinner_accessible_image_interface_init (AtkImageIface *iface)
503 iface->get_image_size = gtk_spinner_accessible_image_get_size;
507 gtk_spinner_accessible_get_type (void)
509 static GType type = 0;
513 if (G_UNLIKELY (type == 0))
515 const GInterfaceInfo atk_image_info = {
516 (GInterfaceInitFunc) gtk_spinner_accessible_image_interface_init,
517 (GInterfaceFinalizeFunc) NULL,
520 GType parent_atk_type;
521 GTypeInfo tinfo = { 0 };
523 AtkObjectFactory *factory;
525 if ((type = g_type_from_name ("GtkSpinnerAccessible")))
528 factory = atk_registry_get_factory (atk_get_default_registry (),
531 return G_TYPE_INVALID;
533 parent_atk_type = atk_object_factory_get_accessible_type (factory);
534 if (!parent_atk_type)
535 return G_TYPE_INVALID;
538 * Figure out the size of the class and instance
539 * we are deriving from
541 g_type_query (parent_atk_type, &query);
543 tinfo.class_init = (GClassInitFunc) gtk_spinner_accessible_class_init;
544 tinfo.class_size = query.class_size;
545 tinfo.instance_size = query.instance_size;
547 /* Register the type */
548 type = g_type_register_static (parent_atk_type,
549 "GtkSpinnerAccessible",
552 g_type_add_interface_static (type, ATK_TYPE_IMAGE,
560 gtk_spinner_get_accessible (GtkWidget *widget)
562 static gboolean first_time = TRUE;
566 AtkObjectFactory *factory;
567 AtkRegistry *registry;
569 GType derived_atk_type;
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.
576 derived_type = g_type_parent (GTK_TYPE_SPINNER);
578 registry = atk_get_default_registry ();
579 factory = atk_registry_get_factory (registry,
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,
585 gtk_spinner_accessible_factory_get_type ());
588 return GTK_WIDGET_CLASS (gtk_spinner_parent_class)->get_accessible (widget);
594 * Returns a new spinner widget. Not yet started.
596 * Return value: a new #GtkSpinner
601 gtk_spinner_new (void)
603 return g_object_new (GTK_TYPE_SPINNER, NULL);
608 * @spinner: a #GtkSpinner
610 * Starts the animation of the spinner.
615 gtk_spinner_start (GtkSpinner *spinner)
617 g_return_if_fail (GTK_IS_SPINNER (spinner));
619 gtk_spinner_set_active (spinner, TRUE);
624 * @spinner: a #GtkSpinner
626 * Stops the animation of the spinner.
631 gtk_spinner_stop (GtkSpinner *spinner)
633 g_return_if_fail (GTK_IS_SPINNER (spinner));
635 gtk_spinner_set_active (spinner, FALSE);