~timchen119/ubuntu/trusty/gnome-bluetooth/lp1035431

« back to all changes in this revision

Viewing changes to lib/bling-spinner.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2011-02-27 15:45:22 UTC
  • mfrom: (1.3.2 upstream)
  • mto: (2.2.3 experimental) (1.5.1)
  • mto: This revision was merged to the branch mainline in revision 53.
  • Revision ID: james.westby@ubuntu.com-20110227154522-dnnoqasv5v3mv42a
Tags: upstream-2.91.5
ImportĀ upstreamĀ versionĀ 2.91.5

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 "bling-spinner.h"
34
 
 
35
 
#define BLING_SPINNER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), BLING_TYPE_SPINNER, BlingSpinnerPrivate))
36
 
 
37
 
G_DEFINE_TYPE (BlingSpinner, bling_spinner, GTK_TYPE_DRAWING_AREA);
38
 
 
39
 
enum
40
 
{
41
 
        PROP_0,
42
 
        PROP_NUM_LINES
43
 
};
44
 
 
45
 
/* STRUCTS & ENUMS */
46
 
struct _BlingSpinnerPrivate
47
 
{
48
 
        /* state */
49
 
        guint current;
50
 
        guint timeout;
51
 
 
52
 
        /* appearance */
53
 
        guint lines;
54
 
};
55
 
 
56
 
/* FORWARDS */
57
 
static void bling_spinner_class_init(BlingSpinnerClass *klass);
58
 
static void bling_spinner_init(BlingSpinner *spinner);
59
 
static void bling_spinner_finalize (GObject *gobject);
60
 
static void bling_spinner_set_property(GObject *gobject, guint prop_id, const GValue *value, GParamSpec *pspec);
61
 
static gboolean bling_spinner_expose(GtkWidget *widget, GdkEventExpose *event);
62
 
static void 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
 
        double x, y;
71
 
        double radius;
72
 
        double half;
73
 
        int i;
74
 
        int width, height;
75
 
 
76
 
        BlingSpinnerPrivate *priv;
77
 
 
78
 
        priv = BLING_SPINNER_GET_PRIVATE (widget);
79
 
 
80
 
        cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
81
 
 
82
 
        width = widget->allocation.width;
83
 
        height = widget->allocation.height;
84
 
 
85
 
        if ( (width < 12) || (height <12) )
86
 
                gtk_widget_set_size_request(widget, 12, 12);
87
 
 
88
 
        //x = widget->allocation.x + widget->allocation.width / 2;
89
 
        //y = widget->allocation.y + widget->allocation.height / 2;
90
 
        x = widget->allocation.width / 2;
91
 
        y = widget->allocation.height / 2;
