Removed RCS-style IDs.
[ruby-edje.git] / src / rb_part.c
1 /*
2  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <ruby.h>
20
21 #include <Edje.h>
22
23 #include "rb_edje_main.h"
24 #include "rb_edje.h"
25
26 static VALUE cPart;
27
28 static inline char *GET_NAME (VALUE o)
29 {
30         static ID id;
31         VALUE name;
32
33         if (!id)
34                 id = rb_intern ("@name");
35
36         name = rb_ivar_get (o, id);
37
38         return StringValuePtr (name);
39 }
40
41 static inline VALUE GET_EDJE (VALUE o)
42 {
43         static ID id;
44
45         if (!id)
46                 id = rb_intern ("@edje");
47
48         return rb_ivar_get (o, id);
49 }
50
51 VALUE TO_PART (VALUE edje, VALUE name)
52 {
53         VALUE self;
54
55         CHECK_CLASS (edje, cEdje);
56         Check_Type (name, T_STRING);
57
58         self = rb_obj_alloc (cPart);
59
60         rb_iv_set (self, "@edje", edje);
61         rb_iv_set (self, "@name", rb_str_dup (name));
62
63         rb_obj_call_init (self, 0, NULL);
64
65         return self;
66 }
67
68 /*
69  * call-seq:
70  *  part.geometry => array
71  *
72  * Returns an array containing the geometry of <i>part</i>.
73  */
74 static VALUE c_geometry_get (VALUE self)
75 {
76         int x = 0, y = 0, w = 0, h = 0;
77
78         GET_OBJ (GET_EDJE (self), RbEdje, e);
79
80         edje_object_part_geometry_get (e->real.real, GET_NAME (self),
81                                        (Evas_Coord *) &x,
82                                        (Evas_Coord *) &y,
83                                        (Evas_Coord *) &w,
84                                        (Evas_Coord *) &h);
85
86         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y),
87                             INT2FIX (w), INT2FIX (h));
88 }
89
90 /*
91  * call-seq:
92  *  part.swallow(evasobject) => nil
93  *
94  * Swallows an <code>Evas::EvasObject</code> into <i>part</i>.
95  */
96 static VALUE c_swallow (VALUE self, VALUE target)
97 {
98         GET_OBJ (GET_EDJE (self), RbEdje, e);
99
100         CHECK_CLASS (target, cEvasObject);
101         GET_OBJ (target, RbEvasObject, t);
102
103         edje_object_part_swallow (e->real.real, GET_NAME (self), t->real);
104         rb_iv_set (self, "swallowed_obj", target);
105
106         return Qnil;
107 }
108
109 /*
110  * call-seq:
111  *  part.unswallow => nil
112  *
113  * Unswallows the <code>Evas::EvasObject</code> swallowed by
114  * <i>part</i>.
115  */
116 static VALUE c_unswallow (VALUE self)
117 {
118         Evas_Object *o;
119
120         GET_OBJ (GET_EDJE (self), RbEdje, e);
121
122         o = edje_object_part_swallow_get (e->real.real, GET_NAME (self));
123         if (!o) {
124                 rb_raise (rb_eException, "Part didn't swallow an EvasObject");
125                 return Qnil;
126         }
127
128         edje_object_part_unswallow (e->real.real, o);
129         rb_iv_set (self, "swallowed_obj", Qnil);
130
131         return Qnil;
132 }
133
134 /*
135  * call-seq:
136  *  part.swallowed_object => evasobject or nil
137  *
138  * Returns the <code>Evas::EvasObject</code> swallowed by
139  * <i>part</i> or nil if <i>part</i> didn't swallow an
140  * <code>Evas::EvasObject</code>.
141  */
142 static VALUE c_swallowed_object_get (VALUE self)
143 {
144         Evas_Object *o;
145
146         GET_OBJ (GET_EDJE (self), RbEdje, e);
147
148         o = edje_object_part_swallow_get (e->real.real, GET_NAME (self));
149         if (!o)
150                 return Qnil;
151
152         return TO_EVAS_OBJECT (o);
153 }
154
155 /*
156  * call-seq:
157  *  part.text => string
158  *
159  * Returns the text of <i>part</i>.
160  */
161 static VALUE c_text_get (VALUE self)
162 {
163         const char *s;
164
165         GET_OBJ (GET_EDJE (self), RbEdje, e);
166
167         s = edje_object_part_text_get (e->real.real, GET_NAME (self));
168
169         return s ? rb_str_new2 (s) : Qnil;
170 }
171
172 /*
173  * call-seq:
174  *  part.text(string)
175  *
176  * Sets the text of <i>part</i>.
177  */
178 static VALUE c_text_set (VALUE self, VALUE text)
179 {
180         GET_OBJ (GET_EDJE (self), RbEdje, e);
181
182         Check_Type (text, T_STRING);
183
184         edje_object_part_text_set (e->real.real, GET_NAME (self),
185                                    StringValuePtr (text));
186
187         return Qnil;
188 }
189
190 /*
191  * call-seq:
192  *  part.get_drag_value => array
193  *
194  * Returns the drag value of <i>part</i>.
195  *
196  *  part.set_drag_value(1.5, 2.5) #=> nil
197  *  part.get_drag_value           #=> [1.5, 2.5]
198  */
199 static VALUE c_get_drag_value (VALUE self)
200 {
201         double dx = 0, dy = 0;
202
203         GET_OBJ (GET_EDJE (self), RbEdje, e);
204
205         edje_object_part_drag_value_get (e->real.real, GET_NAME (self), &dx, &dy);
206
207         return rb_ary_new3 (2, rb_float_new (dx), rb_float_new (dy));
208 }
209
210 /*
211  * call-seq:
212  *  part.set_drag_value(dx, dy) => nil
213  *
214  * Sets the drag value of <i>part</i>.
215  *
216  *  part.set_drag_value(1.5, 2.5) #=> nil
217  */
218 static VALUE c_set_drag_value (VALUE self, VALUE dx, VALUE dy)
219 {
220         GET_OBJ (GET_EDJE (self), RbEdje, e);
221
222         if (!FIXNUM_P (dx))
223                 Check_Type (dx, T_FLOAT);
224
225         if (!FIXNUM_P (dy))
226                 Check_Type (dy, T_FLOAT);
227
228         edje_object_part_drag_value_set (e->real.real, GET_NAME (self),
229                                          NUM2DBL (dx), NUM2DBL (dy));
230
231         return Qnil;
232 }
233
234 static VALUE c_state_get (VALUE self)
235 {
236         const char *name;
237         double val = 0.0;
238
239         GET_OBJ (GET_EDJE (self), RbEdje, e);
240
241         name = edje_object_part_state_get (e->real.real,
242                                            GET_NAME (self), &val);
243
244         return rb_ary_new3 (2, rb_str_new2 (name), rb_float_new (val));
245 }
246
247 void Init_Part (void)
248 {
249         cPart = rb_define_class_under (mEdje, "Part", rb_cObject);
250
251         rb_define_attr (cPart, "edje", 1, 0);
252         rb_define_attr (cPart, "name", 1, 0);
253
254         /* not publically instantiable yet */
255         rb_define_private_method (rb_singleton_class (cPart),
256                                   "new", NULL, 0);
257         rb_define_method (cPart, "geometry", c_geometry_get, 0);
258         rb_define_method (cPart, "swallow", c_swallow, 1);
259         rb_define_method (cPart, "unswallow", c_unswallow, 1);
260         rb_define_method (cPart, "swallowed_object",
261                           c_swallowed_object_get, 0);
262         rb_define_method (cPart, "text", c_text_get, 0);
263         rb_define_method (cPart, "text=", c_text_set, 1);
264         rb_define_method (cPart, "get_drag_value", c_get_drag_value, 0);
265         rb_define_method (cPart, "set_drag_value", c_set_drag_value, 2);
266         rb_define_method (cPart, "state", c_state_get, 0);
267 }