\section{index\-\_\-example\-\_\-01.\-c}

\begin{DoxyCodeInclude}

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

\textcolor{keyword}{static} \textcolor{keyword}{const} \textcolor{keywordtype}{char} *dict[] = \(\backslash\)
\{
\textcolor{preprocessor}{#include "dict.txt"}
\};

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_index\_item\_del(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keywordtype}{void} *event\_info)
\{
   fprintf(stdout, \textcolor{stringliteral}{"Deleting index node (%s). Comparing index "}
           \textcolor{stringliteral}{"item data reported via callback with the one returned by "}
           \textcolor{stringliteral}{"index's API on items: %s.\(\backslash\)n"},
           elm_index_item_letter_get(event\_info),
           data == elm_object_item_data_get(event\_info) ? \textcolor{stringliteral}{"OK"} :
           \textcolor{stringliteral}{"FAIL, something went wrong"});
\}

\textcolor{comment}{/* delete an index item */}
\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_item\_del(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keywordtype}{void} *event\_info)
\{
   Elm_Object_Item *it = elm_index_selected_item_get(data, 0);

   \textcolor{keywordflow}{if} (!it) \textcolor{keywordflow}{return};

   fprintf(stdout, \textcolor{stringliteral}{"Deleting last selected index item, which had letter"}
           \textcolor{stringliteral}{" %s (pointing to %p)\(\backslash\)n"}, elm_index_item_letter_get(it),
           elm_object_item_data_get(it));

   elm_object_item_del(it);
   elm_index_level_go(data, 0);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_item\_del\_all(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keywordtype}{void} *event\_info)
\{
   elm_index_item_clear(data);
   elm_index_level_go(data, 0);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_active\_set(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keywordtype}{void} *event\_info)
\{
   Eina\_Bool disabled = elm_index_autohide_disabled_get(data);
   elm_index_autohide_disabled_set(data, !disabled);

   fprintf(stdout, \textcolor{stringliteral}{"Toggling index programmatically to %s.\(\backslash\)n"},
           !disabled ? \textcolor{stringliteral}{"On"} : \textcolor{stringliteral}{"Off"});
\}

\textcolor{comment}{/* "delay,changed" hook */}
\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_index\_changed(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keywordtype}{void} *event\_info)
\{
   elm_list_item_bring_in(elm_object_item_data_get(event\_info));
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_index\_selected(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keywordtype}{void} *event\_info)
\{
   Elm_Object_Item *lit = event\_info;

   fprintf(stdout, \textcolor{stringliteral}{"New index item selected. Comparing item reported"}
           \textcolor{stringliteral}{" via callback with the selection returned by the API: "}
           \textcolor{stringliteral}{"%s.\(\backslash\)n"}, lit == elm_index_selected_item_get(obj, 0) ? \textcolor{stringliteral}{"OK"} :
           \textcolor{stringliteral}{"FAIL, something went wrong"});
\}

