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 "gtkspinner.h"
43 * @Short_description: Show a spinner animation
45 * @See_also: #GtkCellRendererSpinner, #GtkProgressBar
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.
51 * To start the animation, use gtk_spinner_start(), to stop it
52 * use gtk_spinner_stop().
56 #define SPINNER_SIZE 12
63 struct _GtkSpinnerPrivate
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,
77 static void gtk_spinner_style_set (GtkWidget *widget,
78 GtkStyle *prev_style);
79 static void gtk_spinner_get_property (GObject *object,
83 static void gtk_spinner_set_property (GObject *object,
87 static void gtk_spinner_set_active (GtkSpinner *spinner,
89 static void gtk_spinner_get_preferred_width (GtkWidget *widget,
92 static void gtk_spinner_get_preferred_height (GtkWidget *widget,
96 static AtkObject *gtk_spinner_get_accessible (GtkWidget *widget);
97 static GType gtk_spinner_accessible_get_type (void);
99 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET)
102 gtk_spinner_class_init (GtkSpinnerClass *klass)
104 GObjectClass *gobject_class;
105 GtkWidgetClass *widget_class;
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;
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;
122 /* GtkSpinner:active:
124 * Whether the spinner is active
128 g_object_class_install_property (gobject_class,
130 g_param_spec_boolean ("active",
132 P_("Whether the spinner is active"),
136 * GtkSpinner:num-steps:
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).
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)."),
154 * GtkSpinner:cycle-duration:
156 * The duration in milliseconds for the spinner to complete a full cycle.
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"),
171 gtk_spinner_get_property (GObject *object,
176 GtkSpinnerPrivate *priv;
178 priv = GTK_SPINNER (object)->priv;
183 g_value_set_boolean (value, priv->active);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
191 gtk_spinner_set_property (GObject *object,
199 gtk_spinner_set_active (GTK_SPINNER (object), g_value_get_boolean (value));
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
207 gtk_spinner_init (GtkSpinner *spinner)
209 GtkSpinnerPrivate *priv;
211 priv = G_TYPE_INSTANCE_GET_PRIVATE (spinner,
217 spinner->priv = priv;
219 gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
223 gtk_spinner_get_preferred_width (GtkWidget *widget,
228 *minimum_size = SPINNER_SIZE;
231 *natural_size = SPINNER_SIZE;
235 gtk_spinner_get_preferred_height (GtkWidget *widget,
240 *minimum_size = SPINNER_SIZE;
243 *natural_size = SPINNER_SIZE;
247 gtk_spinner_draw (GtkWidget *widget,
250 GtkStateType state_type;
251 GtkSpinnerPrivate *priv;
253 priv = GTK_SPINNER (widget)->priv;
255 state_type = GTK_STATE_NORMAL;
256 if (!gtk_widget_is_sensitive (widget))
257 state_type = GTK_STATE_INSENSITIVE;
259 gtk_paint_spinner (gtk_widget_get_style (widget),
266 gtk_widget_get_allocated_width (widget),
267 gtk_widget_get_allocated_height (widget));
273 gtk_spinner_timeout (gpointer data)
275 GtkSpinnerPrivate *priv;
277 priv = GTK_SPINNER (data)->priv;
279 if (priv->current + 1 >= priv->num_steps)
284 gtk_widget_queue_draw (GTK_WIDGET (data));
290 gtk_spinner_add_timeout (GtkSpinner *spinner)
292 GtkSpinnerPrivate *priv;
294 priv = spinner->priv;
296 priv->timeout = gdk_threads_add_timeout ((guint) priv->cycle_duration / priv->num_steps, gtk_spinner_timeout, spinner);
300 gtk_spinner_remove_timeout (GtkSpinner *spinner)
302 GtkSpinnerPrivate *priv;
304 priv = spinner->priv;
306 g_source_remove (priv->timeout);
311 gtk_spinner_map (GtkWidget *widget)
313 GtkSpinner *spinner = GTK_SPINNER (widget);
314 GtkSpinnerPrivate *priv = spinner->priv;
316 GTK_WIDGET_CLASS (gtk_spinner_parent_class)->map (widget);
319 gtk_spinner_add_timeout (spinner);
323 gtk_spinner_unmap (GtkWidget *widget)
325 GtkSpinner *spinner = GTK_SPINNER (widget);
326 GtkSpinnerPrivate *priv = spinner->priv;
328 if (priv->timeout != 0)
329 gtk_spinner_remove_timeout (spinner);
331 GTK_WIDGET_CLASS (gtk_spinner_parent_class)->unmap (widget);
335 gtk_spinner_style_set (GtkWidget *widget,
336 GtkStyle *prev_style)
338 GtkSpinnerPrivate *priv;
340 priv = GTK_SPINNER (widget)->priv;
342 gtk_widget_style_get (GTK_WIDGET (widget),
343 "num-steps", &(priv->num_steps),
344 "cycle-duration", &(priv->cycle_duration),
347 if (priv->current > priv->num_steps)
352 gtk_spinner_dispose (GObject *gobject)
354 GtkSpinnerPrivate *priv;
356 priv = GTK_SPINNER (gobject)->priv;
358 if (priv->timeout != 0)
360 gtk_spinner_remove_timeout (GTK_SPINNER (gobject));
363 G_OBJECT_CLASS (gtk_spinner_parent_class)->dispose (gobject);
367 gtk_spinner_set_active (GtkSpinner *spinner, gboolean active)
369 GtkSpinnerPrivate *priv;
371 active = active != FALSE;
373 priv = GTK_SPINNER (spinner)->priv;
375 if (priv->active != active)
377 priv->active = active;
378 g_object_notify (G_OBJECT (spinner), "active");
380 if (active && gtk_widget_get_realized (GTK_WIDGET (spinner)) && priv->timeout == 0)
382 gtk_spinner_add_timeout (spinner);
384 else if (!active && priv->timeout != 0)
386 gtk_spinner_remove_timeout (spinner);
392 gtk_spinner_accessible_factory_get_accessible_type (void)
394 return gtk_spinner_accessible_get_type ();
398 gtk_spinner_accessible_new (GObject *obj)
400 AtkObject *accessible;
402 g_return_val_if_fail (GTK_IS_WIDGET (obj), NULL);
404 accessible = g_object_new (gtk_spinner_accessible_get_type (), NULL);
405 atk_object_initialize (accessible, obj);
411 gtk_spinner_accessible_factory_create_accessible (GObject *obj)
413 return gtk_spinner_accessible_new (obj);
417 gtk_spinner_accessible_factory_class_init (AtkObjectFactoryClass *klass)
419 klass->create_accessible = gtk_spinner_accessible_factory_create_accessible;
420 klass->get_accessible_type = gtk_spinner_accessible_factory_get_accessible_type;
424 gtk_spinner_accessible_factory_get_type (void)
426 static GType type = 0;
430 const GTypeInfo tinfo =
432 sizeof (AtkObjectFactoryClass),
433 NULL, /* base_init */
434 NULL, /* base_finalize */
435 (GClassInitFunc) gtk_spinner_accessible_factory_class_init,
436 NULL, /* class_finalize */
437 NULL, /* class_data */
438 sizeof (AtkObjectFactory),
443 type = g_type_register_static (ATK_TYPE_OBJECT_FACTORY,
444 I_("GtkSpinnerAccessibleFactory"),
450 static AtkObjectClass *a11y_parent_class = NULL;
453 gtk_spinner_accessible_initialize (AtkObject *accessible,
456 atk_object_set_name (accessible, C_("throbbing progress animation widget", "Spinner"));
457 atk_object_set_description (accessible, _("Provides visual indication of progress"));
459 a11y_parent_class->initialize (accessible, widget);
463 gtk_spinner_accessible_class_init (AtkObjectClass *klass)
465 a11y_parent_class = g_type_class_peek_parent (klass);
467 klass->initialize = gtk_spinner_accessible_initialize;
471 gtk_spinner_accessible_image_get_size (AtkImage *image,
475 GtkAllocation allocation;
478 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
481 *width = *height = 0;
485 gtk_widget_get_allocation (widget, &allocation);
486 *width = allocation.width;
487 *height = allocation.height;
492 gtk_spinner_accessible_image_interface_init (AtkImageIface *iface)
494 iface->get_image_size = gtk_spinner_accessible_image_get_size;
498 gtk_spinner_accessible_get_type (void)
500 static GType type = 0;
504 if (G_UNLIKELY (type == 0))
506 const GInterfaceInfo atk_image_info = {
507 (GInterfaceInitFunc) gtk_spinner_accessible_image_interface_init,
508 (GInterfaceFinalizeFunc) NULL,
511 GType parent_atk_type;
512 GTypeInfo tinfo = { 0 };
514 AtkObjectFactory *factory;
516 if ((type = g_type_from_name ("GtkSpinnerAccessible")))
519 factory = atk_registry_get_factory (atk_get_default_registry (),
522 return G_TYPE_INVALID;
524 parent_atk_type = atk_object_factory_get_accessible_type (factory);
525 if (!parent_atk_type)
526 return G_TYPE_INVALID;
529 * Figure out the size of the class and instance
530 * we are deriving from
532 g_type_query (parent_atk_type, &query);
534 tinfo.class_init = (GClassInitFunc) gtk_spinner_accessible_class_init;
535 tinfo.class_size = query.class_size;
536 tinfo.instance_size = query.instance_size;
538 /* Register the type */
539 type = g_type_register_static (parent_atk_type,
540 "GtkSpinnerAccessible",
543 g_type_add_interface_static (type, ATK_TYPE_IMAGE,
551 gtk_spinner_get_accessible (GtkWidget *widget)
553 static gboolean first_time = TRUE;
557 AtkObjectFactory *factory;
558 AtkRegistry *registry;
560 GType derived_atk_type;
563 * Figure out whether accessibility is enabled by looking at the
564 * type of the accessible object which would be created for
565 * the parent type of GtkSpinner.
567 derived_type = g_type_parent (GTK_TYPE_SPINNER);
569 registry = atk_get_default_registry ();
570 factory = atk_registry_get_factory (registry,
572 derived_atk_type = atk_object_factory_get_accessible_type (factory);
573 if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
574 atk_registry_set_factory_type (registry,
576 gtk_spinner_accessible_factory_get_type ());
579 return GTK_WIDGET_CLASS (gtk_spinner_parent_class)->get_accessible (widget);
585 * Returns a new spinner widget. Not yet started.
587 * Return value: a new #GtkSpinner
592 gtk_spinner_new (void)
594 return g_object_new (GTK_TYPE_SPINNER, NULL);
599 * @spinner: a #GtkSpinner
601 * Starts the animation of the spinner.
606 gtk_spinner_start (GtkSpinner *spinner)
608 g_return_if_fail (GTK_IS_SPINNER (spinner));
610 gtk_spinner_set_active (spinner, TRUE);
615 * @spinner: a #GtkSpinner
617 * Stops the animation of the spinner.
622 gtk_spinner_stop (GtkSpinner *spinner)
624 g_return_if_fail (GTK_IS_SPINNER (spinner));
626 gtk_spinner_set_active (spinner, FALSE);