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