~dylanmccall/ubuntu/oneiric/network-manager-applet/lp852961-disable-autostart-for-gnome-shell

« back to all changes in this revision

Viewing changes to src/utils/nma-bling-spinner.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2011-05-30 13:25:18 UTC
  • mto: This revision was merged to the branch mainline in revision 68.
  • Revision ID: james.westby@ubuntu.com-20110530132518-ya5i5mcrl8szsmoj
Tags: upstream-0.8.9997+git.20110529t170033.9ec4c5d
ImportĀ upstreamĀ versionĀ 0.8.9997+git.20110529t170033.9ec4c5d

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
 
/*
3
 
 * @file libbling/bling-spinner.c A apple-esque spinner widger
4
 
 *
5
 
 * @Copyright (C) 2007 John Stowers, Neil Jagdish Patel.
6
 
 *
7
 
 * This library is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Lesser General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2 of the License, or (at your option) any later version.
11
 
 *
12
 
 * This library is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * Lesser General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with this library; if not, write to the
19
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
 
 * Boston, MA  02111-1307, USA.
21
 
 *
22
 
 * Code adapted from egg-spinner
23
 
 * by Christian Hergert <christian.hergert@gmail.com>
24
 
 */
25
 
 
26
 
#ifdef HAVE_CONFIG_H
27
 
#include <config.h>
28
 
#endif
29
 
 
30
 
#include <gtk/gtk.h>
31
 
#include <math.h>
32
 
 
33
 
#include "nma-bling-spinner.h"
34
 
 
35
 
#define NMA_BLING_SPINNER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), NMA_TYPE_BLING_SPINNER, NmaBlingSpinnerPrivate))
36
 
 
37
 
G_DEFINE_TYPE (NmaBlingSpinner, nma_bling_spinner, GTK_TYPE_DRAWING_AREA);
38
 
 
39
 
enum
40
 
{
41
 
        PROP_0,
42
 
        PROP_NUM_LINES
43
 
};
44
 
 
45
 
/* STRUCTS & ENUMS */
46
 
struct _NmaBlingSpinnerPrivate
47
 
{
48
 
        /* state */
49
 
        guint current;
50
 
        guint timeout;
51
 
 
52
 
        /* appearance */
53
 
        guint lines;
54
 
};
55
 
 
56
 
/* FORWARDS */
57
 
static void nma_bling_spinner_class_init(NmaBlingSpinnerClass *klass);
58
 
static void nma_bling_spinner_init(NmaBlingSpinner *spinner);
59
 
static void nma_bling_spinner_finalize (GObject *gobject);
60
 
static void nma_bling_spinner_set_property(GObject *gobject, guint prop_id, const GValue *value, GParamSpec *pspec);
61
 
static gboolean nma_bling_spinner_expose(GtkWidget *widget, GdkEventExpose *event);
62
 
static void nma_bling_spinner_screen_changed (GtkWidget* widget, GdkScreen* old_screen);
63
 
 
64
 
static GtkDrawingAreaClass *parent_class;
65
 
 
66
 
/* DRAWING FUNCTIONS */
67
 
static void
68
 
draw (GtkWidget *widget, cairo_t *cr)
69
 
{
70
 
        NmaBlingSpinnerPrivate *priv;
71
 
        GtkAllocation allocation;
72
 
        double x, y;
73
 
        double radius;
74
 
        double half;
75
 
        int i;
76
 
 
77
 
        priv = NMA_BLING_SPINNER_GET_PRIVATE (widget);
78
 
 
79
 
        cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
80
 
 
81
 
#if GTK_CHECK_VERSION(2,18,0)
82
 
        gtk_widget_get_allocation (widget, &allocation);
83
 
#else
84
 
        allocation = widget->allocation;
85
 
#endif
86
 
        if ((allocation.width < 12) || (allocation.height < 12))
87
 
                gtk_widget_set_size_request(widget, 12, 12);
88
 
 
89
 
        //x = allocation.x + allocation.width / 2;
90
 
        //y = allocation.y + allocation.height / 2;
91
 
        x = allocation.width / 2;
92
 
        y = allocation.height / 2;
93
 
        radius = MIN (allocation.width / 2, allocation.height / 2);
94
 
        half = priv->lines / 2;
95
 
 
96
 
        /*FIXME: render in B&W for non transparency */
97
 
 
98
 
        for (i = 0; i < priv->lines; i++) {
99
 
                int inset = 0.7 * radius;
100
 
                /* transparency is a function of time and intial value */
101
 
                double t = (double) ((i + priv->lines - priv->current)
102
 
                                   % priv->lines) / priv->lines;
103
 
 
104
 
                cairo_save (cr);
105
 
 
106
 
                cairo_set_source_rgba (cr, 0, 0, 0, t);
107
 
                //cairo_set_line_width (cr, 2 * cairo_get_line_width (cr));
108
 
                cairo_set_line_width (cr, 2.0);
109
 
                cairo_move_to (cr,
110
 
                                           x + (radius - inset) * cos (i * M_PI / half),
111
 
                                           y + (radius - inset) * sin (i * M_PI / half));
112
 
                cairo_line_to (cr,
113
 
                                           x + radius * cos (i * M_PI / half),
114
 
                                           y + radius * sin (i * M_PI / half));
115
 
                cairo_stroke (cr);
116
 
 
117
 
                cairo_restore (cr);
118
 
        }
119
 
}
120
 
 
121
 
 
122
 
