1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*
* (C) Copyright 2007 Bastien Nocera <hadess@hadess.net>
*
* Glow code from libwnck/libwnck/tasklist.c:
* Copyright © 2001 Havoc Pennington
* Copyright © 2003 Kim Woelders
* Copyright © 2003 Red Hat, Inc.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <gtk/gtk.h>
#include "totem-glow-button.h"
#define FADE_OPACITY_DEFAULT 0.6
#define ENTER_SPEEDUP_RATIO 0.4
#define FADE_MAX_LOOPS 4
struct _TotemGlowButton {
GtkButton parent;
gdouble glow_start_time;
gdouble glow_factor;
guint button_glow;
guint glow : 1;
guint anim_enabled : 1;
guint pointer_entered : 1;
/* Set when we don't want to play animation
* anymore in pointer entered mode */
guint anim_finished :1;
};
static void totem_glow_button_set_timeout (TotemGlowButton *button, gboolean set_timeout);
static GtkButtonClass *parent_class;
G_DEFINE_TYPE (TotemGlowButton, totem_glow_button, GTK_TYPE_BUTTON);
static gboolean
totem_glow_button_glow (TotemGlowButton *button)
{
GtkWidget *buttonw;
GTimeVal tv;
gdouble now;
gfloat fade_opacity, loop_time;
buttonw = GTK_WIDGET (button);
if (gtk_widget_get_realized (buttonw) == FALSE)
return TRUE;
if (button->anim_enabled != FALSE) {
g_get_current_time (&tv);
now = (tv.tv_sec * (1.0 * G_USEC_PER_SEC) +
tv.tv_usec) / G_USEC_PER_SEC;
/* Hard-coded values */
fade_opacity = FADE_OPACITY_DEFAULT;
loop_time = 3.0;
if (button->glow_start_time <= G_MINDOUBLE) {
button->glow_start_time = now;
/* If the pointer entered, we want to start with 'dark' */
if (button->pointer_entered != FALSE) {
button->glow_start_time -= loop_time / 4.0;
}
}
/* Timing for mouse hover animation
[light]......[dark]......[light]......[dark]...
{mouse hover event}
[dark]..[light]..[dark]..[light]
{mouse leave event}
[light]......[dark]......[light]......[dark]
*/
if (button->pointer_entered != FALSE) {
/* pointer entered animation should be twice as fast */
loop_time *= ENTER_SPEEDUP_RATIO;
}
if ((now - button->glow_start_time) > loop_time * FADE_MAX_LOOPS) {
button->anim_finished = TRUE;
button->glow_factor = FADE_OPACITY_DEFAULT * 0.5;
} else {
button->glow_factor = fade_opacity * (0.5 - 0.5 * cos ((now - button->glow_start_time) * M_PI * 2.0 / loop_time));
}
} else {
button->glow_factor = FADE_OPACITY_DEFAULT * 0.5;
}
gtk_widget_queue_draw (GTK_WIDGET (button));
if (button->anim_finished != FALSE)
totem_glow_button_set_timeout (button, FALSE);
return button->anim_enabled;
}
static void
totem_glow_button_clear_glow_start_timeout_id (TotemGlowButton *button)
{
button->button_glow = 0;
}
static gboolean
totem_glow_button_draw (GtkWidget *widget,
cairo_t *cr,
gpointer user_data)
{
TotemGlowButton *button;
GtkStyleContext *context;
GtkAllocation allocation, child_allocation;
gint width, height;
GtkWidget *child;
GdkRGBA acolor;
button = TOTEM_GLOW_BUTTON (widget);
if (button->glow_factor == 0.0)
return FALSE;
context = gtk_widget_get_style_context (widget);
/* push a translucent overlay to paint to, so we can blend later */
cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
gtk_widget_get_allocation (widget, &allocation);
width = allocation.width;
height = allocation.height;
/* Draw a rectangle with bg[SELECTED] */
gtk_style_context_save (context);
gtk_style_context_add_class (context, "button");
gtk_style_context_set_state (context, GTK_STATE_SELECTED);
gtk_style_context_get_background_color (context, GTK_STATE_SELECTED, &acolor);
gdk_cairo_set_source_rgba (cr, &acolor);
gtk_render_background (context, cr, 0, 0, width, height);
gtk_style_context_restore (context);
/* then the image */
cairo_save (cr);
child = gtk_bin_get_child (GTK_BIN (button));
gtk_widget_get_allocation (child, &child_allocation);
cairo_translate (cr,
child_allocation.x - allocation.x,
child_allocation.y - allocation.y);
gtk_widget_draw (gtk_bin_get_child (GTK_BIN (button)), cr);
cairo_restore (cr);
/* finally blend it */
cairo_pop_group_to_source (cr);
cairo_paint_with_alpha (cr, button->glow_factor);
return FALSE;
}
static void
totem_glow_button_map (GtkWidget *buttonw)
{
TotemGlowButton *button;
(* GTK_WIDGET_CLASS (parent_class)->map) (buttonw);
button = TOTEM_GLOW_BUTTON (buttonw);
if (button->glow != FALSE && button->button_glow == 0) {
totem_glow_button_set_glow (button, TRUE);
}
}
static void
totem_glow_button_unmap (GtkWidget *buttonw)
{
TotemGlowButton *button;
button = TOTEM_GLOW_BUTTON (buttonw);
if (button->button_glow > 0) {
g_source_remove (button->button_glow);
button->button_glow = 0;
}
(* GTK_WIDGET_CLASS (parent_class)->unmap) (buttonw);
}
static void
totem_glow_button_enter (GtkButton *buttonw)
{
TotemGlowButton *button;
button = TOTEM_GLOW_BUTTON (buttonw);
(* GTK_BUTTON_CLASS (parent_class)->enter) (buttonw);
button->pointer_entered = TRUE;
button->anim_finished = FALSE;
button->glow_start_time = G_MINDOUBLE;
totem_glow_button_set_timeout (button, FALSE);
}
static void
totem_glow_button_leave (GtkButton *buttonw)
{
TotemGlowButton *button;
button = TOTEM_GLOW_BUTTON (buttonw);
(* GTK_BUTTON_CLASS (parent_class)->leave) (buttonw);
button->pointer_entered = FALSE;
button->glow_start_time = G_MINDOUBLE;
button->anim_finished = FALSE;
if (button->glow != FALSE)
totem_glow_button_set_timeout (button, TRUE);
}
static void
totem_glow_button_finalize (GObject *object)
{
TotemGlowButton *button = TOTEM_GLOW_BUTTON (object);
totem_glow_button_set_glow (button, FALSE);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
totem_glow_button_class_init (TotemGlowButtonClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = totem_glow_button_finalize;
/* Note that we don't use a draw here because we
* want to modify what the button will draw by itself */
widget_class->map = totem_glow_button_map;
widget_class->unmap = totem_glow_button_unmap;
button_class->enter = totem_glow_button_enter;
button_class->leave = totem_glow_button_leave;
}
static void
totem_glow_button_init (TotemGlowButton *button)
{
button->glow_start_time = 0.0;
button->button_glow = 0;
button->glow_factor = 0.0;
g_signal_connect_object (button, "draw",
G_CALLBACK (totem_glow_button_draw),
G_OBJECT (button),
G_CONNECT_AFTER);
}
GtkWidget *
totem_glow_button_new (void)
{
return g_object_new (TOTEM_TYPE_GLOW_BUTTON, NULL);
}
/* We can only add a timeout once, we assert that, though
* calling it multiple times to disable the animation is fine */
static void
totem_glow_button_set_timeout (TotemGlowButton *button, gboolean set_timeout)
{
if (set_timeout != FALSE) {
if (button->button_glow > 0)
return;
button->glow_start_time = 0.0;
/* The animation doesn't speed up or slow down based on the
* timeout value, but instead will just appear smoother or
* choppier.
*/
button->button_glow =
g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE,
100,
(GSourceFunc) totem_glow_button_glow, button,
(GDestroyNotify) totem_glow_button_clear_glow_start_timeout_id);
} else {
if (button->button_glow > 0) {
g_source_remove (button->button_glow);
button->button_glow = 0;
}
button->glow_factor = 0.0;
gtk_widget_queue_draw (GTK_WIDGET (button));
}
}
void
totem_glow_button_set_glow (TotemGlowButton *button, gboolean glow)
{
GtkSettings *settings;
gboolean anim_enabled;
g_return_if_fail (TOTEM_IS_GLOW_BUTTON (button));
if (gtk_widget_get_mapped (GTK_WIDGET (button)) == FALSE
&& glow != FALSE) {
button->glow = glow;
return;
}
settings = gtk_settings_get_for_screen
(gtk_widget_get_screen (GTK_WIDGET (button)));
g_object_get (G_OBJECT (settings),
"gtk-enable-animations", &anim_enabled,
NULL);
button->anim_enabled = anim_enabled;
if (glow == FALSE && button->button_glow == 0 && button->anim_enabled != FALSE)
return;
if (glow != FALSE && button->button_glow != 0)
return;
button->glow = glow;
totem_glow_button_set_timeout (button, glow);
}
gboolean
totem_glow_button_get_glow (TotemGlowButton *button)
{
return button->glow != FALSE;
}
|