Merge branch 'master' into svn_merge
[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  * @ingroup Elementary
7  *
8  * A menu is a list of items displayed above the window. Each item can
9  * have a sub-menu. The menu object can be used to display a menu on right
10  * click, in a toolbar, anywhere.
11  *
12  * Signals that you can add callbacks for are:
13  *
14  * "clicked" - the user clicked the empty space in the menu to dismiss.
15  *             event_info is NULL.
16  */
17
18 typedef struct _Widget_Data Widget_Data;
19
20 struct _Elm_Menu_Item
21 {
22    Elm_Widget_Item base;
23    Elm_Menu_Item *parent;
24    Evas_Object *icon;
25    const char *icon_str;
26    const char *label;
27    Evas_Smart_Cb func;
28
29    struct {
30       Evas_Object *hv, *bx, *location;
31       Eina_List *items;
32       Eina_Bool open : 1;
33    } submenu;
34
35    Eina_Bool separator : 1;
36    Eina_Bool disabled : 1;
37    Eina_Bool selected: 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    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  *
605  * @ingroup Menu
606  */
607 EAPI Evas_Object *
608 elm_menu_item_object_get(const Elm_Menu_Item *item)
609 {
610    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
611    return item->base.view;
612 }
613
614 static void
615 _item_clone(Evas_Object *obj, Elm_Menu_Item *parent, Elm_Menu_Item *item)
616 {
617    Elm_Menu_Item *new_item, *subitem;
618    Eina_List *iter;
619
620    if (item->separator)
621      new_item = elm_menu_item_separator_add(obj, parent);
622    else
623      new_item = elm_menu_item_add(obj, parent, item->icon_str, item->label, item->func, item->base.data);
624    elm_menu_item_disabled_set(new_item, item->disabled);
625
626    EINA_LIST_FOREACH(item->submenu.items, iter, subitem)
627       _item_clone(obj, new_item, subitem);
628 }
629
630 void
631 elm_menu_clone(Evas_Object *from_menu, Evas_Object *to_menu, Elm_Menu_Item *parent)
632 {
633    ELM_CHECK_WIDTYPE(from_menu, widtype);
634    ELM_CHECK_WIDTYPE(to_menu, widtype);
635    Widget_Data *from_wd = elm_widget_data_get(from_menu);
636    Eina_List *iter;
637    Elm_Menu_Item *item;
638
639    if (!from_wd) return;
640    EINA_LIST_FOREACH(from_wd->items, iter, item)
641       _item_clone(to_menu, parent, item);
642 }
643
644 /**
645  * Add an item at the end
646  *
647  * @param obj The menu object.
648  * @param icon A icon display on the item. The icon will be destryed by the menu.
649  * @param label The label of the item.
650  * @param func Function called when the user select the item.
651  * @param data Data sent by the callback.
652  * @return Returns the new item.
653  *
654  * @ingroup Menu
655  */
656 EAPI Elm_Menu_Item *
657 elm_menu_item_add(Evas_Object *obj, Elm_Menu_Item *parent, const char *icon, const char *label, Evas_Smart_Cb func, const void *data)
658 {
659    Elm_Menu_Item *subitem;
660    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
661    Widget_Data *wd = elm_widget_data_get(obj);
662    Evas_Object *icon_obj;
663
664    if (!wd) return NULL;
665    icon_obj = elm_icon_add(obj);
666    if (!icon_obj) return NULL;
667    subitem = elm_widget_item_new(obj, Elm_Menu_Item);
668    if (!subitem)
669      {
670         evas_object_del(icon_obj);
671         return NULL;
672      }
673    subitem->base.data = data;
674    subitem->func = func;
675    subitem->parent = parent;
676    subitem->icon = icon_obj;
677
678    _item_obj_create(subitem);
679    elm_menu_item_label_set(subitem, label);
680
681    elm_widget_sub_object_add(subitem->base.widget, subitem->icon);
682    edje_object_part_swallow(subitem->base.view, "elm.swallow.content", subitem->icon);
683    if (icon) elm_menu_item_icon_set(subitem, icon);
684
685    if (parent)
686      {
687         if (!parent->submenu.bx) _item_submenu_obj_create(parent);
688         elm_box_pack_end(parent->submenu.bx, subitem->base.view);
689         parent->submenu.items = eina_list_append(parent->submenu.items, subitem);
690      }
691    else
692      {
693         elm_box_pack_end(wd->bx, subitem->base.view);
694         wd->items = eina_list_append(wd->items, subitem);
695      }
696
697    _sizing_eval(obj);
698    return subitem;
699 }
700
701 /**
702  * Set the label of a menu item
703  *
704  * @param item The menu item object.
705  * @param label The label to set for @p item
706  *
707  * @ingroup Menu
708  */
709 EAPI void
710 elm_menu_item_label_set(Elm_Menu_Item *item, const char *label)
711 {
712    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
713    eina_stringshare_replace(&item->label, label);
714
715    if (label)
716      edje_object_signal_emit(item->base.view, "elm,state,text,visible", "elm");
717    else
718      edje_object_signal_emit(item->base.view, "elm,state,text,hidden", "elm");
719
720    edje_object_message_signal_process(item->base.view);
721    edje_object_part_text_set(item->base.view, "elm.text", label);
722    _sizing_eval(item->base.widget);
723 }
724
725 /**
726  * Get the label of a menu item
727  *
728  * @param item The menu item object.
729  * @return The label of @p item
730  *
731  * @ingroup Menu
732  */
733 EAPI const char *
734 elm_menu_item_label_get(const Elm_Menu_Item *item)
735 {
736    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
737    return item->label;
738 }
739
740 /**
741  * Set the icon of a menu item
742  *
743  * Once the icon object is set, a previously set one will be deleted.
744  *
745  * @param item The menu item object.
746  * @param icon The icon object to set for @p item
747  *
748  * @ingroup Menu
749  */
750 EAPI void
751 elm_menu_item_icon_set(Elm_Menu_Item *item, const char *icon)
752 {
753    char icon_tmp[512];
754    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
755    EINA_SAFETY_ON_NULL_RETURN(icon);
756    if (!*icon) return;
757    if ((item->icon_str) && (!strcmp(item->icon_str, icon))) return;
758    if ((snprintf(icon_tmp, sizeof(icon_tmp), "menu/%s", icon) > 0) &&
759        (elm_icon_standard_set(item->icon, icon_tmp)))
760      {
761         eina_stringshare_replace(&item->icon_str, icon);
762         edje_object_signal_emit(item->base.view, "elm,state,icon,visible", "elm");
763      }
764    else
765      edje_object_signal_emit(item->base.view, "elm,state,icon,hidden", "elm");
766    edje_object_message_signal_process(item->base.view);
767    _sizing_eval(item->base.widget);
768 }
769
770 /**
771  * Set the disabled state of @p item.
772  *
773  * @param item The menu item object.
774  * @param disabled The enabled/disabled state of the item
775  *
776  * @ingroup Menu
777  */
778 EAPI void
779 elm_menu_item_disabled_set(Elm_Menu_Item *item, Eina_Bool disabled)
780 {
781    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
782    if (disabled == item->disabled) return;
783    item->disabled = !!disabled;
784    if (disabled)
785      {
786         edje_object_signal_emit(item->base.view, "elm,state,disabled", "elm");
787         if (item->submenu.open) _submenu_hide(item);
788      }
789    else
790      edje_object_signal_emit(item->base.view, "elm,state,enabled", "elm");
791    edje_object_message_signal_process(item->base.view);
792 }
793
794 /**
795  * Get the disabled state of @p item.
796  *
797  * @param item The menu item object.
798  * @return The enabled/disabled state of the item
799  *
800  * @ingroup Menu
801  */
802 EAPI Eina_Bool
803 elm_menu_item_disabled_get(const Elm_Menu_Item *item)
804 {
805    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, EINA_FALSE);
806    return item->disabled;
807 }
808
809 /**
810  * Add a separator item to menu @p obj under @p parent.
811  *
812  * @param obj The menu object
813  * @param parent The item to add the separator under
814  *
815  * @return The created item or NULL on failure
816  *
817  * @ingroup Menu
818  */
819 EAPI Elm_Menu_Item *
820 elm_menu_item_separator_add(Evas_Object *obj, Elm_Menu_Item *parent)
821 {
822    Elm_Menu_Item *subitem;
823    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
824    Widget_Data *wd = elm_widget_data_get(obj);
825    if (!wd) return NULL;
826    /* don't add a separator as the first item */
827    if (!wd->items) return NULL;
828    /* don't allow adding more than one separator in a row */
829    if (parent) subitem = eina_list_last(parent->submenu.items)->data;
830    else subitem = eina_list_last(wd->items)->data;
831    if (subitem->separator) return NULL;
832
833    subitem = elm_widget_item_new(obj, Elm_Menu_Item);
834    if (!subitem) return NULL;
835    subitem->base.widget = obj;
836    subitem->separator = 1;
837    _item_separator_obj_create(subitem);
838    if (!parent)
839      {
840         elm_box_pack_end(wd->bx, subitem->base.view);
841         wd->items = eina_list_append(wd->items, subitem);
842      }
843    else
844      {
845         if (!parent->submenu.bx) _item_submenu_obj_create(parent);
846         elm_box_pack_end(parent->submenu.bx, subitem->base.view);
847         parent->submenu.items = eina_list_append(parent->submenu.items, subitem);
848      }
849    _sizing_eval(obj);
850    return subitem;
851 }
852
853 /**
854  * Get the icon object from a menu item
855  *
856  * @param item The menu item object
857  * @return The icon object or NULL if there's no icon
858  *
859  * @ingroup Menu
860  */
861 EAPI const Evas_Object *
862 elm_menu_item_object_icon_get(const Elm_Menu_Item *item)
863 {
864    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
865    return (const Evas_Object *)item->icon;
866 }
867
868 /**
869  * Get the string representation from the icon of a menu item
870  *
871  * @param item The menu item object.
872  * @return The string representation of @p item's icon or NULL
873  *
874  * @ingroup Menu
875  */
876 EAPI const char *
877 elm_menu_item_icon_get(const Elm_Menu_Item *item)
878 {
879    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
880    return item->icon_str;
881 }
882
883 /**
884  * Returns whether @p item is a separator.
885  *
886  * @param item The item to check
887  * @return If true, @p item is a separator
888  *
889  * @ingroup Menu
890  */
891 EAPI Eina_Bool
892 elm_menu_item_is_separator(Elm_Menu_Item *item)
893 {
894    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, EINA_FALSE);
895    return item->separator;
896 }
897
898 /**
899  * Deletes an item from the menu.
900  *
901  * @param item The item to delete.
902  *
903  * @ingroup Menu
904  */
905 EAPI void
906 elm_menu_item_del(Elm_Menu_Item *item)
907 {
908    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
909    Elm_Menu_Item *_item;
910
911    elm_widget_item_pre_notify_del(item);
912
913    EINA_LIST_FREE(item->submenu.items, _item) elm_menu_item_del(_item);
914    if (item->label) eina_stringshare_del(item->label);
915    if (item->icon) evas_object_del(item->icon);
916    if (item->submenu.hv) evas_object_del(item->submenu.hv);
917    if (item->submenu.location) evas_object_del(item->submenu.location);
918
919    if (item->parent)
920      item->parent->submenu.items = eina_list_remove(item->parent->submenu.items, item);
921    else
922      {
923         Widget_Data *wd = elm_widget_data_get(item->base.widget);
924         wd->items = eina_list_remove(wd->items, item);
925      }
926
927    elm_widget_item_del(item);
928 }
929
930 /**
931  * Set the function called when a menu item is freed.
932  *
933  * @param item The item to set the callback on
934  * @param func The function called
935  *
936  * @ingroup Menu
937  */
938 EAPI void
939 elm_menu_item_del_cb_set(Elm_Menu_Item *item, Evas_Smart_Cb func)
940 {
941    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
942    elm_widget_item_del_cb_set(item, func);
943 }
944
945 /**
946  * Returns the data associated with menu item @p item.
947  *
948  * @param item The item
949  * @return The data associated with @p item
950  *
951  * @ingroup Menu
952  */
953 EAPI void *
954 elm_menu_item_data_get(const Elm_Menu_Item *item)
955 {
956    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
957    return elm_widget_item_data_get(item);
958 }
959
960 /**
961  * Sets the data to be associated with menu item @p item.
962  *
963  * @param item The item
964  * @param data The data to be associated with @p item
965  *
966  * @ingroup Menu
967  */
968 EAPI void
969 elm_menu_item_data_set(Elm_Menu_Item *item, const void *data)
970 {
971    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
972    elm_widget_item_data_set(item, data);
973 }
974
975 /**
976  * Returns a list of @p item's subitems.
977  *
978  * @param item The item
979  * @return An Eina_List* of @p item's subitems
980  *
981  * @ingroup Menu
982  */
983 EAPI const Eina_List *
984 elm_menu_item_subitems_get(const Elm_Menu_Item *item)
985 {
986    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
987    return item->submenu.items;
988 }
989
990 /**
991  * Returns a list of @p item's items.
992  *
993  * @param obj The menu object
994  * @return An Eina_List* of @p item's items
995  *
996  * @ingroup Menu
997  */
998 EAPI const Eina_List *
999 elm_menu_items_get(const Evas_Object * obj)
1000 {
1001    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1002    Widget_Data *wd = elm_widget_data_get(obj);
1003    return wd->items;
1004 }
1005
1006 /**
1007  * Set the selected state of @p item.
1008  *
1009  * @param item The menu item object.
1010  * @param selected The selected/unselected state of the item
1011  *
1012  * @ingroup Menu
1013  */
1014 EAPI void
1015 elm_menu_item_selected_set(Elm_Menu_Item *item, Eina_Bool selected)
1016 {
1017    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
1018    if (selected == item->selected) return;
1019    item->selected = selected;
1020    if (selected)
1021      {
1022         edje_object_signal_emit(item->base.view, "elm,state,selected", "elm");
1023         _menu_item_activate(item, NULL, NULL, NULL);
1024      }
1025    else
1026      {
1027         edje_object_signal_emit(item->base.view, "elm,state,unselected", "elm");
1028         _menu_item_inactivate(item, NULL, NULL, NULL);
1029      }
1030    edje_object_message_signal_process(item->base.view);
1031 }
1032
1033 /**
1034  * Get the selected state of @p item.
1035  *
1036  * @param item The menu item object.
1037  * @return The selected/unselected state of the item
1038  *
1039  * @ingroup Menu
1040  */
1041 EAPI Eina_Bool
1042 elm_menu_item_selected_get(const Elm_Menu_Item *item)
1043 {
1044    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, EINA_FALSE);
1045    return item->selected;
1046 }
1047
1048 /**
1049  * Get the previous item in the menu.
1050  *
1051  * @param item The menu item object.
1052  * @return The item before it, or NULL if none
1053  *
1054  * @ingroup Menu
1055  */
1056 EAPI const Elm_Menu_Item *
1057 elm_menu_item_prev_get(const Elm_Menu_Item *it)
1058 {
1059    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it, NULL);
1060    if (it->parent)
1061      {
1062         Eina_List *l = eina_list_data_find_list(it->parent->submenu.items, it);
1063         l = eina_list_prev(l);
1064         if (!l) return NULL;
1065         return l->data;
1066      }
1067    else
1068      {
1069         Widget_Data *wd = elm_widget_data_get(it->base.widget);
1070         if (!wd | !wd->items) return NULL;
1071         Eina_List *l = eina_list_data_find_list(wd->items, it);
1072         l = eina_list_prev(l);
1073         if (!l) return NULL;
1074         return l->data;
1075      }
1076    return NULL;
1077 }
1078
1079 /**
1080  * Get the next item in the menu.
1081  *
1082  * @param item The menu item object.
1083  * @return The item after it, or NULL if none
1084  *
1085  * @ingroup Menu
1086  */
1087 EAPI const Elm_Menu_Item *
1088 elm_menu_item_next_get(const Elm_Menu_Item *it)
1089 {
1090    ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(it, NULL);
1091    if (it->parent)
1092      {
1093         Eina_List *l = eina_list_data_find_list(it->parent->submenu.items, it);
1094         l = eina_list_next(l);
1095         if (!l) return NULL;
1096         return l->data;
1097      }
1098    else
1099      {
1100         Widget_Data *wd = elm_widget_data_get(it->base.widget);
1101         if (!wd | !wd->items) return NULL;
1102         Eina_List *l = eina_list_data_find_list(wd->items, it);
1103         l = eina_list_next(l);
1104         if (!l) return NULL;
1105         return l->data;
1106      }
1107    return NULL;
1108 }
1109
1110 /**
1111  * Get the first item in the menu
1112  *
1113  * @param obj The menu object
1114  * @return The first item, or NULL if none
1115  *
1116  * @ingroup Menu
1117  */
1118 EAPI const Elm_Menu_Item *
1119 elm_menu_first_item_get(const Evas_Object * obj)
1120 {
1121    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1122    Widget_Data *wd = elm_widget_data_get(obj);
1123    if (!wd) return NULL;
1124    if (wd->items) return wd->items->data;
1125    return NULL;
1126 }
1127
1128 /**
1129  * Get the last item in the menu
1130  *
1131  * @param obj The menu object
1132  * @return The last item, or NULL if none
1133  *
1134  * @ingroup Menu
1135  */
1136 EAPI const Elm_Menu_Item *
1137 elm_menu_last_item_get(const Evas_Object * obj)
1138 {
1139    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1140    Widget_Data *wd = elm_widget_data_get(obj);
1141    if (!wd) return NULL;
1142    Eina_List *l = eina_list_last(wd->items);
1143    if (l) return l->data;
1144    return NULL;
1145 }
1146
1147 /**
1148  * Get the selected item in the menu
1149  *
1150  * @param obj The menu object
1151  * @return The selected item, or NULL if none
1152  *
1153  * @ingroup Menu
1154  */
1155 EAPI const Elm_Menu_Item *
1156 elm_menu_selected_item_get(const Evas_Object * obj)
1157 {
1158    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1159    Widget_Data *wd = elm_widget_data_get(obj);
1160    if (!wd) return NULL;
1161    Eina_List *l;
1162    Elm_Menu_Item *item;
1163    EINA_LIST_FOREACH(wd->items, l, item)
1164      {
1165         if (item->selected) return item;
1166      }
1167    return NULL;
1168 }
1169