Added Part object.
[ruby-edje.git] / src / rb_edje.c
index 6e32bf669572afdf3dd88296b58709f2b509fe1e..6cc37f9508a39e63fb79915a22bbcea5ecaf3864 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: rb_edje.c 21 2004-06-25 17:41:17Z tilman $
+ * $Id: rb_edje.c 47 2004-07-26 13:24:50Z tilman $
  *
  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
  *
 #include <rb_evas.h>
 #include <rb_evas_object.h>
 
+#define __RB_EDJE_C
 #include "rb_edje_main.h"
+#include "rb_part.h"
 
-#define GET_OBJ(obj, type, o, desc) \
-       type **(o) = NULL; \
-\
-       Data_Get_Struct ((obj), type *, (o)); \
-\
-       if (!*(o)) { \
-               rb_raise (rb_eException, desc " destroyed already"); \
-               return Qnil; \
-       }
+VALUE cEdje;
+static VALUE part_hashes;
 
-#define CHECK_BOOL(val) \
-       if (TYPE ((val)) != T_TRUE && TYPE ((val)) != T_FALSE) { \
-               rb_raise (rb_eTypeError, \
-                         "wrong argument type %s (expected true or false)", \
-                         rb_obj_classname ((val))); \
-               return Qnil; \
-       }
+static void c_free (Evas_Object **e)
+{
+       c_evas_object_free (e);
 
-VALUE cEdje;
+       rb_hash_aset (part_hashes, INT2NUM ((long) e), Qnil);
+
+       edje_shutdown ();
+}
 
 static VALUE c_new (VALUE klass, VALUE evas)
 {
        VALUE self, argv[1];
        Evas_Object **edje;
 
-       GET_OBJ (evas, Evas, e, "Evas");
+       CHECK_CLASS (evas, cEvas);
+       GET_OBJ (evas, Evas *, e);
+
+       edje_init ();
 
        self = Data_Make_Struct (klass, Evas_Object *, c_evas_object_mark,
-                                c_evas_object_free, edje);
+                                c_free, edje);
        *edje = edje_object_add (*e);
 
        argv[0] = evas;
