elementry / entry, menu, notify, list, gengrid, actionslider, image, icon, anchorview...
[framework/uifw/elementary.git] / src / lib / elm_menu.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Menu Menu
6  *
7  * A menu is a list of items displayed above the window. Each item can
8  * have a sub-menu. The menu object can be used to display a menu on right
9  * click, in a toolbar, anywhere.
10  *
11  * Signals that you can add callbacks for are:
12  *
13  * "clicked" - the user clicked the empty space in the menu to dismiss. event_info is NULL.
14  */
15
16 typedef struct _Widget_Data Widget_Data;
17
18 struct _Elm_Menu_Item
19 {
20    Elm_Widget_Item base;
21    Elm_Menu_Item *parent;
22    Evas_Object *icon;
23    const char *icon_str;
24    const char *label;
25    Evas_Smart_Cb func;
26
27    struct {
28       Evas_Object *hv, *bx, *location;
29       Eina_List *items;
30       Eina_Bool open : 1;
31    } submenu;
32
33    Eina_Bool separator : 1;
34    Eina_Bool disabled : 1;
35    Eina_Bool selected: 1;
36 };
37
38 struct _Widget_Data
39 {
40    Evas_Object *hv, *bx, *location, *parent, *obj;
41    Eina_List *items;
42    Evas_Coord xloc, yloc;
43 };
44
45 static const char *widtype = NULL;
46 static void _del_hook(Evas_Object *obj);
47 static void _theme_hook(Evas_Object *obj);
48 static void _sizing_eval(Evas_Object *obj);
49 static void _submenu_sizing_eval(Elm_Menu_Item *parent);
50 static void _item_sizing_eval(Elm_Menu_Item *item);
51 static void _submenu_hide(Elm_Menu_Item *item);
52 static void _submenu_open(void *data, Evas_Object *obj, const char *emission, const char *source);
53 static void _parent_resize(void *data, Evas *e, Evas_Object *obj, void *event_info);
54 static void _parent_del(void *data, Evas *e, Evas_Object *obj, void *event_info);
55 static void _menu_hide(void *data, Evas_Object *obj, void *event_info);
56
57 static void
58 _del_item(Elm_Menu_Item *item)
59 {
60    Elm_Menu_Item *child;
61
62    elm_widget_item_pre_notify_del(item);
63
64    EINA_LIST_FREE(item->submenu.items, child)
65      _del_item(child);
66
67    if (item->label) eina_stringshare_del(item->label);
68    if (item->submenu.hv) evas_object_del(item->submenu.hv);
69    if (item->submenu.location) evas_object_del(item->submenu.location);
70    if (item->icon_str) eina_stringshare_del(item->icon_str);
71    elm_widget_item_del(item);
72 }
73
74 static void
75 _del_pre_hook(Evas_Object *obj)
76 {
77    Elm_Menu_Item *item;
78    Widget_Data *wd = elm_widget_data_get(obj);
79    if (!wd) return;
80
81    evas_object_event_callback_del_full(wd->parent, EVAS_CALLBACK_RESIZE, _parent_resize, obj);
82    evas_object_event_callback_del_full(wd->parent, EVAS_CALLBACK_DEL, _parent_del, wd);
83
84    EINA_LIST_FREE(wd->items, item)
85       _del_item(item);
86
87    if (wd->hv) evas_object_del(wd->hv);
88    if (wd->location) evas_object_del(wd->location);
89 }
90
91 static void
92 _del_hook(Evas_Object *obj)
93 {
94    Widget_Data *wd = elm_widget_data_get(obj);
95    free(wd);
96 }
97
98 static void
99 _theme_hook(Evas_Object *obj)
100 {
101    Eina_List *l, *_l, *_ll, *ll = NULL;
102    Elm_Menu_Item *item;
103    Widget_Data *wd = elm_widget_data_get(obj);
104    if (!wd) return;
105    _elm_widget_mirrored_reload(obj);
106    ll = eina_list_append(ll, wd->items);
107    EINA_LIST_FOREACH(ll, _ll, l)
108      {
109         EINA_LIST_FOREACH(l, _l, item)
110           {
111              edje_object_mirrored_set(item->base.view, elm_widget_mirrored_get(obj));
112              ll = eina_list_append(ll, item->submenu.items);
113              if (item->separator)
114                _elm_theme_object_set(obj, item->base.view, "menu", "separator",
115                                      elm_widget_style_get(obj));
116              else if (item->submenu.bx)
117                {
118                   _elm_theme_object_set
119                      (obj, item->base.view, "menu", "item_with_submenu",
120                       elm_widget_style_get(obj));
121                   elm_menu_item_label_set(item, item->label);
122                   elm_menu_item_icon_set(item, item->icon_str);
123                }
124              else
125                {
126                   _elm_theme_object_set(obj, item->base.view, "menu", "item",
127                                         elm_widget_style_get(obj));
128                   elm_menu_item_label_set(item, item->label);
129                   elm_menu_item_icon_set(item, item->icon_str);
130                }
131              if (item->disabled)
132                edje_object_signal_emit
133                   (item->base.view, "elm,state,disabled", "elm");
134              else
135                edje_object_signal_emit
136                   (item->base.view, "elm,state,enabled", "elm");
137              edje_object_message_signal_process(item->base.view);
138              edje_object_scale_set(item->base.view, elm_widget_scale_get(obj) *
139                                    _elm_config->scale);
140           }
141      }
142    _sizing_eval(obj);
143 }
144
145 static void
146 _sizing_eval(Evas_Object *obj)
147 {
148    Eina_List *l;
149    Elm_Menu_Item *item;
150    Evas_Coord x_p, y_p, w_p, h_p, x2, y2, w2, h2, bx, by, bw, bh;
151    Widget_Data *wd = elm_widget_data_get(obj);
152    if ((!wd) || (!wd->parent)) return;
153    EINA_LIST_FOREACH(wd->items,l,item) _item_sizing_eval(item);
154    evas_object_geometry_get(wd->location, &x_p, &y_p, &w_p, &h_p);
155    evas_object_geometry_get(wd->parent, &x2, &y2, &w2, &h2);
156    evas_object_geometry_get(wd->bx, &bx, &by, &bw, &bh);
157
158    x_p = wd->xloc;
159    y_p = wd->yloc;
160
161    if (elm_widget_mirrored_get(obj))
162      x_p -= w_p;
163
164    if (x_p+bw > x2+w2) x_p -= x_p+bw - (x2+w2);
165    if (x_p < x2) x_p += x2 - x_p;
166
167    if (y_p+h_p+bh > y2+h2) y_p -= y_p+h_p+bh - (y2+h2);
168    if (y_p < y2) y_p += y2 - y_p;
169
170
171    evas_object_move(wd->location, x_p, y_p);
172    evas_object_resize(wd->location, bw, h_p);
173    evas_object_size_hint_min_set(wd->location, bw, h_p);
174    evas_object_size_hint_max_set(wd->location, bw, h_p);
175    elm_hover_target_set(wd->hv, wd->location);
176
177    EINA_LIST_FOREACH(wd->items,l,item)
178      {
179         if (item->submenu.open) _submenu_sizing_eval(item);
180      }
181 }
182
183 static void
184 _submenu_sizing_eval(Elm_Menu_Item *parent)
185 {
186    Eina_List *l;
187    Elm_Menu_Item *item;
188    Evas_Coord x_p, y_p, w_p, h_p, x2, y2, w2, h2, bx, by, bw, bh, px, py, pw, ph;
189    Widget_Data *wd = elm_widget_data_get(parent->base.widget);
190    if (!wd) return;
191    EINA_LIST_FOREACH(parent->submenu.items, l, item) _item_sizing_eval(item);
192    evas_object_geometry_get(parent->submenu.location, &x_p, &y_p, &w_p, &h_p);
193    evas_object_geometry_get(parent->base.view, &x2, &y2, &w2, &h2);
194    evas_object_geometry_get(parent->submenu.bx, &bx, &by, &bw, &bh);
195    evas_object_geometry_get(wd->parent, &px, &py, &pw, &ph);
196
197    x_p = x2+w2;
198    y_p = y2;
199
200    /* If it overflows on the right, adjust the x */
201    if ((x_p + bw > px + pw) || elm_widget_mirrored_get(parent->base.widget))
202      x_p = x2-bw;
203
204    /* If it overflows on the left, adjust the x - usually only happens
205     * with an RTL interface */
206    if (x_p < px)
207      x_p = x2 + w2;
208
209    /* If after all the adjustments it still overflows, fix it */
210    if (x_p + bw > px + pw)
211      x_p = x2-bw;
212
213    if (y_p+bh > py+ph)
214      y_p -= y_p+bh - (py+ph);
215    if (y_p < py)
216      y_p += y_p - y_p;
217
218    evas_object_move(parent->submenu.location, x_p, y_p);
219    evas_object_resize(parent->submenu.location, bw, h_p);
220    evas_object_size_hint_min_set(parent->submenu.location, bw, h_p);
221    evas_object_size_hint_max_set(parent->submenu.location, bw, h_p);
222    elm_hover_target_set(parent->submenu.hv, parent->submenu.location);
223
224    EINA_LIST_FOREACH(parent->submenu.items, l, item)
225      {
226         if (item->submenu.open)
227           _submenu_sizing_eval(item);
228      }
229 }
230
231 static void
232 _item_sizing_eval(Elm_Menu_Item *item)
233 {
234    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
235    if (!item->separator)
236      elm_coords_finger_size_adjust(1, &minw, 1, &minh);
237    edje_object_size_min_restricted_calc(item->base.view, &minw, &minh, minw, minh);
238    if (!item->separator)
239      elm_coords_finger_size_adjust(1, &minw, 1, &minh);
240    evas_object_size_hint_min_set(item->base.view, minw, minh);
241    evas_object_size_hint_max_set(item->base.view, maxw, maxh);
242 }
243
244 static void
245 _menu_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
246 {
247    _sizing_eval(data);
248 }
249
250 static void
251 _parent_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
252 {
253    _sizing_eval(data);
254 }
255
256 static void
257 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
258 {
259    Widget_Data *wd = data;
260    evas_object_event_callback_del_full(obj, EVAS_CALLBACK_RESIZE, _parent_resize, wd->obj);
261    wd->parent = NULL;
262 }
263
264 static void
265 _item_move_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
266 {
267    Elm_Menu_Item *item = data;
268    if (item->submenu.open) _submenu_sizing_eval(item);
269 }
270
271 static void
272 _hover_clicked_cb(void *data, Evas_Object *obj, void *event_info)
273 {
274    _menu_hide(data, obj, event_info);
275    evas_object_smart_callback_call(data, "clicked", NULL);
276 }
277
278 static void
279 _menu_hide(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
280 {
281    Eina_List *l;
282    Elm_Menu_Item *item2;
283    Widget_Data *wd = elm_widget_data_get(data);
284    if (!wd) return;
285    evas_object_hide(wd->hv);
286    evas_object_hide(data);
287
288    EINA_LIST_FOREACH(wd->items, l, item2)
289      {
290         if (item2->submenu.open) _submenu_hide(item2);
291      }
292 }
293
294 static void
295 _submenu_hide(Elm_Menu_Item *item)
296 {
297    Eina_List *l;
298    Elm_Menu_Item *item2;
299    evas_object_hide(item->submenu.hv);
300    item->submenu.open = EINA_FALSE;
301    EINA_LIST_FOREACH(item->submenu.items, l, item2)
302      {
303         if (item2->submenu.open) _submenu_hide(item2);
304      }
305 }
306
307 static void
308 _menu_item_select(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
309 {
310    Elm_Menu_Item *item = data;
311    if (item->submenu.items)
312      {
313         if (!item->submenu.open) _submenu_open(item, NULL, NULL, NULL);
314         else _submenu_hide(item);
315      }
316    else
317      _menu_hide(item->base.widget, NULL, NULL);
318
319    if (item->func) item->func((void *)(item->base.data), item->base.widget, item);
320 }
321
322 static void
323 _menu_item_activate(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
324 {
325    Eina_List *l;
326    Elm_Menu_Item *item2;
327    Elm_Menu_Item *item = data;
328    item->selected = 1;
329    if (item->parent)
330      {
331         EINA_LIST_FOREACH(item->parent->submenu.items, l, item2)
332           {
333              if (item2 != item) elm_menu_item_selected_set(item2, 0);
334           }
335      }
336    else
337      {
338         Widget_Data *wd = elm_widget_data_get(item->base.widget);
339         EINA_LIST_FOREACH(wd->items, l, item2)
340           {
341              if (item2 != item) elm_menu_item_selected_set(item2, 0);
342           }
343      }
344 }
345
346 static void
347 _menu_item_inactivate(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
348 {
349    Elm_Menu_Item *item = data;
350    item->selected = 0;
351    if (item->submenu.open) _submenu_hide(item);
352 }
353
354 static void
355 _submenu_open(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
356 {
357    Elm_Menu_Item *item = data;
358    item->submenu.open = EINA_TRUE;
359    evas_object_show(item->submenu.hv);
360    _sizing_eval(item->base.widget);
361 }
362
363 static void
364 _show(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
365 {
366    Widget_Data *wd = elm_widget_data_get(data);
367    if (!wd) return;
368    evas_object_show(wd->hv);
369 }
370
371 static void
372 _item_obj_create(Elm_Menu_Item *item)
373 {
374    Widget_Data *wd = elm_widget_data_get(item->base.widget);
375    if (!wd) return;
376    item->base.view = edje_object_add(evas_object_evas_get(wd->bx));
377    edje_object_mirrored_set(item->base.view, elm_widget_mirrored_get(item->base.widget));
378    evas_object_size_hint_weight_set(item->base.view, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
379    evas_object_size_hint_fill_set(item->base.view, EVAS_HINT_FILL, EVAS_HINT_FILL);
380    _elm_theme_object_set(item->base.widget, item->base.view, "menu", "item",  elm_widget_style_get(item->base.widget));
381    edje_object_signal_callback_add(item->base.view, "elm,action,click", "",
382                                    _menu_item_select, item);
383    edje_object_signal_callback_add(item->base.view, "elm,action,activate", "",
384                                    _menu_item_activate, item);
385    edje_object_signal_callback_add(item->base.view, "elm,action,inactivate", "",
386                                    _menu_item_inactivate, item);
387    evas_object_show(item->base.view);
388 }
389
390 static void
391 _item_separator_obj_create(Elm_Menu_Item *item)
392 {
393    Widget_Data *wd = elm_widget_data_get(item->base.widget);
394    if (!wd) return;
395    item->base.view = edje_object_add(evas_object_evas_get(wd->bx));
396    evas_object_size_hint_weight_set(item->base.view, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
397    evas_object_size_hint_fill_set(item->base.view, EVAS_HINT_FILL, EVAS_HINT_FILL);
398    _elm_theme_object_set(item->base.widget, item->base.view, "menu", "separator",  elm_widget_style_get(item->base.widget));
399    edje_object_signal_callback_add(item->base.view, "elm,action,activate", "",
400                                    _menu_item_activate, item);
401    evas_object_show(item->base.view);
402 }
403
404 static void
405 _item_submenu_obj_create(Elm_Menu_Item *item)
406 {
407    Widget_Data *wd = elm_widget_data_get(item->base.widget);
408    if (!wd) return;
409    item->submenu.location = elm_icon_add(wd->bx);
410    item->submenu.hv = elm_hover_add(wd->bx);
411    elm_widget_mirrored_set(item->submenu.hv, EINA_FALSE);
412    elm_hover_target_set(item->submenu.hv, item->submenu.location);
413    elm_hover_parent_set(item->submenu.hv, wd->parent);
414    elm_object_style_set(item->submenu.hv, "submenu");
415
416    item->submenu.bx = elm_box_add(wd->bx);
417    elm_widget_mirrored_set(item->submenu.bx, EINA_FALSE);
418    evas_object_size_hint_weight_set(item->submenu.bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
419    evas_object_show(item->submenu.bx);
420    elm_hover_content_set(item->submenu.hv, elm_hover_best_content_location_get(item->submenu.hv, ELM_HOVER_AXIS_VERTICAL), item->submenu.bx);
421
422    edje_object_mirrored_set(item->base.view, elm_widget_mirrored_get(item->base.widget));
423    _elm_theme_object_set(item->base.widget, item->base.view, "menu", "item_with_submenu",  elm_widget_style_get(item->base.widget));
424    elm_menu_item_label_set(item, item->label);
425    elm_menu_item_icon_set(item, item->icon_str);
426
427    edje_object_signal_callback_add(item->base.view, "elm,action,open", "",
428                                    _submenu_open, item);
429    evas_object_event_callback_add(item->base.view, EVAS_CALLBACK_MOVE, _item_move_resize, item);
430    evas_object_event_callback_add(item->base.view, EVAS_CALLBACK_RESIZE, _item_move_resize, item);
431
432    evas_object_event_callback_add(item->submenu.bx, EVAS_CALLBACK_RESIZE, _menu_resize, item->base.widget);
433 }
434
435 /**
436  * Add a new menu to the parent
437  *
438  * @param parent The parent object.
439  * @return The new object or NULL if it cannot be created.
440  *
441  * @ingroup Menu
442  */
443 EAPI Evas_Object *
444 elm_menu_add(Evas_Object *parent)
445 {
446    Evas_Object *obj;
447    Evas *e;
448    Widget_Data *wd;
449
450    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
451
452    ELM_SET_WIDTYPE(widtype, "menu");
453    elm_widget_type_set(obj, "menu");
454    elm_widget_sub_object_add(parent, obj);
455    elm_widget_data_set(obj, wd);
456    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
457    elm_widget_del_hook_set(obj, _del_hook);
458    elm_widget_theme_hook_set(obj, _theme_hook);
459    elm_widget_can_focus_set(obj, EINA_FALSE);
460
461    wd->location = elm_icon_add(obj);
462    wd->parent = parent;
463    wd->obj = obj;
464
465    wd->hv = elm_hover_add(obj);
466    elm_widget_mirrored_set(wd->hv, EINA_FALSE);
467    elm_hover_parent_set(wd->hv, parent);
468    elm_hover_target_set(wd->hv, wd->location);
469    elm_object_style_set(wd->hv, "menu");
470    evas_object_smart_callback_add(wd->hv, "clicked", _hover_clicked_cb, obj);
471
472    wd->bx = elm_box_add(obj);
473    elm_widget_mirrored_set(wd->bx, EINA_FALSE);
474    evas_object_size_hint_weight_set(wd->bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
475    evas_object_show(wd->bx);
476    elm_hover_content_set(wd->hv, elm_hover_best_content_location_get(wd->hv, ELM_HOVER_AXIS_VERTICAL), wd->bx);
477
478    evas_object_event_callback_add(wd->parent, EVAS_CALLBACK_RESIZE, _parent_resize, wd->obj);
479    evas_object_event_callback_add(wd->parent, EVAS_CALLBACK_DEL, _parent_del, wd);
480
481    evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW, _show, obj);
482
483    evas_object_event_callback_add(wd->bx, EVAS_CALLBACK_RESIZE, _menu_resize, obj);
484
485    _sizing_eval(obj);
486    return obj;
487 }
488
489 /**
490  * Set the parent
491  *
492  * @param obj The menu object.
493  * @param parent The new parent.
494  *
495  * @ingroup Menu
496  */
497 EAPI void
498 elm_menu_parent_set(Evas_Object *obj, Evas_Object *parent)
499 {
500    Eina_List *l, *_l, *_ll, *ll = NULL;
501    Elm_Menu_Item *item;
502    ELM_CHECK_WIDTYPE(obj, widtype);
503    Widget_Data *wd = elm_widget_data_get(obj);
504    if (!wd) return;
505
506    if (wd->parent == parent) return;
507    if (wd->parent)
508      {
509         evas_object_event_callback_del_full(wd->parent, EVAS_CALLBACK_RESIZE, _parent_resize, wd->obj);
510         evas_object_event_callback_del_full(wd->parent, EVAS_CALLBACK_DEL, _parent_del, wd);
511      }
512    wd->parent = parent;
513    if (wd->parent)
514      {
515         evas_object_event_callback_add(wd->parent, EVAS_CALLBACK_RESIZE, _parent_resize, wd->obj);
516         evas_object_event_callback_add(wd->parent, EVAS_CALLBACK_DEL, _parent_del, wd);
517      }
518    elm_hover_parent_set(wd->hv, parent);
519
520    ll = eina_list_append(ll, wd->items);
521    EINA_LIST_FOREACH(ll, _ll, l)
522      {
523         EINA_LIST_FOREACH(l, _l, item)
524           {
525              if (item->submenu.hv)
526                {
527                   elm_hover_parent_set(item->submenu.hv, parent);
528                   ll = eina_list_append(ll, item->submenu.items);
529                }
530           }
531      }
532    _sizing_eval(obj);
533 }
534
535 /**
536  * Get the parent
537  *
538  * @param obj The menu object.
539  * @return The parent.
540  *
541  * @ingroup Menu
542  */
543 EAPI Evas_Object *
544 elm_menu_parent_get(const Evas_Object *obj)
545 {
546    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
547    Widget_Data *wd = elm_widget_data_get(obj);
548    if (!wd) return NULL;
549    return wd->parent;
550 }
551
552 /**
553  * Move the menu to a new position
554  *
555  * @param obj The menu object.
556  * @param x The new position.
557  * @param y The new position.
558  *
559  * @ingroup Menu
560  */
561 EAPI void
562 elm_menu_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
563 {
564    ELM_CHECK_WIDTYPE(obj, widtype);
565    Widget_Data *wd = elm_widget_data_get(obj);
566    if (!wd) return;
567    wd->xloc = x;
568    wd->yloc = y;
569    _sizing_eval(obj);
570 }
571
572 /**
573  * Close a opened menu
574  *
575  * @param obj the menu object
576  * @return void
577  *
578  * @ingroup Menu
579  */
580 EAPI void
581 elm_menu_close(Evas_Object *obj)
582 {
583    ELM_CHECK_WIDTYPE(obj, widtype);
584    Widget_Data *wd = elm_widget_data_get(obj);
585    _menu_hide(obj, wd->hv, NULL);
586 }
587
588 /**
589  * Get the Evas_Object of an Elm_Menu_Item
590  *
591  * @param item The menu item object.
592  *
593  * @ingroup Menu
594  */
595 EAPI Evas_Object *
596 elm_menu_item_object_get(const Elm_Menu_Item *item)
597 {
598    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
599    return item->base.view;
600 }
601
602 static void
603 _item_clone(Evas_Object *obj, Elm_Menu_Item *parent, Elm_Menu_Item *item)
604 {
605    Elm_Menu_Item *new_item, *subitem;
606    Eina_List *iter;
607
608    if (item->separator)
609      new_item = elm_menu_item_separator_add(obj, parent);
610    else
611      new_item = elm_menu_item_add(obj, parent, item->icon_str, item->label, item->func, item->base.data);
612    elm_menu_item_disabled_set(new_item, item->disabled);
613
614    EINA_LIST_FOREACH(item->submenu.items, iter, subitem)
615       _item_clone(obj, new_item, subitem);
616 }
617
618 void
619 elm_menu_clone(Evas_Object *from_menu, Evas_Object *to_menu, Elm_Menu_Item *parent)
620 {
621    ELM_CHECK_WIDTYPE(from_menu, widtype);
622    ELM_CHECK_WIDTYPE(to_menu, widtype);
623    Widget_Data *from_wd = elm_widget_data_get(from_menu);
624    Eina_List *iter;
625    Elm_Menu_Item *item;
626
627    if (!from_wd) return;
628    EINA_LIST_FOREACH(from_wd->items, iter, item)
629       _item_clone(to_menu, parent, item);
630 }
631
632 /**
633  * Add an item at the end
634  *
635  * @param obj The menu object.
636  * @param icon A icon display on the item. The icon will be destryed by the menu.
637  * @param label The label of the item.
638  * @param func Function called when the user select the item.
639  * @param data Data sent by the callback.
640  * @return Returns the new item.
641  *
642  * @ingroup Menu
643  */
644 EAPI Elm_Menu_Item *
645 elm_menu_item_add(Evas_Object *obj, Elm_Menu_Item *parent, const char *icon, const char *label, Evas_Smart_Cb func, const void *data)
646 {
647    Elm_Menu_Item *subitem;
648    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
649    Widget_Data *wd = elm_widget_data_get(obj);
650    Evas_Object *icon_obj;
651
652    if (!wd) return NULL;
653    icon_obj = elm_icon_add(obj);
654    if (!icon_obj) return NULL;
655    subitem = elm_widget_item_new(obj, Elm_Menu_Item);
656    if (!subitem)
657      {
658         evas_object_del(icon_obj);
659         return NULL;
660      }
661    subitem->base.data = data;
662    subitem->func = func;
663    subitem->parent = parent;
664    subitem->icon = icon_obj;
665
666    _item_obj_create(subitem);
667    elm_menu_item_label_set(subitem, label);
668
669    elm_widget_sub_object_add(subitem->base.widget, subitem->icon);
670    edje_object_part_swallow(subitem->base.view, "elm.swallow.content", subitem->icon);
671    if (icon) elm_menu_item_icon_set(subitem, icon);
672
673    if (parent)
674      {
675         if (!parent->submenu.bx) _item_submenu_obj_create(parent);
676         elm_box_pack_end(parent->submenu.bx, subitem->base.view);
677         parent->submenu.items = eina_list_append(parent->submenu.items, subitem);
678      }
679    else
680      {
681         elm_box_pack_end(wd->bx, subitem->base.view);
682         wd->items = eina_list_append(wd->items, subitem);
683      }
684
685    _sizing_eval(obj);
686    return subitem;
687 }
688
689 /**
690  * Set the label of a menu item
691  *
692  * @param item The menu item object.
693  * @param label The label to set for @p item
694  *
695  * @ingroup Menu
696  */
697 EAPI void
698 elm_menu_item_label_set(Elm_Menu_Item *item, const char *label)
699 {
700    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
701    eina_stringshare_replace(&item->label, label);
702
703    if (label)
704      edje_object_signal_emit(item->base.view, "elm,state,text,visible", "elm");
705    else
706      edje_object_signal_emit(item->base.view, "elm,state,text,hidden", "elm");
707
708    edje_object_message_signal_process(item->base.view);
709    edje_object_part_text_set(item->base.view, "elm.text", label);
710    _sizing_eval(item->base.widget);
711 }
712
713 /**
714  * Get the label of a menu item
715  *
716  * @param item The menu item object.
717  * @return The label of @p item
718  *
719  * @ingroup Menu
720  */
721 EAPI const char *
722 elm_menu_item_label_get(const Elm_Menu_Item *item)
723 {
724    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
725    return item->label;
726 }
727
728 /**
729  * Set the icon of a menu item
730  *
731  * Once the icon object is set, a previously set one will be deleted.
732  *
733  * @param item The menu item object.
734  * @param icon The icon object to set for @p item
735  *
736  * @ingroup Menu
737  */
738 EAPI void
739 elm_menu_item_icon_set(Elm_Menu_Item *item, const char *icon)
740 {
741    char icon_tmp[512];
742    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
743    EINA_SAFETY_ON_NULL_RETURN(icon);
744    if (!*icon) return;
745    if ((item->icon_str) && (!strcmp(item->icon_str, icon))) return;
746    if ((snprintf(icon_tmp, sizeof(icon_tmp), "menu/%s", icon) > 0) &&
747        (elm_icon_standard_set(item->icon, icon_tmp)))
748      {
749         eina_stringshare_replace(&item->icon_str, icon);
750         edje_object_signal_emit(item->base.view, "elm,state,icon,visible", "elm");
751      }
752    else
753      edje_object_signal_emit(item->base.view, "elm,state,icon,hidden", "elm");
754    edje_object_message_signal_process(item->base.view);
755    _sizing_eval(item->base.widget);
756 }
757
758 /**
759  * Set the disabled state of @p item.
760  *
761  * @param item The menu item object.
762  * @param disabled The enabled/disabled state of the item
763  *
764  * @ingroup Menu
765  */
766 EAPI void
767 elm_menu_item_disabled_set(Elm_Menu_Item *item, Eina_Bool disabled)
768 {
769    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
770    if (disabled == item->disabled) return;
771    item->disabled = disabled;
772    if (disabled)
773      {
774         edje_object_signal_emit(item->base.view, "elm,state,disabled", "elm");
775         if (item->submenu.open) _submenu_hide(item);
776      }
777    else
778      edje_object_signal_emit(item->base.view, "elm,state,enabled", "elm");
779    edje_object_message_signal_process(item->base.view);
780 }
781
782 /**
783  * Get the disabled state of @p item.
784  *
785  * @param item The menu item object.
786  * @return The enabled/disabled state of the item
787  *
788  * @ingroup Menu
789  */
790 EAPI Eina_Bool
791 elm_menu_item_disabled_get(const Elm_Menu_Item *item)
792 {
793    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, EINA_FALSE);
794    return item->disabled;
795 }
796
797 /**
798  * Add a separator item to menu @p obj under @p parent.
799  *
800  * @param obj The menu object
801  * @param parent The item to add the separator under
802  *
803  * @return The created item or NULL on failure
804  *
805  * @ingroup Menu
806  */
807 EAPI Elm_Menu_Item *
808 elm_menu_item_separator_add(Evas_Object *obj, Elm_Menu_Item *parent)
809 {
810    Elm_Menu_Item *subitem;
811    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
812    Widget_Data *wd = elm_widget_data_get(obj);
813    if (!wd) return NULL;
814    /* don't add a separator as the first item */
815    if (!wd->items) return NULL;
816    /* don't allow adding more than one separator in a row */
817    if (parent) subitem = eina_list_last(parent->submenu.items)->data;
818    else subitem = eina_list_last(wd->items)->data;
819    if (subitem->separator) return NULL;
820
821    subitem = elm_widget_item_new(obj, Elm_Menu_Item);
822    if (!subitem) return NULL;
823    subitem->base.widget = obj;
824    subitem->separator = 1;
825    _item_separator_obj_create(subitem);
826    if (!parent)
827      {
828         elm_box_pack_end(wd->bx, subitem->base.view);
829         wd->items = eina_list_append(wd->items, subitem);
830      }
831    else
832      {
833         if (!parent->submenu.bx) _item_submenu_obj_create(parent);
834         elm_box_pack_end(parent->submenu.bx, subitem->base.view);
835         parent->submenu.items = eina_list_append(parent->submenu.items, subitem);
836      }
837    _sizing_eval(obj);
838    return subitem;
839 }
840
841 /**
842  * Get the icon object from a menu item
843  *
844  * @param item The menu item object
845  * @return The icon object or NULL if there's no icon
846  *
847  * @ingroup Menu
848  */
849 EAPI const Evas_Object *
850 elm_menu_item_object_icon_get(const Elm_Menu_Item *item)
851 {
852    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
853    return (const Evas_Object *)item->icon;
854 }
855
856 /**
857  * Get the string representation from the icon of a menu item
858  *
859  * @param item The menu item object.
860  * @return The string representation of @p item's icon or NULL
861  *
862  * @ingroup Menu
863  */
864 EAPI const char *
865 elm_menu_item_icon_get(const Elm_Menu_Item *item)
866 {
867    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
868    return item->icon_str;
869 }
870
871 /**
872  * Returns whether @p item is a separator.
873  *
874  * @param item The item to check
875  * @return If true, @p item is a separator
876  *
877  * @ingroup Menu
878  */
879 EAPI Eina_Bool
880 elm_menu_item_is_separator(Elm_Menu_Item *item)
881 {
882    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, EINA_FALSE);
883    return item->separator;
884 }
885
886 /**
887  * Deletes an item from the menu.
888  *
889  * @param item The item to delete.
890  *
891  * @ingroup Menu
892  */
893 EAPI void
894 elm_menu_item_del(Elm_Menu_Item *item)
895 {
896    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
897    Elm_Menu_Item *_item;
898
899    elm_widget_item_pre_notify_del(item);
900
901    EINA_LIST_FREE(item->submenu.items, _item) elm_menu_item_del(_item);
902    if (item->label) eina_stringshare_del(item->label);
903    if (item->icon) evas_object_del(item->icon);
904    if (item->submenu.hv) evas_object_del(item->submenu.hv);
905    if (item->submenu.location) evas_object_del(item->submenu.location);
906
907    if (item->parent)
908      item->parent->submenu.items = eina_list_remove(item->parent->submenu.items, item);
909    else
910      {
911         Widget_Data *wd = elm_widget_data_get(item->base.widget);
912         wd->items = eina_list_remove(wd->items, item);
913      }
914
915    elm_widget_item_del(item);
916 }
917
918 /**
919  * Set the function called when a menu item is freed.
920  *
921  * @param item The item to set the callback on
922  * @param func The function called
923  *
924  * @ingroup Menu
925  */
926 EAPI void
927 elm_menu_item_del_cb_set(Elm_Menu_Item *item, Evas_Smart_Cb func)
928 {
929    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
930    elm_widget_item_del_cb_set(item, func);
931 }
932
933 /**
934  * Returns the data associated with menu item @p item.
935  *
936  * @param item The item
937  * @return The data associated with @p item
938  *
939  * @ingroup Menu
940  */
941 EAPI void *
942 elm_menu_item_data_get(const Elm_Menu_Item *item)
943 {
944    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
945    return elm_widget_item_data_get(item);
946 }
947
948 /**
949  * Sets the data to be associated with menu item @p item.
950  *
951  * @param item The item
952  * @param data The data to be associated with @p item
953  *
954  * @ingroup Menu
955  */
956 EAPI void
957 elm_menu_item_data_set(Elm_Menu_Item *item, const void *data)
958 {
959    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
960    elm_widget_item_data_set(item, data);
961 }
962
963 /**
964  * Returns a list of @p item's subitems.
965  *
966  * @param item The item
967  * @return An Eina_List* of @p item's subitems
968  *
969  * @ingroup Menu
970  */
971 EAPI const Eina_List *
972 elm_menu_item_subitems_get(const Elm_Menu_Item *item)
973 {
974    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
975    return item->submenu.items;
976 }
977
978 /**
979  * Returns a list of @p item's items.
980  *
981  * @param obj The menu object
982  * @return An Eina_List* of @p item's items
983  *
984  * @ingroup Menu
985  */
986 EAPI const Eina_List *
987 elm_menu_items_get(const Evas_Object * obj)
988 {
989    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
990    Widget_Data *wd = elm_widget_data_get(obj);
991    return wd->items;
992 }
993
994 /**
995  * Set the selected state of @p item.
996  *
997  * @param item The menu item object.
998  * @param selected The selected/unselected state of the item
999  *
1000  * @ingroup Menu
1001  */
1002 EAPI void
1003 elm_menu_item_selected_set(Elm_Menu_Item *item, Eina_Bool selected)
1004 {
1005    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
1006    if (selected == item->selected) return;
1007    item->selected = selected;
1008    if (selected)
1009      {
1010         edje_object_signal_emit(item->base.view, "elm,state,selected", "elm");
1011         _menu_item_activate(item, NULL, NULL, NULL);
1012      }
1013    else
1014      {
1015         edje_object_signal_emit(item->base.view, "elm,state,unselected", "elm");
1016         _menu_item_inactivate(item, NULL, NULL, NULL);
1017      }
1018    edje_object_message_signal_process(item->base.view);
1019 }
1020
1021 /**
1022  * Get the selected state of @p item.
1023  *
1024  * @param item The menu item object.
1025  * @return The selected/unselected state of the item
1026  *
1027  * @ingroup Menu
1028  */
1029 EAPI Eina_Bool
1030 elm_menu_item_selected_get(const Elm_Menu_Item *item)
1031 {
1032    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, EINA_FALSE);
1033    return item->selected;
1034 }
1035
1036 /**
1037  * Get the previous item in the menu.
1038  *
1039  * @param item The menu item object.
1040  * @return The item before it, or NULL if none
1041  *
1042  * @ingroup Menu
1043  */
1044 EAPI const Elm_Menu_Item *
1045 elm_menu_item_prev_get(const Elm_Menu_Item *it)
1046 {
1047    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it, NULL);
1048    if (it->parent)
1049      {
1050         Eina_List *l = eina_list_data_find_list(it->parent->submenu.items, it);
1051         l = eina_list_prev(l);
1052         if (!l) return NULL;
1053         return l->data;
1054      }
1055    else
1056      {
1057         Widget_Data *wd = elm_widget_data_get(it->base.widget);
1058         if (!wd | !wd->items) return NULL;
1059         Eina_List *l = eina_list_data_find_list(wd->items, it);
1060         l = eina_list_prev(l);
1061         if (!l) return NULL;
1062         return l->data;
1063      }
1064    return NULL;
1065 }
1066
1067 /**
1068  * Get the next item in the menu.
1069  *
1070  * @param item The menu item object.
1071  * @return The item after it, or NULL if none
1072  *
1073  * @ingroup Menu
1074  */
1075 EAPI const Elm_Menu_Item *
1076 elm_menu_item_next_get(const Elm_Menu_Item *it)
1077 {
1078    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it, NULL);
1079    if (it->parent)
1080      {
1081         Eina_List *l = eina_list_data_find_list(it->parent->submenu.items, it);
1082         l = eina_list_next(l);
1083         if (!l) return NULL;
1084         return l->data;
1085      }
1086    else
1087      {
1088         Widget_Data *wd = elm_widget_data_get(it->base.widget);
1089         if (!wd | !wd->items) return NULL;
1090         Eina_List *l = eina_list_data_find_list(wd->items, it);
1091         l = eina_list_next(l);
1092         if (!l) return NULL;
1093         return l->data;
1094      }
1095    return NULL;
1096 }
1097
1098 /**
1099  * Get the first item in the menu
1100  *
1101  * @param obj The menu object
1102  * @return The first item, or NULL if none
1103  *
1104  * @ingroup Menu
1105  */
1106 EAPI const Elm_Menu_Item *
1107 elm_menu_first_item_get(const Evas_Object * obj)
1108 {
1109    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1110    Widget_Data *wd = elm_widget_data_get(obj);
1111    if (!wd) return NULL;
1112    if (wd->items) return wd->items->data;
1113    return NULL;
1114 }
1115
1116 /**
1117  * Get the last item in the menu
1118  *
1119  * @param obj The menu object
1120  * @return The last item, or NULL if none
1121  *
1122  * @ingroup Menu
1123  */
1124 EAPI const Elm_Menu_Item *
1125 elm_menu_last_item_get(const Evas_Object * obj)
1126 {
1127    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1128    Widget_Data *wd = elm_widget_data_get(obj);
1129    if (!wd) return NULL;
1130    Eina_List *l = eina_list_last(wd->items);
1131    if (l) return l->data;
1132    return NULL;
1133 }
1134
1135 /**
1136  * Get the selected item in the menu
1137  *
1138  * @param obj The menu object
1139  * @return The selected item, or NULL if none
1140  *
1141  * @ingroup Menu
1142  */
1143 EAPI const Elm_Menu_Item *
1144 elm_menu_selected_item_get(const Evas_Object * obj)
1145 {
1146    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1147    Widget_Data *wd = elm_widget_data_get(obj);
1148    if (!wd) return NULL;
1149    Eina_List *l;
1150    Elm_Menu_Item *item;
1151    EINA_LIST_FOREACH(wd->items, l, item)
1152      {
1153         if (item->selected) return item;
1154      }
1155    return NULL;
1156 }
1157