summaryrefslogtreecommitdiff
path: root/cairo-bclock.c
blob: 36fe62ccb0aab3c8a4fbf266517e0160e22afdf6 (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
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <cairo.h>
#include <math.h>

#include <unistd.h>
#include <time.h>

#define SECOND_INTERVAL  1000

#define DEFAULT_WIDTH  200
#define DEFAULT_HEIGHT 100


typedef struct
{
	gboolean active;   /* whether the selection is active or not */
	gdouble  x, y;
} selection_t;

typedef struct 
{
	float r;
	float g;
	float b;
	float a;
} color_t;


static gboolean time_handler (GtkWidget* w)
{
	if (GTK_IS_WIDGET(w)) {
		gtk_widget_queue_draw (w);
		return TRUE;
	} else {
		_exit(-1);
		return FALSE;
	}
}

gboolean on_key_press (GtkWidget *widget, 
		       GdkEventKey *key, 
		       gpointer p) {
	if (key->type == GDK_KEY_PRESS)
	{
		switch (key->keyval)
		{
			case GDK_Escape :
				gtk_main_quit ();
			break;
		}
	}

	return FALSE;
}

gboolean on_mouse_press (GtkWidget      *widget,
			 GdkEventButton *bev,
			 selection_t    *sel) {	
	sel->active = TRUE;

	sel->x = bev->x;	
	sel->y = bev->y;

	gtk_widget_queue_draw (widget);
	
	return TRUE;
}

gboolean on_mouse_release (GtkWidget      *widget,
			   GdkEventButton *bev,
			   selection_t    *sel) {

	sel->active = FALSE;

	sel->x = 0;
	sel->y = 0;
	
	gtk_widget_queue_draw (widget);

	return TRUE;
}

gboolean on_mouse_motion (GtkWidget      *widget,
			  GdkEventButton *bev,
			  selection_t    *sel) {
	sel->x = bev->x;
	sel->y = bev->y;

	gtk_widget_queue_draw (widget);

	return TRUE;
}

static void paint_dot (cairo_t *cr, selection_t *sel) {
	if (!sel->active)
		return;

	cairo_save (cr);
	  cairo_arc (cr, sel->x, sel->y, 30., 0., 2 * M_PI);
	  cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
	  cairo_fill_preserve (cr);
	  
	  cairo_set_source_rgba (cr, 0, 0, 0, 0.5);
	  cairo_stroke (cr);	      
        cairo_restore (cr);

}

void paint_rounded_box (cairo_t *cr, float   x, float   y, 
			             float   w, float   h, 
			             color_t *o, color_t *f, 
			             float c) 
{
	gint t, i;

	cairo_save (cr);
	  cairo_translate (cr, x, y);
	  if (f) cairo_set_source_rgba (cr, f->r, f->g, f->b, f->a);       
	  cairo_move_to (cr, - w/2, - h/2 + c);
	  
	  for ( i = 0; i < 4; i++ ) {		
		  cairo_translate (cr, -w/2.0, -h/2.0);
		  
		  cairo_curve_to (cr,  0.0, c, 0.0, 0.0, c, 0.0);
		  cairo_line_to  (cr,  w - c, 0.0);
		  
		  cairo_translate (cr, w/2.0, h/2.0);
		  cairo_rotate (cr, M_PI/2.0);
		  t = w; w = h; h = t;
	  }
	  
	  if (f) cairo_fill_preserve (cr);
	  cairo_set_source_rgba (cr, o->r, o->g, o->b, o->a);
	  cairo_stroke (cr);	
	cairo_restore (cr);
}

void paint_rect (cairo_t *cr,  float    x, float   y, 
			       float    w, float   h, 
		               color_t *o, color_t *f)
{
	cairo_save (cr);
  	  cairo_translate (cr, x, y);
	  if (f) cairo_set_source_rgba (cr, f->r, f->g, f->b, f->a);  
	  cairo_rectangle (cr, -w/2, -h/2, w, h);
	  if (f) cairo_fill_preserve (cr);
	  cairo_set_source_rgba (cr, o->r, o->g, o->b, o->a);
	  cairo_stroke (cr);		 
	cairo_restore (cr);
}


void paint_clock_dot (cairo_t *cr, float x, float y, 
		                   float w, float h,
		                   color_t *c) 
{
	color_t back_o = { 0.0f, 0.0f, 0.0f, 1.0f };

	/*	paint_rounded_box (cr, x, y, w, h, &back_o, c, 5);*/
	paint_rect (cr, x, y, w, h, &back_o, c);
}

static void paint (GtkWidget *widget, GdkEventExpose *eev, selection_t *sel) {
	gint width, height;
	cairo_t *cr;
	gint i, j;

        time_t now;
        struct tm *tm;

	int timet[3];

	color_t color[]   = {{ 1.0f, 0.0f, 0.0f, 0.6f },
			     { 0.0f, 1.0f, 0.0f, 0.6f },
			     { 0.0f, 0.0f, 1.0f, 0.6f }};

        now = time(NULL);
        tm = localtime(&now);

	timet[0] = tm->tm_hour;
	timet[1] = tm->tm_min;
	timet[2] = tm->tm_sec;
 		
	width  = widget->allocation.width;
	height = widget->allocation.height;

	cr = gdk_cairo_create (widget->window);
	cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);	

	cairo_set_source_rgba (cr, 1.0f, 1.0f, 1.0f, 0.4f);	

	cairo_paint (cr);

	for (i = 0; i < 3; i++) {
		for (j = 0; j < 6; j++) {				
			paint_clock_dot (cr, (width/7)*(6-j) , (height/4)*(i+1), 
					 (width/8), (height/5), 
					 (((timet[i])>>j)&0x1)?&color[i]:NULL);
		}
	}

	paint_dot (cr, sel);
	
	cairo_destroy (cr);
}


