edje_init() and edje_shutdown() aren't exported anymore.
[ruby-edje.git] / src / rb_edje.c
1 /*
2  * $Id: rb_edje.c 28 2004-07-08 18:35:51Z 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 <Edje.h>
24 #include <rb_evas.h>
25 #include <rb_evas_object.h>
26
27 #include "rb_edje_main.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 void c_free (Evas_Object **e)
48 {
49         c_evas_object_free (e);
50
51         edje_shutdown ();
52 }
53
54 static VALUE c_new (VALUE klass, VALUE evas)
55 {
56         VALUE self, argv[1];
57         Evas_Object **edje;
58
59         if (!rb_obj_is_kind_of (evas, cEvas)) {
60                 rb_raise (rb_eTypeError,
61                           "wrong argument type %s (expected Evas)",
62                           rb_obj_classname (evas));
63                 return Qnil;
64         }
65
66         GET_OBJ (evas, Evas, e, "Evas");
67
68         edje_init ();
69
70         self = Data_Make_Struct (klass, Evas_Object *, c_evas_object_mark,
71                                  c_free, edje);
72         *edje = edje_object_add (*e);
73
74         argv[0] = evas;
75         rb_obj_call_init (self, 1, argv);
76
77         return self;
78 }
79
80 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
81 {
82         GET_OBJ (self, Evas_Object, e, "Edje");
83
84         Check_Type (eet, T_STRING);
85         Check_Type (group, T_STRING);
86
87         if (!edje_object_file_set (*e, StringValuePtr (eet),
88                                    StringValuePtr (group)))
89                 rb_raise (rb_eException, "Cannot load eet");
90
91         return Qnil;
92 }
93
94 static VALUE c_get_size_min (VALUE self)
95 {
96         int w = 0, h = 0;
97
98         GET_OBJ (self, Evas_Object, e, "Edje");
99
100         edje_object_size_min_get (*e, &w, &h);
101
102         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
103 }
104
105 static VALUE c_get_size_max (VALUE self)
106 {
107         int w = 0, h = 0;
108
109         GET_OBJ (self, Evas_Object, e, "Edje");
110
111         edje_object_size_max_get (*e, &w, &h);
112
113         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
114 }
115
116 static VALUE c_part_exists_get (VALUE self, VALUE part)
117 {
118         int r;
119
120         GET_OBJ (self, Evas_Object, e, "Edje");
121
122         Check_Type (part, T_STRING);
123
124         r = edje_object_part_exists (*e, StringValuePtr (part));
125
126         return r ? Qtrue : Qfalse;
127 }
128
129 static VALUE c_part_swallow (VALUE self, VALUE part, VALUE target)
130 {
131         GET_OBJ (self, Evas_Object, e, "Edje");
132
133         Check_Type (part, T_STRING);
134
135         if (!rb_obj_is_kind_of (target, cEvasObject)) {
136                 rb_raise (rb_eTypeError,
137                           "wrong argument type %s (expected EvasObject)",
138                           rb_obj_classname (target));
139                 return Qnil;
140         }
141
142         GET_OBJ (target, Evas_Object, target2, "EvasObject");
143
144         edje_object_part_swallow (*e, StringValuePtr (part), *target2);
145
146         return Qnil;
147 }
148
149 static VALUE c_part_unswallow (VALUE self, VALUE target)
150 {
151         GET_OBJ (self, Evas_Object, e, "Edje");
152
153         if (!rb_obj_is_kind_of (target, cEvasObject)) {
154                 rb_raise (rb_eTypeError,
155                           "wrong argument type %s (expected EvasObject)",
156                           rb_obj_classname (target));
157                 return Qnil;
158         }
159
160         GET_OBJ (target, Evas_Object, target2, "EvasObject");
161
162         edje_object_part_unswallow (*e, *target2);
163
164         return Qnil;
165 }
166
167 static VALUE c_get_part_swallow (VALUE self, VALUE part)
168 {
169         Evas_Object *o;
170         void *obj;
171
172         GET_OBJ (self, Evas_Object, e, "Edje");
173
174         Check_Type (part, T_STRING);
175
176         if (!(o = edje_object_part_swallow_get (*e, StringValuePtr (part))))
177                 return Qnil;
178
179         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
180                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
181                 return Qnil;
182         }
183
184         return (VALUE) obj;
185 }
186
187 static VALUE c_get_part_text (VALUE self, VALUE part)
188 {
189         const char *s;
190
191         GET_OBJ (self, Evas_Object, e, "Edje");
192
193         Check_Type (part, T_STRING);
194
195         if (!(s = edje_object_part_text_get (*e, StringValuePtr (part))))
196                 return Qnil;
197         else
198                 return rb_str_new2 (s);
199 }
200
201 static VALUE c_set_part_text (VALUE self, VALUE part, VALUE text)
202 {
203         GET_OBJ (self, Evas_Object, e, "Edje");
204
205         Check_Type (part, T_STRING);
206         Check_Type (text, T_STRING);
207
208         edje_object_part_text_set (*e, StringValuePtr (part),
209                                    StringValuePtr (text));
210
211         return Qnil;
212 }
213
214 static void on_text_changed (void *data, Evas_Object *e,
215                              const char *part)
216 {
217         rb_funcall ((VALUE) data, rb_intern ("call"), 1,
218                     rb_str_new2 (part));
219 }
220
221 static VALUE c_on_text_changed (VALUE self)
222 {
223         GET_OBJ (self, Evas_Object, e, "Edje");
224
225         if (!rb_block_given_p ())
226                 return Qnil;
227
228         edje_object_text_change_cb_set (*e, on_text_changed,
229                                         (void *) rb_block_proc ());
230
231         return Qnil;
232 }
233
234 static VALUE c_signal_emit (VALUE self, VALUE emission, VALUE source)
235 {
236         GET_OBJ (self, Evas_Object, e, "Edje");
237
238         Check_Type (emission, T_STRING);
239         Check_Type (source, T_STRING);
240
241         edje_object_signal_emit (*e, StringValuePtr (emission),
242                                  StringValuePtr (source));
243
244         return Qnil;
245 }
246
247 static void on_signal (void *data, Evas_Object *o,
248                        const char *emission, const char *src) {
249         rb_funcall ((VALUE) data, rb_intern ("call"), 1,
250                     rb_str_new2 (emission), rb_str_new2 (src));
251 }
252
253 static VALUE c_on_signal (VALUE self, VALUE signal, VALUE src)
254 {
255         GET_OBJ (self, Evas_Object, e, "Edje");
256
257         Check_Type (signal, T_STRING);
258         Check_Type (src, T_STRING);
259
260         if (!rb_block_given_p ())
261                 return Qnil;
262
263         edje_object_signal_callback_add (*e, StringValuePtr (signal),
264                                          StringValuePtr (src), on_signal,
265                                          (void *) rb_block_proc ());
266
267         return Qnil;
268 }
269
270 static VALUE c_play_get (VALUE self)
271 {
272         GET_OBJ (self, Evas_Object, e, "Edje");
273
274         return edje_object_play_get (*e) ? Qtrue : Qfalse;
275 }
276
277 static VALUE c_play_set (VALUE self, VALUE val)
278 {
279         GET_OBJ (self, Evas_Object, e, "Edje");
280
281         CHECK_BOOL(val);
282
283         edje_object_play_set (*e, val == Qtrue ? 1 : 0);
284
285         return Qnil;
286 }
287
288 static VALUE c_animation_get (VALUE self)
289 {
290         GET_OBJ (self, Evas_Object, e, "Edje");
291
292         return edje_object_animation_get (*e) ? Qtrue : Qfalse;
293 }
294
295 static VALUE c_animation_set (VALUE self, VALUE val)
296 {
297         GET_OBJ (self, Evas_Object, e, "Edje");
298
299         CHECK_BOOL(val);
300
301         edje_object_animation_set (*e, val == Qtrue ? 1 : 0);
302
303         return Qnil;
304 }
305
306 void Init_Edje (void)
307 {
308         VALUE cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
309
310         rb_define_singleton_method (cEdje, "new", c_new, 1);
311         rb_define_method (cEdje, "load", c_load, 2);
312         rb_define_method (cEdje, "get_size_min", c_get_size_min, 0);
313         rb_define_method (cEdje, "get_size_max", c_get_size_max, 0);
314         rb_define_method (cEdje, "part_exists?", c_part_exists_get, 1);
315         rb_define_method (cEdje, "part_swallow", c_part_swallow, 2);
316         rb_define_method (cEdje, "part_unswallow", c_part_unswallow, 1);
317         rb_define_method (cEdje, "get_part_swallow", c_get_part_swallow, 1);
318         rb_define_method (cEdje, "get_part_text", c_get_part_text, 1);
319         rb_define_method (cEdje, "set_part_text", c_set_part_text, 2);
320         rb_define_method (cEdje, "on_text_changed", c_on_text_changed, 0);
321         rb_define_method (cEdje, "signal_emit", c_signal_emit, 1);
322         rb_define_method (cEdje, "on_signal", c_on_signal, 2);
323         rb_define_method (cEdje, "play?", c_play_get, 0);
324         rb_define_method (cEdje, "play=", c_play_set, 1);
325         rb_define_method (cEdje, "animation?", c_animation_get, 0);
326         rb_define_method (cEdje, "animation=", c_animation_set, 1);
327 }