]> Pileus Git - ~andy/gtk/blob - gtk/gtkspinner.c
spinner: Stop setting state flags on the style context for drawing
[~andy/gtk] / gtk / gtkspinner.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2007 John Stowers, Neil Jagdish Patel.
4  * Copyright (C) 2009 Bastien Nocera, David Zeuthen
5  *
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.
10  *
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.
15  *
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.
20  *
21  * Code adapted from egg-spinner
22  * by Christian Hergert <christian.hergert@gmail.com>
23  */
24
25 /*
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/.
30  */
31
32 #include "config.h"
33
34 #include "gtkintl.h"
35 #include "gtkaccessible.h"
36 #include "gtkimage.h"
37 #include "gtkspinner.h"
38 #include "a11y/gtkspinneraccessible.h"
39
40
41 /**
42  * SECTION:gtkspinner
43  * @Short_description: Show a spinner animation
44  * @Title: GtkSpinner
45  * @See_also: #GtkCellRendererSpinner, #GtkProgressBar
46  *
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.
50  *
51  * To start the animation, use gtk_spinner_start(), to stop it
52  * use gtk_spinner_stop().
53  */
54
55
56 #define SPINNER_SIZE 12
57
58 enum {
59   PROP_0,
60   PROP_ACTIVE
61 };
62
63 struct _GtkSpinnerPrivate
64 {
65   gboolean active;
66 };
67
68 static gboolean gtk_spinner_draw       (GtkWidget       *widget,
69                                         cairo_t         *cr);
70 static void gtk_spinner_get_property   (GObject         *object,
71                                         guint            param_id,
72                                         GValue          *value,
73                                         GParamSpec      *pspec);
74 static void gtk_spinner_set_property   (GObject         *object,
75                                         guint            param_id,
76                                         const GValue    *value,
77                                         GParamSpec      *pspec);
78 static void gtk_spinner_set_active     (GtkSpinner      *spinner,
79                                         gboolean         active);
80 static void gtk_spinner_get_preferred_width (GtkWidget  *widget,
81                                         gint            *minimum_size,
82                                         gint            *natural_size);
83 static void gtk_spinner_get_preferred_height (GtkWidget *widget,
84                                         gint            *minimum_size,
85                                         gint            *natural_size);
86
87
88 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET)
89
90 static void
91 gtk_spinner_class_init (GtkSpinnerClass *klass)
92 {
93   GObjectClass *gobject_class;
94   GtkWidgetClass *widget_class;
95
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;
100
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;
105
106   /* GtkSpinner:active:
107    *
108    * Whether the spinner is active
109    *
110    * Since: 2.20
111    */
112   g_object_class_install_property (gobject_class,
113                                    PROP_ACTIVE,
114                                    g_param_spec_boolean ("active",
115                                                          P_("Active"),
116                                                          P_("Whether the spinner is active"),
117                                                          FALSE,
118                                                          G_PARAM_READWRITE));
119
120   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SPINNER_ACCESSIBLE);
121 }
122
123 static void
124 gtk_spinner_get_property (GObject    *object,
125                           guint       param_id,
126                           GValue     *value,
127                           GParamSpec *pspec)
128 {
129   GtkSpinnerPrivate *priv;
130
131   priv = GTK_SPINNER (object)->priv;
132
133   switch (param_id)
134     {
135       case PROP_ACTIVE:
136         g_value_set_boolean (value, priv->active);
137         break;
138       default:
139         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
140     }
141 }
142
143 static void
144 gtk_spinner_set_property (GObject      *object,
145                           guint         param_id,
146                           const GValue *value,
147                           GParamSpec   *pspec)
148 {
149   switch (param_id)
150     {
151       case PROP_ACTIVE:
152         gtk_spinner_set_active (GTK_SPINNER (object), g_value_get_boolean (value));
153         break;
154       default:
155         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
156     }
157 }
158
159 static void
160 gtk_spinner_init (GtkSpinner *spinner)
161 {
162   GtkSpinnerPrivate *priv;
163   GtkStyleContext *context;
164
165   priv = G_TYPE_INSTANCE_GET_PRIVATE (spinner,
166                                       GTK_TYPE_SPINNER,
167                                       GtkSpinnerPrivate);
168   spinner->priv = priv;
169
170   gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
171
172   context = gtk_widget_get_style_context (GTK_WIDGET (spinner));
173   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINNER);
174 }
175
176 static void
177 gtk_spinner_get_preferred_width (GtkWidget *widget,
178                                  gint      *minimum_size,
179                                  gint      *natural_size)
180 {
181   if (minimum_size)
182     *minimum_size = SPINNER_SIZE;
183
184   if (natural_size)
185     *natural_size = SPINNER_SIZE;
186 }
187
188 static void
189 gtk_spinner_get_preferred_height (GtkWidget *widget,
190                                   gint      *minimum_size,
191                                   gint      *natural_size)
192 {
193   if (minimum_size)
194     *minimum_size = SPINNER_SIZE;
195
196   if (natural_size)
197     *natural_size = SPINNER_SIZE;
198 }
199
200 static gboolean
201 gtk_spinner_draw (GtkWidget *widget,
202                   cairo_t   *cr)
203 {
204   GtkStyleContext *context;
205
206   context = gtk_widget_get_style_context (widget);
207
208   gtk_render_activity (context, cr, 0, 0,
209                        gtk_widget_get_allocated_width (widget),
210                        gtk_widget_get_allocated_height (widget));
211
212   return FALSE;
213 }
214
215 static void
216 gtk_spinner_set_active (GtkSpinner *spinner,
217                         gboolean    active)
218 {
219   GtkSpinnerPrivate *priv = spinner->priv;
220
221   active = !!active;
222
223   if (priv->active != active)
224     {
225       priv->active = active;
226
227       g_object_notify (G_OBJECT (spinner), "active");
228
229       if (active)
230         gtk_widget_set_state_flags (GTK_WIDGET (spinner),
231                                     GTK_STATE_FLAG_ACTIVE, FALSE);
232       else
233         gtk_widget_unset_state_flags (GTK_WIDGET (spinner),
234                                       GTK_STATE_FLAG_ACTIVE);
235     }
236 }
237
238 /**
239  * gtk_spinner_new:
240  *
241  * Returns a new spinner widget. Not yet started.
242  *
243  * Return value: a new #GtkSpinner
244  *
245  * Since: 2.20
246  */
247 GtkWidget *
248 gtk_spinner_new (void)
249 {
250   return g_object_new (GTK_TYPE_SPINNER, NULL);
251 }
252
253 /**
254  * gtk_spinner_start:
255  * @spinner: a #GtkSpinner
256  *
257  * Starts the animation of the spinner.
258  *
259  * Since: 2.20
260  */
261 void
262 gtk_spinner_start (GtkSpinner *spinner)
263 {
264   g_return_if_fail (GTK_IS_SPINNER (spinner));
265
266   gtk_spinner_set_active (spinner, TRUE);
267 }
268
269 /**
270  * gtk_spinner_stop:
271  * @spinner: a #GtkSpinner
272  *
273  * Stops the animation of the spinner.
274  *
275  * Since: 2.20
276  */
277 void
278 gtk_spinner_stop (GtkSpinner *spinner)
279 {
280   g_return_if_fail (GTK_IS_SPINNER (spinner));
281
282   gtk_spinner_set_active (spinner, FALSE);
283 }