ff34c25d2dbf4383aad48a0ea3de175683500656
[ruby-evas.git] / src / rb_evas.c
1 /*
2  * $Id: rb_evas.c 383 2006-05-25 09:21:10Z 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 #define __RB_EVAS_C
26 #include "rb_evas_main.h"
27 #include "rb_evas.h"
28 #include "rb_evas_object.h"
29
30 VALUE cEvas;
31
32 static void c_mark (RbEvas *e)
33 {
34         rb_gc_mark (e->parent);
35 }
36
37 static VALUE c_alloc (VALUE klass)
38 {
39         RbEvas *evas = NULL;
40
41         return Data_Make_Struct (cEvas, RbEvas, c_mark, free, evas);
42 }
43
44 VALUE TO_EVAS (VALUE parent, Evas *e)
45 {
46         VALUE self;
47
48         if (NIL_P (parent) || !e)
49                 return Qnil;
50
51         self = rb_class_new_instance (0, NULL, cEvas);
52
53         GET_OBJ (self, RbEvas, evas);
54
55         evas->real = e;
56         evas->parent = parent;
57
58         return self;
59 }
60
61 /* :nodoc: */
62 static VALUE c_inspect (VALUE self)
63 {
64         INSPECT (self, RbEvas);
65 }
66
67 /*
68  * call-seq:
69  *  e.render => nil
70  *
71  * Forces a re-render of the Evas.
72  */
73 static VALUE c_render (VALUE self)
74 {
75         GET_OBJ (self, RbEvas, e);
76
77         evas_render (e->real);
78
79         return Qnil;
80 }
81
82 static VALUE c_font_path_clear (VALUE self)
83 {
84         GET_OBJ (self, RbEvas, e);
85
86         evas_font_path_clear (e->real);
87
88         return Qnil;
89 }
90
91 /*
92  * call-seq:
93  *  e.font_path_append(path) => nil
94  *
95  * Appends a path to the font path for <i>e</i>.
96  */
97 static VALUE c_font_path_append (VALUE self, VALUE path)
98 {
99         GET_OBJ (self, RbEvas, e);
100
101         evas_font_path_append (e->real, StringValuePtr (path));
102
103         return Qnil;
104 }
105
106 /*
107  * call-seq:
108  *  e.font_path_prepend(path) => nil
109  *
110  * Prepends a path to the font path for <i>e</i>.
111  */
112 static VALUE c_font_path_prepend (VALUE self, VALUE path)
113 {
114         GET_OBJ (self, RbEvas, e);
115
116         evas_font_path_prepend (e->real, StringValuePtr (path));
117
118         return Qnil;
119 }
120
121 /*
122  * call-seq:
123  *  e.font_path => array
124  *
125  * Returns the font path for <i>e</i>.
126  */
127 static VALUE c_font_path_get (VALUE self)
128 {
129         VALUE ary;
130         const Evas_List *list, *l;
131
132         GET_OBJ (self, RbEvas, e);
133
134         if (!(list = evas_font_path_list (e->real)))
135                 return rb_ary_new ();
136
137         ary = rb_ary_new2 (evas_list_count ((Evas_List *) list));
138
139         for (l = list; l; l = l->next)
140                 rb_ary_push (ary, rb_str_new2 (l->data));
141
142         return ary;
143 }
144 /*
145  * call-seq:
146  *  e.font_cache => fixnum
147  *
148  * Returns the size of the font cache for <i>e</i>.
149  */
150 static VALUE c_font_cache_get (VALUE self)
151  {
152         GET_OBJ (self, RbEvas, e);
153
154         return INT2FIX (evas_font_cache_get (e->real));
155 }
156
157 /*
158  * call-seq:
159  *  e.font_cache(fixnum)
160  *
161  * Sets the size of the font cache for <i>e</i>.
162  */
163 static VALUE c_font_cache_set (VALUE self, VALUE val)
164 {
165         GET_OBJ (self, RbEvas, e);
166
167         Check_Type (val, T_FIXNUM);
168
169         evas_font_cache_set (e->real, FIX2INT (val));
170
171         return Qnil;
172 }
173
174 /*
175  * call-seq:
176  *  e.font_cache_reload => nil
177  *
178  * Flushes the font cache for <i>e</i>.
179  */
180 static VALUE c_font_cache_flush (VALUE self)
181 {
182         GET_OBJ (self, RbEvas, e);
183
184         evas_font_cache_flush (e->real);
185
186         return Qnil;
187 }
188
189 /*
190  * call-seq:
191  *  e.image_cache => fixnum
192  *
193  * Returns the size of the image cache for <i>e</i>.
194  */
195 static VALUE c_image_cache_get (VALUE self)
196 {
197         GET_OBJ (self, RbEvas, e);
198
199         return INT2FIX (evas_image_cache_get (e->real));
200 }
201
202 /*
203  * call-seq:
204  *  e.image_cache(fixnum)
205  *
206  * Sets the size of the image cache for <i>e</i>.
207  */
208 static VALUE c_image_cache_set (VALUE self, VALUE val)
209 {
210         GET_OBJ (self, RbEvas, e);
211
212         Check_Type (val, T_FIXNUM);
213
214         evas_image_cache_set (e->real, FIX2INT (val));
215
216         return Qnil;
217 }
218
219 /*
220  * call-seq:
221  *  e.image_cache_reload => nil
222  *
223  * Flushes the image cache for <i>e</i>.
224  */
225 static VALUE c_image_cache_reload (VALUE self)
226 {
227         GET_OBJ (self, RbEvas, e);
228
229         evas_image_cache_reload (e->real);
230
231         return Qnil;
232 }
233
234 /*
235  * call-seq:
236  *  e.image_cache_flush => nil
237  *
238  * Flushes the image cache for <i>e</i>.
239  */
240 static VALUE c_image_cache_flush (VALUE self)
241 {
242         GET_OBJ (self, RbEvas, e);
243
244         evas_image_cache_flush (e->real);
245
246         return Qnil;
247 }
248
249 /*
250  * call-seq:
251  *  e.top => evasobject
252  *
253  * Returns the <code>Evas::EvasObject</code> at the top of <i>e</i>.
254  */
255 static VALUE c_top_get (VALUE self)
256 {
257         Evas_Object *o;
258
259         GET_OBJ (self, RbEvas, e);
260
261         if (!(o = evas_object_top_get (e->real)))
262                 return Qnil;
263
264         return TO_EVAS_OBJECT (o);
265 }
266
267 /*
268  * call-seq:
269  *  e.bottom => evasobject
270  *
271  * Returns the <code>Evas::EvasObject</code> at the bottom of <i>e</i>.
272  */
273 static VALUE c_bottom_get (VALUE self)
274 {
275         Evas_Object *o;
276
277         GET_OBJ (self, RbEvas, e);
278
279         if (!(o = evas_object_bottom_get (e->real)))
280                 return Qnil;
281
282         return TO_EVAS_OBJECT (o);
283 }
284
285 /*
286  * call-seq:
287  *  e.find_object(name) => evasobject
288  *
289  * Returns the <code>Evas::EvasObject</code> with the name <i>name</i>.
290  */
291 static VALUE c_find_object (VALUE self, VALUE name)
292 {
293         Evas_Object *o;
294
295         GET_OBJ (self, RbEvas, e);
296
297         if (!(o = evas_object_name_find (e->real, StringValuePtr (name))))
298                 return Qnil;
299
300         return TO_EVAS_OBJECT (o);
301 }
302
303 static VALUE c_output_size_get (VALUE self)
304 {
305         int w = 0, h = 0;
306
307         GET_OBJ (self, RbEvas, e);
308
309         evas_output_size_get (e->real, &w, &h);
310
311         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
312 }
313
314 static VALUE c_output_viewport_get (VALUE self)
315 {
316         int x = 0, y = 0, w = 0, h = 0;
317
318         GET_OBJ (self, RbEvas, e);
319
320         evas_output_viewport_get (e->real,
321                                   (Evas_Coord *) &x, (Evas_Coord *) &y,
322                                   (Evas_Coord *) &w, (Evas_Coord *) &h);
323
324         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y), INT2FIX (w),
325                             INT2FIX (h));
326 }
327
328 void Init_Evas (void)
329 {
330         cEvas = rb_define_class_under (mEvas, "Evas", rb_cObject);
331
332         rb_define_alloc_func (cEvas, c_alloc);
333
334         /* not publically instantiable yet */
335         rb_define_private_method (rb_singleton_class (cEvas),
336                                   "new", NULL, 0);
337         rb_define_method (cEvas, "inspect", c_inspect, 0);
338         rb_define_method (cEvas, "render", c_render, 0);
339         rb_define_method (cEvas, "font_path_clear", c_font_path_clear, 0);
340         rb_define_method (cEvas, "font_path_append", c_font_path_append, 1);
341         rb_define_method (cEvas, "font_path_prepend", c_font_path_prepend, 1);
342         rb_define_method (cEvas, "font_path", c_font_path_get, 0);
343         rb_define_method (cEvas, "font_cache", c_font_cache_get, 0);
344         rb_define_method (cEvas, "font_cache=", c_font_cache_set, 1);
345         rb_define_method (cEvas, "font_cache_flush",
346                           c_font_cache_flush, 0);
347         rb_define_method (cEvas, "image_cache", c_image_cache_get, 0);
348         rb_define_method (cEvas, "image_cache=", c_image_cache_set, 1);
349         rb_define_method (cEvas, "image_cache_reload",
350                           c_image_cache_reload, 0);
351         rb_define_method (cEvas, "image_cache_flush",
352                           c_image_cache_flush, 0);
353         rb_define_method (cEvas, "top", c_top_get, 0);
354         rb_define_method (cEvas, "bottom", c_bottom_get, 0);
355         rb_define_method (cEvas, "find_object", c_find_object, 1);
356         rb_define_method (cEvas, "output_size", c_output_size_get, 0);
357         rb_define_method (cEvas, "output_viewport", c_output_viewport_get, 0);
358 }