Class instantiation fixes.
[ruby-evas.git] / src / rb_evas.c
1 /*
2  * $Id: rb_evas.c 354 2006-02-10 18:14:08Z 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         Check_Type (path, T_STRING);
102
103         evas_font_path_append (e->real, StringValuePtr (path));
104
105         return Qnil;
106 }
107
108 /*
109  * call-seq:
110  *  e.font_path_prepend(path) => nil
111  *
112  * Prepends a path to the font path for <i>e</i>.
113  */
114 static VALUE c_font_path_prepend (VALUE self, VALUE path)
115 {
116         GET_OBJ (self, RbEvas, e);
117
118         Check_Type (path, T_STRING);
119
120         evas_font_path_append (e->real, StringValuePtr (path));
121
122         return Qnil;
123 }
124
125 /*
126  * call-seq:
127  *  e.font_path => array
128  *
129  * Returns the font path for <i>e</i>.
130  */
131 static VALUE c_font_path_get (VALUE self)
132 {
133         VALUE ary;
134         const Evas_List *list, *l;
135
136         GET_OBJ (self, RbEvas, e);
137
138         if (!(list = evas_font_path_list (e->real)))
139                 return rb_ary_new ();
140
141         ary = rb_ary_new2 (evas_list_count ((Evas_List *) list));
142
143         for (l = list; l; l = l->next)
144                 rb_ary_push (ary, rb_str_new2 (l->data));
145
146         return ary;
147 }
148 /*
149  * call-seq:
150  *  e.font_cache => fixnum
151  *
152  * Returns the size of the font cache for <i>e</i>.
153  */
154 static VALUE c_font_cache_get (VALUE self)
155  {
156         GET_OBJ (self, RbEvas, e);
157
158         return INT2FIX (evas_font_cache_get (e->real));
159 }
160
161 /*
162  * call-seq:
163  *  e.font_cache(fixnum)
164  *
165  * Sets the size of the font cache for <i>e</i>.
166  */
167 static VALUE c_font_cache_set (VALUE self, VALUE val)
168 {
169         GET_OBJ (self, RbEvas, e);
170
171         Check_Type (val, T_FIXNUM);
172
173         evas_font_cache_set (e->real, FIX2INT (val));
174
175         return Qnil;
176 }
177
178 /*
179  * call-seq:
180  *  e.font_cache_reload => nil
181  *
182  * Flushes the font cache for <i>e</i>.
183  */
184 static VALUE c_font_cache_flush (VALUE self)
185 {
186         GET_OBJ (self, RbEvas, e);
187
188         evas_font_cache_flush (e->real);
189
190         return Qnil;
191 }
192
193 /*
194  * call-seq:
195  *  e.image_cache => fixnum
196  *
197  * Returns the size of the image cache for <i>e</i>.
198  */
199 static VALUE c_image_cache_get (VALUE self)
200 {
201         GET_OBJ (self, RbEvas, e);
202
203         return INT2FIX (evas_image_cache_get (e->real));
204 }
205
206 /*
207  * call-seq:
208  *  e.image_cache(fixnum)
209  *
210  * Sets the size of the image cache for <i>e</i>.
211  */
212 static VALUE c_image_cache_set (VALUE self, VALUE val)
213 {
214         GET_OBJ (self, RbEvas, e);
215
216         Check_Type (val, T_FIXNUM);
217
218         evas_image_cache_set (e->real, FIX2INT (val));
219
220         return Qnil;
221 }
222
223 /*
224  * call-seq:
225  *  e.image_cache_reload => nil
226  *
227  * Flushes the image cache for <i>e</i>.
228  */
229 static VALUE c_image_cache_reload (VALUE self)
230 {
231         GET_OBJ (self, RbEvas, e);
232
233         evas_image_cache_reload (e->real);
234
235         return Qnil;
236 }
237
238 /*
239  * call-seq:
240  *  e.image_cache_flush => nil
241  *
242  * Flushes the image cache for <i>e</i>.
243  */
244 static VALUE c_image_cache_flush (VALUE self)
245 {
246         GET_OBJ (self, RbEvas, e);
247
248         evas_image_cache_flush (e->real);
249
250         return Qnil;
251 }
252
253 /*
254  * call-seq:
255  *  e.top => evasobject
256  *
257  * Returns the <code>Evas::EvasObject</code> at the top of <i>e</i>.
258  */
259 static VALUE c_top_get (VALUE self)
260 {
261         Evas_Object *o;
262
263         GET_OBJ (self, RbEvas, e);
264
265         if (!(o = evas_object_top_get (e->real)))
266                 return Qnil;
267
268         return TO_EVAS_OBJECT (o);
269 }
270
271 /*
272  * call-seq:
273  *  e.bottom => evasobject
274  *
275  * Returns the <code>Evas::EvasObject</code> at the bottom of <i>e</i>.
276  */
277 static VALUE c_bottom_get (VALUE self)
278 {
279         Evas_Object *o;
280
281         GET_OBJ (self, RbEvas, e);
282
283         if (!(o = evas_object_bottom_get (e->real)))
284                 return Qnil;
285
286         return TO_EVAS_OBJECT (o);
287 }
288
289 /*
290  * call-seq:
291  *  e.find_object(name) => evasobject
292  *
293  * Returns the <code>Evas::EvasObject</code> with the name <i>name</i>.
294  */
295 static VALUE c_find_object (VALUE self, VALUE name)
296 {
297         Evas_Object *o;
298
299         GET_OBJ (self, RbEvas, e);
300
301         Check_Type (name, T_STRING);
302
303         if (!(o = evas_object_name_find (e->real, StringValuePtr (name))))
304                 return Qnil;
305
306         return TO_EVAS_OBJECT (o);
307 }
308
309 static VALUE c_output_size_get (VALUE self)
310 {
311         int w = 0, h = 0;
312
313         GET_OBJ (self, RbEvas, e);
314
315         evas_output_size_get (e->real, &w, &h);
316
317         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
318 }
319
320 static VALUE c_output_viewport_get (VALUE self)
321 {
322         int x = 0, y = 0, w = 0, h = 0;
323
324         GET_OBJ (self, RbEvas, e);
325
326         evas_output_viewport_get (e->real,
327                                   (Evas_Coord *) &x, (Evas_Coord *) &y,
328                                   (Evas_Coord *) &w, (Evas_Coord *) &h);
329
330         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y), INT2FIX (w),
331                             INT2FIX (h));
332 }
333
334 void Init_Evas (void)
335 {
336         cEvas = rb_define_class_under (mEvas, "Evas", rb_cObject);
337
338         rb_define_alloc_func (cEvas, c_alloc);
339
340         /* not publically instantiable yet */
341         rb_define_private_method (rb_singleton_class (cEvas),
342                                   "new", NULL, 0);
343         rb_define_method (cEvas, "inspect", c_inspect, 0);
344         rb_define_method (cEvas, "render", c_render, 0);
345         rb_define_method (cEvas, "font_path_clear", c_font_path_clear, 0);
346         rb_define_method (cEvas, "font_path_append", c_font_path_append, 1);
347         rb_define_method (cEvas, "font_path_prepend", c_font_path_prepend, 1);
348         rb_define_method (cEvas, "font_path", c_font_path_get, 0);
349         rb_define_method (cEvas, "font_cache", c_font_cache_get, 0);
350         rb_define_method (cEvas, "font_cache=", c_font_cache_set, 1);
351         rb_define_method (cEvas, "font_cache_flush",
352                           c_font_cache_flush, 0);
353         rb_define_method (cEvas, "image_cache", c_image_cache_get, 0);
354         rb_define_method (cEvas, "image_cache=", c_image_cache_set, 1);
355         rb_define_method (cEvas, "image_cache_reload",
356                           c_image_cache_reload, 0);
357         rb_define_method (cEvas, "image_cache_flush",
358                           c_image_cache_flush, 0);
359         rb_define_method (cEvas, "top", c_top_get, 0);
360         rb_define_method (cEvas, "bottom", c_bottom_get, 0);
361         rb_define_method (cEvas, "find_object", c_find_object, 1);
362         rb_define_method (cEvas, "output_size", c_output_size_get, 0);
363         rb_define_method (cEvas, "output_viewport", c_output_viewport_get, 0);
364 }