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 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_DRAWING_AREA);
63 struct _GtkSpinnerPrivate
72 static void gtk_spinner_class_init (GtkSpinnerClass *klass);
73 static void gtk_spinner_init (GtkSpinner *spinner);
74 static void gtk_spinner_dispose (GObject *gobject);
75 static void gtk_spinner_realize (GtkWidget *widget);
76 static void gtk_spinner_unrealize (GtkWidget *widget);
77 static gboolean gtk_spinner_expose (GtkWidget *widget,
78 GdkEventExpose *event);
79 static void gtk_spinner_screen_changed (GtkWidget *widget,
80 GdkScreen *old_screen);
81 static void gtk_spinner_style_set (GtkWidget *widget,
82 GtkStyle *prev_style);
83 static void gtk_spinner_get_property (GObject *object,
87 static void gtk_spinner_set_property (GObject *object,
91 static void gtk_spinner_set_active (GtkSpinner *spinner,
93 static AtkObject *gtk_spinner_get_accessible (GtkWidget *widget);
94 static GType gtk_spinner_accessible_get_type (void);
97 gtk_spinner_class_init (GtkSpinnerClass *klass)
99 GObjectClass *gobject_class;
100 GtkWidgetClass *widget_class;
102 gobject_class = G_OBJECT_CLASS(klass);
103 g_type_class_add_private (gobject_class, sizeof (GtkSpinnerPrivate));
104 gobject_class->dispose = gtk_spinner_dispose;
105 gobject_class->get_property = gtk_spinner_get_property;
106 gobject_class->set_property = gtk_spinner_set_property;
108 widget_class = GTK_WIDGET_CLASS(klass);
109 widget_class->expose_event = gtk_spinner_expose;
110 widget_class->realize = gtk_spinner_realize;
111 widget_class->unrealize = gtk_spinner_unrealize;
112 widget_class->screen_changed = gtk_spinner_screen_changed;
113 widget_class->style_set = gtk_spinner_style_set;
114 widget_class->get_accessible = gtk_spinner_get_accessible;
116 /* GtkSpinner:active:
118 * Whether the spinner is active
122 g_object_class_install_property (gobject_class,
124 g_param_spec_boolean ("active",
126 P_("Whether the spinner is active"),
130 * GtkSpinner:num-steps:
132 * The number of steps for the spinner to complete a full loop.
133 * The animation will complete a full cycle in one second by default
134 * (see the #GtkSpinner:cycle-duration style property).
138 gtk_widget_class_install_style_property (widget_class,
139 g_param_spec_uint ("num-steps",
140 P_("Number of steps"),
141 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 * GtkSpinner:cycle-duration:
150 * The duration in milliseconds for the spinner to complete a full cycle.
154 gtk_widget_class_install_style_property (widget_class,
155 g_param_spec_uint ("cycle-duration",
156 P_("Animation duration"),
157 P_("The length of time in milliseconds for the spinner to complete a full loop"),
165 gtk_spinner_get_property (GObject *object,
170 GtkSpinnerPrivate *priv;
172 priv = GTK_SPINNER (object)->priv;
177 g_value_set_boolean (value, priv->active);
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
185 gtk_spinner_set_property (GObject *object,
193 gtk_spinner_set_active (GTK_SPINNER (object), g_value_get_boolean (value));
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
201 gtk_spinner_init (GtkSpinner *spinner)
203 GtkSpinnerPrivate *priv;
205 priv = G_TYPE_INSTANCE_GET_PRIVATE (spinner,
211 spinner->priv = priv;
213 gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
217 gtk_spinner_expose (GtkWidget *widget,
218 GdkEventExpose *event)
220 GtkStateType state_type;
221 GtkSpinnerPrivate *priv;
224 priv = GTK_SPINNER (widget)->priv;
226 width = widget->allocation.width;
227 height = widget->allocation.height;
229 if ((width < 12) || (height <12))
230 gtk_widget_set_size_request (widget, 12, 12);
232 state_type = GTK_STATE_NORMAL;
233 if (!gtk_widget_is_sensitive (widget))
234 state_type = GTK_STATE_INSENSITIVE;
236 gtk_paint_spinner (widget->style,
243 event->area.x, event->area.y,
244 event->area.width, event->area.height);
250 gtk_spinner_timeout (gpointer data)
252 GtkSpinnerPrivate *priv;
254 priv = GTK_SPINNER (data)->priv;
256 if (priv->current + 1 >= priv->num_steps)
261 gtk_widget_queue_draw (GTK_WIDGET (data));
267 gtk_spinner_add_timeout (GtkSpinner *spinner)
269 GtkSpinnerPrivate *priv;
271 priv = spinner->priv;
273 priv->timeout = gdk_threads_add_timeout ((guint) priv->cycle_duration / priv->num_steps, gtk_spinner_timeout, spinner);
277 gtk_spinner_remove_timeout (GtkSpinner *spinner)
279 GtkSpinnerPrivate *priv;
281 priv = spinner->priv;
283 g_source_remove (priv->timeout);
288 gtk_spinner_realize (GtkWidget *widget)
290 GtkSpinnerPrivate *priv;
292 priv = GTK_SPINNER (widget)->priv;
294 GTK_WIDGET_CLASS (gtk_spinner_parent_class)->realize (widget);
297 gtk_spinner_add_timeout (GTK_SPINNER (widget));
301 gtk_spinner_unrealize (GtkWidget *widget)
303 GtkSpinnerPrivate *priv;
305 priv = GTK_SPINNER (widget)->priv;
307 if (priv->timeout != 0)
309 gtk_spinner_remove_timeout (GTK_SPINNER (widget));
312 GTK_WIDGET_CLASS (gtk_spinner_parent_class)->unrealize (widget);
316 gtk_spinner_screen_changed (GtkWidget* widget, GdkScreen* old_screen)
319 GdkScreen* new_screen;
320 GdkColormap* colormap;
322 spinner = GTK_SPINNER (widget);
324 new_screen = gtk_widget_get_screen (widget);
325 colormap = gdk_screen_get_rgba_colormap (new_screen);
329 colormap = gdk_screen_get_rgb_colormap (new_screen);
332 gtk_widget_set_colormap (widget, colormap);
336 gtk_spinner_style_set (GtkWidget *widget,
337 GtkStyle *prev_style)
339 GtkSpinnerPrivate *priv;
341 priv = GTK_SPINNER (widget)->priv;
343 gtk_widget_style_get (GTK_WIDGET (widget),
344 "num-steps", &(priv->num_steps),
345 "cycle-duration", &(priv->cycle_duration),
348 if (priv->current > priv->num_steps)
353 gtk_spinner_dispose (GObject *gobject)
355 GtkSpinnerPrivate *priv;
357 priv = GTK_SPINNER (gobject)->priv;
359 if (priv->timeout != 0)
361 gtk_spinner_remove_timeout (GTK_SPINNER (gobject));
364 G_OBJECT_CLASS (gtk_spinner_parent_class)->dispose (gobject);
368 gtk_spinner_set_active (GtkSpinner *spinner, gboolean active)
370 GtkSpinnerPrivate *priv;
372 active = active != FALSE;
374 priv = GTK_SPINNER (spinner)->priv;
376 if (priv->active != active)
378 priv->active = active;
379 g_object_notify (G_OBJECT (spinner), "active");
381 if (active && gtk_widget_get_realized (GTK_WIDGET (spinner)) && priv->timeout == 0)
383 gtk_spinner_add_timeout (spinner);
385 else if (!active && priv->timeout != 0)
387 gtk_spinner_remove_timeout (spinner);
393 gtk_spinner_accessible_factory_get_accessible_type (void)
395 return gtk_spinner_accessible_get_type ();
399 gtk_spinner_accessible_new (GObject *obj)
401 AtkObject *accessible;
403 g_return_val_if_fail (GTK_IS_WIDGET (obj), NULL);
405 accessible = g_object_new (gtk_spinner_accessible_get_type (), NULL);
406 atk_object_initialize (accessible, obj);
412 gtk_spinner_accessible_factory_create_accessible (GObject *obj)
414 return gtk_spinner_accessible_new (obj);
418 gtk_spinner_accessible_factory_class_init (AtkObjectFactoryClass *klass)
420 klass->create_accessible = gtk_spinner_accessible_factory_create_accessible;
421 klass->get_accessible_type = gtk_spinner_accessible_factory_get_accessible_type;
425 gtk_spinner_accessible_factory_get_type (void)
427 static GType type = 0;
431 const GTypeInfo tinfo =
433 sizeof (AtkObjectFactoryClass),
434 NULL, /* base_init */
435 NULL, /* base_finalize */
436 (GClassInitFunc) gtk_spinner_accessible_factory_class_init,
437 NULL, /* class_finalize */
438 NULL, /* class_data */
439 sizeof (AtkObjectFactory),
444 type = g_type_register_static (ATK_TYPE_OBJECT_FACTORY,
445 I_("GtkSpinnerAccessibleFactory"),
451 static AtkObjectClass *a11y_parent_class = NULL;
454 gtk_spinner_accessible_initialize (AtkObject *accessible,
457 atk_object_set_name (accessible, C_("throbbing progress animation widget", "Spinner"));
458 atk_object_set_description (accessible, _("Provides visual indication of progress"));
460 a11y_parent_class->initialize (accessible, widget);
464 gtk_spinner_accessible_class_init (AtkObjectClass *klass)
466 a11y_parent_class = g_type_class_peek_parent (klass);
468 klass->initialize = gtk_spinner_accessible_initialize;
472 gtk_spinner_accessible_image_get_size (AtkImage *image,
478 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
481 *width = *height = 0;
485 *width = widget->allocation.width;
486 *height = widget->allocation.height;
491 gtk_spinner_accessible_image_interface_init (AtkImageIface *iface)
493 iface->get_image_size = gtk_spinner_accessible_image_get_size;
497 gtk_spinner_accessible_get_type (void)
499 static GType type = 0;
503 if (G_UNLIKELY (type == 0))
505 const GInterfaceInfo atk_image_info = {
506 (GInterfaceInitFunc) gtk_spinner_accessible_image_interface_init,
507 (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);