Don't create a new Evas Ruby object everytime ecore_evas_get() is called.
[ruby-ecore.git] / src / ecore_evas / rb_ecore_evas.c
1 /*
2  * $Id: rb_ecore_evas.c 26 2004-07-06 18:27:19Z 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 <Ecore_Evas.h>
24 #include <rb_evas.h>
25
26 #include "rb_ecore_evas_main.h"
27 #include "rb_ecore_evas.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 #define CHECK_BOOL(val) \
40         if (TYPE ((val)) != T_TRUE && TYPE ((val)) != T_FALSE) { \
41                 rb_raise (rb_eTypeError, \
42                           "wrong argument type %s (expected true or false)", \
43                           rb_obj_classname ((val))); \
44                 return Qnil; \
45         }
46
47 static VALUE evases;
48
49 /* called by the child classes */
50 void c_ecore_evas_free (Ecore_Evas **ee)
51 {
52         if (*ee)
53                 ecore_evas_free (*ee);
54
55         rb_hash_aset (evases, INT2NUM ((long) ee), Qnil);
56
57         free (ee);
58 }
59
60 static VALUE c_inspect (VALUE self)
61 {
62         char buf[128];
63
64         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
65
66         snprintf (buf, sizeof (buf), "#<EcoreEvas:%p ptr=%p>",
67                   (void *) self, *ee);
68
69         return rb_str_new2 (buf);
70 }
71
72 static VALUE c_show (VALUE self)
73 {
74         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
75
76         ecore_evas_show (*ee);
77
78         return Qnil;
79 }
80
81 static VALUE c_hide (VALUE self)
82 {
83         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
84
85         ecore_evas_hide (*ee);
86
87         return Qnil;
88 }
89
90 static VALUE c_visible_get (VALUE self)
91 {
92         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
93
94         return ecore_evas_visibility_get (*ee) ? Qtrue : Qfalse;
95 }
96
97 static VALUE c_raise (VALUE self)
98 {
99         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
100
101         ecore_evas_raise (*ee);
102
103         return Qnil;
104 }
105
106 static VALUE c_lower (VALUE self)
107 {
108         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
109
110         ecore_evas_lower (*ee);
111
112         return Qnil;
113 }
114
115 static VALUE c_layer_get (VALUE self)
116 {
117         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
118
119         return INT2FIX (ecore_evas_layer_get (*ee));
120 }
121
122 static VALUE c_layer_set (VALUE self, VALUE val)
123 {
124         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
125
126         Check_Type (val, T_FIXNUM);
127
128         ecore_evas_layer_set (*ee, FIX2INT (val));
129
130         return Qnil;
131 }
132
133 static VALUE c_evas (VALUE self)
134 {
135         VALUE evas;
136
137         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
138
139         if (NIL_P (evas = rb_hash_aref (evases, INT2NUM ((long) (ee))))) {
140                 evas = TO_EVAS (self, ecore_evas_get (*ee));
141                 rb_hash_aset (evases, INT2NUM ((long) ee), evas);
142         }
143
144         return evas;
145 }
146
147 static VALUE c_get_size_min (VALUE self)
148 {
149         int w = 0, h = 0;
150
151         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
152
153         ecore_evas_size_min_get (*ee, &w, &h);
154
155         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
156 }
157
158 static VALUE c_set_size_min (VALUE self, VALUE w, VALUE h)
159 {
160         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
161
162         Check_Type (w, T_FIXNUM);
163         Check_Type (h, T_FIXNUM);
164
165         ecore_evas_size_min_set (*ee, FIX2INT (w), FIX2INT (h));
166
167         return Qnil;
168 }
169
170 static VALUE c_get_size_max (VALUE self)
171 {
172         int w = 0, h = 0;
173
174         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
175
176         ecore_evas_size_max_get (*ee, &w, &h);
177
178         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
179 }
180
181 static VALUE c_set_size_max (VALUE self, VALUE w, VALUE h)
182 {
183         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
184
185         Check_Type (w, T_FIXNUM);
186         Check_Type (h, T_FIXNUM);
187
188         ecore_evas_size_max_set (*ee, FIX2INT (w), FIX2INT (h));
189
190         return Qnil;
191 }
192
193 static VALUE c_move (VALUE self, VALUE x, VALUE y)
194 {
195         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
196
197         Check_Type (x, T_FIXNUM);
198         Check_Type (y, T_FIXNUM);
199
200         ecore_evas_move (*ee, FIX2INT (x), FIX2INT (y));
201
202         return Qnil;
203 }
204
205 static VALUE c_resize (VALUE self, VALUE w, VALUE h)
206 {
207         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
208
209         Check_Type (w, T_FIXNUM);
210         Check_Type (h, T_FIXNUM);
211
212         ecore_evas_resize (*ee, FIX2INT (w), FIX2INT (h));
213
214         return Qnil;
215 }
216
217 static VALUE c_title_get (VALUE self)
218 {
219         const char *tmp;
220
221         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
222
223         if (!(tmp = ecore_evas_title_get (*ee)))
224                 return Qnil;
225         else
226                 return rb_str_new2 (tmp);
227 }
228
229 static VALUE c_title_set (VALUE self, VALUE val)
230 {
231         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
232
233         Check_Type (val, T_STRING);
234
235         ecore_evas_title_set (*ee, StringValuePtr (val));
236
237         return Qnil;
238 }
239
240 static VALUE c_borderless_get (VALUE self)
241 {
242         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
243
244         return ecore_evas_borderless_get (*ee) ? Qtrue : Qfalse;
245 }
246
247 static VALUE c_borderless_set (VALUE self, VALUE val)
248 {
249         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
250
251         CHECK_BOOL (val);
252
253         ecore_evas_borderless_set (*ee, val == Qtrue ? 1 : 0);
254
255         return Qnil;
256 }
257
258 static VALUE c_shaped_get (VALUE self)
259 {
260         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
261
262         return ecore_evas_shaped_get (*ee) ? Qtrue : Qfalse;
263 }
264
265 static VALUE c_shaped_set (VALUE self, VALUE val)
266 {
267         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
268
269         CHECK_BOOL (val);
270
271         ecore_evas_shaped_set (*ee, val == Qtrue ? 1 : 0);
272
273         return Qnil;
274 }
275
276 static VALUE c_sticky_get (VALUE self)
277 {
278         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
279
280         return ecore_evas_sticky_get (*ee) ? Qtrue : Qfalse;
281 }
282
283 static VALUE c_sticky_set (VALUE self, VALUE val)
284 {
285         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
286
287         CHECK_BOOL (val);
288
289         ecore_evas_sticky_set (*ee, val == Qtrue ? 1 : 0);
290
291         return Qnil;
292 }
293
294 static VALUE c_rotation_get (VALUE self)
295 {
296         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
297
298         return INT2FIX (ecore_evas_rotation_get (*ee));
299 }
300
301 static VALUE c_rotation_set (VALUE self, VALUE val)
302 {
303         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
304
305         Check_Type (val, T_FIXNUM);
306
307         ecore_evas_rotation_set (*ee, FIX2INT (val));
308
309         return Qnil;
310 }
311
312 static VALUE c_delete (VALUE self)
313 {
314         GET_OBJ (self, Ecore_Evas, ee, "EcoreEvas");
315
316         /* reap our children */
317         rb_gc_start ();
318
319         ecore_evas_free (*ee);
320         *ee = NULL;
321
322         return Qnil;
323 }
324
325 void Init_EcoreEvas (void)
326 {
327         cEcoreEvas = rb_define_class_under (mEvas, "EcoreEvas", rb_cObject);
328
329         rb_define_private_method (rb_singleton_class (cEcoreEvas),
330                                   "new", NULL, 0);
331         rb_define_method (cEcoreEvas, "inspect", c_inspect, 0);
332         rb_define_method (cEcoreEvas, "delete", c_delete, 0);
333         rb_define_method (cEcoreEvas, "show", c_show, 0);
334         rb_define_method (cEcoreEvas, "hide", c_hide, 0);
335         rb_define_method (cEcoreEvas, "visible?", c_visible_get, 0);
336         rb_define_method (cEcoreEvas, "raise", c_raise, 0);
337         rb_define_method (cEcoreEvas, "lower", c_lower, 0);
338         rb_define_method (cEcoreEvas, "layer", c_layer_get, 0);
339         rb_define_method (cEcoreEvas, "layer=", c_layer_set, 1);
340         rb_define_method (cEcoreEvas, "evas", c_evas, 0);
341         rb_define_method (cEcoreEvas, "get_size_min", c_get_size_min, 0);
342         rb_define_method (cEcoreEvas, "set_size_min", c_set_size_min, 2);
343         rb_define_method (cEcoreEvas, "get_size_max", c_get_size_max, 0);
344         rb_define_method (cEcoreEvas, "set_size_max", c_set_size_max, 2);
345         rb_define_method (cEcoreEvas, "move", c_move, 2);
346         rb_define_method (cEcoreEvas, "resize", c_resize, 2);
347         rb_define_method (cEcoreEvas, "title", c_title_get, 0);
348         rb_define_method (cEcoreEvas, "title=", c_title_set, 1);
349         rb_define_method (cEcoreEvas, "borderless?", c_borderless_get, 0);
350         rb_define_method (cEcoreEvas, "borderless=", c_borderless_set, 1);
351         rb_define_method (cEcoreEvas, "shaped?", c_shaped_get, 0);
352         rb_define_method (cEcoreEvas, "shaped=", c_shaped_set, 1);
353         rb_define_method (cEcoreEvas, "sticky?", c_sticky_get, 0);
354         rb_define_method (cEcoreEvas, "sticky=", c_sticky_set, 1);
355         rb_define_method (cEcoreEvas, "rotation", c_rotation_get, 0);
356         rb_define_method (cEcoreEvas, "rotation=", c_rotation_set, 1);
357
358         evases = rb_hash_new ();
359         rb_global_variable (&evases);
360 }