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