elementary - Added 1 more API elm_object_item_del_cb_set()
[framework/uifw/elementary.git] / src / examples / index_example_01.c
1 /**
2  * Simple Elementary's <b>index widget</b> example, illustrating its
3  * usage and API.
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 #endif
18
19 struct example_data
20 {
21    Evas_Object *list, *index;
22 };
23
24 struct example_data d = {NULL, NULL};
25
26 static const char *dict[] = \
27 {
28 #include "dict.txt"
29 };
30
31 static void
32 _index_item_del(void        *data,
33                 Evas_Object *obj __UNUSED__,
34                 void        *event_info)
35 {
36    fprintf(stdout, "Deleting associated list node (%s). Comparing index "
37                    "item data reported via callback with the one returned by "
38                    "index's API on items: %s.\n",
39            elm_list_item_label_get(data),
40            data == elm_index_item_data_get(event_info) ? "OK" :
41            "FAIL, something went wrong");
42
43    elm_list_item_del(data);
44 }
45
46 /* delete an index item */
47 static void
48 _item_del(void        *data __UNUSED__,
49           Evas_Object *obj __UNUSED__,
50           void        *event_info __UNUSED__)
51 {
52    Elm_Object_Item *iit;
53    Elm_List_Item *lit = elm_index_item_selected_get(d.index, 0);
54
55    iit = elm_index_item_find(d.index, lit);
56
57    if (!iit) return;
58
59    fprintf(stdout, "Deleting last selected index item, which had letter"
60            " %s (pointing to %s)\n", elm_index_item_letter_get(iit),
61            elm_list_item_label_get(lit));
62
63    elm_index_item_del(d.index, lit);
64 }
65
66 static void
67 _item_del_all(void        *data __UNUSED__,
68               Evas_Object *obj __UNUSED__,
69               void        *event_info __UNUSED__)
70 {
71    elm_index_item_clear(d.index);
72 }
73
74 static void
75 _active_set(void        *data __UNUSED__,
76             Evas_Object *obj __UNUSED__,
77             void        *event_info __UNUSED__)
78 {
79    elm_index_active_set(d.index, !elm_index_active_get(d.index));
80
81    fprintf(stdout, "Toggling index programmatically.\n");
82 }
83
84 /* "delay,changed" hook */
85 static void
86 _index_changed(void        *data __UNUSED__,
87                Evas_Object *obj __UNUSED__,
88                void        *event_info)
89 {
90    elm_list_item_bring_in(event_info);
91 }
92
93 static void
94 _index_selected(void        *data __UNUSED__,
95                 Evas_Object *obj,
96                 void        *event_info)
97 {
98    Elm_List_Item *it = event_info;
99
100    fprintf(stdout, "New index item selected. Comparing item reported"
101                    " via callback with the selection returned by the API: "
102                    "%s.\n", it == elm_index_item_selected_get(obj, 0) ? "OK" :
103            "FAIL, something went wrong");
104 }
105
106 static void
107 _on_done(void        *data __UNUSED__,
108          Evas_Object *obj __UNUSED__,
109          void        *event_info __UNUSED__)
110 {
111    elm_exit();
112 }
113
114 int
115 elm_main(int    argc __UNUSED__,
116          char **argv __UNUSED__)
117 {
118    Evas_Object *win, *bg, *hbox, *vbox, *bt, *sep;
119    Elm_List_Item *it;
120    unsigned int i;
121    char curr = 0;
122
123    win = elm_win_add(NULL, "index", ELM_WIN_BASIC);
124    elm_win_title_set(win, "Index Example");
125    evas_object_smart_callback_add(win, "delete,request", _on_done, NULL);
126
127    bg = elm_bg_add(win);
128    elm_win_resize_object_add(win, bg);
129    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
130    evas_object_show(bg);
131
132    vbox = elm_box_add(win);
133    elm_win_resize_object_add(win, vbox);
134    evas_object_size_hint_weight_set(vbox, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
135    evas_object_show(vbox);
136
137    d.list = elm_list_add(win);
138    evas_object_size_hint_weight_set(d.list, EVAS_HINT_EXPAND,
139                                     EVAS_HINT_EXPAND);
140    evas_object_size_hint_align_set(d.list, EVAS_HINT_FILL, EVAS_HINT_FILL);
141    evas_object_show(d.list);
142    elm_box_pack_end(vbox, d.list);
143
144    d.index = elm_index_add(win);
145    elm_win_resize_object_add(win, d.index);
146    evas_object_size_hint_weight_set(d.index, EVAS_HINT_EXPAND,
147                                     EVAS_HINT_EXPAND);
148    evas_object_size_hint_align_set(d.list, EVAS_HINT_FILL, EVAS_HINT_FILL);
149    evas_object_show(d.index);
150
151    for (i = 0; i < (sizeof(dict) / sizeof(dict[0])); i++)
152      {
153         it = elm_list_item_append(d.list, dict[i], NULL, NULL, NULL, NULL);
154
155         if (curr != dict[i][0])
156           {
157              Elm_Object_Item *index_it;
158              char buf[32];
159
160              curr = dict[i][0];
161              /* indexing by first letters */
162
163              snprintf(buf, sizeof(buf), "%c", curr);
164              elm_index_item_append(d.index, buf, it);
165              index_it = elm_index_item_find(d.index, it);
166
167              elm_object_item_del_cb_set(index_it, _index_item_del);
168           }
169      }
170
171    evas_object_smart_callback_add(d.index, "delay,changed", _index_changed,
172                                   NULL);
173    evas_object_smart_callback_add(d.index, "selected", _index_selected, NULL);
174
175    /* attribute setting knobs */
176    sep = elm_separator_add(win);
177    elm_separator_horizontal_set(sep, EINA_TRUE);
178    elm_box_pack_end(vbox, sep);
179    evas_object_show(sep);
180
181    hbox = elm_box_add(win);
182    elm_box_horizontal_set(hbox, EINA_TRUE);
183    evas_object_size_hint_weight_set(hbox, EVAS_HINT_EXPAND, 0);
184    evas_object_size_hint_fill_set(hbox, EVAS_HINT_FILL, EVAS_HINT_FILL);
185    elm_box_pack_end(vbox, hbox);
186    evas_object_show(hbox);
187
188    bt = elm_button_add(win);
189    elm_object_text_set(bt, "bring in index");
190    evas_object_smart_callback_add(bt, "clicked", _active_set, NULL);
191    elm_box_pack_end(hbox, bt);
192    evas_object_show(bt);
193
194    bt = elm_button_add(win);
195    elm_object_text_set(bt, "delete last selected item");
196    evas_object_smart_callback_add(bt, "clicked", _item_del, NULL);
197    elm_box_pack_end(hbox, bt);
198    evas_object_show(bt);
199
200    bt = elm_button_add(win);
201    elm_object_text_set(bt, "delete all items");
202    evas_object_smart_callback_add(bt, "clicked", _item_del_all, NULL);
203    elm_box_pack_end(hbox, bt);
204    evas_object_show(bt);
205
206    evas_object_resize(win, 320, 600);
207    evas_object_show(win);
208
209    elm_run();
210    return 0;
211 }
212
213 ELM_MAIN()