2ac56fa597bee9366a63a5c4d4a69183a33b7be6
[ruby-evas.git] / src / rb_evas_object_events.c
1 /*
2  * $Id: rb_evas_object_events.c 281 2005-03-14 20:51:40Z 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         rb_hash_aset (e->callbacks, rb_str_new2 (#name), \
52                       rb_block_proc ()); \
53         evas_object_event_callback_add (e->real, EVAS_CALLBACK_##callback, \
54                                         on_##name, e); \
55 \
56         return Qnil;
57
58 #define CALLBACK_REGISTER(name, clsname) \
59         rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0); \
60 \
61         c = rb_define_class_under (mEvas, (clsname), rb_cObject); \
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);
65
66 static VALUE event_classes;
67
68 static VALUE c_ev_mouse_down_init (VALUE self, VALUE ev)
69 {
70         Evas_Event_Mouse_Down *e = (Evas_Event_Mouse_Down *) ev;
71
72         rb_iv_set (self, "@button", INT2FIX (e->button));
73
74         return self;
75 }
76
77 static VALUE c_ev_mouse_up_init (VALUE self, VALUE ev)
78 {
79         Evas_Event_Mouse_Up *e = (Evas_Event_Mouse_Up *) ev;
80
81         rb_iv_set (self, "@button", INT2FIX (e->button));
82
83         return self;
84 }
85
86 static VALUE c_ev_mouse_move_init (VALUE self, VALUE ev)
87 {
88         Evas_Event_Mouse_Move *e = (Evas_Event_Mouse_Move *) ev;
89
90         rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
91
92         return self;
93 }
94
95 CALLBACK_HANDLER_FUNC (mouse_down);
96 CALLBACK_HANDLER_FUNC (mouse_up);
97 CALLBACK_HANDLER_FUNC (mouse_move);
98
99 static VALUE c_on_mouse_down (VALUE self)
100 {
101         CALLBACK_HANDLER_METHOD (mouse_down, MOUSE_DOWN);
102 }
103
104 static VALUE c_on_mouse_up (VALUE self)
105 {
106         CALLBACK_HANDLER_METHOD (mouse_up, MOUSE_UP);
107 }
108
109 static VALUE c_on_mouse_move (VALUE self)
110 {
111         CALLBACK_HANDLER_METHOD (mouse_move, MOUSE_MOVE);
112 }
113
114 void Init_EvasObjectEvents (void)
115 {
116         VALUE c;
117
118         event_classes = rb_hash_new ();
119         rb_global_variable (&event_classes);
120
121         CALLBACK_REGISTER (mouse_down, "MouseDownEvent");
122         rb_define_attr (c, "button", 1, 0);
123
124         CALLBACK_REGISTER (mouse_up, "MouseUpEvent");
125         rb_define_attr (c, "button", 1, 0);
126
127         CALLBACK_REGISTER (mouse_move, "MouseMoveEvent");
128         rb_define_attr (c, "buttons", 1, 0);
129 }