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