245df55b845d9a8fc7d950c20215500c0aa93df7
[ruby-edje.git] / src / rb_part.c
1 /*
2  * $Id: rb_part.c 47 2004-07-26 13:24:50Z tilman $
3  *
4  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ruby.h>
22
23 #include <Edje.h>
24 #include <rb_evas.h>
25 #include <rb_evas_object.h>
26
27 #include "rb_edje_main.h"
28 #include "rb_edje.h"
29
30 static VALUE cPart;
31
32 static inline char *GET_NAME (VALUE o)
33 {
34         static ID id;
35         VALUE name;
36
37         if (!id)
38                 id = rb_intern ("@name");
39
40         name = rb_ivar_get (o, id);
41
42         return StringValuePtr (name);
43 }
44
45 static inline VALUE GET_EDJE (VALUE o)
46 {
47         static ID id;
48
49         if (!id)
50                 id = rb_intern ("@edje");
51
52         return rb_ivar_get (o, id);
53 }
54
55 static void c_mark (VALUE *self)
56 {
57         rb_gc_mark (GET_EDJE (*self));
58 }
59
60 static void c_free (VALUE *self)
61 {
62         edje_shutdown ();
63
64         free (self);
65 }
66
67 VALUE TO_PART (VALUE edje, VALUE name)
68 {
69         VALUE self, *self2;
70
71         CHECK_CLASS (edje, cEdje);
72         Check_Type (name, T_STRING);
73
74         edje_init ();
75
76         /* we only use Data_Make_Struct to be able to specify
77          * mark and sweep hooks
78          */
79         self = Data_Make_Struct (cPart, VALUE, c_mark, c_free, self2);
80         self2 = &self;
81
82         rb_iv_set (self, "@edje", edje);
83         rb_iv_set (self, "@name", rb_str_dup (name));
84
85         rb_obj_call_init (self, 0, NULL);
86
87         return self;
88 }
89
90 static VALUE c_get_geometry (VALUE self)
91 {
92         int x = 0, y = 0, w = 0, h = 0;
93
94         GET_OBJ (GET_EDJE (self), Evas_Object *, e);
95
96         edje_object_part_geometry_get (*e, GET_NAME (self),
97                                        (Evas_Coord *) &x,
98                                        (Evas_Coord *) &y,
99                                        (Evas_Coord *) &w,
100                                        (Evas_Coord *) &h);
101
102         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y),
103                             INT2FIX (y), INT2FIX (h));
104 }
105
106 static VALUE c_swallow (VALUE self, VALUE target)
107 {
108         GET_OBJ (GET_EDJE (self), Evas_Object *, e);
109
110         CHECK_CLASS (target, cEvasObject);
111         GET_OBJ (target, Evas_Object *, target2);
112
113         edje_object_part_swallow (*e, GET_NAME (self), *target2);
114
115         return Qnil;
116 }
117
118 static VALUE c_unswallow (VALUE self)
119 {
120         Evas_Object *o;
121
122         GET_OBJ (GET_EDJE (self), Evas_Object *, e);
123
124         if (!(o = edje_object_part_swallow_get (*e, GET_NAME (self)))) {
125                 rb_raise (rb_eException, "Part didn't swallow an EvasObject");
126                 return Qnil;
127         }
128
129         edje_object_part_unswallow (*e, o);
130
131         return Qnil;
132 }
133
134 static VALUE c_swallowed_object_get (VALUE self)
135 {
136         Evas_Object *o;
137         void *obj;
138
139         GET_OBJ (GET_EDJE (self), Evas_Object *, e);
140
141         if (!(o = edje_object_part_swallow_get (*e, GET_NAME (self))))
142                 return Qnil;
143
144         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
145                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
146                 return Qnil;
147         }
148
149         return (VALUE) obj;
150 }
151
152 static VALUE c_text_get (VALUE self)
153 {
154         const char *s;
155
156         GET_OBJ (GET_EDJE (self), Evas_Object *, e);
157
158         if (!(s = edje_object_part_text_get (*e, GET_NAME (self))))
159                 return Qnil;
160         else
161                 return rb_str_new2 (s);
162 }
163
164 static VALUE c_text_set (VALUE self, VALUE text)
165 {
166         GET_OBJ (GET_EDJE (self), Evas_Object *, e);
167
168         Check_Type (text, T_STRING);
169
170         edje_object_part_text_set (*e, GET_NAME (self),
171                                    StringValuePtr (text));
172
173         return Qnil;
174 }
175
176 void Init_Part (void)
177 {
178         cPart = rb_define_class_under (mEdje, "Part", rb_cObject);
179
180         rb_define_attr (cPart, "edje", 1, 0);
181         rb_define_attr (cPart, "name", 1, 0);
182
183         /* not publically instantiable yet */
184         rb_define_private_method (rb_singleton_class (cPart),
185                                   "new", NULL, 0);
186         rb_define_method (cPart, "get_geometry", c_get_geometry, 0);
187         rb_define_method (cPart, "swallow", c_swallow, 1);
188         rb_define_method (cPart, "unswallow", c_unswallow, 1);
189         rb_define_method (cPart, "swallowed_object",
190                           c_swallowed_object_get, 0);
191         rb_define_method (cPart, "text", c_text_get, 0);
192         rb_define_method (cPart, "text=", c_text_set, 1);
193 }