/*      GOBJECT INIT CODE */
123
 
static void
124
 
nma_bling_spinner_class_init(NmaBlingSpinnerClass *klass)
125
 
{
126
 
        GObjectClass *gobject_class;
127
 
        GtkWidgetClass *widget_class;
128
 
 
129
 
        parent_class = g_type_class_peek_parent(klass);
130
 
 
131
 
        gobject_class = G_OBJECT_CLASS(klass);
132
 
        g_type_class_add_private (gobject_class, sizeof (NmaBlingSpinnerPrivate));
133
 
        gobject_class->set_property = nma_bling_spinner_set_property;
134
 
        gobject_class->finalize = nma_bling_spinner_finalize;
135
 
 
136
 
        widget_class = GTK_WIDGET_CLASS(klass);
137
 
        widget_class->expose_event = nma_bling_spinner_expose;
138
 
        widget_class->screen_changed = nma_bling_spinner_screen_changed;
139
 
 
140
 
        g_object_class_install_property(gobject_class, PROP_NUM_LINES,
141
 
                g_param_spec_uint("lines", "Num Lines",
142
 
                                                        "The number of lines to animate",
143
 
                                                        0,20,12,
144
 
                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
145
 
 
146
 
}
147
 
 
148
 
static void
149
 
nma_bling_spinner_init (NmaBlingSpinner *spinner)
150
 
{
151
 
        NmaBlingSpinnerPrivate *priv;
152
 
 
153
 
        priv = NMA_BLING_SPINNER_GET_PRIVATE (spinner);
154
 
        priv->current = 0;
155
 
        priv->timeout = 0;
156
 
 
157
 
#if GTK_CHECK_VERSION(2,18,0)
158
 
        gtk_widget_set_has_window (GTK_WIDGET (spinner), FALSE);
159
 
#else
160
 
        GTK_WIDGET_SET_FLAGS (GTK_WIDGET (spinner), GTK_NO_WINDOW);
161
 
#endif
162
 
}
163
 
 
164
 
static gboolean
165
 
nma_bling_spinner_expose (GtkWidget *widget, GdkEventExpose *event)
166
 
{
167
 
        cairo_t *cr;
168
 
 
169
 
        /* get cairo context */
170
 
        cr = gdk_cairo_create (gtk_widget_get_window (widget));
171
 
 
172
 
        /* set a clip region for the expose event */
173
 
        cairo_rectangle (cr,
174
 
                                         event->area.x, event->area.y,
175
 
                                         event->area.width, event->area.height);
176
 
        cairo_clip (cr);
177
 
 
178
 
        cairo_translate (cr, event->area.x, event->area.y);
179
 
 
180
 
        /* draw clip region */
181
 
        draw (widget, cr);
182
 
 
183
 
        /* free memory */
184
 
        cairo_destroy (cr);
185
 
 
186
 
        return FALSE;
187
 
}
188
 
 
189
 
static void
190
 
nma_bling_spinner_screen_changed (GtkWidget* widget, GdkScreen* old_screen)
191
 
{
192
 
        NmaBlingSpinner *spinner;
193
 
        GdkScreen* new_screen;
194
 
        GdkColormap* colormap;
195
 
 
196
 
        spinner = NMA_BLING_SPINNER(widget);
197
 
 
198
 
        new_screen = gtk_widget_get_screen (widget);
199
 
        colormap = gdk_screen_get_rgba_colormap (new_screen);
200
 
 
201
 
        if (!colormap)
202
 
                colormap = gdk_screen_get_rgb_colormap (new_screen);
203
 
 
204
 
        gtk_widget_set_colormap (widget, colormap);
205
 
}
206
 
 
207
 
static gboolean
208
 
nma_bling_spinner_timeout (gpointer data)
209
 
{
210
 
        NmaBlingSpinner *spinner;
211
 
        NmaBlingSpinnerPrivate *priv;
212
 
 
213
 
        spinner = NMA_BLING_SPINNER (data);
214
 
        priv = NMA_BLING_SPINNER_GET_PRIVATE (spinner);
215
 
 
216
 
        if (priv->current + 1 >= priv->lines) {
217
 
                priv->current = 0;
218
 
        } else {
219
 
                priv->current++;
220
 
        }
221
 
 
222
 
        gtk_widget_queue_draw (GTK_WIDGET (data));
223
 
 
224
 
        return TRUE;
225
 
}
226
 
 
227
 
static void
228
 
nma_bling_spinner_set_property(GObject *gobject, guint prop_id,
229
 
                                        const GValue *value, GParamSpec *pspec)
230
 
{
231
 
        NmaBlingSpinner *spinner;
232
 
        NmaBlingSpinnerPrivate *priv;
233
 
 
234
 
        spinner = NMA_BLING_SPINNER(gobject);
235
 
        priv = NMA_BLING_SPINNER_GET_PRIVATE (spinner);
236
 
 
237
 
        switch (prop_id)
238
 
        {
239
 
                case PROP_NUM_LINES:
240
 
                        priv->lines = g_value_get_uint(value);
241
 
                        break;
242
 
                default:
243
 
                        G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
244
 
                        break;
245
 
        }
246
 
}
247
 
 
248
 
static void
249
 
nma_bling_spinner_finalize (GObject *gobject)
250
 
{
251
 
        NmaBlingSpinner *spinner;
252
 
        NmaBlingSpinnerPrivate *priv;
253
 
 
254
 
        spinner = NMA_BLING_SPINNER(gobject);
255
 
        priv = NMA_BLING_SPINNER_GET_PRIVATE (spinner);
256
 
 
257
 
        if (priv->timeout != 0) {
258
 
                g_source_remove (priv->timeout);
259
 
                priv->timeout = 0;
260
 
        }
261
 
}
262
 
 
263
 
/**
264
 
 * nma_bling_spinner_new
265
 
 *
266
 
 * Returns a default spinner. Not yet started.
267
 
 *
268
 
 * Returns: a new #NmaBlingSpinner
269
 
 */
270
 
GtkWidget *
271
 
nma_bling_spinner_new (void)
272
 
{
273
 
        return g_object_new (NMA_TYPE_BLING_SPINNER, NULL);
274
 
}
275
 
 
276
 
/**
277
 
 * nma_bling_spinner_start
278
 
 *
279
 
 * Starts the animation
280
 
 */
281
 
void
282
 
nma_bling_spinner_start (NmaBlingSpinner *spinner)
283
 
{
284
 
        NmaBlingSpinnerPrivate *priv;
285
 
 
286
 
        g_return_if_fail (NMA_IS_BLING_SPINNER (spinner));
287
 
 
288
 
        priv = NMA_BLING_SPINNER_GET_PRIVATE (spinner);
289
 
        if (priv->timeout != 0)
290
 
                return;
291
 
        priv->timeout = g_timeout_add (80, nma_bling_spinner_timeout, spinner);
292
 
}
293
 
 
294
 
/**
295
 
 * nma_bling_spinner_stop
296
 
 *
297
 
 * Stops the animation
298
 
 */
299
 
void
300
 
nma_bling_spinner_stop (NmaBlingSpinner *spinner)
301
 
{
302
 
        NmaBlingSpinnerPrivate *priv;
303
 
 
304
 
        g_return_if_fail (NMA_IS_BLING_SPINNER (spinner));
305
 
 
306
 
        priv = NMA_BLING_SPINNER_GET_PRIVATE (spinner);
307
 
        if (priv->timeout == 0)
308
 
                return;
309
 
        g_source_remove (priv->timeout);
310
 
        priv->timeout = 0;
311
 
}