No need to use a global variable for the Ruby class.
[ruby-edje.git] / src / rb_edje.c
index 37b099c12694189c85287e835985c8814103e742..483067c7c76142ddc7e75d4f614a8a9fa2742b83 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id$
+ * $Id: rb_edje.c 24 2004-06-26 22:59:56Z tilman $
  *
  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
  *
                return Qnil; \
        }
 
-VALUE cEdje;
+#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 VALUE c_new (VALUE klass, VALUE evas)
 {
        VALUE self, argv[1];
        Evas_Object **edje;
 
+       if (!rb_obj_is_kind_of (evas, cEvas)) {
+               rb_raise (rb_eTypeError,
+                         "wrong argument type %s (expected Evas)",
+                         rb_obj_classname (evas));
+               return Qnil;
+       }
+
        GET_OBJ (evas, Evas, e, "Evas");
 
        self = Data_Make_Struct (klass, Evas_Object *, c_evas_object_mark,
@@ -57,22 +70,249 @@ static VALUE c_new (VALUE klass, VALUE evas)
 
 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, Evas_Object, e, "Edje");
 
        Check_Type (eet, T_STRING);
        Check_Type (group, T_STRING);
 
-       if (!edje_object_file_set (*e, STR2CSTR (eet), STR2CSTR (group)))
+       if (!edje_object_file_set (*e, StringValuePtr (eet),
+                                  StringValuePtr (group)))
                rb_raise (rb_eException, "Cannot load eet");
 
        return Qnil;
 }
 
+static VALUE c_get_size_min (VALUE self)
+{
+       int w = 0, h = 0;
+
+       GET_OBJ (self, Evas_Object, e, "Edje");
+
+       edje_object_size_min_get (*e, &w, &h);
+
+       return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
+}
+
+static VALUE c_get_size_max (VALUE self)
+{
+       int w = 0, h = 0;
+
+       GET_OBJ (self, Evas_Object, e, "Edje");
+
+       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)
+{
+       int r;
+
+       GET_OBJ (self, Evas_Object, e, "Edje");
+
+       Check_Type (part, T_STRING);
+
+       r = edje_object_part_exists (*e, StringValuePtr (part));
+
+       return r ? Qtrue : Qfalse;
+}
+
+static VALUE c_part_swallow (VALUE self, VALUE part, VALUE target)
+{
+       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;
+       }
+
+       GET_OBJ (target, Evas_Object, target2, "EvasObject");
+
+       edje_object_part_swallow (*e, StringValuePtr (part), *target2);
+
+       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));
+               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;
+       }
+
+       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));
+
+       return Qnil;
+}
+
+static void on_text_changed (void *data, Evas_Object *e,
+                             const char *part)
+{
+       rb_funcall ((VALUE) data, rb_intern ("call"), 1,
+                   rb_str_new2 (part));
+}
+
+static VALUE c_on_text_changed (VALUE self)
+{
+       GET_OBJ (self, Evas_Object, e, "Edje");
+
+       if (!rb_block_given_p ())
+               return Qnil;
+
+       edje_object_text_change_cb_set (*e, on_text_changed,
+                                       (void *) rb_block_proc ());
+
+       return Qnil;
+}
+
+static VALUE c_signal_emit (VALUE self, VALUE emission, VALUE source)
+{
+       GET_OBJ (self, Evas_Object, e, "Edje");
+
+       Check_Type (emission, T_STRING);
+       Check_Type (source, T_STRING);
+
+       edje_object_signal_emit (*e, StringValuePtr (emission),
+                                StringValuePtr (source));
+
+       return Qnil;
+}
+
+static void on_signal (void *data, Evas_Object *o,
+                       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");
+
+       Check_Type (signal, T_STRING);
+       Check_Type (src, T_STRING);
+
+       if (!rb_block_given_p ())
+               return Qnil;
+
+       edje_object_signal_callback_add (*e, StringValuePtr (signal),
+                                        StringValuePtr (src), on_signal,
+                                        (void *) rb_block_proc ());
+
+       return Qnil;
+}
+
+static VALUE c_play_get (VALUE self)
+{
+       GET_OBJ (self, Evas_Object, e, "Edje");
+
+       return edje_object_play_get (*e) ? Qtrue : Qfalse;
+}
+
+static VALUE c_play_set (VALUE self, VALUE val)
+{
+       GET_OBJ (self, Evas_Object, e, "Edje");
+
+       CHECK_BOOL(val);
+
+       edje_object_play_set (*e, val == Qtrue ? 1 : 0);
+
+       return Qnil;
+}
+
+static VALUE c_animation_get (VALUE self)
+{
+       GET_OBJ (self, Evas_Object, e, "Edje");
+
+       return edje_object_animation_get (*e) ? Qtrue : Qfalse;
+}
+
+static VALUE c_animation_set (VALUE self, VALUE val)
+{
+       GET_OBJ (self, Evas_Object, e, "Edje");
+
+       CHECK_BOOL(val);
+
+       edje_object_animation_set (*e, val == Qtrue ? 1 : 0);
+
+       return Qnil;
+}
+
 void Init_Edje (void)
 {
-       cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
+       VALUE cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
 
        rb_define_singleton_method (cEdje, "new", c_new, 1);
        rb_define_method (cEdje, "load", c_load, 2);
+       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, "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);
+       rb_define_method (cEdje, "play?", c_play_get, 0);
+       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);
 }
-