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