\section{genlist\+\_\+example\+\_\+04.\+c}

\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{//gcc -g genlist\_example\_04.c -o genlist\_example\_04 `pkg-config --cflags --libs elementary`}

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

\textcolor{preprocessor}{#define N\_ITEMS 300}

\textcolor{keyword}{static} Elm_Genlist_Item_Class *\_itc = NULL;
\textcolor{keyword}{static} Elm_Genlist_Item_Class *\_itc\_group = NULL;
\textcolor{keyword}{static} \textcolor{keywordtype}{int} nitems = 0;

\textcolor{keyword}{static} \textcolor{keywordtype}{char} *
\_item\_label\_get(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *part)
\{
   time\_t t = (time\_t)ecore\_time\_unix\_get();
   \textcolor{keywordtype}{char} buf[256];
   \textcolor{keywordtype}{int} i = (int)(\textcolor{keywordtype}{long})data;

   \textcolor{keywordflow}{if} (!strcmp(part, \textcolor{stringliteral}{"elm.text"}))
     snprintf(buf, \textcolor{keyword}{sizeof}(buf), \textcolor{stringliteral}{"Item # %i"}, i);
   \textcolor{keywordflow}{else}
     \{
        \textcolor{keywordtype}{int} n;
        snprintf(buf, \textcolor{keyword}{sizeof}(buf), \textcolor{stringliteral}{"realized at %s"}, ctime(&t));
        n = strlen(buf);
        buf[n - 1] = \textcolor{charliteral}{'\(\backslash\)0'};
     \}

   \textcolor{keywordflow}{return} strdup(buf);
\}

\textcolor{keyword}{static} Evas\_Object *
\_item\_content\_get(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *part)
\{
   Evas\_Object *ic = elm_icon_add(obj);

   \textcolor{keywordflow}{if} (!strcmp(part, \textcolor{stringliteral}{"elm.swallow.icon"}))
     elm\_icon\_standard\_set(ic, \textcolor{stringliteral}{"clock"});

   evas\_object\_size\_hint\_aspect\_set(ic, EVAS\_ASPECT\_CONTROL\_VERTICAL, 1, 1);
   \textcolor{keywordflow}{return} ic;
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_item\_sel\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keywordtype}{void} *event\_info)
\{
   printf(\textcolor{stringliteral}{"sel item data [%p] on genlist obj [%p], item pointer [%p]\(\backslash\)n"},
          data, obj, event\_info);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{char} *
\_group\_label\_get(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *part)
\{
   \textcolor{keywordtype}{char} buf[256];
   \textcolor{keywordtype}{int} i = (int)(\textcolor{keywordtype}{long})data;

   snprintf(buf, \textcolor{keyword}{sizeof}(buf), \textcolor{stringliteral}{"Group %d (item #%d)"}, i / 7, i);

   \textcolor{keywordflow}{return} strdup(buf);
\}

\textcolor{keyword}{static} Evas\_Object *
\_group\_content\_get(\textcolor{keywordtype}{void} *data, Evas\_Object *obj, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *part)
\{
   Evas\_Object *ic = elm_icon_add(obj);

   \textcolor{keywordflow}{if} (!strcmp(part, \textcolor{stringliteral}{"elm.swallow.icon"}))
     elm\_icon\_standard\_set(ic, \textcolor{stringliteral}{"home"});

   evas\_object\_size\_hint\_aspect\_set(ic, EVAS\_ASPECT\_CONTROL\_VERTICAL, 1, 1);
   \textcolor{keywordflow}{return} ic;
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_append\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *list = data;

   elm\_genlist\_item\_append(list, \_itc,
                           (\textcolor{keywordtype}{void} *)(\textcolor{keywordtype}{long})nitems++, NULL,
                           ELM\_GENLIST\_ITEM\_NONE,
                           \_item\_sel\_cb, NULL);

\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_prepend\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *list = data;

   elm\_genlist\_item\_prepend(list, \_itc,
                            (\textcolor{keywordtype}{void} *)(\textcolor{keywordtype}{long})nitems++, NULL,
                            ELM\_GENLIST\_ITEM\_NONE,
                            \_item\_sel\_cb, NULL);

\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_insert\_before\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *list = data;
   Elm_Object_Item *glit = elm\_genlist\_selected\_item\_get(list);

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

   elm\_genlist\_item\_insert\_before(list, \_itc,
                                  (\textcolor{keywordtype}{void} *)(\textcolor{keywordtype}{long})nitems++, NULL,
                                  glit, ELM\_GENLIST\_ITEM\_NONE,
                                  \_item\_sel\_cb, NULL);

\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_insert\_after\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *list = data;
   Elm_Object_Item *glit = elm\_genlist\_selected\_item\_get(list);

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

   elm\_genlist\_item\_insert\_after(list, \_itc,
                                 (\textcolor{keywordtype}{void} *)(\textcolor{keywordtype}{long})nitems++, NULL,
                                 glit, ELM\_GENLIST\_ITEM\_NONE,
                                 \_item\_sel\_cb, NULL);

\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_next\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *list = data;
   Elm_Object_Item *glit = elm\_genlist\_selected\_item\_get(list);

   \textcolor{keywordflow}{if} (glit) glit = elm\_genlist\_item\_next\_get(glit);
   \textcolor{keywordflow}{if} (!glit) glit = elm\_genlist\_first\_item\_get(list);

   elm\_genlist\_item\_selected\_set(glit, EINA\_TRUE);
   elm\_genlist\_item\_show(glit, ELM\_GENLIST\_ITEM\_SCROLLTO\_IN);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_prev\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *list = data;
   Elm_Object_Item *glit = elm\_genlist\_selected\_item\_get(list);

   \textcolor{keywordflow}{if} (glit) glit = elm\_genlist\_item\_prev\_get(glit);
   \textcolor{keywordflow}{if} (!glit) glit = elm\_genlist\_last\_item\_get(list);

   elm\_genlist\_item\_selected\_set(glit, EINA\_TRUE);
   elm\_genlist\_item\_show(glit, ELM\_GENLIST\_ITEM\_SCROLLTO\_IN);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_bring\_in\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Elm_Object_Item *glit = data;
   \textcolor{keywordflow}{if} (!glit) \textcolor{keywordflow}{return};

   elm\_genlist\_item\_bring\_in(glit, ELM\_GENLIST\_ITEM\_SCROLLTO\_IN);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_show\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Elm_Object_Item *glit = data;
   \textcolor{keywordflow}{if} (!glit) \textcolor{keywordflow}{return};

   elm\_genlist\_item\_show(glit, ELM\_GENLIST\_ITEM\_SCROLLTO\_IN);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_middle\_in\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Elm_Object_Item *glit = data;
   \textcolor{keywordflow}{if} (!glit) \textcolor{keywordflow}{return};

   elm\_genlist\_item\_bring\_in(glit, ELM\_GENLIST\_ITEM\_SCROLLTO\_MIDDLE);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_middle\_show\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Elm_Object_Item *glit = data;
   \textcolor{keywordflow}{if} (!glit) \textcolor{keywordflow}{return};

   elm\_genlist\_item\_show(glit, ELM\_GENLIST\_ITEM\_SCROLLTO\_MIDDLE);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_top\_in\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Elm_Object_Item *glit = data;
   \textcolor{keywordflow}{if} (!glit) \textcolor{keywordflow}{return};

   elm\_genlist\_item\_bring\_in(glit, ELM\_GENLIST\_ITEM\_SCROLLTO\_TOP);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_top\_show\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Elm_Object_Item *glit = data;
   \textcolor{keywordflow}{if} (!glit) \textcolor{keywordflow}{return};

   elm\_genlist\_item\_show(glit, ELM\_GENLIST\_ITEM\_SCROLLTO\_TOP);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_realize\_cb(\textcolor{keywordtype}{void} *data, Evas\_Object *o, \textcolor{keywordtype}{void} *event\_info)
\{
   Evas\_Object *list = data;
   Elm_Object_Item *glit = elm\_genlist\_selected\_item\_get(list);
   \textcolor{keywordflow}{if} (!glit) \textcolor{keywordflow}{return};

   elm\_genlist\_item\_update(glit);
\}

\textcolor{keyword}{static} Evas\_Object *
\_button\_add(Evas\_Object *list, Evas\_Object *box, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *label, Evas\_Smart\_Cb cb)
\{
   Evas\_Object *bt;

   bt = elm_button_add(elm_object_parent_widget_get(list));
   elm\_object\_text\_set(bt, label);
   elm\_box\_pack\_end(box, bt);
   evas\_object\_show(bt);

   \textcolor{keywordflow}{if} (cb)
     evas\_object\_smart\_callback\_add(bt, \textcolor{stringliteral}{"clicked"}, cb, list);

   \textcolor{keywordflow}{return} bt;
\}

EAPI\_MAIN \textcolor{keywordtype}{int}
elm\_main(\textcolor{keywordtype}{int} argc, \textcolor{keywordtype}{char} **argv)
\{
   Evas\_Object *win, *box, *fbox;
   Evas\_Object *list;
   Evas\_Object *bt\_bring\_in, *bt\_show;
   Evas\_Object *bt\_middle\_in, *bt\_middle\_show;
   Evas\_Object *bt\_top\_in, *bt\_top\_show;
   \textcolor{keywordtype}{int} i;

   win = elm_win_util_standard_add(\textcolor{stringliteral}{"genlist"}, \textcolor{stringliteral}{"Genlist"});
   elm_policy_set(ELM\_POLICY\_QUIT, ELM\_POLICY\_QUIT\_LAST\_WINDOW\_CLOSED);
   elm\_win\_autodel\_set(win, EINA\_TRUE);

   box = elm_box_add(win);
   evas\_object\_size\_hint\_weight\_set(box, EVAS\_HINT\_EXPAND, EVAS\_HINT\_EXPAND);
   elm\_win\_resize\_object\_add(win, box);
   evas\_object\_show(box);

   \textcolor{keywordflow}{if} (!\_itc)
     \{
        \_itc = elm_genlist_item_class_new();
        \_itc->item\_style = \textcolor{stringliteral}{"default"};
        \_itc->func.text\_get = \_item\_label\_get;
        \_itc->func.content\_get = \_item\_content\_get;
        \_itc->func.state\_get = NULL;
        \_itc->func.del = NULL;
     \}

   \textcolor{keywordflow}{if} (!\_itc\_group)
     \{
        \_itc\_group = elm_genlist_item_class_new();
        \_itc\_group->item\_style = \textcolor{stringliteral}{"group\_index"};
        \_itc\_group->func.text\_get = \_group\_label\_get;
        \_itc\_group->func.content\_get = \_group\_content\_get;
        \_itc\_group->func.state\_get = NULL;
        \_itc\_group->func.del = NULL;
     \}
   list = elm_genlist_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);
   elm\_box\_pack\_end(box, list);
   evas\_object\_show(list);

   fbox = elm_box_add(win);
   elm\_box\_layout\_set(fbox, evas\_object\_box\_layout\_flow\_horizontal,
                      NULL, NULL);
   evas\_object\_size\_hint\_weight\_set(fbox, EVAS\_HINT\_EXPAND, 0);
   evas\_object\_size\_hint\_align\_set(fbox, EVAS\_HINT\_FILL, EVAS\_HINT\_FILL);
   elm\_box\_pack\_end(box, fbox);
   evas\_object\_show(fbox);

   \_button\_add(list, fbox, \textcolor{stringliteral}{"prepend"}, \_prepend\_cb);
   \_button\_add(list, fbox, \textcolor{stringliteral}{"append"}, \_append\_cb);
   \_button\_add(list, fbox, \textcolor{stringliteral}{"insert before"}, \_insert\_before\_cb);
   \_button\_add(list, fbox, \textcolor{stringliteral}{"insert after"}, \_insert\_after\_cb);
   \_button\_add(list, fbox, \textcolor{stringliteral}{"prev"}, \_prev\_cb);
   \_button\_add(list, fbox, \textcolor{stringliteral}{"next"}, \_next\_cb);
   \_button\_add(list, fbox, \textcolor{stringliteral}{"realize"}, \_realize\_cb);
   bt\_bring\_in = \_button\_add(list, fbox, \textcolor{stringliteral}{"bring #50"}, NULL);
   bt\_show = \_button\_add(list, fbox, \textcolor{stringliteral}{"show #50"}, NULL);
   bt\_middle\_in = \_button\_add(list, fbox, \textcolor{stringliteral}{"bring to middle #200"}, NULL);
   bt\_middle\_show = \_button\_add(list, fbox, \textcolor{stringliteral}{"show in middle #200"}, NULL);
   bt\_top\_in = \_button\_add(list, fbox, \textcolor{stringliteral}{"bring to top #250"}, NULL);
   bt\_top\_show = \_button\_add(list, fbox, \textcolor{stringliteral}{"show in top #250"}, NULL);

   \textcolor{keywordflow}{for} (i = 0; i < N\_ITEMS; i++)
     \{
        Elm_Object_Item *gli = NULL, *glg = NULL;

        \textcolor{keywordflow}{if} (i % 7 == 0)
          \{
             glg = gli = elm\_genlist\_item\_append(list, \_itc\_group,
                                                 (\textcolor{keywordtype}{void} *)(\textcolor{keywordtype}{long})nitems++, NULL,
                                                 ELM\_GENLIST\_ITEM\_GROUP,
                                                 \_item\_sel\_cb, NULL);
             elm\_genlist\_item\_select\_mode\_set(gli, ELM\_OBJECT\_SELECT\_MODE\_DISPLAY\_ONLY);
          \}
        \textcolor{keywordflow}{else}
          \{
             gli = elm\_genlist\_item\_append(list, \_itc,
                                           (\textcolor{keywordtype}{void} *)(\textcolor{keywordtype}{long})nitems++, glg,
                                           ELM\_GENLIST\_ITEM\_NONE,
                                           \_item\_sel\_cb, NULL);
          \}

        \textcolor{keywordflow}{switch} (i)
          \{
           \textcolor{keywordflow}{case} 3:
              elm\_object\_item\_disabled\_set(gli, EINA\_TRUE);
              \textcolor{keywordflow}{break};
           \textcolor{keywordflow}{case} 50:
              evas\_object\_smart\_callback\_add(
                 bt\_bring\_in, \textcolor{stringliteral}{"clicked"}, \_bring\_in\_cb, gli);
              evas\_object\_smart\_callback\_add(
                 bt\_show, \textcolor{stringliteral}{"clicked"}, \_show\_cb, gli);
              \textcolor{keywordflow}{break};
           \textcolor{keywordflow}{case} 200:
              evas\_object\_smart\_callback\_add(
                 bt\_middle\_in, \textcolor{stringliteral}{"clicked"}, \_middle\_in\_cb, gli);
              evas\_object\_smart\_callback\_add(
                 bt\_middle\_show, \textcolor{stringliteral}{"clicked"}, \_middle\_show\_cb, gli);
              \textcolor{keywordflow}{break};
           \textcolor{keywordflow}{case} 250:
              evas\_object\_smart\_callback\_add(
                 bt\_top\_in, \textcolor{stringliteral}{"clicked"}, \_top\_in\_cb, gli);
              evas\_object\_smart\_callback\_add(
                 bt\_top\_show, \textcolor{stringliteral}{"clicked"}, \_top\_show\_cb, gli);
          \}
     \}

   evas\_object\_resize(win, 420, 320);
   evas\_object\_show(win);

   elm_run();

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