aboutsummaryrefslogtreecommitdiff
path: root/src/play-button.c
blob: 76a81094e067cc2f9ec3a7758c63ae6f9617a85f (plain)
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
/*
Copyright 2010 Canonical Ltd.

Authors:
    Conor Curran <conor.curran@canonical.com>

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License version 3, as published 
by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranties of 
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>
#include "play-button.h"


typedef struct _PlayButtonPrivate PlayButtonPrivate;

struct _PlayButtonPrivate
{
	GdkColor 	background_colour_fg;
	GdkColor 	background_colour_bg_dark;
	GdkColor 	background_colour_bg_light;
	GdkColor 	foreground_colour_fg;
	GdkColor 	foreground_colour_bg;
};

#define PLAY_BUTTON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PLAY_BUTTON_TYPE, PlayButtonPrivate))

/* Gobject boiler plate */
static void play_button_class_init (PlayButtonClass *klass);
static void play_button_init       (PlayButton *self);
static void play_button_dispose    (GObject *object);
static void play_button_finalize   (GObject *object);

static gboolean play_button_expose (GtkWidget *button, GdkEventExpose *event);
static void draw (GtkWidget* button, cairo_t *cr);
static void play_button_draw_background(GtkWidget* button, cairo_t* cr, double x, double y, double width, double height, double p_radius);
static void play_button_draw_background_shadow_2(GtkWidget* button, cairo_t* cr, double x, double y, double rect_width, double rect_height,  double p_radius);
static void play_button_draw_background_shadow_1(GtkWidget* button, cairo_t* cr, double x, double y, double rect_width, double rect_height, double p_radius);


//static void play_button_draw_play_symbol(cairo_t* cr, double x, double y);
static void play_button_draw_pause_symbol(cairo_t* cr, double x, double y);
static void play_button_draw_previous_symbol(cairo_t* cr, double x, double y);


G_DEFINE_TYPE (PlayButton, play_button, GTK_TYPE_DRAWING_AREA);


static void
play_button_class_init (PlayButtonClass *klass)
{
	
	GObjectClass	*gobject_class = G_OBJECT_CLASS (klass);
 	GtkWidgetClass* widget_class = GTK_WIDGET_CLASS (klass);

	g_type_class_add_private (klass, sizeof (PlayButtonPrivate));

 	widget_class->expose_event = play_button_expose;

	gobject_class->dispose = play_button_dispose;
	gobject_class->finalize = play_button_finalize;
}

static void
play_button_init (PlayButton *self)
{
	g_debug("PlayButton::play_button_init");	
	gtk_widget_set_size_request(GTK_WIDGET(self), 200, 80); 
}

static void
play_button_dispose (GObject *object)
{
	G_OBJECT_CLASS (play_button_parent_class)->dispose (object);
}

static void
play_button_finalize (GObject *object)
{
	G_OBJECT_CLASS (play_button_parent_class)->finalize (object);
}

static gboolean
play_button_expose (GtkWidget *button, GdkEventExpose *event)
{
	cairo_t *cr;
  cr = gdk_cairo_create (button->window);

	g_debug("PlayButton::Draw - width = %i", button->allocation.width);
	g_debug("PlayButton::Draw - event->area.width = %i", event->area.width);
	g_debug("PlayButton::Draw - event->area.x = %i", event->area.x);

	cairo_rectangle (cr,
                   event->area.x, event->area.y,
                   event->area.width, event->area.height);

	cairo_clip(cr);
	draw (button, cr);
  cairo_destroy (cr);
	return FALSE;
}


void
play_button_set_style(GtkWidget* button, GtkStyle* style)
{
	PlayButtonPrivate* priv = PLAY_BUTTON_GET_PRIVATE(button);
	priv->background_colour_fg = style->fg[GTK_STATE_NORMAL];
	priv->background_colour_bg_dark = style->bg[GTK_STATE_NORMAL];
	priv->background_colour_bg_light = style->base[GTK_STATE_NORMAL];
	priv->foreground_colour_fg = style->fg[GTK_STATE_PRELIGHT];
	priv->foreground_colour_bg = style->bg[GTK_STATE_NORMAL];
}

