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/gtkspinneraccessible.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,
88 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET)
91 gtk_spinner_class_init (GtkSpinnerClass *klass)
93 GObjectClass *gobject_class;
94 GtkWidgetClass *widget_class;
96 gobject_class = G_OBJECT_CLASS(klass);
97 g_type_class_add_private (gobject_class, sizeof (GtkSpinnerPrivate));
98 gobject_class->get_property = gtk_spinner_get_property;
99 gobject_class->set_property = gtk_spinner_set_property;
101 widget_class = GTK_WIDGET_CLASS(klass);
102 widget_class->draw = gtk_spinner_draw;
103 widget_class->get_preferred_width = gtk_spinner_get_preferred_width;
104 widget_class->get_preferred_height = gtk_spinner_get_preferred_height;
106 /* GtkSpinner:active:
108 * Whether the spinner is active
112 g_object_class_install_property (gobject_class,
114 g_param_spec_boolean ("active",
116 P_("Whether the spinner is active"),
120 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SPINNER_ACCESSIBLE);
124 gtk_spinner_get_property (GObject *object,
129 GtkSpinnerPrivate *priv;
131 priv = GTK_SPINNER (object)->priv;
136 g_value_set_boolean (value, priv->active);
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
144 gtk_spinner_set_property (GObject *object,
152 gtk_spinner_set_active (GTK_SPINNER (object), g_value_get_boolean (value));
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
160 gtk_spinner_init (GtkSpinner *spinner)
162 GtkSpinnerPrivate *priv;
163 GtkStyleContext *context;
165 priv = G_TYPE_INSTANCE_GET_PRIVATE (spinner,
168 spinner->priv = priv;
170 gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
172 context = gtk_widget_get_style_context (GTK_WIDGET (spinner));
173 gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINNER);
177 gtk_spinner_get_preferred_width (GtkWidget *widget,
182 *minimum_size = SPINNER_SIZE;
185 *natural_size = SPINNER_SIZE;
189 gtk_spinner_get_preferred_height (GtkWidget *widget,
194 *minimum_size = SPINNER_SIZE;
197 *natural_size = SPINNER_SIZE;
201 gtk_spinner_draw (GtkWidget *widget,
204 GtkStyleContext *context;
207 context = gtk_widget_get_style_context (widget);
208 state = gtk_widget_get_state_flags (widget);
210 gtk_style_context_set_state (context, state);
211 gtk_render_activity (context, cr, 0, 0,
212 gtk_widget_get_allocated_width (widget),
213 gtk_widget_get_allocated_height (widget));
219 gtk_spinner_set_active (GtkSpinner *spinner,
222 GtkSpinnerPrivate *priv = spinner->priv;
226 if (priv->active != active)
228 priv->active = active;
230 g_object_notify (G_OBJECT (spinner), "active");
233 gtk_widget_set_state_flags (GTK_WIDGET (spinner),
234 GTK_STATE_FLAG_ACTIVE, FALSE);
236 gtk_widget_unset_state_flags (GTK_WIDGET (spinner),
237 GTK_STATE_FLAG_ACTIVE);
244 * Returns a new spinner widget. Not yet started.
246 * Return value: a new #GtkSpinner
251 gtk_spinner_new (void)
253 return g_object_new (GTK_TYPE_SPINNER, NULL);
258 * @spinner: a #GtkSpinner
260 * Starts the animation of the spinner.
265 gtk_spinner_start (GtkSpinner *spinner)
267 g_return_if_fail (GTK_IS_SPINNER (spinner));
269 gtk_spinner_set_active (spinner, TRUE);
274 * @spinner: a #GtkSpinner
276 * Stops the animation of the spinner.
281 gtk_spinner_stop (GtkSpinner *spinner)
283 g_return_if_fail (GTK_IS_SPINNER (spinner));
285 gtk_spinner_set_active (spinner, FALSE);