Duck-typify message handling.
[ruby-edje.git] / src / rb_edje.c
index 4b76cce71a771229b01f52254012c3852ecf2224..1202b795d20bc953232823ef3f4e6bc57b81d2c7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: rb_edje.c 96 2004-08-23 15:21:41Z tilman $
+ * $Id: rb_edje.c 330 2005-04-28 15:38:47Z tilman $
  *
  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
  *
 #include "rb_part.h"
 
 VALUE cEdje;
+static VALUE cMsg, eEdjeError;
 
 static void c_mark (RbEdje *e)
 {
        c_evas_object_mark (&e->real);
 
-       rb_gc_mark (e->parts);
-       rb_gc_mark (e->callbacks);
+       if (!NIL_P (e->parts))
+               rb_gc_mark (e->parts);
+
+       if (!NIL_P (e->callbacks))
+               rb_gc_mark (e->callbacks);
 
        if (!NIL_P (e->on_text_changed_cb))
                rb_gc_mark (e->on_text_changed_cb);
@@ -45,6 +49,7 @@ static void c_mark (RbEdje *e)
 static void c_free (RbEdje *e)
 {
        c_evas_object_free (&e->real, false);
+       free (e);
 
        edje_shutdown ();
 }
@@ -68,8 +73,8 @@ static VALUE c_new (VALUE klass, VALUE evas)
        self = Data_Make_Struct (klass, RbEdje, c_mark, c_free, edje);
 
        edje->real.real = edje_object_add (e->real);
-       edje->parts = rb_hash_new ();
-       edje->callbacks = rb_ary_new ();
+       edje->parts = Qnil;
+       edje->callbacks = Qnil;
        edje->on_text_changed_cb = Qnil;
 
        argv[0] = evas;
@@ -196,15 +201,18 @@ static VALUE c_part_exists_get (VALUE self, VALUE name)
 static VALUE c_part_get (VALUE self, VALUE name)
 {
        VALUE part;
+       const char *cname = StringValuePtr (name);
 
        GET_OBJ (self, RbEdje, e);
-       Check_Type (name, T_STRING);
 
-       if (!edje_object_part_exists (e->real.real, StringValuePtr (name))) {
-               rb_raise (rb_eException, "Unknown part name");
+       if (!edje_object_part_exists (e->real.real, cname)) {
+               rb_raise (rb_eException, "Unknown part name - %s", cname);
                return Qnil;
        }
 
+       if (NIL_P (e->parts))
+               e->parts = rb_hash_new ();
+
        if (NIL_P (part = rb_hash_aref (e->parts, name))) {
                part = TO_PART (self, name);
                rb_hash_aset (e->parts, name, part);
@@ -222,6 +230,9 @@ static void on_text_changed (void *data, Evas_Object *eo,
 
        name = rb_str_new2 (part_name);
 
+       if (NIL_P (e->parts))
+               e->parts = rb_hash_new ();
+
        if (NIL_P (part = rb_hash_aref (e->parts, name))) {
                part = TO_PART (self, name);
                rb_hash_aset (e->parts, name, part);
@@ -313,6 +324,10 @@ static VALUE c_on_signal (int argc, VALUE *argv, VALUE self)
                return Qnil;
 
        cb = rb_block_proc ();
+
+       if (NIL_P (e->callbacks))
+               e->callbacks = rb_ary_new ();
+
        rb_ary_push (e->callbacks, cb);
 
        edje_object_signal_callback_add (e->real.real,
@@ -382,6 +397,74 @@ static VALUE c_animation_set (VALUE self, VALUE val)
        return Qnil;
 }
 
+static VALUE c_data_get (VALUE self, VALUE key)
+{
+       const char *s;
+
+       GET_OBJ (self, RbEdje, e);
+
+       Check_Type (key, T_STRING);
+
+       s = edje_object_data_get (e->real.real, StringValuePtr (key));
+
+       return s ? rb_str_new2 (s) : Qnil;
+}
+
+static VALUE c_send_message (VALUE self, VALUE msg)
+{
+       Edje_Message_String s;
+       Edje_Message_Int i;
+       Edje_Message_Float f;
+       Edje_Message_Type type;
+       void *data;
+       VALUE v, tmp;
+
+       GET_OBJ (self, RbEdje, e);
+
+       CHECK_CLASS (msg, cMsg);
+
+       v = rb_iv_get (msg, "@value");
+
+       if (NIL_P (v)) {
+               rb_raise (eEdjeError, "value must not be nil");
+               return Qnil;
+       }
+
+       if (!NIL_P (tmp = rb_check_string_type (v))) {
+               type = EDJE_MESSAGE_STRING;
+               s.str = StringValuePtr (tmp);
+               data = &s;
+       } else if (rb_obj_is_kind_of (v, rb_cFixnum)) {
+               type = EDJE_MESSAGE_INT;
+               i.val = FIX2INT (v);
+               data = &i;
+       } else if (rb_obj_is_kind_of (v, rb_cFloat)) {
+               type = EDJE_MESSAGE_FLOAT;
+               f.val = NUM2DBL (v);
+               data = &v;
+       /* } else if (!NIL_P (tmp = rb_check_array_type (v))) { */
+       } else {
+               rb_raise (eEdjeError, "unsupported value");
+       }
+
+       edje_object_message_send (e->real.real, type,
+                                 NUM2INT (rb_iv_get (msg, "@id")), data);
+
+       return Qnil;
+}
+
+static VALUE c_msg_init (int argc, VALUE *argv, VALUE self)
+{
+       VALUE val;
+
+       rb_scan_args (argc, argv, "01", &val);
+
+       rb_iv_set (self, "@id", UINT2NUM (0));
+       rb_iv_set (self, "@value", val);
+
+       return self;
+}
+
 void Init_Edje (void)
 {
        cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
@@ -401,4 +484,16 @@ void Init_Edje (void)
        rb_define_method (cEdje, "play=", c_play_set, 1);
        rb_define_method (cEdje, "animation?", c_animation_get, 0);
        rb_define_method (cEdje, "animation=", c_animation_set, 1);
+       rb_define_method (cEdje, "data", c_data_get, 1);
+       rb_define_method (cEdje, "send_message", c_send_message, 1);
+
+       cMsg = rb_define_class_under (mEdje, "Message", rb_cObject);
+
+       rb_define_method (cMsg, "initialize", c_msg_init, -1);
+
+       rb_define_attr (cMsg, "id", 1, 1);
+       rb_define_attr (cMsg, "value", 1, 1);
+
+       eEdjeError = rb_define_class_under (mEdje, "EdjeError",
+                                          rb_eStandardError);
 }