Minor doc improvements.
[ruby-edje.git] / src / rb_part.c
1 /*
2  * $Id: rb_part.c 59 2004-08-10 14:10:31Z 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 (y), 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
107         return Qnil;
108 }
109
110 /*
111  * call-seq:
112  *  part.unswallow => nil
113  *
114  * Unswallows the <code>Evas::EvasObject</code> swallowed by
115  * <i>part</i>.
116  */
117 static VALUE c_unswallow (VALUE self)
118 {
119         Evas_Object *o;
120
121         GET_OBJ (GET_EDJE (self), RbEdje, e);
122
123         o = edje_object_part_swallow_get (e->real.real, GET_NAME (self));
124         if (!o) {
125                 rb_raise (rb_eException, "Part didn't swallow an EvasObject");
126                 return Qnil;
127         }
128
129         edje_object_part_unswallow (e->real.real, o);
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         void *obj;
146
147         GET_OBJ (GET_EDJE (self), RbEdje, e);
148
149         o = edje_object_part_swallow_get (e->real.real, GET_NAME (self));
150         if (!o)
151                 return Qnil;
152
153         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
154                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
155                 return Qnil;
156         }
157
158         return (VALUE) obj;
159 }
160
161 /*
162  * call-seq:
163  *  part.text => string
164  *
165  * Returns the text of <i>part</i>.
166  */
167 static VALUE c_text_get (VALUE self)
168 {
169         const char *s;
170
171         GET_OBJ (GET_EDJE (self), RbEdje, e);
172
173         s = edje_object_part_text_get (e->real.real, GET_NAME (self));
174
175         return s ? rb_str_new2 (s) : Qnil;
176 }
177
178 /*
179  * call-seq:
180  *  part.text(string)
181  *
182  * Sets the text of <i>part</i>.
183  */
184 static VALUE c_text_set (VALUE self, VALUE text)
185 {
186         GET_OBJ (GET_EDJE (self), RbEdje, e);
187
188         Check_Type (text, T_STRING);
189
190         edje_object_part_text_set (e->real.real, GET_NAME (self),
191                                    StringValuePtr (text));
192
193         return Qnil;
194 }
195
196 /*
197  * call-seq:
198  *  part.get_drag_value => array
199  *
200  * Returns the drag value of <i>part</i>.
201  *
202  *  part.set_drag_value(1.5, 2.5) #=> nil
203  *  part.get_drag_value           #=> [1.5, 2.5]
204  */
205 static VALUE c_get_drag_value (VALUE self)
206 {
207         double dx = 0, dy = 0;
208
209         GET_OBJ (GET_EDJE (self), RbEdje, e);
210
211         edje_object_part_drag_value_get (e->real.real, GET_NAME (self), &dx, &dy);
212
213         return rb_ary_new3 (2, rb_float_new (dx), rb_float_new (dy));
214 }
215
216 /*
217  * call-seq:
218  *  part.set_drag_value(dx, dy) => nil
219  *
220  * Sets the drag value of <i>part</i>.
221  *
222  *  part.set_drag_value(1.5, 2.5) #=> nil
223  */
224 static VALUE c_set_drag_value (VALUE self, VALUE dx, VALUE dy)
225 {
226         GET_OBJ (GET_EDJE (self), RbEdje, e);
227
228         if (!FIXNUM_P (dx))
229                 Check_Type (dx, T_FLOAT);
230
231         if (!FIXNUM_P (dy))
232                 Check_Type (dy, T_FLOAT);
233
234         edje_object_part_drag_value_set (e->real.real, GET_NAME (self),
235                                          NUM2DBL (dx), NUM2DBL (dy));
236
237         return Qnil;
238 }
239
240 void Init_Part (void)
241 {
242         cPart = rb_define_class_under (mEdje, "Part", rb_cObject);
243
244         rb_define_attr (cPart, "edje", 1, 0);
245         rb_define_attr (cPart, "name", 1, 0);
246
247         /* not publically instantiable yet */
248         rb_define_private_method (rb_singleton_class (cPart),
249                                   "new", NULL, 0);
250         rb_define_method (cPart, "geometry", c_geometry_get, 0);
251         rb_define_method (cPart, "swallow", c_swallow, 1);
252         rb_define_method (cPart, "unswallow", c_unswallow, 1);
253         rb_define_method (cPart, "swallowed_object",
254                           c_swallowed_object_get, 0);
255         rb_define_method (cPart, "text", c_text_get, 0);
256         rb_define_method (cPart, "text=", c_text_set, 1);
257         rb_define_method (cPart, "get_drag_value", c_get_drag_value, 2);
258         rb_define_method (cPart, "set_drag_value", c_set_drag_value, 2);
259 }