EAPI\_MAIN \textcolor{keywordtype}{int}
elm\_main(\textcolor{keywordtype}{int} argc, \textcolor{keywordtype}{char} **argv)
\{
   Evas\_Object *win, *hbox, *vbox, *bt, *sep, *list, *id;
   Elm_Object_Item *lit;
   \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} i;
   \textcolor{keywordtype}{char} curr = 0;

   elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);

   win = elm_win_util_standard_add(\textcolor{stringliteral}{"index"}, \textcolor{stringliteral}{"Index Example"});
   elm_win_autodel_set(win, EINA\_TRUE);
   evas\_object\_resize(win, 320, 600);
   evas\_object\_show(win);

   vbox = elm_box_add(win);
   evas\_object\_size\_hint\_weight\_set(vbox, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   elm_win_resize_object_add(win, vbox);
   evas\_object\_show(vbox);

   list = elm_list_add(win);
   evas\_object\_size\_hint\_weight\_set(list, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   evas\_object\_size\_hint\_align\_set(list, EVAS\_HINT\_FILL, EVAS\_HINT\_FILL);
   evas\_object\_show(list);
   elm_box_pack_end(vbox, list);

   \textcolor{keywordtype}{id} = elm_index_add(win);
   evas\_object\_size\_hint\_weight\_set(\textcolor{keywordtype}{id}, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   elm_win_resize_object_add(win, \textcolor{keywordtype}{id});
   evas\_object\_show(\textcolor{keywordtype}{id});

   \textcolor{keywordflow}{for} (i = 0; i < (\textcolor{keyword}{sizeof}(dict) / \textcolor{keyword}{sizeof}(dict[0])); i++)
     \{
        lit = elm_list_item_append(list, dict[i], NULL, NULL, NULL, NULL);

        \textcolor{keywordflow}{if} (curr != dict[i][0])
          \{
             Elm_Object_Item *index\_it, *it;
             \textcolor{keywordtype}{char} buf[32];

             curr = dict[i][0];
             \textcolor{comment}{/* indexing by first letters */}

             snprintf(buf, \textcolor{keyword}{sizeof}(buf), \textcolor{stringliteral}{"%c"}, curr);
             index\_it = elm_index_item_append(\textcolor{keywordtype}{id}, buf, NULL, lit);

             \textcolor{comment}{/* this is here just to demostrate the API call */}
             it = elm_index_item_find(\textcolor{keywordtype}{id}, lit);
             assert(it == index\_it);

             elm_object_item_del_cb_set(index\_it, \_index\_item\_del);
          \}
     \}

   evas\_object\_smart\_callback\_add(\textcolor{keywordtype}{id}, \textcolor{stringliteral}{"delay,changed"}, \_index\_changed,
                                  NULL);
   evas\_object\_smart\_callback\_add(\textcolor{keywordtype}{id}, \textcolor{stringliteral}{"selected"}, \_index\_selected, NULL);
   elm_index_level_go(\textcolor{keywordtype}{id}, 0);

   \textcolor{comment}{/* attribute setting knobs */}
   sep = elm_separator_add(win);
   elm_separator_horizontal_set(sep, EINA\_TRUE);
   elm_box_pack_end(vbox, sep);
   evas\_object\_show(sep);

   hbox = elm_box_add(win);
   elm_box_horizontal_set(hbox, EINA\_TRUE);
   evas\_object\_size\_hint\_weight\_set(hbox, EVAS\_HINT\_EXPAND, 0);
   evas\_object\_size\_hint\_fill\_set(hbox, EVAS\_HINT\_FILL, EVAS\_HINT\_FILL);
   elm_box_pack_end(vbox, hbox);
   evas\_object\_show(hbox);

   bt = elm_button_add(win);
   elm\_object\_text\_set(bt, \textcolor{stringliteral}{"bring in index"});
   evas\_object\_smart\_callback\_add(bt, \textcolor{stringliteral}{"clicked"}, \_active\_set, \textcolor{keywordtype}{id});
   elm_box_pack_end(hbox, bt);
   evas\_object\_show(bt);

   bt = elm_button_add(win);
   elm\_object\_text\_set(bt, \textcolor{stringliteral}{"delete last selected item"});
   evas\_object\_smart\_callback\_add(bt, \textcolor{stringliteral}{"clicked"}, \_item\_del, \textcolor{keywordtype}{id});
   elm_box_pack_end(hbox, bt);
   evas\_object\_show(bt);

   bt = elm_button_add(win);
   elm\_object\_text\_set(bt, \textcolor{stringliteral}{"delete all items"});
   evas\_object\_smart\_callback\_add(bt, \textcolor{stringliteral}{"clicked"}, \_item\_del\_all, \textcolor{keywordtype}{id});
   elm_box_pack_end(hbox, bt);
   evas\_object\_show(bt);

   elm_run();
   elm_shutdown();

   \textcolor{keywordflow}{return} 0;
\}

ELM_MAIN()
\end{DoxyCodeInclude}
 