2 * $Id: rb_evas_object_events.c 285 2005-03-15 18:00:50Z 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); \
38 klass = rb_hash_aref (event_classes, s); \
39 ev = rb_class_new_instance (1, argv, klass); \
40 cb = rb_hash_aref (e->callbacks, s); \
42 rb_funcall (cb, rb_intern ("call"), 1, ev); \
45 #define CALLBACK_HANDLER_METHOD(name, callback) \
46 GET_OBJ (self, RbEvasObject, e); \
48 if (!rb_block_given_p ()) \
51 rb_hash_aset (e->callbacks, rb_str_new2 (#name), \
53 evas_object_event_callback_add (e->real, EVAS_CALLBACK_##callback, \
58 #define CALLBACK_REGISTER(name, clsname) \
59 rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0); \
61 c = rb_define_class_under (mEvas, (clsname), cEvent); \
62 rb_define_private_method (rb_singleton_class (c), "new", NULL, 0); \
63 rb_define_private_method (c, "initialize", c_ev_##name##_init, 1); \
64 rb_hash_aset (event_classes, rb_str_new2 (#name), c);
66 static VALUE event_classes, cPos;
68 static VALUE c_ev_mouse_down_init (VALUE self, VALUE ev)
71 Evas_Event_Mouse_Down *e = (Evas_Event_Mouse_Down *) ev;
73 argv[0] = INT2FIX ((int) e->output.x);
74 argv[1] = INT2FIX ((int) e->output.y);
75 argv[2] = INT2FIX ((int) e->canvas.x);
76 argv[3] = INT2FIX ((int) e->canvas.y);
78 rb_iv_set (self, "@position",
79 rb_class_new_instance (4, argv, cPos));
80 rb_iv_set (self, "@button", INT2FIX (e->button));
85 static VALUE c_ev_mouse_up_init (VALUE self, VALUE ev)
88 Evas_Event_Mouse_Up *e = (Evas_Event_Mouse_Up *) ev;
90 argv[0] = INT2FIX ((int) e->output.x);
91 argv[1] = INT2FIX ((int) e->output.y);
92 argv[2] = INT2FIX ((int) e->canvas.x);
93 argv[3] = INT2FIX ((int) e->canvas.y);
95 rb_iv_set (self, "@position",
96 rb_class_new_instance (4, argv, cPos));
97 rb_iv_set (self, "@button", INT2FIX (e->button));
102 static VALUE c_ev_mouse_move_init (VALUE self, VALUE ev)
105 Evas_Event_Mouse_Move *e = (Evas_Event_Mouse_Move *) ev;
107 argv[0] = INT2FIX ((int) e->cur.output.x);
108 argv[1] = INT2FIX ((int) e->cur.output.y);
109 argv[2] = INT2FIX ((int) e->cur.canvas.x);
110 argv[3] = INT2FIX ((int) e->cur.canvas.y);
112 rb_iv_set (self, "@current",
113 rb_class_new_instance (4, argv, cPos));
115 argv[0] = INT2FIX ((int) e->prev.output.x);
116 argv[1] = INT2FIX ((int) e->prev.output.y);
117 argv[2] = INT2FIX ((int) e->prev.canvas.x);
118 argv[3] = INT2FIX ((int) e->prev.canvas.y);
120 rb_iv_set (self, "@previous",
121 rb_class_new_instance (4, argv, cPos));
122 rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
127 CALLBACK_HANDLER_FUNC (mouse_down);
128 CALLBACK_HANDLER_FUNC (mouse_up);
129 CALLBACK_HANDLER_FUNC (mouse_move);
131 static VALUE c_on_mouse_down (VALUE self)
133 CALLBACK_HANDLER_METHOD (mouse_down, MOUSE_DOWN);
136 static VALUE c_on_mouse_up (VALUE self)
138 CALLBACK_HANDLER_METHOD (mouse_up, MOUSE_UP);
141 static VALUE c_on_mouse_move (VALUE self)
143 CALLBACK_HANDLER_METHOD (mouse_move, MOUSE_MOVE);
146 static VALUE c_ev_init (VALUE self)
151 static VALUE c_pos_init (VALUE self, VALUE output_x, VALUE output_y,
152 VALUE canvas_x, VALUE canvas_y)
154 rb_iv_set (self, "@output_x", output_x);
155 rb_iv_set (self, "@output_y", output_y);
156 rb_iv_set (self, "@canvas_x", canvas_x);
157 rb_iv_set (self, "@canvas_y", canvas_y);
162 void Init_EvasObjectEvents (void)
166 event_classes = rb_hash_new ();
167 rb_global_variable (&event_classes);
169 cEvent = rb_define_class_under (mEvas, "EvasObjectEvent", rb_cObject);
170 rb_define_private_method (rb_singleton_class (cEvent), "new", NULL, 0);
171 rb_define_private_method (cEvent, "initialize", c_ev_init, 0);
173 cPos = rb_define_class_under (cEvent, "Position", rb_cObject);
174 rb_define_private_method (rb_singleton_class (cPos), "new", NULL, 0);
175 rb_define_private_method (cPos, "initialize", c_pos_init, 4);
176 rb_define_attr (cPos, "output_x", 1, 0);
177 rb_define_attr (cPos, "output_y", 1, 0);
178 rb_define_attr (cPos, "canvas_x", 1, 0);
179 rb_define_attr (cPos, "canvas_y", 1, 0);
181 CALLBACK_REGISTER (mouse_down, "MouseDownEvent");
182 rb_define_attr (c, "button", 1, 0);
184 CALLBACK_REGISTER (mouse_up, "MouseUpEvent");
185 rb_define_attr (c, "button", 1, 0);
187 CALLBACK_REGISTER (mouse_move, "MouseMoveEvent");
188 rb_define_attr (c, "current", 1, 0);
189 rb_define_attr (c, "previous", 1, 0);
190 rb_define_attr (c, "buttons", 1, 0);