Added Ecore::Evas::EcoreEvas#has_alpha? and #has_alpha=.
[ruby-ecore.git] / src / ecore / rb_event_handler.c
index 63c501632750095f17510e3169e6d1e65f450418..39e2521c8abfd643503f890ebba8e19f392c9e36 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: rb_event_handler.c 77 2004-08-19 17:39:29Z tilman $
+ * $Id: rb_event_handler.c 373 2006-02-16 21:37:43Z tilman $
  *
  * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
  *
 #include "rb_ecore.h"
 #include "rb_event_handler.h"
 
+typedef struct {
+       Ecore_Event_Handler *real;
+       VALUE event_class;
+       VALUE callback;
+       bool deleted;
+} RbEventHandler;
+
+static int on_ecore_event (void *data, int type, void *event);
+static VALUE c_ev_raise (VALUE klass, VALUE argv);
+
 VALUE event_classes, cEcoreEvent;
-static VALUE handlers;
+static VALUE event_args;
+
+static void c_mark (RbEventHandler *h)
+{
+       rb_gc_mark (h->callback);
+}
 
-static VALUE c_init (VALUE self, VALUE type)
+static void c_free (RbEventHandler *h)
 {
+       if (h->real && !h->deleted)
+               ecore_event_handler_del (h->real);
+
+       free (h);
+}
+
+static VALUE c_alloc (VALUE klass)
+{
+       RbEventHandler *h = NULL;
+
+       return Data_Make_Struct (klass, RbEventHandler, c_mark, c_free, h);
+}
+
+static VALUE c_init (VALUE self, VALUE klass)
+{
+       RbEventHandler *h = NULL;
        int t;
 
        if (!rb_block_given_p ())
-               return Qnil;
+               rb_raise (rb_eStandardError, "block missing");
 
-       t = NUM2INT (type);
+       Data_Get_Struct (self, RbEventHandler, h);
 
-       if (t <= ECORE_EVENT_NONE)
-               return Qnil;
+       if (rb_obj_is_kind_of (klass, rb_cModule) != Qtrue)
+               rb_raise (rb_eArgError, "invalid argument");
 
-       rb_iv_set (self, "@type", type);
-       rb_iv_set (self, "handler", rb_block_proc ());
+       t = NUM2INT (rb_const_get (klass, rb_intern ("TYPE")));
+       if (t <= ECORE_EVENT_NONE)
+               rb_raise (rb_eStandardError, "invalid event");
 
-       rb_ary_push (handlers, self);
+       h->event_class = klass;
+       h->callback = rb_block_proc ();
+       h->deleted = false;
+       h->real = ecore_event_handler_add (t, on_ecore_event, h);
 
        return self;
 }
 
 static VALUE c_delete (VALUE self)
 {
-       int len = RARRAY (handlers)->len, i;
-       VALUE el;
+       RbEventHandler *h = NULL;
 
-       for (i = 0; i < len; i++) {
-               el = rb_ary_shift (handlers);
-               if (el == self)
-                       return Qnil;
+       Data_Get_Struct (self, RbEventHandler, h);
 
-               rb_ary_push (handlers, el);
-       }
+       if (h->real && !h->deleted) {
+               ecore_event_handler_del (h->real);
+               h->real = NULL;
+               h->deleted = true;
+       } else
+               rb_raise (rb_eException, "EventHandler already deleted!");
 
        return Qnil;
 }
 
-int on_ecore_event (void *data, int type, void *event)
+static int on_ecore_event (void *data, int type, void *event)
 {
-       VALUE handler, klass, obj, argv[1], res;
-       int handler_type, len, i;
-       bool called = false;
+       RbEventHandler *h = data;
+       VALUE obj, tmp = (VALUE) event;
 
-       /* instantiate the event object
-        * first, find the class we're gonna use
+       obj = rb_obj_alloc (h->event_class);
+
+       if (rb_respond_to (h->event_class, rb_intern ("raise")))
+               rb_apply (obj, rb_intern ("initialize"), tmp);
+       else
+               rb_obj_call_init (obj, 1, &tmp);
+
+       tmp = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
+
+       /* if the block returned false, don't call the other
+        * event handlers
         */
-       if (NIL_P (klass = rb_hash_aref (event_classes, INT2NUM (type)))) {
-               rb_raise (rb_eException, "Cannot find event class "
-                                        "for event %i\n", type);
-               return 0;
-       }
-
-       /* now create and init the object */
-       obj = rb_obj_alloc (klass);
-       argv[0] = (VALUE) event;
-       rb_obj_call_init (obj, 1, argv);
-
-       len = RARRAY (handlers)->len;
-
-       for (i = 0; i < len; i++) {
-               handler = rb_ary_entry (handlers, i);
-               handler_type = NUM2INT (rb_iv_get (handler, "@type"));
-
-               if (handler_type == type) {
-                       res = rb_funcall (rb_iv_get (handler, "handler"),
-                                         rb_intern ("call"), 1, obj);
-                       called = true;
-
-                       /* if the block returned false, don't call the other
-                        * event handlers
-                        */
-                       if (res == Qfalse)
-                               break;
-               }
-       }
-
-       if (type == ECORE_EVENT_SIGNAL_EXIT && !called)
-               ecore_main_loop_quit ();
-
-       return 0;
+       return (tmp != Qfalse);
 }
 
-VALUE c_ev_generic_init (VALUE self, VALUE event)
+VALUE c_ev_inherited (VALUE klass, VALUE child)
+{
+       VALUE t;
+
+       t = INT2FIX (ecore_event_type_new ());
+       rb_hash_aset (event_classes, t, child);
+
+       rb_define_const (child, "TYPE", t);
+       rb_define_singleton_method (child, "raise", c_ev_raise, -2);
+
+       return Qnil;
+}
+
+static VALUE c_ev_init (int argc, VALUE *argv, VALUE self)
 {
-       /* dummy */
        return self;
 }
 
+static void free_event_args (void *data, void *argv)
+{
+       rb_ary_delete (event_args, (VALUE) argv);
+}
+
+static VALUE c_ev_raise (VALUE klass, VALUE argv)
+{
+       VALUE t;
+
+       rb_ary_push (event_args, argv);
+
+       t = rb_const_get (klass, rb_intern ("TYPE"));
+       ecore_event_add (FIX2INT (t), (void *) argv, free_event_args, NULL);
+
+       return Qnil;
+}
+
 void Init_EventHandler (void)
 {
        VALUE cEventHandler;
@@ -124,17 +163,18 @@ void Init_EventHandler (void)
        cEventHandler = rb_define_class_under (mEcore, "EventHandler",
                                               rb_cObject);
 
+       rb_define_alloc_func (cEventHandler, c_alloc);
        rb_define_method (cEventHandler, "initialize", c_init, 1);
        rb_define_method (cEventHandler, "delete", c_delete, 0);
 
-       handlers = rb_ary_new ();
-       rb_global_variable (&handlers);
-
        event_classes = rb_hash_new ();
        rb_global_variable (&event_classes);
 
+       event_args = rb_ary_new ();
+       rb_global_variable (&event_args);
+
        /* define a base event class */
        cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
-       rb_define_private_method (rb_singleton_class (cEcoreEvent),
-                                 "new", NULL, 0);
+       rb_define_singleton_method (cEcoreEvent, "inherited", c_ev_inherited, 1);
+       rb_define_method (cEcoreEvent, "initialize", c_ev_init, -1);
 }