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