92
 
        radius = MIN (widget->allocation.width  / 2,
93
 
                                  widget->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
 
bling_spinner_class_init(BlingSpinnerClass *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 (BlingSpinnerPrivate));
133
 
        gobject_class->set_property = bling_spinner_set_property;
134
 
        gobject_class->finalize = bling_spinner_finalize;
135
 
 
136
 
        widget_class = GTK_WIDGET_CLASS(klass);
137
 
        widget_class->expose_event = bling_spinner_expose;
138
 
        widget_class->screen_changed = 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
 
bling_spinner_init (BlingSpinner *spinner)
150
 
{
151
 
        BlingSpinnerPrivate *priv;
152
 
 
153
 
        priv = BLING_SPINNER_GET_PRIVATE (spinner);
154
 
        priv->current = 0;
155
 
        priv->timeout = 0;
156
 
 
157
 
        GTK_WIDGET_SET_FLAGS (GTK_WIDGET (spinner), GTK_NO_WINDOW);
158
 
}
159
 
 
160
 
static gboolean
161
 
bling_spinner_expose (GtkWidget *widget, GdkEventExpose *event)
162
 
{
163
 
        cairo_t *cr;
164
 
 
165
 
        /* get cairo context */
166
 
        cr = gdk_cairo_create (gtk_widget_get_window (widget));
167
 
 
168
 
        /* set a clip region for the expose event */
169
 
        cairo_rectangle (cr,
170
 
                                         event->area.x, event->area.y,
171
 
                                         event->area.width, event->area.height);
172
 
        cairo_clip (cr);
173
 
 
174
 
        cairo_translate (cr, event->area.x, event->area.y);
175
 
 
176
 
        /* draw clip region */
177
 
        draw (widget, cr);
178
 
 
179
 
        /* free memory */
180
 
        cairo_destroy (cr);
181
 
 
182
 
        return FALSE;
183
 
}
184
 
 
185
 
static void
186
 
bling_spinner_screen_changed (GtkWidget* widget, GdkScreen* old_screen)
187
 
{
188
 
        BlingSpinner *spinner;
189
 
        GdkScreen* new_screen;
190
 
        GdkColormap* colormap;
191
 
 
192
 
        spinner = BLING_SPINNER(widget);
193
 
 
194
 
        new_screen = gtk_widget_get_screen (widget);
195
 
        colormap = gdk_screen_get_rgba_colormap (new_screen);
196
 
 
197
 
        if (!colormap)
198
 
                colormap = gdk_screen_get_rgb_colormap (new_screen);
199
 
 
200
 
        gtk_widget_set_colormap (widget, colormap);
201
 
}
202
 
 
203
 
static gboolean
204
 
bling_spinner_timeout (gpointer data)
205
 
{
206
 
        BlingSpinner *spinner;
207
 
        BlingSpinnerPrivate *priv;
208
 
 
209
 
        spinner = BLING_SPINNER (data);
210
 
        priv = BLING_SPINNER_GET_PRIVATE (spinner);
211
 
 
212
 
        if (priv->current + 1 >= priv->lines) {
213
 
                priv->current = 0;
214
 
        } else {
215
 
                priv->current++;
216
 
        }
217
 
 
218
 
        gtk_widget_queue_draw (GTK_WIDGET (data));
219
 
 
220
 
        return TRUE;
221
 
}
222
 
 
223
 
static void
224
 
bling_spinner_set_property(GObject *gobject, guint prop_id,
225
 
                                        const GValue *value, GParamSpec *pspec)
226
 
{
227
 
        BlingSpinner *spinner;
228
 
        BlingSpinnerPrivate *priv;
229
 
 
230
 
        spinner = BLING_SPINNER(gobject);
231
 
        priv = BLING_SPINNER_GET_PRIVATE (spinner);
232
 
 
233
 
        switch (prop_id)
234
 
        {
235
 
                case PROP_NUM_LINES:
236
 
                        priv->lines = g_value_get_uint(value);
237
 
                        break;
238
 
                default:
239
 
                        G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
240
 
                        break;
241
 
        }
242
 
}
243
 
 
244
 
static void
245
 
bling_spinner_finalize (GObject *gobject)
246
 
{
247
 
        BlingSpinner *spinner;
248
 
        BlingSpinnerPrivate *priv;
249
 
 
250
 
        spinner = BLING_SPINNER(gobject);
251
 
        priv = BLING_SPINNER_GET_PRIVATE (spinner);
252
 
 
253
 
        if (priv->timeout != 0) {
254
 
                g_source_remove (priv->timeout);
255
 
                priv->timeout = 0;
256
 
        }
257
 
}
258
 
 
259
 
/**
260
 
 * bling_spinner_new
261
 
 *
262
 
 * Returns a default spinner. Not yet started.
263
 
 *
264
 
 * Returns: a new #BlingSpinner
265
 
 */
266
 
GtkWidget *
267
 
bling_spinner_new (void)
268
 
{
269
 
        return g_object_new (BLING_TYPE_SPINNER, NULL);
270
 
}
271
 
 
272
 
/**
273
 
 * bling_spinner_start
274
 
 *
275
 
 * Starts the animation
276
 
 */
277
 
void
278
 
bling_spinner_start (BlingSpinner *spinner)
279
 
{
280
 
        BlingSpinnerPrivate *priv;
281
 
 
282
 
        g_return_if_fail (BLING_IS_SPINNER (spinner));
283
 
 
284
 
        priv = BLING_SPINNER_GET_PRIVATE (spinner);
285
 
        if (priv->timeout != 0)
286
 
                return;
287
 
        priv->timeout = g_timeout_add (80, bling_spinner_timeout, spinner);
288
 
}
289
 
 
290
 
/**
291
 
 * bling_spinner_stop
292
 
 *
293
 
 * Stops the animation
294
 
 */
295
 
void
296
 
bling_spinner_stop (BlingSpinner *spinner)
297
 
{
298
 
        BlingSpinnerPrivate *priv;
299
 
 
300
 
        g_return_if_fail (BLING_IS_SPINNER (spinner));
301
 
 
302
 
        priv = BLING_SPINNER_GET_PRIVATE (spinner);
303
 
        if (priv->timeout == 0)
304
 
                return;
305
 
        g_source_remove (priv->timeout);
306
 
        priv->timeout = 0;
307
 
}