Class instantiation fixes.
[ruby-esmart.git] / src / esmart_container / rb_esmart_container.c
index 8336cd02c22e77042a65be3ce4a579e421feaabe..85b9fdc79f35d5d44a9803104edf1ebd24165212 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id$
+ * $Id: rb_esmart_container.c 356 2006-02-10 18:27:31Z tilman $
  *
  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
  *
 #include <ruby.h>
 
 #include <Esmart/Esmart_Container.h>
-#include <rb_evas.h>
-#include <rb_evas_object.h>
+#include <evas/rb_evas.h>
+#include <evas/rb_evas_object.h>
 
 #include "../rb_esmart.h"
 
-static VALUE c_new (VALUE klass, VALUE evas)
+typedef struct {
+       RbEvasObject real;
+       VALUE elements;
+} RbContainer;
+
+static void c_mark (RbContainer *e)
+{
+       c_evas_object_mark (&e->real);
+
+       rb_gc_mark (e->elements);
+}
+
+static void c_free (RbContainer *e)
 {
-       VALUE self, argv[1];
-       Evas_Object **cont;
+       c_evas_object_free (&e->real, false);
+
+       free (e);
+}
 
+static VALUE c_alloc (VALUE klass)
+{
+       RbContainer *cont = NULL;
+
+       return Data_Make_Struct (klass, RbContainer, c_mark,
+                                c_free, cont);
+}
+
+static VALUE c_init (VALUE self, VALUE evas)
+{
        CHECK_CLASS (evas, cEvas);
-       GET_OBJ (evas, Evas *, e);
+       GET_OBJ (evas, RbEvas, e);
+       GET_OBJ (self, RbContainer, cont);
 
-       self = Data_Make_Struct (klass, Evas_Object *, c_evas_object_mark,
-                                c_evas_object_free, cont);
-       *cont = esmart_container_new (*e);
+       cont->real.real = esmart_container_new (e->real);
 
-       argv[0] = evas;
-       rb_obj_call_init (self, 1, argv);
+       rb_call_super (1, &evas);
+
+       cont->elements = rb_ary_new ();
 
        return self;
 }
 
-static VALUE c_element_append (VALUE self, VALUE element)
+static VALUE c_append_element (VALUE self, VALUE element)
 {
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
        CHECK_CLASS (element, cEvasObject);
-       GET_OBJ (element, Evas_Object *, o);
+       GET_OBJ (element, RbEvasObject, o);
 
-       esmart_container_element_append (*e, *o);
+       esmart_container_element_append (e->real.real, o->real);
+       rb_ary_push (e->elements, element);
 
        return Qnil;
 }
 
-static VALUE c_element_prepend (VALUE self, VALUE element)
+static VALUE c_prepend_element (VALUE self, VALUE element)
 {
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
        CHECK_CLASS (element, cEvasObject);
-       GET_OBJ (element, Evas_Object *, o);
+       GET_OBJ (element, RbEvasObject, o);
 
-       esmart_container_element_prepend (*e, *o);
+       esmart_container_element_prepend (e->real.real, o->real);
+       rb_ary_unshift (e->elements, element);
 
        return Qnil;
 }
 
-static VALUE c_element_remove (VALUE self, VALUE element)
+static VALUE c_remove_element (VALUE self, VALUE element)
 {
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
        CHECK_CLASS (element, cEvasObject);
-       GET_OBJ (element, Evas_Object *, o);
+       GET_OBJ (element, RbEvasObject, o);
 
-       esmart_container_element_remove (*e, *o);
+       esmart_container_element_remove (e->real.real, o->real);
+       rb_ary_delete (e->elements, element);
 
        return Qnil;
 }
 
