Lots of new stuff.
[ruby-evas.git] / src / rb_evas.c
1 /*
2  * $Id: rb_evas.c 20 2004-06-22 20:46:56Z 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 #define GET_OBJ(obj, type, o, desc) \
30         type **(o) = NULL; \
31 \
32         Data_Get_Struct ((obj), type *, (o)); \
33 \
34         if (!*(o)) { \
35                 rb_raise (rb_eException, desc " destroyed already"); \
36                 return Qnil; \
37         }
38
39 static VALUE parents;
40
41 static void c_mark (Evas **e)
42 {
43         VALUE parent;
44
45         parent = rb_hash_aref (parents, INT2NUM ((long) (e)));
46         if (parent != Qnil)
47                 rb_gc_mark (parent);
48 }
49
50 VALUE TO_EVAS (VALUE parent, Evas *e)
51 {
52         VALUE self;
53         Evas **my_e = NULL;
54
55         if (parent == Qnil || !e)
56                 return Qnil;
57
58         self = Data_Make_Struct (cEvas, Evas *,
59                                  c_mark, 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_font_path_clear (VALUE self)
70 {
71         GET_OBJ (self, Evas, e, "Evas");
72
73         evas_font_path_clear (*e);
74
75         return Qnil;
76 }
77
78 static VALUE c_font_path_append (VALUE self, VALUE path)
79 {
80         GET_OBJ (self, Evas, e, "Evas");
81
82         Check_Type (path, T_STRING);
83
84         evas_font_path_append (*e, StringValuePtr (path));
85
86         return Qnil;
87 }
88
89 static VALUE c_font_path_prepend (VALUE self, VALUE path)
90 {
91         GET_OBJ (self, Evas, e, "Evas");
92
93         Check_Type (path, T_STRING);
94
95         evas_font_path_append (*e, StringValuePtr (path));
96
97         return Qnil;
98 }
99
100 static VALUE c_font_path_get (VALUE self)
101 {
102         VALUE ary;
103         const Evas_List *list, *l;
104
105         GET_OBJ (self, Evas, e, "Evas");
106
107         if (!(list = evas_font_path_list (*e)))
108                 return rb_ary_new ();
109
110         ary = rb_ary_new2 (evas_list_count ((Evas_List *) list));
111
112         for (l = list; l; l = l->next)
113                 rb_ary_push (ary, rb_str_new2 (l->data));
114
115         return ary;
116 }
117
118 static VALUE c_font_cache_get (VALUE self)
119  {
120         GET_OBJ (self, Evas, e, "Evas");
121
122         return INT2FIX (evas_font_cache_get (*e));
123 }
124
125 static VALUE c_font_cache_set (VALUE self, VALUE val)
126 {
127         GET_OBJ (self, Evas, e, "Evas");
128
129         Check_Type (val, T_FIXNUM);
130
131         evas_font_cache_set (*e, FIX2INT (val));
132
133         return Qnil;
134 }
135
136 static VALUE c_font_cache_flush (VALUE self)
137 {
138         GET_OBJ (self, Evas, e, "Evas");
139
140         evas_font_cache_flush (*e);
141
142         return Qnil;
143 }
144
145 static VALUE c_image_cache_get (VALUE self)
146 {
147         GET_OBJ (self, Evas, e, "Evas");
148
149         return INT2FIX (evas_image_cache_get (*e));
150 }
151
152 static VALUE c_image_cache_set (VALUE self, VALUE val)
153 {
154         GET_OBJ (self, Evas, e, "Evas");
155
156         Check_Type (val, T_FIXNUM);
157
158         evas_image_cache_set (*e, FIX2INT (val));
159
160         return Qnil;
161 }
162
163 static VALUE c_image_cache_reload (VALUE self)
164 {
165         GET_OBJ (self, Evas, e, "Evas");
166
167         evas_image_cache_reload (*e);
168
169         return Qnil;
170 }
171
172 static VALUE c_image_cache_flush (VALUE self)
173 {
174         GET_OBJ (self, Evas, e, "Evas");
175
176         evas_image_cache_flush (*e);
177
178         return Qnil;
179 }
180
181 static VALUE c_top_get (VALUE self)
182 {
183         Evas_Object *o;
184         void *obj;
185
186         GET_OBJ (self, Evas, e, "Evas");
187
188         if (!(o = evas_object_top_get (*e)))
189                 return Qnil;
190
191         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
192                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
193                 return Qnil;
194         }
195
196         return (VALUE) obj;
197 }
198
199 static VALUE c_bottom_get (VALUE self)
200 {
201         Evas_Object *o;
202         void *obj;
203
204         GET_OBJ (self, Evas, e, "Evas");
205
206         if (!(o = evas_object_bottom_get (*e)))
207                 return Qnil;
208
209         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
210                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
211                 return Qnil;
212         }
213
214         return (VALUE) obj;
215 }
216
217 static VALUE c_find_object (VALUE self, VALUE name)
218 {
219         Evas_Object *o;
220         void *obj;
221
222         GET_OBJ (self, Evas, e, "Evas");
223
224         Check_Type (name, T_STRING);
225
226         if (!(o = evas_object_name_find (*e, StringValuePtr (name))))
227                 return Qnil;
228
229         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
230                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
231                 return Qnil;
232         }
233
234         return (VALUE) obj;
235 }
236
237 void Init_Evas (void)
238 {
239         cEvas = rb_define_class_under (mEvas, "Evas", rb_cObject);
240
241         /* not publically instantiable yet */
242         rb_define_private_method (rb_singleton_class (cEvas),
243                                   "new", NULL, 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 }