Removed RCS-style IDs.
[ruby-evas.git] / src / rb_evas_object.c
index 79dc66f7cc5f56d4f0b69bf3252ac5db5eaf52e8..3be8bf694eef5ca9a78bb0d023cb042caa4a7b53 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id: rb_evas_object.c 29 2004-07-08 18:47:44Z tilman $
- *
  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
  *
  * This library is free software; you can redistribute it and/or
  */
 
 #include <ruby.h>
+#include <stdbool.h>
 
 #include <Evas.h>
 
+#define __RB_EVAS_OBJECT_C
 #include "rb_evas_main.h"
 #include "rb_evas.h"
 #include "rb_evas_object.h"
 
-static VALUE parents;
+VALUE cEvasObject;
+
+VALUE TO_EVAS_OBJECT (Evas_Object *o)
+{
+       void *obj;
+
+       if (!o)
+               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;
+}
 
 /* called by the child classes */
-void c_evas_object_free (Evas_Object **e)
+void c_evas_object_mark (RbEvasObject *e)
 {
-       if (*e)
-               evas_object_del (*e);
+       rb_gc_mark (e->parent);
 
-       rb_hash_aset (parents, INT2NUM ((long) e), Qnil);
+       if (!NIL_P (e->callbacks))
+               rb_gc_mark (e->callbacks);
 
-       free (e);
+       if (!NIL_P (e->userdata))
+               rb_gc_mark (e->userdata);
 }
 
-void c_evas_object_mark (Evas_Object **e)
+void c_evas_object_free (RbEvasObject *e, bool free_mem)
 {
-       VALUE parent;
+       if (e->real)
+               evas_object_del (e->real);
+
+       if (free_mem)
+               free (e);
+}
 
-       parent = rb_hash_aref (parents, INT2NUM ((long) (e)));
-       if (!NIL_P (parent))
-               rb_gc_mark (parent);
+static void c_free (RbEvasObject *e)
+{
+       c_evas_object_free (e, true);
 }
 
+static VALUE c_alloc (VALUE klass)
+{
+       RbEvasObject *e;
+
+       return Data_Make_Struct (klass, RbEvasObject, c_evas_object_mark,
+                                c_free, e);
+}
+
+/* :nodoc: */
 static VALUE c_init (VALUE self, VALUE parent)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       evas_object_data_set (*e, RUBY_EVAS_OBJECT_KEY, (void *) self);
+       evas_object_data_set (e->real, RUBY_EVAS_OBJECT_KEY, (void *) self);
 
-       rb_hash_aset (parents, INT2NUM ((long) e), parent);
+       e->parent = parent;
+       e->callbacks = Qnil;
 
        return self;
 }
 