+static VALUE c_elements_get (VALUE self)
+{
+       VALUE ary;
+
+       GET_OBJ (self, RbContainer, e);
+
+       ary = rb_ary_dup (e->elements);
+       OBJ_FREEZE (ary);
+
+       return ary;
+}
+
+static VALUE c_elements_length_get (VALUE self)
+{
+       double l;
+
+       GET_OBJ (self, RbContainer, e);
+
+       l = esmart_container_elements_length_get (e->real.real);
+
+       return rb_float_new (l);
+}
+
+static VALUE c_elements_orig_length_get (VALUE self)
+{
+       double l;
+
+       GET_OBJ (self, RbContainer, e);
+
+       l = esmart_container_elements_orig_length_get (e->real.real);
+
+       return rb_float_new (l);
+}
+
 static VALUE c_direction_get (VALUE self)
 {
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
-       return INT2FIX (esmart_container_direction_get (*e));
+       return INT2FIX (esmart_container_direction_get (e->real.real));
 }
 
 static VALUE c_direction_set (VALUE self, VALUE val)
 {
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
        Check_Type (val, T_FIXNUM);
 
-       esmart_container_direction_set (*e, FIX2INT (val));
+       esmart_container_direction_set (e->real.real, FIX2INT (val));
 
        return Qnil;
 }
 
 static VALUE c_spacing_get (VALUE self)
 {
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
-       return INT2FIX (esmart_container_spacing_get (*e));
+       return INT2FIX (esmart_container_spacing_get (e->real.real));
 }
 
 static VALUE c_spacing_set (VALUE self, VALUE val)
 {
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
        Check_Type (val, T_FIXNUM);
 
-       esmart_container_spacing_set (*e, FIX2INT (val));
+       esmart_container_spacing_set (e->real.real, FIX2INT (val));
 
        return Qnil;
 }
 
 static VALUE c_fill_policy_get (VALUE self)
 {
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
-       return INT2FIX (esmart_container_fill_policy_get (*e));
+       return INT2FIX (esmart_container_fill_policy_get (e->real.real));
 }
 
 static VALUE c_fill_policy_set (VALUE self, VALUE val)
 {
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
        Check_Type (val, T_FIXNUM);
 
-       esmart_container_fill_policy_set (*e, FIX2INT (val));
+       esmart_container_fill_policy_set (e->real.real, FIX2INT (val));
 
        return Qnil;
 }
 
