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