Macro updates.
[ruby-evas.git] / src / rb_evas.c
1 /*
2  * $Id: rb_evas.c 32 2004-07-10 14:07:49Z 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 static void c_mark (Evas **e)
32 {
33         VALUE parent;
34
35         parent = rb_hash_aref (parents, INT2NUM ((long) (e)));
36         if (parent != Qnil)
37                 rb_gc_mark (parent);
38 }
39
40 static void c_free (Evas **e)
41 {
42         /* do NOT call evas_free() here, since the Evas is freed
43          * by ecore_evas_free() eventually */
44         rb_hash_aset (parents, INT2NUM ((long) e), Qnil);
45
46         free (e);
47 }
48
49 VALUE TO_EVAS (VALUE parent, Evas *e)
50 {
51         VALUE self;
52         Evas **my_e = NULL;
53
54         if (NIL_P (parent) || !e)
55                 return Qnil;
56
57         self = Data_Make_Struct (cEvas, Evas *,
58                                  c_mark, c_free, my_e);
59         *my_e = e;
60
61         rb_hash_aset (parents, INT2NUM ((long) my_e), parent);
62
63         rb_obj_call_init (self, 0, NULL);
64
65         return self;
66 }
67
68 static VALUE c_inspect (VALUE self)
69 {
70         INSPECT (self, Evas);
71 }
72
73 static VALUE c_render (VALUE self)
74 {
75         GET_OBJ (self, Evas, e);
76
77         evas_render (*e);
78
79         return Qnil;
80 }
81
82 static VALUE c_font_path_clear (VALUE self)
83 {
84         GET_OBJ (self, Evas, e);
85
86         evas_font_path_clear (*e);
87
88         return Qnil;
89 }
90
91 static VALUE c_font_path_append (VALUE self, VALUE path)
92 {
93         GET_OBJ (self, Evas, e);
94
95         Check_Type (path, T_STRING);
96
97         evas_font_path_append (*e, StringValuePtr (path));
98
99         return Qnil;
100 }
101
102 static VALUE c_font_path_prepend (VALUE self, VALUE path)
103 {
104         GET_OBJ (self, Evas, e);
105
106         Check_Type (path, T_STRING);
107
108         evas_font_path_append (*e, StringValuePtr (path));
109
110         return Qnil;
111 }
112
113 static VALUE c_font_path_get (VALUE self)
114 {
115         VALUE ary;
116         const Evas_List *list, *l;
117
118         GET_OBJ (self, Evas, e);
119
120         if (!(list = evas_font_path_list (*e)))
121                 return rb_ary_new ();
122
123         ary = rb_ary_new2 (evas_list_count ((Evas_List *) list));
124
125         for (l = list; l; l = l->next)
126                 rb_ary_push (ary, rb_str_new2 (l->data));
127
128         return ary;
129 }
130
131 static VALUE c_font_cache_get (VALUE self)
132  {
133         GET_OBJ (self, Evas, e);
134
135         return INT2FIX (evas_font_cache_get (*e));
136 }
137
138 static VALUE c_font_cache_set (VALUE self, VALUE val)
139 {
140         GET_OBJ (self, Evas, e);
141
142         Check_Type (val, T_FIXNUM);
143
144         evas_font_cache_set (*e, FIX2INT (val));
145
146         return Qnil;
147 }
148
149 static VALUE c_font_cache_flush (VALUE self)
150 {
151         GET_OBJ (self, Evas, e);
152
153         evas_font_cache_flush (*e);
154
155         return Qnil;
156 }
157
158 static VALUE c_image_cache_get (VALUE self)
159 {
160         GET_OBJ (self, Evas, e);
161
162         return INT2FIX (evas_image_cache_get (*e));
163 }
164
165 static VALUE c_image_cache_set (VALUE self, VALUE val)
166 {
167         GET_OBJ (self, Evas, e);
168
169         Check_Type (val, T_FIXNUM);
170
171         evas_image_cache_set (*e, FIX2INT (val));
172
173         return Qnil;
174 }
175
176 static VALUE c_image_cache_reload (VALUE self)
177 {
178         GET_OBJ (self, Evas, e);
179
180         evas_image_cache_reload (*e);
181
182         return Qnil;
183 }
184
185 static VALUE c_image_cache_flush (VALUE self)
186 {
187         GET_OBJ (self, Evas, e);
188
189         evas_image_cache_flush (*e);
190
191         return Qnil;
192 }
193
194 static VALUE c_top_get (VALUE self)
195 {
196         Evas_Object *o;
197         void *obj;
198
199         GET_OBJ (self, Evas, e);
200
201         if (!(o = evas_object_top_get (*e)))
202                 return Qnil;
203
204         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
205                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
206                 return Qnil;
207         }
208
209         return (VALUE) obj;
210 }
211
212 static VALUE c_bottom_get (VALUE self)
213 {
214         Evas_Object *o;
215         void *obj;
216
217         GET_OBJ (self, Evas, e);
218
219         if (!(o = evas_object_bottom_get (*e)))
220                 return Qnil;
221
222         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
223                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
224                 return Qnil;
225         }
226
227         return (VALUE) obj;
228 }
229
230 static VALUE c_find_object (VALUE self, VALUE name)
231 {
232         Evas_Object *o;
233         void *obj;
234
235         GET_OBJ (self, Evas, e);
236
237         Check_Type (name, T_STRING);
238
239         if (!(o = evas_object_name_find (*e, StringValuePtr (name))))
240                 return Qnil;
241
242         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
243                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
244                 return Qnil;
245         }
246
247         return (VALUE) obj;
248 }
249
250 void Init_Evas (void)
251 {
252         cEvas = rb_define_class_under (mEvas, "Evas", rb_cObject);
253
254         /* not publically instantiable yet */
255         rb_define_private_method (rb_singleton_class (cEvas),
256                                   "new", NULL, 0);
257         rb_define_method (cEvas, "inspect", c_inspect, 0);
258         rb_define_method (cEvas, "render", c_render, 0);
259         rb_define_method (cEvas, "font_path_clear", c_font_path_clear, 0);
260         rb_define_method (cEvas, "font_path_append", c_font_path_append, 1);
261         rb_define_method (cEvas, "font_path_prepend", c_font_path_prepend, 1);
262         rb_define_method (cEvas, "font_path", c_font_path_get, 0);
263         rb_define_method (cEvas, "font_cache", c_font_cache_get, 0);
264         rb_define_method (cEvas, "font_cache=", c_font_cache_set, 1);
265         rb_define_method (cEvas, "font_cache_flush",
266                           c_font_cache_flush, 0);
267         rb_define_method (cEvas, "image_cache", c_image_cache_get, 0);
268         rb_define_method (cEvas, "image_cache=", c_image_cache_set, 1);
269         rb_define_method (cEvas, "image_cache_reload",
270                           c_image_cache_reload, 0);
271         rb_define_method (cEvas, "image_cache_flush",
272                           c_image_cache_flush, 0);
273         rb_define_method (cEvas, "top", c_top_get, 0);
274         rb_define_method (cEvas, "bottom", c_bottom_get, 0);
275         rb_define_method (cEvas, "find_object", c_find_object, 1);
276
277         parents = rb_hash_new ();
278         rb_global_variable (&parents);
279 }