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 "gtkprivate.h" |
37 | #include "gtkcssnodeprivate.h" |
38 | #include "gtkwidgetprivate.h" |
39 | #include "gtkcssnumbervalueprivate.h" |
40 | #include "gtkrendericonprivate.h" |
41 | |
42 | |
43 | /** |
44 | * GtkSpinner: |
45 | * |
46 | * A `GtkSpinner` widget displays an icon-size spinning animation. |
47 | * |
48 | * It is often used as an alternative to a [class@Gtk.ProgressBar] |
49 | * for displaying indefinite activity, instead of actual progress. |
50 | * |
51 | * ![An example GtkSpinner](spinner.png) |
52 | * |
53 | * To start the animation, use [method@Gtk.Spinner.start], to stop it |
54 | * use [method@Gtk.Spinner.stop]. |
55 | * |
56 | * # CSS nodes |
57 | * |
58 | * `GtkSpinner` has a single CSS node with the name spinner. |
59 | * When the animation is active, the :checked pseudoclass is |
60 | * added to this node. |
61 | */ |
62 | |
63 | typedef struct _GtkSpinnerClass GtkSpinnerClass; |
64 | |
65 | struct _GtkSpinner |
66 | { |
67 | GtkWidget parent; |
68 | }; |
69 | |
70 | struct _GtkSpinnerClass |
71 | { |
72 | GtkWidgetClass parent_class; |
73 | }; |
74 | |
75 | |
76 | enum { |
77 | PROP_0, |
78 | PROP_SPINNING |
79 | }; |
80 | |
81 | G_DEFINE_TYPE (GtkSpinner, gtk_spinner, GTK_TYPE_WIDGET) |
82 | |
83 | #define DEFAULT_SIZE 16 |
84 | |
85 | static void |
86 | gtk_spinner_measure (GtkWidget *widget, |
87 | GtkOrientation orientation, |
88 | int for_size, |
89 | int *minimum, |
90 | int *natural, |
91 | int *minimum_baseline, |
92 | int *natural_baseline) |
93 | { |
94 | GtkCssStyle *style; |
95 | |
96 | style = gtk_css_node_get_style (cssnode: gtk_widget_get_css_node (widget)); |
97 | *minimum = *natural = _gtk_css_number_value_get (number: style->icon->icon_size, one_hundred_percent: 100); |
98 | } |
99 | |
100 | static void |
101 | gtk_spinner_snapshot (GtkWidget *widget, |
102 | GtkSnapshot *snapshot) |
103 | { |
104 | GtkCssStyle *style = gtk_css_node_get_style (cssnode: gtk_widget_get_css_node (widget)); |
105 | |
106 | gtk_css_style_snapshot_icon (style, |
107 | snapshot, |
108 | width: gtk_widget_get_width (widget), |
109 | height: gtk_widget_get_height (widget)); |
110 | } |
111 | |
112 | static void |
113 | gtk_spinner_css_changed (GtkWidget *widget, |
114 | GtkCssStyleChange *change) |
115 | { |
116 | GTK_WIDGET_CLASS (gtk_spinner_parent_class)->css_changed (widget, change); |
117 | |
118 | if (change == NULL || |
119 | gtk_css_style_change_affects (change, affects: GTK_CSS_AFFECTS_ICON_SIZE)) |
120 | { |
121 | gtk_widget_queue_resize (widget); |
122 | } |
123 | else if (gtk_css_style_change_affects (change, affects: GTK_CSS_AFFECTS_ICON_TEXTURE | |
124 | GTK_CSS_AFFECTS_ICON_REDRAW)) |
125 | { |
126 | gtk_widget_queue_draw (widget); |
127 | } |
128 | } |
129 | |
130 | /** |
131 | * gtk_spinner_get_spinning: (attributes org.gtk.Method.get_property=spinning) |
132 | * @spinner: a `GtkSpinner` |
133 | * |
134 | * Returns whether the spinner is spinning. |
135 | * |
136 | * Returns: %TRUE if the spinner is active |
137 | */ |
138 | gboolean |
139 | gtk_spinner_get_spinning (GtkSpinner *spinner) |
140 | { |
141 | g_return_val_if_fail (GTK_IS_SPINNER (spinner), FALSE); |
142 | |
143 | return (gtk_widget_get_state_flags (widget: (GtkWidget *)spinner) & GTK_STATE_FLAG_CHECKED) > 0; |
144 | } |
145 | |
146 | /** |
147 | * gtk_spinner_set_spinning: (attributes org.gtk.Method.set_property=spinning) |
148 | * @spinner: a `GtkSpinner` |
149 | * @spinning: whether the spinner should be spinning |
150 | * |
151 | * Sets the activity of the spinner. |
152 | */ |
153 | void |
154 | gtk_spinner_set_spinning (GtkSpinner *spinner, |
155 | gboolean spinning) |
156 | { |
157 | g_return_if_fail (GTK_IS_SPINNER (spinner)); |
158 | |
159 | spinning = !!spinning; |
160 | |
161 | if (spinning != gtk_spinner_get_spinning (spinner)) |
162 | { |
163 | g_object_notify (G_OBJECT (spinner), property_name: "spinning" ); |
164 | |
165 | if (spinning) |
166 | gtk_widget_set_state_flags (GTK_WIDGET (spinner), |
167 | flags: GTK_STATE_FLAG_CHECKED, FALSE); |
168 | else |
169 | gtk_widget_unset_state_flags (GTK_WIDGET (spinner), |
170 | flags: GTK_STATE_FLAG_CHECKED); |
171 | } |
172 | } |
173 | |
174 | static void |
175 | gtk_spinner_get_property (GObject *object, |
176 | guint param_id, |
177 | GValue *value, |
178 | GParamSpec *pspec) |
179 | { |
180 | switch (param_id) |
181 | { |
182 | case PROP_SPINNING: |
183 | g_value_set_boolean (value, v_boolean: gtk_spinner_get_spinning (GTK_SPINNER (object))); |
184 | break; |
185 | default: |
186 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); |
187 | } |
188 | } |
189 | |
190 | static void |
191 | gtk_spinner_set_property (GObject *object, |
192 | guint param_id, |
193 | const GValue *value, |
194 | GParamSpec *pspec) |
195 | { |
196 | switch (param_id) |
197 | { |
198 | case PROP_SPINNING: |
199 | gtk_spinner_set_spinning (GTK_SPINNER (object), spinning: g_value_get_boolean (value)); |
200 | break; |
201 | default: |
202 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); |
203 | } |
204 | } |
205 | |
206 | static void |
207 | gtk_spinner_class_init (GtkSpinnerClass *klass) |
208 | { |
209 | GObjectClass *gobject_class; |
210 | GtkWidgetClass *widget_class; |
211 | |
212 | gobject_class = G_OBJECT_CLASS(klass); |
213 | gobject_class->get_property = gtk_spinner_get_property; |
214 | gobject_class->set_property = gtk_spinner_set_property; |
215 | |
216 | widget_class = GTK_WIDGET_CLASS(klass); |
217 | widget_class->snapshot = gtk_spinner_snapshot; |
218 | widget_class->measure = gtk_spinner_measure; |
219 | widget_class->css_changed = gtk_spinner_css_changed; |
220 | |
221 | /** |
222 | * GtkSpinner:spinning: (attributes org.gtk.Property.get=gtk_spinner_get_spinning org.gtk.Property.set=gtk_spinner_set_spinning) |
223 | * |
224 | * Whether the spinner is spinning |
225 | */ |
226 | g_object_class_install_property (oclass: gobject_class, |
227 | property_id: PROP_SPINNING, |
228 | pspec: g_param_spec_boolean (name: "spinning" , |
229 | P_("Spinning" ), |
230 | P_("Whether the spinner is spinning" ), |
231 | FALSE, |
232 | GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY)); |
233 | |
234 | gtk_widget_class_set_css_name (widget_class, I_("spinner" )); |
235 | } |
236 | |
237 | static void |
238 | gtk_spinner_init (GtkSpinner *spinner) |
239 | { |
240 | } |
241 | |
242 | /** |
243 | * gtk_spinner_new: |
244 | * |
245 | * Returns a new spinner widget. Not yet started. |
246 | * |
247 | * Returns: a new `GtkSpinner` |
248 | */ |
249 | GtkWidget * |
250 | gtk_spinner_new (void) |
251 | { |
252 | return g_object_new (GTK_TYPE_SPINNER, NULL); |
253 | } |
254 | |
255 | /** |
256 | * gtk_spinner_start: |
257 | * @spinner: a `GtkSpinner` |
258 | * |
259 | * Starts the animation of the spinner. |
260 | */ |
261 | void |
262 | gtk_spinner_start (GtkSpinner *spinner) |
263 | { |
264 | g_return_if_fail (GTK_IS_SPINNER (spinner)); |
265 | |
266 | gtk_spinner_set_spinning (spinner, TRUE); |
267 | } |
268 | |
269 | /** |
270 | * gtk_spinner_stop: |
271 | * @spinner: a `GtkSpinner` |
272 | * |
273 | * Stops the animation of the spinner. |
274 | */ |
275 | void |
276 | gtk_spinner_stop (GtkSpinner *spinner) |
277 | { |
278 | g_return_if_fail (GTK_IS_SPINNER (spinner)); |
279 | |
280 | gtk_spinner_set_spinning (spinner, FALSE); |
281 | } |
282 | |