

A button is simple, you click on it and something happens. That said, we\textquotesingle{}ll go through an example to show in detail the button A\+PI less commonly used.

In the end, we\textquotesingle{}ll be presented with something that looks like this\+:

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


The full code of the example is \hyperlink{button_example_01_8c-example}{here} and we will follow here with a rundown of it.


\begin{DoxyCodeInclude}
\textcolor{preprocessor}{#include <Elementary.h>}
\textcolor{keyword}{typedef} \textcolor{keyword}{struct}
\{
   Evas\_Object *mid;
   Evas\_Object *icon\_still;
   \textcolor{keyword}{struct}
     \{
        Evas\_Object *up;
        Evas\_Object *down;
        Evas\_Object *left;
        Evas\_Object *right;
     \} cursors;
\} App\_Data;

\end{DoxyCodeInclude}


We have several buttons to set different times for the autorepeat timeouts of the buttons that use it and a few more that we keep track of in our data struct. The mid button doesn\textquotesingle{}t do much, just moves around according to what other buttons the user presses. Then four more buttons to move the central one, and we\textquotesingle{}re also keeping track of the icon set in the middle button, since when this one moves, we change the icon, and when movement is finished (by releasing one of the four arrow buttons), we set back the normal icon. 
\begin{DoxyCodeInclude}
\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_btn\_cursors\_release\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *btn, \textcolor{keywordtype}{void} *ev)
\{
   App\_Data *app = data;
   elm_object_part_content_set(app->mid, \textcolor{stringliteral}{"icon"}, app->icon\_still);
   app->icon\_still = NULL;
\}

\end{DoxyCodeInclude}


Keeping any of those four buttons pressed will trigger their autorepeat callback, where we move the button doing some size hint magic. To understand how that works better, refer to the Box documentation. Also, the first time the function is called, we change the icon in the middle button, using elm\+\_\+object\+\_\+content\+\_\+unset() first to keep the reference to the previous one, so we don\textquotesingle{}t need to recreate it when we are done moving it. 
\begin{DoxyCodeInclude}
\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_btn\_cursors\_move\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *btn, \textcolor{keywordtype}{void} *ev)
\{
   App\_Data *app = data;
   \textcolor{keywordtype}{double} ax, ay;

   \textcolor{keywordflow}{if} (!app->icon\_still)
     \{
        Evas\_Object *icon;
        app->icon\_still = elm\_object\_content\_unset(app->mid);
        evas\_object\_hide(app->icon\_still);
        icon = elm_icon_add(app->mid);
        elm_icon_standard_set(icon, \textcolor{stringliteral}{"chat"});
        elm_object_part_content_set(app->mid, \textcolor{stringliteral}{"icon"}, icon);
     \}

   evas\_object\_size\_hint\_align\_get(app->mid, &ax, &ay);
   \textcolor{keywordflow}{if} (btn == app->cursors.up)
     \{
        ay -= 0.05;
        \textcolor{keywordflow}{if} (ay < 0.0)
          ay = 0.0;
     \}
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (btn == app->cursors.down)
     \{
        ay += 0.05;
        \textcolor{keywordflow}{if} (ay > 1.0)
          ay = 1.0;
     \}
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (btn == app->cursors.left)
     \{
        ax -= 0.05;
        \textcolor{keywordflow}{if} (ax < 0.0)
          ax = 0.0;
     \}
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (btn == app->cursors.right)
     \{
        ax += 0.05;
        \textcolor{keywordflow}{if} (ax > 1.0)
          ax = 1.0;
     \}
   evas\_object\_size\_hint\_align\_set(app->mid, ax, ay);
\}

\end{DoxyCodeInclude}


One more callback for the option buttons, that just sets the timeouts for the different autorepeat options.


\begin{DoxyCodeInclude}
\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_btn\_options\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *btn, \textcolor{keywordtype}{void} *ev)
\{
   \textcolor{keywordtype}{char} *ptr;
   \textcolor{keywordtype}{double} t;
   App\_Data *app = data;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *lbl = elm\_object\_text\_get(btn);

   ptr = strchr(lbl, \textcolor{charliteral}{':'});
   ptr += 2;
   t = strtod(ptr, NULL);

   \textcolor{keywordflow}{if} (!strncmp(lbl, \textcolor{stringliteral}{"Initial"}, 7))
     \{
        elm_button_autorepeat_initial_timeout_set(app->cursors.up, t);
        elm_button_autorepeat_initial_timeout_set(app->cursors.down, t);
        elm_button_autorepeat_initial_timeout_set(app->cursors.left, t);
        elm_button_autorepeat_initial_timeout_set(app->cursors.right, t);
     \}
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (!strncmp(lbl, \textcolor{stringliteral}{"Gap"}, 3))
     \{
        elm_button_autorepeat_gap_timeout_set(app->cursors.up, t);
        elm_button_autorepeat_gap_timeout_set(app->cursors.down, t);
        elm_button_autorepeat_gap_timeout_set(app->cursors.left, t);
        elm_button_autorepeat_gap_timeout_set(app->cursors.right, t);
     \}
\}

\end{DoxyCodeInclude}


And the main function, which does some setting up of the buttons in boxes to make things work. Here we\textquotesingle{}ll go through some snippets only.

For the option buttons, it\textquotesingle{}s just the button with its label and callback. 
\begin{DoxyCodeInclude}
   btn = elm_button_add(win);
   elm\_object\_text\_set(btn, \textcolor{stringliteral}{"Initial: 0.0"});
   elm_box_pack_end(box2, btn);
   evas\_object\_show(btn);
   evas\_object\_smart\_callback\_add(btn, \textcolor{stringliteral}{"clicked"}, \_btn\_options\_cb, &data);

\end{DoxyCodeInclude}


For the ones that move the central button, we have no labels. There are icons instead, and the autorepeat option is toggled. 
\begin{DoxyCodeInclude}
   btn = elm_button_add(win);
   elm_button_autorepeat_set(btn, EINA\_TRUE);
   elm_button_autorepeat_initial_timeout_set(btn, 1.0);
   elm_button_autorepeat_gap_timeout_set(btn, 0.5);
   evas\_object\_size\_hint\_weight\_set(btn, EVAS\_HINT\_EXPAND, 0.0);
   evas\_object\_size\_hint\_align\_set(btn, EVAS\_HINT\_FILL, 0.0);
   elm_box_pack_end(box, btn);
   evas\_object\_show(btn);
   evas\_object\_smart\_callback\_add(btn, \textcolor{stringliteral}{"repeated"}, \_btn\_cursors\_move\_cb, &data);
   evas\_object\_smart\_callback\_add(btn, \textcolor{stringliteral}{"unpressed"}, \_btn\_cursors\_release\_cb,
                                  &data);

   icon = elm_icon_add(win);
   elm_icon_standard_set(icon, \textcolor{stringliteral}{"arrow\_up"});
   elm_object_part_content_set(btn, \textcolor{stringliteral}{"icon"}, icon);

   data.cursors.up = btn;

\end{DoxyCodeInclude}


And just to show the mid button, which doesn\textquotesingle{}t have anything special. 
\begin{DoxyCodeInclude}
   btn = elm_button_add(win);
   evas\_object\_size\_hint\_weight\_set(btn, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   elm_box_pack_end(box2, btn);
   evas\_object\_show(btn);

   icon = elm_icon_add(win);
   elm_icon_standard_set(icon, \textcolor{stringliteral}{"close"});
   elm_object_part_content_set(btn, \textcolor{stringliteral}{"icon"}, icon);

   data.mid = btn;

\end{DoxyCodeInclude}


And we are done. 