@@ -65,7 +62,7 @@ static VALUE c_new (VALUE klass, VALUE evas)
 
 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
 {
-       GET_OBJ (self, Evas_Object, e, "Edje");
+       GET_OBJ (self, Evas_Object *, e);
 
        Check_Type (eet, T_STRING);
        Check_Type (group, T_STRING);
@@ -81,7 +78,7 @@ static VALUE c_get_size_min (VALUE self)
 {
        int w = 0, h = 0;
 
-       GET_OBJ (self, Evas_Object, e, "Edje");
+       GET_OBJ (self, Evas_Object *, e);
 
        edje_object_size_min_get (*e, &w, &h);
 
@@ -92,109 +89,49 @@ static VALUE c_get_size_max (VALUE self)
 {
        int w = 0, h = 0;
 
-       GET_OBJ (self, Evas_Object, e, "Edje");
+       GET_OBJ (self, Evas_Object *, e);
 
        edje_object_size_max_get (*e, &w, &h);
 
        return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
 }
 
-static VALUE c_part_exists_get (VALUE self, VALUE part)
+static VALUE c_part_exists_get (VALUE self, VALUE name)
 {
        int r;
 
-       GET_OBJ (self, Evas_Object, e, "Edje");
+       GET_OBJ (self, Evas_Object *, e);
 
-       Check_Type (part, T_STRING);
+       Check_Type (name, T_STRING);
 
-       r = edje_object_part_exists (*e, StringValuePtr (part));
+       r = edje_object_part_exists (*e, StringValuePtr (name));
 
        return r ? Qtrue : Qfalse;
 }
 
-static VALUE c_part_swallow (VALUE self, VALUE part, VALUE target)
+static VALUE c_part_get (VALUE self, VALUE name)
 {
-       GET_OBJ (self, Evas_Object, e, "Edje");
-
-       Check_Type (part, T_STRING);
-
-       if (!rb_obj_is_kind_of (target, cEvasObject)) {
-               rb_raise (rb_eTypeError,
-                         "wrong argument type %s (expected EvasObject)",
-                         rb_obj_classname (target));
-               return Qnil;
-       }
+       VALUE parts, part;
 
-       GET_OBJ (target, Evas_Object, target2, "EvasObject");
+       GET_OBJ (self, Evas_Object *, e);
 
-       edje_object_part_swallow (*e, StringValuePtr (part), *target2);
+       Check_Type (name, T_STRING);
 
-       return Qnil;
-}
-
-static VALUE c_part_unswallow (VALUE self, VALUE target)
-{
-       GET_OBJ (self, Evas_Object, e, "Edje");
-
-       if (!rb_obj_is_kind_of (target, cEvasObject)) {
-               rb_raise (rb_eTypeError,
-                         "wrong argument type %s (expected EvasObject)",
-                         rb_obj_classname (target));
+       if (!edje_object_part_exists (*e, StringValuePtr (name)))
                return Qnil;
-       }
 
-       GET_OBJ (target, Evas_Object, target2, "EvasObject");
-
-       edje_object_part_unswallow (*e, *target2);
-
-       return Qnil;
-}
-
-static VALUE c_get_part_swallow (VALUE self, VALUE part)
-{
-       Evas_Object *o;
-       void *obj;
-
-       GET_OBJ (self, Evas_Object, e, "Edje");
-
-       Check_Type (part, T_STRING);
-
-       if (!(o = edje_object_part_swallow_get (*e, StringValuePtr (part))))
-               return Qnil;
-
-       if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
-               rb_raise (rb_eException, "EvasObject Ruby object key missing");
-               return Qnil;
+       parts = rb_hash_aref (part_hashes, INT2NUM ((long) (e)));
+       if (NIL_P (parts)) {
+               parts = rb_hash_new ();
+               rb_hash_aset (part_hashes, INT2NUM ((long) e), parts);
        }
 
-       return (VALUE) obj;
-}
-
-static VALUE c_get_part_text (VALUE self, VALUE part)
-{
-       const char *s;
-
-       GET_OBJ (self, Evas_Object, e, "Edje");
-
-       Check_Type (part, T_STRING);
-
-       if (!(s = edje_object_part_text_get (*e, StringValuePtr (part))))
-               return Qnil;
-       else
-               return rb_str_new2 (s);
-}
-
-static VALUE c_set_part_text (VALUE self, VALUE part, VALUE text)
-{
-       GET_OBJ (self, Evas_Object, e, "Edje");
-
-       Check_Type (part, T_STRING);
-       Check_Type (text, T_STRING);
-
-       edje_object_part_text_set (*e, StringValuePtr (part),
-                                  StringValuePtr (text));
+       if (NIL_P (part = rb_hash_aref (parts, name))) {
+               part = TO_PART (self, name);
+               rb_hash_aset (parts, name, part);
+       }
 
-       return Qnil;
+       return part;
 }
 
 static void on_text_changed (void *data, Evas_Object *e,
@@ -206,7 +143,7 @@ static void on_text_changed (void *data, Evas_Object *e,
 
 static VALUE c_on_text_changed (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "Edje");
+       GET_OBJ (self, Evas_Object *, e);
 
        if (!rb_block_given_p ())
                return Qnil;
@@ -219,7 +156,7 @@ static VALUE c_on_text_changed (VALUE self)
 
 static VALUE c_signal_emit (VALUE self, VALUE emission, VALUE source)
 {
-       GET_OBJ (self, Evas_Object, e, "Edje");
+       GET_OBJ (self, Evas_Object *, e);
 
        Check_Type (emission, T_STRING);
        Check_Type (source, T_STRING);
@@ -231,14 +168,15 @@ static VALUE c_signal_emit (VALUE self, VALUE emission, VALUE source)
 }
 
 static void on_signal (void *data, Evas_Object *o,
-                       const char *emission, const char *src) {
+                       const char *emission, const char *src)
+{
        rb_funcall ((VALUE) data, rb_intern ("call"), 1,
                    rb_str_new2 (emission), rb_str_new2 (src));
 }
 
 static VALUE c_on_signal (VALUE self, VALUE signal, VALUE src)
 {
-       GET_OBJ (self, Evas_Object, e, "Edje");
+       GET_OBJ (self, Evas_Object *, e);
 
        Check_Type (signal, T_STRING);
        Check_Type (src, T_STRING);
@@ -255,36 +193,36 @@ static VALUE c_on_signal (VALUE self, VALUE signal, VALUE src)
 
 static VALUE c_play_get (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "Edje");
+       GET_OBJ (self, Evas_Object *, e);
 
        return edje_object_play_get (*e) ? Qtrue : Qfalse;
 }
 
 static VALUE c_play_set (VALUE self, VALUE val)
 {
-       GET_OBJ (self, Evas_Object, e, "Edje");
+       GET_OBJ (self, Evas_Object *, e);
 
        CHECK_BOOL(val);
 
-       edje_object_play_set (*e, val == Qtrue ? 1 : 0);
+       edje_object_play_set (*e, val == Qtrue);
 
        return Qnil;
 }
 
 static VALUE c_animation_get (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "Edje");
+       GET_OBJ (self, Evas_Object *, e);
 
        return edje_object_animation_get (*e) ? Qtrue : Qfalse;
 }
 
 static VALUE c_animation_set (VALUE self, VALUE val)
 {
-       GET_OBJ (self, Evas_Object, e, "Edje");
+       GET_OBJ (self, Evas_Object *, e);
 
        CHECK_BOOL(val);
 
-       edje_object_animation_set (*e, val == Qtrue ? 1 : 0);
+       edje_object_animation_set (*e, val == Qtrue);
 
        return Qnil;
 }
@@ -298,11 +236,7 @@ void Init_Edje (void)
        rb_define_method (cEdje, "get_size_min", c_get_size_min, 0);
        rb_define_method (cEdje, "get_size_max", c_get_size_max, 0);
        rb_define_method (cEdje, "part_exists?", c_part_exists_get, 1);
-       rb_define_method (cEdje, "part_swallow", c_part_swallow, 2);
-       rb_define_method (cEdje, "part_unswallow", c_part_unswallow, 1);
-       rb_define_method (cEdje, "get_part_swallow", c_get_part_swallow, 1);
-       rb_define_method (cEdje, "get_part_text", c_get_part_text, 1);
-       rb_define_method (cEdje, "set_part_text", c_set_part_text, 2);
+       rb_define_method (cEdje, "part", c_part_get, 1);
        rb_define_method (cEdje, "on_text_changed", c_on_text_changed, 0);
        rb_define_method (cEdje, "signal_emit", c_signal_emit, 1);
        rb_define_method (cEdje, "on_signal", c_on_signal, 2);
@@ -310,5 +244,7 @@ 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);
-}
 
+       part_hashes = rb_hash_new ();
+       rb_global_variable (&part_hashes);
+}