static void
draw (GtkWidget* button, cairo_t *cr)
{

	//PlayButtonPrivate* priv = PLAY_BUTTON_GET_PRIVATE(button);
	double rect_width = 115;
	double rect_height = 28;
	double p_radius = 21;
	double y = 15;
	double x = 22;
	//double radius=35;
	//double x= button->allocation.width/2 - rect_width/2;
	//double y= button->allocation.height/2 -rect_height/2;

	// Draw the outside drop shadow background
	play_button_draw_background_shadow_1(button, cr, x, y, rect_width, rect_height, p_radius);
	
	// Draw the inside drop shadow background
	gint offset = 4;
	play_button_draw_background_shadow_2(button, cr, x + offset-1, y + offset/2, rect_width-offset, rect_height-offset, p_radius-(offset/2));

	offset = 5;
	// Draw the inside actual background
	play_button_draw_background(button, cr, x+offset-1, y + offset/2+1, rect_width-offset, rect_height-offset, p_radius-offset/2);

	play_button_draw_pause_symbol(cr, rect_width/2 + rect_height/10 + x -1 + offset/2, rect_height/5 +y );

	play_button_draw_pause_symbol(cr, rect_width/2 + rect_height/10 + x + offset/2 + 10, rect_height/5 +y );
	play_button_draw_previous_symbol(cr, x+rect_height/2 + offset, y + offset+2);
	cairo_surface_write_to_png(cairo_get_target (cr), "/tmp/foobar.png");	
}

static void 
play_button_draw_previous_symbol(cairo_t* cr, double x, double y)
{
	gint line_width = 2;
	cairo_set_line_width (cr, line_width);
	gint shape_height = 15;
	gint shape_width = 22;
	gint single_arrow_width=16;

	cairo_pattern_t *pat;
	pat = cairo_pattern_create_linear (x, y,  x, y+shape_height);

	cairo_pattern_add_color_stop_rgb(pat, 0, 227/255.0, 222/255.0, 214/255.0);
	cairo_pattern_add_color_stop_rgb(pat, .1, 207/255.0, 201/255.0, 190/255.0);
	cairo_pattern_add_color_stop_rgb(pat, .4, 123/255.0, 123/255.0, 120/255.0);
	cairo_set_source (cr, pat);	

	cairo_move_to(cr, x, y);
	cairo_rel_line_to (cr, 0, shape_height);
	cairo_stroke(cr);
	
	cairo_move_to (cr, x+line_width, y+shape_height/2);	
	cairo_line_to (cr, x+single_arrow_width, y);
	cairo_line_to (cr, x+single_arrow_width, y+shape_height);
	cairo_line_to (cr, x+line_width, y+shape_height/2);
	cairo_fill(cr);
	
	cairo_pattern_destroy (pat);
	cairo_close_path(cr);		
}

static void 
play_button_draw_pause_symbol(cairo_t* cr, double x, double y)
{
	cairo_set_line_width (cr, 7);
	cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
	cairo_move_to (cr, x, y);
	cairo_rel_line_to (cr, 0, 18);
	//cairo_set_source_rgb(cr, 94/255.0, 93/255.0, 90/255.0);

	cairo_pattern_t *pat;
	pat = cairo_pattern_create_linear (x, y,  x, y+16);
	cairo_pattern_add_color_stop_rgb(pat, 0, 227/255.0, 222/255.0, 214/255.0);
	cairo_pattern_add_color_stop_rgb(pat, .1, 207/255.0, 201/255.0, 190/255.0);
	cairo_pattern_add_color_stop_rgb(pat, .6, 123/255.0, 123/255.0, 120/255.0);
	cairo_set_source (cr, pat);	
	cairo_stroke(cr);
	cairo_close_path(cr);

	cairo_set_line_width (cr, 5.5);
	cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
	cairo_move_to (cr, x, y+0.75);
	cairo_rel_line_to (cr, 0, 16.5);

	pat = cairo_pattern_create_linear (x+1, y+1,  x+1, y+16);
	cairo_pattern_add_color_stop_rgb(pat, .3, 252/255.0, 252/255.0, 251/255.0);
	cairo_pattern_add_color_stop_rgb(pat, .8, 227/255.0, 222/255.0, 214/255.0);
	//cairo_pattern_add_color_stop_rgb(pat, .9, 207/255.0, 201/255.0, 190/255.0);
	cairo_set_source (cr, pat);	
	cairo_stroke(cr);
	cairo_close_path(cr);
	
	cairo_pattern_destroy (pat);
}