+/* :nodoc: */
 static VALUE c_inspect (VALUE self)
 {
-       char buf[128];
+       INSPECT (self, RbEvasObject);
+}
 
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+static VALUE c_type_get (VALUE self)
+{
+       const char *s;
 
-       snprintf (buf, sizeof (buf), "#<Evas::EvasObject:%p ptr=%p>",
-                 (void *) self, *e);
+       GET_OBJ (self, RbEvasObject, e);
 
-       return rb_str_new2 (buf);
+       s = evas_object_type_get (e->real);
+
+       return s ? rb_str_new2 (s) : Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.delete => nil
+ *
+ * Deletes <i>e</i>.
+ */
 static VALUE c_delete (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       evas_object_del (*e);
-       *e = NULL;
+       evas_object_del (e->real);
+       e->real = NULL;
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.resize(width, height) => nil
+ *
+ * Resizes <i>e</i> to width x height.
+ *
+ *  e.resize(100, 200) #=> nil
+ */
 static VALUE c_resize (VALUE self, VALUE w, VALUE h)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
        Check_Type (w, T_FIXNUM);
        Check_Type (h, T_FIXNUM);
 
-       evas_object_resize (*e, (Evas_Coord) FIX2INT (w),
-                               (Evas_Coord) FIX2INT (h));
+       evas_object_resize (e->real, (Evas_Coord) FIX2INT (w),
+                           (Evas_Coord) FIX2INT (h));
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.move(x, y) => nil
+ *
+ * Moves <i>e</i> to the coordinates specified in
+ * <i>x</i> and <i>y</i>.
+ *
+ *  e.move(100, 200) #=> nil
+ */
 static VALUE c_move (VALUE self, VALUE x, VALUE y)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
        Check_Type (x, T_FIXNUM);
        Check_Type (y, T_FIXNUM);
 
-       evas_object_move (*e, (Evas_Coord) FIX2INT (x),
-                             (Evas_Coord) FIX2INT (y));
+       evas_object_move (e->real, (Evas_Coord) FIX2INT (x),
+                         (Evas_Coord) FIX2INT (y));
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.geometry => array
+ *
+ * Returns an array containing the geometry of <i>e</i>.
+ *
+ *  e.move(150, 300)   #=> nil
+ *  e.resize(200, 200) #=> nil
+ *  e.geometry         #=> [150, 300, 200, 200]
+ */
 static VALUE c_geometry_get (VALUE self)
 {
        int x = 0, y = 0, w = 0, h = 0;
 
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       evas_object_geometry_get (*e, (Evas_Coord *) &x, (Evas_Coord *) &y,
-                                 (Evas_Coord *) & w, (Evas_Coord *) &h);
+       evas_object_geometry_get (e->real,
+                                 (Evas_Coord *) &x, (Evas_Coord *) &y,
+                                 (Evas_Coord *) &w, (Evas_Coord *) &h);
 
        return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y), INT2FIX (w),
                            INT2FIX (h));
 }
 
