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