[elementary] Documentation for the index widget.
[framework/uifw/elementary.git] / src / examples / index_example_02.c
1 /**
2  * Simple Elementary's <b>index widget</b> example, illustrating its
3  * usage and API -- now with sorted insertions.
4  *
5  * See stdout/stderr for output. Compile with:
6  *
7  * @verbatim
8  * gcc -g `pkg-config --cflags --libs elementary` index_example.c -o index_example
9  * @endverbatim
10  */
11
12 #include <Elementary.h>
13 #ifdef HAVE_CONFIG_H
14 # include "elementary_config.h"
15 #else
16 # define __UNUSED__
17 # define PACKAGE_DATA_DIR "../../data"
18 #endif
19
20 static const char *items[] = \
21 {
22    "Judith",
23    "Paulina",
24    "Cathy",
25    "Vendella",
26    "Naomi",
27    "Ashley",
28    "Stacey",
29    "Gail"
30 };
31
32 static void
33 _index_changed(void        *data __UNUSED__,
34                Evas_Object *obj __UNUSED__,
35                void        *event_info)
36 {
37    elm_gengrid_item_bring_in(event_info);
38 }
39
40 static void
41 _on_done(void        *data __UNUSED__,
42          Evas_Object *obj __UNUSED__,
43          void        *event_info __UNUSED__)
44 {
45    elm_exit();
46 }
47
48 static char *
49 _grid_label_get(void        *data,
50                 Evas_Object *obj __UNUSED__,
51                 const char  *part __UNUSED__)
52 {
53    int idx = (int)data;
54    return strdup(items[idx]);
55 }
56
57 Evas_Object *
58 _grid_icon_get(void        *data __UNUSED__,
59                Evas_Object *obj,
60                const char  *part)
61 {
62    if (!strcmp(part, "elm.swallow.icon"))
63      {
64         char buf[PATH_MAX];
65         snprintf(buf, sizeof(buf), "%s/images/%s", PACKAGE_DATA_DIR,
66                  "sky_01.jpg");
67
68         Evas_Object *icon = elm_bg_add(obj);
69         elm_bg_file_set(icon, buf, NULL);
70         evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1,
71                                          1);
72         evas_object_show(icon);
73         return icon;
74      }
75
76    return NULL;
77 }
78
79 /* ordering alphabetically */
80 static int
81 _index_icmp(const void *data1,
82             const void *data2)
83 {
84    const char *label1, *label2;
85
86    const Elm_Index_Item *it1 = data1;
87    const Elm_Index_Item *it2 = data2;
88
89    label1 = elm_index_item_letter_get(it1);
90    label2 = elm_index_item_letter_get(it2);
91
92    return strcasecmp(label1, label2);
93 }
94
95 int
96 elm_main(int    argc __UNUSED__,
97          char **argv __UNUSED__)
98 {
99    Evas_Object *win, *bg, *grid, *index;
100    Elm_Gengrid_Item *it;
101    unsigned int i;
102
103    Elm_Gengrid_Item_Class gic;
104
105    win = elm_win_add(NULL, "index", ELM_WIN_BASIC);
106    elm_win_title_set(win, "Index Example");
107    evas_object_smart_callback_add(win, "delete,request", _on_done, NULL);
108
109    bg = elm_bg_add(win);
110    elm_win_resize_object_add(win, bg);
111    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
112    evas_object_show(bg);
113
114    grid = elm_gengrid_add(win);
115    elm_gengrid_item_size_set(grid, 150, 150);
116
117    gic.item_style = "default";
118    gic.func.label_get = _grid_label_get;
119    gic.func.icon_get = _grid_icon_get;
120    gic.func.state_get = NULL;
121    gic.func.del = NULL;
122
123    evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
124    elm_win_resize_object_add(win, grid);
125    evas_object_show(grid);
126
127    index = elm_index_add(win);
128    evas_object_size_hint_weight_set(index, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
129    elm_win_resize_object_add(win, index);
130
131    evas_object_show(index);
132
133    for (i = 0; i < (sizeof(items) / sizeof(items[0])); i++)
134      {
135         char buf[32];
136
137         it = elm_gengrid_item_append(grid, &gic, (void *)i, NULL, NULL);
138
139         /* indexing by first letters */
140         snprintf(buf, sizeof(buf), "%c", items[i][0]);
141         elm_index_item_sorted_insert(index, buf, it, _index_icmp, NULL);
142      }
143
144    evas_object_smart_callback_add(index, "delay,changed", _index_changed, NULL);
145
146    evas_object_resize(win, 320, 300);
147    evas_object_show(win);
148
149    elm_index_active_set(index, EINA_TRUE);
150
151    elm_run();
152    return 0;
153 }
154
155 ELM_MAIN()