+static VALUE c_alignment_get (VALUE self)
+{
+       GET_OBJ (self, RbContainer, e);
+
+       return INT2FIX (esmart_container_alignment_get (e->real.real));
+}
+
+static VALUE c_alignment_set (VALUE self, VALUE val)
+{
+       GET_OBJ (self, RbContainer, e);
+
+       Check_Type (val, T_FIXNUM);
+
+       esmart_container_alignment_set (e->real.real, FIX2INT (val));
+
+       return Qnil;
+}
+
+
 static VALUE c_get_padding (VALUE self)
 {
        double l = 0, r = 0, t = 0, b = 0;
 
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
-       esmart_container_padding_get (*e, &l, &r, &t, &b);
+       esmart_container_padding_get (e->real.real, &l, &r, &t, &b);
 
        return rb_ary_new3 (4, rb_float_new (l), rb_float_new (r),
                            rb_float_new (t), rb_float_new (b));
@@ -149,14 +229,14 @@ static VALUE c_get_padding (VALUE self)
 static VALUE c_set_padding (VALUE self, VALUE l, VALUE r,
                             VALUE t, VALUE b)
 {
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
        Check_Type (l, T_FLOAT);
        Check_Type (r, T_FLOAT);
        Check_Type (t, T_FLOAT);
        Check_Type (b, T_FLOAT);
 
-       esmart_container_padding_set (*e, NUM2DBL (l), NUM2DBL (r),
+       esmart_container_padding_set (e->real.real, NUM2DBL (l), NUM2DBL (r),
                                      NUM2DBL (t), NUM2DBL (b));
 
        return Qnil;
@@ -164,11 +244,33 @@ static VALUE c_set_padding (VALUE self, VALUE l, VALUE r,
 
 static VALUE c_scroll (VALUE self, VALUE val)
 {
-       GET_OBJ (self, Evas_Object *, e);
+       GET_OBJ (self, RbContainer, e);
 
        Check_Type (val, T_FIXNUM);
 
-       esmart_container_scroll (*e, FIX2INT (val));
+       esmart_container_scroll (e->real.real, FIX2INT (val));
+
+       return Qnil;
+}
+
+static VALUE c_scroll_percent_get (VALUE self)
+{
+       double val;
+
+       GET_OBJ (self, RbContainer, e);
+
+       val = esmart_container_scroll_percent_get (e->real.real);
+
+       return rb_float_new (val);
+}
+
+static VALUE c_scroll_percent_set (VALUE self, VALUE val)
+{
+       GET_OBJ (self, RbContainer, e);
+
+       Check_Type (val, T_FLOAT);
+
+       esmart_container_scroll_percent_set (e->real.real, NUM2DBL (val));
 
        return Qnil;
 }
@@ -181,19 +283,28 @@ void Init_esmart_container (void)
 
        c = rb_define_class_under (mEsmart, "Container", cEvasObject);
 
-       rb_define_singleton_method (c, "new", c_new, 1);
+       rb_define_alloc_func (c, c_alloc);
+       rb_define_method (c, "initialize", c_init, 1);
        rb_define_method (c, "direction", c_direction_get, 0);
        rb_define_method (c, "direction=", c_direction_set, 1);
        rb_define_method (c, "spacing", c_spacing_get, 0);
        rb_define_method (c, "spacing=", c_spacing_set, 1);
        rb_define_method (c, "fill_policy", c_fill_policy_get, 0);
        rb_define_method (c, "fill_policy=", c_fill_policy_set, 1);
+       rb_define_method (c, "alignment", c_alignment_get, 0);
+       rb_define_method (c, "alignment=", c_alignment_set, 1);
        rb_define_method (c, "get_padding", c_get_padding, 0);
        rb_define_method (c, "set_padding", c_set_padding, 4);
-       rb_define_method (c, "element_append", c_element_append, 1);
-       rb_define_method (c, "element_prepend", c_element_prepend, 1);
-       rb_define_method (c, "element_remove", c_element_remove, 1);
+       rb_define_method (c, "append_element", c_append_element, 1);
+       rb_define_method (c, "prepend_element", c_prepend_element, 1);
+       rb_define_method (c, "remove_element", c_remove_element, 1);
+       rb_define_method (c, "elements", c_elements_get, 0);
+       rb_define_method (c, "elements_length", c_elements_length_get, 0);
+       rb_define_method (c, "elements_orig_length",
+                         c_elements_orig_length_get, 0);
        rb_define_method (c, "scroll", c_scroll, 1);
+       rb_define_method (c, "scroll_percent", c_scroll_percent_get, 0);
+       rb_define_method (c, "scroll_percent=", c_scroll_percent_set, 1);
 
        rb_define_const (c, "HORIZONTAL",
                         INT2FIX (CONTAINER_DIRECTION_HORIZONTAL));
@@ -210,4 +321,15 @@ void Init_esmart_container (void)
                         INT2FIX (CONTAINER_FILL_POLICY_FILL_Y));
        rb_define_const (c, "HOMOGENOUS",
                         INT2FIX (CONTAINER_FILL_POLICY_HOMOGENOUS));
+
+       rb_define_const (c, "CENTER",
+                        INT2FIX (CONTAINER_ALIGN_CENTER));
+       rb_define_const (c, "LEFT",
+                        INT2FIX (CONTAINER_ALIGN_LEFT));
+       rb_define_const (c, "RIGHT",
+                        INT2FIX (CONTAINER_ALIGN_RIGHT));
+       rb_define_const (c, "BOTTOM",
+                        INT2FIX (CONTAINER_ALIGN_BOTTOM));
+       rb_define_const (c, "TOP",
+                        INT2FIX (CONTAINER_ALIGN_TOP));
 }