2 * Copyright (C) 2005 Tilman Sauerbeck (tilman at code-monkey de)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "rb_evas_main.h"
26 #include "rb_evas_object.h"
28 #define CALLBACK_HANDLER_FUNC(name) \
29 static void on_##name (void *data, Evas *evas, \
30 Evas_Object *evas_obj, void *event) \
32 VALUE tmp = (VALUE) event, klass, cb = (VALUE) data, ev; \
34 klass = rb_hash_aref (event_classes, INT2FIX (EVAS_CALLBACK_##name)); \
35 if (!NIL_P (klass)) { \
36 ev = rb_class_new_instance (1, &tmp, klass); \
37 rb_funcall (cb, rb_intern ("call"), 1, ev); \
39 rb_funcall (cb, rb_intern ("call"), 0); \
42 #define CALLBACK_HANDLER_METHOD(callback) \
44 GET_OBJ (self, RbEvasObject, e); \
46 if (!rb_block_given_p ()) \
49 cb = rb_block_proc (); \
51 if (NIL_P (e->callbacks)) \
52 e->callbacks = rb_ary_new (); \
54 rb_ary_push (e->callbacks, cb); \
56 evas_object_event_callback_add (e->real, EVAS_CALLBACK_##callback, \
57 on_##callback, (void *) cb); \
61 #define CALLBACK_REGISTER(name, callback, clsname) \
62 rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0); \
64 c = rb_define_class_under (mEvas, (clsname), cEvent); \
65 rb_define_private_method (rb_singleton_class (c), "new", NULL, 0); \
66 rb_define_private_method (c, "initialize", c_ev_##name##_init, 1); \
68 rb_hash_aset (event_classes, INT2FIX (EVAS_CALLBACK_##callback), c);
70 #define CALLBACK_REGISTER2(name) \
71 rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0);
74 (s) ? rb_str_new2 ((s)) : Qnil
76 static VALUE event_classes, cPos;
78 static VALUE c_ev_mouse_in_init (VALUE self, VALUE ev)
81 Evas_Event_Mouse_In *e = (Evas_Event_Mouse_In *) ev;
83 argv[0] = INT2FIX ((int) e->output.x);
84 argv[1] = INT2FIX ((int) e->output.y);
85 argv[2] = INT2FIX ((int) e->canvas.x);
86 argv[3] = INT2FIX ((int) e->canvas.y);
88 rb_iv_set (self, "@position",
89 rb_class_new_instance (4, argv, cPos));
90 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
95 static VALUE c_ev_mouse_out_init (VALUE self, VALUE ev)
98 Evas_Event_Mouse_Out *e = (Evas_Event_Mouse_Out *) ev;
100 argv[0] = INT2FIX ((int) e->output.x);
101 argv[1] = INT2FIX ((int) e->output.y);
102 argv[2] = INT2FIX ((int) e->canvas.x);
103 argv[3] = INT2FIX ((int) e->canvas.y);
105 rb_iv_set (self, "@position",
106 rb_class_new_instance (4, argv, cPos));
107 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
112 static VALUE c_ev_mouse_down_init (VALUE self, VALUE ev)
115 Evas_Event_Mouse_Down *e = (Evas_Event_Mouse_Down *) ev;
117 argv[0] = INT2FIX ((int) e->output.x);
118 argv[1] = INT2FIX ((int) e->output.y);
119 argv[2] = INT2FIX ((int) e->canvas.x);
120 argv[3] = INT2FIX ((int) e->canvas.y);
122 rb_iv_set (self, "@position",
123 rb_class_new_instance (4, argv, cPos));
124 rb_iv_set (self, "@button", INT2FIX (e->button));
129 static VALUE c_ev_mouse_up_init (VALUE self, VALUE ev)
132 Evas_Event_Mouse_Up *e = (Evas_Event_Mouse_Up *) ev;
134 argv[0] = INT2FIX ((int) e->output.x);
135 argv[1] = INT2FIX ((int) e->output.y);
136 argv[2] = INT2FIX ((int) e->canvas.x);
137 argv[3] = INT2FIX ((int) e->canvas.y);
139 rb_iv_set (self, "@position",
140 rb_class_new_instance (4, argv, cPos));
141 rb_iv_set (self, "@button", INT2FIX (e->button));
146 static VALUE c_ev_mouse_move_init (VALUE self, VALUE ev)
149 Evas_Event_Mouse_Move *e = (Evas_Event_Mouse_Move *) ev;
151 argv[0] = INT2FIX ((int) e->cur.output.x);
152 argv[1] = INT2FIX ((int) e->cur.output.y);
153 argv[2] = INT2FIX ((int) e->cur.canvas.x);
154 argv[3] = INT2FIX ((int) e->cur.canvas.y);
156 rb_iv_set (self, "@current",
157 rb_class_new_instance (4, argv, cPos));
159 argv[0] = INT2FIX ((int) e->prev.output.x);
160 argv[1] = INT2FIX ((int) e->prev.output.y);
161 argv[2] = INT2FIX ((int) e->prev.canvas.x);
162 argv[3] = INT2FIX ((int) e->prev.canvas.y);
164 rb_iv_set (self, "@previous",
165 rb_class_new_instance (4, argv, cPos));
166 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
171 static VALUE c_ev_mouse_wheel_init (VALUE self, VALUE ev)
174 Evas_Event_Mouse_Wheel *e = (Evas_Event_Mouse_Wheel *) ev;
176 argv[0] = INT2FIX ((int) e->output.x);
177 argv[1] = INT2FIX ((int) e->output.y);
178 argv[2] = INT2FIX ((int) e->canvas.x);
179 argv[3] = INT2FIX ((int) e->canvas.y);
181 rb_iv_set (self, "@position",
182 rb_class_new_instance (4, argv, cPos));
183 rb_iv_set (self, "@direction", INT2FIX (e->direction));
184 rb_iv_set (self, "@z", INT2FIX (e->z));
189 static VALUE c_ev_key_down_init (VALUE self, VALUE ev)
191 Evas_Event_Key_Down *e = (Evas_Event_Key_Down *) ev;
193 rb_iv_set (self, "@keyname", TO_STR (e->keyname));
194 rb_iv_set (self, "@key", TO_STR (e->key));
195 rb_iv_set (self, "@string", TO_STR (e->string));
196 rb_iv_set (self, "@compose", TO_STR (e->compose));
201 static VALUE c_ev_key_up_init (VALUE self, VALUE ev)
203 Evas_Event_Key_Up *e = (Evas_Event_Key_Up *) ev;
205 rb_iv_set (self, "@keyname", TO_STR (e->keyname));
206 rb_iv_set (self, "@key", TO_STR (e->key));
207 rb_iv_set (self, "@string", TO_STR (e->string));
208 rb_iv_set (self, "@compose", TO_STR (e->compose));
213 CALLBACK_HANDLER_FUNC (MOUSE_IN);
214 CALLBACK_HANDLER_FUNC (MOUSE_OUT);
215 CALLBACK_HANDLER_FUNC (MOUSE_DOWN);
216 CALLBACK_HANDLER_FUNC (MOUSE_UP);
217 CALLBACK_HANDLER_FUNC (MOUSE_MOVE);
218 CALLBACK_HANDLER_FUNC (MOUSE_WHEEL);
219 CALLBACK_HANDLER_FUNC (KEY_DOWN);
220 CALLBACK_HANDLER_FUNC (KEY_UP);
221 CALLBACK_HANDLER_FUNC (FOCUS_IN);
222 CALLBACK_HANDLER_FUNC (FOCUS_OUT);
223 CALLBACK_HANDLER_FUNC (SHOW);
224 CALLBACK_HANDLER_FUNC (HIDE);
225 CALLBACK_HANDLER_FUNC (MOVE);
226 CALLBACK_HANDLER_FUNC (RESIZE);
227 CALLBACK_HANDLER_FUNC (RESTACK);
229 static VALUE c_on_mouse_in (VALUE self)
231 CALLBACK_HANDLER_METHOD (MOUSE_IN);
234 static VALUE c_on_mouse_out (VALUE self)
236 CALLBACK_HANDLER_METHOD (MOUSE_OUT);
239 static VALUE c_on_mouse_down (VALUE self)
241 CALLBACK_HANDLER_METHOD (MOUSE_DOWN);
244 static VALUE c_on_mouse_up (VALUE self)
246 CALLBACK_HANDLER_METHOD (MOUSE_UP);
249 static VALUE c_on_mouse_move (VALUE self)
251 CALLBACK_HANDLER_METHOD (MOUSE_MOVE);
254 static VALUE c_on_mouse_wheel (VALUE self)
256 CALLBACK_HANDLER_METHOD (MOUSE_WHEEL);
259 static VALUE c_on_key_down (VALUE self)
261 CALLBACK_HANDLER_METHOD (KEY_DOWN);
264 static VALUE c_on_key_up (VALUE self)
266 CALLBACK_HANDLER_METHOD (KEY_UP);
269 static VALUE c_on_focus_in (VALUE self)
271 CALLBACK_HANDLER_METHOD (FOCUS_IN);
274 static VALUE c_on_focus_out (VALUE self)
276 CALLBACK_HANDLER_METHOD (FOCUS_OUT);
279 static VALUE c_on_show (VALUE self)
281 CALLBACK_HANDLER_METHOD (SHOW);
284 static VALUE c_on_hide (VALUE self)
286 CALLBACK_HANDLER_METHOD (HIDE);
289 static VALUE c_on_move (VALUE self)
291 CALLBACK_HANDLER_METHOD (MOVE);
294 static VALUE c_on_resize (VALUE self)
296 CALLBACK_HANDLER_METHOD (RESIZE);
299 static VALUE c_on_restack (VALUE self)
301 CALLBACK_HANDLER_METHOD (RESTACK);
304 static VALUE c_ev_init (VALUE argc, VALUE argv, VALUE self)
309 static VALUE c_pos_init (VALUE self, VALUE output_x, VALUE output_y,
310 VALUE canvas_x, VALUE canvas_y)
312 rb_iv_set (self, "@output_x", output_x);
313 rb_iv_set (self, "@output_y", output_y);
314 rb_iv_set (self, "@canvas_x", canvas_x);
315 rb_iv_set (self, "@canvas_y", canvas_y);
320 void Init_EvasObjectEvents (void)
324 event_classes = rb_hash_new ();
325 rb_global_variable (&event_classes);
327 cEvent = rb_define_class_under (mEvas, "EvasObjectEvent", rb_cObject);
328 rb_define_private_method (rb_singleton_class (cEvent), "new", NULL, 0);
329 rb_define_private_method (cEvent, "initialize", c_ev_init, -1);
331 cPos = rb_define_class_under (cEvent, "Position", rb_cObject);
332 rb_define_private_method (rb_singleton_class (cPos), "new", NULL, 0);
333 rb_define_private_method (cPos, "initialize", c_pos_init, 4);
334 rb_define_attr (cPos, "output_x", 1, 0);
335 rb_define_attr (cPos, "output_y", 1, 0);
336 rb_define_attr (cPos, "canvas_x", 1, 0);
337 rb_define_attr (cPos, "canvas_y", 1, 0);
339 CALLBACK_REGISTER (mouse_in, MOUSE_IN, "MouseInEvent");
340 rb_define_attr (c, "buttons", 1, 0);
341 rb_define_attr (c, "position", 1, 0);
343 CALLBACK_REGISTER (mouse_out, MOUSE_OUT, "MouseOutEvent");
344 rb_define_attr (c, "buttons", 1, 0);
345 rb_define_attr (c, "position", 1, 0);
347 CALLBACK_REGISTER (mouse_down, MOUSE_DOWN, "MouseDownEvent");
348 rb_define_attr (c, "button", 1, 0);
349 rb_define_attr (c, "position", 1, 0);
351 CALLBACK_REGISTER (mouse_up, MOUSE_UP, "MouseUpEvent");
352 rb_define_attr (c, "button", 1, 0);
353 rb_define_attr (c, "position", 1, 0);
355 CALLBACK_REGISTER (mouse_move, MOUSE_MOVE, "MouseMoveEvent");
356 rb_define_attr (c, "current", 1, 0);
357 rb_define_attr (c, "previous", 1, 0);
358 rb_define_attr (c, "buttons", 1, 0);
360 CALLBACK_REGISTER (mouse_wheel, MOUSE_WHEEL, "MouseWheelEvent");
361 rb_define_attr (c, "direction", 1, 0);
362 rb_define_attr (c, "z", 1, 0);
363 rb_define_attr (c, "position", 1, 0);
365 CALLBACK_REGISTER (key_down, KEY_DOWN, "KeyDownEvent");
366 rb_define_attr (c, "keyname", 1, 0);
367 rb_define_attr (c, "key", 1, 0);
368 rb_define_attr (c, "string", 1, 0);
369 rb_define_attr (c, "compose", 1, 0);
371 CALLBACK_REGISTER (key_up, KEY_UP, "KeyUpEvent");
372 rb_define_attr (c, "keyname", 1, 0);
373 rb_define_attr (c, "key", 1, 0);
374 rb_define_attr (c, "string", 1, 0);
375 rb_define_attr (c, "compose", 1, 0);
377 CALLBACK_REGISTER2 (focus_in);
378 CALLBACK_REGISTER2 (focus_out);
379 CALLBACK_REGISTER2 (show);
380 CALLBACK_REGISTER2 (hide);
381 CALLBACK_REGISTER2 (move);
382 CALLBACK_REGISTER2 (resize);
383 CALLBACK_REGISTER2 (restack);