\chapter{Menu Example}
\hypertarget{tutorial_menu}{}\label{tutorial_menu}\index{Menu Example@{Menu Example}}


This example shows how to create a menu with regular items, object items, submenus and how to delete items from a menu. The full source for this example is \doxylink{menu_example_01_8c-example}{menu\+\_\+example\+\_\+01.c}.

We\textquotesingle{}ll start looking at the menu creation and how to create a very simple item\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ menu\ =\ \mbox{\hyperlink{group__Elm__Menu_gab5d9f82f2078b1ff0330dbc1b9a5f8fb}{elm\_menu\_add}}(win);}
\DoxyCodeLine{\ \ \ elm\_menu\_item\_add(menu,\ NULL,\ NULL,\ \textcolor{stringliteral}{"{}first\ item"{}},\ NULL,\ NULL);}

\end{DoxyCodeInclude}
 For our next item we are going to add an icon\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ menu\_it\ =\ elm\_menu\_item\_add(menu,\ NULL,\ \textcolor{stringliteral}{"{}mail-\/reply-\/all"{}},\ \textcolor{stringliteral}{"{}second\ item"{}},\ NULL,\ NULL);}

\end{DoxyCodeInclude}
 Now we are going to add more items, but these icons are going to have a parent, which will put them in a sub-\/menu. First just another item with an icon\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ elm\_menu\_item\_add(menu,\ menu\_it,\ \textcolor{stringliteral}{"{}object-\/rotate-\/left"{}},\ \textcolor{stringliteral}{"{}menu\ 1"{}},\ NULL,\ NULL);}

\end{DoxyCodeInclude}
 Next we are going to add a button to our menu(any elm widget can be added to a menu)\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ button\ =\ \mbox{\hyperlink{group__Elm__Button_gaf5caf46d74c4e122a18da323935d25e4}{elm\_button\_add}}(win);}
\DoxyCodeLine{\ \ \ elm\_object\_text\_set(button,\ \textcolor{stringliteral}{"{}button\ -\/\ delete\ items"{}});}
\DoxyCodeLine{\ \ \ menu\_it1\ =\ elm\_menu\_item\_add(menu,\ menu\_it,\ NULL,\ NULL,\ NULL,\ NULL);}

\end{DoxyCodeInclude}
 We are also going to have the button delete the first item of our sub-\/menu when clicked\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ elm\_object\_item\_content\_set(menu\_it1,\ button);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Smart__Object__Group_gac12dd6655f488cc47ce6cf00eb7d12b2}{evas\_object\_smart\_callback\_add}}(button,\ \textcolor{stringliteral}{"{}clicked"{}},\ \_del\_it,\ menu);}

\end{DoxyCodeInclude}
 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_del\_it(\textcolor{keywordtype}{void}\ *data,\ Evas\_Object\ *obj\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event\_info\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}\ *l;}
\DoxyCodeLine{\ \ \ Elm\_Object\_Item\ *menu\_it\ =\ elm\_menu\_first\_item\_get(data);}
\DoxyCodeLine{\ \ \ menu\_it\ =\ elm\_menu\_item\_next\_get(menu\_it);}
\DoxyCodeLine{\ \ \ l\ =\ elm\_menu\_item\_subitems\_get(menu\_it);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Elm__General_gad1b5a0d41fa322b28000a290154ec3a4}{elm\_object\_item\_del}}(eina\_list\_data\_get(l));}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 We now add a separator and three more regular items\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_show(\textcolor{keywordtype}{void}\ *data,\ Evas\ *e\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ Evas\_Object\ *obj\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event\_info)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Evas__Event__Mouse__Down}{Evas\_Event\_Mouse\_Down}}\ *ev\ =\ event\_info;}
\DoxyCodeLine{\ \ \ elm\_menu\_move(data,\ ev-\/>\mbox{\hyperlink{struct__Evas__Event__Mouse__Down_adcaa4cd5eadd8c04cc5510a5b2514eb5}{canvas}}.\mbox{\hyperlink{struct__Evas__Coord__Point_ad809771faf3700a1492e4465336e6b1e}{x}},\ ev-\/>\mbox{\hyperlink{struct__Evas__Event__Mouse__Down_adcaa4cd5eadd8c04cc5510a5b2514eb5}{canvas}}.\mbox{\hyperlink{struct__Evas__Coord__Point_ad7450d6ad43e6a7394af6807721144f7}{y}});}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Basic_ga07208fcafd4739de606bc35fff684bf5}{evas\_object\_show}}(data);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{EAPI\_MAIN\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{elm\_main(\textcolor{keywordtype}{int}\ argc\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{char}\ **argv\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ Evas\_Object\ *win,\ *menu,\ *button,\ *rect;}
\DoxyCodeLine{\ \ \ Elm\_Object\_Item\ *menu\_it,\ *menu\_it1;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ win\ =\ \mbox{\hyperlink{group__Elm__Win__Group_ga23d9d1ffa0a65a1a1dd482d570db1ba0}{elm\_win\_util\_standard\_add}}(\textcolor{stringliteral}{"{}menu"{}},\ \textcolor{stringliteral}{"{}Menu"{}});}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Elm__Win__Group_ga9a16a2f8c9fd5b6e9203ed9dcb53ea8b}{elm\_win\_autodel\_set}}(win,\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Elm__General_ga7a05f37fd3a019da5e26c5fc91fcd937}{elm\_policy\_set}}(\mbox{\hyperlink{group__Elm__General_ggaa16f3378ecdbf660fa378402e3c5a0a9a02fadb6faada04b19d0fa42c11a8d32a}{ELM\_POLICY\_QUIT}},\ \mbox{\hyperlink{group__Elm__General_gga46af37e6ac2c406321060be827117196abbe0962d88ae12afe1a173da881f56cd}{ELM\_POLICY\_QUIT\_LAST\_WINDOW\_CLOSED}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ rect\ =\ \mbox{\hyperlink{group__Evas__Object__Rectangle_ga1b7a9d8e6ce240590c1734da5f3449d2}{evas\_object\_rectangle\_add}}(\mbox{\hyperlink{group__Evas__Object__Group_ga7e211219d11abced5f3089362f7476b9}{evas\_object\_evas\_get}}(win));}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Elm__Win__Group_ga505d6bd217e5ee01e82b548ec35986eb}{elm\_win\_resize\_object\_add}}(win,\ rect);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group_gaf20fdb7dc524a95091d72d24cf6a05e8}{evas\_object\_size\_hint\_min\_set}}(rect,\ 0,\ 0);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Basic_gab751e9d24c16e74af6f7c08ea5b930c2}{evas\_object\_color\_set}}(rect,\ 0,\ 0,\ 0,\ 0);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Basic_ga07208fcafd4739de606bc35fff684bf5}{evas\_object\_show}}(rect);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ menu\ =\ \mbox{\hyperlink{group__Elm__Menu_gab5d9f82f2078b1ff0330dbc1b9a5f8fb}{elm\_menu\_add}}(win);}
\DoxyCodeLine{\ \ \ elm\_menu\_item\_add(menu,\ NULL,\ NULL,\ \textcolor{stringliteral}{"{}first\ item"{}},\ NULL,\ NULL);}
\DoxyCodeLine{\ \ \ menu\_it\ =\ elm\_menu\_item\_add(menu,\ NULL,\ \textcolor{stringliteral}{"{}mail-\/reply-\/all"{}},\ \textcolor{stringliteral}{"{}second\ item"{}},\ NULL,\ NULL);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ elm\_menu\_item\_add(menu,\ menu\_it,\ \textcolor{stringliteral}{"{}object-\/rotate-\/left"{}},\ \textcolor{stringliteral}{"{}menu\ 1"{}},\ NULL,\ NULL);}

