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"
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 GTK_SPINNER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SPINNER, GtkSpinnerPrivate))
59 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_DRAWING_AREA);
66 struct _GtkSpinnerPrivate
74 static void gtk_spinner_class_init (GtkSpinnerClass *klass);
75 static void gtk_spinner_init (GtkSpinner *spinner);
76 static void gtk_spinner_dispose (GObject *gobject);
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,
92 static AtkObject *gtk_spinner_get_accessible (GtkWidget *widget);
93 static GType gtk_spinner_accessible_get_type (void);
96 gtk_spinner_class_init (GtkSpinnerClass *klass)
98 GObjectClass *gobject_class;
99 GtkWidgetClass *widget_class;
101 gtk_spinner_parent_class = g_type_class_peek_parent (klass);
103 gobject_class = G_OBJECT_CLASS(klass);
104 g_type_class_add_private (gobject_class, sizeof (GtkSpinnerPrivate));
105 gobject_class->dispose = gtk_spinner_dispose;
106 gobject_class->get_property = gtk_spinner_get_property;
107 gobject_class->set_property = gtk_spinner_set_property;
109 widget_class = GTK_WIDGET_CLASS(klass);
110 widget_class->expose_event = gtk_spinner_expose;
111 widget_class->screen_changed = gtk_spinner_screen_changed;
112 widget_class->style_set = gtk_spinner_style_set;
113 widget_class->get_accessible = gtk_spinner_get_accessible;
115 /* GtkSpinner:active:
117 * Whether the spinner is active
121 g_object_class_install_property (gobject_class,
123 g_param_spec_boolean ("active",
125 P_("Whether the spinner is active"),
129 * GtkSpinner:num-steps:
131 * The number of steps for the spinner to complete a full loop.
132 * The animation will complete a full cycle in one second by default
133 * (see the #GtkSpinner:cycle-duration style property).
137 gtk_widget_class_install_style_property (widget_class,
138 g_param_spec_uint ("num-steps",
139 P_("Number of steps"),
140 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)."),
147 * GtkSpinner::cycle-duration:
149 * The duration in milliseconds for the spinner to complete a full cycle.
153 gtk_widget_class_install_style_property (widget_class,
154 g_param_spec_uint ("cycle-duration",
155 P_("Animation duration"),
156 P_("The length of time in milliseconds for the spinner to complete a full loop"),
164 gtk_spinner_get_property (GObject *object,
169 GtkSpinnerPrivate *priv;
171 priv = GTK_SPINNER_GET_PRIVATE (object);
176 g_value_set_boolean (value, priv->timeout != 0);
179 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
184 gtk_spinner_set_property (GObject *object,
192 if (g_value_get_boolean (value))
193 gtk_spinner_start (GTK_SPINNER (object));
195 gtk_spinner_stop (GTK_SPINNER (object));
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
203 gtk_spinner_init (GtkSpinner *spinner)
205 GtkSpinnerPrivate *priv;
207 priv = GTK_SPINNER_GET_PRIVATE (spinner);
211 GTK_WIDGET_SET_FLAGS (GTK_WIDGET (spinner), GTK_NO_WINDOW);
215 gtk_spinner_expose (GtkWidget *widget, GdkEventExpose *event)
217 GtkStateType state_type;
218 GtkSpinnerPrivate *priv;
221 priv = GTK_SPINNER_GET_PRIVATE (widget);
223 width = widget->allocation.width;
224 height = widget->allocation.height;
226 if ((width < 12) || (height <12))
227 gtk_widget_set_size_request (widget, 12, 12);
229 state_type = GTK_STATE_NORMAL;
230 if (!GTK_WIDGET_IS_SENSITIVE (widget))
231 state_type = GTK_STATE_INSENSITIVE;
233 gtk_paint_spinner (widget->style,
237 event->area.x, event->area.y,
238 event->area.width, event->area.height);
244 gtk_spinner_screen_changed (GtkWidget* widget, GdkScreen* old_screen)
247 GdkScreen* new_screen;
248 GdkColormap* colormap;
250 spinner = GTK_SPINNER(widget);
252 new_screen = gtk_widget_get_screen (widget);
253 colormap = gdk_screen_get_rgba_colormap (new_screen);
257 colormap = gdk_screen_get_rgb_colormap (new_screen);
260 gtk_widget_set_colormap (widget, colormap);
264 gtk_spinner_style_set (GtkWidget *widget,
265 GtkStyle *prev_style)
267 GtkSpinnerPrivate *priv;
269 priv = GTK_SPINNER_GET_PRIVATE (widget);
271 gtk_widget_style_get (GTK_WIDGET (widget),
272 "num-steps", &(priv->num_steps),
273 "cycle-duration", &(priv->cycle_duration),
276 if (priv->current > priv->num_steps)
281 gtk_spinner_timeout (gpointer data)
283 GtkSpinnerPrivate *priv;
285 priv = GTK_SPINNER_GET_PRIVATE (data);
287 if (priv->current + 1 >= priv->num_steps)
292 gtk_widget_queue_draw (GTK_WIDGET (data));
297 gtk_spinner_dispose (GObject *gobject)
299 GtkSpinnerPrivate *priv;
301 priv = GTK_SPINNER_GET_PRIVATE (gobject);
303 if (priv->timeout != 0)
305 g_source_remove (priv->timeout);
309 G_OBJECT_CLASS (gtk_spinner_parent_class)->dispose (gobject);
313 gtk_spinner_accessible_factory_get_accessible_type (void)
315 return gtk_spinner_accessible_get_type ();
319 gtk_spinner_accessible_new (GObject *obj)
321 AtkObject *accessible;
323 g_return_val_if_fail (GTK_IS_WIDGET (obj), NULL);
325 accessible = g_object_new (gtk_spinner_accessible_get_type (), NULL);
326 atk_object_initialize (accessible, obj);
332 gtk_spinner_accessible_factory_create_accessible (GObject *obj)
334 return gtk_spinner_accessible_new (obj);
338 gtk_spinner_accessible_factory_class_init (AtkObjectFactoryClass *klass)
340 klass->create_accessible = gtk_spinner_accessible_factory_create_accessible;
341 klass->get_accessible_type = gtk_spinner_accessible_factory_get_accessible_type;
345 gtk_spinner_accessible_factory_get_type (void)
347 static GType type = 0;
351 const GTypeInfo tinfo =
353 sizeof (AtkObjectFactoryClass),
354 NULL, /* base_init */
355 NULL, /* base_finalize */
356 (GClassInitFunc) gtk_spinner_accessible_factory_class_init,
357 NULL, /* class_finalize */
358 NULL, /* class_data */
359 sizeof (AtkObjectFactory),
364 type = g_type_register_static (ATK_TYPE_OBJECT_FACTORY,
365 I_("GtkSpinnerAccessibleFactory"),
371 static AtkObjectClass *a11y_parent_class = NULL;
374 gtk_spinner_accessible_initialize (AtkObject *accessible,
377 atk_object_set_name (accessible, _("Spinner"));
378 atk_object_set_description (accessible, _("Provides visual status"));
380 a11y_parent_class->initialize (accessible, widget);
384 gtk_spinner_accessible_class_init (AtkObjectClass *klass)
386 a11y_parent_class = g_type_class_peek_parent (klass);
388 klass->initialize = gtk_spinner_accessible_initialize;
392 gtk_spinner_accessible_image_get_size (AtkImage *image,
398 widget = GTK_ACCESSIBLE (image)->widget;
401 *width = *height = 0;
405 *width = widget->allocation.width;
406 *height = widget->allocation.height;
411 gtk_spinner_accessible_image_interface_init (AtkImageIface *iface)
413 iface->get_image_size = gtk_spinner_accessible_image_get_size;
417 gtk_spinner_accessible_get_type (void)
419 static GType type = 0;
423 if (G_UNLIKELY (type == 0))
425 const GInterfaceInfo atk_image_info = {
426 (GInterfaceInitFunc) gtk_spinner_accessible_image_interface_init,
427 (GInterfaceFinalizeFunc) NULL,
431 GType parent_atk_type;
432 GTypeInfo tinfo = { 0 };
434 AtkObjectFactory *factory;
436 if ((type = g_type_from_name ("GtkSpinnerAccessible")))
439 factory = atk_registry_get_factory (atk_get_default_registry (),
442 return G_TYPE_INVALID;
444 parent_atk_type = atk_object_factory_get_accessible_type (factory);
445 if (!parent_atk_type)
446 return G_TYPE_INVALID;
449 * Figure out the size of the class and instance
450 * we are deriving from
452 g_type_query (parent_atk_type, &query);
454 tinfo.class_init = (GClassInitFunc) gtk_spinner_accessible_class_init;
455 tinfo.class_size = query.class_size;
456 tinfo.instance_size = query.instance_size;
458 /* Register the type */
459 type = g_type_register_static (parent_atk_type,
460 "GtkSpinnerAccessible",
463 g_type_add_interface_static (type, ATK_TYPE_IMAGE,
471 gtk_spinner_get_accessible (GtkWidget *widget)
473 static gboolean first_time = TRUE;
477 AtkObjectFactory *factory;
478 AtkRegistry *registry;
480 GType derived_atk_type;
483 * Figure out whether accessibility is enabled by looking at the
484 * type of the accessible object which would be created for
485 * the parent type of GtkSpinner.
487 derived_type = g_type_parent (GTK_TYPE_SPINNER);
489 registry = atk_get_default_registry ();
490 factory = atk_registry_get_factory (registry,
492 derived_atk_type = atk_object_factory_get_accessible_type (factory);
493 if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
494 atk_registry_set_factory_type (registry,
496 gtk_spinner_accessible_factory_get_type ());
499 return GTK_WIDGET_CLASS (gtk_spinner_parent_class)->get_accessible (widget);
505 * Returns a new spinner widget. Not yet started.
507 * Return value: a new #GtkSpinner
512 gtk_spinner_new (void)
514 return g_object_new (GTK_TYPE_SPINNER, NULL);
519 * @spinner: a #GtkSpinner
521 * Starts the animation of the spinner.
526 gtk_spinner_start (GtkSpinner *spinner)
528 GtkSpinnerPrivate *priv;
530 g_return_if_fail (GTK_IS_SPINNER (spinner));
532 priv = GTK_SPINNER_GET_PRIVATE (spinner);
534 if (priv->timeout != 0)
537 priv->timeout = gdk_threads_add_timeout (priv->cycle_duration / priv->num_steps, gtk_spinner_timeout, spinner);
538 g_object_notify (G_OBJECT (spinner), "active");
543 * @spinner: a #GtkSpinner
545 * Stops the animation of the spinner.
550 gtk_spinner_stop (GtkSpinner *spinner)
552 GtkSpinnerPrivate *priv;
554 g_return_if_fail (GTK_IS_SPINNER (spinner));
556 priv = GTK_SPINNER_GET_PRIVATE (spinner);
557 if (priv->timeout == 0)
560 g_source_remove (priv->timeout);
562 g_object_notify (G_OBJECT (spinner), "active");
565 #define __GTK_SPINNER_C__
566 #include "gtkaliasdef.c"