We now use real structs to wrap objects.
[ruby-evas.git] / src / rb_evas_object.c
1 /*
2  * $Id: rb_evas_object.c 49 2004-08-01 10:17:39Z 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 #include <stdbool.h>
23
24 #include <Evas.h>
25
26 #define __RB_EVAS_OBJECT_C
27 #include "rb_evas_main.h"
28 #include "rb_evas.h"
29 #include "rb_evas_object.h"
30
31 VALUE cEvasObject;
32
33 /* called by the child classes */
34 void c_evas_object_mark (RbEvasObject *e)
35 {
36         rb_gc_mark (e->parent);
37 }
38
39 void c_evas_object_free (RbEvasObject *e, bool free_mem)
40 {
41         if (e->real)
42                 evas_object_del (e->real);
43
44         if (free_mem)
45                 free (e);
46 }
47
48 static VALUE c_init (VALUE self, VALUE parent)
49 {
50         GET_OBJ (self, RbEvasObject, e);
51
52         evas_object_data_set (e->real, RUBY_EVAS_OBJECT_KEY, (void *) self);
53
54         e->parent = parent;
55
56         return self;
57 }
58
59 static VALUE c_inspect (VALUE self)
60 {
61         INSPECT (self, RbEvasObject);
62 }
63
64 static VALUE c_delete (VALUE self)
65 {
66         GET_OBJ (self, RbEvasObject, e);
67
68         evas_object_del (e->real);
69         e->real = NULL;
70
71         return Qnil;
72 }
73
74 static VALUE c_resize (VALUE self, VALUE w, VALUE h)
75 {
76         GET_OBJ (self, RbEvasObject, e);
77
78         Check_Type (w, T_FIXNUM);
79         Check_Type (h, T_FIXNUM);
80
81         evas_object_resize (e->real, (Evas_Coord) FIX2INT (w),
82                             (Evas_Coord) FIX2INT (h));
83
84         return Qnil;
85 }
86
87 static VALUE c_move (VALUE self, VALUE x, VALUE y)
88 {
89         GET_OBJ (self, RbEvasObject, e);
90
91         Check_Type (x, T_FIXNUM);
92         Check_Type (y, T_FIXNUM);
93
94         evas_object_move (e->real, (Evas_Coord) FIX2INT (x),
95                           (Evas_Coord) FIX2INT (y));
96
97         return Qnil;
98 }
99
100 static VALUE c_geometry_get (VALUE self)
101 {
102         int x = 0, y = 0, w = 0, h = 0;
103
104         GET_OBJ (self, RbEvasObject, e);
105
106         evas_object_geometry_get (e->real,
107                                   (Evas_Coord *) &x, (Evas_Coord *) &y,
108                                   (Evas_Coord *) &w, (Evas_Coord *) &h);
109
110         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y), INT2FIX (w),
111                             INT2FIX (h));
112 }
113
114 static VALUE c_show (VALUE self)
115 {
116         GET_OBJ (self, RbEvasObject, e);
117
118         evas_object_show (e->real);
119
120         return Qnil;
121 }
122
123 static VALUE c_hide (VALUE self)
124 {
125         GET_OBJ (self, RbEvasObject, e);
126
127         evas_object_hide (e->real);
128
129         return Qnil;
130 }
131
132 static VALUE c_visible_get (VALUE self)
133 {
134         GET_OBJ (self, RbEvasObject, e);
135
136         return evas_object_visible_get (e->real) ? Qtrue : Qfalse;
137 }
138
139 static VALUE c_evas_get (VALUE self)
140 {
141         GET_OBJ (self, RbEvasObject, e);
142
143         return e->parent;
144 }
145
146 static VALUE c_name_get (VALUE self)
147 {
148         const char *name;
149
150         GET_OBJ (self, RbEvasObject, e);
151
152         if (!(name = evas_object_name_get (e->real)))
153                 return Qnil;
154         else
155                 return rb_str_new2 (name);
156 }
157
158 static VALUE c_name_set (VALUE self, VALUE val)
159 {
160         GET_OBJ (self, RbEvasObject, e);
161
162         Check_Type (val, T_STRING);
163
164         evas_object_name_set (e->real, StringValuePtr (val));
165
166         return Qnil;
167 }
168
169 static VALUE c_layer_get (VALUE self)
170 {
171         GET_OBJ (self, RbEvasObject, e);
172
173         return INT2FIX (evas_object_layer_get (e->real));
174 }
175
176 static VALUE c_layer_set (VALUE self, VALUE val)
177 {
178         GET_OBJ (self, RbEvasObject, e);
179
180         Check_Type (val, T_FIXNUM);
181
182         evas_object_layer_set (e->real, NUM2INT (val));
183
184         return Qnil;
185 }
186
187 static VALUE c_get_color (VALUE self)
188 {
189         int r = 0, g = 0, b = 0, a = 0;
190
191         GET_OBJ (self, RbEvasObject, e);
192
193         evas_object_color_get (e->real, &r, &g, &b, &a);
194
195         return rb_ary_new3 (4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
196                             INT2FIX (a));
197 }
198
199 static VALUE c_set_color (VALUE self, VALUE r, VALUE g, VALUE b,
200                           VALUE a)
201 {
202         GET_OBJ (self, RbEvasObject, e);
203
204         Check_Type (r, T_FIXNUM);
205         Check_Type (g, T_FIXNUM);
206         Check_Type (b, T_FIXNUM);
207         Check_Type (a, T_FIXNUM);
208
209         evas_object_color_set (e->real, FIX2INT (r), FIX2INT (g),
210                                FIX2INT (b), FIX2INT (a));
211
212         return Qnil;
213 }
214
215 static VALUE c_pass_events_get (VALUE self)
216 {
217         GET_OBJ (self, RbEvasObject, e);
218
219         return evas_object_pass_events_get (e->real) ? Qtrue : Qfalse;
220 }
221
222 static VALUE c_pass_events_set (VALUE self, VALUE val)
223 {
224         GET_OBJ (self, RbEvasObject, e);
225
226         CHECK_BOOL (val);
227
228         evas_object_pass_events_set (e->real, val == Qtrue);
229
230         return Qnil;
231 }
232
233 static VALUE c_raise (VALUE self)
234 {
235         GET_OBJ (self, RbEvasObject, e);
236
237         evas_object_raise (e->real);
238
239         return Qnil;
240 }
241
242 static VALUE c_lower (VALUE self)
243 {
244         GET_OBJ (self, RbEvasObject, e);
245
246         evas_object_lower (e->real);
247
248         return Qnil;
249 }
250
251 static VALUE c_stack_above (VALUE self, VALUE target)
252 {
253         GET_OBJ (self, RbEvasObject, e);
254
255         if (!rb_obj_is_kind_of (target, cEvasObject)) {
256                 rb_raise (rb_eTypeError,
257                           "wrong argument type %s (expected EvasObject)",
258                           rb_obj_classname (target));
259                 return Qnil;
260         }
261
262         GET_OBJ (target, RbEvasObject, t);
263
264         evas_object_stack_above (e->real, t->real);
265
266         return Qnil;
267 }
268
269 static VALUE c_stack_below (VALUE self, VALUE target)
270 {
271         GET_OBJ (self, RbEvasObject, e);
272
273         if (!rb_obj_is_kind_of (target, cEvasObject)) {
274                 rb_raise (rb_eTypeError,
275                           "wrong argument type %s (expected EvasObject)",
276                           rb_obj_classname (target));
277                 return Qnil;
278         }
279
280         GET_OBJ (target, RbEvasObject, t);
281
282         evas_object_stack_below (e->real, t->real);
283
284         return Qnil;
285 }
286
287 static VALUE c_above_get (VALUE self)
288 {
289         Evas_Object *o;
290         void *obj;
291
292         GET_OBJ (self, RbEvasObject, e);
293
294         if (!(o = evas_object_above_get (e->real)))
295                 return Qnil;
296
297         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
298                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
299                 return Qnil;
300         }
301
302         return (VALUE) obj;
303 }
304
305 static VALUE c_below_get (VALUE self)
306 {
307         Evas_Object *o;
308         void *obj;
309
310         GET_OBJ (self, RbEvasObject, e);
311
312         if (!(o = evas_object_below_get (e->real)))
313                 return Qnil;
314
315         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
316                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
317                 return Qnil;
318         }
319
320         return (VALUE) obj;
321 }
322
323 void Init_EvasObject (void)
324 {
325         cEvasObject = rb_define_class_under (mEvas, "EvasObject",
326                                              rb_cObject);
327
328         rb_define_private_method (rb_singleton_class (cEvasObject),
329                                   "new", NULL, 0);
330         rb_define_method (cEvasObject, "initialize", c_init, 1);
331         rb_define_method (cEvasObject, "inspect", c_inspect, 0);
332         rb_define_method (cEvasObject, "delete", c_delete, 0);
333         rb_define_method (cEvasObject, "resize", c_resize, 2);
334         rb_define_method (cEvasObject, "move", c_move, 2);
335         rb_define_method (cEvasObject, "geometry", c_geometry_get, 0);
336         rb_define_method (cEvasObject, "show", c_show, 0);
337         rb_define_method (cEvasObject, "hide", c_hide, 0);
338         rb_define_method (cEvasObject, "visible?", c_visible_get, 0);
339         rb_define_method (cEvasObject, "evas", c_evas_get, 0);
340         rb_define_method (cEvasObject, "name", c_name_get, 0);
341         rb_define_method (cEvasObject, "name=", c_name_set, 1);
342         rb_define_method (cEvasObject, "layer", c_layer_get, 0);
343         rb_define_method (cEvasObject, "layer=", c_layer_set, 1);
344         rb_define_method (cEvasObject, "get_color", c_get_color, 0);
345         rb_define_method (cEvasObject, "set_color", c_set_color, 4);
346         rb_define_method (cEvasObject, "pass_events?", c_pass_events_get, 0);
347         rb_define_method (cEvasObject, "pass_events=", c_pass_events_set, 1);
348         rb_define_method (cEvasObject, "raise", c_raise, 0);
349         rb_define_method (cEvasObject, "lower", c_lower, 0);
350         rb_define_method (cEvasObject, "stack_above", c_stack_above, 1);
351         rb_define_method (cEvasObject, "stack_below", c_stack_below, 1);
352         rb_define_method (cEvasObject, "above", c_above_get, 0);
353         rb_define_method (cEvasObject, "below", c_below_get, 0);
354 }