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