Duck-typify message handling.
[ruby-edje.git] / src / rb_edje.c
1 /*
2  * $Id: rb_edje.c 330 2005-04-28 15:38:47Z 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 <evas/rb_evas.h>
25 #include <evas/rb_evas_object.h>
26
27 #define __RB_EDJE_C
28 #include "rb_edje.h"
29 #include "rb_edje_main.h"
30 #include "rb_part.h"
31
32 VALUE cEdje;
33 static VALUE cMsg, eEdjeError;
34
35 static void c_mark (RbEdje *e)
36 {
37         c_evas_object_mark (&e->real);
38
39         if (!NIL_P (e->parts))
40                 rb_gc_mark (e->parts);
41
42         if (!NIL_P (e->callbacks))
43                 rb_gc_mark (e->callbacks);
44
45         if (!NIL_P (e->on_text_changed_cb))
46                 rb_gc_mark (e->on_text_changed_cb);
47 }
48
49 static void c_free (RbEdje *e)
50 {
51         c_evas_object_free (&e->real, false);
52         free (e);
53
54         edje_shutdown ();
55 }
56
57 /*
58  * call-seq:
59  *  Edje::Edje.new(evas) => edje
60  *
61  * Creates an Edje::Edje object.
62  */
63 static VALUE c_new (VALUE klass, VALUE evas)
64 {
65         VALUE self, argv[1];
66         RbEdje *edje = NULL;
67
68         CHECK_CLASS (evas, cEvas);
69         GET_OBJ (evas, RbEvas, e);
70
71         edje_init ();
72
73         self = Data_Make_Struct (klass, RbEdje, c_mark, c_free, edje);
74
75         edje->real.real = edje_object_add (e->real);
76         edje->parts = Qnil;
77         edje->callbacks = Qnil;
78         edje->on_text_changed_cb = Qnil;
79
80         argv[0] = evas;
81         rb_obj_call_init (self, 1, argv);
82
83         return self;
84 }
85
86 /*
87  * call-seq:
88  *  edje.freeze => nil
89  *
90  * Freezes <i>edje</i>.
91  */
92 static VALUE c_freeze (VALUE self)
93 {
94         GET_OBJ (self, RbEdje, e);
95
96         edje_object_freeze (e->real.real);
97
98         return Qnil;
99 }
100
101 /*
102  * call-seq:
103  *  edje.thaw => nil
104  *
105  * Thaws <i>edje</i>.
106  */
107 static VALUE c_thaw (VALUE self)
108 {
109         GET_OBJ (self, RbEdje, e);
110
111         edje_object_thaw (e->real.real);
112
113         return Qnil;
114 }
115
116 /*
117  * call-seq:
118  *  edje.load(eet, group) => nil
119  *
120  * Loads <i>eet</i> into <i>edje</i>. <i>group</i> is the
121  * name of the group to be displayed.
122  */
123 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
124 {
125         GET_OBJ (self, RbEdje, e);
126
127         Check_Type (eet, T_STRING);
128         Check_Type (group, T_STRING);
129
130         if (!edje_object_file_set (e->real.real, StringValuePtr (eet),
131                                    StringValuePtr (group)))
132                 rb_raise (rb_eException, "Cannot load eet");
133
134         return Qnil;
135 }
136
137 /*
138  * call-seq:
139  *  edje.get_size_min => array
140  *
141  * Returns an array that contains the minimum size
142  * of <i>edje</i>.
143  */
144 static VALUE c_get_size_min (VALUE self)
145 {
146         int w = 0, h = 0;
147
148         GET_OBJ (self, RbEdje, e);
149
150         edje_object_size_min_get (e->real.real, &w, &h);
151
152         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
153 }
154
155 /*
156  * call-seq:
157  *  edje.get_size_max => array
158  *
159  * Returns an array that contains the maximum size
160  * of <i>edje</i>.
161  */
162 static VALUE c_get_size_max (VALUE self)
163 {
164         int w = 0, h = 0;
165
166         GET_OBJ (self, RbEdje, e);
167
168         edje_object_size_max_get (e->real.real, &w, &h);
169
170         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
171 }
172
173 /*
174  * call-seq:
175  *  edje.part_exists?(part) => true or false
176  *
177  * Returns true if <i>edje</i> has a part called <i>part</i>,
178  * else returns false.
179  */
180 static VALUE c_part_exists_get (VALUE self, VALUE name)
181 {
182         int r;
183
184         GET_OBJ (self, RbEdje, e);
185
186         Check_Type (name, T_STRING);
187
188         r = edje_object_part_exists (e->real.real, StringValuePtr (name));
189
190         return r ? Qtrue : Qfalse;
191 }
192
193 /*
194  * call-seq:
195  *  edje.part(part_name) => part
196  *
197  * Returns the <code>Edje::Part</code> object that corresponds to
198  * <i>part_name</i>. If there's no part with that name in <i>edje</i>,
199  * an exception is raised.
200  */
201 static VALUE c_part_get (VALUE self, VALUE name)
202 {
203         VALUE part;
204         const char *cname = StringValuePtr (name);
205
206         GET_OBJ (self, RbEdje, e);
207
208         if (!edje_object_part_exists (e->real.real, cname)) {
209                 rb_raise (rb_eException, "Unknown part name - %s", cname);
210                 return Qnil;
211         }
212
213         if (NIL_P (e->parts))
214                 e->parts = rb_hash_new ();
215
216         if (NIL_P (part = rb_hash_aref (e->parts, name))) {
217                 part = TO_PART (self, name);
218                 rb_hash_aset (e->parts, name, part);
219         }
220
221         return part;
222 }
223
224 static void on_text_changed (void *data, Evas_Object *eo,
225                              const char *part_name)
226 {
227         VALUE self = (VALUE) data, part, name;
228
229         GET_OBJ (self, RbEdje, e);
230
231         name = rb_str_new2 (part_name);
232
233         if (NIL_P (e->parts))
234                 e->parts = rb_hash_new ();
235
236         if (NIL_P (part = rb_hash_aref (e->parts, name))) {
237                 part = TO_PART (self, name);
238                 rb_hash_aset (e->parts, name, part);
239         }
240
241         rb_funcall (e->on_text_changed_cb,
242                     rb_intern ("call"), 1, part);
243 }
244
245 /*
246  * call-seq:
247  *  edje.on_text_changed { |part_obj| block }
248  *
249  * Registers a callback that will get called when the text
250  * of any part is changed in <i>edje</i>.
251  * The block is passed the <code>Edje::Part</code> object
252  * of which the text changed.
253  */
254 static VALUE c_on_text_changed (VALUE self)
255 {
256         GET_OBJ (self, RbEdje, e);
257
258         if (!rb_block_given_p ())
259                 return Qnil;
260
261         e->on_text_changed_cb = rb_block_proc ();
262
263         edje_object_text_change_cb_set (e->real.real, on_text_changed,
264                                         (void *) self);
265
266         return Qnil;
267 }
268
269 /*
270  * call-seq:
271  *  edje.emit_signal(signal, source) => nil
272  *
273  * Emits a signal to <i>edje</i>.
274  *
275  *  edje.emit_signal("signal_foo", "part_bar") #=> nil
276  */
277 static VALUE c_emit_signal (VALUE self, VALUE signal, VALUE source)
278 {
279         GET_OBJ (self, RbEdje, e);
280
281         Check_Type (signal, T_STRING);
282         Check_Type (source, T_STRING);
283
284         edje_object_signal_emit (e->real.real, StringValuePtr (signal),
285                                  StringValuePtr (source));
286
287         return Qnil;
288 }
289
290 static void on_signal (void *data, Evas_Object *o,
291                        const char *signal, const char *src)
292 {
293         rb_funcall ((VALUE) data, rb_intern ("call"), 2,
294                     rb_str_new2 (signal), rb_str_new2 (src));
295 }
296
297 /*
298  * call-seq:
299  *  edje.on_signal(signal [, source]) { |signal, source| block } => nil
300  *
301  * Registers a callback that will get called when <i>signal</i>
302  * is emitted by <i>source</i>.
303  * If source is nil, "*" will be used instead.
304  * The block is passed two strings, signal and source, which identify
305  * the emission.
306  */
307 static VALUE c_on_signal (int argc, VALUE *argv, VALUE self)
308 {
309         VALUE signal, src, cb;
310         char *ssrc = "*";
311
312         GET_OBJ (self, RbEdje, e);
313
314         rb_scan_args (argc, argv, "11", &signal, &src);
315
316         Check_Type (signal, T_STRING);
317
318         if (!NIL_P (src)) {
319                 Check_Type (src, T_STRING);
320                 ssrc = StringValuePtr (src);
321         }
322
323         if (!rb_block_given_p ())
324                 return Qnil;
325
326         cb = rb_block_proc ();
327
328         if (NIL_P (e->callbacks))
329                 e->callbacks = rb_ary_new ();
330
331         rb_ary_push (e->callbacks, cb);
332
333         edje_object_signal_callback_add (e->real.real,
334                                          StringValuePtr (signal),
335                                          ssrc, on_signal, (void *) cb);
336
337         return Qnil;
338 }
339
340 /*
341  * call-seq:
342  *  edje.play? => true or false
343  *
344  * Returns true if <i>edje</i> is in play mode, else returns false.
345  */
346 static VALUE c_play_get (VALUE self)
347 {
348         GET_OBJ (self, RbEdje, e);
349
350         return edje_object_play_get (e->real.real) ? Qtrue : Qfalse;
351 }
352
353 /*
354  * call-seq:
355  *  edje.play(true or false)
356  *
357  * Sets <i>edje</i> to play resp. pause mode.
358  */
359 static VALUE c_play_set (VALUE self, VALUE val)
360 {
361         GET_OBJ (self, RbEdje, e);
362
363         CHECK_BOOL(val);
364
365         edje_object_play_set (e->real.real, val == Qtrue);
366
367         return Qnil;
368 }
369
370 /*
371  * call-seq:
372  *  edje.animation? => true or false
373  *
374  * Returns the animation state of <i>edje</i>.
375  */
376 static VALUE c_animation_get (VALUE self)
377 {
378         GET_OBJ (self, RbEdje, e);
379
380         return edje_object_animation_get (e->real.real) ? Qtrue : Qfalse;
381 }
382
383 /*
384  * call-seq:
385  *  edje.animation(true or false)
386  *
387  * Sets the animation state of <i>edje</i>.
388  */
389 static VALUE c_animation_set (VALUE self, VALUE val)
390 {
391         GET_OBJ (self, RbEdje, e);
392
393         CHECK_BOOL(val);
394
395         edje_object_animation_set (e->real.real, val == Qtrue);
396
397         return Qnil;
398 }
399
400 static VALUE c_data_get (VALUE self, VALUE key)
401 {
402         const char *s;
403
404         GET_OBJ (self, RbEdje, e);
405
406         Check_Type (key, T_STRING);
407
408         s = edje_object_data_get (e->real.real, StringValuePtr (key));
409
410         return s ? rb_str_new2 (s) : Qnil;
411 }
412
413 static VALUE c_send_message (VALUE self, VALUE msg)
414 {
415         Edje_Message_String s;
416         Edje_Message_Int i;
417         Edje_Message_Float f;
418         Edje_Message_Type type;
419         void *data;
420         VALUE v, tmp;
421
422         GET_OBJ (self, RbEdje, e);
423
424         CHECK_CLASS (msg, cMsg);
425
426         v = rb_iv_get (msg, "@value");
427
428         if (NIL_P (v)) {
429                 rb_raise (eEdjeError, "value must not be nil");
430                 return Qnil;
431         }
432
433         if (!NIL_P (tmp = rb_check_string_type (v))) {
434                 type = EDJE_MESSAGE_STRING;
435                 s.str = StringValuePtr (tmp);
436                 data = &s;
437         } else if (rb_obj_is_kind_of (v, rb_cFixnum)) {
438                 type = EDJE_MESSAGE_INT;
439                 i.val = FIX2INT (v);
440                 data = &i;
441         } else if (rb_obj_is_kind_of (v, rb_cFloat)) {
442                 type = EDJE_MESSAGE_FLOAT;
443                 f.val = NUM2DBL (v);
444                 data = &v;
445         /* } else if (!NIL_P (tmp = rb_check_array_type (v))) { */
446         } else {
447                 rb_raise (eEdjeError, "unsupported value");
448         }
449
450         edje_object_message_send (e->real.real, type,
451                                   NUM2INT (rb_iv_get (msg, "@id")), data);
452
453         return Qnil;
454 }
455
456 static VALUE c_msg_init (int argc, VALUE *argv, VALUE self)
457 {
458         VALUE val;
459
460         rb_scan_args (argc, argv, "01", &val);
461
462         rb_iv_set (self, "@id", UINT2NUM (0));
463         rb_iv_set (self, "@value", val);
464
465         return self;
466 }
467
468 void Init_Edje (void)
469 {
470         cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
471
472         rb_define_singleton_method (cEdje, "new", c_new, 1);
473         rb_define_method (cEdje, "freeze", c_freeze, 0);
474         rb_define_method (cEdje, "thaw", c_thaw, 0);
475         rb_define_method (cEdje, "load", c_load, 2);
476         rb_define_method (cEdje, "get_size_min", c_get_size_min, 0);
477         rb_define_method (cEdje, "get_size_max", c_get_size_max, 0);
478         rb_define_method (cEdje, "part_exists?", c_part_exists_get, 1);
479         rb_define_method (cEdje, "part", c_part_get, 1);
480         rb_define_method (cEdje, "on_text_changed", c_on_text_changed, 0);
481         rb_define_method (cEdje, "emit_signal", c_emit_signal, 2);
482         rb_define_method (cEdje, "on_signal", c_on_signal, -1);
483         rb_define_method (cEdje, "play?", c_play_get, 0);
484         rb_define_method (cEdje, "play=", c_play_set, 1);
485         rb_define_method (cEdje, "animation?", c_animation_get, 0);
486         rb_define_method (cEdje, "animation=", c_animation_set, 1);
487         rb_define_method (cEdje, "data", c_data_get, 1);
488         rb_define_method (cEdje, "send_message", c_send_message, 1);
489
490         cMsg = rb_define_class_under (mEdje, "Message", rb_cObject);
491
492         rb_define_method (cMsg, "initialize", c_msg_init, -1);
493
494         rb_define_attr (cMsg, "id", 1, 1);
495         rb_define_attr (cMsg, "value", 1, 1);
496
497         eEdjeError = rb_define_class_under (mEdje, "EdjeError",
498                                            rb_eStandardError);
499 }