

In this example we have a list with two items, when either item is clicked a ctxpopup for it will be shown. Our two ctxpopups are quite different, the one for the first item is a vertical and it\textquotesingle{}s items contain both labels and icons, the one for the second item is horizontal and it\textquotesingle{}s items have icons but not labels.

We will begin examining our example code by looking at the callback we\textquotesingle{}ll use when items in the ctxpopup are clicked. It\textquotesingle{}s very simple, all it does is print the label present in the ctxpopup item\+: 
\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{//gcc -o ctxpopup\_example\_01 ctxpopup\_example\_01.c -g `pkg-config --cflags --libs elementary`}

\textcolor{preprocessor}{#include <Elementary.h>}

\textcolor{keyword}{static} \textcolor{keywordtype}{int} list\_mouse\_down = 0;

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_dismissed\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keywordtype}{void} *event\_info)
\{
   evas\_object\_del(obj);
\}

\end{DoxyCodeInclude}


Next we examine a function that creates ctxpopup items, it was created to avoid repeating the same code whenever we needed to add an item to our ctxpopup. Our function creates an icon from the standard set of icons, and then creates the item, with the label received as an argument. We also set the callback to be called when the item is clicked\+: 
\begin{DoxyCodeInclude}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_ctxpopup\_item\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keywordtype}{void} *event\_info)
\{
   printf(\textcolor{stringliteral}{"ctxpopup item selected: %s\(\backslash\)n"}, elm\_object\_item\_text\_get(event\_info));
\}

\end{DoxyCodeInclude}


Finally we have the function that will create the ctxpopup for the first item in our list. This one is somewhat more complex though, so let\textquotesingle{}s go through it in parts. First we declare our variable and add the ctxpopup\+: 
\begin{DoxyCodeInclude}

Elm_Object_Item *item\_new(Evas\_Object *ctxpopup, \textcolor{keyword}{const} \textcolor{keywordtype}{char} * label, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *icon)
\{
   Evas\_Object *ic = elm_icon_add(ctxpopup);
   elm\_icon\_standard\_set(ic, icon);
   elm\_image\_resizable\_set(ic, EINA\_FALSE, EINA\_FALSE);
   \textcolor{keywordflow}{return} elm\_ctxpopup\_item\_append(ctxpopup, label, ic, \_ctxpopup\_item\_cb, NULL);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_list\_item\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *ctxpopup;
   Elm_Object_Item *it;
   Evas\_Coord x,y;

   \textcolor{keywordflow}{if} (list\_mouse\_down > 0) \textcolor{keywordflow}{return};

   ctxpopup = elm_ctxpopup_add(obj);

\end{DoxyCodeInclude}


Next we create a bunch of items for our ctxpopup, marking two of them as disabled just so we can see what that will look like\+: 
\begin{DoxyCodeInclude}
   evas\_object\_smart\_callback\_add(ctxpopup, \textcolor{stringliteral}{"dismissed"}, \_dismissed\_cb, NULL);

   item\_new(ctxpopup, \textcolor{stringliteral}{"Go to home folder"}, \textcolor{stringliteral}{"home"});
   item\_new(ctxpopup, \textcolor{stringliteral}{"Save file"}, \textcolor{stringliteral}{"file"});
   item\_new(ctxpopup, \textcolor{stringliteral}{"Delete file"}, \textcolor{stringliteral}{"delete"});
   it = item\_new(ctxpopup, \textcolor{stringliteral}{"Navigate to folder"}, \textcolor{stringliteral}{"folder"});
   elm\_object\_item\_disabled\_set(it, EINA\_TRUE);
   item\_new(ctxpopup, \textcolor{stringliteral}{"Edit entry"}, \textcolor{stringliteral}{"edit"});
   it = item\_new(ctxpopup, \textcolor{stringliteral}{"Set date and time"}, \textcolor{stringliteral}{"clock"});
   elm\_object\_item\_disabled\_set(it, EINA\_TRUE);

\end{DoxyCodeInclude}


Then we ask evas where the mouse pointer was so that we can have our ctxpopup appear in the right place, set a maximum size for the ctxpopup, move it and show it\+: 
\begin{DoxyCodeInclude}

   evas\_pointer\_canvas\_xy\_get(evas\_object\_evas\_get(obj), &x, &y);
   evas\_object\_move(ctxpopup, x, y);
   evas\_object\_show(ctxpopup);

\end{DoxyCodeInclude}


And last we mark the list item as not selected\+: 
\begin{DoxyCodeInclude}

   elm\_list\_item\_selected\_set(event\_info, EINA\_FALSE);
\}

\end{DoxyCodeInclude}


