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
68 static gboolean gtk_spinner_draw (GtkWidget *widget,
70 static void gtk_spinner_get_property (GObject *object,
74 static void gtk_spinner_set_property (GObject *object,
78 static void gtk_spinner_set_active (GtkSpinner *spinner,
80 static void gtk_spinner_get_preferred_width (GtkWidget *widget,
83 static void gtk_spinner_get_preferred_height (GtkWidget *widget,
87 static AtkObject *gtk_spinner_get_accessible (GtkWidget *widget);
88 static GType gtk_spinner_accessible_get_type (void);
90 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET)
93 gtk_spinner_class_init (GtkSpinnerClass *klass)
95 GObjectClass *gobject_class;
96 GtkWidgetClass *widget_class;
98 gobject_class = G_OBJECT_CLASS(klass);
99 g_type_class_add_private (gobject_class, sizeof (GtkSpinnerPrivate));
100 gobject_class->get_property = gtk_spinner_get_property;
101 gobject_class->set_property = gtk_spinner_set_property;
103 widget_class = GTK_WIDGET_CLASS(klass);
104 widget_class->draw = gtk_spinner_draw;
105 widget_class->get_accessible = gtk_spinner_get_accessible;
106 widget_class->get_preferred_width = gtk_spinner_get_preferred_width;
107 widget_class->get_preferred_height = gtk_spinner_get_preferred_height;
109 /* GtkSpinner:active:
111 * Whether the spinner is active
115 g_object_class_install_property (gobject_class,
117 g_param_spec_boolean ("active",
119 P_("Whether the spinner is active"),
123 * GtkSpinner:num-steps:
125 * The number of steps for the spinner to complete a full loop.
126 * The animation will complete a full cycle in one second by default
127 * (see the #GtkSpinner:cycle-duration style property).
133 gtk_widget_class_install_style_property (widget_class,
134 g_param_spec_uint ("num-steps",
135 P_("Number of steps"),
136 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)."),
143 * GtkSpinner:cycle-duration:
145 * The duration in milliseconds for the spinner to complete a full cycle.
151 gtk_widget_class_install_style_property (widget_class,
152 g_param_spec_uint ("cycle-duration",
153 P_("Animation duration"),
154 P_("The length of time in milliseconds for the spinner to complete a full loop"),
162 gtk_spinner_get_property (GObject *object,
167 GtkSpinnerPrivate *priv;
169 priv = GTK_SPINNER (object)->priv;
174 g_value_set_boolean (value, priv->active);
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
182 gtk_spinner_set_property (GObject *object,
190 gtk_spinner_set_active (GTK_SPINNER (object), g_value_get_boolean (value));
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
198 gtk_spinner_init (GtkSpinner *spinner)
200 GtkSpinnerPrivate *priv;
201 GtkStyleContext *context;
203 priv = G_TYPE_INSTANCE_GET_PRIVATE (spinner,
206 spinner->priv = priv;
208 gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
210 context = gtk_widget_get_style_context (GTK_WIDGET (spinner));
211 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINNER);
215 gtk_spinner_get_preferred_width (GtkWidget *widget,
220 *minimum_size = SPINNER_SIZE;
223 *natural_size = SPINNER_SIZE;
227 gtk_spinner_get_preferred_height (GtkWidget *widget,
232 *minimum_size = SPINNER_SIZE;
235 *natural_size = SPINNER_SIZE;
239 gtk_spinner_draw (GtkWidget *widget,
242 GtkSpinnerPrivate *priv;
243 GtkStyleContext *context;
246 priv = GTK_SPINNER (widget)->priv;
247 context = gtk_widget_get_style_context (widget);
248 state = gtk_widget_get_state_flags (widget);
250 gtk_style_context_set_state (context, state);
251 gtk_render_activity (context, cr, 0, 0,
252 gtk_widget_get_allocated_width (widget),
253 gtk_widget_get_allocated_height (widget));
259 gtk_spinner_set_active (GtkSpinner *spinner,
262 GtkSpinnerPrivate *priv = spinner->priv;
266 if (priv->active != active)
268 priv->active = active;
270 g_object_notify (G_OBJECT (spinner), "active");
273 gtk_widget_set_state_flags (GTK_WIDGET (spinner),
274 GTK_STATE_FLAG_ACTIVE, FALSE);
276 gtk_widget_unset_state_flags (GTK_WIDGET (spinner),
277 GTK_STATE_FLAG_ACTIVE);
281 /* accessible implementation */
284 gtk_spinner_accessible_image_get_size (AtkImage *image,
288 GtkAllocation allocation;
291 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
294 *width = *height = 0;
298 gtk_widget_get_allocation (widget, &allocation);
299 *width = allocation.width;
300 *height = allocation.height;
305 gtk_spinner_accessible_image_iface_init (AtkImageIface *iface)
307 iface->get_image_size = gtk_spinner_accessible_image_get_size;
311 typedef struct _GtkSpinnerAccessible GtkSpinnerAccessible;
312 typedef struct _GtkSpinnerAccessibleClass GtkSpinnerAccessibleClass;
314 ATK_DEFINE_TYPE_WITH_CODE (GtkSpinnerAccessible,
315 gtk_spinner_accessible,
317 G_IMPLEMENT_INTERFACE (ATK_TYPE_IMAGE,
318 gtk_spinner_accessible_image_iface_init));
321 gtk_spinner_accessible_initialize (AtkObject *accessible,
324 ATK_OBJECT_CLASS (gtk_spinner_accessible_parent_class)->initialize (accessible, widget);
326 atk_object_set_name (accessible, C_("throbbing progress animation widget", "Spinner"));
327 atk_object_set_description (accessible, _("Provides visual indication of progress"));
331 gtk_spinner_accessible_class_init (GtkSpinnerAccessibleClass *klass)
333 AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
335 atk_class->initialize = gtk_spinner_accessible_initialize;
339 gtk_spinner_accessible_init (GtkSpinnerAccessible *self)
344 typedef AtkObjectFactory GtkSpinnerAccessibleFactory;
345 typedef AtkObjectFactoryClass GtkSpinnerAccessibleFactoryClass;
347 G_DEFINE_TYPE (GtkSpinnerAccessibleFactory,
348 gtk_spinner_accessible_factory,
349 ATK_TYPE_OBJECT_FACTORY);
352 gtk_spinner_accessible_factory_get_accessible_type (void)
354 return gtk_spinner_accessible_get_type ();
358 gtk_spinner_accessible_factory_create_accessible (GObject *obj)
360 AtkObject *accessible;
362 accessible = g_object_new (gtk_spinner_accessible_get_type (), NULL);
363 atk_object_initialize (accessible, obj);
369 gtk_spinner_accessible_factory_class_init (AtkObjectFactoryClass *klass)
371 klass->create_accessible = gtk_spinner_accessible_factory_create_accessible;
372 klass->get_accessible_type = gtk_spinner_accessible_factory_get_accessible_type;
376 gtk_spinner_accessible_factory_init (AtkObjectFactory *factory)
381 gtk_spinner_get_accessible (GtkWidget *widget)
383 static gboolean first_time = TRUE;
387 AtkObjectFactory *factory;
388 AtkRegistry *registry;
390 GType derived_atk_type;
393 * Figure out whether accessibility is enabled by looking at the
394 * type of the accessible object which would be created for
395 * the parent type of GtkSpinner.
397 derived_type = g_type_parent (GTK_TYPE_SPINNER);
399 registry = atk_get_default_registry ();
400 factory = atk_registry_get_factory (registry, derived_type);
401 derived_atk_type = atk_object_factory_get_accessible_type (factory);
402 if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
403 atk_registry_set_factory_type (registry,
405 gtk_spinner_accessible_factory_get_type ());
409 return GTK_WIDGET_CLASS (gtk_spinner_parent_class)->get_accessible (widget);
415 * Returns a new spinner widget. Not yet started.
417 * Return value: a new #GtkSpinner
422 gtk_spinner_new (void)
424 return g_object_new (GTK_TYPE_SPINNER, NULL);
429 * @spinner: a #GtkSpinner
431 * Starts the animation of the spinner.
436 gtk_spinner_start (GtkSpinner *spinner)
438 g_return_if_fail (GTK_IS_SPINNER (spinner));
440 gtk_spinner_set_active (spinner, TRUE);
445 * @spinner: a #GtkSpinner
447 * Stops the animation of the spinner.
452 gtk_spinner_stop (GtkSpinner *spinner)
454 g_return_if_fail (GTK_IS_SPINNER (spinner));
456 gtk_spinner_set_active (spinner, FALSE);