From 3f668760dde13be68327e243183007ba624e1e9a Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Mon, 26 Jul 2004 13:24:50 +0000 Subject: [PATCH] Added Part object. --- src/Makefile.am | 5 +- src/rb_edje.c | 160 +++++++++++-------------------------- src/rb_edje.h | 6 +- src/rb_edje_main.c | 4 +- src/rb_edje_main.h | 31 +++++++- src/rb_part.c | 193 +++++++++++++++++++++++++++++++++++++++++++++ src/rb_part.h | 27 +++++++ 7 files changed, 306 insertions(+), 120 deletions(-) create mode 100644 src/rb_part.c create mode 100644 src/rb_part.h diff --git a/src/Makefile.am b/src/Makefile.am index 1df4028..141484a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am 7 2004-06-19 19:32:33Z tilman $ +## $Id: Makefile.am 47 2004-07-26 13:24:50Z tilman $ AM_CFLAGS = $(EVAS_CFLAGS) $(EDJE_CFLAGS) INCLUDES = -I$(RUBYDIR) -I$(RUBYSITEDIR) @@ -7,7 +7,8 @@ ext_LTLIBRARIES = edje.la extdir = $(RUBYSITEDIR) edje_la_SOURCES = rb_edje_main.c rb_edje_main.h \ - rb_edje.c rb_edje.h + rb_edje.c rb_edje.h \ + rb_part.c rb_part.h edje_la_LIBADD = -lruby $(EVAS_LIBS) $(EDJE_LIBS) edje_la_LDFLAGS = -module -avoid-version diff --git a/src/rb_edje.c b/src/rb_edje.c index a8fe221..6cc37f9 100644 --- a/src/rb_edje.c +++ b/src/rb_edje.c @@ -1,5 +1,5 @@ /* - * $Id: rb_edje.c 37 2004-07-25 11:19:48Z tilman $ + * $Id: rb_edje.c 47 2004-07-26 13:24:50Z tilman $ * * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de) * @@ -24,41 +24,19 @@ #include #include +#define __RB_EDJE_C #include "rb_edje_main.h" +#include "rb_part.h" -#define GET_OBJ(obj, type, o) \ - type *(o) = NULL; \ -\ - Data_Get_Struct ((obj), type, (o)); \ -\ - if (!*(o)) { \ - rb_raise (rb_eException, \ - "%s destroyed already", \ - rb_obj_classname ((obj))); \ - return Qnil; \ - } - -#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; \ - } - -#define CHECK_CLASS(val, klass) \ - if (!rb_obj_is_kind_of ((val), (klass))) { \ - rb_raise (rb_eTypeError, \ - "wrong argument type %s (expected %s)", \ - rb_obj_classname ((val)), \ - rb_class2name ((klass))); \ - return Qnil; \ - } +VALUE cEdje; +static VALUE part_hashes; static void c_free (Evas_Object **e) { c_evas_object_free (e); + rb_hash_aset (part_hashes, INT2NUM ((long) e), Qnil); + edje_shutdown (); } @@ -118,90 +96,42 @@ static VALUE c_get_size_max (VALUE self) 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); - 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); - - Check_Type (part, T_STRING); - - CHECK_CLASS (target, cEvasObject); - GET_OBJ (target, Evas_Object *, target2); + VALUE parts, part; - 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); - CHECK_CLASS (target, cEvasObject); - GET_OBJ (target, Evas_Object *, target2); - - edje_object_part_unswallow (*e, *target2); - - return Qnil; -} + Check_Type (name, T_STRING); -static VALUE c_get_part_swallow (VALUE self, VALUE part) -{ - Evas_Object *o; - void *obj; - - GET_OBJ (self, Evas_Object *, e); - - Check_Type (part, T_STRING); - - if (!(o = edje_object_part_swallow_get (*e, StringValuePtr (part)))) + if (!edje_object_part_exists (*e, StringValuePtr (name))) 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); - - 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); - - 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, @@ -238,7 +168,8 @@ 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)); } @@ -273,7 +204,7 @@ static VALUE c_play_set (VALUE self, VALUE val) CHECK_BOOL(val); - edje_object_play_set (*e, val == Qtrue ? 1 : 0); + edje_object_play_set (*e, val == Qtrue); return Qnil; } @@ -291,30 +222,29 @@ static VALUE c_animation_set (VALUE self, VALUE val) CHECK_BOOL(val); - edje_object_animation_set (*e, val == Qtrue ? 1 : 0); + edje_object_animation_set (*e, val == Qtrue); return Qnil; } void Init_Edje (void) { - VALUE c = rb_define_class_under (mEdje, "Edje", cEvasObject); - - rb_define_singleton_method (c, "new", c_new, 1); - rb_define_method (c, "load", c_load, 2); - rb_define_method (c, "get_size_min", c_get_size_min, 0); - rb_define_method (c, "get_size_max", c_get_size_max, 0); - rb_define_method (c, "part_exists?", c_part_exists_get, 1); - rb_define_method (c, "part_swallow", c_part_swallow, 2); - rb_define_method (c, "part_unswallow", c_part_unswallow, 1); - rb_define_method (c, "get_part_swallow", c_get_part_swallow, 1); - rb_define_method (c, "get_part_text", c_get_part_text, 1); - rb_define_method (c, "set_part_text", c_set_part_text, 2); - rb_define_method (c, "on_text_changed", c_on_text_changed, 0); - rb_define_method (c, "signal_emit", c_signal_emit, 1); - rb_define_method (c, "on_signal", c_on_signal, 2); - rb_define_method (c, "play?", c_play_get, 0); - rb_define_method (c, "play=", c_play_set, 1); - rb_define_method (c, "animation?", c_animation_get, 0); - rb_define_method (c, "animation=", c_animation_set, 1); + 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", 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); + 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); + + part_hashes = rb_hash_new (); + rb_global_variable (&part_hashes); } diff --git a/src/rb_edje.h b/src/rb_edje.h index 2920daf..f9a8bce 100644 --- a/src/rb_edje.h +++ b/src/rb_edje.h @@ -1,5 +1,5 @@ /* - * $Id: rb_edje.h 7 2004-06-19 19:32:33Z tilman $ + * $Id: rb_edje.h 47 2004-07-26 13:24:50Z tilman $ * * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de) * @@ -21,6 +21,10 @@ #ifndef __RB_EDJE_H #define __RB_EDJE_H +#ifndef __RB_EDJE_C +VALUE cEdje; +#endif + void Init_Edje (void); #endif diff --git a/src/rb_edje_main.c b/src/rb_edje_main.c index 974d7ff..10fc3c7 100644 --- a/src/rb_edje_main.c +++ b/src/rb_edje_main.c @@ -1,5 +1,5 @@ /* - * $Id: rb_edje_main.c 28 2004-07-08 18:35:51Z tilman $ + * $Id: rb_edje_main.c 47 2004-07-26 13:24:50Z tilman $ * * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de) * @@ -24,6 +24,7 @@ #include "rb_edje_main.h" #include "rb_edje.h" +#include "rb_part.h" #ifdef DEBUG static VALUE m_init (VALUE self) @@ -82,5 +83,6 @@ void Init_edje (void) rb_define_module_function (mEdje, "frametime=", m_frametime_set, 1); Init_Edje (); + Init_Part (); } diff --git a/src/rb_edje_main.h b/src/rb_edje_main.h index 0f9aa60..9e8425c 100644 --- a/src/rb_edje_main.h +++ b/src/rb_edje_main.h @@ -1,5 +1,5 @@ /* - * $Id: rb_edje_main.h 7 2004-06-19 19:32:33Z tilman $ + * $Id: rb_edje_main.h 47 2004-07-26 13:24:50Z tilman $ * * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de) * @@ -21,6 +21,35 @@ #ifndef __RB_EDJE_MAIN_H #define __RB_EDJE_MAIN_H +#define GET_OBJ(obj, type, o) \ + type *(o) = NULL; \ +\ + Data_Get_Struct ((obj), type, (o)); \ +\ + if (!*(o)) { \ + rb_raise (rb_eException, \ + "%s destroyed already", \ + rb_obj_classname ((obj))); \ + return Qnil; \ + } + +#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; \ + } + +#define CHECK_CLASS(val, klass) \ + if (!rb_obj_is_kind_of ((val), (klass))) { \ + rb_raise (rb_eTypeError, \ + "wrong argument type %s (expected %s)", \ + rb_obj_classname ((val)), \ + rb_class2name ((klass))); \ + return Qnil; \ + } + VALUE mEdje; #endif diff --git a/src/rb_part.c b/src/rb_part.c new file mode 100644 index 0000000..245df55 --- /dev/null +++ b/src/rb_part.c @@ -0,0 +1,193 @@ +/* + * $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 + +#include +#include +#include + +#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); +} diff --git a/src/rb_part.h b/src/rb_part.h new file mode 100644 index 0000000..b84f2ce --- /dev/null +++ b/src/rb_part.h @@ -0,0 +1,27 @@ +/* + * $Id: rb_part.h 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 + */ + +#ifndef __RB_PART_H +#define __RB_PART_H + +void Init_Part (void); +VALUE TO_PART (VALUE edje, VALUE name); + +#endif -- 2.30.2