\end{DoxyCodeInclude}
 We now add another item, however this time it won\textquotesingle{}t go the sub-\/menu and it\textquotesingle{}ll be disabled\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ button\ =\ \mbox{\hyperlink{group__Elm__Button_gaf5caf46d74c4e122a18da323935d25e4}{elm\_button\_add}}(win);}
\DoxyCodeLine{\ \ \ elm\_object\_text\_set(button,\ \textcolor{stringliteral}{"{}button\ -\/\ delete\ items"{}});}
\DoxyCodeLine{\ \ \ menu\_it1\ =\ elm\_menu\_item\_add(menu,\ menu\_it,\ NULL,\ NULL,\ NULL,\ NULL);}
\DoxyCodeLine{\ \ \ elm\_object\_item\_content\_set(menu\_it1,\ button);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Smart__Object__Group_gac12dd6655f488cc47ce6cf00eb7d12b2}{evas\_object\_smart\_callback\_add}}(button,\ \textcolor{stringliteral}{"{}clicked"{}},\ \_del\_it,\ menu);}
\DoxyCodeLine{\ \ \ elm\_menu\_item\_separator\_add(menu,\ menu\_it);}
\DoxyCodeLine{\ \ \ elm\_menu\_item\_add(menu,\ menu\_it,\ NULL,\ \textcolor{stringliteral}{"{}third\ item"{}},\ NULL,\ NULL);}
\DoxyCodeLine{\ \ \ elm\_menu\_item\_add(menu,\ menu\_it,\ NULL,\ \textcolor{stringliteral}{"{}fourth\ item"{}},\ NULL,\ NULL);}
\DoxyCodeLine{\ \ \ elm\_menu\_item\_add(menu,\ menu\_it,\ \textcolor{stringliteral}{"{}window-\/new"{}},\ \textcolor{stringliteral}{"{}sub\ menu"{}},\ NULL,\ NULL);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ menu\_it\ =\ elm\_menu\_item\_add(menu,\ NULL,\ NULL,\ \textcolor{stringliteral}{"{}third\ item"{}},\ NULL,\ NULL);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Elm__General_ga1d4c8d3c83ed9408eaa35d605adbd7d2}{elm\_object\_item\_disabled\_set}}(menu\_it,\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});}

\end{DoxyCodeInclude}
 To make sure that our menu is shown whenever the window is clicked(and where clicked) we use the following callback\+:  
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_del\_it(\textcolor{keywordtype}{void}\ *data,\ Evas\_Object\ *obj\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event\_info\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}\ *l;}
\DoxyCodeLine{\ \ \ Elm\_Object\_Item\ *menu\_it\ =\ elm\_menu\_first\_item\_get(data);}
\DoxyCodeLine{\ \ \ menu\_it\ =\ elm\_menu\_item\_next\_get(menu\_it);}
\DoxyCodeLine{\ \ \ l\ =\ elm\_menu\_item\_subitems\_get(menu\_it);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Elm__General_gad1b5a0d41fa322b28000a290154ec3a4}{elm\_object\_item\_del}}(eina\_list\_data\_get(l));}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 Our example will look like this\+:

 
\begin{DoxyImageNoCaption}
  \mbox{\includegraphics[width=\textwidth]{screenshots/menu_example_01}}
\end{DoxyImageNoCaption}
 