================================================================
[framework/uifw/elementary.git] / src / lib / elm_layout.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Layout Layout
6  * @ingroup Elementary
7  *
8  * This takes a standard Edje design file and wraps it very thinly
9  * in a widget and handles swallowing widgets into swallow regions
10  * in the Edje object, allowing Edje to be used as a design and
11  * layout tool
12  */
13
14 typedef struct _Widget_Data Widget_Data;
15 typedef struct _Subinfo Subinfo;
16 typedef struct _Part_Cursor Part_Cursor;
17
18 struct _Widget_Data
19 {
20    Evas_Object *obj;
21    Evas_Object *lay;
22    Eina_List *subs;
23    Eina_List *parts_cursors;
24    Eina_Bool needs_size_calc:1;
25    const char *clas, *group, *style;
26 };
27
28 struct _Subinfo
29 {
30    const char *part;
31    Evas_Object *obj;
32    enum {
33      SWALLOW,
34      BOX_APPEND,
35      BOX_PREPEND,
36      BOX_INSERT_BEFORE,
37      BOX_INSERT_AT,
38      TABLE_PACK,
39      TEXT
40    } type;
41    union {
42       union {
43          const Evas_Object *reference;
44          unsigned int pos;
45       } box;
46       struct {
47          unsigned short col, row, colspan, rowspan;
48       } table;
49       struct {
50          const char *text;
51       } text;
52    } p;
53 };
54
55 struct _Part_Cursor
56 {
57    Evas_Object *obj;
58    const char *part;
59    const char *cursor;
60    const char *style;
61    Eina_Bool engine_only:1;
62 };
63
64 static const char *widtype = NULL;
65 static void _del_hook(Evas_Object *obj);
66 static void _theme_hook(Evas_Object *obj);
67 static void _sizing_eval(Widget_Data *wd);
68 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
69 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
70 static void _part_cursor_free(Part_Cursor *pc);
71
72 static void
73 _del_hook(Evas_Object *obj)
74 {
75    Widget_Data *wd = elm_widget_data_get(obj);
76    Subinfo *si;
77    Part_Cursor *pc;
78
79    if (!wd) return;
80    EINA_LIST_FREE(wd->subs, si)
81      {
82         eina_stringshare_del(si->part);
83         if (si->type == TEXT)
84           eina_stringshare_del(si->p.text.text);
85         free(si);
86      }
87    EINA_LIST_FREE(wd->parts_cursors, pc) _part_cursor_free(pc);
88    free(wd);
89 }
90
91 static void
92 _theme_hook(Evas_Object *obj)
93 {
94    Widget_Data *wd = elm_widget_data_get(obj);
95    if (!wd) return;
96
97    _elm_theme_object_set(obj, wd->lay, wd->clas, wd->group, wd->style);
98    edje_object_scale_set(wd->lay, elm_widget_scale_get(obj) *
99                          _elm_config->scale);
100    _sizing_eval(wd);
101 }
102
103 static void
104 _changed_hook(Evas_Object *obj)
105 {
106    Widget_Data *wd = elm_widget_data_get(obj);
107    if (!wd) return;
108    if (wd->needs_size_calc)
109      {
110         _sizing_eval(wd);
111         wd->needs_size_calc = 0;
112      }
113 }
114
115 static void
116 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
117 {
118    Widget_Data *wd = elm_widget_data_get(obj);
119    edje_object_signal_emit(wd->lay, emission, source);
120 }
121
122 static void
123 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), void *data)
124 {
125    Widget_Data *wd = elm_widget_data_get(obj);
126    edje_object_signal_callback_add(wd->lay, emission, source, func_cb, data);
127 }
128
129 static void
130 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), void *data)
131 {
132    Widget_Data *wd = elm_widget_data_get(obj);
133    edje_object_signal_callback_del_full(wd->lay, emission, source, func_cb,
134                                         data);
135 }
136
137
138 static void *
139 _elm_layout_list_data_get(const Eina_List *list)
140 {
141    Subinfo *si = eina_list_data_get(list);
142    return si->obj;
143 }
144
145 static Eina_Bool
146 _elm_layout_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
147 {
148    Widget_Data *wd = elm_widget_data_get(obj);
149    const Eina_List *items;
150    void *(*list_data_get) (const Eina_List *list);
151
152    if ((!wd) || (!wd->subs))
153      return EINA_FALSE;
154
155    /* Focus chain (This block is diferent of elm_win cycle)*/
156    if ((items = elm_widget_focus_custom_chain_get(obj)))
157      list_data_get = eina_list_data_get;
158    else
159      {
160         items = wd->subs;
161         list_data_get = _elm_layout_list_data_get;
162
163         if (!items) return EINA_FALSE;
164      }
165
166    return elm_widget_focus_list_next_get(obj, items, list_data_get, dir,
167                                           next);
168 }
169
170 static void
171 _sizing_eval(Widget_Data *wd)
172 {
173    Evas_Coord minw = -1, minh = -1;
174    edje_object_size_min_calc(wd->lay, &minw, &minh);
175    evas_object_size_hint_min_set(wd->obj, minw, minh);
176    evas_object_size_hint_max_set(wd->obj, -1, -1);
177 }
178
179 static void
180 _request_sizing_eval(Widget_Data *wd)
181 {
182    if (wd->needs_size_calc) return;
183    wd->needs_size_calc = 1;
184    evas_object_smart_changed(wd->obj);
185 }
186
187 static void
188 _part_cursor_free(Part_Cursor *pc)
189 {
190    eina_stringshare_del(pc->part);
191    eina_stringshare_del(pc->style);
192    eina_stringshare_del(pc->cursor);
193    free(pc);
194 }
195
196 static void
197 _part_cursor_part_apply(const Part_Cursor *pc)
198 {
199    elm_object_cursor_set(pc->obj, pc->cursor);
200    elm_object_cursor_style_set(pc->obj, pc->style);
201    elm_object_cursor_engine_only_set(pc->obj, pc->engine_only);
202 }
203
204 static Part_Cursor *
205 _parts_cursors_find(Widget_Data *wd, const char *part)
206 {
207    const Eina_List *l;
208    Part_Cursor *pc;
209    EINA_LIST_FOREACH(wd->parts_cursors, l, pc)
210      {
211         if (!strcmp(pc->part, part))
212           return pc;
213      }
214    return NULL;
215 }
216
217 static void
218 _parts_cursors_apply(Widget_Data *wd)
219 {
220    const char *file, *group;
221    const Eina_List *l;
222    Part_Cursor *pc;
223
224    edje_object_file_get(wd->lay, &file, &group);
225
226    EINA_LIST_FOREACH(wd->parts_cursors, l, pc)
227      {
228         Evas_Object *obj = (Evas_Object *)edje_object_part_object_get
229           (wd->lay, pc->part);
230
231         if (!obj)
232           {
233              pc->obj = NULL;
234              WRN("no part '%s' in group '%s' of file '%s'. "
235                  "Cannot set cursor '%s'",
236                  pc->part, group, file, pc->cursor);
237              continue;
238           }
239         else if (evas_object_pass_events_get(obj))
240           {
241              pc->obj = NULL;
242              WRN("part '%s' in group '%s' of file '%s' has mouse_events: 0. "
243                  "Cannot set cursor '%s'",
244                  pc->part, group, file, pc->cursor);
245              continue;
246           }
247
248         pc->obj = obj;
249         _part_cursor_part_apply(pc);
250      }
251 }
252
253 static void
254 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
255 {
256    _request_sizing_eval(data);
257 }
258
259 static void
260 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
261 {
262    Widget_Data *wd = elm_widget_data_get(obj);
263    Evas_Object *sub = event_info;
264    Eina_List *l;
265    Subinfo *si;
266    if (!wd) return;
267    EINA_LIST_FOREACH(wd->subs, l, si)
268      {
269         if (si->obj == sub)
270           {
271              evas_object_event_callback_del_full(sub,
272                                             EVAS_CALLBACK_CHANGED_SIZE_HINTS,
273                                             _changed_size_hints,
274                                             wd);
275              wd->subs = eina_list_remove_list(wd->subs, l);
276              eina_stringshare_del(si->part);
277              free(si);
278              break;
279           }
280      }
281 }
282
283 static void
284 _signal_size_eval(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
285 {
286    _request_sizing_eval(data);
287 }
288
289 static void
290 _parts_text_fix(Widget_Data *wd)
291 {
292    const Eina_List *l;
293    Subinfo *si;
294
295    EINA_LIST_FOREACH(wd->subs, l, si)
296      {
297         if (si->type == TEXT)
298           edje_object_part_text_set(wd->lay, si->part, si->p.text.text);
299      }
300 }
301
302 /**
303  * Add a new layout to the parent
304  *
305  * @param parent The parent object
306  * @return The new object or NULL if it cannot be created
307  *
308  * @ingroup Layout
309  */
310 EAPI Evas_Object *
311 elm_layout_add(Evas_Object *parent)
312 {
313    Evas_Object *obj;
314    Evas *e;
315    Widget_Data *wd;
316
317    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
318
319    wd = ELM_NEW(Widget_Data);
320    e = evas_object_evas_get(parent);
321    if (!e) return NULL;
322    wd->obj = obj = elm_widget_add(e);
323    ELM_SET_WIDTYPE(widtype, "layout");
324    elm_widget_type_set(obj, "layout");
325    elm_widget_sub_object_add(parent, obj);
326    elm_widget_data_set(obj, wd);
327    elm_widget_del_hook_set(obj, _del_hook);
328    elm_widget_theme_hook_set(obj, _theme_hook);
329    elm_widget_changed_hook_set(obj, _changed_hook);
330    elm_widget_can_focus_set(obj, EINA_FALSE);
331    elm_widget_focus_next_hook_set(obj, _elm_layout_focus_next_hook);
332    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
333    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
334    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
335
336    wd->lay = edje_object_add(e);
337    elm_widget_resize_object_set(obj, wd->lay);
338    edje_object_signal_callback_add(wd->lay, "size,eval", "elm",
339                                    _signal_size_eval, wd);
340    
341    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
342
343    _request_sizing_eval(wd);
344    return obj;
345 }
346
347 /**
348  * Set the file that will be used as layout
349  *
350  * @param obj The layout object
351  * @param file The path to file (edj) that will be used as layout
352  * @param group The group that the layout belongs in edje file
353  *
354  * @return (1 = success, 0 = error)
355  *
356  * @ingroup Layout
357  */
358 EAPI Eina_Bool
359 elm_layout_file_set(Evas_Object *obj, const char *file, const char *group)
360 {
361    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
362    Widget_Data *wd = elm_widget_data_get(obj);
363    if (!wd) return EINA_FALSE;
364    Eina_Bool ret = edje_object_file_set(wd->lay, file, group);
365    if (ret)
366      {
367         _parts_text_fix(wd);
368         _request_sizing_eval(wd);
369         _parts_cursors_apply(wd);
370      }
371    else DBG("failed to set edje file '%s', group '%s': %s",
372             file, group,
373             edje_load_error_str(edje_object_load_error_get(wd->lay)));
374    return ret;
375 }
376
377 /**
378  * Set the edje group from the elementary theme that will be used as layout
379  *
380  * @param obj The layout object
381  * @param clas the clas of the group
382  * @param group the group
383  * @param style the style to used
384  *
385  * @return (1 = success, 0 = error)
386  *
387  * @ingroup Layout
388  */
389 EAPI Eina_Bool
390 elm_layout_theme_set(Evas_Object *obj, const char *clas, const char *group, const char *style)
391 {
392    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
393    Widget_Data *wd = elm_widget_data_get(obj);
394    if (!wd) return EINA_FALSE;
395    Eina_Bool ret = _elm_theme_object_set(obj, wd->lay, clas, group, style);
396    wd->clas = clas;
397    wd->group = group;
398    wd->style = style;
399    if (ret)
400      {
401         _parts_text_fix(wd);
402         _request_sizing_eval(wd);
403         _parts_cursors_apply(wd);
404      }
405    return ret;
406 }
407
408 /**
409  * Set the layout content
410  *
411  * Once the content object is set, a previously set one will be deleted.
412  * If you want to keep that old content object, use the
413  * elm_layout_content_unset() function.
414  *
415  * @param obj The layout object
416  * @param swallow The swallow group name in the edje file
417  * @param content The content will be filled in this layout object
418  *
419  * @ingroup Layout
420  */
421 EAPI void
422 elm_layout_content_set(Evas_Object *obj, const char *swallow, Evas_Object *content)
423 {
424    ELM_CHECK_WIDTYPE(obj, widtype);
425    Widget_Data *wd = elm_widget_data_get(obj);
426    Subinfo *si;
427    const Eina_List *l;
428    if (!wd) return;
429    EINA_LIST_FOREACH(wd->subs, l, si)
430      {
431         if ((si->type == SWALLOW) && (!strcmp(swallow, si->part)))
432           {
433              if (content == si->obj) return;
434              evas_object_del(si->obj);
435              break;
436           }
437      }
438    if (content)
439      {
440         elm_widget_sub_object_add(obj, content);
441         evas_object_event_callback_add(content,
442                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
443                                        _changed_size_hints, wd);
444         if (!edje_object_part_swallow(wd->lay, swallow, content))
445           WRN("could not swallow %p into part '%s'", content, swallow);
446         si = ELM_NEW(Subinfo);
447         si->type = SWALLOW;
448         si->part = eina_stringshare_add(swallow);
449         si->obj = content;
450         wd->subs = eina_list_append(wd->subs, si);
451      }
452    _request_sizing_eval(wd);
453 }
454
455 /**
456  * Get the swallowed object in the given part
457  *
458  * @param obj The layout object
459  * @param swallow The SWALLOW part to get its content
460  *
461  * @return The swallowed object or NULL if none or an error occurred
462  *
463  * @ingroup Layout
464  */
465 EAPI const Evas_Object *
466 elm_layout_content_get(const Evas_Object *obj, const char *swallow)
467 {
468    Widget_Data *wd = elm_widget_data_get(obj);
469    const Eina_List *l;
470    Subinfo *si;
471    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
472
473    EINA_LIST_FOREACH(wd->subs, l, si)
474      {
475         if ((si->type == SWALLOW) && !strcmp(swallow, si->part))
476           return si->obj;
477      }
478    return NULL;
479 }
480
481 /**
482  * Unset the layout content
483  *
484  * Unparent and return the content object which was set for this widget
485  *
486  * @param obj The layout object
487  * @param swallow The swallow group name in the edje file
488  * @return The content that was being used
489  *
490  * @ingroup Layout
491  */
492 EAPI Evas_Object *
493 elm_layout_content_unset(Evas_Object *obj, const char *swallow)
494 {
495    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
496    Widget_Data *wd = elm_widget_data_get(obj);
497    Subinfo *si;
498    const Eina_List *l;
499    if (!wd) return NULL;
500    EINA_LIST_FOREACH(wd->subs, l, si)
501      {
502         if ((si->type == SWALLOW) && (!strcmp(swallow, si->part)))
503           {
504              Evas_Object *content;
505              if (!si->obj) return NULL;
506              content = si->obj; /* si will die in _sub_del due elm_widget_sub_object_del() */
507              elm_widget_sub_object_del(obj, content);
508              edje_object_part_unswallow(wd->lay, content);
509              return content;
510           }
511      }
512    return NULL;
513 }
514
515 /**
516  * Set the text of the given part
517  *
518  * @param obj The layout object
519  * @param part The TEXT part where to set the text
520  * @param text The text to set
521  *
522  * @ingroup Layout
523  */
524 EAPI void
525 elm_layout_text_set(Evas_Object *obj, const char *part, const char *text)
526 {
527    Widget_Data *wd = elm_widget_data_get(obj);
528    Subinfo *si = NULL;
529    Eina_List *l;
530    ELM_CHECK_WIDTYPE(obj, widtype);
531
532    EINA_LIST_FOREACH(wd->subs, l, si)
533      {
534         if ((si->type == TEXT) && (!strcmp(part, si->part)))
535           {
536              if (!text)
537                {
538                   eina_stringshare_del(si->part);
539                   eina_stringshare_del(si->p.text.text);
540                   free(si);
541                   edje_object_part_text_set(wd->lay, part, NULL);
542                   wd->subs = eina_list_remove_list(wd->subs, l);
543                   return;
544                }
545              else
546                break;
547           }
548         si = NULL;
549      }
550
551    if (!si)
552      {
553         si = ELM_NEW(Subinfo);
554         if (!si) return;
555         si->type = TEXT;
556         si->part = eina_stringshare_add(part);
557         wd->subs = eina_list_append(wd->subs, si);
558      }
559
560    eina_stringshare_replace(&si->p.text.text, text);
561    edje_object_part_text_set(wd->lay, part, text);
562    _request_sizing_eval(wd);
563 }
564
565 /**
566  * Get the text set in the given part
567  *
568  * @param obj The layout object
569  * @param part The TEXT part to retrieve the text off
570  *
571  * @return The text set in @p part
572  *
573  * @ingroup Layout
574  */
575 EAPI const char *
576 elm_layout_text_get(const Evas_Object *obj, const char *part)
577 {
578    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
579    Widget_Data *wd = elm_widget_data_get(obj);
580    return edje_object_part_text_get(wd->lay, part);
581 }
582
583 /**
584  * Append child to layout box part.
585  *
586  * Once the object is appended, its lifetime will be bound to the
587  * layout, whenever the layout dies the child will be deleted
588  * automatically. One should use elm_layout_box_remove() to make this
589  * layout forget about the object.
590  *
591  * @param obj the layout object
592  * @param part the box part to append.
593  * @param child the child object to append to box.
594  *
595  * @ingroup Layout
596  */
597 EAPI void
598 elm_layout_box_append(Evas_Object *obj, const char *part, Evas_Object *child)
599 {
600    ELM_CHECK_WIDTYPE(obj, widtype);
601    Widget_Data *wd = elm_widget_data_get(obj);
602    Subinfo *si;
603    if (!wd) return;
604
605    if (!edje_object_part_box_append(wd->lay, part, child))
606      WRN("child %p could not be appended to box part '%s'", child, part);
607    elm_widget_sub_object_add(obj, child);
608    evas_object_event_callback_add
609      (child, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, wd);
610
611    si = ELM_NEW(Subinfo);
612    si->type = BOX_APPEND;
613    si->part = eina_stringshare_add(part);
614    si->obj = child;
615    wd->subs = eina_list_append(wd->subs, si);
616    _request_sizing_eval(wd);
617 }
618
619 /**
620  * Prepend child to layout box part.
621  *
622  * Once the object is prepended, its lifetime will be bound to the
623  * layout, whenever the layout dies the child will be deleted
624  * automatically. One should use elm_layout_box_remove() to make this
625  * layout forget about the object.
626  *
627  * @param obj the layout object
628  * @param part the box part to prepend.
629  * @param child the child object to prepend to box.
630  *
631  * @ingroup Layout
632  */
633 EAPI void
634 elm_layout_box_prepend(Evas_Object *obj, const char *part, Evas_Object *child)
635 {
636    ELM_CHECK_WIDTYPE(obj, widtype);
637    Widget_Data *wd = elm_widget_data_get(obj);
638    Subinfo *si;
639    if (!wd) return;
640
641    if (!edje_object_part_box_prepend(wd->lay, part, child))
642      WRN("child %p could not be prepended to box part '%s'", child, part);
643    elm_widget_sub_object_add(obj, child);
644    evas_object_event_callback_add
645      (child, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, wd);
646
647    si = ELM_NEW(Subinfo);
648    si->type = BOX_PREPEND;
649    si->part = eina_stringshare_add(part);
650    si->obj = child;
651    wd->subs = eina_list_prepend(wd->subs, si);
652    _request_sizing_eval(wd);
653 }
654
655 static void
656 _box_reference_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
657 {
658    Subinfo *si = data;
659    si->p.box.reference = NULL;
660 }
661
662 /**
663  * Insert child to layout box part before a reference object.
664  *
665  * Once the object is inserted, its lifetime will be bound to the
666  * layout, whenever the layout dies the child will be deleted
667  * automatically. One should use elm_layout_box_remove() to make this
668  * layout forget about the object.
669  *
670  * @param obj the layout object
671  * @param part the box part to insert.
672  * @param child the child object to insert into box.
673  * @param reference another reference object to insert before in box.
674  *
675  * @ingroup Layout
676  */
677 EAPI void
678 elm_layout_box_insert_before(Evas_Object *obj, const char *part, Evas_Object *child, const Evas_Object *reference)
679 {
680    ELM_CHECK_WIDTYPE(obj, widtype);
681    Widget_Data *wd = elm_widget_data_get(obj);
682    Subinfo *si;
683    if (!wd) return;
684
685    if (!edje_object_part_box_insert_before(wd->lay, part, child, reference))
686      WRN("child %p could not be inserted before %p inf box part '%s'",
687          child, reference, part);
688
689    si = ELM_NEW(Subinfo);
690    si->type = BOX_INSERT_BEFORE;
691    si->part = eina_stringshare_add(part);
692    si->obj = child;
693    si->p.box.reference = reference;
694
695    elm_widget_sub_object_add(obj, child);
696    evas_object_event_callback_add
697      (child, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, wd);
698    evas_object_event_callback_add
699      ((Evas_Object *)reference, EVAS_CALLBACK_DEL, _box_reference_del, si);
700
701    wd->subs = eina_list_append(wd->subs, si);
702    _request_sizing_eval(wd);
703 }
704
705 /**
706  * Insert child to layout box part at a given position.
707  *
708  * Once the object is inserted, its lifetime will be bound to the
709  * layout, whenever the layout dies the child will be deleted
710  * automatically. One should use elm_layout_box_remove() to make this
711  * layout forget about the object.
712  *
713  * @param obj the layout object
714  * @param part the box part to insert.
715  * @param child the child object to insert into box.
716  * @param pos the numeric position >=0 to insert the child.
717  *
718  * @ingroup Layout
719  */
720 EAPI void
721 elm_layout_box_insert_at(Evas_Object *obj, const char *part, Evas_Object *child, unsigned int pos)
722 {
723    ELM_CHECK_WIDTYPE(obj, widtype);
724    Widget_Data *wd = elm_widget_data_get(obj);
725    Subinfo *si;
726    if (!wd) return;
727
728    if (!edje_object_part_box_insert_at(wd->lay, part, child, pos))
729      WRN("child %p could not be inserted at %u to box part '%s'",
730          child, pos, part);
731
732    elm_widget_sub_object_add(obj, child);
733    evas_object_event_callback_add
734      (child, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, wd);
735
736    si = ELM_NEW(Subinfo);
737    si->type = BOX_INSERT_AT;
738    si->part = eina_stringshare_add(part);
739    si->obj = child;
740    si->p.box.pos = pos;
741    wd->subs = eina_list_append(wd->subs, si);
742    _request_sizing_eval(wd);
743 }
744
745 static Evas_Object *
746 _sub_box_remove(Widget_Data *wd, Subinfo *si)
747 {
748    Evas_Object *child;
749
750    if (si->type == BOX_INSERT_BEFORE)
751      evas_object_event_callback_del_full
752        ((Evas_Object *)si->p.box.reference,
753         EVAS_CALLBACK_DEL, _box_reference_del, si);
754
755    child = si->obj; /* si will die in _sub_del due elm_widget_sub_object_del() */
756    edje_object_part_box_remove(wd->lay, si->part, child);
757    elm_widget_sub_object_del(wd->obj, child);
758    return child;
759 }
760
761 static Evas_Object *
762 _sub_table_remove(Widget_Data *wd, Subinfo *si)
763 {
764    Evas_Object *child;
765
766    child = si->obj; /* si will die in _sub_del due elm_widget_sub_object_del() */
767    edje_object_part_table_unpack(wd->lay, si->part, child);
768    elm_widget_sub_object_del(wd->obj, child);
769    return child;
770 }
771
772 static Eina_Bool
773 _sub_box_is(const Subinfo *si)
774 {
775    switch (si->type)
776      {
777       case BOX_APPEND:
778       case BOX_PREPEND:
779       case BOX_INSERT_BEFORE:
780       case BOX_INSERT_AT:
781          return EINA_TRUE;
782       default:
783          return EINA_FALSE;
784      }
785 }
786
787 /**
788  * Remove a child of the given part box.
789  *
790  * The object will be removed from the box part and its lifetime will
791  * not be handled by the layout anymore. This is equivalent to
792  * elm_layout_content_unset() for box.
793  *
794  * @param obj The layout object
795  * @param part The box part name to remove child.
796  * @param child The object to remove from box.
797  * @return The object that was being used, or NULL if not found.
798  *
799  * @ingroup Layout
800  */
801 EAPI Evas_Object *
802 elm_layout_box_remove(Evas_Object *obj, const char *part, Evas_Object *child)
803 {
804    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
805    Widget_Data *wd = elm_widget_data_get(obj);
806    const Eina_List *l;
807    Subinfo *si;
808
809    if (!wd) return NULL;
810
811    EINA_SAFETY_ON_NULL_RETURN_VAL(part, NULL);
812    EINA_SAFETY_ON_NULL_RETURN_VAL(child, NULL);
813    EINA_LIST_FOREACH(wd->subs, l, si)
814      {
815         if (!_sub_box_is(si)) continue;
816         if ((si->obj == child) && (!strcmp(si->part, part)))
817           return _sub_box_remove(wd, si);
818      }
819    return NULL;
820 }
821
822 /**
823  * Remove all child of the given part box.
824  *
825  * The objects will be removed from the box part and their lifetime will
826  * not be handled by the layout anymore. This is equivalent to
827  * elm_layout_content_unset() for all box children.
828  *
829  * @param obj The layout object
830  * @param part The box part name to remove child.
831  * @param clear If EINA_TRUE, then all objects will be deleted as
832  *        well, otherwise they will just be removed and will be
833  *        dangling on the canvas.
834  *
835  * @ingroup Layout
836  */
837 EAPI void
838 elm_layout_box_remove_all(Evas_Object *obj, const char *part, Eina_Bool clear)
839 {
840    ELM_CHECK_WIDTYPE(obj, widtype);
841    Widget_Data *wd = elm_widget_data_get(obj);
842    Subinfo *si;
843    Eina_List *lst;
844
845    if (!wd) return;
846    EINA_SAFETY_ON_NULL_RETURN(part);
847
848    lst = eina_list_clone(wd->subs);
849    EINA_LIST_FREE(lst, si)
850      {
851         if (!_sub_box_is(si)) continue;
852         if (!strcmp(si->part, part))
853           {
854              Evas_Object *child = _sub_box_remove(wd, si);
855              if ((clear) && (child)) evas_object_del(child);
856           }
857      }
858    /* eventually something may not be added with layout, del them as well */
859    edje_object_part_box_remove_all(wd->lay, part, clear);
860 }
861
862 /**
863  * Insert child to layout table part.
864  *
865  * Once the object is inserted, its lifetime will be bound to the
866  * layout, whenever the layout dies the child will be deleted
867  * automatically. One should use elm_layout_box_remove() to make this
868  * layout forget about the object.
869  *
870  * @param obj the layout object
871  * @param part the box part to pack child.
872  * @param child the child object to pack into table.
873  * @param reference another reference object to insert before in box.
874  *
875  * @ingroup Layout
876  */
877 EAPI void
878 elm_layout_table_pack(Evas_Object *obj, const char *part, Evas_Object *child, unsigned short col, unsigned short row, unsigned short colspan, unsigned short rowspan)
879 {
880    ELM_CHECK_WIDTYPE(obj, widtype);
881    Widget_Data *wd = elm_widget_data_get(obj);
882    Subinfo *si;
883    if (!wd) return;
884
885    if (!edje_object_part_table_pack
886        (wd->lay, part, child, col, row, colspan, rowspan))
887      WRN("child %p could not be packed into box part '%s' col=%uh, row=%hu, "
888          "colspan=%hu, rowspan=%hu", child, part, col, row, colspan, rowspan);
889
890    elm_widget_sub_object_add(obj, child);
891    evas_object_event_callback_add
892      (child, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, wd);
893
894    si = ELM_NEW(Subinfo);
895    si->type = TABLE_PACK;
896    si->part = eina_stringshare_add(part);
897    si->obj = child;
898    si->p.table.col = col;
899    si->p.table.row = row;
900    si->p.table.colspan = colspan;
901    si->p.table.rowspan = rowspan;
902    wd->subs = eina_list_append(wd->subs, si);
903    _request_sizing_eval(wd);
904 }
905
906 /**
907  * Unpack (remove) a child of the given part table.
908  *
909  * The object will be unpacked from the table part and its lifetime
910  * will not be handled by the layout anymore. This is equivalent to
911  * elm_layout_content_unset() for table.
912  *
913  * @param obj The layout object
914  * @param part The table part name to remove child.
915  * @param child The object to remove from table.
916  * @return The object that was being used, or NULL if not found.
917  *
918  * @ingroup Layout
919  */
920 EAPI Evas_Object *
921 elm_layout_table_unpack(Evas_Object *obj, const char *part, Evas_Object *child)
922 {
923    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
924    Widget_Data *wd = elm_widget_data_get(obj);
925    const Eina_List *l;
926    Subinfo *si;
927
928    if (!wd) return NULL;
929
930    EINA_SAFETY_ON_NULL_RETURN_VAL(part, NULL);
931    EINA_SAFETY_ON_NULL_RETURN_VAL(child, NULL);
932    EINA_LIST_FOREACH(wd->subs, l, si)
933      {
934         if (si->type != TABLE_PACK) continue;
935         if ((si->obj == child) && (!strcmp(si->part, part)))
936           return _sub_table_remove(wd, si);
937      }
938    return NULL;
939 }
940
941 /**
942  * Remove all child of the given part table.
943  *
944  * The objects will be removed from the table part and their lifetime will
945  * not be handled by the layout anymore. This is equivalent to
946  * elm_layout_content_unset() for all table children.
947  *
948  * @param obj The layout object
949  * @param part The table part name to remove child.
950  * @param clear If EINA_TRUE, then all objects will be deleted as
951  *        well, otherwise they will just be removed and will be
952  *        dangling on the canvas.
953  *
954  * @ingroup Layout
955  */
956 EAPI void
957 elm_layout_table_clear(Evas_Object *obj, const char *part, Eina_Bool clear)
958 {
959    ELM_CHECK_WIDTYPE(obj, widtype);
960    Widget_Data *wd = elm_widget_data_get(obj);
961    Subinfo *si;
962    Eina_List *lst;
963
964    if (!wd) return;
965    EINA_SAFETY_ON_NULL_RETURN(part);
966
967    lst = eina_list_clone(wd->subs);
968    EINA_LIST_FREE(lst, si)
969      {
970         if (si->type != TABLE_PACK) continue;
971         if (!strcmp(si->part, part))
972           {
973              Evas_Object *child = _sub_table_remove(wd, si);
974              if ((clear) && (child)) evas_object_del(child);
975           }
976      }
977    /* eventually something may not be added with layout, del them as well */
978    edje_object_part_table_clear(wd->lay, part, clear);
979 }
980
981 /**
982  * Get the edje layout
983  *
984  * @param obj The layout object
985  * 
986  * This returns the edje object. It is not expected to be used to then swallow
987  * objects via edje_object_part_swallow() for example. Use 
988  * elm_layout_content_set() instead so child object handling and sizing is
989  * done properly. This is more intended for setting text, emitting signals,
990  * hooking to signal callbacks etc.
991  *
992  * @return A Evas_Object with the edje layout settings loaded
993  * with function elm_layout_file_set
994  *
995  * @ingroup Layout
996  */
997 EAPI Evas_Object *
998 elm_layout_edje_get(const Evas_Object *obj)
999 {
1000    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1001    Widget_Data *wd = elm_widget_data_get(obj);
1002    if (!wd) return NULL;
1003    return wd->lay;
1004 }
1005
1006 /**
1007  * Eval sizing
1008  * 
1009  * Manually forms a sizing re-evaluation when contents changed state so that
1010  * minimum size might have changed and needs re-evaluation. Also note that
1011  * a standard signal of "size,eval" "elm" emitted by the edje object will
1012  * cause this to happen too
1013  *
1014  * @param obj The layout object
1015  *
1016  * @ingroup Layout
1017  */
1018 EAPI void
1019 elm_layout_sizing_eval(Evas_Object *obj)
1020 {
1021    ELM_CHECK_WIDTYPE(obj, widtype);
1022    Widget_Data *wd = elm_widget_data_get(obj);
1023    EINA_SAFETY_ON_NULL_RETURN(wd);
1024    _request_sizing_eval(wd);
1025 }
1026
1027 /**
1028  * Sets a specific cursor for an edje part.
1029  *
1030  * @param obj The layout object.
1031  * @param part_name a part from loaded edje group.
1032  * @param cursor cursor name to use, see Elementary_Cursor.h
1033  *
1034  * @return EINA_TRUE on success or EINA_FALSE on failure, that may be
1035  *         part not exists or it has "mouse_events: 0".
1036  *
1037  * @ingroup Layout
1038  */
1039 EAPI Eina_Bool
1040 elm_layout_part_cursor_set(Evas_Object *obj, const char *part_name, const char *cursor)
1041 {
1042    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1043    EINA_SAFETY_ON_NULL_RETURN_VAL(part_name, EINA_FALSE);
1044    Widget_Data *wd = elm_widget_data_get(obj);
1045    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, EINA_FALSE);
1046    Evas_Object *part_obj;
1047    Part_Cursor *pc;
1048
1049    part_obj = (Evas_Object *)edje_object_part_object_get(wd->lay, part_name);
1050    if (!part_obj)
1051      {
1052         const char *group, *file;
1053         edje_object_file_get(wd->lay, &file, &group);
1054         WRN("no part '%s' in group '%s' of file '%s'. Cannot set cursor '%s'",
1055             part_name, group, file, cursor);
1056         return EINA_FALSE;
1057      }
1058    if (evas_object_pass_events_get(part_obj))
1059      {
1060         const char *group, *file;
1061         edje_object_file_get(wd->lay, &file, &group);
1062         WRN("part '%s' in group '%s' of file '%s' has mouse_events: 0. "
1063             "Cannot set cursor '%s'",
1064             part_name, group, file, cursor);
1065         return EINA_FALSE;
1066      }
1067
1068    pc = _parts_cursors_find(wd, part_name);
1069    if (pc) eina_stringshare_replace(&pc->cursor, cursor);
1070    else
1071      {
1072         pc = calloc(1, sizeof(*pc));
1073         pc->part = eina_stringshare_add(part_name);
1074         pc->cursor = eina_stringshare_add(cursor);
1075      }
1076
1077    pc->obj = part_obj;
1078    elm_object_sub_cursor_set(part_obj, obj, pc->cursor);
1079    return EINA_TRUE;
1080 }
1081
1082 /**
1083  * Get the cursor to be shown when mouse is over an edje part
1084  *
1085  * @param obj The layout object.
1086  * @param part_name a part from loaded edje group.
1087  * @return the cursor name.
1088  *
1089  * @ingroup Layout
1090  */
1091 EAPI const char *
1092 elm_layout_part_cursor_get(const Evas_Object *obj, const char *part_name)
1093 {
1094    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1095    EINA_SAFETY_ON_NULL_RETURN_VAL(part_name, NULL);
1096    Widget_Data *wd = elm_widget_data_get(obj);
1097    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, NULL);
1098    Part_Cursor *pc = _parts_cursors_find(wd, part_name);
1099    EINA_SAFETY_ON_NULL_RETURN_VAL(pc, NULL);
1100    EINA_SAFETY_ON_NULL_RETURN_VAL(pc->obj, NULL);
1101    return elm_object_cursor_get(pc->obj);
1102 }
1103
1104 /**
1105  * Unsets a cursor previously set with elm_layout_part_cursor_set().
1106  *
1107  * @param obj The layout object.
1108  * @param part_name a part from loaded edje group, that had a cursor set
1109  *        with elm_layout_part_cursor_set().
1110  *
1111  * @ingroup Layout
1112  */
1113 EAPI void
1114 elm_layout_part_cursor_unset(Evas_Object *obj, const char *part_name)
1115 {
1116    ELM_CHECK_WIDTYPE(obj, widtype);
1117    EINA_SAFETY_ON_NULL_RETURN(part_name);
1118    Widget_Data *wd = elm_widget_data_get(obj);
1119    EINA_SAFETY_ON_NULL_RETURN(wd);
1120    Eina_List *l;
1121    Part_Cursor *pc;
1122
1123    EINA_LIST_FOREACH(wd->parts_cursors, l, pc)
1124      {
1125         if (!strcmp(part_name, pc->part))
1126           {
1127              if (pc->obj) elm_object_cursor_unset(pc->obj);
1128              _part_cursor_free(pc);
1129              wd->parts_cursors = eina_list_remove_list(wd->parts_cursors, l);
1130              return;
1131           }
1132      }
1133 }
1134
1135 /**
1136  * Sets a specific cursor style for an edje part.
1137  *
1138  * @param obj The layout object.
1139  * @param part_name a part from loaded edje group.
1140  * @param style the theme style to use (default, transparent, ...)
1141  *
1142  * @return EINA_TRUE on success or EINA_FALSE on failure, that may be
1143  *         part not exists or it did not had a cursor set.
1144  *
1145  * @ingroup Layout
1146  */
1147 EAPI Eina_Bool
1148 elm_layout_part_cursor_style_set(Evas_Object *obj, const char *part_name, const char *style)
1149 {
1150    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1151    EINA_SAFETY_ON_NULL_RETURN_VAL(part_name, EINA_FALSE);
1152    Widget_Data *wd = elm_widget_data_get(obj);
1153    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, EINA_FALSE);
1154    Part_Cursor *pc = _parts_cursors_find(wd, part_name);
1155    EINA_SAFETY_ON_NULL_RETURN_VAL(pc, EINA_FALSE);
1156    EINA_SAFETY_ON_NULL_RETURN_VAL(pc->obj, EINA_FALSE);
1157
1158    eina_stringshare_replace(&pc->style, style);
1159    elm_object_cursor_style_set(pc->obj, pc->style);
1160    return EINA_TRUE;
1161 }
1162
1163 /**
1164  * Gets a specific cursor style for an edje part.
1165  *
1166  * @param obj The layout object.
1167  * @param part_name a part from loaded edje group.
1168  *
1169  * @return the theme style in use, defaults to "default". If the
1170  *         object does not have a cursor set, then NULL is returned.
1171  *
1172  * @ingroup Layout
1173  */
1174 EAPI const char *
1175 elm_layout_part_cursor_style_get(const Evas_Object *obj, const char *part_name)
1176 {
1177    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1178    EINA_SAFETY_ON_NULL_RETURN_VAL(part_name, NULL);
1179    Widget_Data *wd = elm_widget_data_get(obj);
1180    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, NULL);
1181    Part_Cursor *pc = _parts_cursors_find(wd, part_name);
1182    EINA_SAFETY_ON_NULL_RETURN_VAL(pc, NULL);
1183    EINA_SAFETY_ON_NULL_RETURN_VAL(pc->obj, NULL);
1184    return elm_object_cursor_style_get(pc->obj);
1185 }
1186
1187 /**
1188  * Sets if the cursor set should be searched on the theme or should use
1189  * the provided by the engine, only.
1190  *
1191  * @note before you set if should look on theme you should define a
1192  * cursor with elm_layout_part_cursor_set(). By default it will only
1193  * look for cursors provided by the engine.
1194  *
1195  * @param obj The layout object.
1196  * @param part_name a part from loaded edje group.
1197  * @param engine_only if cursors should be just provided by the engine
1198  *        or should also search on widget's theme as well
1199  *
1200  * @return EINA_TRUE on success or EINA_FALSE on failure, that may be
1201  *         part not exists or it did not had a cursor set.
1202  *
1203  * @ingroup Layout
1204  */
1205 EAPI Eina_Bool
1206 elm_layout_part_cursor_engine_only_set(Evas_Object *obj, const char *part_name, Eina_Bool engine_only)
1207 {
1208    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1209    EINA_SAFETY_ON_NULL_RETURN_VAL(part_name, EINA_FALSE);
1210    Widget_Data *wd = elm_widget_data_get(obj);
1211    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, EINA_FALSE);
1212    Part_Cursor *pc = _parts_cursors_find(wd, part_name);
1213    EINA_SAFETY_ON_NULL_RETURN_VAL(pc, EINA_FALSE);
1214    EINA_SAFETY_ON_NULL_RETURN_VAL(pc->obj, EINA_FALSE);
1215
1216    pc->engine_only = !!engine_only;
1217    elm_object_cursor_engine_only_set(pc->obj, pc->engine_only);
1218    return EINA_TRUE;
1219 }
1220
1221 /**
1222  * Gets a specific cursor engine_only for an edje part.
1223  *
1224  * @param obj The layout object.
1225  * @param part_name a part from loaded edje group.
1226  *
1227  * @return whenever the cursor is just provided by engine or also from theme.
1228  *
1229  * @ingroup Layout
1230  */
1231 EAPI Eina_Bool
1232 elm_layout_part_cursor_engine_only_get(const Evas_Object *obj, const char *part_name)
1233 {
1234    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1235    EINA_SAFETY_ON_NULL_RETURN_VAL(part_name, EINA_FALSE);
1236    Widget_Data *wd = elm_widget_data_get(obj);
1237    EINA_SAFETY_ON_NULL_RETURN_VAL(wd, EINA_FALSE);
1238    Part_Cursor *pc = _parts_cursors_find(wd, part_name);
1239    EINA_SAFETY_ON_NULL_RETURN_VAL(pc, EINA_FALSE);
1240    EINA_SAFETY_ON_NULL_RETURN_VAL(pc->obj, EINA_FALSE);
1241    return elm_object_cursor_engine_only_get(pc->obj);
1242 }