Our next function is the callback that will create the ctxpopup for the second list item, it is very similar to the previous function. A couple of interesting things to note is that we ask our ctxpopup to be horizontal, and that we pass N\+U\+L\+L as the label for every item\+: 
\begin{DoxyCodeInclude}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_list\_item\_cb2(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *ctxpopup;
   Elm_Object_Item *it;
   Evas\_Coord x,y;

   \textcolor{keywordflow}{if} (list\_mouse\_down > 0) \textcolor{keywordflow}{return};

   ctxpopup = elm_ctxpopup_add(obj);
   evas\_object\_smart\_callback\_add(ctxpopup, \textcolor{stringliteral}{"dismissed"}, \_dismissed\_cb, NULL);
   elm\_ctxpopup\_horizontal\_set(ctxpopup, EINA\_TRUE);

   item\_new(ctxpopup, NULL, \textcolor{stringliteral}{"home"});
   item\_new(ctxpopup, NULL, \textcolor{stringliteral}{"file"});
   item\_new(ctxpopup, NULL, \textcolor{stringliteral}{"delete"});
   item\_new(ctxpopup, NULL, \textcolor{stringliteral}{"folder"});
   it = item\_new(ctxpopup, NULL, \textcolor{stringliteral}{"edit"});
   elm\_object\_item\_disabled\_set(it, EINA\_TRUE);
   item\_new(ctxpopup, NULL, \textcolor{stringliteral}{"clock"});

   evas\_pointer\_canvas\_xy\_get(evas\_object\_evas\_get(obj), &x, &y);
   evas\_object\_move(ctxpopup, x, y);
   evas\_object\_show(ctxpopup);

   elm\_list\_item\_selected\_set(event\_info, EINA\_FALSE);
\}

\end{DoxyCodeInclude}


And with all of that in place we can now get to our main function where we create the window, the list, the list items and run the main loop\+: 
\begin{DoxyCodeInclude}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_list\_mouse\_down(\textcolor{keywordtype}{void} *data EINA\_UNUSED, Evas *e EINA\_UNUSED, Evas\_Object *obj EINA\_UNUSED, \textcolor{keywordtype}{void} *
      event\_info EINA\_UNUSED)
\{
   list\_mouse\_down++;
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_list\_mouse\_up(\textcolor{keywordtype}{void} *data EINA\_UNUSED, Evas *e EINA\_UNUSED, Evas\_Object *obj EINA\_UNUSED, \textcolor{keywordtype}{void} *event\_info 
      EINA\_UNUSED)
\{
   list\_mouse\_down--;
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_win\_del(\textcolor{keywordtype}{void} *data EINA\_UNUSED, Evas\_Object *obj EINA\_UNUSED, \textcolor{keywordtype}{void} *event\_info EINA\_UNUSED)
\{
   list\_mouse\_down = 0;
\}

EAPI\_MAIN \textcolor{keywordtype}{int}
elm\_main(\textcolor{keywordtype}{int} argc, \textcolor{keywordtype}{char} **argv)
\{
   Evas\_Object *win, *list;

   elm_policy_set(ELM\_POLICY\_QUIT, ELM\_POLICY\_QUIT\_LAST\_WINDOW\_CLOSED);

   win = elm_win_util_standard_add(\textcolor{stringliteral}{"Contextual Popup"}, \textcolor{stringliteral}{"Contextual Popup"});
   evas\_object\_smart\_callback\_add(win, \textcolor{stringliteral}{"delete,request"}, \_win\_del, NULL);
   elm\_win\_autodel\_set(win, EINA\_TRUE);

   list = elm_list_add(win);
   evas\_object\_event\_callback\_add(list, EVAS\_CALLBACK\_MOUSE\_DOWN,
                                  \_list\_mouse\_down, NULL);
   evas\_object\_event\_callback\_add(list, EVAS\_CALLBACK\_MOUSE\_UP,
                                  \_list\_mouse\_up, NULL);
   evas\_object\_size\_hint\_weight\_set(list, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   elm\_win\_resize\_object\_add(win, list);
   elm\_list\_mode\_set(list, ELM\_LIST\_COMPRESS);

   elm\_list\_item\_append(list, \textcolor{stringliteral}{"Ctxpopup with icons and labels"}, NULL, NULL,
                        \_list\_item\_cb, NULL);
   elm\_list\_item\_append(list, \textcolor{stringliteral}{"Ctxpopup with icons only"}, NULL, NULL,
                        \_list\_item\_cb2, NULL);
   evas\_object\_show(list);
   elm\_list\_go(list);

   evas\_object\_resize(win, 400, 400);
   evas\_object\_show(win);

   elm_run();

   \textcolor{keywordflow}{return} 0;
\}
ELM_MAIN()

\end{DoxyCodeInclude}


The example will initially look like this\+:

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


\begin{DoxyNote}{Note}
This doesn\textquotesingle{}t show the ctxpopup tough, since it will only appear when we click one of the list items.
\end{DoxyNote}
Here is what our first ctxpopup will look like\+:

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


And here the second ctxpopup\+:

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