2 * $Id: rb_edje.c 331 2005-04-28 18:17:21Z tilman $
4 * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
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.
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.
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
25 #include <evas/rb_evas.h>
26 #include <evas/rb_evas_object.h>
30 #include "rb_edje_main.h"
34 static VALUE cMsg, eEdjeError;
36 static void c_mark (RbEdje *e)
38 c_evas_object_mark (&e->real);
40 if (!NIL_P (e->parts))
41 rb_gc_mark (e->parts);
43 if (!NIL_P (e->callbacks))
44 rb_gc_mark (e->callbacks);
46 if (!NIL_P (e->on_text_changed_cb))
47 rb_gc_mark (e->on_text_changed_cb);
50 static void c_free (RbEdje *e)
52 c_evas_object_free (&e->real, false);
60 * Edje::Edje.new(evas) => edje
62 * Creates an Edje::Edje object.
64 static VALUE c_new (VALUE klass, VALUE evas)
69 CHECK_CLASS (evas, cEvas);
70 GET_OBJ (evas, RbEvas, e);
74 self = Data_Make_Struct (klass, RbEdje, c_mark, c_free, edje);
76 edje->real.real = edje_object_add (e->real);
78 edje->callbacks = Qnil;
79 edje->on_text_changed_cb = Qnil;
82 rb_obj_call_init (self, 1, argv);
91 * Freezes <i>edje</i>.
93 static VALUE c_freeze (VALUE self)
95 GET_OBJ (self, RbEdje, e);
97 edje_object_freeze (e->real.real);
108 static VALUE c_thaw (VALUE self)
110 GET_OBJ (self, RbEdje, e);
112 edje_object_thaw (e->real.real);
119 * edje.load(eet, group) => nil
121 * Loads <i>eet</i> into <i>edje</i>. <i>group</i> is the
122 * name of the group to be displayed.
124 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
126 GET_OBJ (self, RbEdje, e);
128 Check_Type (eet, T_STRING);
129 Check_Type (group, T_STRING);
131 if (!edje_object_file_set (e->real.real, StringValuePtr (eet),
132 StringValuePtr (group)))
133 rb_raise (rb_eException, "Cannot load eet");
140 * edje.get_size_min => array
142 * Returns an array that contains the minimum size
145 static VALUE c_get_size_min (VALUE self)
149 GET_OBJ (self, RbEdje, e);
151 edje_object_size_min_get (e->real.real, &w, &h);
153 return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
158 * edje.get_size_max => array
160 * Returns an array that contains the maximum size
163 static VALUE c_get_size_max (VALUE self)
167 GET_OBJ (self, RbEdje, e);
169 edje_object_size_max_get (e->real.real, &w, &h);
171 return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
176 * edje.part_exists?(part) => true or false
178 * Returns true if <i>edje</i> has a part called <i>part</i>,
179 * else returns false.
181 static VALUE c_part_exists_get (VALUE self, VALUE name)
185 GET_OBJ (self, RbEdje, e);
187 Check_Type (name, T_STRING);
189 r = edje_object_part_exists (e->real.real, StringValuePtr (name));
191 return r ? Qtrue : Qfalse;
196 * edje.part(part_name) => part
198 * Returns the <code>Edje::Part</code> object that corresponds to
199 * <i>part_name</i>. If there's no part with that name in <i>edje</i>,
200 * an exception is raised.
202 static VALUE c_part_get (VALUE self, VALUE name)
205 const char *cname = StringValuePtr (name);
207 GET_OBJ (self, RbEdje, e);
209 if (!edje_object_part_exists (e->real.real, cname)) {
210 rb_raise (rb_eException, "Unknown part name - %s", cname);
214 if (NIL_P (e->parts))
215 e->parts = rb_hash_new ();
217 if (NIL_P (part = rb_hash_aref (e->parts, name))) {
218 part = TO_PART (self, name);
219 rb_hash_aset (e->parts, name, part);
225 static void on_text_changed (void *data, Evas_Object *eo,
226 const char *part_name)
228 VALUE self = (VALUE) data, part, name;
230 GET_OBJ (self, RbEdje, e);
232 name = rb_str_new2 (part_name);
234 if (NIL_P (e->parts))
235 e->parts = rb_hash_new ();
237 if (NIL_P (part = rb_hash_aref (e->parts, name))) {
238 part = TO_PART (self, name);
239 rb_hash_aset (e->parts, name, part);
242 rb_funcall (e->on_text_changed_cb,
243 rb_intern ("call"), 1, part);
248 * edje.on_text_changed { |part_obj| block }
250 * Registers a callback that will get called when the text
251 * of any part is changed in <i>edje</i>.
252 * The block is passed the <code>Edje::Part</code> object
253 * of which the text changed.
255 static VALUE c_on_text_changed (VALUE self)
257 GET_OBJ (self, RbEdje, e);
259 if (!rb_block_given_p ())
262 e->on_text_changed_cb = rb_block_proc ();
264 edje_object_text_change_cb_set (e->real.real, on_text_changed,
272 * edje.emit_signal(signal, source) => nil
274 * Emits a signal to <i>edje</i>.
276 * edje.emit_signal("signal_foo", "part_bar") #=> nil
278 static VALUE c_emit_signal (VALUE self, VALUE signal, VALUE source)
280 GET_OBJ (self, RbEdje, e);
282 Check_Type (signal, T_STRING);
283 Check_Type (source, T_STRING);
285 edje_object_signal_emit (e->real.real, StringValuePtr (signal),
286 StringValuePtr (source));
291 static void on_signal (void *data, Evas_Object *o,
292 const char *signal, const char *src)
294 rb_funcall ((VALUE) data, rb_intern ("call"), 2,
295 rb_str_new2 (signal), rb_str_new2 (src));
300 * edje.on_signal(signal [, source]) { |signal, source| block } => nil
302 * Registers a callback that will get called when <i>signal</i>
303 * is emitted by <i>source</i>.
304 * If source is nil, "*" will be used instead.
305 * The block is passed two strings, signal and source, which identify
308 static VALUE c_on_signal (int argc, VALUE *argv, VALUE self)
310 VALUE signal, src, cb;
313 GET_OBJ (self, RbEdje, e);
315 rb_scan_args (argc, argv, "11", &signal, &src);
317 Check_Type (signal, T_STRING);
320 Check_Type (src, T_STRING);
321 ssrc = StringValuePtr (src);
324 if (!rb_block_given_p ())
327 cb = rb_block_proc ();
329 if (NIL_P (e->callbacks))
330 e->callbacks = rb_ary_new ();
332 rb_ary_push (e->callbacks, cb);
334 edje_object_signal_callback_add (e->real.real,
335 StringValuePtr (signal),
336 ssrc, on_signal, (void *) cb);
343 * edje.play? => true or false
345 * Returns true if <i>edje</i> is in play mode, else returns false.
347 static VALUE c_play_get (VALUE self)
349 GET_OBJ (self, RbEdje, e);
351 return edje_object_play_get (e->real.real) ? Qtrue : Qfalse;
356 * edje.play(true or false)
358 * Sets <i>edje</i> to play resp. pause mode.
360 static VALUE c_play_set (VALUE self, VALUE val)
362 GET_OBJ (self, RbEdje, e);
366 edje_object_play_set (e->real.real, val == Qtrue);
373 * edje.animation? => true or false
375 * Returns the animation state of <i>edje</i>.
377 static VALUE c_animation_get (VALUE self)
379 GET_OBJ (self, RbEdje, e);
381 return edje_object_animation_get (e->real.real) ? Qtrue : Qfalse;
386 * edje.animation(true or false)
388 * Sets the animation state of <i>edje</i>.
390 static VALUE c_animation_set (VALUE self, VALUE val)
392 GET_OBJ (self, RbEdje, e);
396 edje_object_animation_set (e->real.real, val == Qtrue);
401 static VALUE c_data_get (VALUE self, VALUE key)
405 GET_OBJ (self, RbEdje, e);
407 Check_Type (key, T_STRING);
409 s = edje_object_data_get (e->real.real, StringValuePtr (key));
411 return s ? rb_str_new2 (s) : Qnil;
414 static Edje_Message_Type get_msg_type (VALUE val)
417 Edje_Message_Type type;
421 return EDJE_MESSAGE_NONE;
423 if (!NIL_P (rb_check_string_type (val))) {
424 return EDJE_MESSAGE_STRING;
425 } else if (rb_obj_is_kind_of (val, rb_cFixnum)) {
426 return EDJE_MESSAGE_INT;
427 } else if (rb_obj_is_kind_of (val, rb_cFloat)) {
428 return EDJE_MESSAGE_FLOAT;
429 } else if (NIL_P (ary = rb_check_array_type (val)))
430 return EDJE_MESSAGE_NONE;
432 len = RARRAY (ary)->len;
434 return EDJE_MESSAGE_NONE;
436 entry = rb_ary_entry (ary, 0);
438 if (rb_obj_is_kind_of (entry, rb_cFixnum))
439 return EDJE_MESSAGE_INT_SET;
440 else if (rb_obj_is_kind_of (entry, rb_cFloat))
441 return EDJE_MESSAGE_FLOAT_SET;
442 else if (NIL_P (rb_check_string_type (entry)))
443 return EDJE_MESSAGE_NONE;
445 /* first entry is a string.
446 * so if we only have one entry, it's a string set
449 return EDJE_MESSAGE_STRING_SET;
451 entry = rb_ary_entry (ary, 1);
453 if (!NIL_P (rb_check_string_type (entry)))
454 type = EDJE_MESSAGE_STRING_SET;
455 else if (rb_obj_is_kind_of (entry, rb_cFixnum))
456 type = len == 2 ? EDJE_MESSAGE_STRING_INT : EDJE_MESSAGE_STRING_INT_SET;
457 else if (rb_obj_is_kind_of (entry, rb_cFloat))
458 type = len == 2 ? EDJE_MESSAGE_STRING_FLOAT :EDJE_MESSAGE_STRING_FLOAT_SET;
460 return EDJE_MESSAGE_NONE;
463 case EDJE_MESSAGE_STRING_SET:
464 for (i = 2; i < len; i++) {
465 entry = rb_ary_entry (ary, i);
466 if (NIL_P(rb_check_string_type (entry)))
467 return EDJE_MESSAGE_NONE;
471 case EDJE_MESSAGE_INT_SET:
472 case EDJE_MESSAGE_STRING_INT_SET:
473 for (i = 2; i < len; i++) {
474 entry = rb_ary_entry (ary, i);
475 if (!rb_obj_is_kind_of (entry, rb_cFixnum))
476 return EDJE_MESSAGE_NONE;
480 case EDJE_MESSAGE_FLOAT_SET:
481 case EDJE_MESSAGE_STRING_FLOAT_SET:
482 for (i = 2; i < len; i++) {
483 entry = rb_ary_entry (ary, 2);
484 if (!rb_obj_is_kind_of (entry, rb_cFloat))
485 return EDJE_MESSAGE_NONE;
496 static VALUE c_send_message (VALUE self, VALUE msg)
498 Edje_Message_String msg_s;
499 Edje_Message_Int msg_i;
500 Edje_Message_Float msg_f;
501 Edje_Message_String_Set *s_set = NULL;
502 Edje_Message_Int_Set *i_set = NULL;
503 Edje_Message_Float_Set *f_set = NULL;
504 Edje_Message_String_Int si;
505 Edje_Message_String_Float sf;
506 Edje_Message_String_Int_Set *si_set = NULL;
507 Edje_Message_String_Float_Set *sf_set = NULL;
508 Edje_Message_Type type;
511 bool free_data = false;
514 GET_OBJ (self, RbEdje, e);
516 CHECK_CLASS (msg, cMsg);
518 id = NUM2INT (rb_iv_get (msg, "@id"));
519 v = rb_iv_get (msg, "@value");
521 type = get_msg_type (v);
522 ary = rb_check_array_type (v);
525 case EDJE_MESSAGE_NONE:
526 rb_raise (eEdjeError, "unsupported value");
528 case EDJE_MESSAGE_SIGNAL:
529 return Qnil; /* cannot happen */
530 case EDJE_MESSAGE_STRING:
531 msg_s.str = StringValuePtr (v);
534 case EDJE_MESSAGE_INT:
535 msg_i.val = FIX2INT (v);
538 case EDJE_MESSAGE_FLOAT:
539 msg_f.val = NUM2DBL (v);
542 case EDJE_MESSAGE_STRING_SET:
543 s_set = malloc (sizeof (Edje_Message_String_Set) + ((len - 1) * sizeof (char *)));
547 for (i = 0; i < len; i++) {
548 entry = rb_ary_entry (ary, i);
549 s_set->str[i] = StringValuePtr (entry);
555 case EDJE_MESSAGE_INT_SET:
556 i_set = malloc (sizeof (Edje_Message_Int_Set) + ((len - 1) * sizeof (int)));
560 for (i = 0; i < len; i++) {
561 entry = rb_ary_entry (ary, i);
562 i_set->val[i] = FIX2INT (entry);
568 case EDJE_MESSAGE_FLOAT_SET:
569 f_set = malloc (sizeof (Edje_Message_Float_Set) + ((len - 1) * sizeof (double)));
573 for (i = 0; i < len; i++) {
574 entry = rb_ary_entry (ary, i);
575 f_set->val[i] = NUM2DBL (entry);
581 case EDJE_MESSAGE_STRING_INT:
582 entry = rb_ary_entry (ary, 0);
583 si.str = StringValuePtr (entry);
584 entry = rb_ary_entry (ary, 1);
585 si.val = FIX2INT (entry);
590 case EDJE_MESSAGE_STRING_FLOAT:
591 entry = rb_ary_entry (ary, 0);
592 sf.str = StringValuePtr (entry);
593 entry = rb_ary_entry (ary, 1);
594 sf.val = NUM2DBL (entry);
599 case EDJE_MESSAGE_STRING_INT_SET:
600 si_set = malloc (sizeof (Edje_Message_String_Int_Set) + ((len - 1) * sizeof (int)));
601 si_set->count = len - 1;
604 entry = rb_ary_entry (ary, 0);
605 si_set->str = StringValuePtr (entry);
607 for (i = 1; i < len; i++) {
608 entry = rb_ary_entry (ary, i);
609 si_set->val[i - 1] = FIX2INT (entry);
615 case EDJE_MESSAGE_STRING_FLOAT_SET:
616 sf_set = malloc (sizeof (Edje_Message_String_Float_Set) + ((len - 1) * sizeof (double)));
617 sf_set->count = len - 1;
620 entry = rb_ary_entry (ary, 0);
621 sf_set->str = StringValuePtr (entry);
623 for (i = 1; i < len; i++) {
624 entry = rb_ary_entry (ary, i);
625 sf_set->val[i - 1] = NUM2DBL (entry);
633 edje_object_message_send (e->real.real, type, id, data);
641 static VALUE c_msg_init (int argc, VALUE *argv, VALUE self)
645 rb_scan_args (argc, argv, "01", &val);
647 rb_iv_set (self, "@id", UINT2NUM (0));
648 rb_iv_set (self, "@value", val);
653 void Init_Edje (void)
655 cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
657 rb_define_singleton_method (cEdje, "new", c_new, 1);
658 rb_define_method (cEdje, "freeze", c_freeze, 0);
659 rb_define_method (cEdje, "thaw", c_thaw, 0);
660 rb_define_method (cEdje, "load", c_load, 2);
661 rb_define_method (cEdje, "get_size_min", c_get_size_min, 0);
662 rb_define_method (cEdje, "get_size_max", c_get_size_max, 0);
663 rb_define_method (cEdje, "part_exists?", c_part_exists_get, 1);
664 rb_define_method (cEdje, "part", c_part_get, 1);
665 rb_define_method (cEdje, "on_text_changed", c_on_text_changed, 0);
666 rb_define_method (cEdje, "emit_signal", c_emit_signal, 2);
667 rb_define_method (cEdje, "on_signal", c_on_signal, -1);
668 rb_define_method (cEdje, "play?", c_play_get, 0);
669 rb_define_method (cEdje, "play=", c_play_set, 1);
670 rb_define_method (cEdje, "animation?", c_animation_get, 0);
671 rb_define_method (cEdje, "animation=", c_animation_set, 1);
672 rb_define_method (cEdje, "data", c_data_get, 1);
673 rb_define_method (cEdje, "send_message", c_send_message, 1);
675 cMsg = rb_define_class_under (mEdje, "Message", rb_cObject);
677 rb_define_method (cMsg, "initialize", c_msg_init, -1);
679 rb_define_attr (cMsg, "id", 1, 1);
680 rb_define_attr (cMsg, "value", 1, 1);
682 eEdjeError = rb_define_class_under (mEdje, "EdjeError",