Create the callbacks hash on first use.
[ruby-evas.git] / src / rb_evas_object_events.c
1 /*
2  * $Id: rb_evas_object_events.c 302 2005-03-22 17:41:35Z tilman $
3  *
4  * Copyright (C) 2005 Tilman Sauerbeck (tilman at code-monkey de)
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <ruby.h>
22 #include <stdbool.h>
23
24 #include <Evas.h>
25
26 #include "rb_evas_main.h"
27 #include "rb_evas.h"
28 #include "rb_evas_object.h"
29
30 #define CALLBACK_HANDLER_FUNC(name) \
31         static void on_##name (void *data, Evas *evas, \
32                                Evas_Object *evas_obj, void *event) \
33 { \
34         RbEvasObject *e = (RbEvasObject *) data; \
35         VALUE argv[1] = {(VALUE) event}, klass, cb, ev, s; \
36 \
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); \
41 \
42         rb_funcall (cb, rb_intern ("call"), 1, ev); \
43 }
44
45 #define CALLBACK_HANDLER_METHOD(name, callback) \
46         GET_OBJ (self, RbEvasObject, e); \
47 \
48         if (!rb_block_given_p ()) \
49                 return Qnil; \
50 \
51         if (NIL_P (e->callbacks)) \
52                 e->callbacks = rb_hash_new (); \
53 \
54         rb_hash_aset (e->callbacks, rb_str_new2 (#name), \
55                       rb_block_proc ()); \
56         evas_object_event_callback_add (e->real, EVAS_CALLBACK_##callback, \
57                                         on_##name, e); \
58 \
59         return Qnil;
60
61 #define CALLBACK_REGISTER(name, clsname) \
62         rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0); \
63 \
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); \
67         rb_hash_aset (event_classes, rb_str_new2 (#name), c);
68
69 static VALUE event_classes, cPos;
70
71 static VALUE c_ev_mouse_down_init (VALUE self, VALUE ev)
72 {
73         VALUE argv[4];
74         Evas_Event_Mouse_Down *e = (Evas_Event_Mouse_Down *) ev;
75
76         argv[0] = INT2FIX ((int) e->output.x);
77         argv[1] = INT2FIX ((int) e->output.y);
78         argv[2] = INT2FIX ((int) e->canvas.x);
79         argv[3] = INT2FIX ((int) e->canvas.y);
80
81         rb_iv_set (self, "@position",
82                    rb_class_new_instance (4, argv, cPos));
83         rb_iv_set (self, "@button", INT2FIX (e->button));
84
85         return self;
86 }
87
88 static VALUE c_ev_mouse_up_init (VALUE self, VALUE ev)
89 {
90         VALUE argv[4];
91         Evas_Event_Mouse_Up *e = (Evas_Event_Mouse_Up *) ev;
92
93         argv[0] = INT2FIX ((int) e->output.x);
94         argv[1] = INT2FIX ((int) e->output.y);
95         argv[2] = INT2FIX ((int) e->canvas.x);
96         argv[3] = INT2FIX ((int) e->canvas.y);
97
98         rb_iv_set (self, "@position",
99                    rb_class_new_instance (4, argv, cPos));
100         rb_iv_set (self, "@button", INT2FIX (e->button));
101
102         return self;
103 }
104
105 static VALUE c_ev_mouse_move_init (VALUE self, VALUE ev)
106 {
107         VALUE argv[4];
108         Evas_Event_Mouse_Move *e = (Evas_Event_Mouse_Move *) ev;
109
110         argv[0] = INT2FIX ((int) e->cur.output.x);
111         argv[1] = INT2FIX ((int) e->cur.output.y);
112         argv[2] = INT2FIX ((int) e->cur.canvas.x);
113         argv[3] = INT2FIX ((int) e->cur.canvas.y);
114
115         rb_iv_set (self, "@current",
116                    rb_class_new_instance (4, argv, cPos));
117
118         argv[0] = INT2FIX ((int) e->prev.output.x);
119         argv[1] = INT2FIX ((int) e->prev.output.y);
120         argv[2] = INT2FIX ((int) e->prev.canvas.x);
121         argv[3] = INT2FIX ((int) e->prev.canvas.y);
122
123         rb_iv_set (self, "@previous",
124                    rb_class_new_instance (4, argv, cPos));
125         rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
126
127         return self;
128 }
129
130 CALLBACK_HANDLER_FUNC (mouse_down);
131 CALLBACK_HANDLER_FUNC (mouse_up);
132 CALLBACK_HANDLER_FUNC (mouse_move);
133
134 static VALUE c_on_mouse_down (VALUE self)
135 {
136         CALLBACK_HANDLER_METHOD (mouse_down, MOUSE_DOWN);
137 }
138
139 static VALUE c_on_mouse_up (VALUE self)
140 {
141         CALLBACK_HANDLER_METHOD (mouse_up, MOUSE_UP);
142 }
143
144 static VALUE c_on_mouse_move (VALUE self)
145 {
146         CALLBACK_HANDLER_METHOD (mouse_move, MOUSE_MOVE);
147 }
148
149 static VALUE c_ev_init (VALUE self)
150 {
151         return self;
152 }
153
154 static VALUE c_pos_init (VALUE self, VALUE output_x, VALUE output_y,
155                          VALUE canvas_x, VALUE canvas_y)
156 {
157         rb_iv_set (self, "@output_x", output_x);
158         rb_iv_set (self, "@output_y", output_y);
159         rb_iv_set (self, "@canvas_x", canvas_x);
160         rb_iv_set (self, "@canvas_y", canvas_y);
161
162         return self;
163 }
164
165 void Init_EvasObjectEvents (void)
166 {
167         VALUE cEvent, c;
168
169         event_classes = rb_hash_new ();
170         rb_global_variable (&event_classes);
171
172         cEvent = rb_define_class_under (mEvas, "EvasObjectEvent", rb_cObject);
173         rb_define_private_method (rb_singleton_class (cEvent), "new", NULL, 0);
174         rb_define_private_method (cEvent, "initialize", c_ev_init, 0);
175
176         cPos = rb_define_class_under (cEvent, "Position", rb_cObject);
177         rb_define_private_method (rb_singleton_class (cPos), "new", NULL, 0);
178         rb_define_private_method (cPos, "initialize", c_pos_init, 4);
179         rb_define_attr (cPos, "output_x", 1, 0);
180         rb_define_attr (cPos, "output_y", 1, 0);
181         rb_define_attr (cPos, "canvas_x", 1, 0);
182         rb_define_attr (cPos, "canvas_y", 1, 0);
183
184         CALLBACK_REGISTER (mouse_down, "MouseDownEvent");
185         rb_define_attr (c, "button", 1, 0);
186
187         CALLBACK_REGISTER (mouse_up, "MouseUpEvent");
188         rb_define_attr (c, "button", 1, 0);
189
190         CALLBACK_REGISTER (mouse_move, "MouseMoveEvent");
191         rb_define_attr (c, "current", 1, 0);
192         rb_define_attr (c, "previous", 1, 0);
193         rb_define_attr (c, "buttons", 1, 0);
194 }