9d2d90e9e9e24c9ac9d0c62b37bd62703686c917
[ruby-edje.git] / src / rb_part.c
1 /*
2  * $Id: rb_part.c 384 2006-07-23 08:21:33Z 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
25 #include "rb_edje_main.h"
26 #include "rb_edje.h"
27
28 static VALUE cPart;
29
30 static inline char *GET_NAME (VALUE o)
31 {
32         static ID id;
33         VALUE name;
34
35         if (!id)
36                 id = rb_intern ("@name");
37
38         name = rb_ivar_get (o, id);
39
40         return StringValuePtr (name);
41 }
42
43 static inline VALUE GET_EDJE (VALUE o)
44 {
45         static ID id;
46
47         if (!id)
48                 id = rb_intern ("@edje");
49
50         return rb_ivar_get (o, id);
51 }
52
53 VALUE TO_PART (VALUE edje, VALUE name)
54 {
55         VALUE self;
56
57         CHECK_CLASS (edje, cEdje);
58         Check_Type (name, T_STRING);
59
60         self = rb_obj_alloc (cPart);
61
62         rb_iv_set (self, "@edje", edje);
63         rb_iv_set (self, "@name", rb_str_dup (name));
64
65         rb_obj_call_init (self, 0, NULL);
66
67         return self;
68 }
69
70 /*
71  * call-seq:
72  *  part.geometry => array
73  *
74  * Returns an array containing the geometry of <i>part</i>.
75  */
76 static VALUE c_geometry_get (VALUE self)
77 {
78         int x = 0, y = 0, w = 0, h = 0;
79
80         GET_OBJ (GET_EDJE (self), RbEdje, e);
81
82         edje_object_part_geometry_get (e->real.real, GET_NAME (self),
83                                        (Evas_Coord *) &x,
84                                        (Evas_Coord *) &y,
85                                        (Evas_Coord *) &w,
86                                        (Evas_Coord *) &h);
87
88         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y),
89                             INT2FIX (w), INT2FIX (h));
90 }
91
92 /*
93  * call-seq:
94  *  part.swallow(evasobject) => nil
95  *
96  * Swallows an <code>Evas::EvasObject</code> into <i>part</i>.
97  */
98 static VALUE c_swallow (VALUE self, VALUE target)
99 {
100         GET_OBJ (GET_EDJE (self), RbEdje, e);
101
102         CHECK_CLASS (target, cEvasObject);
103         GET_OBJ (target, RbEvasObject, t);
104
105         edje_object_part_swallow (e->real.real, GET_NAME (self), t->real);
106         rb_iv_set (self, "swallowed_obj", target);
107
108         return Qnil;
109 }
110
111 /*
112  * call-seq:
113  *  part.unswallow => nil
114  *
115  * Unswallows the <code>Evas::EvasObject</code> swallowed by
116  * <i>part</i>.
117  */
118 static VALUE c_unswallow (VALUE self)
119 {
120         Evas_Object *o;
121
122         GET_OBJ (GET_EDJE (self), RbEdje, e);
123
124         o = edje_object_part_swallow_get (e->real.real, GET_NAME (self));
125         if (!o) {
126                 rb_raise (rb_eException, "Part didn't swallow an EvasObject");
127                 return Qnil;
128         }
129
130         edje_object_part_unswallow (e->real.real, o);
131         rb_iv_set (self, "swallowed_obj", Qnil);
132
133         return Qnil;
134 }
135
136 /*
137  * call-seq:
138  *  part.swallowed_object => evasobject or nil
139  *
140  * Returns the <code>Evas::EvasObject</code> swallowed by
141  * <i>part</i> or nil if <i>part</i> didn't swallow an
142  * <code>Evas::EvasObject</code>.
143  */
144 static VALUE c_swallowed_object_get (VALUE self)
145 {
146         Evas_Object *o;
147
148         GET_OBJ (GET_EDJE (self), RbEdje, e);
149
150         o = edje_object_part_swallow_get (e->real.real, GET_NAME (self));
151         if (!o)
152                 return Qnil;
153
154         return TO_EVAS_OBJECT (o);
155 }
156
157 /*
158  * call-seq:
159  *  part.text => string
160  *
161  * Returns the text of <i>part</i>.
162  */
163 static VALUE c_text_get (VALUE self)
164 {
165         const char *s;
166
167         GET_OBJ (GET_EDJE (self), RbEdje, e);
168
169         s = edje_object_part_text_get (e->real.real, GET_NAME (self));
170
171         return s ? rb_str_new2 (s) : Qnil;
172 }
173
174 /*
175  * call-seq:
176  *  part.text(string)
177  *
178  * Sets the text of <i>part</i>.
179  */
180 static VALUE c_text_set (VALUE self, VALUE text)
181 {
182         GET_OBJ (GET_EDJE (self), RbEdje, e);
183
184         Check_Type (text, T_STRING);
185
186         edje_object_part_text_set (e->real.real, GET_NAME (self),
187                                    StringValuePtr (text));
188
189         return Qnil;
190 }
191
192 /*
193  * call-seq:
194  *  part.get_drag_value => array
195  *
196  * Returns the drag value of <i>part</i>.
197  *
198  *  part.set_drag_value(1.5, 2.5) #=> nil
199  *  part.get_drag_value           #=> [1.5, 2.5]
200  */
201 static VALUE c_get_drag_value (VALUE self)
202 {
203         double dx = 0, dy = 0;
204
205         GET_OBJ (GET_EDJE (self), RbEdje, e);
206
207         edje_object_part_drag_value_get (e->real.real, GET_NAME (self), &dx, &dy);
208
209         return rb_ary_new3 (2, rb_float_new (dx), rb_float_new (dy));
210 }
211
212 /*
213  * call-seq:
214  *  part.set_drag_value(dx, dy) => nil
215  *
216  * Sets the drag value of <i>part</i>.
217  *
218  *  part.set_drag_value(1.5, 2.5) #=> nil
219  */
220 static VALUE c_set_drag_value (VALUE self, VALUE dx, VALUE dy)
221 {
222         GET_OBJ (GET_EDJE (self), RbEdje, e);
223
224         if (!FIXNUM_P (dx))
225                 Check_Type (dx, T_FLOAT);
226
227         if (!FIXNUM_P (dy))
228                 Check_Type (dy, T_FLOAT);
229
230         edje_object_part_drag_value_set (e->real.real, GET_NAME (self),
231                                          NUM2DBL (dx), NUM2DBL (dy));
232
233         return Qnil;
234 }
235
236 static VALUE c_state_get (VALUE self)
237 {
238         const char *name;
239         double val = 0.0;
240
241         GET_OBJ (GET_EDJE (self), RbEdje, e);
242
243         name = edje_object_part_state_get (e->real.real,
244                                            GET_NAME (self), &val);
245
246         return rb_ary_new3 (2, rb_str_new2 (name), rb_float_new (val));
247 }
248
249 void Init_Part (void)
250 {
251         cPart = rb_define_class_under (mEdje, "Part", rb_cObject);
252
253         rb_define_attr (cPart, "edje", 1, 0);
254         rb_define_attr (cPart, "name", 1, 0);
255
256         /* not publically instantiable yet */
257         rb_define_private_method (rb_singleton_class (cPart),
258                                   "new", NULL, 0);
259         rb_define_method (cPart, "geometry", c_geometry_get, 0);
260         rb_define_method (cPart, "swallow", c_swallow, 1);
261         rb_define_method (cPart, "unswallow", c_unswallow, 1);
262         rb_define_method (cPart, "swallowed_object",
263                           c_swallowed_object_get, 0);
264         rb_define_method (cPart, "text", c_text_get, 0);
265         rb_define_method (cPart, "text=", c_text_set, 1);
266         rb_define_method (cPart, "get_drag_value", c_get_drag_value, 0);
267         rb_define_method (cPart, "set_drag_value", c_set_drag_value, 2);
268         rb_define_method (cPart, "state", c_state_get, 0);
269 }