2 * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
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.
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.
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
23 #include <evas/rb_evas.h>
24 #include <evas/rb_evas_object.h>
28 #include "rb_edje_main.h"
32 static VALUE cMsg, eEdjeError;
34 static void c_mark (RbEdje *e)
36 c_evas_object_mark (&e->real);
38 if (!NIL_P (e->parts))
39 rb_gc_mark (e->parts);
41 if (!NIL_P (e->callbacks))
42 rb_gc_mark (e->callbacks);
44 if (!NIL_P (e->on_text_changed_cb))
45 rb_gc_mark (e->on_text_changed_cb);
48 static void c_free (RbEdje *e)
50 c_evas_object_free (&e->real, false);
56 static VALUE c_alloc (VALUE klass)
62 return Data_Make_Struct (klass, RbEdje, c_mark, c_free, e);
67 * Edje::Edje.new(evas) => edje
69 * Creates an Edje::Edje object.
71 static VALUE c_init (VALUE self, VALUE evas)
73 CHECK_CLASS (evas, cEvas);
74 GET_OBJ (evas, RbEvas, e);
75 GET_OBJ (self, RbEdje, edje);
77 /* c_new_from_pointer() might already have initialized the pointer */
79 edje->real.real = edje_object_add (e->real);
82 edje->callbacks = Qnil;
83 edje->on_text_changed_cb = Qnil;
85 rb_call_super (1, &evas);
90 static VALUE c_new_from_pointer (VALUE klass, VALUE evas, VALUE ptr)
92 VALUE self = rb_obj_alloc (klass);
94 GET_OBJ (self, RbEdje, edje);
96 edje->real.real = (Evas_Object *) ptr;
98 rb_obj_call_init (self, 1, &evas);
107 * Freezes <i>edje</i>.
109 static VALUE c_freeze (VALUE self)
111 GET_OBJ (self, RbEdje, e);
113 edje_object_freeze (e->real.real);
124 static VALUE c_thaw (VALUE self)
126 GET_OBJ (self, RbEdje, e);
128 edje_object_thaw (e->real.real);
135 * edje.load(eet, group) => nil
137 * Loads <i>eet</i> into <i>edje</i>. <i>group</i> is the
138 * name of the group to be displayed.
140 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
142 GET_OBJ (self, RbEdje, e);
144 Check_Type (eet, T_STRING);
145 Check_Type (group, T_STRING);
147 if (!edje_object_file_set (e->real.real, StringValuePtr (eet),
148 StringValuePtr (group)))
149 rb_raise (rb_eException, "Cannot load eet");
156 * edje.get_size_min => array
158 * Returns an array that contains the minimum size
161 static VALUE c_get_size_min (VALUE self)
165 GET_OBJ (self, RbEdje, e);
167 edje_object_size_min_get (e->real.real, &w, &h);
169 return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
174 * edje.get_size_max => array
176 * Returns an array that contains the maximum size
179 static VALUE c_get_size_max (VALUE self)
183 GET_OBJ (self, RbEdje, e);
185 edje_object_size_max_get (e->real.real, &w, &h);
187 return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
192 * edje.part_exists?(part) => true or false
194 * Returns true if <i>edje</i> has a part called <i>part</i>,
195 * else returns false.
197 static VALUE c_part_exists_get (VALUE self, VALUE name)
201 GET_OBJ (self, RbEdje, e);
203 Check_Type (name, T_STRING);
205 r = edje_object_part_exists (e->real.real, StringValuePtr (name));
207 return r ? Qtrue : Qfalse;
212 * edje.part(part_name) => part
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.
218 static VALUE c_part_get (VALUE self, VALUE name)
221 const char *cname = StringValuePtr (name);
223 GET_OBJ (self, RbEdje, e);
225 if (!edje_object_part_exists (e->real.real, cname)) {
226 rb_raise (rb_eException, "Unknown part name - %s", cname);
230 if (NIL_P (e->parts))
231 e->parts = rb_hash_new ();
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);
241 static void on_text_changed (void *data, Evas_Object *eo,
242 const char *part_name)
244 VALUE self = (VALUE) data, part, name;
246 GET_OBJ (self, RbEdje, e);
248 name = rb_str_new2 (part_name);
250 if (NIL_P (e->parts))
251 e->parts = rb_hash_new ();
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);
258 rb_funcall (e->on_text_changed_cb,
259 rb_intern ("call"), 1, part);
264 * edje.on_text_changed { |part_obj| block }
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.
271 static VALUE c_on_text_changed (VALUE self)
273 GET_OBJ (self, RbEdje, e);
275 if (!rb_block_given_p ())
278 e->on_text_changed_cb = rb_block_proc ();
280 edje_object_text_change_cb_set (e->real.real, on_text_changed,
288 * edje.emit_signal(signal, source) => nil
290 * Emits a signal to <i>edje</i>.
292 * edje.emit_signal("signal_foo", "part_bar") #=> nil
294 static VALUE c_emit_signal (VALUE self, VALUE signal, VALUE source)
296 GET_OBJ (self, RbEdje, e);
298 Check_Type (signal, T_STRING);
299 Check_Type (source, T_STRING);
301 edje_object_signal_emit (e->real.real, StringValuePtr (signal),
302 StringValuePtr (source));
307 static void on_signal (void *data, Evas_Object *o,
308 const char *signal, const char *src)
310 rb_funcall ((VALUE) data, rb_intern ("call"), 2,
311 rb_str_new2 (signal), rb_str_new2 (src));
316 * edje.on_signal(signal [, source]) { |signal, source| block } => nil
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
324 static VALUE c_on_signal (int argc, VALUE *argv, VALUE self)
326 VALUE signal, src, cb;
327 const char *ssrc = "*";
329 GET_OBJ (self, RbEdje, e);
331 rb_scan_args (argc, argv, "11", &signal, &src);
333 Check_Type (signal, T_STRING);
336 Check_Type (src, T_STRING);
337 ssrc = StringValuePtr (src);
340 if (!rb_block_given_p ())
343 cb = rb_block_proc ();
345 if (NIL_P (e->callbacks))
346 e->callbacks = rb_ary_new ();
348 rb_ary_push (e->callbacks, cb);
350 edje_object_signal_callback_add (e->real.real,
351 StringValuePtr (signal),
352 ssrc, on_signal, (void *) cb);
359 * edje.play? => true or false
361 * Returns true if <i>edje</i> is in play mode, else returns false.
363 static VALUE c_play_get (VALUE self)
365 GET_OBJ (self, RbEdje, e);
367 return edje_object_play_get (e->real.real) ? Qtrue : Qfalse;
372 * edje.play(true or false)
374 * Sets <i>edje</i> to play resp. pause mode.
376 static VALUE c_play_set (VALUE self, VALUE val)
378 GET_OBJ (self, RbEdje, e);
382 edje_object_play_set (e->real.real, val == Qtrue);
389 * edje.animation? => true or false
391 * Returns the animation state of <i>edje</i>.
393 static VALUE c_animation_get (VALUE self)
395 GET_OBJ (self, RbEdje, e);
397 return edje_object_animation_get (e->real.real) ? Qtrue : Qfalse;
402 * edje.animation(true or false)
404 * Sets the animation state of <i>edje</i>.
406 static VALUE c_animation_set (VALUE self, VALUE val)
408 GET_OBJ (self, RbEdje, e);
412 edje_object_animation_set (e->real.real, val == Qtrue);
417 static VALUE c_data_get (VALUE self, VALUE key)
421 GET_OBJ (self, RbEdje, e);
423 Check_Type (key, T_STRING);
425 s = edje_object_data_get (e->real.real, StringValuePtr (key));
427 return s ? rb_str_new2 (s) : Qnil;
430 static Edje_Message_Type get_msg_type (VALUE val)
433 Edje_Message_Type type;
437 return EDJE_MESSAGE_NONE;
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;
448 len = RARRAY (ary)->len;
450 return EDJE_MESSAGE_NONE;
452 entry = rb_ary_entry (ary, 0);
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;
461 /* first entry is a string.
462 * so if we only have one entry, it's a string set
465 return EDJE_MESSAGE_STRING_SET;
467 entry = rb_ary_entry (ary, 1);
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;
476 return EDJE_MESSAGE_NONE;
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;
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;
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;
512 static VALUE c_send_message (VALUE self, VALUE msg)
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;
527 bool free_data = false;
530 GET_OBJ (self, RbEdje, e);
532 CHECK_CLASS (msg, cMsg);
534 id = FIX2INT (rb_iv_get (msg, "@id"));
535 v = rb_iv_get (msg, "@value");
537 type = get_msg_type (v);
538 if (!NIL_P (ary = rb_check_array_type (v)))
539 len = RARRAY (ary)->len;
542 case EDJE_MESSAGE_NONE:
543 rb_raise (eEdjeError, "unsupported value");
545 case EDJE_MESSAGE_SIGNAL:
546 return Qnil; /* cannot happen */
547 case EDJE_MESSAGE_STRING:
548 msg_s.str = StringValuePtr (v);
551 case EDJE_MESSAGE_INT:
552 msg_i.val = FIX2INT (v);
555 case EDJE_MESSAGE_FLOAT:
556 msg_f.val = NUM2DBL (v);
559 case EDJE_MESSAGE_STRING_SET:
560 s_set = malloc (sizeof (Edje_Message_String_Set) + ((len - 1) * sizeof (char *)));
564 for (i = 0; i < len; i++) {
565 entry = rb_ary_entry (ary, i);
566 s_set->str[i] = StringValuePtr (entry);
572 case EDJE_MESSAGE_INT_SET:
573 i_set = malloc (sizeof (Edje_Message_Int_Set) + ((len - 1) * sizeof (int)));
577 for (i = 0; i < len; i++) {
578 entry = rb_ary_entry (ary, i);
579 i_set->val[i] = FIX2INT (entry);
585 case EDJE_MESSAGE_FLOAT_SET:
586 f_set = malloc (sizeof (Edje_Message_Float_Set) + ((len - 1) * sizeof (double)));
590 for (i = 0; i < len; i++) {
591 entry = rb_ary_entry (ary, i);
592 f_set->val[i] = NUM2DBL (entry);
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);
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);
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;
621 entry = rb_ary_entry (ary, 0);
622 si_set->str = StringValuePtr (entry);
624 for (i = 1; i < len; i++) {
625 entry = rb_ary_entry (ary, i);
626 si_set->val[i - 1] = FIX2INT (entry);
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;
637 entry = rb_ary_entry (ary, 0);
638 sf_set->str = StringValuePtr (entry);
640 for (i = 1; i < len; i++) {
641 entry = rb_ary_entry (ary, i);
642 sf_set->val[i - 1] = NUM2DBL (entry);
650 edje_object_message_send (e->real.real, type, id, data);
658 static VALUE c_msg_init (int argc, VALUE *argv, VALUE self)
663 rb_scan_args (argc, argv, "11", &id, &val);
665 rb_scan_args (argc, argv, "1*", &id, &val);
667 Check_Type (id, T_FIXNUM);
669 rb_iv_set (self, "@id", id);
670 rb_iv_set (self, "@value", val);
675 void Init_Edje (void)
677 cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
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);
700 cMsg = rb_define_class_under (mEdje, "Message", rb_cObject);
702 rb_define_method (cMsg, "initialize", c_msg_init, -1);
704 rb_define_attr (cMsg, "id", 1, 1);
705 rb_define_attr (cMsg, "value", 1, 1);
707 eEdjeError = rb_define_class_under (mEdje, "EdjeError",