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