Started EvasObject callback implementation.
[ruby-evas.git] / src / rb_evas_object_events.c
diff --git a/src/rb_evas_object_events.c b/src/rb_evas_object_events.c
new file mode 100644 (file)
index 0000000..2ac56fa
--- /dev/null
@@ -0,0 +1,129 @@
+/*
+ * $Id: rb_evas_object_events.c 281 2005-03-14 20:51:40Z tilman $
+ *
+ * Copyright (C) 2005 Tilman Sauerbeck (tilman at code-monkey de)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <ruby.h>
+#include <stdbool.h>
+
+#include <Evas.h>
+
+#include "rb_evas_main.h"
+#include "rb_evas.h"
+#include "rb_evas_object.h"
+
+#define CALLBACK_HANDLER_FUNC(name) \
+       static void on_##name (void *data, Evas *evas, \
+                              Evas_Object *evas_obj, void *event) \
+{ \
+       RbEvasObject *e = (RbEvasObject *) data; \
+       VALUE argv[1] = {(VALUE) event}, klass, cb, ev, s; \
+\
+       s = rb_str_new2 (#name); \
+       klass = rb_hash_aref (event_classes, s); \
+       ev = rb_class_new_instance(1, argv, klass); \
+       cb = rb_hash_aref (e->callbacks, s); \
+\
+       rb_funcall (cb, rb_intern ("call"), 1, ev); \
+}
+
+#define CALLBACK_HANDLER_METHOD(name, callback) \
+       GET_OBJ (self, RbEvasObject, e); \
+\
+       if (!rb_block_given_p ()) \
+               return Qnil; \
+\
+       rb_hash_aset (e->callbacks, rb_str_new2 (#name), \
+                     rb_block_proc ()); \
+       evas_object_event_callback_add (e->real, EVAS_CALLBACK_##callback, \
+                                       on_##name, e); \
+\
+       return Qnil;
+
+#define CALLBACK_REGISTER(name, clsname) \
+       rb_define_method (cEvasObject, "on_"#name, c_on_##name, 0); \
+\
+       c = rb_define_class_under (mEvas, (clsname), rb_cObject); \
+       rb_define_private_method (rb_singleton_class (c), "new", NULL, 0); \
+       rb_define_private_method (c, "initialize", c_ev_##name##_init, 1); \
+       rb_hash_aset (event_classes, rb_str_new2 (#name), c);
+
+static VALUE event_classes;
+
+static VALUE c_ev_mouse_down_init (VALUE self, VALUE ev)
+{
+       Evas_Event_Mouse_Down *e = (Evas_Event_Mouse_Down *) ev;
+
+       rb_iv_set (self, "@button", INT2FIX (e->button));
+
+       return self;
+}
+
+static VALUE c_ev_mouse_up_init (VALUE self, VALUE ev)
+{
+       Evas_Event_Mouse_Up *e = (Evas_Event_Mouse_Up *) ev;
+
+       rb_iv_set (self, "@button", INT2FIX (e->button));
+
+       return self;
+}
+
+static VALUE c_ev_mouse_move_init (VALUE self, VALUE ev)
+{
+       Evas_Event_Mouse_Move *e = (Evas_Event_Mouse_Move *) ev;
+
+       rb_iv_set (self, "@buttons", INT2FIX (e->buttons));
+
+       return self;
+}
+
+CALLBACK_HANDLER_FUNC (mouse_down);
+CALLBACK_HANDLER_FUNC (mouse_up);
+CALLBACK_HANDLER_FUNC (mouse_move);
+
+static VALUE c_on_mouse_down (VALUE self)
+{
+       CALLBACK_HANDLER_METHOD (mouse_down, MOUSE_DOWN);
+}
+
+static VALUE c_on_mouse_up (VALUE self)
+{
+       CALLBACK_HANDLER_METHOD (mouse_up, MOUSE_UP);
+}
+
+static VALUE c_on_mouse_move (VALUE self)
+{
+       CALLBACK_HANDLER_METHOD (mouse_move, MOUSE_MOVE);
+}
+
+void Init_EvasObjectEvents (void)
+{
+       VALUE c;
+
+       event_classes = rb_hash_new ();
+       rb_global_variable (&event_classes);
+
+       CALLBACK_REGISTER (mouse_down, "MouseDownEvent");
+       rb_define_attr (c, "button", 1, 0);
+
+       CALLBACK_REGISTER (mouse_up, "MouseUpEvent");
+       rb_define_attr (c, "button", 1, 0);
+
+       CALLBACK_REGISTER (mouse_move, "MouseMoveEvent");
+       rb_define_attr (c, "buttons", 1, 0);
+}