]> Pileus Git - ~andy/gtk/blob - gtk/gtkspinner.c
Initial conversion of GailWidget to GtkWidgetAccessible
[~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/gtkwidgetaccessible.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 GType      _gtk_spinner_accessible_get_type (void);
88
89 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET)
90
91 static void
92 gtk_spinner_class_init (GtkSpinnerClass *klass)
93 {
94   GObjectClass *gobject_class;
95   GtkWidgetClass *widget_class;
96
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;
101
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;
106
107   /* GtkSpinner:active:
108    *
109    * Whether the spinner is active
110    *
111    * Since: 2.20
112    */
113   g_object_class_install_property (gobject_class,
114                                    PROP_ACTIVE,
115                                    g_param_spec_boolean ("active",
116                                                          P_("Active"),
117                                                          P_("Whether the spinner is active"),
118                                                          FALSE,
119                                                          G_PARAM_READWRITE));
120
121   gtk_widget_class_set_accessible_type (widget_class, _gtk_spinner_accessible_get_type ());
122 }
123
124 static void
125 gtk_spinner_get_property (GObject    *object,
126                           guint       param_id,
127                           GValue     *value,
128                           GParamSpec *pspec)
129 {
130   GtkSpinnerPrivate *priv;
131
132   priv = GTK_SPINNER (object)->priv;
133
134   switch (param_id)
135     {
136       case PROP_ACTIVE:
137         g_value_set_boolean (value, priv->active);
138         break;
139       default:
140         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
141     }
142 }
143
144 static void
145 gtk_spinner_set_property (GObject      *object,
146                           guint         param_id,
147                           const GValue *value,
148                           GParamSpec   *pspec)
149 {
150   switch (param_id)
151     {
152       case PROP_ACTIVE:
153         gtk_spinner_set_active (GTK_SPINNER (object), g_value_get_boolean (value));
154         break;
155       default:
156         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
157     }
158 }
159
160 static void
161 gtk_spinner_init (GtkSpinner *spinner)
162 {
163   GtkSpinnerPrivate *priv;
164   GtkStyleContext *context;
165
166   priv = G_TYPE_INSTANCE_GET_PRIVATE (spinner,
167                                       GTK_TYPE_SPINNER,
168                                       GtkSpinnerPrivate);
169   spinner->priv = priv;
170
171   gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
172
173   context = gtk_widget_get_style_context (GTK_WIDGET (spinner));
174   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINNER);
175 }
176
177 static void
178 gtk_spinner_get_preferred_width (GtkWidget *widget,
179                                  gint      *minimum_size,
180                                  gint      *natural_size)
181 {
182   if (minimum_size)
183     *minimum_size = SPINNER_SIZE;
184
185   if (natural_size)
186     *natural_size = SPINNER_SIZE;
187 }
188
189 static void
190 gtk_spinner_get_preferred_height (GtkWidget *widget,
191                                   gint      *minimum_size,
192                                   gint      *natural_size)
193 {
194   if (minimum_size)
195     *minimum_size = SPINNER_SIZE;
196
197   if (natural_size)
198     *natural_size = SPINNER_SIZE;
199 }
200
201 static gboolean
202 gtk_spinner_draw (GtkWidget *widget,
203                   cairo_t   *cr)
204 {
205   GtkStyleContext *context;
206   GtkStateFlags state;
207
208   context = gtk_widget_get_style_context (widget);
209   state = gtk_widget_get_state_flags (widget);
210
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));
215
216   return FALSE;
217 }
218
219 static void
220 gtk_spinner_set_active (GtkSpinner *spinner,
221                         gboolean    active)
222 {
223   GtkSpinnerPrivate *priv = spinner->priv;
224
225   active = !!active;
226
227   if (priv->active != active)
228     {
229       priv->active = active;
230
231       g_object_notify (G_OBJECT (spinner), "active");
232
233       if (active)
234         gtk_widget_set_state_flags (GTK_WIDGET (spinner),
235                                     GTK_STATE_FLAG_ACTIVE, FALSE);
236       else
237         gtk_widget_unset_state_flags (GTK_WIDGET (spinner),
238                                       GTK_STATE_FLAG_ACTIVE);
239     }
240 }
241
242 /* accessible implementation */
243
244 static void
245 gtk_spinner_accessible_image_get_size (AtkImage *image,
246                                        gint     *width,
247                                        gint     *height)
248 {
249   GtkAllocation allocation;
250   GtkWidget *widget;
251
252   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
253   if (widget == NULL)
254     {
255       *width = *height = 0;
256     }
257   else
258     {
259       gtk_widget_get_allocation (widget, &allocation);
260       *width = allocation.width;
261       *height = allocation.height;
262     }
263 }
264
265 static void
266 gtk_spinner_accessible_image_iface_init (AtkImageIface *iface)
267 {
268   iface->get_image_size = gtk_spinner_accessible_image_get_size;
269 }
270
271 /* dummy typedef */
272 typedef GtkWidgetAccessible      GtkSpinnerAccessible;
273 typedef GtkWidgetAccessibleClass GtkSpinnerAccessibleClass;
274
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));
278
279 static void
280 gtk_spinner_accessible_initialize (AtkObject *accessible,
281                                    gpointer   widget)
282 {
283   ATK_OBJECT_CLASS (_gtk_spinner_accessible_parent_class)->initialize (accessible, widget);
284
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);
288 }
289
290 static void
291 _gtk_spinner_accessible_class_init (GtkSpinnerAccessibleClass *klass)
292 {
293   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
294
295   atk_class->initialize = gtk_spinner_accessible_initialize;
296 }
297
298 static void
299 _gtk_spinner_accessible_init (GtkSpinnerAccessible *self)
300 {
301 }
302
303 /**
304  * gtk_spinner_new:
305  *
306  * Returns a new spinner widget. Not yet started.
307  *
308  * Return value: a new #GtkSpinner
309  *
310  * Since: 2.20
311  */
312 GtkWidget *
313 gtk_spinner_new (void)
314 {
315   return g_object_new (GTK_TYPE_SPINNER, NULL);
316 }
317
318 /**
319  * gtk_spinner_start:
320  * @spinner: a #GtkSpinner
321  *
322  * Starts the animation of the spinner.
323  *
324  * Since: 2.20
325  */
326 void
327 gtk_spinner_start (GtkSpinner *spinner)
328 {
329   g_return_if_fail (GTK_IS_SPINNER (spinner));
330
331   gtk_spinner_set_active (spinner, TRUE);
332 }
333
334 /**
335  * gtk_spinner_stop:
336  * @spinner: a #GtkSpinner
337  *
338  * Stops the animation of the spinner.
339  *
340  * Since: 2.20
341  */
342 void
343 gtk_spinner_stop (GtkSpinner *spinner)
344 {
345   g_return_if_fail (GTK_IS_SPINNER (spinner));
346
347   gtk_spinner_set_active (spinner, FALSE);
348 }