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