+/*
+ * call-seq:
+ *  e.show => nil
+ *
+ * Shows <i>e</i>.
+ */
 static VALUE c_show (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       evas_object_show (*e);
+       evas_object_show (e->real);
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.hide => nil
+ *
+ * Hides <i>e</i>.
+ */
 static VALUE c_hide (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       evas_object_hide (*e);
+       evas_object_hide (e->real);
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.visible? => true or false
+ *
+ * Returns true if <i>e</i> is visible, else returns false.
+ */
 static VALUE c_visible_get (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
+
+       return evas_object_visible_get (e->real) ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq:
+ *  e.focused? => true or false
+ *
+ * Returns true if <i>e</i> is focused, else returns false.
+ */
+static VALUE c_focused_get (VALUE self)
+{
+       GET_OBJ (self, RbEvasObject, e);
 
-       return evas_object_visible_get (*e) ? Qtrue : Qfalse;
+       return evas_object_focus_get (e->real) ? Qtrue : Qfalse;
 }
 
+/*
+ * call-seq:
+ *  e.focused(true or false)
+ *
+ * (Un)focuses <i>e</i>.
+ */
+static VALUE c_focused_set (VALUE self, VALUE val)
+{
+       GET_OBJ (self, RbEvasObject, e);
+
+       CHECK_BOOL (val);
+
+       evas_object_focus_set (e->real, val == Qtrue);
+
+       return Qnil;
+}
+
+/*
+ * call-seq:
+ *  e.evas => evas
+ *
+ * Returns the <code>Evas::Evas</code> for <i>e</i>.
+ */
 static VALUE c_evas_get (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       return rb_hash_aref (parents, INT2NUM ((long) (e)));
+       return e->parent;
 }
 
+/*
+ * call-seq:
+ *  e.name => string
+ *
+ * Returns the name of <i>e</i>.
+ */
 static VALUE c_name_get (VALUE self)
 {
        const char *name;
 
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       if (!(name = evas_object_name_get (*e)))
+       if (!(name = evas_object_name_get (e->real)))
                return Qnil;
        else
                return rb_str_new2 (name);
 }
 
+/*
+ * call-seq:
+ *  e.name(string)
+ *
+ * Sets the name of <i>e</i>.
+ */
 static VALUE c_name_set (VALUE self, VALUE val)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
-
-       Check_Type (val, T_STRING);
+       GET_OBJ (self, RbEvasObject, e);
 
-       evas_object_name_set (*e, StringValuePtr (val));
+       evas_object_name_set (e->real, StringValuePtr (val));
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.layer => fixnum
+ *
+ * Returns the layer <i>e</i> is in.
+ */
 static VALUE c_layer_get (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       return INT2FIX (evas_object_layer_get (*e));
+       return INT2FIX (evas_object_layer_get (e->real));
 }
 
+/*
+ * call-seq:
+ *  e.layer(fixnum)
+ *
+ * Sets the layer <i>e</i> is in.
+ */
 static VALUE c_layer_set (VALUE self, VALUE val)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
        Check_Type (val, T_FIXNUM);
 
-       evas_object_layer_set (*e, NUM2INT (val));
+       evas_object_layer_set (e->real, NUM2INT (val));
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.get_color => array
+ *
+ * Returns the color of <i>e</i>.
+ *
+ *  e.set_color(128, 128, 128, 0) #=> nil
+ *  e.get_color                   #=> [128, 128, 128, 0]
+ */
 static VALUE c_get_color (VALUE self)
 {
        int r = 0, g = 0, b = 0, a = 0;
 
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       evas_object_color_get (*e, &r, &g, &b, &a);
+       evas_object_color_get (e->real, &r, &g, &b, &a);
 
        return rb_ary_new3 (4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
                            INT2FIX (a));
 }
 
+/*
+ * call-seq:
+ *  e.set_color(r, g, b, a) => nil
+ *
+ * Sets the color of <i>e</i>.
+ *
+ *  e.set_color(128, 128, 128, 0) #=> nil
+ */
 static VALUE c_set_color (VALUE self, VALUE r, VALUE g, VALUE b,
                           VALUE a)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
        Check_Type (r, T_FIXNUM);
        Check_Type (g, T_FIXNUM);
        Check_Type (b, T_FIXNUM);
        Check_Type (a, T_FIXNUM);
 
-       evas_object_color_set (*e, FIX2INT (r), FIX2INT (g), FIX2INT (b),
-                              FIX2INT (a));
+       evas_object_color_set (e->real, FIX2INT (r), FIX2INT (g),
+                              FIX2INT (b), FIX2INT (a));
 
        return Qnil;
 }
 
+static VALUE c_get_clip (VALUE self)
+{
+       GET_OBJ (self, RbEvasObject, e);
+
+       return TO_EVAS_OBJECT (evas_object_clip_get (e->real));
+}
+
+static VALUE c_set_clip (VALUE self, VALUE clip)
+{
+       GET_OBJ (self, RbEvasObject, e);
+       GET_OBJ (clip, RbEvasObject, e2);
+
+       evas_object_clip_set (e->real, e2->real);
+
+       return Qnil;
+}
+
+/*
+ * call-seq:
+ *  e.pass_events? => true or false
+ *
+ * Returns true if <i>e</i> passes events on to EvasObjects that are
+ * below itself, else returns false.
+ */
 static VALUE c_pass_events_get (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       return evas_object_pass_events_get (*e) ? Qtrue : Qfalse;
+       return evas_object_pass_events_get (e->real) ? Qtrue : Qfalse;
 }
 
+/*
+ * call-seq:
+ *  e.pass_events(true or false)
+ *
+ * Sets whether <i>e</i> passes events on to EvasObjects that are
+ * below itself.
+ */
 static VALUE c_pass_events_set (VALUE self, VALUE val)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
+
+       CHECK_BOOL (val);
+
+       evas_object_pass_events_set (e->real, val == Qtrue);
+
+       return Qnil;
+}
+
+/*
+ * call-seq:
+ *  e.repeat_events? => true or false
+ *
+ * Returns true if <i>e</i> repeats events to EvasObjects that are
+ * below itself, else returns false.
+ */
+static VALUE c_repeat_events_get (VALUE self)
+{
+       GET_OBJ (self, RbEvasObject, e);
+
+       return evas_object_repeat_events_get (e->real) ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq:
+ *  e.repeat_events(true or false)
+ *
+ * Sets whether <i>e</i> repeats events to EvasObjects that are
+ * below itself.
+ */
+static VALUE c_repeat_events_set (VALUE self, VALUE val)
+{
+       GET_OBJ (self, RbEvasObject, e);
+
+       CHECK_BOOL (val);
+
+       evas_object_repeat_events_set (e->real, val == Qtrue);
+
+       return Qnil;
+}
+
+static VALUE c_anti_alias_get (VALUE self)
+{
+       GET_OBJ (self, RbEvasObject, e);
+
+       return evas_object_anti_alias_get (e->real) ? Qtrue : Qfalse;
+}
+
+static VALUE c_anti_alias_set (VALUE self, VALUE val)
+{
+       GET_OBJ (self, RbEvasObject, e);
 
        CHECK_BOOL (val);
 
-       evas_object_pass_events_set (*e, val == Qtrue ? 1 : 0);
+       evas_object_anti_alias_set (e->real, val == Qtrue);
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *   e.raise => nil
+ *
+ * Raises <i>e</i>.
+ */
 static VALUE c_raise (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       evas_object_raise (*e);
+       evas_object_raise (e->real);
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.lower => nil
+ *
+ * Lowers <i>e</i>.
+ */
 static VALUE c_lower (VALUE self)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       evas_object_lower (*e);
+       evas_object_lower (e->real);
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.stack_above(evasobject) => nil
+ *
+ * Positions <i>e</i> above <i>evasobject</i>.
+ */
 static VALUE c_stack_above (VALUE self, VALUE target)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       if (!rb_obj_is_kind_of (target, cEvasObject)) {
-               rb_raise (rb_eTypeError,
-                         "wrong argument type %s (expected EvasObject)",
-                         rb_obj_classname (target));
-               return Qnil;
-       }
+       CHECK_CLASS (target, cEvasObject);
+       GET_OBJ (target, RbEvasObject, t);
 
-       GET_OBJ (target, Evas_Object, target2, "EvasObject");
-
-       evas_object_stack_above (*e, *target2);
+       evas_object_stack_above (e->real, t->real);
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.stack_below(evasobject) => nil
+ *
+ * Positions <i>e</i> below <i>evasobject</i>.
+ */
 static VALUE c_stack_below (VALUE self, VALUE target)
 {
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       GET_OBJ (self, RbEvasObject, e);
 
-       if (!rb_obj_is_kind_of (target, cEvasObject)) {
-               rb_raise (rb_eTypeError,
-                         "wrong argument type %s (expected EvasObject)",
-                         rb_obj_classname (target));
-               return Qnil;
-       }
+       CHECK_CLASS (target, cEvasObject);
+       GET_OBJ (target, RbEvasObject, t);
 
-       GET_OBJ (target, Evas_Object, target2, "EvasObject");
-
-       evas_object_stack_below (*e, *target2);
+       evas_object_stack_below (e->real, t->real);
 
        return Qnil;
 }
 
+/*
+ * call-seq:
+ *  e.above => evasobject
+ *
+ * Returns the <code>Evas::EvasObject</code> that's positioned above
+ * <i>e</i>.
+ */
 static VALUE c_above_get (VALUE self)
 {
-       Evas_Object *o;
-       void *obj;
+       GET_OBJ (self, RbEvasObject, e);
 
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
-
-       if (!(evas_object_above_get (*e)))
-               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;
+       return TO_EVAS_OBJECT (evas_object_above_get (e->real));
 }
 
+/*
+ * call-seq:
+ *  e.below => evasobject
+ *
+ * Returns the <code>Evas::EvasObject</code> that's positioned below
+ * <i>e</i>.
+ */
 static VALUE c_below_get (VALUE self)
 {
-       Evas_Object *o;
-       void *obj;
+       GET_OBJ (self, RbEvasObject, e);
 
-       GET_OBJ (self, Evas_Object, e, "EvasObject");
+       return TO_EVAS_OBJECT (evas_object_below_get (e->real));
+}
 
-       if (!(evas_object_below_get (*e)))
-               return Qnil;
+static VALUE c_userdata_get (VALUE self)
+{
+       GET_OBJ (self, RbEvasObject, e);
 
-       if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
-               rb_raise (rb_eException, "EvasObject Ruby object key missing");
-               return Qnil;
-       }
+       if (NIL_P (e->userdata))
+               e->userdata = rb_hash_new ();
 
-       return (VALUE) obj;
+       return e->userdata;
 }
 
 void Init_EvasObject (void)
@@ -334,10 +592,10 @@ void Init_EvasObject (void)
        cEvasObject = rb_define_class_under (mEvas, "EvasObject",
                                             rb_cObject);
 
-       rb_define_private_method (rb_singleton_class (cEvasObject),
-                                 "new", NULL, 0);
+       rb_define_alloc_func (cEvasObject, c_alloc);
        rb_define_method (cEvasObject, "initialize", c_init, 1);
        rb_define_method (cEvasObject, "inspect", c_inspect, 0);
+       rb_define_method (cEvasObject, "type", c_type_get, 0);
        rb_define_method (cEvasObject, "delete", c_delete, 0);
        rb_define_method (cEvasObject, "resize", c_resize, 2);
        rb_define_method (cEvasObject, "move", c_move, 2);
@@ -345,6 +603,8 @@ void Init_EvasObject (void)
        rb_define_method (cEvasObject, "show", c_show, 0);
        rb_define_method (cEvasObject, "hide", c_hide, 0);
        rb_define_method (cEvasObject, "visible?", c_visible_get, 0);
+       rb_define_method (cEvasObject, "focused?", c_focused_get, 0);
+       rb_define_method (cEvasObject, "focused=", c_focused_set, 1);
        rb_define_method (cEvasObject, "evas", c_evas_get, 0);
        rb_define_method (cEvasObject, "name", c_name_get, 0);
        rb_define_method (cEvasObject, "name=", c_name_set, 1);
@@ -352,15 +612,23 @@ void Init_EvasObject (void)
        rb_define_method (cEvasObject, "layer=", c_layer_set, 1);
        rb_define_method (cEvasObject, "get_color", c_get_color, 0);
        rb_define_method (cEvasObject, "set_color", c_set_color, 4);
-       rb_define_method (cEvasObject, "pass_events?", c_pass_events_get, 0);
-       rb_define_method (cEvasObject, "pass_events=", c_pass_events_set, 1);
+       rb_define_method (cEvasObject, "clip", c_get_clip, 0);
+       rb_define_method (cEvasObject, "clip=", c_set_clip, 1);
+       rb_define_method (cEvasObject, "pass_events?",
+                         c_pass_events_get, 0);
+       rb_define_method (cEvasObject, "pass_events=",
+                         c_pass_events_set, 1);
+       rb_define_method (cEvasObject, "repeat_events?",
+                         c_repeat_events_get, 0);
+       rb_define_method (cEvasObject, "repeat_events=",
+                         c_repeat_events_set, 1);
+       rb_define_method (cEvasObject, "anti_alias?", c_anti_alias_get, 0);
+       rb_define_method (cEvasObject, "anti_alias=", c_anti_alias_set, 1);
        rb_define_method (cEvasObject, "raise", c_raise, 0);
        rb_define_method (cEvasObject, "lower", c_lower, 0);
        rb_define_method (cEvasObject, "stack_above", c_stack_above, 1);
        rb_define_method (cEvasObject, "stack_below", c_stack_below, 1);
        rb_define_method (cEvasObject, "above", c_above_get, 0);
        rb_define_method (cEvasObject, "below", c_below_get, 0);
-
-       parents = rb_hash_new ();
-       rb_global_variable (&parents);
+       rb_define_method (cEvasObject, "userdata", c_userdata_get, 0);
 }