EFL migration revision 67547
[framework/uifw/elementary.git] / tests / src / examples / genlist_example_05.c
1 //Compile with:
2 //gcc -g `pkg-config --cflags --libs elementary` genlist_example_04.c -o genlist_example_04
3
4 #include <Elementary.h>
5 #ifdef HAVE_CONFIG_H
6 # include "elementary_config.h"
7 #else
8 # define __UNUSED__
9 #endif
10
11 #define N_ITEMS 6
12
13 typedef struct _Node_Data {
14      Eina_List *children;
15      int value;
16      int level;
17      Eina_Bool favorite;
18 } Node_Data;
19
20 static Elm_Genlist_Item_Class _itc;
21 static Elm_Genlist_Item_Class _itp;
22 static Elm_Genlist_Item_Class _itfav;
23 static int nitems = 0;
24
25 static char *
26 _item_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part)
27 {
28    char buf[256];
29    Node_Data *d = data;
30
31    if (!strcmp(part, "elm.text"))
32      snprintf(buf, sizeof(buf), "Item # %i (level %i)", d->value, d->level);
33
34    return strdup(buf);
35 }
36
37 static Evas_Object *
38 _item_content_get(void *data __UNUSED__, Evas_Object *obj, const char *part)
39 {
40    Evas_Object *ic = elm_icon_add(obj);
41
42    if (!strcmp(part, "elm.swallow.icon"))
43      elm_icon_standard_set(ic, "file");
44
45    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
46    return ic;
47 }
48
49 static void
50 _item_sel_cb(void *data, Evas_Object *obj, void *event_info)
51 {
52    printf("sel item data [%p] on genlist obj [%p], item pointer [%p]\n",
53           data, obj, event_info);
54 }
55
56 static char *
57 _parent_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part __UNUSED__)
58 {
59    char buf[256];
60    Node_Data *d = data;
61
62    snprintf(buf, sizeof(buf), "Group %d (%d items)", d->value / 7,
63             eina_list_count(d->children));
64
65    return strdup(buf);
66 }
67
68 static Evas_Object *
69 _parent_content_get(void *data __UNUSED__, Evas_Object *obj, const char *part __UNUSED__)
70 {
71    Evas_Object *ic = elm_icon_add(obj);
72
73    if (!strcmp(part, "elm.swallow.icon"))
74      elm_icon_standard_set(ic, "folder");
75
76    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
77    return ic;
78 }
79
80 static char *
81 _favorite_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part)
82 {
83    char buf[256];
84    Node_Data *d = data;
85
86    if (!strcmp(part, "elm.text"))
87      snprintf(buf, sizeof(buf), "Favorite # %i", d->value);
88
89    return strdup(buf);
90 }
91
92 static Evas_Object *
93 _favorite_content_get(void *data __UNUSED__, Evas_Object *obj, const char *part)
94 {
95    Evas_Object *ic = elm_icon_add(obj);
96
97    if (!strcmp(part, "elm.swallow.icon"))
98      elm_icon_standard_set(ic, "apps");
99
100    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
101    return ic;
102 }
103
104 static void
105 _append_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
106 {
107    Evas_Object *list = data;
108    Elm_Object_Item *it, *parent = NULL;
109    Node_Data *pdata, *d = malloc(sizeof(*d));
110
111    d->children = NULL;
112    d->value = nitems++;
113    d->favorite = EINA_FALSE;
114
115    it = elm_genlist_selected_item_get(list);
116    if (it)
117      parent = elm_genlist_item_parent_get(it);
118
119    if (parent)
120      {
121         d->level = elm_genlist_item_expanded_depth_get(parent) + 1;
122         pdata = elm_genlist_item_data_get(parent);
123         pdata->children = eina_list_append(pdata->children, d);
124      }
125    else
126      d->level = 0;
127
128    elm_genlist_item_append(list, &_itc,
129                            d, parent,
130                            ELM_GENLIST_ITEM_NONE,
131                            _item_sel_cb, NULL);
132 }
133
134 static void
135 _favorite_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
136 {
137    Evas_Object *list = data;
138    Elm_Object_Item *it = elm_genlist_selected_item_get(list);
139
140    if (!it)
141      return;
142
143    Node_Data *d = elm_genlist_item_data_get(it);
144    d->favorite = !d->favorite;
145    if (d->favorite)
146      elm_genlist_item_item_class_update(it, &_itfav);
147    else
148      {
149         if (d->children)
150           elm_genlist_item_item_class_update(it, &_itp);
151         else
152           elm_genlist_item_item_class_update(it, &_itc);
153      }
154
155    elm_genlist_item_update(it);
156 }
157
158 static void
159 _add_child_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
160 {
161    Evas_Object *list = data;
162    Elm_Object_Item *it = elm_genlist_selected_item_get(list);
163    Elm_Object_Item *prev, *parent;
164
165    if (!it)
166      return;
167
168    Node_Data *d = elm_genlist_item_data_get(it);
169    prev = elm_genlist_item_prev_get(it);
170    parent = elm_genlist_item_parent_get(it);
171
172    Eina_Bool change_item = !d->children;
173
174    // creating new item data
175    Node_Data *ndata = malloc(sizeof(*ndata));
176    ndata->value = nitems++;
177    ndata->children = NULL;
178    ndata->favorite = EINA_FALSE;
179    ndata->level = elm_genlist_item_expanded_depth_get(it) + 1;
180    d->children = eina_list_append(d->children, ndata);
181
182    // Changing leaf item to parent item
183    if (change_item)
184      {
185         elm_genlist_item_del(it);
186
187         if (prev != parent)
188           it = elm_genlist_item_insert_after(list, &_itp, d, parent, prev,
189                                              ELM_GENLIST_ITEM_SUBITEMS,
190                                              _item_sel_cb, NULL);
191         else
192           it = elm_genlist_item_prepend(list, &_itp, d, parent,
193                                         ELM_GENLIST_ITEM_SUBITEMS,
194                                         _item_sel_cb, NULL);
195         elm_genlist_item_expanded_set(it, EINA_FALSE);
196         elm_genlist_item_selected_set(it, EINA_TRUE);
197      }
198    else if (elm_genlist_item_expanded_get(it))
199      {
200         elm_genlist_item_append(list, &_itc, ndata, it,
201                                 ELM_GENLIST_ITEM_NONE, _item_sel_cb, NULL);
202      }
203
204    elm_genlist_item_update(it);
205 }
206
207 static void
208 _clear_list(Node_Data *d)
209 {
210    Node_Data *tmp;
211
212    EINA_LIST_FREE(d->children, tmp)
213       _clear_list(tmp);
214    free(d);
215 }
216
217 static void
218 _del_item_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
219 {
220    Evas_Object *list = data;
221    Elm_Object_Item *it = elm_genlist_selected_item_get(list);
222    Elm_Object_Item *parent = NULL;
223
224    if (!it)
225      return;
226
227    Node_Data *pdata, *d = elm_genlist_item_data_get(it);
228    parent = elm_genlist_item_parent_get(it);
229    elm_genlist_item_subitems_clear(it);
230    elm_genlist_item_del(it);
231
232    _clear_list(d);
233
234    if (!parent)
235      return;
236
237    pdata = elm_genlist_item_data_get(parent);
238    pdata->children = eina_list_remove(pdata->children, d);
239    elm_genlist_item_update(parent);
240 }
241
242 static void
243 _expand_request_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
244 {
245    Elm_Object_Item *it = event_info;
246    printf("expand request on item: %p\n", event_info);
247    elm_genlist_item_expanded_set(it, EINA_TRUE);
248 }
249
250 static void
251 _contract_request_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
252 {
253    Elm_Object_Item *it = event_info;
254    printf("contract request on item: %p\n", event_info);
255    elm_genlist_item_expanded_set(it, EINA_FALSE);
256 }
257
258 static void
259 _expanded_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
260 {
261    Eina_List *l;
262    Elm_Object_Item *it = event_info;
263    Node_Data *it_data, *d = elm_genlist_item_data_get(it);
264    Evas_Object *list = elm_genlist_item_genlist_get(it);
265
266    Elm_Genlist_Item_Class *ic;
267
268    EINA_LIST_FOREACH(d->children, l, it_data)
269      {
270         Elm_Object_Item *nitem;
271         Elm_Genlist_Item_Flags flags = ELM_GENLIST_ITEM_NONE;
272         printf("expanding item: #%d from parent #%d\n", it_data->value, d->value);
273         if (it_data->favorite)
274           ic = &_itfav;
275         else if (it_data->children)
276           {
277              ic = &_itp;
278              flags = ELM_GENLIST_ITEM_SUBITEMS;
279           }
280         else
281           ic = &_itc;
282
283         nitem = elm_genlist_item_append(list, ic, it_data, it,
284                                         flags, _item_sel_cb, NULL);
285         elm_genlist_item_expanded_set(nitem, EINA_FALSE);
286      }
287 }
288
289 static void
290 _contracted_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
291 {
292    Elm_Object_Item *it = event_info;
293    elm_genlist_item_subitems_clear(it);
294 }
295
296 static Evas_Object *
297 _button_add(Evas_Object *list, Evas_Object *box, const char *label, Evas_Smart_Cb cb)
298 {
299    Evas_Object *bt;
300
301    bt = elm_button_add(elm_object_parent_widget_get(list));
302    elm_object_text_set(bt, label);
303    elm_box_pack_end(box, bt);
304    evas_object_show(bt);
305
306    if (cb)
307      evas_object_smart_callback_add(bt, "clicked", cb, list);
308
309    return bt;
310 }
311
312 int
313 elm_main(int argc __UNUSED__, char **argv __UNUSED__)
314 {
315    Evas_Object *win, *bg, *box, *fbox;
316    Evas_Object *list;
317    int i;
318
319    win = elm_win_add(NULL, "icon", ELM_WIN_BASIC);
320    elm_win_title_set(win, "Icon");
321    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
322    elm_win_autodel_set(win, EINA_TRUE);
323
324    bg = elm_bg_add(win);
325    elm_bg_color_set(bg, 255,255 ,255);
326    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
327    elm_win_resize_object_add(win, bg);
328    evas_object_show(bg);
329
330    box = elm_box_add(win);
331    evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
332    evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
333    elm_win_resize_object_add(win, box);
334    evas_object_show(box);
335
336    _itc.item_style = "default";
337    _itc.func.text_get = _item_text_get;
338    _itc.func.content_get = _item_content_get;
339    _itc.func.state_get = NULL;
340    _itc.func.del = NULL;
341
342    _itp.item_style = "default";
343    _itp.func.text_get = _parent_text_get;
344    _itp.func.content_get = _parent_content_get;
345    _itp.func.state_get = NULL;
346    _itp.func.del = NULL;
347
348    _itfav.item_style = "default";
349    _itfav.func.text_get = _favorite_text_get;
350    _itfav.func.content_get = _favorite_content_get;
351    _itfav.func.state_get = NULL;
352    _itfav.func.del = NULL;
353
354    list = elm_genlist_add(win);
355
356    evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
357    evas_object_size_hint_align_set(list, EVAS_HINT_FILL, EVAS_HINT_FILL);
358    elm_box_pack_end(box, list);
359    evas_object_show(list);
360
361    fbox = elm_box_add(win);
362    elm_box_layout_set(fbox, evas_object_box_layout_flow_horizontal,
363                       NULL, NULL);
364    evas_object_size_hint_weight_set(fbox, EVAS_HINT_EXPAND, 0);
365    evas_object_size_hint_align_set(fbox, EVAS_HINT_FILL, EVAS_HINT_FILL);
366    elm_box_pack_end(box, fbox);
367    evas_object_show(fbox);
368
369    _button_add(list, fbox, "append item", _append_cb);
370    _button_add(list, fbox, "favorite", _favorite_cb);
371    _button_add(list, fbox, "add child", _add_child_cb);
372    _button_add(list, fbox, "del item", _del_item_cb);
373
374    for (i = 0; i < N_ITEMS; i++)
375      {
376         Elm_Object_Item *gli, *glg;
377         Node_Data *data = malloc(sizeof(*data)); // data for this item
378         data->children = NULL;
379         data->value = i;
380         data->favorite = EINA_FALSE;
381         nitems++;
382
383         Node_Data *pdata; // data for the parent of the group
384
385         printf("creating item: #%d\n", data->value);
386         if (i % 3 == 0)
387           {
388              glg = gli = elm_genlist_item_append(list, &_itp, data, NULL,
389                                                  ELM_GENLIST_ITEM_SUBITEMS,
390                                                  _item_sel_cb, NULL);
391              elm_genlist_item_expanded_set(glg, EINA_TRUE);
392              pdata = data;
393              data->level = 0;
394           }
395         else
396           {
397              gli = elm_genlist_item_append(list, &_itc, data, glg,
398                                            ELM_GENLIST_ITEM_NONE,
399                                            _item_sel_cb, NULL);
400              pdata->children = eina_list_append(pdata->children, data);
401              data->level = 1;
402           }
403      }
404
405    evas_object_smart_callback_add(list, "expand,request", _expand_request_cb, list);
406    evas_object_smart_callback_add(list, "contract,request", _contract_request_cb, list);
407    evas_object_smart_callback_add(list, "expanded", _expanded_cb, list);
408    evas_object_smart_callback_add(list, "contracted", _contracted_cb, list);
409
410    evas_object_size_hint_min_set(bg, 160, 160);
411    evas_object_size_hint_max_set(bg, 640, 800);
412    evas_object_resize(win, 420, 600);
413    evas_object_show(win);
414
415    elm_run();
416
417    return 0;
418 }
419
420 ELM_MAIN()