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