More event work.
[ruby-evas.git] / src / rb_evas_object_events.c
1 /*
2  * $Id: rb_evas_object_events.c 306 2005-03-23 17:29:37Z tilman $
3  *
4  * Copyright (C) 2005 Tilman Sauerbeck (tilman at code-monkey de)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ruby.h>
22 #include <stdbool.h>
23
24 #include <Evas.h>
25
26 #include "rb_evas_main.h"
27 #include "rb_evas.h"
28 #include "rb_evas_object.h"
29
30 #define CALLBACK_HANDLER_FUNC(name) \
31         static void on_##name (void *data, Evas *evas, \
32                                Evas_Object *evas_obj, void *event) \
33 { \
34         RbEvasObject *e = (RbEvasObject *) data; \
35         VALUE argv[1] = {(VALUE) event}, klass, cb, ev, s; \
36 \
37         s = rb_str_new2 (#name); \
38 \
39         cb = rb_hash_aref (e->callbacks, s); \
40 \
41         klass = rb_hash_aref (event_classes, s); \
42         if (!NIL_P (klass)) { \
43                 ev = rb_class_new_instance (1, argv, klass); \
44                 rb_funcall (cb, rb_intern ("call"), 1, ev); \
45         } else \
46                 rb_funcall (cb, rb_intern ("call"), 0); \
47 }
48
49 #define CALLBACK_HANDLER_METHOD(name, callback) \
50         GET_OBJ (self, RbEvasObject, e); \
51 \
52         if (!rb_block_given_p ()) \
53                 return Qnil; \
54 \
55         if (NIL_P (e->callbacks)) \
56                 e->callbacks = rb_hash_new (); \
57 \
58         rb_hash_aset (e->callbacks, rb_str_new2 (#name), \
59                       rb_block_proc ()); \
60         evas_object_event_callback_add (e->real, EVAS_CALLBACK_##callback, \
61                                         on_##name, e); \
62 \
63         return Qnil;
64
65 #define CALLBACK_REGISTER(name, clsname) \
66         rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0); \
67 \
68         c = rb_define_class_under (mEvas, (clsname), cEvent); \
69         rb_define_private_method (rb_singleton_class (c), "new", NULL, 0); \
70         rb_define_private_method (c, "initialize", c_ev_##name##_init, 1); \
71 \
72         rb_hash_aset (event_classes, rb_str_new2 (#name), c);
73
74 #define CALLBACK_REGISTER2(name) \
75         rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0);
76
77 static VALUE event_classes, cPos;
78
79 static VALUE c_ev_mouse_in_init (VALUE self, VALUE ev)
80 {
81         VALUE argv[4];
82         Evas_Event_Mouse_In *e = (Evas_Event_Mouse_In *) ev;
83
84         argv[0] = INT2FIX ((int) e->output.x);
85         argv[1] = INT2FIX ((int) e->output.y);
86         argv[2] = INT2FIX ((int) e->canvas.x);
87         argv[3] = INT2FIX ((int) e->canvas.y);
88
89         rb_iv_set (self, "@position",
90                    rb_class_new_instance (4, argv, cPos));
91         rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
92
93         return self;
94 }
95
96 static VALUE c_ev_mouse_out_init (VALUE self, VALUE ev)
97 {
98         VALUE argv[4];
99         Evas_Event_Mouse_Out *e = (Evas_Event_Mouse_Out *) ev;
100
101         argv[0] = INT2FIX ((int) e->output.x);
102         argv[1] = INT2FIX ((int) e->output.y);
103         argv[2] = INT2FIX ((int) e->canvas.x);
104         argv[3] = INT2FIX ((int) e->canvas.y);
105
106         rb_iv_set (self, "@position",
107                    rb_class_new_instance (4, argv, cPos));
108         rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
109
110         return self;
111 }
112
113 static VALUE c_ev_mouse_down_init (VALUE self, VALUE ev)
114 {
115         VALUE argv[4];
116         Evas_Event_Mouse_Down *e = (Evas_Event_Mouse_Down *) ev;
117
118         argv[0] = INT2FIX ((int) e->output.x);
119         argv[1] = INT2FIX ((int) e->output.y);
120         argv[2] = INT2FIX ((int) e->canvas.x);
121         argv[3] = INT2FIX ((int) e->canvas.y);
122
123         rb_iv_set (self, "@position",
124                    rb_class_new_instance (4, argv, cPos));
125         rb_iv_set (self, "@button", INT2FIX (e->button));
126
127         return self;
128 }
129
130 static VALUE c_ev_mouse_up_init (VALUE self, VALUE ev)
131 {
132         VALUE argv[4];
133         Evas_Event_Mouse_Up *e = (Evas_Event_Mouse_Up *) ev;
134
135         argv[0] = INT2FIX ((int) e->output.x);
136         argv[1] = INT2FIX ((int) e->output.y);
137         argv[2] = INT2FIX ((int) e->canvas.x);
138         argv[3] = INT2FIX ((int) e->canvas.y);
139
140         rb_iv_set (self, "@position",
141                    rb_class_new_instance (4, argv, cPos));
142         rb_iv_set (self, "@button", INT2FIX (e->button));
143
144         return self;
145 }
146
147 static VALUE c_ev_mouse_move_init (VALUE self, VALUE ev)
148 {
149         VALUE argv[4];
150         Evas_Event_Mouse_Move *e = (Evas_Event_Mouse_Move *) ev;
151
152         argv[0] = INT2FIX ((int) e->cur.output.x);
153         argv[1] = INT2FIX ((int) e->cur.output.y);
154         argv[2] = INT2FIX ((int) e->cur.canvas.x);
155         argv[3] = INT2FIX ((int) e->cur.canvas.y);
156
157         rb_iv_set (self, "@current",
158                    rb_class_new_instance (4, argv, cPos));
159
160         argv[0] = INT2FIX ((int) e->prev.output.x);
161         argv[1] = INT2FIX ((int) e->prev.output.y);
162         argv[2] = INT2FIX ((int) e->prev.canvas.x);
163         argv[3] = INT2FIX ((int) e->prev.canvas.y);
164
165         rb_iv_set (self, "@previous",
166                    rb_class_new_instance (4, argv, cPos));
167         rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
168
169         return self;
170 }
171
172 static VALUE c_ev_mouse_wheel_init (VALUE self, VALUE ev)
173 {
174         VALUE argv[4];
175         Evas_Event_Mouse_Wheel *e = (Evas_Event_Mouse_Wheel *) ev;
176
177         argv[0] = INT2FIX ((int) e->output.x);
178         argv[1] = INT2FIX ((int) e->output.y);
179         argv[2] = INT2FIX ((int) e->canvas.x);
180         argv[3] = INT2FIX ((int) e->canvas.y);
181
182         rb_iv_set (self, "@position",
183                    rb_class_new_instance (4, argv, cPos));
184         rb_iv_set (self, "@direction", INT2FIX (e->direction));
185         rb_iv_set (self, "@z", INT2FIX (e->z));
186
187         return self;
188 }
189
190 static VALUE c_ev_key_down_init (VALUE self, VALUE ev)
191 {
192         Evas_Event_Key_Down *e = (Evas_Event_Key_Down *) ev;
193
194         rb_iv_set (self, "@keyname", rb_str_new2 (e->keyname));
195         rb_iv_set (self, "@key", rb_str_new2 (e->key));
196         rb_iv_set (self, "@string", rb_str_new2 (e->string));
197         rb_iv_set (self, "@compose", rb_str_new2 (e->compose));
198
199         return self;
200 }
201
202 static VALUE c_ev_key_up_init (VALUE self, VALUE ev)
203 {
204         Evas_Event_Key_Up *e = (Evas_Event_Key_Up *) ev;
205
206         rb_iv_set (self, "@keyname", rb_str_new2 (e->keyname));
207         rb_iv_set (self, "@key", rb_str_new2 (e->key));
208         rb_iv_set (self, "@string", rb_str_new2 (e->string));
209         rb_iv_set (self, "@compose", rb_str_new2 (e->compose));
210
211         return self;
212 }
213
214 CALLBACK_HANDLER_FUNC (mouse_in);
215 CALLBACK_HANDLER_FUNC (mouse_out);
216 CALLBACK_HANDLER_FUNC (mouse_down);
217 CALLBACK_HANDLER_FUNC (mouse_up);
218 CALLBACK_HANDLER_FUNC (mouse_move);
219 CALLBACK_HANDLER_FUNC (mouse_wheel);
220 CALLBACK_HANDLER_FUNC (key_down);
221 CALLBACK_HANDLER_FUNC (key_up);
222 CALLBACK_HANDLER_FUNC (focus_in);
223 CALLBACK_HANDLER_FUNC (focus_out);
224 CALLBACK_HANDLER_FUNC (show);
225 CALLBACK_HANDLER_FUNC (hide);
226 CALLBACK_HANDLER_FUNC (move);
227 CALLBACK_HANDLER_FUNC (resize);
228 CALLBACK_HANDLER_FUNC (restack);
229
230 static VALUE c_on_mouse_in (VALUE self)
231 {
232         CALLBACK_HANDLER_METHOD (mouse_in, MOUSE_IN);
233 }
234
235 static VALUE c_on_mouse_out (VALUE self)
236 {
237         CALLBACK_HANDLER_METHOD (mouse_out, MOUSE_OUT);
238 }
239
240 static VALUE c_on_mouse_down (VALUE self)
241 {
242         CALLBACK_HANDLER_METHOD (mouse_down, MOUSE_DOWN);
243 }
244
245 static VALUE c_on_mouse_up (VALUE self)
246 {
247         CALLBACK_HANDLER_METHOD (mouse_up, MOUSE_UP);
248 }
249
250 static VALUE c_on_mouse_move (VALUE self)
251 {
252         CALLBACK_HANDLER_METHOD (mouse_move, MOUSE_MOVE);
253 }
254
255 static VALUE c_on_mouse_wheel (VALUE self)
256 {
257         CALLBACK_HANDLER_METHOD (mouse_wheel, MOUSE_WHEEL);
258 }
259
260 static VALUE c_on_key_down (VALUE self)
261 {
262         CALLBACK_HANDLER_METHOD (key_down, KEY_DOWN);
263 }
264
265 static VALUE c_on_key_up (VALUE self)
266 {
267         CALLBACK_HANDLER_METHOD (key_up, KEY_UP);
268 }
269
270 static VALUE c_on_focus_in (VALUE self)
271 {
272         CALLBACK_HANDLER_METHOD (focus_in, FOCUS_IN);
273 }
274
275 static VALUE c_on_focus_out (VALUE self)
276 {
277         CALLBACK_HANDLER_METHOD (focus_out, FOCUS_OUT);
278 }
279
280 static VALUE c_on_show (VALUE self)
281 {
282         CALLBACK_HANDLER_METHOD (show, SHOW);
283 }
284
285 static VALUE c_on_hide (VALUE self)
286 {
287         CALLBACK_HANDLER_METHOD (hide, HIDE);
288 }
289
290 static VALUE c_on_move (VALUE self)
291 {
292         CALLBACK_HANDLER_METHOD (move, MOVE);
293 }
294
295 static VALUE c_on_resize (VALUE self)
296 {
297         CALLBACK_HANDLER_METHOD (resize, RESIZE);
298 }
299
300 static VALUE c_on_restack (VALUE self)
301 {
302         CALLBACK_HANDLER_METHOD (restack, RESTACK);
303 }
304
305 static VALUE c_ev_init (VALUE argc, VALUE argv, VALUE self)
306 {
307         return self;
308 }
309
310 static VALUE c_pos_init (VALUE self, VALUE output_x, VALUE output_y,
311                          VALUE canvas_x, VALUE canvas_y)
312 {
313         rb_iv_set (self, "@output_x", output_x);
314         rb_iv_set (self, "@output_y", output_y);
315         rb_iv_set (self, "@canvas_x", canvas_x);
316         rb_iv_set (self, "@canvas_y", canvas_y);
317
318         return self;
319 }
320
321 void Init_EvasObjectEvents (void)
322 {
323         VALUE cEvent, c;
324
325         event_classes = rb_hash_new ();
326         rb_global_variable (&event_classes);
327
328         cEvent = rb_define_class_under (mEvas, "EvasObjectEvent", rb_cObject);
329         rb_define_private_method (rb_singleton_class (cEvent), "new", NULL, 0);
330         rb_define_private_method (cEvent, "initialize", c_ev_init, -1);
331
332         cPos = rb_define_class_under (cEvent, "Position", rb_cObject);
333         rb_define_private_method (rb_singleton_class (cPos), "new", NULL, 0);
334         rb_define_private_method (cPos, "initialize", c_pos_init, 4);
335         rb_define_attr (cPos, "output_x", 1, 0);
336         rb_define_attr (cPos, "output_y", 1, 0);
337         rb_define_attr (cPos, "canvas_x", 1, 0);
338         rb_define_attr (cPos, "canvas_y", 1, 0);
339
340         CALLBACK_REGISTER (mouse_in, "MouseInEvent");
341         rb_define_attr (c, "buttons", 1, 0);
342         rb_define_attr (c, "position", 1, 0);
343
344         CALLBACK_REGISTER (mouse_out, "MouseOutEvent");
345         rb_define_attr (c, "buttons", 1, 0);
346         rb_define_attr (c, "position", 1, 0);
347
348         CALLBACK_REGISTER (mouse_down, "MouseDownEvent");
349         rb_define_attr (c, "button", 1, 0);
350         rb_define_attr (c, "position", 1, 0);
351
352         CALLBACK_REGISTER (mouse_up, "MouseUpEvent");
353         rb_define_attr (c, "button", 1, 0);
354         rb_define_attr (c, "position", 1, 0);
355
356         CALLBACK_REGISTER (mouse_move, "MouseMoveEvent");
357         rb_define_attr (c, "current", 1, 0);
358         rb_define_attr (c, "previous", 1, 0);
359         rb_define_attr (c, "buttons", 1, 0);
360
361         CALLBACK_REGISTER (mouse_wheel, "MouseWheelEvent");
362         rb_define_attr (c, "direction", 1, 0);
363         rb_define_attr (c, "z", 1, 0);
364         rb_define_attr (c, "position", 1, 0);
365
366         CALLBACK_REGISTER (key_down, "KeyDownEvent");
367         rb_define_attr (c, "keyname", 1, 0);
368         rb_define_attr (c, "key", 1, 0);
369         rb_define_attr (c, "string", 1, 0);
370         rb_define_attr (c, "compose", 1, 0);
371
372         CALLBACK_REGISTER (key_up, "KeyUpEvent");
373         rb_define_attr (c, "keyname", 1, 0);
374         rb_define_attr (c, "key", 1, 0);
375         rb_define_attr (c, "string", 1, 0);
376         rb_define_attr (c, "compose", 1, 0);
377
378         CALLBACK_REGISTER2 (focus_in);
379         CALLBACK_REGISTER2 (focus_out);
380         CALLBACK_REGISTER2 (show);
381         CALLBACK_REGISTER2 (hide);
382         CALLBACK_REGISTER2 (move);
383         CALLBACK_REGISTER2 (resize);
384         CALLBACK_REGISTER2 (restack);
385 }