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