1 #include "evas_common.h"
2 #include "evas_private.h"
4 typedef struct _Evas_Object_Smart Evas_Object_Smart;
5 typedef struct _Evas_Smart_Callback Evas_Smart_Callback;
7 struct _Evas_Object_Smart
13 Eina_Inlist *contained;
14 Evas_Smart_Cb_Description_Array callbacks_descriptions;
16 Eina_Bool deletions_waiting : 1;
17 Eina_Bool need_recalculate : 1;
20 struct _Evas_Smart_Callback
25 Evas_Callback_Priority priority;
29 /* private methods for smart objects */
30 static void evas_object_smart_callbacks_clear(Evas_Object *obj);
31 static void evas_object_smart_init(Evas_Object *obj);
32 static void *evas_object_smart_new(void);
33 static void evas_object_smart_render(Evas_Object *obj, void *output, void *context, void *surface, int x, int y);
34 static void evas_object_smart_free(Evas_Object *obj);
35 static void evas_object_smart_render_pre(Evas_Object *obj);
36 static void evas_object_smart_render_post(Evas_Object *obj);
38 static unsigned int evas_object_smart_id_get(Evas_Object *obj);
39 static unsigned int evas_object_smart_visual_id_get(Evas_Object *obj);
40 static void *evas_object_smart_engine_data_get(Evas_Object *obj);
42 static const Evas_Object_Func object_func =
44 /* methods (compulsory) */
45 evas_object_smart_free,
46 evas_object_smart_render,
47 evas_object_smart_render_pre,
48 evas_object_smart_render_post,
49 evas_object_smart_id_get,
50 evas_object_smart_visual_id_get,
51 evas_object_smart_engine_data_get,
52 /* these are optional. NULL = nothing */
68 EVAS_MEMPOOL(_mp_obj);
73 evas_object_smart_data_set(Evas_Object *obj, void *data)
77 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
80 o = (Evas_Object_Smart *)(obj->object_data);
81 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
88 evas_object_smart_data_get(const Evas_Object *obj)
92 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
95 o = (Evas_Object_Smart *)(obj->object_data);
101 evas_object_smart_smart_get(const Evas_Object *obj)
103 Evas_Object_Smart *o;
105 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
108 o = (Evas_Object_Smart *)(obj->object_data);
109 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
112 return obj->smart.smart;
116 evas_object_smart_member_add(Evas_Object *obj, Evas_Object *smart_obj)
118 Evas_Object_Smart *o;
120 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
123 MAGIC_CHECK(smart_obj, Evas_Object, MAGIC_OBJ);
126 o = (Evas_Object_Smart *)(smart_obj->object_data);
127 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
133 CRIT("Adding deleted object %p to smart obj %p", obj, smart_obj);
137 if (smart_obj->delete_me)
139 CRIT("Adding object %p to deleted smart obj %p", obj, smart_obj);
143 if (!smart_obj->layer)
145 CRIT("No evas surface associated with smart object (%p)", smart_obj);
149 if (obj->layer && smart_obj->layer
150 && obj->layer->evas != smart_obj->layer->evas)
152 CRIT("Adding object %p from Evas (%p) from another Evas (%p)", obj, obj->layer->evas, smart_obj->layer->evas);
157 if (obj->smart.parent == smart_obj) return;
159 if (obj->smart.parent) evas_object_smart_member_del(obj);
161 evas_object_release(obj, 1);
162 obj->layer = smart_obj->layer;
163 obj->cur.layer = obj->layer->layer;
165 obj->smart.parent = smart_obj;
166 o->contained = eina_inlist_append(o->contained, EINA_INLIST_GET(obj));
167 evas_object_smart_member_cache_invalidate(obj);
169 evas_object_change(obj);
170 evas_object_mapped_clip_across_mark(obj);
171 if (smart_obj->smart.smart->smart_class->member_add)
172 smart_obj->smart.smart->smart_class->member_add(smart_obj, obj);
176 evas_object_smart_member_del(Evas_Object *obj)
178 Evas_Object_Smart *o;
179 Evas_Object *smart_obj;
181 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
185 if (!obj->smart.parent) return;
187 smart_obj = obj->smart.parent;
188 if (smart_obj->smart.smart->smart_class->member_del)
189 smart_obj->smart.smart->smart_class->member_del(smart_obj, obj);
191 o = (Evas_Object_Smart *)(obj->smart.parent->object_data);
192 o->contained = eina_inlist_remove(o->contained, EINA_INLIST_GET(obj));
193 obj->smart.parent = NULL;
194 evas_object_smart_member_cache_invalidate(obj);
196 obj->cur.layer = obj->layer->layer;
197 evas_object_inject(obj, obj->layer->evas);
199 evas_object_change(obj);
200 evas_object_mapped_clip_across_mark(obj);
204 evas_object_smart_parent_get(const Evas_Object *obj)
206 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
210 return obj->smart.parent;
214 evas_object_smart_type_check(const Evas_Object *obj, const char *type)
216 const Evas_Smart_Class *sc;
218 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
222 if (!obj->smart.smart)
224 sc = obj->smart.smart->smart_class;
227 if (!strcmp(sc->name, type))
236 evas_object_smart_type_check_ptr(const Evas_Object *obj, const char *type)
238 const Evas_Smart_Class *sc;
240 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
244 if (!obj->smart.smart)
246 sc = obj->smart.smart->smart_class;
249 if (sc->name == type)
258 evas_object_smart_members_get(const Evas_Object *obj)
260 Evas_Object_Smart *o;
264 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
267 o = (Evas_Object_Smart *)(obj->object_data);
268 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
273 for (member = o->contained; member; member = member->next)
274 members = eina_list_append(members, member);
280 evas_object_smart_members_get_direct(const Evas_Object *obj)
282 Evas_Object_Smart *o;
284 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
287 o = (Evas_Object_Smart *)(obj->object_data);
288 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
295 _evas_object_smart_members_all_del(Evas_Object *obj)
297 Evas_Object_Smart *o = (Evas_Object_Smart *)(obj->object_data);
299 evas_object_del((Evas_Object *)(o->contained));
303 evas_object_smart_add(Evas *e, Evas_Smart *s)
307 MAGIC_CHECK(e, Evas, MAGIC_EVAS);
310 MAGIC_CHECK(s, Evas_Smart, MAGIC_SMART);
314 obj = evas_object_new(e);
315 if (!obj) return NULL;
316 obj->smart.smart = s;
317 obj->type = s->smart_class->name;
318 evas_object_smart_init(obj);
319 evas_object_inject(obj, e);
321 evas_object_smart_use(s);
323 if (s->smart_class->add) s->smart_class->add(obj);
329 _callback_priority_cmp(const void *_a, const void *_b)
331 const Evas_Smart_Callback *a, *b;
332 a = (const Evas_Smart_Callback *) _a;
333 b = (const Evas_Smart_Callback *) _b;
334 if (a->priority < b->priority)
341 evas_object_smart_callback_add(Evas_Object *obj, const char *event, Evas_Smart_Cb func, const void *data)
343 evas_object_smart_callback_priority_add(obj, event,
344 EVAS_CALLBACK_PRIORITY_DEFAULT, func, data);
348 evas_object_smart_callback_priority_add(Evas_Object *obj, const char *event, Evas_Callback_Priority priority, Evas_Smart_Cb func, const void *data)
350 Evas_Object_Smart *o;
351 Evas_Smart_Callback *cb;
353 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
356 o = (Evas_Object_Smart *)(obj->object_data);
357 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
362 EVAS_MEMPOOL_INIT(_mp_cb, "evas_smart_callback", Evas_Smart_Callback, 512, );
363 cb = EVAS_MEMPOOL_ALLOC(_mp_cb, Evas_Smart_Callback);
365 EVAS_MEMPOOL_PREP(_mp_cb, cb, Evas_Smart_Callback);
366 cb->event = eina_stringshare_add(event);
368 cb->func_data = (void *)data;
369 cb->priority = priority;
370 o->callbacks = eina_list_sorted_insert(o->callbacks, _callback_priority_cmp,
375 evas_object_smart_callback_del(Evas_Object *obj, const char *event, Evas_Smart_Cb func)
377 Evas_Object_Smart *o;
379 Evas_Smart_Callback *cb;
381 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
384 o = (Evas_Object_Smart *)(obj->object_data);
385 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
388 if (!event) return NULL;
389 EINA_LIST_FOREACH(o->callbacks, l, cb)
391 if ((!strcmp(cb->event, event)) && (cb->func == func))
395 data = cb->func_data;
397 o->deletions_waiting = 1;
398 evas_object_smart_callbacks_clear(obj);
406 evas_object_smart_callback_call(Evas_Object *obj, const char *event, void *event_info)
408 Evas_Object_Smart *o;
410 Evas_Smart_Callback *cb;
411 const char *strshare;
413 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
416 o = (Evas_Object_Smart *)(obj->object_data);
417 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
421 if (obj->delete_me) return;
423 strshare = eina_stringshare_add(event);
424 EINA_LIST_FOREACH(o->callbacks, l, cb)
428 if (cb->event == strshare)
429 cb->func(cb->func_data, obj, event_info);
434 eina_stringshare_del(strshare);
436 evas_object_smart_callbacks_clear(obj);
440 evas_object_smart_callbacks_descriptions_set(Evas_Object *obj, const Evas_Smart_Cb_Description *descriptions)
442 const Evas_Smart_Cb_Description *d;
443 Evas_Object_Smart *o;
444 unsigned int i, count = 0;
446 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
449 o = (Evas_Object_Smart *)(obj->object_data);
450 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
454 if ((!descriptions) || (!descriptions->name))
456 evas_smart_cb_descriptions_resize(&o->callbacks_descriptions, 0);
460 for (count = 0, d = descriptions; d->name; d++)
463 evas_smart_cb_descriptions_resize(&o->callbacks_descriptions, count);
464 if (count == 0) return EINA_TRUE;
466 for (i = 0, d = descriptions; i < count; d++, i++)
467 o->callbacks_descriptions.array[i] = d;
469 evas_smart_cb_descriptions_fix(&o->callbacks_descriptions);
475 evas_object_smart_callbacks_descriptions_get(const Evas_Object *obj, const Evas_Smart_Cb_Description ***class_descriptions, unsigned int *class_count, const Evas_Smart_Cb_Description ***instance_descriptions, unsigned int *instance_count)
477 Evas_Object_Smart *o;
479 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
480 if (class_descriptions) *class_descriptions = NULL;
481 if (class_count) *class_count = 0;
482 if (instance_descriptions) *instance_descriptions = NULL;
483 if (instance_count) *instance_count = 0;
486 o = (Evas_Object_Smart *)(obj->object_data);
487 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
488 if (class_descriptions) *class_descriptions = NULL;
489 if (class_count) *class_count = 0;
490 if (instance_descriptions) *instance_descriptions = NULL;
491 if (instance_count) *instance_count = 0;
495 if (class_descriptions)
496 *class_descriptions = obj->smart.smart->callbacks.array;
498 *class_count = obj->smart.smart->callbacks.size;
500 if (instance_descriptions)
501 *instance_descriptions = o->callbacks_descriptions.array;
503 *instance_count = o->callbacks_descriptions.size;
507 evas_object_smart_callback_description_find(const Evas_Object *obj, const char *name, const Evas_Smart_Cb_Description **class_description, const Evas_Smart_Cb_Description **instance_description)
509 Evas_Object_Smart *o;
513 if (class_description) *class_description = NULL;
514 if (instance_description) *instance_description = NULL;
518 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
519 if (class_description) *class_description = NULL;
520 if (instance_description) *instance_description = NULL;
523 o = (Evas_Object_Smart *)(obj->object_data);
524 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
525 if (class_description) *class_description = NULL;
526 if (instance_description) *instance_description = NULL;
530 if (class_description)
531 *class_description = evas_smart_cb_description_find
532 (&obj->smart.smart->callbacks, name);
534 if (instance_description)
535 *instance_description = evas_smart_cb_description_find
536 (&o->callbacks_descriptions, name);
540 evas_object_smart_need_recalculate_set(Evas_Object *obj, Eina_Bool value)
542 Evas_Object_Smart *o;
543 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
546 o = obj->object_data;
547 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
552 if (o->need_recalculate == value)
555 if (obj->recalculate_cycle > 64)
557 ERR("Object %p is not stable during recalc loop", obj);
560 if (obj->layer->evas->in_smart_calc)
561 obj->recalculate_cycle++;
562 o->need_recalculate = value;
564 if (!obj->smart.smart->smart_class->calculate) return;
566 /* XXX: objects can be present multiple times in calculate_objects()
567 * XXX: after a set-unset-set cycle, but it's not a problem since
568 * XXX: on _evas_render_call_smart_calculate() will check for the flag
569 * XXX: and it will be unset after the first.
571 if (o->need_recalculate)
573 Evas *e = obj->layer->evas;
574 eina_array_push(&e->calculate_objects, obj);
576 /* TODO: else, remove from array */
580 evas_object_smart_need_recalculate_get(const Evas_Object *obj)
582 Evas_Object_Smart *o;
583 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
586 o = obj->object_data;
587 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
591 return o->need_recalculate;
595 evas_object_smart_calculate(Evas_Object *obj)
597 Evas_Object_Smart *o;
598 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
601 o = obj->object_data;
602 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
606 if (!obj->smart.smart->smart_class->calculate)
609 o->need_recalculate = 0;
610 obj->smart.smart->smart_class->calculate(obj);
614 evas_smart_objects_calculate(Evas *e)
616 MAGIC_CHECK(e, Evas, MAGIC_EVAS);
619 evas_call_smarts_calculate(e);
623 * Call calculate() on all smart objects that need_recalculate.
628 evas_call_smarts_calculate(Evas *e)
630 Eina_Array *calculate;
632 Eina_Array_Iterator it;
636 calculate = &e->calculate_objects;
637 for (i = 0; i < eina_array_count_get(calculate); ++i)
639 Evas_Object_Smart *o;
641 obj = eina_array_data_get(calculate, i);
645 o = obj->object_data;
646 if (o->need_recalculate)
648 o->need_recalculate = 0;
649 obj->smart.smart->smart_class->calculate(obj);
652 EINA_ARRAY_ITER_NEXT(calculate, i, obj, it)
654 obj->recalculate_cycle = 0;
657 if (e->in_smart_calc == 0) eina_array_clean(calculate);
661 evas_object_smart_changed(Evas_Object *obj)
663 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
666 evas_object_change(obj);
667 evas_object_smart_need_recalculate_set(obj, 1);
672 evas_object_smart_callbacks_clear(Evas_Object *obj)
674 Evas_Object_Smart *o;
676 Evas_Smart_Callback *cb;
678 o = (Evas_Object_Smart *)(obj->object_data);
680 if (o->walking_list) return;
681 if (!o->deletions_waiting) return;
682 for (l = o->callbacks; l;)
684 cb = eina_list_data_get(l);
685 l = eina_list_next(l);
688 o->callbacks = eina_list_remove(o->callbacks, cb);
689 if (cb->event) eina_stringshare_del(cb->event);
690 EVAS_MEMPOOL_FREE(_mp_cb, cb);
696 evas_object_smart_del(Evas_Object *obj)
700 if (obj->delete_me) return;
701 s = obj->smart.smart;
702 if ((s) && (s->smart_class->del)) s->smart_class->del(obj);
703 if (obj->smart.parent) evas_object_smart_member_del(obj);
704 if (s) evas_object_smart_unuse(s);
708 evas_object_smart_cleanup(Evas_Object *obj)
710 Evas_Object_Smart *o;
712 if (obj->smart.parent)
713 evas_object_smart_member_del(obj);
715 o = (Evas_Object_Smart *)(obj->object_data);
716 if (o->magic == MAGIC_OBJ_SMART)
719 evas_object_smart_member_del((Evas_Object *)o->contained);
723 Evas_Smart_Callback *cb;
725 cb = o->callbacks->data;
726 o->callbacks = eina_list_remove(o->callbacks, cb);
727 if (cb->event) eina_stringshare_del(cb->event);
728 EVAS_MEMPOOL_FREE(_mp_cb, cb);
731 evas_smart_cb_descriptions_resize(&o->callbacks_descriptions, 0);
735 obj->smart.parent = NULL;
736 obj->smart.smart = NULL;
740 evas_object_smart_member_cache_invalidate(Evas_Object *obj)
742 Evas_Object_Smart *o;
745 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
749 obj->parent_cache_valid = 0;
751 o = (Evas_Object_Smart *)(obj->object_data);
752 if (o->magic != MAGIC_OBJ_SMART)
755 for (l = o->contained; l; l = l->next)
759 obj2 = (Evas_Object *)l;
760 evas_object_smart_member_cache_invalidate(obj2);
765 evas_object_smart_member_raise(Evas_Object *member)
767 Evas_Object_Smart *o;
769 o = (Evas_Object_Smart *)(member->smart.parent->object_data);
770 o->contained = eina_inlist_demote(o->contained, EINA_INLIST_GET(member));
774 evas_object_smart_member_lower(Evas_Object *member)
776 Evas_Object_Smart *o;
778 o = (Evas_Object_Smart *)(member->smart.parent->object_data);
779 o->contained = eina_inlist_promote(o->contained, EINA_INLIST_GET(member));
783 evas_object_smart_member_stack_above(Evas_Object *member, Evas_Object *other)
785 Evas_Object_Smart *o;
787 o = (Evas_Object_Smart *)(member->smart.parent->object_data);
788 o->contained = eina_inlist_remove(o->contained, EINA_INLIST_GET(member));
789 o->contained = eina_inlist_append_relative(o->contained, EINA_INLIST_GET(member), EINA_INLIST_GET(other));
793 evas_object_smart_member_stack_below(Evas_Object *member, Evas_Object *other)
795 Evas_Object_Smart *o;
797 o = (Evas_Object_Smart *)(member->smart.parent->object_data);
798 o->contained = eina_inlist_remove(o->contained, EINA_INLIST_GET(member));
799 o->contained = eina_inlist_prepend_relative(o->contained, EINA_INLIST_GET(member), EINA_INLIST_GET(other));
802 /* all nice and private */
804 evas_object_smart_init(Evas_Object *obj)
806 /* alloc smart obj, setup methods and default values */
807 obj->object_data = evas_object_smart_new();
808 /* set up default settings for this kind of object */
809 obj->cur.color.r = 255;
810 obj->cur.color.g = 255;
811 obj->cur.color.b = 255;
812 obj->cur.color.a = 255;
813 obj->cur.geometry.x = 0;
814 obj->cur.geometry.y = 0;
815 obj->cur.geometry.w = 0;
816 obj->cur.geometry.h = 0;
818 /* set up object-specific settings */
819 obj->prev = obj->cur;
820 /* set up methods (compulsory) */
821 obj->func = &object_func;
825 evas_object_smart_new(void)
827 Evas_Object_Smart *o;
829 /* alloc obj private data */
830 EVAS_MEMPOOL_INIT(_mp_obj, "evas_object_smart", Evas_Object_Smart, 256, NULL);
831 o = EVAS_MEMPOOL_ALLOC(_mp_obj, Evas_Object_Smart);
833 EVAS_MEMPOOL_PREP(_mp_obj, o, Evas_Object_Smart);
834 o->magic = MAGIC_OBJ_SMART;
839 evas_object_smart_free(Evas_Object *obj)
841 Evas_Object_Smart *o;
843 /* frees private object data. very simple here */
844 o = (Evas_Object_Smart *)(obj->object_data);
845 MAGIC_CHECK(o, Evas_Object_Smart, MAGIC_OBJ_SMART);
850 EVAS_MEMPOOL_FREE(_mp_obj, o);
854 evas_object_smart_render(Evas_Object *obj __UNUSED__, void *output __UNUSED__, void *context __UNUSED__, void *surface __UNUSED__, int x __UNUSED__, int y __UNUSED__)
860 evas_object_smart_render_pre(Evas_Object *obj)
862 if (obj->pre_render_done) return;
863 if ((obj->cur.map != obj->prev.map) ||
864 (obj->cur.usemap != obj->prev.usemap))
866 evas_object_render_pre_prev_cur_add(&obj->layer->evas->clip_changes, obj);
870 obj->pre_render_done = 1;
874 evas_object_smart_render_post(Evas_Object *obj)
876 obj->prev = obj->cur;
879 static unsigned int evas_object_smart_id_get(Evas_Object *obj)
881 Evas_Object_Smart *o;
883 o = (Evas_Object_Smart *)(obj->object_data);
885 return MAGIC_OBJ_SMART;
888 static unsigned int evas_object_smart_visual_id_get(Evas_Object *obj)
890 Evas_Object_Smart *o;
892 o = (Evas_Object_Smart *)(obj->object_data);
894 return MAGIC_OBJ_CONTAINER;
897 static void *evas_object_smart_engine_data_get(Evas_Object *obj)
899 Evas_Object_Smart *o;
901 o = (Evas_Object_Smart *)(obj->object_data);
903 return o->engine_data;