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