static void
play_button_draw_background(GtkWidget* button, cairo_t* cr, double x, double y, double rect_width, double rect_height, double p_radius)
{
	double radius=rect_height/2;
	cairo_set_line_width (cr, rect_height);
	cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
	cairo_move_to(cr, x+radius, y+radius);
	cairo_line_to(cr, x+rect_width, y+radius);

	cairo_pattern_t *pat;
	pat = cairo_pattern_create_linear (x, y,  x, y+rect_height);
	cairo_pattern_add_color_stop_rgb(pat, .5, 225/255.0, 218/255.0, 211/255.0);
	cairo_pattern_add_color_stop_rgb(pat, .7, 197/255.0, 192/255.0, 185/255.0);
	cairo_pattern_add_color_stop_rgb(pat, .9, 185/255.0, 179/255.0, 173/255.0);
	cairo_set_source (cr, pat);
	cairo_stroke (cr);

	cairo_close_path(cr);
	cairo_arc(cr, rect_width/2 + radius/2 + x + 2, rect_height/2 +y, p_radius, 0, 2*M_PI);
	cairo_set_source (cr, pat);	
	cairo_fill(cr);

	cairo_close_path(cr);
	cairo_arc(cr, rect_width/2 + radius/2 + x + 2, rect_height/2 +y, p_radius, 0, 2*M_PI);
	cairo_set_source_rgb(cr, 94/255.0, 93/255.0, 90/255.0);
	cairo_set_line_width (cr, 0.75);	
	cairo_stroke(cr);
	cairo_close_path(cr);	
	cairo_pattern_destroy (pat);
}

static void
play_button_draw_background_shadow_1(GtkWidget* button, cairo_t* cr, double x, double y, double rect_width, double rect_height, double p_radius)
{
	double radius=rect_height/2;

	cairo_set_line_width (cr, rect_height);
	cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
	cairo_move_to(cr, x+radius, y+radius);
	cairo_line_to(cr, x+rect_width, y+radius);

	cairo_pattern_t *pat;
	pat = cairo_pattern_create_linear (x, y,  x, y+rect_height);
	cairo_pattern_add_color_stop_rgb(pat, .4, 36/255.0, 35/255.0, 33/255.0);
	cairo_pattern_add_color_stop_rgb(pat, .7, 123/255.0, 123/255.0, 120/255.0);
	cairo_set_source (cr, pat);
	cairo_stroke (cr);

	cairo_close_path(cr);
	cairo_arc(cr, rect_width/2 + radius/2 + x + 2, rect_height/2 +y, p_radius, 0, 2*M_PI);
	pat = cairo_pattern_create_linear ((rect_width/2 + radius/2 + x),  rect_height/2 + y-p_radius, (rect_width/2 + radius/2 + x), rect_height+(p_radius-rect_height/2));
	cairo_pattern_add_color_stop_rgb(pat, .4, 36/255.0, 35/255.0, 33/255.0);
	cairo_pattern_add_color_stop_rgb(pat, .7, 123/255.0, 123/255.0, 120/255.0);
	cairo_set_source (cr, pat);	
	cairo_fill(cr);
	cairo_close_path(cr);
	cairo_pattern_destroy (pat);
	
}

static void
play_button_draw_background_shadow_2(GtkWidget* button, cairo_t* cr, double x, double y, double rect_width, double rect_height, double p_radius)
{
	double radius=rect_height/2;
	
	cairo_set_line_width (cr, rect_height);
	cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
	cairo_move_to(cr, x+radius, y+radius);
	cairo_line_to(cr, x+rect_width, y+radius);

	cairo_pattern_t *pat;
	pat = cairo_pattern_create_linear (x, y,  x, y+rect_height);
	cairo_pattern_add_color_stop_rgb(pat, .4, 61/255.0, 60/255.0, 57/255.0);
	cairo_pattern_add_color_stop_rgb(pat, .7, 94/255.0, 93/255.0, 90/255.0);
	cairo_set_source (cr, pat);
	cairo_stroke (cr);
	
	cairo_close_path(cr);
	cairo_arc(cr, rect_width/2 + radius/2 + x + 2, rect_height/2 +y, p_radius, 0, 2*M_PI);
	pat = cairo_pattern_create_linear ((rect_width/2 + radius/2 + x),  rect_height/2 + y-p_radius, (rect_width/2 + radius/2 + x), rect_height+(p_radius-rect_height/2));
	cairo_pattern_add_color_stop_rgb(pat, .4, 61/255.0, 60/255.0, 57/255.0);
	cairo_pattern_add_color_stop_rgb(pat, .7, 94/255.0, 93/255.0, 90/255.0);
	
	cairo_set_source (cr, pat);	
	cairo_fill(cr);
	cairo_close_path(cr);	
	cairo_pattern_destroy (pat);
	
}




/**
* play_button_new:
* @returns: a new #PlayButton.
**/
GtkWidget* 
play_button_new()
{
	
	GtkWidget* widget =	g_object_new(PLAY_BUTTON_TYPE, NULL);
	gtk_widget_set_app_paintable (widget, TRUE);	
	return widget;
}