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