SVN Merging for button, radio, check and toggle
[framework/uifw/elementary.git] / src / examples / diskselector_example_02.c
1 /**
2  * Elementary's <b>diskselector widget</b> example, illustrating its
3  * covering most of item functions.
4  *
5  * See stdout/stderr for output. Compile with:
6  *
7  * @verbatim
8  * gcc -g `pkg-config --cflags --libs elementary` diskselector_example_02.c -o diskselector_example_02
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 static int counter = 3;
20
21 static void
22 _on_done(void        *data __UNUSED__,
23          Evas_Object *obj __UNUSED__,
24          void        *event_info __UNUSED__)
25 {
26    elm_exit();
27 }
28
29 static void
30 _add_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
31 {
32    Elm_Object_Item *ds_it;
33    Evas_Object *ds = data;
34    char label[32];
35
36    snprintf(label, sizeof(label), "Item %i", counter++);
37    ds_it = elm_diskselector_item_append(ds, label, NULL, NULL, NULL);
38    if (!ds_it) printf("Error adding item\n");
39 }
40
41 static void
42 _add_ic_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
43 {
44    Elm_Object_Item *ds_it;
45    Evas_Object *ic, *ds = data;
46    char label[32];
47
48    snprintf(label, sizeof(label), "Item %i", counter++);
49    ic = elm_icon_add(ds);
50    elm_icon_standard_set(ic, "home");
51
52    ds_it = elm_diskselector_item_append(ds, label, ic, NULL, NULL);
53    if (!ds_it) printf("Error adding item with icon\n");
54 }
55
56 static void
57 _sel_cb(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
58 {
59    Elm_Object_Item *ds_it = event_info;
60    printf("Selected label: %s\n", elm_object_item_text_get(ds_it));
61 }
62
63 static void
64 _add_func_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
65 {
66    Elm_Object_Item *ds_it;
67    Evas_Object *ds = data;
68    char label[32];
69
70    snprintf(label, sizeof(label), "Item %i", counter++);
71    ds_it = elm_diskselector_item_append(ds, label, NULL, _sel_cb, NULL);
72    if (!ds_it) printf("Error adding item\n");
73 }
74
75 static void
76 _sel_data_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info)
77 {
78    char *content = data;
79    Elm_Object_Item *ds_it = event_info;
80    printf("Selected label: %s with data: %s\n",
81           elm_object_item_text_get(ds_it), content);
82 }
83
84 static void
85 _free_data(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
86 {
87    free(data);
88 }
89
90 static void
91 _add_data_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
92 {
93    Elm_Object_Item *ds_it;
94    Evas_Object *ds = data;
95    char label[32];
96    char *content = malloc(sizeof(char) * 32);
97
98    snprintf(content, 32, "Item content %i", counter);
99    snprintf(label, sizeof(label), "Item %i", counter++);
100    ds_it = elm_diskselector_item_append(ds, label, NULL, _sel_data_cb, content);
101    if (!ds_it) {
102      printf("Error adding item\n");
103      return;
104    }
105    elm_object_item_del_cb_set(ds_it, _free_data);
106 }
107
108 static void
109 _del_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
110 {
111    Elm_Object_Item *selected_ds_it;
112    Evas_Object *ds = data;
113
114    selected_ds_it = elm_diskselector_selected_item_get(ds);
115    elm_object_item_del(selected_ds_it);
116 }
117
118 static void
119 _unselect_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
120 {
121    Elm_Object_Item *selected_ds_it;
122    Evas_Object *ds = data;
123
124    selected_ds_it = elm_diskselector_selected_item_get(ds);
125    elm_diskselector_item_selected_set(selected_ds_it, EINA_FALSE);
126 }
127
128 static void
129 _print_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
130 {
131    const Eina_List *l, *items;
132    Elm_Object_Item *ds_it;
133    Evas_Object *ds = data;
134
135    items = elm_diskselector_items_get(ds);
136    EINA_LIST_FOREACH(items, l, ds_it)
137      printf("%s\n", elm_object_item_text_get(ds_it));
138 }
139
140 static void
141 _clear_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
142 {
143    Evas_Object *ds = data;
144    elm_diskselector_clear(ds);
145 }
146
147 static void
148 _select_first_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
149 {
150    Elm_Object_Item *first_ds_it;
151    Evas_Object *ds = data;
152
153    first_ds_it = elm_diskselector_first_item_get(ds);
154    if (first_ds_it)
155      elm_diskselector_item_selected_set(first_ds_it, EINA_TRUE);
156 }
157
158 static void
159 _select_last_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
160 {
161    Elm_Object_Item *last_ds_it;
162    Evas_Object *ds = data;
163
164    last_ds_it = elm_diskselector_last_item_get(ds);
165    if (last_ds_it)
166      elm_diskselector_item_selected_set(last_ds_it, EINA_TRUE);
167 }
168
169 static void
170 _select_next_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
171 {
172    Elm_Object_Item *selected_ds_it, *next_ds_it;
173    Evas_Object *ds = data;
174
175    selected_ds_it = elm_diskselector_selected_item_get(ds);
176    if (!selected_ds_it) return;
177
178    next_ds_it = elm_diskselector_item_next_get(selected_ds_it);
179    if (next_ds_it)
180      elm_diskselector_item_selected_set(next_ds_it, EINA_TRUE);
181 }
182
183 static void
184 _select_prev_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
185 {
186    Elm_Object_Item *selected_ds_it, *prev_ds_it;
187    Evas_Object *ds = data;
188
189    selected_ds_it = elm_diskselector_selected_item_get(ds);
190    if (!selected_ds_it) return;
191
192    prev_ds_it = elm_diskselector_item_prev_get(selected_ds_it);
193    if (prev_ds_it)
194      elm_diskselector_item_selected_set(prev_ds_it, EINA_TRUE);
195 }
196
197 EAPI_MAIN int
198 elm_main(int argc __UNUSED__, char **argv __UNUSED__)
199 {
200    Evas_Object *win, *bg, *bx, *hbx, *ds, *bt;
201
202    win = elm_win_add(NULL, "diskselector", ELM_WIN_BASIC);
203    elm_win_title_set(win, "Diskselector Items Example");
204    evas_object_smart_callback_add(win, "delete,request", _on_done, NULL);
205
206    bg = elm_bg_add(win);
207    elm_win_resize_object_add(win, bg);
208    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
209    evas_object_show(bg);
210
211    bx = elm_box_add(win);
212    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
213    elm_win_resize_object_add(win, bx);
214    evas_object_show(bx);
215
216    ds = elm_diskselector_add(win);
217    evas_object_size_hint_weight_set(ds, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
218    evas_object_size_hint_align_set(ds, EVAS_HINT_FILL, EVAS_HINT_FILL);
219    elm_box_pack_end(bx, ds);
220    evas_object_show(ds);
221
222    elm_diskselector_item_append(ds, "Item 0", NULL, NULL, NULL);
223    elm_diskselector_item_append(ds, "Item 1", NULL, NULL, NULL);
224    elm_diskselector_item_append(ds, "Item 2", NULL, NULL, NULL);
225
226    hbx = elm_box_add(win);
227    elm_box_horizontal_set(hbx, EINA_TRUE);
228    evas_object_size_hint_weight_set(hbx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
229    evas_object_size_hint_align_set(hbx, EVAS_HINT_FILL, EVAS_HINT_FILL);
230    elm_box_pack_end(bx, hbx);
231    evas_object_show(hbx);
232
233    bt = elm_button_add(win);
234    elm_object_text_set(bt, "Append item");
235    evas_object_smart_callback_add(bt, "clicked", _add_cb, ds);
236    elm_box_pack_end(hbx, bt);
237    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
238    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
239    evas_object_show(bt);
240
241    bt = elm_button_add(win);
242    elm_object_text_set(bt, "Append with icon");
243    evas_object_smart_callback_add(bt, "clicked", _add_ic_cb, ds);
244    elm_box_pack_end(hbx, bt);
245    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
246    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
247    evas_object_show(bt);
248
249    bt = elm_button_add(win);
250    elm_object_text_set(bt, "Append with selected function");
251    evas_object_smart_callback_add(bt, "clicked", _add_func_cb, ds);
252    elm_box_pack_end(hbx, bt);
253    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
254    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
255    evas_object_show(bt);
256
257    bt = elm_button_add(win);
258    elm_object_text_set(bt, "Append with data");
259    evas_object_smart_callback_add(bt, "clicked", _add_data_cb, ds);
260    elm_box_pack_end(hbx, bt);
261    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
262    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
263    evas_object_show(bt);
264
265    hbx = elm_box_add(win);
266    elm_box_horizontal_set(hbx, EINA_TRUE);
267    evas_object_size_hint_weight_set(hbx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
268    evas_object_size_hint_align_set(hbx, EVAS_HINT_FILL, EVAS_HINT_FILL);
269    elm_box_pack_end(bx, hbx);
270    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
271    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
272    evas_object_show(hbx);
273
274    bt = elm_button_add(win);
275    elm_object_text_set(bt, "Delete item");
276    evas_object_smart_callback_add(bt, "clicked", _del_cb, ds);
277    elm_box_pack_end(hbx, bt);
278    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
279    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
280    evas_object_show(bt);
281
282    bt = elm_button_add(win);
283    elm_object_text_set(bt, "Unselect item");
284    evas_object_smart_callback_add(bt, "clicked", _unselect_cb, ds);
285    elm_box_pack_end(hbx, bt);
286    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
287    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
288    evas_object_show(bt);
289
290    bt = elm_button_add(win);
291    elm_object_text_set(bt, "Print items");
292    evas_object_smart_callback_add(bt, "clicked", _print_cb, ds);
293    elm_box_pack_end(hbx, bt);
294    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
295    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
296    evas_object_show(bt);
297
298    bt = elm_button_add(win);
299    elm_object_text_set(bt, "Clear");
300    evas_object_smart_callback_add(bt, "clicked", _clear_cb, ds);
301    elm_box_pack_end(hbx, bt);
302    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
303    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
304    evas_object_show(bt);
305
306    hbx = elm_box_add(win);
307    elm_box_horizontal_set(hbx, EINA_TRUE);
308    evas_object_size_hint_weight_set(hbx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
309    evas_object_size_hint_align_set(hbx, EVAS_HINT_FILL, EVAS_HINT_FILL);
310    elm_box_pack_end(bx, hbx);
311    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
312    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
313    evas_object_show(hbx);
314
315    bt = elm_button_add(win);
316    elm_object_text_set(bt, "Select first item");
317    evas_object_smart_callback_add(bt, "clicked", _select_first_cb, ds);
318    elm_box_pack_end(hbx, bt);
319    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
320    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
321    evas_object_show(bt);
322
323    bt = elm_button_add(win);
324    elm_object_text_set(bt, "Select last item");
325    evas_object_smart_callback_add(bt, "clicked", _select_last_cb, ds);
326    elm_box_pack_end(hbx, bt);
327    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
328    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
329    evas_object_show(bt);
330
331    bt = elm_button_add(win);
332    elm_object_text_set(bt, "Select next item");
333    evas_object_smart_callback_add(bt, "clicked", _select_next_cb, ds);
334    elm_box_pack_end(hbx, bt);
335    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
336    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
337    evas_object_show(bt);
338
339    bt = elm_button_add(win);
340    elm_object_text_set(bt, "Select previous item");
341    evas_object_smart_callback_add(bt, "clicked", _select_prev_cb, ds);
342    elm_box_pack_end(hbx, bt);
343    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
344    evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
345    evas_object_show(bt);
346
347    evas_object_show(win);
348
349    elm_run();
350    return 0;
351 }
352 ELM_MAIN()