]> Pileus Git - ~andy/gtk/blob - gtk/gtkspinner.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
18  *
19  * Code adapted from egg-spinner
20  * by Christian Hergert <christian.hergert@gmail.com>
21  */
22
23 /*
24  * Modified by the GTK+ Team and others 2007.  See the AUTHORS
25  * file for a list of people on the GTK+ Team.  See the ChangeLog
26  * files for a list of changes.  These files are distributed with
27  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28  */
29
30 #include "config.h"
31
32 #include "gtkspinner.h"
33
34 #include "gtkimage.h"
35 #include "gtkintl.h"
36 #include "gtkstylecontext.h"
37 #include "a11y/gtkspinneraccessible.h"
38
39
40 /**
41  * SECTION:gtkspinner
42  * @Short_description: Show a spinner animation
43  * @Title: GtkSpinner
44  * @See_also: #GtkCellRendererSpinner, #GtkProgressBar
45  *
46  * A GtkSpinner widget displays an icon-size spinning animation.
47  * It is often used as an alternative to a #GtkProgressBar for
48  * displaying indefinite activity, instead of actual progress.
49  *
50  * To start the animation, use gtk_spinner_start(), to stop it
51  * use gtk_spinner_stop().
52  */
53
54
55 #define SPINNER_SIZE 12
56
57 enum {
58   PROP_0,
59   PROP_ACTIVE
60 };
61
62 struct _GtkSpinnerPrivate
63 {
64   gboolean active;
65 };
66
67 static gboolean gtk_spinner_draw       (GtkWidget       *widget,
68                                         cairo_t         *cr);
69 static void gtk_spinner_get_property   (GObject         *object,
70                                         guint            param_id,
71                                         GValue          *value,
72                                         GParamSpec      *pspec);
73 static void gtk_spinner_set_property   (GObject         *object,
74                                         guint            param_id,
75                                         const GValue    *value,
76                                         GParamSpec      *pspec);
77 static void gtk_spinner_set_active     (GtkSpinner      *spinner,
78                                         gboolean         active);
79 static void gtk_spinner_get_preferred_width (GtkWidget  *widget,
80                                         gint            *minimum_size,
81                                         gint            *natural_size);
82 static void gtk_spinner_get_preferred_height (GtkWidget *widget,
83                                         gint            *minimum_size,
84                                         gint            *natural_size);
85
86
87 G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET)
88
89 static void
90 gtk_spinner_class_init (GtkSpinnerClass *klass)
91 {
92   GObjectClass *gobject_class;
93   GtkWidgetClass *widget_class;
94
95   gobject_class = G_OBJECT_CLASS(klass);
96   g_type_class_add_private (gobject_class, sizeof (GtkSpinnerPrivate));
97   gobject_class->get_property = gtk_spinner_get_property;
98   gobject_class->set_property = gtk_spinner_set_property;
99
100   widget_class = GTK_WIDGET_CLASS(klass);
101   widget_class->draw = gtk_spinner_draw;
102   widget_class->get_preferred_width = gtk_spinner_get_preferred_width;
103   widget_class->get_preferred_height = gtk_spinner_get_preferred_height;
104
105   /* GtkSpinner:active:
106    *
107    * Whether the spinner is active
108    *
109    * Since: 2.20
110    */
111   g_object_class_install_property (gobject_class,
112                                    PROP_ACTIVE,
113                                    g_param_spec_boolean ("active",
114                                                          P_("Active"),
115                                                          P_("Whether the spinner is active"),
116                                                          FALSE,
117                                                          G_PARAM_READWRITE));
118
119   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SPINNER_ACCESSIBLE);
120 }
121
122 static void
123 gtk_spinner_get_property (GObject    *object,
124                           guint       param_id,
125                           GValue     *value,
126                           GParamSpec *pspec)
127 {
128   GtkSpinnerPrivate *priv;
129
130   priv = GTK_SPINNER (object)->priv;
131
132   switch (param_id)
133     {
134       case PROP_ACTIVE:
135         g_value_set_boolean (value, priv->active);
136         break;
137       default:
138         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
139     }
140 }
141
142 static void
143 gtk_spinner_set_property (GObject      *object,
144                           guint         param_id,
145                           const GValue *value,
146                           GParamSpec   *pspec)
147 {
148   switch (param_id)
149     {
150       case PROP_ACTIVE:
151         gtk_spinner_set_active (GTK_SPINNER (object), g_value_get_boolean (value));
152         break;
153       default:
154         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
155     }
156 }
157
158 static void
159 gtk_spinner_init (GtkSpinner *spinner)
160 {
161   GtkSpinnerPrivate *priv;
162   GtkStyleContext *context;
163
164   priv = G_TYPE_INSTANCE_GET_PRIVATE (spinner,
165                                       GTK_TYPE_SPINNER,
166                                       GtkSpinnerPrivate);
167   spinner->priv = priv;
168
169   gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
170
171   context = gtk_widget_get_style_context (GTK_WIDGET (spinner));
172   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SPINNER);
173 }
174
175 static void
176 gtk_spinner_get_preferred_width (GtkWidget *widget,
177                                  gint      *minimum_size,
178                                  gint      *natural_size)
179 {
180   if (minimum_size)
181     *minimum_size = SPINNER_SIZE;
182
183   if (natural_size)
184     *natural_size = SPINNER_SIZE;
185 }
186
187 static void
188 gtk_spinner_get_preferred_height (GtkWidget *widget,
189                                   gint      *minimum_size,
190                                   gint      *natural_size)
191 {
192   if (minimum_size)
193     *minimum_size = SPINNER_SIZE;
194
195   if (natural_size)
196     *natural_size = SPINNER_SIZE;
197 }
198
199 static gboolean
200 gtk_spinner_draw (GtkWidget *widget,
201                   cairo_t   *cr)
202 {
203   GtkStyleContext *context;
204   gint width, height;
205   gint size;
206
207   context = gtk_widget_get_style_context (widget);
208
209   width = gtk_widget_get_allocated_width (widget);
210   height = gtk_widget_get_allocated_height (widget);
211   size = MIN (width, height);
212
213   gtk_render_activity (context, cr,
214                        (width - size) / 2,
215                        (height - size) / 2,
216                        size, size);
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 /**
245  * gtk_spinner_new:
246  *
247  * Returns a new spinner widget. Not yet started.
248  *
249  * Return value: a new #GtkSpinner
250  *
251  * Since: 2.20
252  */
253 GtkWidget *
254 gtk_spinner_new (void)
255 {
256   return g_object_new (GTK_TYPE_SPINNER, NULL);
257 }
258
259 /**
260  * gtk_spinner_start:
261  * @spinner: a #GtkSpinner
262  *
263  * Starts the animation of the spinner.
264  *
265  * Since: 2.20
266  */
267 void
268 gtk_spinner_start (GtkSpinner *spinner)
269 {
270   g_return_if_fail (GTK_IS_SPINNER (spinner));
271
272   gtk_spinner_set_active (spinner, TRUE);
273 }
274
275 /**
276  * gtk_spinner_stop:
277  * @spinner: a #GtkSpinner
278  *
279  * Stops the animation of the spinner.
280  *
281  * Since: 2.20
282  */
283 void
284 gtk_spinner_stop (GtkSpinner *spinner)
285 {
286   g_return_if_fail (GTK_IS_SPINNER (spinner));
287
288   gtk_spinner_set_active (spinner, FALSE);
289 }