static void install_colormap (GtkWidget *w) {
	GdkScreen* screen     = gtk_widget_get_screen (w);
	GdkColormap* colormap = gdk_screen_get_rgba_colormap (screen);

	if (!colormap)
		colormap = gdk_screen_get_rgb_colormap (screen);

	gtk_widget_set_colormap (w, colormap);
}	       
	
gint main (gint argc, gchar **argv) { 
	GtkWidget *window;
	selection_t sel = {FALSE, 0, 0,};

	gtk_init(&argc, &argv);
	
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

	gtk_window_stick (GTK_WINDOW (window));
	//	gtk_window_set_keep_below (GTK_WINDOW (window), TRUE);

        gtk_window_set_skip_taskbar_hint (GTK_WINDOW (window), TRUE);
	gtk_window_set_skip_pager_hint   (GTK_WINDOW (window), TRUE);
	// gtk_window_set_accept_focus      (GTK_WINDOW (window), FALSE);

	gtk_window_set_decorated (GTK_WINDOW (window), TRUE);
	gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
	gtk_widget_set_app_paintable (window, TRUE);
	
	gtk_window_set_default_size (GTK_WINDOW (window),
				     DEFAULT_WIDTH,
				     DEFAULT_HEIGHT);

	install_colormap(window);
	
	gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK  | 
			               GDK_BUTTON_MOTION_MASK |
			               GDK_BUTTON_RELEASE_MASK);

	g_signal_connect (G_OBJECT (window), "expose-event",
			  G_CALLBACK (paint),
			  &sel);
	
	g_signal_connect (G_OBJECT (window),
			  "key-press-event",
			  G_CALLBACK (on_key_press),
			  NULL);

	/* connect our rubber banding callbacks, like the paint callback
	 * we pass the selection as userdata
	 */
	g_signal_connect (G_OBJECT (window), "button_press_event",
			  G_CALLBACK (on_mouse_press),
			  &sel);

	g_signal_connect (G_OBJECT (window), "button_release_event",
			  G_CALLBACK (on_mouse_release),
			  &sel);

	g_signal_connect (G_OBJECT (window), "motion_notify_event",
			  G_CALLBACK (on_mouse_motion),
			  &sel);

	g_timeout_add (SECOND_INTERVAL/9, (GtkFunction) time_handler, window);

	gtk_widget_show_all (window);

	gtk_main ();

	return 0;
}