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