+/*
+ * $Id: rb_part.c 47 2004-07-26 13:24:50Z tilman $
+ *
+ * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <ruby.h>
+
+#include <Edje.h>
+#include <rb_evas.h>
+#include <rb_evas_object.h>
+
+#include "rb_edje_main.h"
+#include "rb_edje.h"
+
+static VALUE cPart;
+
+static inline char *GET_NAME (VALUE o)
+{
+ static ID id;
+ VALUE name;
+
+ if (!id)
+ id = rb_intern ("@name");
+
+ name = rb_ivar_get (o, id);
+
+ return StringValuePtr (name);
+}
+
+static inline VALUE GET_EDJE (VALUE o)
+{
+ static ID id;
+
+ if (!id)
+ id = rb_intern ("@edje");
+
+ return rb_ivar_get (o, id);
+}
+
+static void c_mark (VALUE *self)
+{
+ rb_gc_mark (GET_EDJE (*self));
+}
+
+static void c_free (VALUE *self)
+{
+ edje_shutdown ();
+
+ free (self);
+}
+
+VALUE TO_PART (VALUE edje, VALUE name)
+{
+ VALUE self, *self2;
+
+ CHECK_CLASS (edje, cEdje);
+ Check_Type (name, T_STRING);
+
+ edje_init ();
+
+ /* we only use Data_Make_Struct to be able to specify
+ * mark and sweep hooks
+ */
+ self = Data_Make_Struct (cPart, VALUE, c_mark, c_free, self2);
+ self2 = &self;
+
+ rb_iv_set (self, "@edje", edje);
+ rb_iv_set (self, "@name", rb_str_dup (name));
+
+ rb_obj_call_init (self, 0, NULL);
+
+ return self;
+}
+
+static VALUE c_get_geometry (VALUE self)
+{
+ int x = 0, y = 0, w = 0, h = 0;
+
+ GET_OBJ (GET_EDJE (self), Evas_Object *, e);
+
+ edje_object_part_geometry_get (*e, GET_NAME (self),
+ (Evas_Coord *) &x,
+ (Evas_Coord *) &y,
+ (Evas_Coord *) &w,
+ (Evas_Coord *) &h);
+
+ return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y),
+ INT2FIX (y), INT2FIX (h));
+}
+
+static VALUE c_swallow (VALUE self, VALUE target)
+{
+ GET_OBJ (GET_EDJE (self), Evas_Object *, e);
+
+ CHECK_CLASS (target, cEvasObject);
+ GET_OBJ (target, Evas_Object *, target2);
+
+ edje_object_part_swallow (*e, GET_NAME (self), *target2);
+
+ return Qnil;
+}
+
+static VALUE c_unswallow (VALUE self)
+{
+ Evas_Object *o;
+
+ GET_OBJ (GET_EDJE (self), Evas_Object *, e);
+
+ if (!(o = edje_object_part_swallow_get (*e, GET_NAME (self)))) {
+ rb_raise (rb_eException, "Part didn't swallow an EvasObject");
+ return Qnil;
+ }
+
+ edje_object_part_unswallow (*e, o);
+
+ return Qnil;
+}
+
+static VALUE c_swallowed_object_get (VALUE self)
+{
+ Evas_Object *o;
+ void *obj;
+
+ GET_OBJ (GET_EDJE (self), Evas_Object *, e);
+
+ if (!(o = edje_object_part_swallow_get (*e, GET_NAME (self))))
+ 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_text_get (VALUE self)
+{
+ const char *s;
+
+ GET_OBJ (GET_EDJE (self), Evas_Object *, e);
+
+ if (!(s = edje_object_part_text_get (*e, GET_NAME (self))))
+ return Qnil;
+ else
+ return rb_str_new2 (s);
+}
+
+static VALUE c_text_set (VALUE self, VALUE text)
+{
+ GET_OBJ (GET_EDJE (self), Evas_Object *, e);
+
+ Check_Type (text, T_STRING);
+
+ edje_object_part_text_set (*e, GET_NAME (self),
+ StringValuePtr (text));
+
+ return Qnil;
+}
+
+void Init_Part (void)
+{
+ cPart = rb_define_class_under (mEdje, "Part", rb_cObject);
+
+ rb_define_attr (cPart, "edje", 1, 0);
+ rb_define_attr (cPart, "name", 1, 0);
+
+ /* not publically instantiable yet */
+ rb_define_private_method (rb_singleton_class (cPart),
+ "new", NULL, 0);
+ rb_define_method (cPart, "get_geometry", c_get_geometry, 0);
+ rb_define_method (cPart, "swallow", c_swallow, 1);
+ rb_define_method (cPart, "unswallow", c_unswallow, 1);
+ rb_define_method (cPart, "swallowed_object",
+ c_swallowed_object_get, 0);
+ rb_define_method (cPart, "text", c_text_get, 0);
+ rb_define_method (cPart, "text=", c_text_set, 1);
+}