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"
38 #include "a11y/gtkwidgetaccessible.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 GType _gtk_spinner_accessible_get_type (void);
89 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET)
92 gtk_spinner_class_init (GtkSpinnerClass *klass)
94 GObjectClass *gobject_class;
95 GtkWidgetClass *widget_class;
97 gobject_class = G_OBJECT_CLASS(klass);
98 g_type_class_add_private (gobject_class, sizeof (GtkSpinnerPrivate));
99 gobject_class->get_property = gtk_spinner_get_property;
100 gobject_class->set_property = gtk_spinner_set_property;
102 widget_class = GTK_WIDGET_CLASS(klass);
103 widget_class->draw = gtk_spinner_draw;
104 widget_class->get_preferred_width = gtk_spinner_get_preferred_width;
105 widget_class->get_preferred_height = gtk_spinner_get_preferred_height;
107 /* GtkSpinner:active:
109 * Whether the spinner is active
113 g_object_class_install_property (gobject_class,
115 g_param_spec_boolean ("active",
117 P_("Whether the spinner is active"),
121 gtk_widget_class_set_accessible_type (widget_class, _gtk_spinner_accessible_get_type ());
125 gtk_spinner_get_property (GObject *object,
130 GtkSpinnerPrivate *priv;
132 priv = GTK_SPINNER (object)->priv;
137 g_value_set_boolean (value, priv->active);
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
145 gtk_spinner_set_property (GObject *object,
153 gtk_spinner_set_active (GTK_SPINNER (object), g_value_get_boolean (value));
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
161 gtk_spinner_init (GtkSpinner *spinner)
163 GtkSpinnerPrivate *priv;
164 GtkStyleContext *context;
166 priv = G_TYPE_INSTANCE_GET_PRIVATE (spinner,
169 spinner->priv = priv;
171 gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
173 context = gtk_widget_get_style_context (GTK_WIDGET (spinner));
174 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINNER);
178 gtk_spinner_get_preferred_width (GtkWidget *widget,
183 *minimum_size = SPINNER_SIZE;
186 *natural_size = SPINNER_SIZE;
190 gtk_spinner_get_preferred_height (GtkWidget *widget,
195 *minimum_size = SPINNER_SIZE;
198 *natural_size = SPINNER_SIZE;
202 gtk_spinner_draw (GtkWidget *widget,
205 GtkStyleContext *context;
208 context = gtk_widget_get_style_context (widget);
209 state = gtk_widget_get_state_flags (widget);
211 gtk_style_context_set_state (context, state);
212 gtk_render_activity (context, cr, 0, 0,
213 gtk_widget_get_allocated_width (widget),
214 gtk_widget_get_allocated_height (widget));
220 gtk_spinner_set_active (GtkSpinner *spinner,
223 GtkSpinnerPrivate *priv = spinner->priv;
227 if (priv->active != active)
229 priv->active = active;
231 g_object_notify (G_OBJECT (spinner), "active");
234 gtk_widget_set_state_flags (GTK_WIDGET (spinner),
235 GTK_STATE_FLAG_ACTIVE, FALSE);
237 gtk_widget_unset_state_flags (GTK_WIDGET (spinner),
238 GTK_STATE_FLAG_ACTIVE);
242 /* accessible implementation */
245 gtk_spinner_accessible_image_get_size (AtkImage *image,
249 GtkAllocation allocation;
252 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
255 *width = *height = 0;
259 gtk_widget_get_allocation (widget, &allocation);
260 *width = allocation.width;
261 *height = allocation.height;
266 gtk_spinner_accessible_image_iface_init (AtkImageIface *iface)
268 iface->get_image_size = gtk_spinner_accessible_image_get_size;
272 typedef GtkWidgetAccessible GtkSpinnerAccessible;
273 typedef GtkWidgetAccessibleClass GtkSpinnerAccessibleClass;
275 G_DEFINE_TYPE_WITH_CODE (GtkSpinnerAccessible, _gtk_spinner_accessible, GTK_TYPE_WIDGET_ACCESSIBLE,
276 G_IMPLEMENT_INTERFACE (ATK_TYPE_IMAGE,
277 gtk_spinner_accessible_image_iface_init));
280 gtk_spinner_accessible_initialize (AtkObject *accessible,
283 ATK_OBJECT_CLASS (_gtk_spinner_accessible_parent_class)->initialize (accessible, widget);
285 atk_object_set_name (accessible, C_("throbbing progress animation widget", "Spinner"));
286 atk_object_set_description (accessible, _("Provides visual indication of progress"));
287 atk_object_set_role (accessible, ATK_ROLE_ANIMATION);
291 _gtk_spinner_accessible_class_init (GtkSpinnerAccessibleClass *klass)
293 AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
295 atk_class->initialize = gtk_spinner_accessible_initialize;
299 _gtk_spinner_accessible_init (GtkSpinnerAccessible *self)
306 * Returns a new spinner widget. Not yet started.
308 * Return value: a new #GtkSpinner
313 gtk_spinner_new (void)
315 return g_object_new (GTK_TYPE_SPINNER, NULL);
320 * @spinner: a #GtkSpinner
322 * Starts the animation of the spinner.
327 gtk_spinner_start (GtkSpinner *spinner)
329 g_return_if_fail (GTK_IS_SPINNER (spinner));
331 gtk_spinner_set_active (spinner, TRUE);
336 * @spinner: a #GtkSpinner
338 * Stops the animation of the spinner.
343 gtk_spinner_stop (GtkSpinner *spinner)
345 g_return_if_fail (GTK_IS_SPINNER (spinner));
347 gtk_spinner_set_active (spinner, FALSE);