2 * $Id: rb_evas_object_events.c 306 2005-03-23 17:29:37Z tilman $
4 * Copyright (C) 2005 Tilman Sauerbeck (tilman at code-monkey de)
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.
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.
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
26 #include "rb_evas_main.h"
28 #include "rb_evas_object.h"
30 #define CALLBACK_HANDLER_FUNC(name) \
31 static void on_##name (void *data, Evas *evas, \
32 Evas_Object *evas_obj, void *event) \
34 RbEvasObject *e = (RbEvasObject *) data; \
35 VALUE argv[1] = {(VALUE) event}, klass, cb, ev, s; \
37 s = rb_str_new2 (#name); \
39 cb = rb_hash_aref (e->callbacks, s); \
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); \
46 rb_funcall (cb, rb_intern ("call"), 0); \
49 #define CALLBACK_HANDLER_METHOD(name, callback) \
50 GET_OBJ (self, RbEvasObject, e); \
52 if (!rb_block_given_p ()) \
55 if (NIL_P (e->callbacks)) \
56 e->callbacks = rb_hash_new (); \
58 rb_hash_aset (e->callbacks, rb_str_new2 (#name), \
60 evas_object_event_callback_add (e->real, EVAS_CALLBACK_##callback, \
65 #define CALLBACK_REGISTER(name, clsname) \
66 rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0); \
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); \
72 rb_hash_aset (event_classes, rb_str_new2 (#name), c);
74 #define CALLBACK_REGISTER2(name) \
75 rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0);
77 static VALUE event_classes, cPos;
79 static VALUE c_ev_mouse_in_init (VALUE self, VALUE ev)
82 Evas_Event_Mouse_In *e = (Evas_Event_Mouse_In *) ev;
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);
89 rb_iv_set (self, "@position",
90 rb_class_new_instance (4, argv, cPos));
91 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
96 static VALUE c_ev_mouse_out_init (VALUE self, VALUE ev)
99 Evas_Event_Mouse_Out *e = (Evas_Event_Mouse_Out *) ev;
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);
106 rb_iv_set (self, "@position",
107 rb_class_new_instance (4, argv, cPos));
108 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
113 static VALUE c_ev_mouse_down_init (VALUE self, VALUE ev)
116 Evas_Event_Mouse_Down *e = (Evas_Event_Mouse_Down *) ev;
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);
123 rb_iv_set (self, "@position",
124 rb_class_new_instance (4, argv, cPos));
125 rb_iv_set (self, "@button", INT2FIX (e->button));
130 static VALUE c_ev_mouse_up_init (VALUE self, VALUE ev)
133 Evas_Event_Mouse_Up *e = (Evas_Event_Mouse_Up *) ev;
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);
140 rb_iv_set (self, "@position",
141 rb_class_new_instance (4, argv, cPos));
142 rb_iv_set (self, "@button", INT2FIX (e->button));
147 static VALUE c_ev_mouse_move_init (VALUE self, VALUE ev)
150 Evas_Event_Mouse_Move *e = (Evas_Event_Mouse_Move *) ev;
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);
157 rb_iv_set (self, "@current",
158 rb_class_new_instance (4, argv, cPos));
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);
165 rb_iv_set (self, "@previous",
166 rb_class_new_instance (4, argv, cPos));
167 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
172 static VALUE c_ev_mouse_wheel_init (VALUE self, VALUE ev)
175 Evas_Event_Mouse_Wheel *e = (Evas_Event_Mouse_Wheel *) ev;
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);
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));
190 static VALUE c_ev_key_down_init (VALUE self, VALUE ev)
192 Evas_Event_Key_Down *e = (Evas_Event_Key_Down *) ev;
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));
202 static VALUE c_ev_key_up_init (VALUE self, VALUE ev)
204 Evas_Event_Key_Up *e = (Evas_Event_Key_Up *) ev;
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));
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);
230 static VALUE c_on_mouse_in (VALUE self)
232 CALLBACK_HANDLER_METHOD (mouse_in, MOUSE_IN);
235 static VALUE c_on_mouse_out (VALUE self)
237 CALLBACK_HANDLER_METHOD (mouse_out, MOUSE_OUT);
240 static VALUE c_on_mouse_down (VALUE self)
242 CALLBACK_HANDLER_METHOD (mouse_down, MOUSE_DOWN);
245 static VALUE c_on_mouse_up (VALUE self)
247 CALLBACK_HANDLER_METHOD (mouse_up, MOUSE_UP);
250 static VALUE c_on_mouse_move (VALUE self)
252 CALLBACK_HANDLER_METHOD (mouse_move, MOUSE_MOVE);
255 static VALUE c_on_mouse_wheel (VALUE self)
257 CALLBACK_HANDLER_METHOD (mouse_wheel, MOUSE_WHEEL);
260 static VALUE c_on_key_down (VALUE self)
262 CALLBACK_HANDLER_METHOD (key_down, KEY_DOWN);
265 static VALUE c_on_key_up (VALUE self)
267 CALLBACK_HANDLER_METHOD (key_up, KEY_UP);
270 static VALUE c_on_focus_in (VALUE self)
272 CALLBACK_HANDLER_METHOD (focus_in, FOCUS_IN);
275 static VALUE c_on_focus_out (VALUE self)
277 CALLBACK_HANDLER_METHOD (focus_out, FOCUS_OUT);
280 static VALUE c_on_show (VALUE self)
282 CALLBACK_HANDLER_METHOD (show, SHOW);
285 static VALUE c_on_hide (VALUE self)
287 CALLBACK_HANDLER_METHOD (hide, HIDE);
290 static VALUE c_on_move (VALUE self)
292 CALLBACK_HANDLER_METHOD (move, MOVE);
295 static VALUE c_on_resize (VALUE self)
297 CALLBACK_HANDLER_METHOD (resize, RESIZE);
300 static VALUE c_on_restack (VALUE self)
302 CALLBACK_HANDLER_METHOD (restack, RESTACK);
305 static VALUE c_ev_init (VALUE argc, VALUE argv, VALUE self)
310 static VALUE c_pos_init (VALUE self, VALUE output_x, VALUE output_y,
311 VALUE canvas_x, VALUE canvas_y)
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);
321 void Init_EvasObjectEvents (void)
325 event_classes = rb_hash_new ();
326 rb_global_variable (&event_classes);
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);
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);
340 CALLBACK_REGISTER (mouse_in, "MouseInEvent");
341 rb_define_attr (c, "buttons", 1, 0);
342 rb_define_attr (c, "position", 1, 0);
344 CALLBACK_REGISTER (mouse_out, "MouseOutEvent");
345 rb_define_attr (c, "buttons", 1, 0);
346 rb_define_attr (c, "position", 1, 0);
348 CALLBACK_REGISTER (mouse_down, "MouseDownEvent");
349 rb_define_attr (c, "button", 1, 0);
350 rb_define_attr (c, "position", 1, 0);
352 CALLBACK_REGISTER (mouse_up, "MouseUpEvent");
353 rb_define_attr (c, "button", 1, 0);
354 rb_define_attr (c, "position", 1, 0);
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);
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);
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);
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);
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);