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