57dd2a6a946275472dc953e84b70ecb556db87c5
[ruby-evas.git] / src / rb_evas_object.c
1 /*
2  * $Id$
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_object.h"
26
27 #define GET_OBJ(obj, e) \
28         Evas_Object **(e) = NULL; \
29 \
30         Data_Get_Struct ((obj), Evas_Object *, (e)); \
31 \
32         if (!*(e)) { \
33                 rb_raise (rb_eException, "EvasObject destroyed already"); \
34                 return Qnil; \
35         }
36
37 static VALUE parents;
38
39 /* called by the child classes */
40 void c_evas_object_free (Evas_Object **e)
41 {
42         if (*e)
43                 evas_object_del (*e);
44
45         free (e);
46 }
47
48 void c_evas_object_mark (Evas_Object **e)
49 {
50         VALUE parent;
51
52         parent = rb_hash_aref (parents, INT2NUM ((long) (e)));
53         if (!NIL_P (parent))
54                 rb_gc_mark (parent);
55 }
56
57 static VALUE c_init (VALUE self, VALUE parent)
58 {
59         GET_OBJ (self, e);
60
61         rb_hash_aset (parents, INT2NUM ((long) e), parent);
62
63         return self;
64 }
65
66 static VALUE c_delete (VALUE self)
67 {
68         GET_OBJ (self, e);
69
70         if (*e) {
71                 evas_object_del (*e);
72                 *e = NULL;
73         }
74
75         return Qnil;
76 }
77
78 static VALUE c_resize (VALUE self, VALUE w, VALUE h)
79 {
80         GET_OBJ (self, e);
81
82         Check_Type (w, T_FIXNUM);
83         Check_Type (h, T_FIXNUM);
84
85         evas_object_resize (*e, (Evas_Coord) FIX2INT (w),
86                                 (Evas_Coord) FIX2INT (h));
87
88         return Qnil;
89 }
90
91 static VALUE c_show (VALUE self)
92 {
93         GET_OBJ (self, e);
94
95         evas_object_show (*e);
96
97         return Qnil;
98 }
99
100 static VALUE c_hide (VALUE self)
101 {
102         GET_OBJ (self, e);
103
104         evas_object_hide (*e);
105
106         return Qnil;
107 }
108
109 static VALUE c_is_visible (VALUE self)
110 {
111         GET_OBJ (self, e);
112
113         return evas_object_visible_get (*e) ? Qtrue : Qfalse;
114 }
115
116 void Init_EvasObject (void)
117 {
118         cEvasObject = rb_define_class ("EvasObject", rb_cObject);
119
120         rb_define_private_method (rb_singleton_class (cEvasObject),
121                                   "new", NULL, 0);
122
123         rb_define_method (cEvasObject, "initialize", c_init, 1);
124         rb_define_method (cEvasObject, "delete", c_delete, 0);
125         rb_define_method (cEvasObject, "resize", c_resize, 2);
126         rb_define_method (cEvasObject, "show", c_show, 0);
127         rb_define_method (cEvasObject, "hide", c_hide, 0);
128         rb_define_method (cEvasObject, "visible?", c_is_visible, 0);
129
130         parents = rb_hash_new ();
131         rb_global_variable (&parents);
132 }