2 * $Id: rb_evas_object_events.c 315 2005-04-07 18:27:26Z 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);
78 (s) ? rb_str_new2 ((s)) : Qnil
80 static VALUE event_classes, cPos;
82 static VALUE c_ev_mouse_in_init (VALUE self, VALUE ev)
85 Evas_Event_Mouse_In *e = (Evas_Event_Mouse_In *) ev;
87 argv[0] = INT2FIX ((int) e->output.x);
88 argv[1] = INT2FIX ((int) e->output.y);
89 argv[2] = INT2FIX ((int) e->canvas.x);
90 argv[3] = INT2FIX ((int) e->canvas.y);
92 rb_iv_set (self, "@position",
93 rb_class_new_instance (4, argv, cPos));
94 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
99 static VALUE c_ev_mouse_out_init (VALUE self, VALUE ev)
102 Evas_Event_Mouse_Out *e = (Evas_Event_Mouse_Out *) ev;
104 argv[0] = INT2FIX ((int) e->output.x);
105 argv[1] = INT2FIX ((int) e->output.y);
106 argv[2] = INT2FIX ((int) e->canvas.x);
107 argv[3] = INT2FIX ((int) e->canvas.y);
109 rb_iv_set (self, "@position",
110 rb_class_new_instance (4, argv, cPos));
111 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
116 static VALUE c_ev_mouse_down_init (VALUE self, VALUE ev)
119 Evas_Event_Mouse_Down *e = (Evas_Event_Mouse_Down *) ev;
121 argv[0] = INT2FIX ((int) e->output.x);
122 argv[1] = INT2FIX ((int) e->output.y);
123 argv[2] = INT2FIX ((int) e->canvas.x);
124 argv[3] = INT2FIX ((int) e->canvas.y);
126 rb_iv_set (self, "@position",
127 rb_class_new_instance (4, argv, cPos));
128 rb_iv_set (self, "@button", INT2FIX (e->button));
133 static VALUE c_ev_mouse_up_init (VALUE self, VALUE ev)
136 Evas_Event_Mouse_Up *e = (Evas_Event_Mouse_Up *) ev;
138 argv[0] = INT2FIX ((int) e->output.x);
139 argv[1] = INT2FIX ((int) e->output.y);
140 argv[2] = INT2FIX ((int) e->canvas.x);
141 argv[3] = INT2FIX ((int) e->canvas.y);
143 rb_iv_set (self, "@position",
144 rb_class_new_instance (4, argv, cPos));
145 rb_iv_set (self, "@button", INT2FIX (e->button));
150 static VALUE c_ev_mouse_move_init (VALUE self, VALUE ev)
153 Evas_Event_Mouse_Move *e = (Evas_Event_Mouse_Move *) ev;
155 argv[0] = INT2FIX ((int) e->cur.output.x);
156 argv[1] = INT2FIX ((int) e->cur.output.y);
157 argv[2] = INT2FIX ((int) e->cur.canvas.x);
158 argv[3] = INT2FIX ((int) e->cur.canvas.y);
160 rb_iv_set (self, "@current",
161 rb_class_new_instance (4, argv, cPos));
163 argv[0] = INT2FIX ((int) e->prev.output.x);
164 argv[1] = INT2FIX ((int) e->prev.output.y);
165 argv[2] = INT2FIX ((int) e->prev.canvas.x);
166 argv[3] = INT2FIX ((int) e->prev.canvas.y);
168 rb_iv_set (self, "@previous",
169 rb_class_new_instance (4, argv, cPos));
170 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
175 static VALUE c_ev_mouse_wheel_init (VALUE self, VALUE ev)
178 Evas_Event_Mouse_Wheel *e = (Evas_Event_Mouse_Wheel *) ev;
180 argv[0] = INT2FIX ((int) e->output.x);
181 argv[1] = INT2FIX ((int) e->output.y);
182 argv[2] = INT2FIX ((int) e->canvas.x);
183 argv[3] = INT2FIX ((int) e->canvas.y);
185 rb_iv_set (self, "@position",
186 rb_class_new_instance (4, argv, cPos));
187 rb_iv_set (self, "@direction", INT2FIX (e->direction));
188 rb_iv_set (self, "@z", INT2FIX (e->z));
193 static VALUE c_ev_key_down_init (VALUE self, VALUE ev)
195 Evas_Event_Key_Down *e = (Evas_Event_Key_Down *) ev;
197 rb_iv_set (self, "@keyname", TO_STR (e->keyname));
198 rb_iv_set (self, "@key", TO_STR (e->key));
199 rb_iv_set (self, "@string", TO_STR (e->string));
200 rb_iv_set (self, "@compose", TO_STR (e->compose));
205 static VALUE c_ev_key_up_init (VALUE self, VALUE ev)
207 Evas_Event_Key_Up *e = (Evas_Event_Key_Up *) ev;
209 rb_iv_set (self, "@keyname", TO_STR (e->keyname));
210 rb_iv_set (self, "@key", TO_STR (e->key));
211 rb_iv_set (self, "@string", TO_STR (e->string));
212 rb_iv_set (self, "@compose", TO_STR (e->compose));
217 CALLBACK_HANDLER_FUNC (mouse_in);
218 CALLBACK_HANDLER_FUNC (mouse_out);
219 CALLBACK_HANDLER_FUNC (mouse_down);
220 CALLBACK_HANDLER_FUNC (mouse_up);
221 CALLBACK_HANDLER_FUNC (mouse_move);
222 CALLBACK_HANDLER_FUNC (mouse_wheel);
223 CALLBACK_HANDLER_FUNC (key_down);
224 CALLBACK_HANDLER_FUNC (key_up);
225 CALLBACK_HANDLER_FUNC (focus_in);
226 CALLBACK_HANDLER_FUNC (focus_out);
227 CALLBACK_HANDLER_FUNC (show);
228 CALLBACK_HANDLER_FUNC (hide);
229 CALLBACK_HANDLER_FUNC (move);
230 CALLBACK_HANDLER_FUNC (resize);
231 CALLBACK_HANDLER_FUNC (restack);
233 static VALUE c_on_mouse_in (VALUE self)
235 CALLBACK_HANDLER_METHOD (mouse_in, MOUSE_IN);
238 static VALUE c_on_mouse_out (VALUE self)
240 CALLBACK_HANDLER_METHOD (mouse_out, MOUSE_OUT);
243 static VALUE c_on_mouse_down (VALUE self)
245 CALLBACK_HANDLER_METHOD (mouse_down, MOUSE_DOWN);
248 static VALUE c_on_mouse_up (VALUE self)
250 CALLBACK_HANDLER_METHOD (mouse_up, MOUSE_UP);
253 static VALUE c_on_mouse_move (VALUE self)
255 CALLBACK_HANDLER_METHOD (mouse_move, MOUSE_MOVE);
258 static VALUE c_on_mouse_wheel (VALUE self)
260 CALLBACK_HANDLER_METHOD (mouse_wheel, MOUSE_WHEEL);
263 static VALUE c_on_key_down (VALUE self)
265 CALLBACK_HANDLER_METHOD (key_down, KEY_DOWN);
268 static VALUE c_on_key_up (VALUE self)
270 CALLBACK_HANDLER_METHOD (key_up, KEY_UP);
273 static VALUE c_on_focus_in (VALUE self)
275 CALLBACK_HANDLER_METHOD (focus_in, FOCUS_IN);
278 static VALUE c_on_focus_out (VALUE self)
280 CALLBACK_HANDLER_METHOD (focus_out, FOCUS_OUT);
283 static VALUE c_on_show (VALUE self)
285 CALLBACK_HANDLER_METHOD (show, SHOW);
288 static VALUE c_on_hide (VALUE self)
290 CALLBACK_HANDLER_METHOD (hide, HIDE);
293 static VALUE c_on_move (VALUE self)
295 CALLBACK_HANDLER_METHOD (move, MOVE);
298 static VALUE c_on_resize (VALUE self)
300 CALLBACK_HANDLER_METHOD (resize, RESIZE);
303 static VALUE c_on_restack (VALUE self)
305 CALLBACK_HANDLER_METHOD (restack, RESTACK);
308 static VALUE c_ev_init (VALUE argc, VALUE argv, VALUE self)
313 static VALUE c_pos_init (VALUE self, VALUE output_x, VALUE output_y,
314 VALUE canvas_x, VALUE canvas_y)
316 rb_iv_set (self, "@output_x", output_x);
317 rb_iv_set (self, "@output_y", output_y);
318 rb_iv_set (self, "@canvas_x", canvas_x);
319 rb_iv_set (self, "@canvas_y", canvas_y);
324 void Init_EvasObjectEvents (void)
328 event_classes = rb_hash_new ();
329 rb_global_variable (&event_classes);
331 cEvent = rb_define_class_under (mEvas, "EvasObjectEvent", rb_cObject);
332 rb_define_private_method (rb_singleton_class (cEvent), "new", NULL, 0);
333 rb_define_private_method (cEvent, "initialize", c_ev_init, -1);
335 cPos = rb_define_class_under (cEvent, "Position", rb_cObject);
336 rb_define_private_method (rb_singleton_class (cPos), "new", NULL, 0);
337 rb_define_private_method (cPos, "initialize", c_pos_init, 4);
338 rb_define_attr (cPos, "output_x", 1, 0);
339 rb_define_attr (cPos, "output_y", 1, 0);
340 rb_define_attr (cPos, "canvas_x", 1, 0);
341 rb_define_attr (cPos, "canvas_y", 1, 0);
343 CALLBACK_REGISTER (mouse_in, "MouseInEvent");
344 rb_define_attr (c, "buttons", 1, 0);
345 rb_define_attr (c, "position", 1, 0);
347 CALLBACK_REGISTER (mouse_out, "MouseOutEvent");
348 rb_define_attr (c, "buttons", 1, 0);
349 rb_define_attr (c, "position", 1, 0);
351 CALLBACK_REGISTER (mouse_down, "MouseDownEvent");
352 rb_define_attr (c, "button", 1, 0);
353 rb_define_attr (c, "position", 1, 0);
355 CALLBACK_REGISTER (mouse_up, "MouseUpEvent");
356 rb_define_attr (c, "button", 1, 0);
357 rb_define_attr (c, "position", 1, 0);
359 CALLBACK_REGISTER (mouse_move, "MouseMoveEvent");
360 rb_define_attr (c, "current", 1, 0);
361 rb_define_attr (c, "previous", 1, 0);
362 rb_define_attr (c, "buttons", 1, 0);
364 CALLBACK_REGISTER (mouse_wheel, "MouseWheelEvent");
365 rb_define_attr (c, "direction", 1, 0);
366 rb_define_attr (c, "z", 1, 0);
367 rb_define_attr (c, "position", 1, 0);
369 CALLBACK_REGISTER (key_down, "KeyDownEvent");
370 rb_define_attr (c, "keyname", 1, 0);
371 rb_define_attr (c, "key", 1, 0);
372 rb_define_attr (c, "string", 1, 0);
373 rb_define_attr (c, "compose", 1, 0);
375 CALLBACK_REGISTER (key_up, "KeyUpEvent");
376 rb_define_attr (c, "keyname", 1, 0);
377 rb_define_attr (c, "key", 1, 0);
378 rb_define_attr (c, "string", 1, 0);
379 rb_define_attr (c, "compose", 1, 0);
381 CALLBACK_REGISTER2 (focus_in);
382 CALLBACK_REGISTER2 (focus_out);
383 CALLBACK_REGISTER2 (show);
384 CALLBACK_REGISTER2 (hide);
385 CALLBACK_REGISTER2 (move);
386 CALLBACK_REGISTER2 (resize);
387 CALLBACK_REGISTER2 (restack);