2 * $Id: rb_evas_object_events.c 370 2006-02-15 18:14:53Z 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 VALUE tmp = (VALUE) event, klass, cb = (VALUE) data, ev; \
36 klass = rb_hash_aref (event_classes, INT2FIX (EVAS_CALLBACK_##name)); \
37 if (!NIL_P (klass)) { \
38 ev = rb_class_new_instance (1, &tmp, klass); \
39 rb_funcall (cb, rb_intern ("call"), 1, ev); \
41 rb_funcall (cb, rb_intern ("call"), 0); \
44 #define CALLBACK_HANDLER_METHOD(callback) \
46 GET_OBJ (self, RbEvasObject, e); \
48 if (!rb_block_given_p ()) \
51 cb = rb_block_proc (); \
53 if (NIL_P (e->callbacks)) \
54 e->callbacks = rb_ary_new (); \
56 rb_ary_push (e->callbacks, cb); \
58 evas_object_event_callback_add (e->real, EVAS_CALLBACK_##callback, \
59 on_##callback, (void *) cb); \
63 #define CALLBACK_REGISTER(name, callback, clsname) \
64 rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0); \
66 c = rb_define_class_under (mEvas, (clsname), cEvent); \
67 rb_define_private_method (rb_singleton_class (c), "new", NULL, 0); \
68 rb_define_private_method (c, "initialize", c_ev_##name##_init, 1); \
70 rb_hash_aset (event_classes, INT2FIX (EVAS_CALLBACK_##callback), c);
72 #define CALLBACK_REGISTER2(name) \
73 rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0);
76 (s) ? rb_str_new2 ((s)) : Qnil
78 static VALUE event_classes, cPos;
80 static VALUE c_ev_mouse_in_init (VALUE self, VALUE ev)
83 Evas_Event_Mouse_In *e = (Evas_Event_Mouse_In *) ev;
85 argv[0] = INT2FIX ((int) e->output.x);
86 argv[1] = INT2FIX ((int) e->output.y);
87 argv[2] = INT2FIX ((int) e->canvas.x);
88 argv[3] = INT2FIX ((int) e->canvas.y);
90 rb_iv_set (self, "@position",
91 rb_class_new_instance (4, argv, cPos));
92 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
97 static VALUE c_ev_mouse_out_init (VALUE self, VALUE ev)
100 Evas_Event_Mouse_Out *e = (Evas_Event_Mouse_Out *) ev;
102 argv[0] = INT2FIX ((int) e->output.x);
103 argv[1] = INT2FIX ((int) e->output.y);
104 argv[2] = INT2FIX ((int) e->canvas.x);
105 argv[3] = INT2FIX ((int) e->canvas.y);
107 rb_iv_set (self, "@position",
108 rb_class_new_instance (4, argv, cPos));
109 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
114 static VALUE c_ev_mouse_down_init (VALUE self, VALUE ev)
117 Evas_Event_Mouse_Down *e = (Evas_Event_Mouse_Down *) ev;
119 argv[0] = INT2FIX ((int) e->output.x);
120 argv[1] = INT2FIX ((int) e->output.y);
121 argv[2] = INT2FIX ((int) e->canvas.x);
122 argv[3] = INT2FIX ((int) e->canvas.y);
124 rb_iv_set (self, "@position",
125 rb_class_new_instance (4, argv, cPos));
126 rb_iv_set (self, "@button", INT2FIX (e->button));
131 static VALUE c_ev_mouse_up_init (VALUE self, VALUE ev)
134 Evas_Event_Mouse_Up *e = (Evas_Event_Mouse_Up *) ev;
136 argv[0] = INT2FIX ((int) e->output.x);
137 argv[1] = INT2FIX ((int) e->output.y);
138 argv[2] = INT2FIX ((int) e->canvas.x);
139 argv[3] = INT2FIX ((int) e->canvas.y);
141 rb_iv_set (self, "@position",
142 rb_class_new_instance (4, argv, cPos));
143 rb_iv_set (self, "@button", INT2FIX (e->button));
148 static VALUE c_ev_mouse_move_init (VALUE self, VALUE ev)
151 Evas_Event_Mouse_Move *e = (Evas_Event_Mouse_Move *) ev;
153 argv[0] = INT2FIX ((int) e->cur.output.x);
154 argv[1] = INT2FIX ((int) e->cur.output.y);
155 argv[2] = INT2FIX ((int) e->cur.canvas.x);
156 argv[3] = INT2FIX ((int) e->cur.canvas.y);
158 rb_iv_set (self, "@current",
159 rb_class_new_instance (4, argv, cPos));
161 argv[0] = INT2FIX ((int) e->prev.output.x);
162 argv[1] = INT2FIX ((int) e->prev.output.y);
163 argv[2] = INT2FIX ((int) e->prev.canvas.x);
164 argv[3] = INT2FIX ((int) e->prev.canvas.y);
166 rb_iv_set (self, "@previous",
167 rb_class_new_instance (4, argv, cPos));
168 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
173 static VALUE c_ev_mouse_wheel_init (VALUE self, VALUE ev)
176 Evas_Event_Mouse_Wheel *e = (Evas_Event_Mouse_Wheel *) ev;
178 argv[0] = INT2FIX ((int) e->output.x);
179 argv[1] = INT2FIX ((int) e->output.y);
180 argv[2] = INT2FIX ((int) e->canvas.x);
181 argv[3] = INT2FIX ((int) e->canvas.y);
183 rb_iv_set (self, "@position",
184 rb_class_new_instance (4, argv, cPos));
185 rb_iv_set (self, "@direction", INT2FIX (e->direction));
186 rb_iv_set (self, "@z", INT2FIX (e->z));
191 static VALUE c_ev_key_down_init (VALUE self, VALUE ev)
193 Evas_Event_Key_Down *e = (Evas_Event_Key_Down *) ev;
195 rb_iv_set (self, "@keyname", TO_STR (e->keyname));
196 rb_iv_set (self, "@key", TO_STR (e->key));
197 rb_iv_set (self, "@string", TO_STR (e->string));
198 rb_iv_set (self, "@compose", TO_STR (e->compose));
203 static VALUE c_ev_key_up_init (VALUE self, VALUE ev)
205 Evas_Event_Key_Up *e = (Evas_Event_Key_Up *) ev;
207 rb_iv_set (self, "@keyname", TO_STR (e->keyname));
208 rb_iv_set (self, "@key", TO_STR (e->key));
209 rb_iv_set (self, "@string", TO_STR (e->string));
210 rb_iv_set (self, "@compose", TO_STR (e->compose));
215 CALLBACK_HANDLER_FUNC (MOUSE_IN);
216 CALLBACK_HANDLER_FUNC (MOUSE_OUT);
217 CALLBACK_HANDLER_FUNC (MOUSE_DOWN);
218 CALLBACK_HANDLER_FUNC (MOUSE_UP);
219 CALLBACK_HANDLER_FUNC (MOUSE_MOVE);
220 CALLBACK_HANDLER_FUNC (MOUSE_WHEEL);
221 CALLBACK_HANDLER_FUNC (KEY_DOWN);
222 CALLBACK_HANDLER_FUNC (KEY_UP);
223 CALLBACK_HANDLER_FUNC (FOCUS_IN);
224 CALLBACK_HANDLER_FUNC (FOCUS_OUT);
225 CALLBACK_HANDLER_FUNC (SHOW);
226 CALLBACK_HANDLER_FUNC (HIDE);
227 CALLBACK_HANDLER_FUNC (MOVE);
228 CALLBACK_HANDLER_FUNC (RESIZE);
229 CALLBACK_HANDLER_FUNC (RESTACK);
231 static VALUE c_on_mouse_in (VALUE self)
233 CALLBACK_HANDLER_METHOD (MOUSE_IN);
236 static VALUE c_on_mouse_out (VALUE self)
238 CALLBACK_HANDLER_METHOD (MOUSE_OUT);
241 static VALUE c_on_mouse_down (VALUE self)
243 CALLBACK_HANDLER_METHOD (MOUSE_DOWN);
246 static VALUE c_on_mouse_up (VALUE self)
248 CALLBACK_HANDLER_METHOD (MOUSE_UP);
251 static VALUE c_on_mouse_move (VALUE self)
253 CALLBACK_HANDLER_METHOD (MOUSE_MOVE);
256 static VALUE c_on_mouse_wheel (VALUE self)
258 CALLBACK_HANDLER_METHOD (MOUSE_WHEEL);
261 static VALUE c_on_key_down (VALUE self)
263 CALLBACK_HANDLER_METHOD (KEY_DOWN);
266 static VALUE c_on_key_up (VALUE self)
268 CALLBACK_HANDLER_METHOD (KEY_UP);
271 static VALUE c_on_focus_in (VALUE self)
273 CALLBACK_HANDLER_METHOD (FOCUS_IN);
276 static VALUE c_on_focus_out (VALUE self)
278 CALLBACK_HANDLER_METHOD (FOCUS_OUT);
281 static VALUE c_on_show (VALUE self)
283 CALLBACK_HANDLER_METHOD (SHOW);
286 static VALUE c_on_hide (VALUE self)
288 CALLBACK_HANDLER_METHOD (HIDE);
291 static VALUE c_on_move (VALUE self)
293 CALLBACK_HANDLER_METHOD (MOVE);
296 static VALUE c_on_resize (VALUE self)
298 CALLBACK_HANDLER_METHOD (RESIZE);
301 static VALUE c_on_restack (VALUE self)
303 CALLBACK_HANDLER_METHOD (RESTACK);
306 static VALUE c_ev_init (VALUE argc, VALUE argv, VALUE self)
311 static VALUE c_pos_init (VALUE self, VALUE output_x, VALUE output_y,
312 VALUE canvas_x, VALUE canvas_y)
314 rb_iv_set (self, "@output_x", output_x);
315 rb_iv_set (self, "@output_y", output_y);
316 rb_iv_set (self, "@canvas_x", canvas_x);
317 rb_iv_set (self, "@canvas_y", canvas_y);
322 void Init_EvasObjectEvents (void)
326 event_classes = rb_hash_new ();
327 rb_global_variable (&event_classes);
329 cEvent = rb_define_class_under (mEvas, "EvasObjectEvent", rb_cObject);
330 rb_define_private_method (rb_singleton_class (cEvent), "new", NULL, 0);
331 rb_define_private_method (cEvent, "initialize", c_ev_init, -1);
333 cPos = rb_define_class_under (cEvent, "Position", rb_cObject);
334 rb_define_private_method (rb_singleton_class (cPos), "new", NULL, 0);
335 rb_define_private_method (cPos, "initialize", c_pos_init, 4);
336 rb_define_attr (cPos, "output_x", 1, 0);
337 rb_define_attr (cPos, "output_y", 1, 0);
338 rb_define_attr (cPos, "canvas_x", 1, 0);
339 rb_define_attr (cPos, "canvas_y", 1, 0);
341 CALLBACK_REGISTER (mouse_in, MOUSE_IN, "MouseInEvent");
342 rb_define_attr (c, "buttons", 1, 0);
343 rb_define_attr (c, "position", 1, 0);
345 CALLBACK_REGISTER (mouse_out, MOUSE_OUT, "MouseOutEvent");
346 rb_define_attr (c, "buttons", 1, 0);
347 rb_define_attr (c, "position", 1, 0);
349 CALLBACK_REGISTER (mouse_down, MOUSE_DOWN, "MouseDownEvent");
350 rb_define_attr (c, "button", 1, 0);
351 rb_define_attr (c, "position", 1, 0);
353 CALLBACK_REGISTER (mouse_up, MOUSE_UP, "MouseUpEvent");
354 rb_define_attr (c, "button", 1, 0);
355 rb_define_attr (c, "position", 1, 0);
357 CALLBACK_REGISTER (mouse_move, MOUSE_MOVE, "MouseMoveEvent");
358 rb_define_attr (c, "current", 1, 0);
359 rb_define_attr (c, "previous", 1, 0);
360 rb_define_attr (c, "buttons", 1, 0);
362 CALLBACK_REGISTER (mouse_wheel, MOUSE_WHEEL, "MouseWheelEvent");
363 rb_define_attr (c, "direction", 1, 0);
364 rb_define_attr (c, "z", 1, 0);
365 rb_define_attr (c, "position", 1, 0);
367 CALLBACK_REGISTER (key_down, KEY_DOWN, "KeyDownEvent");
368 rb_define_attr (c, "keyname", 1, 0);
369 rb_define_attr (c, "key", 1, 0);
370 rb_define_attr (c, "string", 1, 0);
371 rb_define_attr (c, "compose", 1, 0);
373 CALLBACK_REGISTER (key_up, KEY_UP, "KeyUpEvent");
374 rb_define_attr (c, "keyname", 1, 0);
375 rb_define_attr (c, "key", 1, 0);
376 rb_define_attr (c, "string", 1, 0);
377 rb_define_attr (c, "compose", 1, 0);
379 CALLBACK_REGISTER2 (focus_in);
380 CALLBACK_REGISTER2 (focus_out);
381 CALLBACK_REGISTER2 (show);
382 CALLBACK_REGISTER2 (hide);
383 CALLBACK_REGISTER2 (move);
384 CALLBACK_REGISTER2 (resize);
385 CALLBACK_REGISTER2 (restack);