cde787e17933fab59e0c24b55d20235f5f455040
[framework/uifw/elementary.git] / src / lib / elm_widget.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 static const char SMART_NAME[] = "elm_widget";
5
6 #define API_ENTRY \
7    Smart_Data *sd = evas_object_smart_data_get(obj); \
8    if ((!obj) || (!sd) || (!_elm_widget_is(obj)))
9 #define INTERNAL_ENTRY \
10    Smart_Data *sd = evas_object_smart_data_get(obj); \
11    if (!sd) return;
12
13 typedef struct _Smart_Data Smart_Data;
14 typedef struct _Edje_Signal_Data Edje_Signal_Data;
15 typedef struct _Elm_Event_Cb_Data Elm_Event_Cb_Data;
16
17 struct _Smart_Data
18 {
19    Evas_Object   *obj;
20    const char    *type;
21    Evas_Object   *parent_obj;
22    Evas_Coord     x, y, w, h;
23    Eina_List     *subobjs;
24    Evas_Object   *resize_obj;
25    Evas_Object   *hover_obj;
26    Eina_List     *tooltips, *cursors;
27    void         (*del_func) (Evas_Object *obj);
28    void         (*del_pre_func) (Evas_Object *obj);
29    void         (*focus_func) (Evas_Object *obj);
30    void         (*activate_func) (Evas_Object *obj);
31    void         (*disable_func) (Evas_Object *obj);
32    void         (*theme_func) (Evas_Object *obj);
33    Eina_Bool    (*event_func) (Evas_Object *obj, Evas_Object *source, Evas_Callback_Type type, void *event_info);
34    void         (*signal_func) (Evas_Object *obj, const char *emission,
35                                 const char *source);
36    void         (*callback_add_func) (Evas_Object *obj, const char *emission,
37                                 const char *source, void (*func) (void *data,
38                                    Evas_Object *o, const char *emission,
39                                    const char *source), void *data);
40    void         (*callback_del_func) (Evas_Object *obj, const char *emission,
41                                   const char *source, void (*func) (void *data,
42                                      Evas_Object *o, const char *emission,
43                                      const char *source), void *data);
44    void         (*changed_func) (Evas_Object *obj);
45    Eina_Bool    (*focus_next_func) (const Evas_Object *obj, Elm_Focus_Direction dir,
46                                     Evas_Object **next);
47    void         (*on_focus_func) (void *data, Evas_Object *obj);
48    void          *on_focus_data;
49    void         (*on_change_func) (void *data, Evas_Object *obj);
50    void          *on_change_data;
51    void         (*on_show_region_func) (void *data, Evas_Object *obj);
52    void          *on_show_region_data;
53    void         (*focus_region_func) (Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h);
54    void         (*on_focus_region_func) (const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h);
55    void          *data;
56    Evas_Coord     rx, ry, rw, rh;
57    int            scroll_hold;
58    int            scroll_freeze;
59    double         scale;
60    Elm_Theme     *theme;
61    const char    *style;
62    unsigned int   focus_order;
63    Eina_Bool      focus_order_on_calc;
64    
65    int            child_drag_x_locked;
66    int            child_drag_y_locked;
67
68    Eina_List     *edje_signals;
69
70    Eina_Bool      drag_x_locked : 1;
71    Eina_Bool      drag_y_locked : 1;
72    
73    Eina_Bool      can_focus : 1;
74    Eina_Bool      child_can_focus : 1;
75    Eina_Bool      focused : 1;
76    Eina_Bool      highlight_ignore : 1;
77    Eina_Bool      highlight_in_theme : 1;
78    Eina_Bool      disabled : 1;
79
80    Eina_List     *focus_chain;
81    Eina_List     *event_cb;
82 };
83
84 struct _Edje_Signal_Data
85 {
86    Evas_Object *obj;
87    Edje_Signal_Cb func;
88    const char *emission;
89    const char *source;
90    void *data;
91 };
92
93 struct _Elm_Event_Cb_Data {
94      Elm_Event_Cb func;
95      const void *data;
96 };
97
98 /* local subsystem functions */
99 static void _smart_reconfigure(Smart_Data *sd);
100 static void _smart_add(Evas_Object *obj);
101 static void _smart_del(Evas_Object *obj);
102 static void _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
103 static void _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
104 static void _smart_show(Evas_Object *obj);
105 static void _smart_hide(Evas_Object *obj);
106 static void _smart_color_set(Evas_Object *obj, int r, int g, int b, int a);
107 static void _smart_clip_set(Evas_Object *obj, Evas_Object * clip);
108 static void _smart_clip_unset(Evas_Object *obj);
109 static void _smart_calculate(Evas_Object *obj);
110 static void _smart_init(void);
111
112 static void _if_focused_revert(Evas_Object *obj, Eina_Bool can_focus_only);
113 static Evas_Object *_newest_focus_order_get(Evas_Object *obj, unsigned int *newest_focus_order, Eina_Bool can_focus_only);
114
115 /* local subsystem globals */
116 static Evas_Smart *_e_smart = NULL;
117 static Eina_List  *widtypes = NULL;
118
119 static unsigned int focus_order = 0;
120
121 // internal funcs
122 static inline Eina_Bool
123 _elm_widget_is(const Evas_Object *obj)
124 {
125    const char *type = evas_object_type_get(obj);
126    return type == SMART_NAME;
127 }
128
129 static inline Eina_Bool
130 _is_focusable(Evas_Object *obj)
131 {
132    API_ENTRY return EINA_FALSE;
133    return sd->can_focus || (sd->child_can_focus);
134 }
135
136 static void
137 _unfocus_parents(Evas_Object *obj)
138 {
139    for (; obj; obj = elm_widget_parent_get(obj))
140      {
141         Smart_Data *sd = evas_object_smart_data_get(obj);
142         if (!sd) return;
143         if (!sd->focused) return;
144         sd->focused = 0;
145      }
146 }
147
148 static void
149 _focus_parents(Evas_Object *obj)
150 {
151    for (; obj; obj = elm_widget_parent_get(obj))
152      {
153         Smart_Data *sd = evas_object_smart_data_get(obj);
154         if (!sd) return;
155         if (sd->focused) return;
156         sd->focused = 1;
157      }
158 }
159
160 static void
161 _sub_obj_del(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
162 {
163    Smart_Data *sd = data;
164
165    if (_elm_widget_is(obj))
166      {
167         if (elm_widget_focus_get(obj)) _unfocus_parents(sd->obj);
168      }
169    if (obj == sd->resize_obj)
170      sd->resize_obj = NULL;
171    else if (obj == sd->hover_obj)
172      sd->hover_obj = NULL;
173    else
174      sd->subobjs = eina_list_remove(sd->subobjs, obj);
175    evas_object_smart_callback_call(sd->obj, "sub-object-del", obj);
176 }
177
178 static void
179 _sub_obj_hide(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
180 {
181    _if_focused_revert(obj, EINA_TRUE);
182 }
183
184 static void
185 _sub_obj_mouse_down(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
186 {
187    Evas_Object *o = obj;
188    do 
189      {
190         if (_elm_widget_is(o)) break;
191         o = evas_object_smart_parent_get(o);
192      }
193    while (o);
194    if (!o) return;
195    if (!_is_focusable(o)) return;
196    elm_widget_focus_steal(o);
197 }
198
199 static void
200 _propagate_x_drag_lock(Evas_Object *obj, int dir)
201 {
202    Smart_Data *sd = evas_object_smart_data_get(obj);
203    if (sd->parent_obj)
204      {
205         Smart_Data *sd2 = evas_object_smart_data_get(sd->parent_obj);
206         if (sd2)
207           {
208              sd2->child_drag_x_locked += dir;
209              _propagate_x_drag_lock(sd->parent_obj, dir);
210           }
211      }
212 }
213
214 static void
215 _propagate_y_drag_lock(Evas_Object *obj, int dir)
216 {
217    Smart_Data *sd = evas_object_smart_data_get(obj);
218    if (sd->parent_obj)
219      {
220         Smart_Data *sd2 = evas_object_smart_data_get(sd->parent_obj);
221         if (sd2)
222           {
223              sd2->child_drag_y_locked += dir;
224              _propagate_y_drag_lock(sd->parent_obj, dir);
225           }
226      }
227 }
228
229 static void
230 _propagate_event(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info)
231 {
232    INTERNAL_ENTRY;
233    Evas_Callback_Type type = (Evas_Callback_Type)(long) data;
234    Evas_Event_Flags *event_flags = NULL;
235
236    switch (type)
237      {
238      case EVAS_CALLBACK_KEY_DOWN:
239           {
240              Evas_Event_Key_Down *ev = event_info;
241              event_flags = &(ev->event_flags);
242              break;
243           }
244      case EVAS_CALLBACK_KEY_UP:
245           {
246              Evas_Event_Key_Up *ev = event_info;
247              event_flags = &(ev->event_flags);
248              break;
249           }
250      case EVAS_CALLBACK_MOUSE_WHEEL:
251           {
252              Evas_Event_Mouse_Wheel *ev = event_info;
253              event_flags = &(ev->event_flags);
254              break;
255           }
256      default:
257         break;
258      }
259
260    elm_widget_event_propagate(obj, type, event_info, event_flags);
261 }
262
263 static void
264 _parent_focus(Evas_Object *obj)
265 {
266    API_ENTRY return;
267
268    Evas_Object *o = elm_widget_parent_get(obj);
269    sd->focus_order_on_calc = EINA_TRUE;
270
271    if (sd->focused) return;
272    if (o)
273      {
274         unsigned int i = 0;
275         Evas_Object *ret;
276
277         ret = _newest_focus_order_get(o, &i, EINA_TRUE);
278
279         /* we don't want to bump a common widget ancestor's
280            focus_order *twice* while parent focusing */
281         if (!ret || (!i) || (i != focus_order))
282           _parent_focus(o);
283      }
284
285    if (!sd->focus_order_on_calc)
286      return; /* we don't want to override it if by means of any of the
287                 callbacks below one gets to calculate our order
288                 first. */
289
290    focus_order++;
291    sd->focus_order = focus_order;
292    sd->focused = EINA_TRUE;
293    if (sd->on_focus_func) sd->on_focus_func(sd->on_focus_data, obj);
294    if (sd->focus_func) sd->focus_func(obj);
295
296    if (sd->can_focus) _elm_widget_focus_region_show(obj);
297
298    sd->focus_order_on_calc = EINA_FALSE;
299 }
300
301 static void
302 _elm_object_focus_chain_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
303 {
304    Smart_Data *sd = data;
305
306    sd->focus_chain = eina_list_remove(sd->focus_chain, obj);
307 }
308
309 // exposed util funcs to elm
310 void
311 _elm_widget_type_clear(void)
312 {
313    const char **ptr;
314    
315    EINA_LIST_FREE(widtypes, ptr)
316      {
317         eina_stringshare_del(*ptr);
318         *ptr = NULL;
319      }
320 }
321
322 void
323 _elm_widget_focus_region_show(const Evas_Object *obj)
324 {
325    Evas_Coord x, y, w, h, ox, oy;
326    Smart_Data *sd2;
327    Evas_Object *o;
328
329    API_ENTRY return;
330
331    o = elm_widget_parent_get(obj);
332    if (!o) return;
333
334    elm_widget_focus_region_get(obj, &x, &y, &w, &h);
335    evas_object_geometry_get(obj, &ox, &oy, NULL, NULL);
336    while (o)
337      {
338         Evas_Coord px, py;
339         sd2 = evas_object_smart_data_get(o);
340         if (sd2->focus_region_func)
341           {
342              sd2->focus_region_func(o, x, y, w, h);
343              elm_widget_focus_region_get(o, &x, &y, &w, &h);
344           }
345         else
346           {
347              evas_object_geometry_get(o, &px, &py, NULL, NULL);
348              x += ox - px;
349              y += oy - py;
350              ox = px;
351              oy = py;
352           }
353         o = elm_widget_parent_get(o);
354      }
355 }
356
357 /**
358  * @defgroup Widget Widget
359  *
360  * @internal
361  * Exposed api for making widgets
362  */
363 EAPI void
364 elm_widget_type_register(const char **ptr)
365 {
366    widtypes = eina_list_append(widtypes, (void *)ptr);
367 }
368
369 EAPI Eina_Bool
370 elm_widget_api_check(int ver)
371 {
372    if (ver != ELM_INTERNAL_API_VERSION)
373      {
374         CRITICAL("Elementary widget api versions do not match");
375         return EINA_FALSE;
376      }
377    return EINA_TRUE;
378 }
379
380 EAPI Evas_Object *
381 elm_widget_add(Evas *evas)
382 {
383    _smart_init();
384    return evas_object_smart_add(evas, _e_smart);
385 }
386
387 EAPI void
388 elm_widget_del_hook_set(Evas_Object *obj, void (*func) (Evas_Object *obj))
389 {
390    API_ENTRY return;
391    sd->del_func = func;
392 }
393
394 EAPI void
395 elm_widget_del_pre_hook_set(Evas_Object *obj, void (*func) (Evas_Object *obj))
396 {
397    API_ENTRY return;
398    sd->del_pre_func = func;
399 }
400
401 EAPI void
402 elm_widget_focus_hook_set(Evas_Object *obj, void (*func) (Evas_Object *obj))
403 {
404    API_ENTRY return;
405    sd->focus_func = func;
406 }
407
408 EAPI void
409 elm_widget_activate_hook_set(Evas_Object *obj, void (*func) (Evas_Object *obj))
410 {
411    API_ENTRY return;
412    sd->activate_func = func;
413 }
414
415 EAPI void
416 elm_widget_disable_hook_set(Evas_Object *obj, void (*func) (Evas_Object *obj))
417 {
418    API_ENTRY return;
419    sd->disable_func = func;
420 }
421
422 EAPI void
423 elm_widget_theme_hook_set(Evas_Object *obj, void (*func) (Evas_Object *obj))
424 {
425    API_ENTRY return;
426    sd->theme_func = func;
427 }
428
429 EAPI void
430 elm_widget_event_hook_set(Evas_Object *obj, Eina_Bool (*func) (Evas_Object *obj, Evas_Object *source, Evas_Callback_Type type, void *event_info))
431 {
432    API_ENTRY return;
433    sd->event_func = func;
434 }
435
436 EAPI void
437 elm_widget_changed_hook_set(Evas_Object *obj, void (*func) (Evas_Object *obj))
438 {
439    API_ENTRY return;
440    sd->changed_func = func;
441 }
442
443 EAPI void
444 elm_widget_signal_emit_hook_set(Evas_Object *obj, void (*func) (Evas_Object *obj, const char *emission, const char *source))
445 {
446    API_ENTRY return;
447    sd->signal_func = func;
448 }
449
450 EAPI void
451 elm_widget_signal_callback_add_hook_set(Evas_Object *obj, void (*func) (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))
452 {
453    API_ENTRY return;
454    sd->callback_add_func = func;
455 }
456
457 EAPI void
458 elm_widget_signal_callback_del_hook_set(Evas_Object *obj, void (*func) (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))
459 {
460    API_ENTRY return;
461    sd->callback_del_func = func;
462 }
463
464 EAPI void
465 elm_widget_theme(Evas_Object *obj)
466 {
467    const Eina_List *l;
468    Evas_Object *child;
469    Elm_Tooltip *tt;
470    Elm_Cursor *cur;
471
472    API_ENTRY return;
473    EINA_LIST_FOREACH(sd->subobjs, l, child) elm_widget_theme(child);
474    if (sd->resize_obj) elm_widget_theme(sd->resize_obj);
475    if (sd->hover_obj) elm_widget_theme(sd->hover_obj);
476    EINA_LIST_FOREACH(sd->tooltips, l, tt) elm_tooltip_theme(tt);
477    EINA_LIST_FOREACH(sd->cursors, l, cur) elm_cursor_theme(cur);
478    if (sd->theme_func) sd->theme_func(obj);
479 }
480
481 EAPI void
482 elm_widget_theme_specific(Evas_Object *obj, Elm_Theme *th, Eina_Bool force)
483 {
484    const Eina_List *l;
485    Evas_Object *child;
486    Elm_Tooltip *tt;
487    Elm_Cursor *cur;
488    Elm_Theme *th2, *thdef;
489
490    API_ENTRY return;
491    thdef = elm_theme_default_get();
492    if (!th) th = thdef;
493    if (!force)
494      {
495         th2 = sd->theme;
496         if (!th2) th2 = thdef;
497         while (th2)
498           {
499              if (th2 == th)
500                {
501                  force = EINA_TRUE;
502                  break;
503                }
504              if (th2 == thdef) break;
505              th2 = th2->ref_theme;
506              if (!th2) th2 = thdef;
507           }
508      }
509    if (!force) return;
510    EINA_LIST_FOREACH(sd->subobjs, l, child)
511      elm_widget_theme_specific(child, th, force);
512    if (sd->resize_obj) elm_widget_theme(sd->resize_obj);
513    if (sd->hover_obj) elm_widget_theme(sd->hover_obj);
514    EINA_LIST_FOREACH(sd->tooltips, l, tt) elm_tooltip_theme(tt);
515    EINA_LIST_FOREACH(sd->cursors, l, cur) elm_cursor_theme(cur);
516    if (sd->theme_func) sd->theme_func(obj);
517 }
518
519 /**
520  * @internal
521  *
522  * Set hook to get next object in object focus chain.
523  *
524  * @param obj The widget object.
525  * @param func The hook to be used with this widget.
526  *
527  * @ingroup Widget
528  */
529 EAPI void
530 elm_widget_focus_next_hook_set(Evas_Object *obj, Eina_Bool (*func) (const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next))
531 {
532    API_ENTRY return;
533    sd->focus_next_func = func;
534 }
535
536 EAPI void
537 elm_widget_on_focus_hook_set(Evas_Object *obj, void (*func) (void *data, Evas_Object *obj), void *data)
538 {
539    API_ENTRY return;
540    sd->on_focus_func = func;
541    sd->on_focus_data = data;
542 }
543
544 EAPI void
545 elm_widget_on_change_hook_set(Evas_Object *obj, void (*func) (void *data, Evas_Object *obj), void *data)
546 {
547    API_ENTRY return;
548    sd->on_change_func = func;
549    sd->on_change_data = data;
550 }
551
552 EAPI void
553 elm_widget_on_show_region_hook_set(Evas_Object *obj, void (*func) (void *data, Evas_Object *obj), void *data)
554 {
555    API_ENTRY return;
556    sd->on_show_region_func = func;
557    sd->on_show_region_data = data;
558 }
559
560 /**
561  * @internal
562  *
563  * Set the hook to use to show the focused region.
564  *
565  * Whenever a new widget gets focused or it's needed to show the focused
566  * area of the current one, this hook will be called on objects that may
567  * want to move their children into their visible area.
568  * The area given in the hook function is relative to the @p obj widget.
569  *
570  * @param obj The widget object
571  * @param func The function to call to show the specified area.
572  *
573  * @ingroup Widget
574  */
575 EAPI void
576 elm_widget_focus_region_hook_set(Evas_Object *obj, void (*func) (Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h))
577 {
578    API_ENTRY return;
579    sd->focus_region_func = func;
580 }
581
582 /**
583  * @internal
584  *
585  * Set the hook to retrieve the focused region of a widget.
586  *
587  * This hook will be called by elm_widget_focus_region_get() whenever
588  * it's needed to get the focused area of a widget. The area must be relative
589  * to the widget itself and if no hook is set, it will default to the entire
590  * object.
591  *
592  * @param obj The widget object
593  * @param func The function used to retrieve the focus region.
594  *
595  * @ingroup Widget
596  */
597 EAPI void
598 elm_widget_on_focus_region_hook_set(Evas_Object *obj, void (*func) (const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h))
599 {
600    API_ENTRY return;
601    sd->on_focus_region_func = func;
602 }
603
604 EAPI void
605 elm_widget_data_set(Evas_Object *obj, void *data)
606 {
607    API_ENTRY return;
608    sd->data = data;
609 }
610
611 EAPI void *
612 elm_widget_data_get(const Evas_Object *obj)
613 {
614    API_ENTRY return NULL;
615    return sd->data;
616 }
617
618 EAPI void
619 elm_widget_sub_object_add(Evas_Object *obj, Evas_Object *sobj)
620 {
621    API_ENTRY return;
622    double scale, pscale = elm_widget_scale_get(sobj);
623    Elm_Theme *th, *pth = elm_widget_theme_get(sobj);
624
625    if (_elm_widget_is(sobj))
626      {
627         Smart_Data *sd2 = evas_object_smart_data_get(sobj);
628         if (sd2)
629           {
630              if (sd2->parent_obj == obj)
631                return;
632              if (sd2->parent_obj)
633                elm_widget_sub_object_del(sd2->parent_obj, sobj);
634              sd2->parent_obj = obj;
635              if (!sd->child_can_focus && (_is_focusable(sobj)))
636                sd->child_can_focus = EINA_TRUE;
637           }
638      }
639    else
640      {
641         void *data = evas_object_data_get(sobj, "elm-parent");
642         if (data)
643           {
644              if (data == obj) return;
645              evas_object_event_callback_del(sobj, EVAS_CALLBACK_DEL, 
646                                             _sub_obj_del);
647           }
648      }
649
650    sd->subobjs = eina_list_append(sd->subobjs, sobj);
651    evas_object_data_set(sobj, "elm-parent", obj);
652    evas_object_event_callback_add(sobj, EVAS_CALLBACK_DEL, _sub_obj_del, sd);
653    if (_elm_widget_is(sobj))
654      evas_object_event_callback_add(sobj, EVAS_CALLBACK_HIDE, _sub_obj_hide, sd);
655    evas_object_smart_callback_call(obj, "sub-object-add", sobj);
656    scale = elm_widget_scale_get(sobj);
657    th = elm_widget_theme_get(sobj);
658    if ((scale != pscale) || (th != pth)) elm_widget_theme(sobj);
659    if (elm_widget_focus_get(sobj)) _focus_parents(obj);
660 }
661
662 EAPI void
663 elm_widget_sub_object_del(Evas_Object *obj, Evas_Object *sobj)
664 {
665    Evas_Object *sobj_parent;
666    API_ENTRY return;
667    if (!sobj) return;
668
669    sobj_parent = evas_object_data_del(sobj, "elm-parent");
670    if (sobj_parent != obj)
671      {
672         static int abort_on_warn = -1;
673         ERR("removing sub object %p from parent %p, "
674             "but elm-parent is different %p!",
675             sobj, obj, sobj_parent);
676         if (EINA_UNLIKELY(abort_on_warn == -1))
677           {
678              if (getenv("ELM_ERROR_ABORT")) abort_on_warn = 1;
679              else abort_on_warn = 0;
680           }
681         if (abort_on_warn == 1) abort();
682      }
683    if (!sd->child_can_focus)
684      {
685         if (_is_focusable(sobj)) sd->child_can_focus = 0;
686      }
687    if (_elm_widget_is(sobj))
688      {
689         Smart_Data *sd2 = evas_object_smart_data_get(sobj);
690         if (sd2)
691           {
692              sd2->parent_obj = NULL;
693              if (sd2->resize_obj == sobj)
694                sd2->resize_obj = NULL;
695              else
696                sd->subobjs = eina_list_remove(sd->subobjs, sobj);
697           }
698         else
699           sd->subobjs = eina_list_remove(sd->subobjs, sobj);
700         if (elm_widget_focus_get(sobj)) _unfocus_parents(obj);
701      }
702    else
703      sd->subobjs = eina_list_remove(sd->subobjs, sobj);
704    evas_object_event_callback_del_full(sobj, EVAS_CALLBACK_DEL, 
705                                        _sub_obj_del, sd);
706    if (_elm_widget_is(sobj))
707      evas_object_event_callback_del_full(sobj, EVAS_CALLBACK_HIDE,
708                                          _sub_obj_hide, sd);
709    evas_object_smart_callback_call(obj, "sub-object-del", sobj);
710 }
711
712 EAPI void
713 elm_widget_resize_object_set(Evas_Object *obj, Evas_Object *sobj)
714 {
715    API_ENTRY return;
716    // orphan previous resize obj
717    if (sd->resize_obj)
718      {
719         evas_object_clip_unset(sd->resize_obj);
720         evas_object_data_del(sd->resize_obj, "elm-parent");
721         if (_elm_widget_is(sd->resize_obj))
722           {
723              Smart_Data *sd2 = evas_object_smart_data_get(sd->resize_obj);
724              if (sd2) sd2->parent_obj = NULL;
725              evas_object_event_callback_del_full(sd->resize_obj, EVAS_CALLBACK_HIDE,
726                                                  _sub_obj_hide, sd);
727           }
728         evas_object_event_callback_del_full(sd->resize_obj, EVAS_CALLBACK_DEL,
729                                             _sub_obj_del, sd);
730         evas_object_event_callback_del_full(sd->resize_obj, EVAS_CALLBACK_MOUSE_DOWN,
731                                             _sub_obj_mouse_down, sd);
732         evas_object_smart_member_del(sd->resize_obj);
733         if (_elm_widget_is(sd->resize_obj))
734           {
735              if (elm_widget_focus_get(sd->resize_obj)) _unfocus_parents(obj);
736           }
737      }
738    // orphan new resize obj
739    if (sobj)
740      {
741         evas_object_data_del(sobj, "elm-parent");
742         if (_elm_widget_is(sobj))
743           {
744              Smart_Data *sd2 = evas_object_smart_data_get(sobj);
745              if (sd2) sd2->parent_obj = NULL;
746              evas_object_event_callback_del_full(sobj, EVAS_CALLBACK_HIDE,
747                                                  _sub_obj_hide, sd);
748           }
749         evas_object_event_callback_del_full(sobj, EVAS_CALLBACK_DEL,
750                                             _sub_obj_del, sd);
751         evas_object_event_callback_del_full(sobj, EVAS_CALLBACK_MOUSE_DOWN,
752                                             _sub_obj_mouse_down, sd);
753         evas_object_smart_member_del(sobj);
754         if (_elm_widget_is(sobj))
755           {
756              if (elm_widget_focus_get(sobj)) _unfocus_parents(obj);
757           }
758      }
759    // set the resize obj up
760    sd->resize_obj = sobj;
761    if (sd->resize_obj)
762      {
763         if (_elm_widget_is(sd->resize_obj))
764           {
765              Smart_Data *sd2 = evas_object_smart_data_get(sd->resize_obj);
766              if (sd2) sd2->parent_obj = obj;
767              evas_object_event_callback_add(sobj, EVAS_CALLBACK_HIDE,
768                                             _sub_obj_hide, sd);
769           }
770         evas_object_clip_set(sobj, evas_object_clip_get(obj));
771         evas_object_smart_member_add(sobj, obj);
772         evas_object_event_callback_add(sobj, EVAS_CALLBACK_DEL,
773                                        _sub_obj_del, sd);
774         evas_object_event_callback_add(sobj, EVAS_CALLBACK_MOUSE_DOWN,
775                                        _sub_obj_mouse_down, sd);
776         _smart_reconfigure(sd);
777         evas_object_data_set(sobj, "elm-parent", obj);
778         evas_object_smart_callback_call(obj, "sub-object-add", sobj);
779         if (_elm_widget_is(sobj))
780           {
781              if (elm_widget_focus_get(sobj)) _focus_parents(obj);
782           }
783      }
784 }
785
786 EAPI void
787 elm_widget_hover_object_set(Evas_Object *obj, Evas_Object *sobj)
788 {
789    API_ENTRY return;
790    if (sd->hover_obj)
791      {
792         evas_object_event_callback_del_full(sd->hover_obj, EVAS_CALLBACK_DEL,
793            _sub_obj_del, sd);
794      }
795    sd->hover_obj = sobj;
796    if (sd->hover_obj)
797      {
798         evas_object_event_callback_add(sobj, EVAS_CALLBACK_DEL,
799                                        _sub_obj_del, sd);
800         _smart_reconfigure(sd);
801      }
802 }
803
804 EAPI void
805 elm_widget_can_focus_set(Evas_Object *obj, Eina_Bool can_focus)
806 {
807    API_ENTRY return;
808    sd->can_focus = can_focus;
809    if (can_focus)
810      {
811         evas_object_event_callback_add(obj, EVAS_CALLBACK_KEY_DOWN,
812                                        _propagate_event,
813                                        (void *)(long) EVAS_CALLBACK_KEY_DOWN);
814         evas_object_event_callback_add(obj, EVAS_CALLBACK_KEY_UP,
815                                        _propagate_event,
816                                        (void *)(long) EVAS_CALLBACK_KEY_UP);
817         evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_WHEEL,
818                                        _propagate_event,
819                                        (void *)(long)EVAS_CALLBACK_MOUSE_WHEEL);
820      }
821    else
822      {
823         evas_object_event_callback_del(obj, EVAS_CALLBACK_KEY_DOWN,
824                                        _propagate_event);
825         evas_object_event_callback_del(obj, EVAS_CALLBACK_KEY_UP,
826                                        _propagate_event);
827         evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_WHEEL,
828                                        _propagate_event);
829      }
830 }
831
832 EAPI Eina_Bool
833 elm_widget_can_focus_get(const Evas_Object *obj)
834 {
835    API_ENTRY return EINA_FALSE;
836    return sd->can_focus;
837 }
838
839 EAPI Eina_Bool
840 elm_widget_child_can_focus_get(const Evas_Object *obj)
841 {
842    API_ENTRY return EINA_FALSE;
843    return sd->child_can_focus;
844 }
845
846 EAPI void
847 elm_widget_highlight_ignore_set(Evas_Object *obj, Eina_Bool ignore)
848 {
849    API_ENTRY return;
850    sd->highlight_ignore = !!ignore;
851 }
852
853 EAPI Eina_Bool
854 elm_widget_highlight_ignore_get(const Evas_Object *obj)
855 {
856    API_ENTRY return EINA_FALSE;
857    return sd->highlight_ignore;
858 }
859
860 EAPI void
861 elm_widget_highlight_in_theme_set(Evas_Object *obj, Eina_Bool highlight)
862 {
863    API_ENTRY return;
864    sd->highlight_in_theme = !!highlight;
865    /* FIXME: if focused, it should switch from one mode to the other */
866 }
867
868 EAPI Eina_Bool
869 elm_widget_highlight_in_theme_get(const Evas_Object *obj)
870 {
871    API_ENTRY return EINA_FALSE;
872    return sd->highlight_in_theme;
873 }
874
875 EAPI Eina_Bool
876 elm_widget_focus_get(const Evas_Object *obj)
877 {
878    API_ENTRY return EINA_FALSE;
879    return sd->focused;
880 }
881
882 EAPI Evas_Object *
883 elm_widget_focused_object_get(const Evas_Object *obj)
884 {
885    const Evas_Object *subobj;
886    const Eina_List *l;
887    API_ENTRY return NULL;
888
889    if (!sd->focused) return NULL;
890    EINA_LIST_FOREACH(sd->subobjs, l, subobj)
891      {
892         Evas_Object *fobj = elm_widget_focused_object_get(subobj);
893         if (fobj) return fobj;
894      }
895    return (Evas_Object *)obj;
896 }
897
898 EAPI Evas_Object *
899 elm_widget_top_get(const Evas_Object *obj)
900 {
901    API_ENTRY return NULL;
902    if (sd->parent_obj) return elm_widget_top_get(sd->parent_obj);
903    return (Evas_Object *)obj;
904 }
905
906 EAPI Eina_Bool
907 elm_widget_is(const Evas_Object *obj)
908 {
909    return _elm_widget_is(obj);
910 }
911
912 EAPI Evas_Object *
913 elm_widget_parent_widget_get(const Evas_Object *obj)
914 {
915    Evas_Object *parent;
916
917    if (_elm_widget_is(obj))
918      {
919         Smart_Data *sd = evas_object_smart_data_get(obj);
920         if (!sd) return NULL;
921         parent = sd->parent_obj;
922      }
923    else
924      {
925         parent = evas_object_data_get(obj, "elm-parent");
926         if (!parent) parent = evas_object_smart_parent_get(obj);
927      }
928
929    while (parent)
930      {
931         Evas_Object *elm_parent;
932         if (_elm_widget_is(parent)) break;
933         elm_parent = evas_object_data_get(parent, "elm-parent");
934         if (elm_parent) parent = elm_parent;
935         else parent = evas_object_smart_parent_get(parent);
936      }
937    return parent;
938 }
939
940 EAPI void
941 elm_widget_event_callback_add(Evas_Object *obj, Elm_Event_Cb func, const void *data)
942 {
943    API_ENTRY return;
944    EINA_SAFETY_ON_NULL_RETURN(func);
945    Elm_Event_Cb_Data *ecb = ELM_NEW(Elm_Event_Cb_Data);
946    ecb->func = func;
947    ecb->data = data;
948    sd->event_cb = eina_list_append(sd->event_cb, ecb);
949 }
950
951 EAPI void *
952 elm_widget_event_callback_del(Evas_Object *obj, Elm_Event_Cb func, const void *data)
953 {
954    API_ENTRY return NULL;
955    EINA_SAFETY_ON_NULL_RETURN_VAL(func, NULL);
956    Eina_List *l;
957    Elm_Event_Cb_Data *ecd;
958    EINA_LIST_FOREACH(sd->event_cb, l, ecd)
959       if ((ecd->func == func) && (ecd->data == data))
960         {
961            free(ecd);
962            sd->event_cb = eina_list_remove_list(sd->event_cb, l);
963            return (void *)data;
964         }
965    return NULL;
966 }
967
968 EAPI Eina_Bool
969 elm_widget_event_propagate(Evas_Object *obj, Evas_Callback_Type type, void *event_info, Evas_Event_Flags *event_flags)
970 {
971    API_ENTRY return EINA_FALSE; //TODO reduce.
972    if (!_elm_widget_is(obj)) return EINA_FALSE;
973    Evas_Object *parent = obj;
974    Elm_Event_Cb_Data *ecd;
975    Eina_List *l, *l_prev;
976
977    while (parent &&
978           (!(event_flags && ((*event_flags) & EVAS_EVENT_FLAG_ON_HOLD))))
979      {
980         sd = evas_object_smart_data_get(parent);
981         if ((!sd) || (!_elm_widget_is(obj)))
982           return EINA_FALSE; //Not Elm Widget
983
984         if (sd->event_func && (sd->event_func(parent, obj, type, event_info)))
985           return EINA_TRUE;
986
987         EINA_LIST_FOREACH_SAFE(sd->event_cb, l, l_prev, ecd)
988           {
989              if (ecd->func((void *)ecd->data, parent, obj, type, event_info) ||
990                  (event_flags && ((*event_flags) & EVAS_EVENT_FLAG_ON_HOLD)))
991                  return EINA_TRUE;
992           }
993         parent = sd->parent_obj;
994      }
995
996    return EINA_FALSE;
997 }
998
999 /**
1000  * @internal
1001  *
1002  * Set custom focus chain.
1003  *
1004  * This function i set one new and overwrite any previous custom focus chain
1005  * with the list of objects. The previous list will be deleted and this list
1006  * will be managed. After setted, don't modity it.
1007  *
1008  * @note On focus cycle, only will be evaluated children of this container.
1009  *
1010  * @param obj The container widget
1011  * @param objs Chain of objects to pass focus
1012  * @ingroup Widget
1013  */
1014 EAPI void
1015 elm_widget_focus_custom_chain_set(Evas_Object *obj, Eina_List *objs)
1016 {
1017    API_ENTRY return;
1018    if (!sd->focus_next_func)
1019      return;
1020
1021    elm_widget_focus_custom_chain_unset(obj);
1022
1023    Eina_List *l;
1024    Evas_Object *o;
1025
1026    EINA_LIST_FOREACH(objs, l, o)
1027      {
1028         evas_object_event_callback_add(o, EVAS_CALLBACK_DEL,
1029                                        _elm_object_focus_chain_del_cb, sd);
1030      }
1031
1032    sd->focus_chain = objs;
1033 }
1034
1035 /**
1036  * @internal
1037  *
1038  * Get custom focus chain
1039  *
1040  * @param obj The container widget
1041  * @ingroup Widget
1042  */
1043 EAPI const Eina_List *
1044 elm_widget_focus_custom_chain_get(const Evas_Object *obj)
1045 {
1046    API_ENTRY return NULL;
1047    return (const Eina_List *) sd->focus_chain;
1048 }
1049
1050 /**
1051  * @internal
1052  *
1053  * Unset custom focus chain
1054  *
1055  * @param obj The container widget
1056  * @ingroup Widget
1057  */
1058 EAPI void
1059 elm_widget_focus_custom_chain_unset(Evas_Object *obj)
1060 {
1061    API_ENTRY return;
1062    Eina_List *l, *l_next;
1063    Evas_Object *o;
1064
1065    EINA_LIST_FOREACH_SAFE(sd->focus_chain, l, l_next, o)
1066      {
1067         evas_object_event_callback_del_full(o, EVAS_CALLBACK_DEL,
1068                                             _elm_object_focus_chain_del_cb, sd);
1069         sd->focus_chain = eina_list_remove_list(sd->focus_chain, l);
1070      }
1071 }
1072
1073 /**
1074  * @internal
1075  *
1076  * Append object to custom focus chain.
1077  *
1078  * @note If relative_child equal to NULL or not in custom chain, the object
1079  * will be added in end.
1080  *
1081  * @note On focus cycle, only will be evaluated children of this container.
1082  *
1083  * @param obj The container widget
1084  * @param child The child to be added in custom chain
1085  * @param relative_child The relative object to position the child
1086  * @ingroup Widget
1087  */
1088 EAPI void
1089 elm_widget_focus_custom_chain_append(Evas_Object *obj, Evas_Object *child, Evas_Object *relative_child)
1090 {
1091    API_ENTRY return;
1092    EINA_SAFETY_ON_NULL_RETURN(child);
1093    if (!sd->focus_next_func)
1094      return;
1095
1096    evas_object_event_callback_del_full(child, EVAS_CALLBACK_DEL,
1097                                        _elm_object_focus_chain_del_cb, sd);
1098
1099    if (!relative_child)
1100      {
1101         sd->focus_chain = eina_list_append(sd->focus_chain, child);
1102         return;
1103      }
1104
1105    sd->focus_chain = eina_list_append_relative(sd->focus_chain, child, relative_child);
1106    return;
1107 }
1108
1109 /**
1110  * @internal
1111  *
1112  * Prepend object to custom focus chain.
1113  *
1114  * @note If relative_child equal to NULL or not in custom chain, the object
1115  * will be added in begin.
1116  *
1117  * @note On focus cycle, only will be evaluated children of this container.
1118  *
1119  * @param obj The container widget
1120  * @param child The child to be added in custom chain
1121  * @param relative_child The relative object to position the child
1122  * @ingroup Widget
1123  */
1124 EAPI void
1125 elm_widget_focus_custom_chain_prepend(Evas_Object *obj, Evas_Object *child, Evas_Object *relative_child)
1126 {
1127    API_ENTRY return;
1128    EINA_SAFETY_ON_NULL_RETURN(child);
1129    if (!sd->focus_next_func)
1130      return;
1131
1132    evas_object_event_callback_del_full(child, EVAS_CALLBACK_DEL,
1133                                        _elm_object_focus_chain_del_cb, sd);
1134
1135    if (!relative_child)
1136      {
1137         sd->focus_chain = eina_list_prepend(sd->focus_chain, child);
1138         return;
1139      }
1140
1141    sd->focus_chain = eina_list_prepend_relative(sd->focus_chain, child, relative_child);
1142    return;
1143 }
1144
1145 /**
1146  * @internal
1147  *
1148  * Give focus to next object in object tree.
1149  *
1150  * Give focus to next object in focus chain of one object sub-tree.
1151  * If the last object of chain already have focus, the focus will go to the
1152  * first object of chain.
1153  *
1154  * @param obj The widget root of sub-tree
1155  * @param dir Direction to cycle the focus
1156  *
1157  * @ingroup Widget
1158  */
1159 EAPI void
1160 elm_widget_focus_cycle(Evas_Object *obj, Elm_Focus_Direction dir)
1161 {
1162    Evas_Object *target = NULL;
1163    if (!_elm_widget_is(obj))
1164      return;
1165    elm_widget_focus_next_get(obj, dir, &target);
1166    if (target)
1167      elm_widget_focus_steal(target);
1168 }
1169
1170 /**
1171  * @internal
1172  *
1173  * Give focus to near object in one direction.
1174  *
1175  * Give focus to near object in direction of one object.
1176  * If none focusable object in given direction, the focus will not change.
1177  *
1178  * @param obj The reference widget
1179  * @param x Horizontal component of direction to focus
1180  * @param y Vertical component of direction to focus
1181  *
1182  * @ingroup Widget
1183  */
1184 EAPI void
1185 elm_widget_focus_direction_go(Evas_Object *obj __UNUSED__, int x __UNUSED__, int y __UNUSED__)
1186 {
1187    return; /* TODO */
1188 }
1189
1190 /**
1191  * @internal
1192  *
1193  * Get next object in focus chain of object tree.
1194  *
1195  * Get next object in focus chain of one object sub-tree.
1196  * Return the next object by reference. If don't have any candidate to receive
1197  * focus before chain end, the first candidate will be returned.
1198  *
1199  * @param obj The widget root of sub-tree
1200  * @param dir Direction os focus chain
1201  * @param next The next object in focus chain
1202  * @return EINA_TRUE if don't need focus chain restart/loop back
1203  *         to use 'next' obj.
1204  *
1205  * @ingroup Widget
1206  */
1207 EAPI Eina_Bool
1208 elm_widget_focus_next_get(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
1209 {
1210    if (!next)
1211      return EINA_FALSE;
1212    *next = NULL;
1213
1214    API_ENTRY return EINA_FALSE;
1215
1216    /* Ignore if disabled */
1217    if ((!evas_object_visible_get(obj)) || (elm_widget_disabled_get(obj)))
1218      return EINA_FALSE;
1219
1220    /* Try use hook */
1221    if (sd->focus_next_func)
1222      return sd->focus_next_func(obj, dir, next);
1223
1224    if (!elm_widget_can_focus_get(obj))
1225      return EINA_FALSE;
1226
1227    /* Return */
1228    *next = (Evas_Object *)obj;
1229    return !elm_widget_focus_get(obj);
1230 }
1231
1232
1233 /**
1234  * @internal
1235  *
1236  * Get next object in focus chain of object tree in list.
1237  *
1238  * Get next object in focus chain of one object sub-tree ordered by one list.
1239  * Return the next object by reference. If don't have any candidate to receive
1240  * focus before list end, the first candidate will be returned.
1241  *
1242  * @param obj The widget root of sub-tree
1243  * @param dir Direction os focus chain
1244  * @param items list with ordered objects
1245  * @param list_data_get function to get the object from one item of list
1246  * @param next The next object in focus chain
1247  * @return EINA_TRUE if don't need focus chain restart/loop back
1248  *         to use 'next' obj.
1249  *
1250  * @ingroup Widget
1251  */
1252 EAPI Eina_Bool
1253 elm_widget_focus_list_next_get(const Evas_Object *obj, const Eina_List *items, void *(*list_data_get) (const Eina_List *list), Elm_Focus_Direction dir, Evas_Object **next)
1254 {
1255    Eina_List *(*list_next) (const Eina_List *list);
1256
1257    if (!next)
1258      return EINA_FALSE;
1259    *next = NULL;
1260
1261    if (!_elm_widget_is(obj))
1262      return EINA_FALSE;
1263
1264    if (!items)
1265      return EINA_FALSE;
1266
1267    /* Direction */
1268    if (dir == ELM_FOCUS_PREVIOUS)
1269      {
1270         items = eina_list_last(items);
1271         list_next = eina_list_prev;
1272      }
1273    else if (dir == ELM_FOCUS_NEXT)
1274      list_next = eina_list_next;
1275    else
1276      return EINA_FALSE;
1277
1278    const Eina_List *l = items;
1279
1280    /* Recovery last focused sub item */
1281    if (elm_widget_focus_get(obj))
1282      for (; l; l = list_next(l))
1283        {
1284           Evas_Object *cur = list_data_get(l);
1285           if (elm_widget_focus_get(cur)) break;
1286        }
1287
1288    const Eina_List *start = l;
1289    Evas_Object *to_focus = NULL;
1290
1291    /* Interate sub items */
1292    /* Go to end of list */
1293    for (; l; l = list_next(l))
1294      {
1295         Evas_Object *tmp = NULL;
1296         Evas_Object *cur = list_data_get(l);
1297
1298         if (elm_widget_parent_get(cur) != obj)
1299           continue;
1300
1301         /* Try Focus cycle in subitem */
1302         if (elm_widget_focus_next_get(cur, dir, &tmp))
1303           {
1304              *next = tmp;
1305              return EINA_TRUE;
1306           }
1307         else if ((tmp) && (!to_focus))
1308           to_focus = tmp;
1309      }
1310
1311    l = items;
1312
1313    /* Get First possible */
1314    for (;l != start; l = list_next(l))
1315      {
1316         Evas_Object *tmp = NULL;
1317         Evas_Object *cur = list_data_get(l);
1318
1319         if (elm_widget_parent_get(cur) != obj)
1320           continue;
1321
1322         /* Try Focus cycle in subitem */
1323         elm_widget_focus_next_get(cur, dir, &tmp);
1324         if (tmp)
1325           {
1326              *next = tmp;
1327              return EINA_FALSE;
1328           }
1329      }
1330
1331    *next = to_focus;
1332    return EINA_FALSE;
1333 }
1334
1335 EAPI void
1336 elm_widget_signal_emit(Evas_Object *obj, const char *emission, const char *source)
1337 {
1338    API_ENTRY return;
1339    if (!sd->signal_func) return;
1340    sd->signal_func(obj, emission, source);
1341 }
1342
1343 static void
1344 _edje_signal_callback(void *data, Evas_Object *obj __UNUSED__, const char *emission, const char *source)
1345 {
1346    Edje_Signal_Data *esd = data;
1347    esd->func(esd->data, esd->obj, emission, source);
1348 }
1349
1350 EAPI void
1351 elm_widget_signal_callback_add(Evas_Object *obj, const char *emission, const char *source, void (*func) (void *data, Evas_Object *o, const char *emission, const char *source), void *data)
1352 {
1353    Edje_Signal_Data *esd;
1354    API_ENTRY return;
1355    if (!sd->callback_add_func) return;
1356    EINA_SAFETY_ON_NULL_RETURN(func);
1357
1358    esd = ELM_NEW(Edje_Signal_Data);
1359    if (!esd) return;
1360
1361    esd->obj = obj;
1362    esd->func = func;
1363    esd->emission = eina_stringshare_add(emission);
1364    esd->source = eina_stringshare_add(source);
1365    esd->data = data;
1366    sd->edje_signals = eina_list_append(sd->edje_signals, esd);
1367    sd->callback_add_func(obj, emission, source, _edje_signal_callback, esd);
1368 }
1369
1370 EAPI void *
1371 elm_widget_signal_callback_del(Evas_Object *obj, const char *emission, const char *source, void (*func) (void *data, Evas_Object *o, const char *emission, const char *source))
1372 {
1373    Edje_Signal_Data *esd;
1374    Eina_List *l;
1375    void *data = NULL;
1376    API_ENTRY return NULL;
1377    if (!sd->callback_del_func) return NULL;
1378
1379    EINA_LIST_FOREACH(sd->edje_signals, l, esd)
1380      {
1381         if ((esd->func == func) && (!strcmp(esd->emission, emission)) &&
1382             (!strcmp(esd->source, source)))
1383           {
1384              sd->edje_signals = eina_list_remove_list(sd->edje_signals, l);
1385              eina_stringshare_del(esd->emission);
1386              eina_stringshare_del(esd->source);
1387              data = esd->data;
1388              free(esd);
1389              break;
1390           }
1391      }
1392    sd->callback_del_func(obj, emission, source, _edje_signal_callback, esd);
1393    return data;
1394 }
1395
1396 EAPI void
1397 elm_widget_focus_set(Evas_Object *obj, int first)
1398 {
1399    API_ENTRY return;
1400    if (!sd->focused)
1401      {
1402         focus_order++;
1403         sd->focus_order = focus_order;
1404         sd->focused = EINA_TRUE;
1405         if (sd->on_focus_func) sd->on_focus_func(sd->on_focus_data, obj);
1406      }
1407    if (sd->focus_func)
1408      {
1409         sd->focus_func(obj);
1410         return;
1411      }
1412    else
1413      {
1414         if (first)
1415           {
1416              if ((_is_focusable(sd->resize_obj)) &&
1417                  (!elm_widget_disabled_get(sd->resize_obj)))
1418                {
1419                   elm_widget_focus_set(sd->resize_obj, first);
1420                }
1421              else
1422                {
1423                   const Eina_List *l;
1424                   Evas_Object *child;
1425                   EINA_LIST_FOREACH(sd->subobjs, l, child)
1426                     {
1427                        if ((_is_focusable(child)) &&
1428                            (!elm_widget_disabled_get(child)))
1429                          {
1430                             elm_widget_focus_set(child, first);
1431                             break;
1432                          }
1433                     }
1434                }
1435           }
1436         else
1437           {
1438              const Eina_List *l;
1439              Evas_Object *child;
1440              EINA_LIST_REVERSE_FOREACH(sd->subobjs, l, child)
1441                {
1442                   if ((_is_focusable(child)) &&
1443                       (!elm_widget_disabled_get(child)))
1444                     {
1445                        elm_widget_focus_set(child, first);
1446                        break;
1447                     }
1448                }
1449              if (!l)
1450                {
1451                   if ((_is_focusable(sd->resize_obj)) &&
1452                       (!elm_widget_disabled_get(sd->resize_obj)))
1453                     {
1454                        elm_widget_focus_set(sd->resize_obj, first);
1455                     }
1456                }
1457           }
1458      }
1459 }
1460
1461 EAPI Evas_Object *
1462 elm_widget_parent_get(const Evas_Object *obj)
1463 {
1464    API_ENTRY return NULL;
1465    return sd->parent_obj;
1466 }
1467
1468 EAPI void
1469 elm_widget_focused_object_clear(Evas_Object *obj)
1470 {
1471    API_ENTRY return;
1472    if (!sd->focused) return;
1473    if (elm_widget_focus_get(sd->resize_obj))
1474       elm_widget_focused_object_clear(sd->resize_obj);
1475    else
1476      {
1477         const Eina_List *l;
1478         Evas_Object *child;
1479         EINA_LIST_FOREACH(sd->subobjs, l, child)
1480           {
1481              if (elm_widget_focus_get(child))
1482                {
1483                   elm_widget_focused_object_clear(child);
1484                   break;
1485                }
1486           }
1487      }
1488    sd->focused = EINA_FALSE;
1489    if (sd->on_focus_func) sd->on_focus_func(sd->on_focus_data, obj);
1490    if (sd->focus_func) sd->focus_func(obj);
1491 }
1492
1493 EAPI void
1494 elm_widget_focus_steal(Evas_Object *obj)
1495 {
1496    Evas_Object *parent, *o;
1497    API_ENTRY return;
1498
1499    if (sd->focused) return;
1500    if (sd->disabled) return;
1501    parent = obj;
1502    for (;;)
1503      {
1504         o = elm_widget_parent_get(parent);
1505         if (!o) break;
1506         sd = evas_object_smart_data_get(o);
1507         if (sd->focused) break;
1508         parent = o;
1509      }
1510    if (!elm_widget_parent_get(parent))
1511      elm_widget_focused_object_clear(parent);
1512    else
1513      {
1514         parent = elm_widget_parent_get(parent);
1515         sd = evas_object_smart_data_get(parent);
1516         if ((sd->resize_obj) && (elm_widget_focus_get(sd->resize_obj)))
1517           elm_widget_focused_object_clear(sd->resize_obj);
1518         else
1519           {
1520              const Eina_List *l;
1521              Evas_Object *child;
1522              EINA_LIST_FOREACH(sd->subobjs, l, child)
1523                {
1524                   if (elm_widget_focus_get(child))
1525                     {
1526                        elm_widget_focused_object_clear(child);
1527                        break;
1528                     }
1529                }
1530           }
1531      }
1532    _parent_focus(obj);
1533    return;
1534 }
1535
1536 EAPI void
1537 elm_widget_activate(Evas_Object *obj)
1538 {
1539    API_ENTRY return;
1540    elm_widget_change(obj);
1541    if (sd->activate_func) sd->activate_func(obj);
1542 }
1543
1544 EAPI void
1545 elm_widget_change(Evas_Object *obj)
1546 {
1547    API_ENTRY return;
1548    elm_widget_change(elm_widget_parent_get(obj));
1549    if (sd->on_change_func) sd->on_change_func(sd->on_change_data, obj);
1550 }
1551
1552 EAPI void
1553 elm_widget_disabled_set(Evas_Object *obj, int disabled)
1554 {
1555    API_ENTRY return;
1556
1557    if (sd->disabled == disabled) return;
1558    sd->disabled = disabled;
1559    if (sd->focused)
1560      {
1561         Evas_Object *o, *parent;
1562
1563         parent = obj;
1564         for (;;)
1565           {
1566              o = elm_widget_parent_get(parent);
1567              if (!o) break;
1568              parent = o;
1569           }
1570         if (elm_widget_focus_get(obj))
1571           elm_widget_focus_cycle(parent, ELM_FOCUS_NEXT);
1572      }
1573    if (sd->disable_func) sd->disable_func(obj);
1574 }
1575
1576 EAPI int
1577 elm_widget_disabled_get(const Evas_Object *obj)
1578 {
1579    API_ENTRY return 0;
1580    return sd->disabled;
1581 }
1582
1583 EAPI void
1584 elm_widget_show_region_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
1585 {
1586    Evas_Object *parent_obj, *child_obj;
1587    Evas_Coord px, py, cx, cy;
1588
1589    API_ENTRY return;
1590    if ((x == sd->rx) && (y == sd->ry) && (w == sd->rw) && (h == sd->rh)) return;
1591    sd->rx = x;
1592    sd->ry = y;
1593    sd->rw = w;
1594    sd->rh = h;
1595    if (sd->on_show_region_func)
1596       sd->on_show_region_func(sd->on_show_region_data, obj);
1597
1598    do
1599      {
1600         parent_obj = sd->parent_obj;
1601         child_obj = sd->obj;
1602         sd = evas_object_smart_data_get(parent_obj);
1603
1604         if ((!parent_obj) || (!sd) || (!_elm_widget_is(parent_obj))) break;
1605
1606         evas_object_geometry_get(parent_obj, &px, &py, NULL, NULL);
1607         evas_object_geometry_get(child_obj, &cx, &cy, NULL, NULL);
1608
1609         x += (cx - px);
1610         y += (cy - py);
1611         sd->rx = x;
1612         sd->ry = y;
1613         sd->rw = w;
1614         sd->rh = h;
1615
1616         if (sd->on_show_region_func)
1617           {
1618              sd->on_show_region_func(sd->on_show_region_data, parent_obj);
1619           }
1620      }
1621    while (parent_obj);
1622 }
1623
1624 EAPI void
1625 elm_widget_show_region_get(const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
1626 {
1627    API_ENTRY return;
1628    if (x) *x = sd->rx;
1629    if (y) *y = sd->ry;
1630    if (w) *w = sd->rw;
1631    if (h) *h = sd->rh;
1632 }
1633
1634 /**
1635  * @internal
1636  *
1637  * Get the focus region of the given widget.
1638  *
1639  * The focus region is the area of a widget that should brought into the
1640  * visible area when the widget is focused. Mostly used to show the part of
1641  * an entry where the cursor is, for example. The area returned is relative
1642  * to the object @p obj.
1643  * If the @p obj doesn't have the proper on_focus_region_hook set, this
1644  * function will return the full size of the object.
1645  *
1646  * @param obj The widget object
1647  * @param x Where to store the x coordinate of the area
1648  * @param y Where to store the y coordinate of the area
1649  * @param w Where to store the width of the area
1650  * @param h Where to store the height of the area
1651  *
1652  * @ingroup Widget
1653  */
1654 EAPI void
1655 elm_widget_focus_region_get(const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
1656 {
1657    Smart_Data *sd;
1658
1659    if (!obj) return;
1660
1661    sd = evas_object_smart_data_get(obj);
1662    if (!sd || !_elm_widget_is(obj) || !sd->on_focus_region_func)
1663      {
1664         evas_object_geometry_get(obj, NULL, NULL, w, h);
1665         if (x) *x = 0;
1666         if (y) *y = 0;
1667         return;
1668      }
1669    sd->on_focus_region_func(obj, x, y, w, h);
1670 }
1671
1672 EAPI void
1673 elm_widget_scroll_hold_push(Evas_Object *obj)
1674 {
1675    API_ENTRY return;
1676    sd->scroll_hold++;
1677    if (sd->scroll_hold == 1)
1678       evas_object_smart_callback_call(obj, "scroll-hold-on", obj);
1679    if (sd->parent_obj) elm_widget_scroll_hold_push(sd->parent_obj);
1680    // FIXME: on delete/reparent hold pop
1681 }
1682
1683 EAPI void
1684 elm_widget_scroll_hold_pop(Evas_Object *obj)
1685 {
1686    API_ENTRY return;
1687    sd->scroll_hold--;
1688    if (sd->scroll_hold < 0) sd->scroll_hold = 0;
1689    if (!sd->scroll_hold)
1690       evas_object_smart_callback_call(obj, "scroll-hold-off", obj);
1691    if (sd->parent_obj) elm_widget_scroll_hold_pop(sd->parent_obj);
1692 }
1693
1694 EAPI int
1695 elm_widget_scroll_hold_get(const Evas_Object *obj)
1696 {
1697    API_ENTRY return 0;
1698    return sd->scroll_hold;
1699 }
1700
1701 EAPI void
1702 elm_widget_scroll_freeze_push(Evas_Object *obj)
1703 {
1704    API_ENTRY return;
1705    sd->scroll_freeze++;
1706    if (sd->scroll_freeze == 1)
1707       evas_object_smart_callback_call(obj, "scroll-freeze-on", obj);
1708    if (sd->parent_obj) elm_widget_scroll_freeze_push(sd->parent_obj);
1709    // FIXME: on delete/reparent freeze pop
1710 }
1711
1712 EAPI void
1713 elm_widget_scroll_freeze_pop(Evas_Object *obj)
1714 {
1715    API_ENTRY return;
1716    sd->scroll_freeze--;
1717    if (sd->scroll_freeze < 0) sd->scroll_freeze = 0;
1718    if (!sd->scroll_freeze)
1719       evas_object_smart_callback_call(obj, "scroll-freeze-off", obj);
1720    if (sd->parent_obj) elm_widget_scroll_freeze_pop(sd->parent_obj);
1721 }
1722
1723 EAPI int
1724 elm_widget_scroll_freeze_get(const Evas_Object *obj)
1725 {
1726    API_ENTRY return 0;
1727    return sd->scroll_freeze;
1728 }
1729
1730 EAPI void
1731 elm_widget_scale_set(Evas_Object *obj, double scale)
1732 {
1733    API_ENTRY return;
1734    if (scale <= 0.0) scale = 0.0;
1735    if (sd->scale != scale)
1736      {
1737         sd->scale = scale;
1738         elm_widget_theme(obj);
1739      }
1740 }
1741
1742 EAPI double
1743 elm_widget_scale_get(const Evas_Object *obj)
1744 {
1745    API_ENTRY return 1.0;
1746    // FIXME: save walking up the tree by storing/caching parent scale
1747    if (sd->scale == 0.0)
1748      {
1749         if (sd->parent_obj)
1750            return elm_widget_scale_get(sd->parent_obj);
1751         else
1752            return 1.0;
1753      }
1754    return sd->scale;
1755 }
1756
1757 EAPI void
1758 elm_widget_theme_set(Evas_Object *obj, Elm_Theme *th)
1759 {
1760    API_ENTRY return;
1761    if (sd->theme != th)
1762      {
1763         if (sd->theme) elm_theme_free(sd->theme);
1764         sd->theme = th;
1765         if (th) th->ref++;
1766         elm_widget_theme(obj);
1767      }
1768 }
1769
1770 EAPI Elm_Theme *
1771 elm_widget_theme_get(const Evas_Object *obj)
1772 {
1773    API_ENTRY return NULL;
1774    if (!sd->theme)
1775      {
1776         if (sd->parent_obj)
1777            return elm_widget_theme_get(sd->parent_obj);
1778         else
1779            return NULL;
1780      }
1781    return sd->theme;
1782 }
1783
1784 EAPI void
1785 elm_widget_style_set(Evas_Object *obj, const char *style)
1786 {
1787    API_ENTRY return;
1788    
1789    if (eina_stringshare_replace(&sd->style, style))
1790       elm_widget_theme(obj);
1791 }
1792
1793 EAPI const char *
1794 elm_widget_style_get(const Evas_Object *obj)
1795 {
1796    API_ENTRY return NULL;
1797    if (sd->style) return sd->style;
1798    return "default";
1799 }
1800
1801 EAPI void
1802 elm_widget_type_set(Evas_Object *obj, const char *type)
1803 {
1804    API_ENTRY return;
1805    eina_stringshare_replace(&sd->type, type);
1806 }
1807
1808 EAPI const char *
1809 elm_widget_type_get(const Evas_Object *obj)
1810 {
1811    API_ENTRY return NULL;
1812    if (sd->type) return sd->type;
1813    return "";
1814 }
1815
1816 EAPI void
1817 elm_widget_tooltip_add(Evas_Object *obj, Elm_Tooltip *tt)
1818 {
1819    API_ENTRY return;
1820    sd->tooltips = eina_list_append(sd->tooltips, tt);
1821 }
1822
1823 EAPI void
1824 elm_widget_tooltip_del(Evas_Object *obj, Elm_Tooltip *tt)
1825 {
1826    API_ENTRY return;
1827    sd->tooltips = eina_list_remove(sd->tooltips, tt);
1828 }
1829
1830 EAPI void
1831 elm_widget_cursor_add(Evas_Object *obj, Elm_Cursor *cur)
1832 {
1833    API_ENTRY return;
1834    sd->cursors = eina_list_append(sd->cursors, cur);
1835 }
1836
1837 EAPI void
1838 elm_widget_cursor_del(Evas_Object *obj, Elm_Cursor *cur)
1839 {
1840    API_ENTRY return;
1841    sd->cursors = eina_list_remove(sd->cursors, cur);
1842 }
1843
1844 EAPI void
1845 elm_widget_drag_lock_x_set(Evas_Object *obj, Eina_Bool lock)
1846 {
1847    API_ENTRY return;
1848    if (sd->drag_x_locked == lock) return;
1849    sd->drag_x_locked = lock;
1850    if (sd->drag_x_locked) _propagate_x_drag_lock(obj, 1);
1851    else _propagate_x_drag_lock(obj, -1);
1852 }
1853
1854 EAPI void
1855 elm_widget_drag_lock_y_set(Evas_Object *obj, Eina_Bool lock)
1856 {
1857    API_ENTRY return;
1858    if (sd->drag_y_locked == lock) return;
1859    sd->drag_y_locked = lock;
1860    if (sd->drag_y_locked) _propagate_y_drag_lock(obj, 1);
1861    else _propagate_y_drag_lock(obj, -1);
1862 }
1863
1864 EAPI Eina_Bool
1865 elm_widget_drag_lock_x_get(const Evas_Object *obj)
1866 {
1867    API_ENTRY return EINA_FALSE;
1868    return sd->drag_x_locked;
1869 }
1870
1871 EAPI Eina_Bool
1872 elm_widget_drag_lock_y_get(const Evas_Object *obj)
1873 {
1874    API_ENTRY return EINA_FALSE;
1875    return sd->drag_y_locked;
1876 }
1877
1878 EAPI int
1879 elm_widget_drag_child_locked_x_get(const Evas_Object *obj)
1880 {
1881    API_ENTRY return 0;
1882    return sd->child_drag_x_locked;
1883 }
1884
1885 EAPI int
1886 elm_widget_drag_child_locked_y_get(const Evas_Object *obj)
1887 {
1888    API_ENTRY return 0;
1889    return sd->child_drag_y_locked;
1890 }
1891
1892 EAPI Eina_Bool
1893 elm_widget_theme_object_set(Evas_Object *obj, Evas_Object *edj, const char *wname, const char *welement, const char *wstyle)
1894 {
1895    API_ENTRY return EINA_FALSE;
1896    return _elm_theme_object_set(obj, edj, wname, welement, wstyle);
1897 }
1898
1899 EAPI Eina_Bool
1900 elm_widget_type_check(const Evas_Object *obj, const char *type)
1901 {
1902    const char *provided, *expected = "(unknown)";
1903    static int abort_on_warn = -1;
1904    provided = elm_widget_type_get(obj);
1905    if (EINA_LIKELY(provided == type)) return EINA_TRUE;
1906    if (type) expected = type;
1907    if ((!provided) || (!provided[0]))
1908      {
1909         provided = evas_object_type_get(obj);
1910         if ((!provided) || (!provided[0]))
1911            provided = "(unknown)";
1912      }
1913    ERR("Passing Object: %p, of type: '%s' when expecting type: '%s'", obj, provided, expected);
1914    if (abort_on_warn == -1)
1915      {
1916         if (getenv("ELM_ERROR_ABORT")) abort_on_warn = 1;
1917         else abort_on_warn = 0;
1918      }
1919    if (abort_on_warn == 1) abort();
1920    return EINA_FALSE;
1921 }
1922
1923 /**
1924  * @internal
1925  *
1926  * Split string in words
1927  *
1928  * @param str Source string
1929  * @return List of const words
1930  *
1931  * @see elm_widget_stringlist_free()
1932  * @ingroup Widget
1933  */
1934 EAPI Eina_List *
1935 elm_widget_stringlist_get(const char *str)
1936 {
1937    Eina_List *list = NULL;
1938    const char *s, *b;
1939    if (!str) return NULL;
1940    for (b = s = str; 1; s++)
1941      {
1942         if ((*s == ' ') || (!*s))
1943           {
1944              char *t = malloc(s - b + 1);
1945              if (t)
1946                {
1947                   strncpy(t, b, s - b);
1948                   t[s - b] = 0;
1949                   list = eina_list_append(list, eina_stringshare_add(t));
1950                   free(t);
1951                }
1952              b = s + 1;
1953           }
1954         if (!*s) break;
1955      }
1956    return list;
1957 }
1958
1959 EAPI void
1960 elm_widget_stringlist_free(Eina_List *list)
1961 {
1962    const char *s;
1963    EINA_LIST_FREE(list, s) eina_stringshare_del(s);
1964 }
1965
1966 /**
1967  * @internal
1968  *
1969  * Allocate a new Elm_Widget_Item-derived structure.
1970  *
1971  * The goal of this structure is to provide common ground for actions
1972  * that a widget item have, such as the owner widget, callback to
1973  * notify deletion, data pointer and maybe more.
1974  *
1975  * @param widget the owner widget that holds this item, must be an elm_widget!
1976  * @param alloc_size any number greater than sizeof(Elm_Widget_Item) that will
1977  *        be used to allocate memory.
1978  *
1979  * @return allocated memory that is already zeroed out, or NULL on errors.
1980  *
1981  * @see elm_widget_item_new() convenience macro.
1982  * @see elm_widget_item_del() to release memory.
1983  * @ingroup Widget
1984  */
1985 EAPI Elm_Widget_Item *
1986 _elm_widget_item_new(Evas_Object *widget, size_t alloc_size)
1987 {
1988    if (!_elm_widget_is(widget))
1989      return NULL;
1990
1991    Elm_Widget_Item *item;
1992
1993    EINA_SAFETY_ON_TRUE_RETURN_VAL(alloc_size < sizeof(Elm_Widget_Item), NULL);
1994    EINA_SAFETY_ON_TRUE_RETURN_VAL(!_elm_widget_is(widget), NULL);
1995
1996    item = calloc(1, alloc_size);
1997    EINA_SAFETY_ON_NULL_RETURN_VAL(item, NULL);
1998
1999    EINA_MAGIC_SET(item, ELM_WIDGET_ITEM_MAGIC);
2000    item->widget = widget;
2001    return item;
2002 }
2003
2004 /**
2005  * @internal
2006  *
2007  * Releases widget item memory, calling back del_cb() if it exists.
2008  *
2009  * If there is a Elm_Widget_Item::del_cb, then it will be called prior
2010  * to memory release. Note that elm_widget_item_pre_notify_del() calls
2011  * this function and then unset it, thus being useful for 2 step
2012  * cleanup whenever the del_cb may use any of the data that must be
2013  * deleted from item.
2014  *
2015  * The Elm_Widget_Item::view will be deleted (evas_object_del()) if it
2016  * is presented!
2017  *
2018  * @param item a valid #Elm_Widget_Item to be deleted.
2019  * @see elm_widget_item_del() convenience macro.
2020  * @ingroup Widget
2021  */
2022 EAPI void
2023 _elm_widget_item_del(Elm_Widget_Item *item)
2024 {
2025    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2026
2027    if (item->del_cb)
2028      item->del_cb((void *)item->data, item->widget, item);
2029
2030    if (item->view)
2031      evas_object_del(item->view);
2032
2033    EINA_MAGIC_SET(item, EINA_MAGIC_NONE);
2034    free(item);
2035 }
2036
2037 /**
2038  * @internal
2039  *
2040  * Notify object will be deleted without actually deleting it.
2041  *
2042  * This function will callback Elm_Widget_Item::del_cb if it is set
2043  * and then unset it so it is not called twice (ie: from
2044  * elm_widget_item_del()).
2045  *
2046  * @param item a valid #Elm_Widget_Item to be notified
2047  * @see elm_widget_item_pre_notify_del() convenience macro.
2048  * @ingroup Widget
2049  */
2050 EAPI void
2051 _elm_widget_item_pre_notify_del(Elm_Widget_Item *item)
2052 {
2053    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2054    if (!item->del_cb) return;
2055    item->del_cb((void *)item->data, item->widget, item);
2056    item->del_cb = NULL;
2057 }
2058
2059 /**
2060  * @internal
2061  *
2062  * Set the function to notify when item is being deleted.
2063  *
2064  * This function will complain if there was a callback set already,
2065  * however it will set the new one.
2066  *
2067  * The callback will be called from elm_widget_item_pre_notify_del()
2068  * or elm_widget_item_del() will be called with:
2069  *   - data: the Elm_Widget_Item::data value.
2070  *   - obj: the Elm_Widget_Item::widget evas object.
2071  *   - event_info: the item being deleted.
2072  *
2073  * @param item a valid #Elm_Widget_Item to be notified
2074  * @see elm_widget_item_del_cb_set() convenience macro.
2075  * @ingroup Widget
2076  */
2077 EAPI void
2078 _elm_widget_item_del_cb_set(Elm_Widget_Item *item, Evas_Smart_Cb del_cb)
2079 {
2080    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2081
2082    if ((item->del_cb) && (item->del_cb != del_cb))
2083      WRN("You're replacing a previously set del_cb %p of item %p with %p",
2084          item->del_cb, item, del_cb);
2085
2086    item->del_cb = del_cb;
2087 }
2088
2089 /**
2090  * @internal
2091  *
2092  * Set user-data in this item.
2093  *
2094  * User data may be used to identify this item or just store any
2095  * application data. It is automatically given as the first parameter
2096  * of the deletion notify callback.
2097  *
2098  * @param item a valid #Elm_Widget_Item to store data in.
2099  * @param data user data to store.
2100  * @see elm_widget_item_del_cb_set() convenience macro.
2101  * @ingroup Widget
2102  */
2103 EAPI void
2104 _elm_widget_item_data_set(Elm_Widget_Item *item, const void *data)
2105 {
2106    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2107    if ((item->data) && (item->data != data))
2108      DBG("Replacing item %p data %p with %p", item, item->data, data);
2109    item->data = data;
2110 }
2111
2112 /**
2113  * @internal
2114  *
2115  * Retrieves user-data of this item.
2116  *
2117  * @param item a valid #Elm_Widget_Item to get data from.
2118  * @see elm_widget_item_data_set()
2119  * @ingroup Widget
2120  */
2121 EAPI void *
2122 _elm_widget_item_data_get(const Elm_Widget_Item *item)
2123 {
2124    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item, NULL);
2125    return (void *)item->data;
2126 }
2127
2128 typedef struct _Elm_Widget_Item_Tooltip Elm_Widget_Item_Tooltip;
2129
2130 struct _Elm_Widget_Item_Tooltip
2131 {
2132    Elm_Widget_Item             *item;
2133    Elm_Tooltip_Item_Content_Cb  func;
2134    Evas_Smart_Cb                del_cb;
2135    const void                  *data;
2136 };
2137
2138 static Evas_Object *
2139 _elm_widget_item_tooltip_label_create(void *data, Evas_Object *obj, void *item __UNUSED__)
2140 {
2141    Evas_Object *label = elm_label_add(obj);
2142    if (!label)
2143      return NULL;
2144    elm_object_style_set(label, "tooltip");
2145    elm_label_label_set(label, data);
2146    return label;
2147 }
2148
2149 static void
2150 _elm_widget_item_tooltip_label_del_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2151 {
2152    eina_stringshare_del(data);
2153 }
2154
2155 /**
2156  * @internal
2157  *
2158  * Set the text to be shown in the widget item.
2159  *
2160  * @param item Target item
2161  * @param text The text to set in the content
2162  *
2163  * Setup the text as tooltip to object. The item can have only one tooltip,
2164  * so any previous tooltip data is removed.
2165  *
2166  * @ingroup Widget
2167  */
2168 EAPI void
2169 _elm_widget_item_tooltip_text_set(Elm_Widget_Item *item, const char *text)
2170 {
2171    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2172    EINA_SAFETY_ON_NULL_RETURN(text);
2173
2174    text = eina_stringshare_add(text);
2175    _elm_widget_item_tooltip_content_cb_set
2176      (item, _elm_widget_item_tooltip_label_create, text,
2177       _elm_widget_item_tooltip_label_del_cb);
2178 }
2179
2180 static Evas_Object *
2181 _elm_widget_item_tooltip_create(void *data, Evas_Object *obj)
2182 {
2183    Elm_Widget_Item_Tooltip *wit = data;
2184    return wit->func((void *)wit->data, obj, wit->item);
2185 }
2186
2187 static void
2188 _elm_widget_item_tooltip_del_cb(void *data, Evas_Object *obj, void *event_info __UNUSED__)
2189 {
2190    Elm_Widget_Item_Tooltip *wit = data;
2191    if (wit->del_cb) wit->del_cb((void *)wit->data, obj, wit->item);
2192    free(wit);
2193 }
2194
2195 /**
2196  * @internal
2197  *
2198  * Set the content to be shown in the tooltip item
2199  *
2200  * Setup the tooltip to item. The item can have only one tooltip,
2201  * so any previous tooltip data is removed. @p func(with @p data) will
2202  * be called every time that need show the tooltip and it should
2203  * return a valid Evas_Object. This object is then managed fully by
2204  * tooltip system and is deleted when the tooltip is gone.
2205  *
2206  * @param item the widget item being attached a tooltip.
2207  * @param func the function used to create the tooltip contents.
2208  * @param data what to provide to @a func as callback data/context.
2209  * @param del_cb called when data is not needed anymore, either when
2210  *        another callback replaces @func, the tooltip is unset with
2211  *        elm_widget_item_tooltip_unset() or the owner @a item
2212  *        dies. This callback receives as the first parameter the
2213  *        given @a data, and @c event_info is the item.
2214  *
2215  * @ingroup Widget
2216  */
2217 EAPI void
2218 _elm_widget_item_tooltip_content_cb_set(Elm_Widget_Item *item, Elm_Tooltip_Item_Content_Cb func, const void *data, Evas_Smart_Cb del_cb)
2219 {
2220    Elm_Widget_Item_Tooltip *wit;
2221
2222    ELM_WIDGET_ITEM_CHECK_OR_GOTO(item, error_noitem);
2223
2224    if (!func)
2225      {
2226         _elm_widget_item_tooltip_unset(item);
2227         return;
2228      }
2229
2230    wit = ELM_NEW(Elm_Widget_Item_Tooltip);
2231    if (!wit) goto error;
2232    wit->item = item;
2233    wit->func = func;
2234    wit->data = data;
2235    wit->del_cb = del_cb;
2236
2237    elm_object_sub_tooltip_content_cb_set
2238      (item->view, item->widget, _elm_widget_item_tooltip_create, wit,
2239       _elm_widget_item_tooltip_del_cb);
2240
2241    return;
2242
2243  error_noitem:
2244    if (del_cb) del_cb((void *)data, NULL, item);
2245    return;
2246  error:
2247    if (del_cb) del_cb((void *)data, item->widget, item);
2248 }
2249
2250 /**
2251  * @internal
2252  *
2253  * Unset tooltip from item
2254  *
2255  * @param item widget item to remove previously set tooltip.
2256  *
2257  * Remove tooltip from item. The callback provided as del_cb to
2258  * elm_widget_item_tooltip_content_cb_set() will be called to notify
2259  * it is not used anymore.
2260  *
2261  * @see elm_widget_item_tooltip_content_cb_set()
2262  *
2263  * @ingroup Widget
2264  */
2265 EAPI void
2266 _elm_widget_item_tooltip_unset(Elm_Widget_Item *item)
2267 {
2268    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2269    elm_object_tooltip_unset(item->view);
2270 }
2271
2272 /**
2273  * @internal
2274  *
2275  * Sets a different style for this item tooltip.
2276  *
2277  * @note before you set a style you should define a tooltip with
2278  *       elm_widget_item_tooltip_content_cb_set() or
2279  *       elm_widget_item_tooltip_text_set()
2280  *
2281  * @param item widget item with tooltip already set.
2282  * @param style the theme style to use (default, transparent, ...)
2283  *
2284  * @ingroup Widget
2285  */
2286 EAPI void
2287 _elm_widget_item_tooltip_style_set(Elm_Widget_Item *item, const char *style)
2288 {
2289    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2290    elm_object_tooltip_style_set(item->view, style);
2291 }
2292
2293 /**
2294  * @internal
2295  *
2296  * Get the style for this item tooltip.
2297  *
2298  * @param item widget item with tooltip already set.
2299  * @return style the theme style in use, defaults to "default". If the
2300  *         object does not have a tooltip set, then NULL is returned.
2301  *
2302  * @ingroup Widget
2303  */
2304 EAPI const char *
2305 _elm_widget_item_tooltip_style_get(const Elm_Widget_Item *item)
2306 {
2307    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item, NULL);
2308    return elm_object_tooltip_style_get(item->view);
2309 }
2310
2311 EAPI void
2312 _elm_widget_item_cursor_set(Elm_Widget_Item *item, const char *cursor)
2313 {
2314    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2315    elm_object_sub_cursor_set(item->view, item->widget, cursor);
2316 }
2317
2318 EAPI const char *
2319 _elm_widget_item_cursor_get(const Elm_Widget_Item *item)
2320 {
2321    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item, NULL);
2322    return elm_object_cursor_get(item->view);
2323 }
2324
2325 EAPI void
2326 _elm_widget_item_cursor_unset(Elm_Widget_Item *item)
2327 {
2328    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2329    elm_object_cursor_unset(item->view);
2330 }
2331
2332 /**
2333  * @internal
2334  *
2335  * Sets a different style for this item cursor.
2336  *
2337  * @note before you set a style you should define a cursor with
2338  *       elm_widget_item_cursor_set()
2339  *
2340  * @param item widget item with cursor already set.
2341  * @param style the theme style to use (default, transparent, ...)
2342  *
2343  * @ingroup Widget
2344  */
2345 EAPI void
2346 _elm_widget_item_cursor_style_set(Elm_Widget_Item *item, const char *style)
2347 {
2348    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2349    elm_object_cursor_style_set(item->view, style);
2350 }
2351
2352 /**
2353  * @internal
2354  *
2355  * Get the style for this item cursor.
2356  *
2357  * @param item widget item with cursor already set.
2358  * @return style the theme style in use, defaults to "default". If the
2359  *         object does not have a cursor set, then NULL is returned.
2360  *
2361  * @ingroup Widget
2362  */
2363 EAPI const char *
2364 _elm_widget_item_cursor_style_get(const Elm_Widget_Item *item)
2365 {
2366    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item, NULL);
2367    return elm_object_cursor_style_get(item->view);
2368 }
2369
2370 /**
2371  * @internal
2372  *
2373  * Set if the cursor set should be searched on the theme or should use
2374  * the provided by the engine, only.
2375  *
2376  * @note before you set if should look on theme you should define a cursor
2377  * with elm_object_cursor_set(). By default it will only look for cursors
2378  * provided by the engine.
2379  *
2380  * @param item widget item with cursor already set.
2381  * @param engine_only boolean to define it cursors should be looked only
2382  * between the provided by the engine or searched on widget's theme as well.
2383  *
2384  * @ingroup Widget
2385  */
2386 EAPI void
2387 _elm_widget_item_cursor_engine_only_set(Elm_Widget_Item *item, Eina_Bool engine_only)
2388 {
2389    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2390    elm_object_cursor_engine_only_set(item->view, engine_only);
2391 }
2392
2393 /**
2394  * @internal
2395  *
2396  * Get the cursor engine only usage for this item cursor.
2397  *
2398  * @param item widget item with cursor already set.
2399  * @return engine_only boolean to define it cursors should be looked only
2400  * between the provided by the engine or searched on widget's theme as well. If
2401  *         the object does not have a cursor set, then EINA_FALSE is returned.
2402  *
2403  * @ingroup Widget
2404  */
2405 EAPI Eina_Bool
2406 _elm_widget_item_cursor_engine_only_get(const Elm_Widget_Item *item)
2407 {
2408    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item, EINA_FALSE);
2409    return elm_object_cursor_engine_only_get(item->view);
2410 }
2411
2412 // smart object funcs
2413 static void
2414 _smart_reconfigure(Smart_Data *sd)
2415 {
2416    if (sd->resize_obj)
2417      {
2418         evas_object_move(sd->resize_obj, sd->x, sd->y);
2419         evas_object_resize(sd->resize_obj, sd->w, sd->h);
2420      }
2421    if (sd->hover_obj)
2422      {
2423         evas_object_move(sd->hover_obj, sd->x, sd->y);
2424         evas_object_resize(sd->hover_obj, sd->w, sd->h);
2425      }
2426 }
2427
2428 static void
2429 _smart_add(Evas_Object *obj)
2430 {
2431    Smart_Data *sd;
2432
2433    sd = calloc(1, sizeof(Smart_Data));
2434    if (!sd) return;
2435    sd->obj = obj;
2436    sd->x = sd->y = sd->w = sd->h = 0;
2437    sd->can_focus = 1;
2438    evas_object_smart_data_set(obj, sd);
2439 }
2440
2441 static Evas_Object *
2442 _newest_focus_order_get(Evas_Object *obj, unsigned int *newest_focus_order, Eina_Bool can_focus_only)
2443 {
2444    const Eina_List *l;
2445    Evas_Object *child, *ret, *best;
2446    
2447    API_ENTRY return NULL;
2448    if (!evas_object_visible_get(obj)) return NULL;
2449    best = NULL;
2450    if (*newest_focus_order < sd->focus_order)
2451      {
2452         *newest_focus_order = sd->focus_order;
2453         best = obj;
2454      }
2455    EINA_LIST_FOREACH(sd->subobjs, l, child)
2456      {
2457         ret = _newest_focus_order_get(child, newest_focus_order, can_focus_only);
2458         if (!ret) continue;
2459         best = ret;
2460      }
2461    if (can_focus_only)
2462      {
2463         if ((!best) || (!elm_widget_can_focus_get(best)))
2464           return NULL;
2465      }
2466    return best;
2467 }
2468
2469 static void
2470 _if_focused_revert(Evas_Object *obj, Eina_Bool can_focus_only)
2471 {
2472    Evas_Object *top;
2473    Evas_Object *newest = NULL;
2474    unsigned int newest_focus_order = 0;
2475    
2476    INTERNAL_ENTRY;
2477
2478    if (!sd->focused) return;
2479    if (!sd->parent_obj) return;
2480
2481    top = elm_widget_top_get(sd->parent_obj);
2482    if (top)
2483      {
2484         newest = _newest_focus_order_get(top, &newest_focus_order, can_focus_only);
2485         if (newest)
2486           {
2487              elm_object_unfocus(newest);
2488              elm_object_focus(newest);
2489           }
2490      }
2491 }
2492
2493 static void
2494 _smart_del(Evas_Object *obj)
2495 {
2496    Evas_Object *sobj;
2497    Edje_Signal_Data *esd;
2498
2499    INTERNAL_ENTRY;
2500
2501    if (sd->del_pre_func) sd->del_pre_func(obj);
2502    if (sd->resize_obj)
2503      {
2504         sobj = sd->resize_obj;
2505         sd->resize_obj = NULL;
2506         evas_object_event_callback_del_full(sobj, EVAS_CALLBACK_DEL, _sub_obj_del, sd);
2507         evas_object_smart_callback_call(sd->obj, "sub-object-del", sobj);
2508         evas_object_del(sobj);
2509      }
2510    if (sd->hover_obj)
2511      {
2512         sobj = sd->hover_obj;
2513         sd->hover_obj = NULL;
2514         evas_object_event_callback_del_full(sobj, EVAS_CALLBACK_DEL, _sub_obj_del, sd);
2515         evas_object_smart_callback_call(sd->obj, "sub-object-del", sobj);
2516         evas_object_del(sobj);
2517      }
2518    EINA_LIST_FREE(sd->subobjs, sobj)
2519      {
2520         evas_object_event_callback_del_full(sobj, EVAS_CALLBACK_DEL, _sub_obj_del, sd);
2521         evas_object_smart_callback_call(sd->obj, "sub-object-del", sobj);
2522         evas_object_del(sobj);
2523      }
2524    eina_list_free(sd->tooltips); /* should be empty anyway */
2525    eina_list_free(sd->cursors); /* should be empty anyway */
2526    EINA_LIST_FREE(sd->edje_signals, esd)
2527      {
2528         eina_stringshare_del(esd->emission);
2529         eina_stringshare_del(esd->source);
2530         free(esd);
2531      }
2532    eina_list_free(sd->event_cb); /* should be empty anyway */
2533    if (sd->del_func) sd->del_func(obj);
2534    if (sd->style) eina_stringshare_del(sd->style);
2535    if (sd->type) eina_stringshare_del(sd->type);
2536    if (sd->theme) elm_theme_free(sd->theme);
2537    _if_focused_revert(obj, EINA_TRUE);
2538    free(sd);
2539 }
2540
2541 static void
2542 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
2543 {
2544    INTERNAL_ENTRY;
2545    sd->x = x;
2546    sd->y = y;
2547    _smart_reconfigure(sd);
2548 }
2549
2550 static void
2551 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
2552 {
2553    INTERNAL_ENTRY;
2554    sd->w = w;
2555    sd->h = h;
2556    _smart_reconfigure(sd);
2557 }
2558
2559 static void
2560 _smart_show(Evas_Object *obj)
2561 {
2562    Eina_List *list;
2563    Evas_Object *o;
2564    INTERNAL_ENTRY;
2565    if ((list = evas_object_smart_members_get(obj)))
2566      {
2567         EINA_LIST_FREE(list, o)
2568           {
2569              if (evas_object_data_get(o, "_elm_leaveme")) continue;
2570              evas_object_show(o);
2571           }
2572      }
2573 }
2574
2575 static void
2576 _smart_hide(Evas_Object *obj)
2577 {
2578    Eina_List *list;
2579    Evas_Object *o;
2580    INTERNAL_ENTRY;
2581    list = evas_object_smart_members_get(obj);
2582    EINA_LIST_FREE(list, o)
2583      {
2584         if (evas_object_data_get(o, "_elm_leaveme")) continue;
2585         evas_object_hide(o);
2586      }
2587 }
2588
2589 static void
2590 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
2591 {
2592    Eina_List *list;
2593    Evas_Object *o;
2594    INTERNAL_ENTRY;
2595    if ((list = evas_object_smart_members_get(obj)))
2596      {
2597         EINA_LIST_FREE(list, o)
2598           {
2599              if (evas_object_data_get(o, "_elm_leaveme")) continue;
2600              evas_object_color_set(o, r, g, b, a);
2601           }
2602      }
2603 }
2604
2605 static void
2606 _smart_clip_set(Evas_Object *obj, Evas_Object *clip)
2607 {
2608    Eina_List *list;
2609    Evas_Object *o;
2610    INTERNAL_ENTRY;
2611    if ((list = evas_object_smart_members_get(obj)))
2612      {
2613         EINA_LIST_FREE(list, o)
2614           {
2615              if (evas_object_data_get(o, "_elm_leaveme")) continue;
2616              evas_object_clip_set(o, clip);
2617           }
2618      }
2619 }
2620
2621 static void
2622 _smart_clip_unset(Evas_Object *obj)
2623 {
2624    Eina_List *list;
2625    Evas_Object *o;
2626    INTERNAL_ENTRY;
2627    if ((list = evas_object_smart_members_get(obj)))
2628      {
2629         EINA_LIST_FREE(list, o)
2630           {
2631              if (evas_object_data_get(o, "_elm_leaveme")) continue;
2632              evas_object_clip_unset(o);
2633           }
2634      }
2635 }
2636
2637 static void
2638 _smart_calculate(Evas_Object *obj)
2639 {
2640    INTERNAL_ENTRY;
2641    if (sd->changed_func) sd->changed_func(obj);
2642 }
2643
2644 /* never need to touch this */
2645 static void
2646 _smart_init(void)
2647 {
2648    if (_e_smart) return;
2649      {
2650         static const Evas_Smart_Class sc =
2651           {
2652              SMART_NAME,
2653              EVAS_SMART_CLASS_VERSION,
2654              _smart_add,
2655              _smart_del,
2656              _smart_move,
2657              _smart_resize,
2658              _smart_show,
2659              _smart_hide,
2660              _smart_color_set,
2661              _smart_clip_set,
2662              _smart_clip_unset,
2663              _smart_calculate,
2664              NULL,
2665              NULL,
2666              NULL,
2667              NULL,
2668              NULL,
2669              NULL
2670           };
2671         _e_smart = evas_smart_class_new(&sc);
2672      }
2673 }
2674
2675 /* happy debug functions */
2676 #ifdef ELM_DEBUG
2677 static void
2678 _sub_obj_tree_dump(const Evas_Object *o, int lvl)
2679 {
2680    int i;
2681
2682    for (i = 0; i < lvl*3; i++)
2683      putchar(' ');
2684
2685    if (_elm_widget_is(o))
2686      {
2687         Eina_List *l;
2688         Smart_Data *sd = evas_object_smart_data_get(o);
2689         printf("+ %s(%p)\n", sd->type, o);
2690         if (sd->resize_obj)
2691           _sub_obj_tree_dump(sd->resize_obj, lvl + 1);
2692         EINA_LIST_FOREACH(sd->subobjs, l, o)
2693           {
2694              if (o != sd->resize_obj)
2695                _sub_obj_tree_dump(o, lvl + 1);
2696           }
2697      }
2698    else
2699      printf("+ %s(%p)\n", evas_object_type_get(o), o);
2700 }
2701
2702 static void
2703 _sub_obj_tree_dot_dump(const Evas_Object *obj, FILE *output)
2704 {
2705    if (!_elm_widget_is(obj))
2706      return;
2707
2708    Smart_Data *sd = evas_object_smart_data_get(obj);
2709
2710    Eina_Bool visible = evas_object_visible_get(obj);
2711    Eina_Bool disabled = elm_widget_disabled_get(obj);
2712    Eina_Bool focused = elm_widget_focus_get(obj);
2713    Eina_Bool can_focus = elm_widget_can_focus_get(obj);
2714
2715    if (sd->parent_obj)
2716      {
2717         fprintf(output, "\"%p\" -- \"%p\" [ color=black", sd->parent_obj, obj);
2718
2719         if (focused)
2720           fprintf(output, ", style=bold");
2721
2722         if (!visible)
2723           fprintf(output, ", color=gray28");
2724
2725         fprintf(output, " ];\n");
2726      }
2727
2728    fprintf(output, "\"%p\" [ label = \"{%p|%s|%s|visible: %d|"
2729            "disabled: %d|focused: %d/%d|focus order:%d}\"", obj, obj, sd->type,
2730            evas_object_name_get(obj), visible, disabled, focused, can_focus,
2731            sd->focus_order);
2732
2733    if (focused)
2734         fprintf(output, ", style=bold");
2735
2736    if (!visible)
2737         fprintf(output, ", fontcolor=gray28");
2738
2739    if ((disabled) || (!visible))
2740         fprintf(output, ", color=gray");
2741
2742
2743    fprintf(output, " ];\n");
2744
2745    Eina_List *l;
2746    Evas_Object *o;
2747    EINA_LIST_FOREACH(sd->subobjs, l, o)
2748       _sub_obj_tree_dot_dump(o, output);
2749 }
2750 #endif
2751
2752 EAPI void
2753 elm_widget_tree_dump(const Evas_Object *top)
2754 {
2755 #ifdef ELM_DEBUG
2756    _sub_obj_tree_dump(top, 0);
2757 #else
2758    return;
2759    (void)top;
2760 #endif
2761 }
2762
2763 EAPI void
2764 elm_widget_tree_dot_dump(const Evas_Object *top, FILE *output)
2765 {
2766 #ifdef ELM_DEBUG
2767    if (!_elm_widget_is(top))
2768      return;
2769    fprintf(output, "graph "" { node [shape=record];\n");
2770    _sub_obj_tree_dot_dump(top, output);
2771    fprintf(output, "}\n");
2772 #else
2773    return;
2774    (void)top;
2775    (void)output;
2776 #endif
2777 }