SVN Merging for button, radio, check and toggle
[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_object_item_text_get(data),
40            data == elm_index_item_data_get(event_info) ? "OK" :
41            "FAIL, something went wrong");
42
43 <<<<<<< HEAD
44    elm_list_item_del(data);
45 =======
46    elm_object_item_del(data);
47 >>>>>>> remotes/origin/upstream
48 }
49
50 /* delete an index item */
51 static void
52 _item_del(void        *data __UNUSED__,
53           Evas_Object *obj __UNUSED__,
54           void        *event_info __UNUSED__)
55 {
56    Elm_Object_Item *iit;
57    Elm_Object_Item *lit = elm_index_item_selected_get(d.index, 0);
58
59    iit = elm_index_item_find(d.index, lit);
60
61    if (!iit) return;
62
63    fprintf(stdout, "Deleting last selected index item, which had letter"
64            " %s (pointing to %s)\n", elm_index_item_letter_get(iit),
65            elm_object_item_text_get(lit));
66
67    elm_object_item_del(lit);
68 }
69
70 static void
71 _item_del_all(void        *data __UNUSED__,
72               Evas_Object *obj __UNUSED__,
73               void        *event_info __UNUSED__)
74 {
75    elm_index_item_clear(d.index);
76 }
77
78 static void
79 _active_set(void        *data __UNUSED__,
80             Evas_Object *obj __UNUSED__,
81             void        *event_info __UNUSED__)
82 {
83    elm_index_active_set(d.index, !elm_index_active_get(d.index));
84
85    fprintf(stdout, "Toggling index programmatically.\n");
86 }
87
88 /* "delay,changed" hook */
89 static void
90 _index_changed(void        *data __UNUSED__,
91                Evas_Object *obj __UNUSED__,
92                void        *event_info)
93 {
94    elm_list_item_bring_in(event_info);
95 }
96
97 static void
98 _index_selected(void        *data __UNUSED__,
99                 Evas_Object *obj,
100                 void        *event_info)
101 {
102    Elm_Object_Item *lit = event_info;
103
104    fprintf(stdout, "New index item selected. Comparing item reported"
105                    " via callback with the selection returned by the API: "
106                    "%s.\n", lit == elm_index_item_selected_get(obj, 0) ? "OK" :
107            "FAIL, something went wrong");
108 }
109
110 static void
111 _on_done(void        *data __UNUSED__,
112          Evas_Object *obj __UNUSED__,
113          void        *event_info __UNUSED__)
114 {
115    elm_exit();
116 }
117
118 int
119 elm_main(int    argc __UNUSED__,
120          char **argv __UNUSED__)
121 {
122    Evas_Object *win, *bg, *hbox, *vbox, *bt, *sep;
123    Elm_Object_Item *lit;
124 <<<<<<< HEAD
125
126 =======
127 >>>>>>> remotes/origin/upstream
128    unsigned int i;
129    char curr = 0;
130
131    win = elm_win_add(NULL, "index", ELM_WIN_BASIC);
132    elm_win_title_set(win, "Index Example");
133    evas_object_smart_callback_add(win, "delete,request", _on_done, NULL);
134
135    bg = elm_bg_add(win);
136    elm_win_resize_object_add(win, bg);
137    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
138    evas_object_show(bg);
139
140    vbox = elm_box_add(win);
141    elm_win_resize_object_add(win, vbox);
142    evas_object_size_hint_weight_set(vbox, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
143    evas_object_show(vbox);
144
145    d.list = elm_list_add(win);
146    evas_object_size_hint_weight_set(d.list, 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.list);
150    elm_box_pack_end(vbox, d.list);
151
152    d.index = elm_index_add(win);
153    elm_win_resize_object_add(win, d.index);
154    evas_object_size_hint_weight_set(d.index, EVAS_HINT_EXPAND,
155                                     EVAS_HINT_EXPAND);
156    evas_object_size_hint_align_set(d.list, EVAS_HINT_FILL, EVAS_HINT_FILL);
157    evas_object_show(d.index);
158
159    for (i = 0; i < (sizeof(dict) / sizeof(dict[0])); i++)
160      {
161         lit = elm_list_item_append(d.list, dict[i], NULL, NULL, NULL, NULL);
162
163         if (curr != dict[i][0])
164           {
165              Elm_Object_Item *index_it;
166              char buf[32];
167
168              curr = dict[i][0];
169              /* indexing by first letters */
170
171              snprintf(buf, sizeof(buf), "%c", curr);
172 <<<<<<< HEAD
173
174              elm_index_item_append(d.index, buf, lit);
175 =======
176              elm_index_item_append(d.index, buf, NULL, lit);
177 >>>>>>> remotes/origin/upstream
178              index_it = elm_index_item_find(d.index, lit);
179
180              elm_object_item_del_cb_set(index_it, _index_item_del);
181           }
182      }
183
184    evas_object_smart_callback_add(d.index, "delay,changed", _index_changed,
185                                   NULL);
186    evas_object_smart_callback_add(d.index, "selected", _index_selected, NULL);
187
188    /* attribute setting knobs */
189    sep = elm_separator_add(win);
190    elm_separator_horizontal_set(sep, EINA_TRUE);
191    elm_box_pack_end(vbox, sep);
192    evas_object_show(sep);
193
194    hbox = elm_box_add(win);
195    elm_box_horizontal_set(hbox, EINA_TRUE);
196    evas_object_size_hint_weight_set(hbox, EVAS_HINT_EXPAND, 0);
197    evas_object_size_hint_fill_set(hbox, EVAS_HINT_FILL, EVAS_HINT_FILL);
198    elm_box_pack_end(vbox, hbox);
199    evas_object_show(hbox);
200
201    bt = elm_button_add(win);
202    elm_object_text_set(bt, "bring in index");
203    evas_object_smart_callback_add(bt, "clicked", _active_set, NULL);
204    elm_box_pack_end(hbox, bt);
205    evas_object_show(bt);
206
207    bt = elm_button_add(win);
208    elm_object_text_set(bt, "delete last selected item");
209    evas_object_smart_callback_add(bt, "clicked", _item_del, NULL);
210    elm_box_pack_end(hbox, bt);
211    evas_object_show(bt);
212
213    bt = elm_button_add(win);
214    elm_object_text_set(bt, "delete all items");
215    evas_object_smart_callback_add(bt, "clicked", _item_del_all, NULL);
216    elm_box_pack_end(hbox, bt);
217    evas_object_show(bt);
218
219    evas_object_resize(win, 320, 600);
220    evas_object_show(win);
221
222    elm_run();
223    return 0;
224 }
225
226 ELM_MAIN()