]> Pileus Git - ~andy/gtk/blob - gtk/gtkspinner.c
[GI] Add (type) annotations to real types
[~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 "gtkstyle.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 static AtkObject *gtk_spinner_get_accessible      (GtkWidget *widget);
88 static GType      gtk_spinner_accessible_get_type (void);
89
90 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET)
91
92 static void
93 gtk_spinner_class_init (GtkSpinnerClass *klass)
94 {
95   GObjectClass *gobject_class;
96   GtkWidgetClass *widget_class;
97
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;
102
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;
108
109   /* GtkSpinner:active:
110    *
111    * Whether the spinner is active
112    *
113    * Since: 2.20
114    */
115   g_object_class_install_property (gobject_class,
116                                    PROP_ACTIVE,
117                                    g_param_spec_boolean ("active",
118                                                          P_("Active"),
119                                                          P_("Whether the spinner is active"),
120                                                          FALSE,
121                                                          G_PARAM_READWRITE));
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   GtkSpinnerPrivate *priv;
206   GtkStyleContext *context;
207   GtkStateFlags state;
208
209   priv = GTK_SPINNER (widget)->priv;
210   context = gtk_widget_get_style_context (widget);
211   state = gtk_widget_get_state_flags (widget);
212
213   gtk_style_context_set_state (context, state);
214   gtk_render_activity (context, cr, 0, 0,
215                        gtk_widget_get_allocated_width (widget),
216                        gtk_widget_get_allocated_height (widget));
217
218   return FALSE;
219 }
220
221 static void
222 gtk_spinner_set_active (GtkSpinner *spinner,
223                         gboolean    active)
224 {
225   GtkSpinnerPrivate *priv = spinner->priv;
226
227   active = !!active;
228
229   if (priv->active != active)
230     {
231       priv->active = active;
232
233       g_object_notify (G_OBJECT (spinner), "active");
234
235       if (active)
236         gtk_widget_set_state_flags (GTK_WIDGET (spinner),
237                                     GTK_STATE_FLAG_ACTIVE, FALSE);
238       else
239         gtk_widget_unset_state_flags (GTK_WIDGET (spinner),
240                                       GTK_STATE_FLAG_ACTIVE);
241     }
242 }
243
244 /* accessible implementation */
245
246 static void
247 gtk_spinner_accessible_image_get_size (AtkImage *image,
248                                        gint     *width,
249                                        gint     *height)
250 {
251   GtkAllocation allocation;
252   GtkWidget *widget;
253
254   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (image));
255   if (widget == NULL)
256     {
257       *width = *height = 0;
258     }
259   else
260     {
261       gtk_widget_get_allocation (widget, &allocation);
262       *width = allocation.width;
263       *height = allocation.height;
264     }
265 }
266
267 static void
268 gtk_spinner_accessible_image_iface_init (AtkImageIface *iface)
269 {
270   iface->get_image_size = gtk_spinner_accessible_image_get_size;
271 }
272
273 /* dummy typedef */
274 typedef struct _GtkSpinnerAccessible            GtkSpinnerAccessible;
275 typedef struct _GtkSpinnerAccessibleClass       GtkSpinnerAccessibleClass;
276
277 ATK_DEFINE_TYPE_WITH_CODE (GtkSpinnerAccessible,
278                            gtk_spinner_accessible,
279                            GTK_TYPE_IMAGE,
280                            G_IMPLEMENT_INTERFACE (ATK_TYPE_IMAGE,
281                                                   gtk_spinner_accessible_image_iface_init));
282
283 static void
284 gtk_spinner_accessible_initialize (AtkObject *accessible,
285                                    gpointer   widget)
286 {
287   ATK_OBJECT_CLASS (gtk_spinner_accessible_parent_class)->initialize (accessible, widget);
288
289   atk_object_set_name (accessible, C_("throbbing progress animation widget", "Spinner"));
290   atk_object_set_description (accessible, _("Provides visual indication of progress"));
291 }
292
293 static void
294 gtk_spinner_accessible_class_init (GtkSpinnerAccessibleClass *klass)
295 {
296   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
297
298   atk_class->initialize = gtk_spinner_accessible_initialize;
299 }
300
301 static void
302 gtk_spinner_accessible_init (GtkSpinnerAccessible *self)
303 {
304 }
305
306 /* factory */
307 typedef AtkObjectFactory        GtkSpinnerAccessibleFactory;
308 typedef AtkObjectFactoryClass   GtkSpinnerAccessibleFactoryClass;
309
310 G_DEFINE_TYPE (GtkSpinnerAccessibleFactory,
311                _gtk_spinner_accessible_factory,
312                ATK_TYPE_OBJECT_FACTORY);
313
314 static GType
315 gtk_spinner_accessible_factory_get_accessible_type (void)
316 {
317   return gtk_spinner_accessible_get_type ();
318 }
319
320 static AtkObject *
321 gtk_spinner_accessible_factory_create_accessible (GObject *obj)
322 {
323   AtkObject *accessible;
324
325   accessible = g_object_new (gtk_spinner_accessible_get_type (), NULL);
326   atk_object_initialize (accessible, obj);
327
328   return accessible;
329 }
330
331 static void
332 _gtk_spinner_accessible_factory_class_init (AtkObjectFactoryClass *klass)
333 {
334   klass->create_accessible = gtk_spinner_accessible_factory_create_accessible;
335   klass->get_accessible_type = gtk_spinner_accessible_factory_get_accessible_type;
336 }
337
338 static void
339 _gtk_spinner_accessible_factory_init (AtkObjectFactory *factory)
340 {
341 }
342
343 static AtkObject *
344 gtk_spinner_get_accessible (GtkWidget *widget)
345 {
346   static gboolean first_time = TRUE;
347
348   if (first_time)
349     {
350       AtkObjectFactory *factory;
351       AtkRegistry *registry;
352       GType derived_type;
353       GType derived_atk_type;
354
355       /*
356        * Figure out whether accessibility is enabled by looking at the
357        * type of the accessible object which would be created for
358        * the parent type of GtkSpinner.
359        */
360       derived_type = g_type_parent (GTK_TYPE_SPINNER);
361
362       registry = atk_get_default_registry ();
363       factory = atk_registry_get_factory (registry, derived_type);
364       derived_atk_type = atk_object_factory_get_accessible_type (factory);
365       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
366         atk_registry_set_factory_type (registry,
367                                        GTK_TYPE_SPINNER,
368                                        _gtk_spinner_accessible_factory_get_type ());
369       first_time = FALSE;
370     }
371
372   return GTK_WIDGET_CLASS (gtk_spinner_parent_class)->get_accessible (widget);
373 }
374
375 /**
376  * gtk_spinner_new:
377  *
378  * Returns a new spinner widget. Not yet started.
379  *
380  * Return value: a new #GtkSpinner
381  *
382  * Since: 2.20
383  */
384 GtkWidget *
385 gtk_spinner_new (void)
386 {
387   return g_object_new (GTK_TYPE_SPINNER, NULL);
388 }
389
390 /**
391  * gtk_spinner_start:
392  * @spinner: a #GtkSpinner
393  *
394  * Starts the animation of the spinner.
395  *
396  * Since: 2.20
397  */
398 void
399 gtk_spinner_start (GtkSpinner *spinner)
400 {
401   g_return_if_fail (GTK_IS_SPINNER (spinner));
402
403   gtk_spinner_set_active (spinner, TRUE);
404 }
405
406 /**
407  * gtk_spinner_stop:
408  * @spinner: a #GtkSpinner
409  *
410  * Stops the animation of the spinner.
411  *
412  * Since: 2.20
413  */
414 void
415 gtk_spinner_stop (GtkSpinner *spinner)
416 {
417   g_return_if_fail (GTK_IS_SPINNER (spinner));
418
419   gtk_spinner_set_active (spinner, FALSE);
420 }