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