[elm_widget] fix elm_widget_show_region_set problem
[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         if ((!parent_obj) || (!_elm_widget_is(parent_obj))) break;
1603         sd = evas_object_smart_data_get(parent_obj);
1604         if (!sd) break;
1605
1606         if ((!parent_obj) || (!sd) || (!_elm_widget_is(parent_obj))) break;
1607
1608         evas_object_geometry_get(parent_obj, &px, &py, NULL, NULL);
1609         evas_object_geometry_get(child_obj, &cx, &cy, NULL, NULL);
1610
1611         x += (cx - px);
1612         y += (cy - py);
1613         sd->rx = x;
1614         sd->ry = y;
1615         sd->rw = w;
1616         sd->rh = h;
1617
1618         if (sd->on_show_region_func)
1619           {
1620              sd->on_show_region_func(sd->on_show_region_data, parent_obj);
1621           }
1622      }
1623    while (parent_obj);
1624 }
1625
1626 EAPI void
1627 elm_widget_show_region_get(const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
1628 {
1629    API_ENTRY return;
1630    if (x) *x = sd->rx;
1631    if (y) *y = sd->ry;
1632    if (w) *w = sd->rw;
1633    if (h) *h = sd->rh;
1634 }
1635
1636 /**
1637  * @internal
1638  *
1639  * Get the focus region of the given widget.
1640  *
1641  * The focus region is the area of a widget that should brought into the
1642  * visible area when the widget is focused. Mostly used to show the part of
1643  * an entry where the cursor is, for example. The area returned is relative
1644  * to the object @p obj.
1645  * If the @p obj doesn't have the proper on_focus_region_hook set, this
1646  * function will return the full size of the object.
1647  *
1648  * @param obj The widget object
1649  * @param x Where to store the x coordinate of the area
1650  * @param y Where to store the y coordinate of the area
1651  * @param w Where to store the width of the area
1652  * @param h Where to store the height of the area
1653  *
1654  * @ingroup Widget
1655  */
1656 EAPI void
1657 elm_widget_focus_region_get(const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
1658 {
1659    Smart_Data *sd;
1660
1661    if (!obj) return;
1662
1663    sd = evas_object_smart_data_get(obj);
1664    if (!sd || !_elm_widget_is(obj) || !sd->on_focus_region_func)
1665      {
1666         evas_object_geometry_get(obj, NULL, NULL, w, h);
1667         if (x) *x = 0;
1668         if (y) *y = 0;
1669         return;
1670      }
1671    sd->on_focus_region_func(obj, x, y, w, h);
1672 }
1673
1674 EAPI void
1675 elm_widget_scroll_hold_push(Evas_Object *obj)
1676 {
1677    API_ENTRY return;
1678    sd->scroll_hold++;
1679    if (sd->scroll_hold == 1)
1680       evas_object_smart_callback_call(obj, "scroll-hold-on", obj);
1681    if (sd->parent_obj) elm_widget_scroll_hold_push(sd->parent_obj);
1682    // FIXME: on delete/reparent hold pop
1683 }
1684
1685 EAPI void
1686 elm_widget_scroll_hold_pop(Evas_Object *obj)
1687 {
1688    API_ENTRY return;
1689    sd->scroll_hold--;
1690    if (sd->scroll_hold < 0) sd->scroll_hold = 0;
1691    if (!sd->scroll_hold)
1692       evas_object_smart_callback_call(obj, "scroll-hold-off", obj);
1693    if (sd->parent_obj) elm_widget_scroll_hold_pop(sd->parent_obj);
1694 }
1695
1696 EAPI int
1697 elm_widget_scroll_hold_get(const Evas_Object *obj)
1698 {
1699    API_ENTRY return 0;
1700    return sd->scroll_hold;
1701 }
1702
1703 EAPI void
1704 elm_widget_scroll_freeze_push(Evas_Object *obj)
1705 {
1706    API_ENTRY return;
1707    sd->scroll_freeze++;
1708    if (sd->scroll_freeze == 1)
1709       evas_object_smart_callback_call(obj, "scroll-freeze-on", obj);
1710    if (sd->parent_obj) elm_widget_scroll_freeze_push(sd->parent_obj);
1711    // FIXME: on delete/reparent freeze pop
1712 }
1713
1714 EAPI void
1715 elm_widget_scroll_freeze_pop(Evas_Object *obj)
1716 {
1717    API_ENTRY return;
1718    sd->scroll_freeze--;
1719    if (sd->scroll_freeze < 0) sd->scroll_freeze = 0;
1720    if (!sd->scroll_freeze)
1721       evas_object_smart_callback_call(obj, "scroll-freeze-off", obj);
1722    if (sd->parent_obj) elm_widget_scroll_freeze_pop(sd->parent_obj);
1723 }
1724
1725 EAPI int
1726 elm_widget_scroll_freeze_get(const Evas_Object *obj)
1727 {
1728    API_ENTRY return 0;
1729    return sd->scroll_freeze;
1730 }
1731
1732 EAPI void
1733 elm_widget_scale_set(Evas_Object *obj, double scale)
1734 {
1735    API_ENTRY return;
1736    if (scale <= 0.0) scale = 0.0;
1737    if (sd->scale != scale)
1738      {
1739         sd->scale = scale;
1740         elm_widget_theme(obj);
1741      }
1742 }
1743
1744 EAPI double
1745 elm_widget_scale_get(const Evas_Object *obj)
1746 {
1747    API_ENTRY return 1.0;
1748    // FIXME: save walking up the tree by storing/caching parent scale
1749    if (sd->scale == 0.0)
1750      {
1751         if (sd->parent_obj)
1752            return elm_widget_scale_get(sd->parent_obj);
1753         else
1754            return 1.0;
1755      }
1756    return sd->scale;
1757 }
1758
1759 EAPI void
1760 elm_widget_theme_set(Evas_Object *obj, Elm_Theme *th)
1761 {
1762    API_ENTRY return;
1763    if (sd->theme != th)
1764      {
1765         if (sd->theme) elm_theme_free(sd->theme);
1766         sd->theme = th;
1767         if (th) th->ref++;
1768         elm_widget_theme(obj);
1769      }
1770 }
1771
1772 EAPI Elm_Theme *
1773 elm_widget_theme_get(const Evas_Object *obj)
1774 {
1775    API_ENTRY return NULL;
1776    if (!sd->theme)
1777      {
1778         if (sd->parent_obj)
1779            return elm_widget_theme_get(sd->parent_obj);
1780         else
1781            return NULL;
1782      }
1783    return sd->theme;
1784 }
1785
1786 EAPI void
1787 elm_widget_style_set(Evas_Object *obj, const char *style)
1788 {
1789    API_ENTRY return;
1790    
1791    if (eina_stringshare_replace(&sd->style, style))
1792       elm_widget_theme(obj);
1793 }
1794
1795 EAPI const char *
1796 elm_widget_style_get(const Evas_Object *obj)
1797 {
1798    API_ENTRY return NULL;
1799    if (sd->style) return sd->style;
1800    return "default";
1801 }
1802
1803 EAPI void
1804 elm_widget_type_set(Evas_Object *obj, const char *type)
1805 {
1806    API_ENTRY return;
1807    eina_stringshare_replace(&sd->type, type);
1808 }
1809
1810 EAPI const char *
1811 elm_widget_type_get(const Evas_Object *obj)
1812 {
1813    API_ENTRY return NULL;
1814    if (sd->type) return sd->type;
1815    return "";
1816 }
1817
1818 EAPI void
1819 elm_widget_tooltip_add(Evas_Object *obj, Elm_Tooltip *tt)
1820 {
1821    API_ENTRY return;
1822    sd->tooltips = eina_list_append(sd->tooltips, tt);
1823 }
1824
1825 EAPI void
1826 elm_widget_tooltip_del(Evas_Object *obj, Elm_Tooltip *tt)
1827 {
1828    API_ENTRY return;
1829    sd->tooltips = eina_list_remove(sd->tooltips, tt);
1830 }
1831
1832 EAPI void
1833 elm_widget_cursor_add(Evas_Object *obj, Elm_Cursor *cur)
1834 {
1835    API_ENTRY return;
1836    sd->cursors = eina_list_append(sd->cursors, cur);
1837 }
1838
1839 EAPI void
1840 elm_widget_cursor_del(Evas_Object *obj, Elm_Cursor *cur)
1841 {
1842    API_ENTRY return;
1843    sd->cursors = eina_list_remove(sd->cursors, cur);
1844 }
1845
1846 EAPI void
1847 elm_widget_drag_lock_x_set(Evas_Object *obj, Eina_Bool lock)
1848 {
1849    API_ENTRY return;
1850    if (sd->drag_x_locked == lock) return;
1851    sd->drag_x_locked = lock;
1852    if (sd->drag_x_locked) _propagate_x_drag_lock(obj, 1);
1853    else _propagate_x_drag_lock(obj, -1);
1854 }
1855
1856 EAPI void
1857 elm_widget_drag_lock_y_set(Evas_Object *obj, Eina_Bool lock)
1858 {
1859    API_ENTRY return;
1860    if (sd->drag_y_locked == lock) return;
1861    sd->drag_y_locked = lock;
1862    if (sd->drag_y_locked) _propagate_y_drag_lock(obj, 1);
1863    else _propagate_y_drag_lock(obj, -1);
1864 }
1865
1866 EAPI Eina_Bool
1867 elm_widget_drag_lock_x_get(const Evas_Object *obj)
1868 {
1869    API_ENTRY return EINA_FALSE;
1870    return sd->drag_x_locked;
1871 }
1872
1873 EAPI Eina_Bool
1874 elm_widget_drag_lock_y_get(const Evas_Object *obj)
1875 {
1876    API_ENTRY return EINA_FALSE;
1877    return sd->drag_y_locked;
1878 }
1879
1880 EAPI int
1881 elm_widget_drag_child_locked_x_get(const Evas_Object *obj)
1882 {
1883    API_ENTRY return 0;
1884    return sd->child_drag_x_locked;
1885 }
1886
1887 EAPI int
1888 elm_widget_drag_child_locked_y_get(const Evas_Object *obj)
1889 {
1890    API_ENTRY return 0;
1891    return sd->child_drag_y_locked;
1892 }
1893
1894 EAPI Eina_Bool
1895 elm_widget_theme_object_set(Evas_Object *obj, Evas_Object *edj, const char *wname, const char *welement, const char *wstyle)
1896 {
1897    API_ENTRY return EINA_FALSE;
1898    return _elm_theme_object_set(obj, edj, wname, welement, wstyle);
1899 }
1900
1901 EAPI Eina_Bool
1902 elm_widget_type_check(const Evas_Object *obj, const char *type)
1903 {
1904    const char *provided, *expected = "(unknown)";
1905    static int abort_on_warn = -1;
1906    provided = elm_widget_type_get(obj);
1907    if (EINA_LIKELY(provided == type)) return EINA_TRUE;
1908    if (type) expected = type;
1909    if ((!provided) || (!provided[0]))
1910      {
1911         provided = evas_object_type_get(obj);
1912         if ((!provided) || (!provided[0]))
1913            provided = "(unknown)";
1914      }
1915    ERR("Passing Object: %p, of type: '%s' when expecting type: '%s'", obj, provided, expected);
1916    if (abort_on_warn == -1)
1917      {
1918         if (getenv("ELM_ERROR_ABORT")) abort_on_warn = 1;
1919         else abort_on_warn = 0;
1920      }
1921    if (abort_on_warn == 1) abort();
1922    return EINA_FALSE;
1923 }
1924
1925 /**
1926  * @internal
1927  *
1928  * Split string in words
1929  *
1930  * @param str Source string
1931  * @return List of const words
1932  *
1933  * @see elm_widget_stringlist_free()
1934  * @ingroup Widget
1935  */
1936 EAPI Eina_List *
1937 elm_widget_stringlist_get(const char *str)
1938 {
1939    Eina_List *list = NULL;
1940    const char *s, *b;
1941    if (!str) return NULL;
1942    for (b = s = str; 1; s++)
1943      {
1944         if ((*s == ' ') || (!*s))
1945           {
1946              char *t = malloc(s - b + 1);
1947              if (t)
1948                {
1949                   strncpy(t, b, s - b);
1950                   t[s - b] = 0;
1951                   list = eina_list_append(list, eina_stringshare_add(t));
1952                   free(t);
1953                }
1954              b = s + 1;
1955           }
1956         if (!*s) break;
1957      }
1958    return list;
1959 }
1960
1961 EAPI void
1962 elm_widget_stringlist_free(Eina_List *list)
1963 {
1964    const char *s;
1965    EINA_LIST_FREE(list, s) eina_stringshare_del(s);
1966 }
1967
1968 /**
1969  * @internal
1970  *
1971  * Allocate a new Elm_Widget_Item-derived structure.
1972  *
1973  * The goal of this structure is to provide common ground for actions
1974  * that a widget item have, such as the owner widget, callback to
1975  * notify deletion, data pointer and maybe more.
1976  *
1977  * @param widget the owner widget that holds this item, must be an elm_widget!
1978  * @param alloc_size any number greater than sizeof(Elm_Widget_Item) that will
1979  *        be used to allocate memory.
1980  *
1981  * @return allocated memory that is already zeroed out, or NULL on errors.
1982  *
1983  * @see elm_widget_item_new() convenience macro.
1984  * @see elm_widget_item_del() to release memory.
1985  * @ingroup Widget
1986  */
1987 EAPI Elm_Widget_Item *
1988 _elm_widget_item_new(Evas_Object *widget, size_t alloc_size)
1989 {
1990    if (!_elm_widget_is(widget))
1991      return NULL;
1992
1993    Elm_Widget_Item *item;
1994
1995    EINA_SAFETY_ON_TRUE_RETURN_VAL(alloc_size < sizeof(Elm_Widget_Item), NULL);
1996    EINA_SAFETY_ON_TRUE_RETURN_VAL(!_elm_widget_is(widget), NULL);
1997
1998    item = calloc(1, alloc_size);
1999    EINA_SAFETY_ON_NULL_RETURN_VAL(item, NULL);
2000
2001    EINA_MAGIC_SET(item, ELM_WIDGET_ITEM_MAGIC);
2002    item->widget = widget;
2003    return item;
2004 }
2005
2006 /**
2007  * @internal
2008  *
2009  * Releases widget item memory, calling back del_cb() if it exists.
2010  *
2011  * If there is a Elm_Widget_Item::del_cb, then it will be called prior
2012  * to memory release. Note that elm_widget_item_pre_notify_del() calls
2013  * this function and then unset it, thus being useful for 2 step
2014  * cleanup whenever the del_cb may use any of the data that must be
2015  * deleted from item.
2016  *
2017  * The Elm_Widget_Item::view will be deleted (evas_object_del()) if it
2018  * is presented!
2019  *
2020  * @param item a valid #Elm_Widget_Item to be deleted.
2021  * @see elm_widget_item_del() convenience macro.
2022  * @ingroup Widget
2023  */
2024 EAPI void
2025 _elm_widget_item_del(Elm_Widget_Item *item)
2026 {
2027    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2028
2029    if (item->del_cb)
2030      item->del_cb((void *)item->data, item->widget, item);
2031
2032    if (item->view)
2033      evas_object_del(item->view);
2034
2035    EINA_MAGIC_SET(item, EINA_MAGIC_NONE);
2036    free(item);
2037 }
2038
2039 /**
2040  * @internal
2041  *
2042  * Notify object will be deleted without actually deleting it.
2043  *
2044  * This function will callback Elm_Widget_Item::del_cb if it is set
2045  * and then unset it so it is not called twice (ie: from
2046  * elm_widget_item_del()).
2047  *
2048  * @param item a valid #Elm_Widget_Item to be notified
2049  * @see elm_widget_item_pre_notify_del() convenience macro.
2050  * @ingroup Widget
2051  */
2052 EAPI void
2053 _elm_widget_item_pre_notify_del(Elm_Widget_Item *item)
2054 {
2055    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2056    if (!item->del_cb) return;
2057    item->del_cb((void *)item->data, item->widget, item);
2058    item->del_cb = NULL;
2059 }
2060
2061 /**
2062  * @internal
2063  *
2064  * Set the function to notify when item is being deleted.
2065  *
2066  * This function will complain if there was a callback set already,
2067  * however it will set the new one.
2068  *
2069  * The callback will be called from elm_widget_item_pre_notify_del()
2070  * or elm_widget_item_del() will be called with:
2071  *   - data: the Elm_Widget_Item::data value.
2072  *   - obj: the Elm_Widget_Item::widget evas object.
2073  *   - event_info: the item being deleted.
2074  *
2075  * @param item a valid #Elm_Widget_Item to be notified
2076  * @see elm_widget_item_del_cb_set() convenience macro.
2077  * @ingroup Widget
2078  */
2079 EAPI void
2080 _elm_widget_item_del_cb_set(Elm_Widget_Item *item, Evas_Smart_Cb del_cb)
2081 {
2082    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2083
2084    if ((item->del_cb) && (item->del_cb != del_cb))
2085      WRN("You're replacing a previously set del_cb %p of item %p with %p",
2086          item->del_cb, item, del_cb);
2087
2088    item->del_cb = del_cb;
2089 }
2090
2091 /**
2092  * @internal
2093  *
2094  * Set user-data in this item.
2095  *
2096  * User data may be used to identify this item or just store any
2097  * application data. It is automatically given as the first parameter
2098  * of the deletion notify callback.
2099  *
2100  * @param item a valid #Elm_Widget_Item to store data in.
2101  * @param data user data to store.
2102  * @see elm_widget_item_del_cb_set() convenience macro.
2103  * @ingroup Widget
2104  */
2105 EAPI void
2106 _elm_widget_item_data_set(Elm_Widget_Item *item, const void *data)
2107 {
2108    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2109    if ((item->data) && (item->data != data))
2110      DBG("Replacing item %p data %p with %p", item, item->data, data);
2111    item->data = data;
2112 }
2113
2114 /**
2115  * @internal
2116  *
2117  * Retrieves user-data of this item.
2118  *
2119  * @param item a valid #Elm_Widget_Item to get data from.
2120  * @see elm_widget_item_data_set()
2121  * @ingroup Widget
2122  */
2123 EAPI void *
2124 _elm_widget_item_data_get(const Elm_Widget_Item *item)
2125 {
2126    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item, NULL);
2127    return (void *)item->data;
2128 }
2129
2130 typedef struct _Elm_Widget_Item_Tooltip Elm_Widget_Item_Tooltip;
2131
2132 struct _Elm_Widget_Item_Tooltip
2133 {
2134    Elm_Widget_Item             *item;
2135    Elm_Tooltip_Item_Content_Cb  func;
2136    Evas_Smart_Cb                del_cb;
2137    const void                  *data;
2138 };
2139
2140 static Evas_Object *
2141 _elm_widget_item_tooltip_label_create(void *data, Evas_Object *obj, void *item __UNUSED__)
2142 {
2143    Evas_Object *label = elm_label_add(obj);
2144    if (!label)
2145      return NULL;
2146    elm_object_style_set(label, "tooltip");
2147    elm_label_label_set(label, data);
2148    return label;
2149 }
2150
2151 static void
2152 _elm_widget_item_tooltip_label_del_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
2153 {
2154    eina_stringshare_del(data);
2155 }
2156
2157 /**
2158  * @internal
2159  *
2160  * Set the text to be shown in the widget item.
2161  *
2162  * @param item Target item
2163  * @param text The text to set in the content
2164  *
2165  * Setup the text as tooltip to object. The item can have only one tooltip,
2166  * so any previous tooltip data is removed.
2167  *
2168  * @ingroup Widget
2169  */
2170 EAPI void
2171 _elm_widget_item_tooltip_text_set(Elm_Widget_Item *item, const char *text)
2172 {
2173    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2174    EINA_SAFETY_ON_NULL_RETURN(text);
2175
2176    text = eina_stringshare_add(text);
2177    _elm_widget_item_tooltip_content_cb_set
2178      (item, _elm_widget_item_tooltip_label_create, text,
2179       _elm_widget_item_tooltip_label_del_cb);
2180 }
2181
2182 static Evas_Object *
2183 _elm_widget_item_tooltip_create(void *data, Evas_Object *obj)
2184 {
2185    Elm_Widget_Item_Tooltip *wit = data;
2186    return wit->func((void *)wit->data, obj, wit->item);
2187 }
2188
2189 static void
2190 _elm_widget_item_tooltip_del_cb(void *data, Evas_Object *obj, void *event_info __UNUSED__)
2191 {
2192    Elm_Widget_Item_Tooltip *wit = data;
2193    if (wit->del_cb) wit->del_cb((void *)wit->data, obj, wit->item);
2194    free(wit);
2195 }
2196
2197 /**
2198  * @internal
2199  *
2200  * Set the content to be shown in the tooltip item
2201  *
2202  * Setup the tooltip to item. The item can have only one tooltip,
2203  * so any previous tooltip data is removed. @p func(with @p data) will
2204  * be called every time that need show the tooltip and it should
2205  * return a valid Evas_Object. This object is then managed fully by
2206  * tooltip system and is deleted when the tooltip is gone.
2207  *
2208  * @param item the widget item being attached a tooltip.
2209  * @param func the function used to create the tooltip contents.
2210  * @param data what to provide to @a func as callback data/context.
2211  * @param del_cb called when data is not needed anymore, either when
2212  *        another callback replaces @func, the tooltip is unset with
2213  *        elm_widget_item_tooltip_unset() or the owner @a item
2214  *        dies. This callback receives as the first parameter the
2215  *        given @a data, and @c event_info is the item.
2216  *
2217  * @ingroup Widget
2218  */
2219 EAPI void
2220 _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)
2221 {
2222    Elm_Widget_Item_Tooltip *wit;
2223
2224    ELM_WIDGET_ITEM_CHECK_OR_GOTO(item, error_noitem);
2225
2226    if (!func)
2227      {
2228         _elm_widget_item_tooltip_unset(item);
2229         return;
2230      }
2231
2232    wit = ELM_NEW(Elm_Widget_Item_Tooltip);
2233    if (!wit) goto error;
2234    wit->item = item;
2235    wit->func = func;
2236    wit->data = data;
2237    wit->del_cb = del_cb;
2238
2239    elm_object_sub_tooltip_content_cb_set
2240      (item->view, item->widget, _elm_widget_item_tooltip_create, wit,
2241       _elm_widget_item_tooltip_del_cb);
2242
2243    return;
2244
2245  error_noitem:
2246    if (del_cb) del_cb((void *)data, NULL, item);
2247    return;
2248  error:
2249    if (del_cb) del_cb((void *)data, item->widget, item);
2250 }
2251
2252 /**
2253  * @internal
2254  *
2255  * Unset tooltip from item
2256  *
2257  * @param item widget item to remove previously set tooltip.
2258  *
2259  * Remove tooltip from item. The callback provided as del_cb to
2260  * elm_widget_item_tooltip_content_cb_set() will be called to notify
2261  * it is not used anymore.
2262  *
2263  * @see elm_widget_item_tooltip_content_cb_set()
2264  *
2265  * @ingroup Widget
2266  */
2267 EAPI void
2268 _elm_widget_item_tooltip_unset(Elm_Widget_Item *item)
2269 {
2270    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2271    elm_object_tooltip_unset(item->view);
2272 }
2273
2274 /**
2275  * @internal
2276  *
2277  * Sets a different style for this item tooltip.
2278  *
2279  * @note before you set a style you should define a tooltip with
2280  *       elm_widget_item_tooltip_content_cb_set() or
2281  *       elm_widget_item_tooltip_text_set()
2282  *
2283  * @param item widget item with tooltip already set.
2284  * @param style the theme style to use (default, transparent, ...)
2285  *
2286  * @ingroup Widget
2287  */
2288 EAPI void
2289 _elm_widget_item_tooltip_style_set(Elm_Widget_Item *item, const char *style)
2290 {
2291    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2292    elm_object_tooltip_style_set(item->view, style);
2293 }
2294
2295 /**
2296  * @internal
2297  *
2298  * Get the style for this item tooltip.
2299  *
2300  * @param item widget item with tooltip already set.
2301  * @return style the theme style in use, defaults to "default". If the
2302  *         object does not have a tooltip set, then NULL is returned.
2303  *
2304  * @ingroup Widget
2305  */
2306 EAPI const char *
2307 _elm_widget_item_tooltip_style_get(const Elm_Widget_Item *item)
2308 {
2309    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item, NULL);
2310    return elm_object_tooltip_style_get(item->view);
2311 }
2312
2313 EAPI void
2314 _elm_widget_item_cursor_set(Elm_Widget_Item *item, const char *cursor)
2315 {
2316    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2317    elm_object_sub_cursor_set(item->view, item->widget, cursor);
2318 }
2319
2320 EAPI const char *
2321 _elm_widget_item_cursor_get(const Elm_Widget_Item *item)
2322 {
2323    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item, NULL);
2324    return elm_object_cursor_get(item->view);
2325 }
2326
2327 EAPI void
2328 _elm_widget_item_cursor_unset(Elm_Widget_Item *item)
2329 {
2330    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2331    elm_object_cursor_unset(item->view);
2332 }
2333
2334 /**
2335  * @internal
2336  *
2337  * Sets a different style for this item cursor.
2338  *
2339  * @note before you set a style you should define a cursor with
2340  *       elm_widget_item_cursor_set()
2341  *
2342  * @param item widget item with cursor already set.
2343  * @param style the theme style to use (default, transparent, ...)
2344  *
2345  * @ingroup Widget
2346  */
2347 EAPI void
2348 _elm_widget_item_cursor_style_set(Elm_Widget_Item *item, const char *style)
2349 {
2350    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2351    elm_object_cursor_style_set(item->view, style);
2352 }
2353
2354 /**
2355  * @internal
2356  *
2357  * Get the style for this item cursor.
2358  *
2359  * @param item widget item with cursor already set.
2360  * @return style the theme style in use, defaults to "default". If the
2361  *         object does not have a cursor set, then NULL is returned.
2362  *
2363  * @ingroup Widget
2364  */
2365 EAPI const char *
2366 _elm_widget_item_cursor_style_get(const Elm_Widget_Item *item)
2367 {
2368    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item, NULL);
2369    return elm_object_cursor_style_get(item->view);
2370 }
2371
2372 /**
2373  * @internal
2374  *
2375  * Set if the cursor set should be searched on the theme or should use
2376  * the provided by the engine, only.
2377  *
2378  * @note before you set if should look on theme you should define a cursor
2379  * with elm_object_cursor_set(). By default it will only look for cursors
2380  * provided by the engine.
2381  *
2382  * @param item widget item with cursor already set.
2383  * @param engine_only boolean to define it cursors should be looked only
2384  * between the provided by the engine or searched on widget's theme as well.
2385  *
2386  * @ingroup Widget
2387  */
2388 EAPI void
2389 _elm_widget_item_cursor_engine_only_set(Elm_Widget_Item *item, Eina_Bool engine_only)
2390 {
2391    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item);
2392    elm_object_cursor_engine_only_set(item->view, engine_only);
2393 }
2394
2395 /**
2396  * @internal
2397  *
2398  * Get the cursor engine only usage for this item cursor.
2399  *
2400  * @param item widget item with cursor already set.
2401  * @return engine_only boolean to define it cursors should be looked only
2402  * between the provided by the engine or searched on widget's theme as well. If
2403  *         the object does not have a cursor set, then EINA_FALSE is returned.
2404  *
2405  * @ingroup Widget
2406  */
2407 EAPI Eina_Bool
2408 _elm_widget_item_cursor_engine_only_get(const Elm_Widget_Item *item)
2409 {
2410    ELM_WIDGET_ITEM_CHECK_OR_RETURN(item, EINA_FALSE);
2411    return elm_object_cursor_engine_only_get(item->view);
2412 }
2413
2414 // smart object funcs
2415 static void
2416 _smart_reconfigure(Smart_Data *sd)
2417 {
2418    if (sd->resize_obj)
2419      {
2420         evas_object_move(sd->resize_obj, sd->x, sd->y);
2421         evas_object_resize(sd->resize_obj, sd->w, sd->h);
2422      }
2423    if (sd->hover_obj)
2424      {
2425         evas_object_move(sd->hover_obj, sd->x, sd->y);
2426         evas_object_resize(sd->hover_obj, sd->w, sd->h);
2427      }
2428 }
2429
2430 static void
2431 _smart_add(Evas_Object *obj)
2432 {
2433    Smart_Data *sd;
2434
2435    sd = calloc(1, sizeof(Smart_Data));
2436    if (!sd) return;
2437    sd->obj = obj;
2438    sd->x = sd->y = sd->w = sd->h = 0;
2439    sd->can_focus = 1;
2440    evas_object_smart_data_set(obj, sd);
2441 }
2442
2443 static Evas_Object *
2444 _newest_focus_order_get(Evas_Object *obj, unsigned int *newest_focus_order, Eina_Bool can_focus_only)
2445 {
2446    const Eina_List *l;
2447    Evas_Object *child, *ret, *best;
2448    
2449    API_ENTRY return NULL;
2450    if (!evas_object_visible_get(obj)) return NULL;
2451    best = NULL;
2452    if (*newest_focus_order < sd->focus_order)
2453      {
2454         *newest_focus_order = sd->focus_order;
2455         best = obj;
2456      }
2457    EINA_LIST_FOREACH(sd->subobjs, l, child)
2458      {
2459         ret = _newest_focus_order_get(child, newest_focus_order, can_focus_only);
2460         if (!ret) continue;
2461         best = ret;
2462      }
2463    if (can_focus_only)
2464      {
2465         if ((!best) || (!elm_widget_can_focus_get(best)))
2466           return NULL;
2467      }
2468    return best;
2469 }
2470
2471 static void
2472 _if_focused_revert(Evas_Object *obj, Eina_Bool can_focus_only)
2473 {
2474    Evas_Object *top;
2475    Evas_Object *newest = NULL;
2476    unsigned int newest_focus_order = 0;
2477    
2478    INTERNAL_ENTRY;
2479
2480    if (!sd->focused) return;
2481    if (!sd->parent_obj) return;
2482
2483    top = elm_widget_top_get(sd->parent_obj);
2484    if (top)
2485      {
2486         newest = _newest_focus_order_get(top, &newest_focus_order, can_focus_only);
2487         if (newest)
2488           {
2489              elm_object_unfocus(newest);
2490              elm_object_focus(newest);
2491           }
2492      }
2493 }
2494
2495 static void
2496 _smart_del(Evas_Object *obj)
2497 {
2498    Evas_Object *sobj;
2499    Edje_Signal_Data *esd;
2500
2501    INTERNAL_ENTRY;
2502
2503    if (sd->del_pre_func) sd->del_pre_func(obj);
2504    if (sd->resize_obj)
2505      {
2506         sobj = sd->resize_obj;
2507         sd->resize_obj = NULL;
2508         evas_object_event_callback_del_full(sobj, EVAS_CALLBACK_DEL, _sub_obj_del, sd);
2509         evas_object_smart_callback_call(sd->obj, "sub-object-del", sobj);
2510         evas_object_del(sobj);
2511      }
2512    if (sd->hover_obj)
2513      {
2514         sobj = sd->hover_obj;
2515         sd->hover_obj = NULL;
2516         evas_object_event_callback_del_full(sobj, EVAS_CALLBACK_DEL, _sub_obj_del, sd);
2517         evas_object_smart_callback_call(sd->obj, "sub-object-del", sobj);
2518         evas_object_del(sobj);
2519      }
2520    EINA_LIST_FREE(sd->subobjs, sobj)
2521      {
2522         evas_object_event_callback_del_full(sobj, EVAS_CALLBACK_DEL, _sub_obj_del, sd);
2523         evas_object_smart_callback_call(sd->obj, "sub-object-del", sobj);
2524         evas_object_del(sobj);
2525      }
2526    eina_list_free(sd->tooltips); /* should be empty anyway */
2527    eina_list_free(sd->cursors); /* should be empty anyway */
2528    EINA_LIST_FREE(sd->edje_signals, esd)
2529      {
2530         eina_stringshare_del(esd->emission);
2531         eina_stringshare_del(esd->source);
2532         free(esd);
2533      }
2534    eina_list_free(sd->event_cb); /* should be empty anyway */
2535    if (sd->del_func) sd->del_func(obj);
2536    if (sd->style) eina_stringshare_del(sd->style);
2537    if (sd->type) eina_stringshare_del(sd->type);
2538    if (sd->theme) elm_theme_free(sd->theme);
2539    _if_focused_revert(obj, EINA_TRUE);
2540    free(sd);
2541 }
2542
2543 static void
2544 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
2545 {
2546    INTERNAL_ENTRY;
2547    sd->x = x;
2548    sd->y = y;
2549    _smart_reconfigure(sd);
2550 }
2551
2552 static void
2553 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
2554 {
2555    INTERNAL_ENTRY;
2556    sd->w = w;
2557    sd->h = h;
2558    _smart_reconfigure(sd);
2559 }
2560
2561 static void
2562 _smart_show(Evas_Object *obj)
2563 {
2564    Eina_List *list;
2565    Evas_Object *o;
2566    INTERNAL_ENTRY;
2567    if ((list = evas_object_smart_members_get(obj)))
2568      {
2569         EINA_LIST_FREE(list, o)
2570           {
2571              if (evas_object_data_get(o, "_elm_leaveme")) continue;
2572              evas_object_show(o);
2573           }
2574      }
2575 }
2576
2577 static void
2578 _smart_hide(Evas_Object *obj)
2579 {
2580    Eina_List *list;
2581    Evas_Object *o;
2582    INTERNAL_ENTRY;
2583    list = evas_object_smart_members_get(obj);
2584    EINA_LIST_FREE(list, o)
2585      {
2586         if (evas_object_data_get(o, "_elm_leaveme")) continue;
2587         evas_object_hide(o);
2588      }
2589 }
2590
2591 static void
2592 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
2593 {
2594    Eina_List *list;
2595    Evas_Object *o;
2596    INTERNAL_ENTRY;
2597    if ((list = evas_object_smart_members_get(obj)))
2598      {
2599         EINA_LIST_FREE(list, o)
2600           {
2601              if (evas_object_data_get(o, "_elm_leaveme")) continue;
2602              evas_object_color_set(o, r, g, b, a);
2603           }
2604      }
2605 }
2606
2607 static void
2608 _smart_clip_set(Evas_Object *obj, Evas_Object *clip)
2609 {
2610    Eina_List *list;
2611    Evas_Object *o;
2612    INTERNAL_ENTRY;
2613    if ((list = evas_object_smart_members_get(obj)))
2614      {
2615         EINA_LIST_FREE(list, o)
2616           {
2617              if (evas_object_data_get(o, "_elm_leaveme")) continue;
2618              evas_object_clip_set(o, clip);
2619           }
2620      }
2621 }
2622
2623 static void
2624 _smart_clip_unset(Evas_Object *obj)
2625 {
2626    Eina_List *list;
2627    Evas_Object *o;
2628    INTERNAL_ENTRY;
2629    if ((list = evas_object_smart_members_get(obj)))
2630      {
2631         EINA_LIST_FREE(list, o)
2632           {
2633              if (evas_object_data_get(o, "_elm_leaveme")) continue;
2634              evas_object_clip_unset(o);
2635           }
2636      }
2637 }
2638
2639 static void
2640 _smart_calculate(Evas_Object *obj)
2641 {
2642    INTERNAL_ENTRY;
2643    if (sd->changed_func) sd->changed_func(obj);
2644 }
2645
2646 /* never need to touch this */
2647 static void
2648 _smart_init(void)
2649 {
2650    if (_e_smart) return;
2651      {
2652         static const Evas_Smart_Class sc =
2653           {
2654              SMART_NAME,
2655              EVAS_SMART_CLASS_VERSION,
2656              _smart_add,
2657              _smart_del,
2658              _smart_move,
2659              _smart_resize,
2660              _smart_show,
2661              _smart_hide,
2662              _smart_color_set,
2663              _smart_clip_set,
2664              _smart_clip_unset,
2665              _smart_calculate,
2666              NULL,
2667              NULL,
2668              NULL,
2669              NULL,
2670              NULL,
2671              NULL
2672           };
2673         _e_smart = evas_smart_class_new(&sc);
2674      }
2675 }
2676
2677 /* happy debug functions */
2678 #ifdef ELM_DEBUG
2679 static void
2680 _sub_obj_tree_dump(const Evas_Object *o, int lvl)
2681 {
2682    int i;
2683
2684    for (i = 0; i < lvl*3; i++)
2685      putchar(' ');
2686
2687    if (_elm_widget_is(o))
2688      {
2689         Eina_List *l;
2690         Smart_Data *sd = evas_object_smart_data_get(o);
2691         printf("+ %s(%p)\n", sd->type, o);
2692         if (sd->resize_obj)
2693           _sub_obj_tree_dump(sd->resize_obj, lvl + 1);
2694         EINA_LIST_FOREACH(sd->subobjs, l, o)
2695           {
2696              if (o != sd->resize_obj)
2697                _sub_obj_tree_dump(o, lvl + 1);
2698           }
2699      }
2700    else
2701      printf("+ %s(%p)\n", evas_object_type_get(o), o);
2702 }
2703
2704 static void
2705 _sub_obj_tree_dot_dump(const Evas_Object *obj, FILE *output)
2706 {
2707    if (!_elm_widget_is(obj))
2708      return;
2709
2710    Smart_Data *sd = evas_object_smart_data_get(obj);
2711
2712    Eina_Bool visible = evas_object_visible_get(obj);
2713    Eina_Bool disabled = elm_widget_disabled_get(obj);
2714    Eina_Bool focused = elm_widget_focus_get(obj);
2715    Eina_Bool can_focus = elm_widget_can_focus_get(obj);
2716
2717    if (sd->parent_obj)
2718      {
2719         fprintf(output, "\"%p\" -- \"%p\" [ color=black", sd->parent_obj, obj);
2720
2721         if (focused)
2722           fprintf(output, ", style=bold");
2723
2724         if (!visible)
2725           fprintf(output, ", color=gray28");
2726
2727         fprintf(output, " ];\n");
2728      }
2729
2730    fprintf(output, "\"%p\" [ label = \"{%p|%s|%s|visible: %d|"
2731            "disabled: %d|focused: %d/%d|focus order:%d}\"", obj, obj, sd->type,
2732            evas_object_name_get(obj), visible, disabled, focused, can_focus,
2733            sd->focus_order);
2734
2735    if (focused)
2736         fprintf(output, ", style=bold");
2737
2738    if (!visible)
2739         fprintf(output, ", fontcolor=gray28");
2740
2741    if ((disabled) || (!visible))
2742         fprintf(output, ", color=gray");
2743
2744
2745    fprintf(output, " ];\n");
2746
2747    Eina_List *l;
2748    Evas_Object *o;
2749    EINA_LIST_FOREACH(sd->subobjs, l, o)
2750       _sub_obj_tree_dot_dump(o, output);
2751 }
2752 #endif
2753
2754 EAPI void
2755 elm_widget_tree_dump(const Evas_Object *top)
2756 {
2757 #ifdef ELM_DEBUG
2758    _sub_obj_tree_dump(top, 0);
2759 #else
2760    return;
2761    (void)top;
2762 #endif
2763 }
2764
2765 EAPI void
2766 elm_widget_tree_dot_dump(const Evas_Object *top, FILE *output)
2767 {
2768 #ifdef ELM_DEBUG
2769    if (!_elm_widget_is(top))
2770      return;
2771    fprintf(output, "graph "" { node [shape=record];\n");
2772    _sub_obj_tree_dot_dump(top, output);
2773    fprintf(output, "}\n");
2774 #else
2775    return;
2776    (void)top;
2777    (void)output;
2778 #endif
2779 }