0e32b83c18c59bd5955eaca4fd58578781018efc
[ruby-edje.git] / src / rb_edje.c
1 /*
2  * $Id: rb_edje.c 355 2006-02-10 18:15:13Z 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 #include <stdbool.h>
23
24 #include <Edje.h>
25 #include <evas/rb_evas.h>
26 #include <evas/rb_evas_object.h>
27
28 #define __RB_EDJE_C
29 #include "rb_edje.h"
30 #include "rb_edje_main.h"
31 #include "rb_part.h"
32
33 VALUE cEdje;
34 static VALUE cMsg, eEdjeError;
35
36 static void c_mark (RbEdje *e)
37 {
38         c_evas_object_mark (&e->real);
39
40         if (!NIL_P (e->parts))
41                 rb_gc_mark (e->parts);
42
43         if (!NIL_P (e->callbacks))
44                 rb_gc_mark (e->callbacks);
45
46         if (!NIL_P (e->on_text_changed_cb))
47                 rb_gc_mark (e->on_text_changed_cb);
48 }
49
50 static void c_free (RbEdje *e)
51 {
52         c_evas_object_free (&e->real, false);
53         free (e);
54
55         edje_shutdown ();
56 }
57
58 static VALUE c_alloc (VALUE klass)
59 {
60         RbEdje *e = NULL;
61
62         edje_init ();
63
64         return Data_Make_Struct (klass, RbEdje, c_mark, c_free, e);
65 }
66
67 /*
68  * call-seq:
69  *  Edje::Edje.new(evas) => edje
70  *
71  * Creates an Edje::Edje object.
72  */
73 static VALUE c_init (VALUE self, VALUE evas)
74 {
75         CHECK_CLASS (evas, cEvas);
76         GET_OBJ (evas, RbEvas, e);
77         GET_OBJ (self, RbEdje, edje);
78
79         /* c_new_from_pointer() might already have initialized the pointer */
80         if (!edje->real.real)
81                 edje->real.real = edje_object_add (e->real);
82
83         edje->parts = Qnil;
84         edje->callbacks = Qnil;
85         edje->on_text_changed_cb = Qnil;
86
87         rb_call_super (1, &evas);
88
89         return self;
90 }
91
92 static VALUE c_new_from_pointer (VALUE klass, VALUE evas, VALUE ptr)
93 {
94         VALUE self = rb_obj_alloc (klass);
95
96         GET_OBJ (self, RbEdje, edje);
97
98         edje->real.real = (Evas_Object *) ptr;
99
100         rb_obj_call_init (self, 1, &evas);
101
102         return self;
103 }
104
105 /*
106  * call-seq:
107  *  edje.freeze => nil
108  *
109  * Freezes <i>edje</i>.
110  */
111 static VALUE c_freeze (VALUE self)
112 {
113         GET_OBJ (self, RbEdje, e);
114
115         edje_object_freeze (e->real.real);
116
117         return Qnil;
118 }
119
120 /*
121  * call-seq:
122  *  edje.thaw => nil
123  *
124  * Thaws <i>edje</i>.
125  */
126 static VALUE c_thaw (VALUE self)
127 {
128         GET_OBJ (self, RbEdje, e);
129
130         edje_object_thaw (e->real.real);
131
132         return Qnil;
133 }
134
135 /*
136  * call-seq:
137  *  edje.load(eet, group) => nil
138  *
139  * Loads <i>eet</i> into <i>edje</i>. <i>group</i> is the
140  * name of the group to be displayed.
141  */
142 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
143 {
144         GET_OBJ (self, RbEdje, e);
145
146         Check_Type (eet, T_STRING);
147         Check_Type (group, T_STRING);
148
149         if (!edje_object_file_set (e->real.real, StringValuePtr (eet),
150                                    StringValuePtr (group)))
151                 rb_raise (rb_eException, "Cannot load eet");
152
153         return Qnil;
154 }
155
156 /*
157  * call-seq:
158  *  edje.get_size_min => array
159  *
160  * Returns an array that contains the minimum size
161  * of <i>edje</i>.
162  */
163 static VALUE c_get_size_min (VALUE self)
164 {
165         int w = 0, h = 0;
166
167         GET_OBJ (self, RbEdje, e);
168
169         edje_object_size_min_get (e->real.real, &w, &h);
170
171         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
172 }
173
174 /*
175  * call-seq:
176  *  edje.get_size_max => array
177  *
178  * Returns an array that contains the maximum size
179  * of <i>edje</i>.
180  */
181 static VALUE c_get_size_max (VALUE self)
182 {
183         int w = 0, h = 0;
184
185         GET_OBJ (self, RbEdje, e);
186
187         edje_object_size_max_get (e->real.real, &w, &h);
188
189         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
190 }
191
192 /*
193  * call-seq:
194  *  edje.part_exists?(part) => true or false
195  *
196  * Returns true if <i>edje</i> has a part called <i>part</i>,
197  * else returns false.
198  */
199 static VALUE c_part_exists_get (VALUE self, VALUE name)
200 {
201         int r;
202
203         GET_OBJ (self, RbEdje, e);
204
205         Check_Type (name, T_STRING);
206
207         r = edje_object_part_exists (e->real.real, StringValuePtr (name));
208
209         return r ? Qtrue : Qfalse;
210 }
211
212 /*
213  * call-seq:
214  *  edje.part(part_name) => part
215  *
216  * Returns the <code>Edje::Part</code> object that corresponds to
217  * <i>part_name</i>. If there's no part with that name in <i>edje</i>,
218  * an exception is raised.
219  */
220 static VALUE c_part_get (VALUE self, VALUE name)
221 {
222         VALUE part;
223         const char *cname = StringValuePtr (name);
224
225         GET_OBJ (self, RbEdje, e);
226
227         if (!edje_object_part_exists (e->real.real, cname)) {
228                 rb_raise (rb_eException, "Unknown part name - %s", cname);
229                 return Qnil;
230         }
231
232         if (NIL_P (e->parts))
233                 e->parts = rb_hash_new ();
234
235         if (NIL_P (part = rb_hash_aref (e->parts, name))) {
236                 part = TO_PART (self, name);
237                 rb_hash_aset (e->parts, name, part);
238         }
239
240         return part;
241 }
242
243 static void on_text_changed (void *data, Evas_Object *eo,
244                              const char *part_name)
245 {
246         VALUE self = (VALUE) data, part, name;
247
248         GET_OBJ (self, RbEdje, e);
249
250         name = rb_str_new2 (part_name);
251
252         if (NIL_P (e->parts))
253                 e->parts = rb_hash_new ();
254
255         if (NIL_P (part = rb_hash_aref (e->parts, name))) {
256                 part = TO_PART (self, name);
257                 rb_hash_aset (e->parts, name, part);
258         }
259
260         rb_funcall (e->on_text_changed_cb,
261                     rb_intern ("call"), 1, part);
262 }
263
264 /*
265  * call-seq:
266  *  edje.on_text_changed { |part_obj| block }
267  *
268  * Registers a callback that will get called when the text
269  * of any part is changed in <i>edje</i>.
270  * The block is passed the <code>Edje::Part</code> object
271  * of which the text changed.
272  */
273 static VALUE c_on_text_changed (VALUE self)
274 {
275         GET_OBJ (self, RbEdje, e);
276
277         if (!rb_block_given_p ())
278                 return Qnil;
279
280         e->on_text_changed_cb = rb_block_proc ();
281
282         edje_object_text_change_cb_set (e->real.real, on_text_changed,
283                                         (void *) self);
284
285         return Qnil;
286 }
287
288 /*
289  * call-seq:
290  *  edje.emit_signal(signal, source) => nil
291  *
292  * Emits a signal to <i>edje</i>.
293  *
294  *  edje.emit_signal("signal_foo", "part_bar") #=> nil
295  */
296 static VALUE c_emit_signal (VALUE self, VALUE signal, VALUE source)
297 {
298         GET_OBJ (self, RbEdje, e);
299
300         Check_Type (signal, T_STRING);
301         Check_Type (source, T_STRING);
302
303         edje_object_signal_emit (e->real.real, StringValuePtr (signal),
304                                  StringValuePtr (source));
305
306         return Qnil;
307 }
308
309 static void on_signal (void *data, Evas_Object *o,
310                        const char *signal, const char *src)
311 {
312         rb_funcall ((VALUE) data, rb_intern ("call"), 2,
313                     rb_str_new2 (signal), rb_str_new2 (src));
314 }
315
316 /*
317  * call-seq:
318  *  edje.on_signal(signal [, source]) { |signal, source| block } => nil
319  *
320  * Registers a callback that will get called when <i>signal</i>
321  * is emitted by <i>source</i>.
322  * If source is nil, "*" will be used instead.
323  * The block is passed two strings, signal and source, which identify
324  * the emission.
325  */
326 static VALUE c_on_signal (int argc, VALUE *argv, VALUE self)
327 {
328         VALUE signal, src, cb;
329         char *ssrc = "*";
330
331         GET_OBJ (self, RbEdje, e);
332
333         rb_scan_args (argc, argv, "11", &signal, &src);
334
335         Check_Type (signal, T_STRING);
336
337         if (!NIL_P (src)) {
338                 Check_Type (src, T_STRING);
339                 ssrc = StringValuePtr (src);
340         }
341
342         if (!rb_block_given_p ())
343                 return Qnil;
344
345         cb = rb_block_proc ();
346
347         if (NIL_P (e->callbacks))
348                 e->callbacks = rb_ary_new ();
349
350         rb_ary_push (e->callbacks, cb);
351
352         edje_object_signal_callback_add (e->real.real,
353                                          StringValuePtr (signal),
354                                          ssrc, on_signal, (void *) cb);
355
356         return Qnil;
357 }
358
359 /*
360  * call-seq:
361  *  edje.play? => true or false
362  *
363  * Returns true if <i>edje</i> is in play mode, else returns false.
364  */
365 static VALUE c_play_get (VALUE self)
366 {
367         GET_OBJ (self, RbEdje, e);
368
369         return edje_object_play_get (e->real.real) ? Qtrue : Qfalse;
370 }
371
372 /*
373  * call-seq:
374  *  edje.play(true or false)
375  *
376  * Sets <i>edje</i> to play resp. pause mode.
377  */
378 static VALUE c_play_set (VALUE self, VALUE val)
379 {
380         GET_OBJ (self, RbEdje, e);
381
382         CHECK_BOOL(val);
383
384         edje_object_play_set (e->real.real, val == Qtrue);
385
386         return Qnil;
387 }
388
389 /*
390  * call-seq:
391  *  edje.animation? => true or false
392  *
393  * Returns the animation state of <i>edje</i>.
394  */
395 static VALUE c_animation_get (VALUE self)
396 {
397         GET_OBJ (self, RbEdje, e);
398
399         return edje_object_animation_get (e->real.real) ? Qtrue : Qfalse;
400 }
401
402 /*
403  * call-seq:
404  *  edje.animation(true or false)
405  *
406  * Sets the animation state of <i>edje</i>.
407  */
408 static VALUE c_animation_set (VALUE self, VALUE val)
409 {
410         GET_OBJ (self, RbEdje, e);
411
412         CHECK_BOOL(val);
413
414         edje_object_animation_set (e->real.real, val == Qtrue);
415
416         return Qnil;
417 }
418
419 static VALUE c_data_get (VALUE self, VALUE key)
420 {
421         const char *s;
422
423         GET_OBJ (self, RbEdje, e);
424
425         Check_Type (key, T_STRING);
426
427         s = edje_object_data_get (e->real.real, StringValuePtr (key));
428
429         return s ? rb_str_new2 (s) : Qnil;
430 }
431
432 static Edje_Message_Type get_msg_type (VALUE val)
433 {
434         VALUE ary, entry;
435         Edje_Message_Type type;
436         int i, len;
437
438         if (NIL_P (val))
439                 return EDJE_MESSAGE_NONE;
440
441         if (!NIL_P (rb_check_string_type (val))) {
442                 return EDJE_MESSAGE_STRING;
443         } else if (rb_obj_is_kind_of (val, rb_cFixnum)) {
444                 return EDJE_MESSAGE_INT;
445         } else if (rb_obj_is_kind_of (val, rb_cFloat)) {
446                 return EDJE_MESSAGE_FLOAT;
447         } else if (NIL_P (ary = rb_check_array_type (val)))
448                 return EDJE_MESSAGE_NONE;
449
450         len = RARRAY (ary)->len;
451         if (len <= 0)
452                 return EDJE_MESSAGE_NONE;
453
454         entry = rb_ary_entry (ary, 0);
455
456         if (rb_obj_is_kind_of (entry, rb_cFixnum))
457                 return EDJE_MESSAGE_INT_SET;
458         else if (rb_obj_is_kind_of (entry, rb_cFloat))
459                 return EDJE_MESSAGE_FLOAT_SET;
460         else if (NIL_P (rb_check_string_type (entry)))
461                 return EDJE_MESSAGE_NONE;
462
463         /* first entry is a string.
464          * so if we only have one entry, it's a string set
465          */
466         if (len == 1)
467                 return EDJE_MESSAGE_STRING_SET;
468
469         entry = rb_ary_entry (ary, 1);
470
471         if (!NIL_P (rb_check_string_type (entry)))
472                 type = EDJE_MESSAGE_STRING_SET;
473         else if (rb_obj_is_kind_of (entry, rb_cFixnum))
474                 type = len == 2 ? EDJE_MESSAGE_STRING_INT : EDJE_MESSAGE_STRING_INT_SET;
475         else if (rb_obj_is_kind_of (entry, rb_cFloat))
476                 type = len == 2 ? EDJE_MESSAGE_STRING_FLOAT :EDJE_MESSAGE_STRING_FLOAT_SET;
477         else
478                 return EDJE_MESSAGE_NONE;
479
480         switch (type) {
481                 case EDJE_MESSAGE_STRING_SET:
482                         for (i = 2; i < len; i++) {
483                                 entry = rb_ary_entry (ary, i);
484                                 if (NIL_P(rb_check_string_type (entry)))
485                                         return EDJE_MESSAGE_NONE;
486                         }
487
488                         break;
489                 case EDJE_MESSAGE_INT_SET:
490                 case EDJE_MESSAGE_STRING_INT_SET:
491                         for (i = 2; i < len; i++) {
492                                 entry = rb_ary_entry (ary, i);
493                                 if (!rb_obj_is_kind_of (entry, rb_cFixnum))
494                                         return EDJE_MESSAGE_NONE;
495                         }
496
497                         break;
498                 case EDJE_MESSAGE_FLOAT_SET:
499                 case EDJE_MESSAGE_STRING_FLOAT_SET:
500                         for (i = 2; i < len; i++) {
501                                 entry = rb_ary_entry (ary, 2);
502                                 if (!rb_obj_is_kind_of (entry, rb_cFloat))
503                                         return EDJE_MESSAGE_NONE;
504                         }
505
506                         break;
507                 default:
508                         break;
509         }
510
511         return type;
512 }
513
514 static VALUE c_send_message (VALUE self, VALUE msg)
515 {
516         Edje_Message_String msg_s;
517         Edje_Message_Int msg_i;
518         Edje_Message_Float msg_f;
519         Edje_Message_String_Set *s_set = NULL;
520         Edje_Message_Int_Set *i_set = NULL;
521         Edje_Message_Float_Set *f_set = NULL;
522         Edje_Message_String_Int si;
523         Edje_Message_String_Float sf;
524         Edje_Message_String_Int_Set *si_set = NULL;
525         Edje_Message_String_Float_Set *sf_set = NULL;
526         Edje_Message_Type type;
527         void *data = NULL;
528         int id, i, len = 0;
529         bool free_data = false;
530         VALUE v, ary, entry;
531
532         GET_OBJ (self, RbEdje, e);
533
534         CHECK_CLASS (msg, cMsg);
535
536         id = FIX2INT (rb_iv_get (msg, "@id"));
537         v = rb_iv_get (msg, "@value");
538
539         type = get_msg_type (v);
540         if (!NIL_P (ary = rb_check_array_type (v)))
541                 len = RARRAY (ary)->len;
542
543         switch (type) {
544                 case EDJE_MESSAGE_NONE:
545                         rb_raise (eEdjeError, "unsupported value");
546                         return Qnil;
547                 case EDJE_MESSAGE_SIGNAL:
548                         return Qnil; /* cannot happen */
549                 case EDJE_MESSAGE_STRING:
550                         msg_s.str = StringValuePtr (v);
551                         data = &msg_s;
552                         break;
553                 case EDJE_MESSAGE_INT:
554                         msg_i.val = FIX2INT (v);
555                         data = &msg_i;
556                         break;
557                 case EDJE_MESSAGE_FLOAT:
558                         msg_f.val = NUM2DBL (v);
559                         data = &msg_f;
560                         break;
561                 case EDJE_MESSAGE_STRING_SET:
562                         s_set = malloc (sizeof (Edje_Message_String_Set) + ((len - 1) * sizeof (char *)));
563                         s_set->count = len;
564                         free_data = true;
565
566                         for (i = 0; i < len; i++) {
567                                 entry = rb_ary_entry (ary, i);
568                                 s_set->str[i] = StringValuePtr (entry);
569                         }
570
571                         data = s_set;
572
573                         break;
574                 case EDJE_MESSAGE_INT_SET:
575                         i_set = malloc (sizeof (Edje_Message_Int_Set) + ((len - 1) * sizeof (int)));
576                         i_set->count = len;
577                         free_data = true;
578
579                         for (i = 0; i < len; i++) {
580                                 entry = rb_ary_entry (ary, i);
581                                 i_set->val[i] = FIX2INT (entry);
582                         }
583
584                         data = i_set;
585
586                         break;
587                 case EDJE_MESSAGE_FLOAT_SET:
588                         f_set = malloc (sizeof (Edje_Message_Float_Set) + ((len - 1) * sizeof (double)));
589                         f_set->count = len;
590                         free_data = true;
591
592                         for (i = 0; i < len; i++) {
593                                 entry = rb_ary_entry (ary, i);
594                                 f_set->val[i] = NUM2DBL (entry);
595                         }
596
597                         data = f_set;
598
599                         break;
600                 case EDJE_MESSAGE_STRING_INT:
601                         entry = rb_ary_entry (ary, 0);
602                         si.str = StringValuePtr (entry);
603                         entry = rb_ary_entry (ary, 1);
604                         si.val = FIX2INT (entry);
605
606                         data = &si;
607
608                         break;
609                 case EDJE_MESSAGE_STRING_FLOAT:
610                         entry = rb_ary_entry (ary, 0);
611                         sf.str = StringValuePtr (entry);
612                         entry = rb_ary_entry (ary, 1);
613                         sf.val = NUM2DBL (entry);
614
615                         data = &sf;
616
617                         break;
618                 case EDJE_MESSAGE_STRING_INT_SET:
619                         si_set = malloc (sizeof (Edje_Message_String_Int_Set) + ((len - 1) * sizeof (int)));
620                         si_set->count = len - 1;
621                         free_data = true;
622
623                         entry = rb_ary_entry (ary, 0);
624                         si_set->str = StringValuePtr (entry);
625
626                         for (i = 1; i < len; i++) {
627                                 entry = rb_ary_entry (ary, i);
628                                 si_set->val[i - 1] = FIX2INT (entry);
629                         }
630
631                         data = &si_set;
632
633                         break;
634                 case EDJE_MESSAGE_STRING_FLOAT_SET:
635                         sf_set = malloc (sizeof (Edje_Message_String_Float_Set) + ((len - 1) * sizeof (double)));
636                         sf_set->count = len - 1;
637                         free_data = true;
638
639                         entry = rb_ary_entry (ary, 0);
640                         sf_set->str = StringValuePtr (entry);
641
642                         for (i = 1; i < len; i++) {
643                                 entry = rb_ary_entry (ary, i);
644                                 sf_set->val[i - 1] = NUM2DBL (entry);
645                         }
646
647                         data = &sf_set;
648
649                         break;
650         }
651
652         edje_object_message_send (e->real.real, type, id, data);
653
654         if (free_data)
655                 free (data);
656
657         return Qnil;
658 }
659
660 static VALUE c_msg_init (int argc, VALUE *argv, VALUE self)
661 {
662         VALUE id, val;
663
664         if (argc == 2)
665                 rb_scan_args (argc, argv, "11", &id, &val);
666         else
667                 rb_scan_args (argc, argv, "1*", &id, &val);
668
669         Check_Type (id, T_FIXNUM);
670
671         rb_iv_set (self, "@id", id);
672         rb_iv_set (self, "@value", val);
673
674         return self;
675 }
676
677 void Init_Edje (void)
678 {
679         cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
680
681         rb_define_alloc_func (cEdje, c_alloc);
682         rb_define_singleton_method (cEdje, "new_from_pointer",
683                                     c_new_from_pointer, 2);
684         rb_define_method (cEdje, "initialize", c_init, 1);
685         rb_define_method (cEdje, "freeze", c_freeze, 0);
686         rb_define_method (cEdje, "thaw", c_thaw, 0);
687         rb_define_method (cEdje, "load", c_load, 2);
688         rb_define_method (cEdje, "get_size_min", c_get_size_min, 0);
689         rb_define_method (cEdje, "get_size_max", c_get_size_max, 0);
690         rb_define_method (cEdje, "part_exists?", c_part_exists_get, 1);
691         rb_define_method (cEdje, "part", c_part_get, 1);
692         rb_define_method (cEdje, "on_text_changed", c_on_text_changed, 0);
693         rb_define_method (cEdje, "emit_signal", c_emit_signal, 2);
694         rb_define_method (cEdje, "on_signal", c_on_signal, -1);
695         rb_define_method (cEdje, "play?", c_play_get, 0);
696         rb_define_method (cEdje, "play=", c_play_set, 1);
697         rb_define_method (cEdje, "animation?", c_animation_get, 0);
698         rb_define_method (cEdje, "animation=", c_animation_set, 1);
699         rb_define_method (cEdje, "data", c_data_get, 1);
700         rb_define_method (cEdje, "send_message", c_send_message, 1);
701
702         cMsg = rb_define_class_under (mEdje, "Message", rb_cObject);
703
704         rb_define_method (cMsg, "initialize", c_msg_init, -1);
705
706         rb_define_attr (cMsg, "id", 1, 1);
707         rb_define_attr (cMsg, "value", 1, 1);
708
709         eEdjeError = rb_define_class_under (mEdje, "EdjeError",
710                                            rb_eStandardError);
711 }