aboutsummaryrefslogtreecommitdiff
path: root/src/play-button.c
blob: e798668ff8934b95c67416cb7464c90a2716190d (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
/*
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
{
	
};

#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, gpointer userdata);
static void draw (GtkWidget* button, cairo_t *cr);
                                          
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");	
	g_signal_connect(GTK_WIDGET(self), "expose-event", G_CALLBACK (play_button_expose), NULL);
}

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, gpointer userdata)
{
	GtkAllocation alloc;

	alloc.width = 200;
	alloc.height = 600;
	alloc.x = 10;
	alloc.y = 10;
	
	gtk_widget_set_allocation(GTK_WIDGET(button), 
	                          &alloc);

	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;
}

static void
draw (GtkWidget* button, cairo_t *cr)
{
	double x, y;
	double radius;
	int i;
	
	x = button->allocation.x + button->allocation.width / 2;
	y = button->allocation.y + button->allocation.height / 2;
	radius = MIN (button->allocation.width / 2,
		      button->allocation.height / 2) - 5;

	/* button back */
	cairo_arc (cr, x, y, radius, 0, 2 * G_PI);
	cairo_set_source_rgb (cr, 1, 1, 1);
	cairo_fill_preserve (cr);
	cairo_set_source_rgb (cr, 0, 0, 0);
	cairo_stroke (cr);

	/* button ticks */
	for (i = 0; i < 12; i++)
	{
		int inset;
	
		cairo_save (cr); /* stack-pen-size */
		
		if (i % 3 == 0)
		{
			inset = 0.2 * radius;
		}
		else
		{
			inset = 0.1 * radius;
			cairo_set_line_width (cr, 0.5 *
					cairo_get_line_width (cr));
		}
		
		cairo_move_to (cr,
				x + (radius - inset) * cos (i * G_PI / 6),
				y + (radius - inset) * sin (i * G_PI / 6));
		cairo_line_to (cr,
				x + radius * cos (i * G_PI / 6),
				y + radius * sin (i * G_PI / 6));
		cairo_stroke (cr);
		cairo_restore (cr); /* stack-pen-size */
	}
	 cairo_surface_write_to_png(cairo_get_target (cr), "/tmp/foobar.png");	
}

/**
* 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;
}