[JungWooHyun] doing merge job ~
[framework/uifw/elementary.git] / 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 <<<<<<< HEAD
21 static Elm_Genlist_Item_Class _itc;
22 static Elm_Genlist_Item_Class _itp;
23 static Elm_Genlist_Item_Class _itfav;
24 static int nitems = 0;
25
26 static char *
27 _item_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part)
28 =======
29 static Elm_Genlist_Item_Class *_itc = NULL;
30 static Elm_Genlist_Item_Class *_itp = NULL;
31 static Elm_Genlist_Item_Class *_itfav = NULL;
32 static int nitems = 0;
33
34 static char *
35 _item_label_get(void *data, Evas_Object *obj __UNUSED__, const char *part)
36 >>>>>>> remotes/origin/upstream
37 {
38    char buf[256];
39    Node_Data *d = data;
40
41    if (!strcmp(part, "elm.text"))
42      snprintf(buf, sizeof(buf), "Item # %i (level %i)", d->value, d->level);
43
44    return strdup(buf);
45 }
46
47 static Evas_Object *
48 _item_content_get(void *data __UNUSED__, Evas_Object *obj, const char *part)
49 {
50    Evas_Object *ic = elm_icon_add(obj);
51
52    if (!strcmp(part, "elm.swallow.icon"))
53      elm_icon_standard_set(ic, "file");
54
55    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
56    return ic;
57 }
58
59 static void
60 _item_sel_cb(void *data, Evas_Object *obj, void *event_info)
61 {
62    printf("sel item data [%p] on genlist obj [%p], item pointer [%p]\n",
63           data, obj, event_info);
64 }
65
66 static char *
67 <<<<<<< HEAD
68 _parent_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part __UNUSED__)
69 =======
70 _parent_label_get(void *data, Evas_Object *obj __UNUSED__, const char *part __UNUSED__)
71 >>>>>>> remotes/origin/upstream
72 {
73    char buf[256];
74    Node_Data *d = data;
75
76    snprintf(buf, sizeof(buf), "Group %d (%d items)", d->value / 7,
77             eina_list_count(d->children));
78
79    return strdup(buf);
80 }
81
82 static Evas_Object *
83 _parent_content_get(void *data __UNUSED__, Evas_Object *obj, const char *part __UNUSED__)
84 {
85    Evas_Object *ic = elm_icon_add(obj);
86
87    if (!strcmp(part, "elm.swallow.icon"))
88      elm_icon_standard_set(ic, "folder");
89
90    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
91    return ic;
92 }
93
94 static char *
95 <<<<<<< HEAD
96 _favorite_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part)
97 =======
98 _favorite_label_get(void *data, Evas_Object *obj __UNUSED__, const char *part)
99 >>>>>>> remotes/origin/upstream
100 {
101    char buf[256];
102    Node_Data *d = data;
103
104    if (!strcmp(part, "elm.text"))
105      snprintf(buf, sizeof(buf), "Favorite # %i", d->value);
106
107    return strdup(buf);
108 }
109
110 static Evas_Object *
111 _favorite_content_get(void *data __UNUSED__, Evas_Object *obj, const char *part)
112 {
113    Evas_Object *ic = elm_icon_add(obj);
114
115    if (!strcmp(part, "elm.swallow.icon"))
116      elm_icon_standard_set(ic, "apps");
117
118    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
119    return ic;
120 }
121
122 static void
123 _append_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
124 {
125    Evas_Object *list = data;
126 <<<<<<< HEAD
127    Elm_Object_Item *it, *parent = NULL;
128 =======
129    Elm_Object_Item *glit, *parent = NULL;
130 >>>>>>> remotes/origin/upstream
131    Node_Data *pdata, *d = malloc(sizeof(*d));
132
133    d->children = NULL;
134    d->value = nitems++;
135    d->favorite = EINA_FALSE;
136
137 <<<<<<< HEAD
138    it = elm_genlist_selected_item_get(list);
139    if (it)
140      parent = elm_genlist_item_parent_get(it);
141
142    if (parent)
143      {
144         d->level = elm_genlist_item_expanded_depth_get(parent) + 1;
145         pdata = elm_genlist_item_data_get(parent);
146         pdata->children = eina_list_append(pdata->children, d);
147 =======
148    glit = elm_genlist_selected_item_get(list);
149    if (glit)
150      parent = elm_genlist_item_parent_get(glit);
151
152    if (parent)
153      {
154         d->level = elm_genlist_item_expanded_depth_get(parent) + 1;
155         pdata = elm_object_item_data_get(parent);
156         pdata->children = eina_list_append(pdata->children, d);
157 >>>>>>> remotes/origin/upstream
158      }
159    else
160      d->level = 0;
161
162 <<<<<<< HEAD
163    elm_genlist_item_append(list, &_itc,
164                            d, parent,
165                            ELM_GENLIST_ITEM_NONE,
166                            _item_sel_cb, NULL);
167 =======
168    elm_genlist_item_append(list, _itc,
169                            d, parent,
170                            ELM_GENLIST_ITEM_NONE,
171                            _item_sel_cb, NULL);
172 >>>>>>> remotes/origin/upstream
173 }
174
175 static void
176 _favorite_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
177 {
178    Evas_Object *list = data;
179 <<<<<<< HEAD
180    Elm_Object_Item *it = elm_genlist_selected_item_get(list);
181
182    if (!it)
183      return;
184
185    Node_Data *d = elm_genlist_item_data_get(it);
186    d->favorite = !d->favorite;
187    if (d->favorite)
188      elm_genlist_item_item_class_update(it, &_itfav);
189    else
190      {
191         if (d->children)
192           elm_genlist_item_item_class_update(it, &_itp);
193         else
194           elm_genlist_item_item_class_update(it, &_itc);
195      }
196
197    elm_genlist_item_update(it);
198 =======
199    Elm_Object_Item *glit = elm_genlist_selected_item_get(list);
200
201    if (!glit) return;
202
203    Node_Data *d = elm_object_item_data_get(glit);
204    d->favorite = !d->favorite;
205    if (d->favorite)
206      elm_genlist_item_item_class_update(glit, _itfav);
207    else
208      {
209         if (d->children)
210           elm_genlist_item_item_class_update(glit, _itp);
211         else
212           elm_genlist_item_item_class_update(glit, _itc);
213      }
214
215    elm_genlist_item_update(glit);
216 >>>>>>> remotes/origin/upstream
217 }
218
219 static void
220 _add_child_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
221 {
222    Evas_Object *list = data;
223 <<<<<<< HEAD
224    Elm_Object_Item *it = elm_genlist_selected_item_get(list);
225    Elm_Object_Item *prev, *parent;
226
227    if (!it)
228      return;
229
230    Node_Data *d = elm_genlist_item_data_get(it);
231    prev = elm_genlist_item_prev_get(it);
232    parent = elm_genlist_item_parent_get(it);
233 =======
234    Elm_Object_Item *glit = elm_genlist_selected_item_get(list);
235    Elm_Object_Item *glit_prev, *glit_parent;
236
237    if (!glit) return;
238
239    Node_Data *d = elm_object_item_data_get(glit);
240    glit_prev = elm_genlist_item_prev_get(glit);
241    glit_parent = elm_genlist_item_parent_get(glit);
242 >>>>>>> remotes/origin/upstream
243
244    Eina_Bool change_item = !d->children;
245
246    // creating new item data
247    Node_Data *ndata = malloc(sizeof(*ndata));
248    ndata->value = nitems++;
249    ndata->children = NULL;
250    ndata->favorite = EINA_FALSE;
251 <<<<<<< HEAD
252    ndata->level = elm_genlist_item_expanded_depth_get(it) + 1;
253 =======
254    ndata->level = elm_genlist_item_expanded_depth_get(glit) + 1;
255 >>>>>>> remotes/origin/upstream
256    d->children = eina_list_append(d->children, ndata);
257
258    // Changing leaf item to parent item
259    if (change_item)
260      {
261 <<<<<<< HEAD
262         elm_genlist_item_del(it);
263
264         if (prev != parent)
265           it = elm_genlist_item_insert_after(list, &_itp, d, parent, prev,
266                                              ELM_GENLIST_ITEM_SUBITEMS,
267                                              _item_sel_cb, NULL);
268         else
269           it = elm_genlist_item_prepend(list, &_itp, d, parent,
270                                         ELM_GENLIST_ITEM_SUBITEMS,
271                                         _item_sel_cb, NULL);
272         elm_genlist_item_expanded_set(it, EINA_FALSE);
273         elm_genlist_item_selected_set(it, EINA_TRUE);
274      }
275    else if (elm_genlist_item_expanded_get(it))
276      {
277         elm_genlist_item_append(list, &_itc, ndata, it,
278                                 ELM_GENLIST_ITEM_NONE, _item_sel_cb, NULL);
279      }
280
281    elm_genlist_item_update(it);
282 =======
283         elm_object_item_del(glit);
284
285         if (glit_prev != glit_parent)
286           glit = elm_genlist_item_insert_after(list, _itp, d, glit_parent,
287                                                glit_prev,
288                                                ELM_GENLIST_ITEM_TREE,
289                                                _item_sel_cb, NULL);
290         else
291           glit = elm_genlist_item_prepend(list, _itp, d, glit_parent,
292                                           ELM_GENLIST_ITEM_TREE,
293                                           _item_sel_cb, NULL);
294         elm_genlist_item_expanded_set(glit, EINA_FALSE);
295         elm_genlist_item_selected_set(glit, EINA_TRUE);
296      }
297    else if (elm_genlist_item_expanded_get(glit))
298      {
299         elm_genlist_item_append(list, _itc, ndata, glit,
300                                 ELM_GENLIST_ITEM_NONE, _item_sel_cb, NULL);
301      }
302
303    elm_genlist_item_update(glit);
304
305 >>>>>>> remotes/origin/upstream
306 }
307
308 static void
309 _clear_list(Node_Data *d)
310 {
311    Node_Data *tmp;
312
313    EINA_LIST_FREE(d->children, tmp)
314       _clear_list(tmp);
315    free(d);
316 }
317
318 static void
319 _del_item_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
320 {
321    Evas_Object *list = data;
322 <<<<<<< HEAD
323    Elm_Object_Item *it = elm_genlist_selected_item_get(list);
324    Elm_Object_Item *parent = NULL;
325
326    if (!it)
327      return;
328
329    Node_Data *pdata, *d = elm_genlist_item_data_get(it);
330    parent = elm_genlist_item_parent_get(it);
331    elm_genlist_item_subitems_clear(it);
332    elm_genlist_item_del(it);
333
334    _clear_list(d);
335
336    if (!parent)
337      return;
338
339    pdata = elm_genlist_item_data_get(parent);
340    pdata->children = eina_list_remove(pdata->children, d);
341    elm_genlist_item_update(parent);
342 =======
343    Elm_Object_Item *glit = elm_genlist_selected_item_get(list);
344    Elm_Object_Item *glit_parent = NULL;
345
346    if (!glit) return;
347
348    Node_Data *pdata, *d = elm_object_item_data_get(glit);
349    glit_parent = elm_genlist_item_parent_get(glit);
350    elm_genlist_item_subitems_clear(glit);
351    elm_object_item_del(glit);
352
353    _clear_list(d);
354
355    if (!glit_parent) return;
356
357    pdata = elm_object_item_data_get(glit_parent);
358    pdata->children = eina_list_remove(pdata->children, d);
359    elm_genlist_item_update(glit_parent);
360 >>>>>>> remotes/origin/upstream
361 }
362
363 static void
364 _expand_request_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
365 {
366 <<<<<<< HEAD
367    Elm_Object_Item *it = event_info;
368    printf("expand request on item: %p\n", event_info);
369    elm_genlist_item_expanded_set(it, EINA_TRUE);
370 =======
371    Elm_Object_Item *glit = event_info;
372    printf("expand request on item: %p\n", event_info);
373    elm_genlist_item_expanded_set(glit, EINA_TRUE);
374 >>>>>>> remotes/origin/upstream
375 }
376
377 static void
378 _contract_request_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
379 {
380 <<<<<<< HEAD
381    Elm_Object_Item *it = event_info;
382    printf("contract request on item: %p\n", event_info);
383    elm_genlist_item_expanded_set(it, EINA_FALSE);
384 =======
385    Elm_Object_Item *glit = event_info;
386    printf("contract request on item: %p\n", event_info);
387    elm_genlist_item_expanded_set(glit, EINA_FALSE);
388 >>>>>>> remotes/origin/upstream
389 }
390
391 static void
392 _expanded_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
393 {
394    Eina_List *l;
395 <<<<<<< HEAD
396    Elm_Object_Item *it = event_info;
397    Node_Data *it_data, *d = elm_genlist_item_data_get(it);
398    Evas_Object *list = elm_genlist_item_genlist_get(it);
399 =======
400    Elm_Object_Item *glit = event_info;
401    Node_Data *it_data, *d = elm_object_item_data_get(glit);
402    Evas_Object *list = elm_object_item_widget_get(glit);
403 >>>>>>> remotes/origin/upstream
404
405    Elm_Genlist_Item_Class *ic;
406
407    EINA_LIST_FOREACH(d->children, l, it_data)
408      {
409 <<<<<<< HEAD
410         Elm_Object_Item *nitem;
411         Elm_Genlist_Item_Flags flags = ELM_GENLIST_ITEM_NONE;
412         printf("expanding item: #%d from parent #%d\n", it_data->value, d->value);
413         if (it_data->favorite)
414           ic = &_itfav;
415         else if (it_data->children)
416           {
417              ic = &_itp;
418              flags = ELM_GENLIST_ITEM_SUBITEMS;
419           }
420         else
421           ic = &_itc;
422
423         nitem = elm_genlist_item_append(list, ic, it_data, it,
424                                         flags, _item_sel_cb, NULL);
425         elm_genlist_item_expanded_set(nitem, EINA_FALSE);
426 =======
427         Elm_Object_Item *nitem;
428         Elm_Genlist_Item_Type type = ELM_GENLIST_ITEM_NONE;
429         printf("expanding item: #%d from parent #%d\n", it_data->value, d->value);
430         if (it_data->favorite)
431           ic = _itfav;
432         else if (it_data->children)
433           {
434              ic = _itp;
435              type = ELM_GENLIST_ITEM_TREE;
436           }
437         else
438           ic = _itc;
439
440         nitem = elm_genlist_item_append(list, ic, it_data, glit,
441                                         type, _item_sel_cb, NULL);
442         elm_genlist_item_expanded_set(nitem, EINA_FALSE);
443 >>>>>>> remotes/origin/upstream
444      }
445 }
446
447 static void
448 _contracted_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
449 {
450 <<<<<<< HEAD
451    Elm_Object_Item *it = event_info;
452    elm_genlist_item_subitems_clear(it);
453 =======
454    Elm_Object_Item *glit = event_info;
455    elm_genlist_item_subitems_clear(glit);
456 >>>>>>> remotes/origin/upstream
457 }
458
459 static Evas_Object *
460 _button_add(Evas_Object *list, Evas_Object *box, const char *label, Evas_Smart_Cb cb)
461 {
462    Evas_Object *bt;
463
464    bt = elm_button_add(elm_object_parent_widget_get(list));
465    elm_object_text_set(bt, label);
466    elm_box_pack_end(box, bt);
467    evas_object_show(bt);
468
469    if (cb)
470      evas_object_smart_callback_add(bt, "clicked", cb, list);
471
472    return bt;
473 }
474
475 int
476 elm_main(int argc __UNUSED__, char **argv __UNUSED__)
477 {
478    Evas_Object *win, *bg, *box, *fbox;
479    Evas_Object *list;
480    int i;
481
482    win = elm_win_add(NULL, "icon", ELM_WIN_BASIC);
483    elm_win_title_set(win, "Icon");
484    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
485    elm_win_autodel_set(win, EINA_TRUE);
486
487    bg = elm_bg_add(win);
488    elm_bg_color_set(bg, 255,255 ,255);
489    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
490    elm_win_resize_object_add(win, bg);
491    evas_object_show(bg);
492
493    box = elm_box_add(win);
494    evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
495    evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
496    elm_win_resize_object_add(win, box);
497    evas_object_show(box);
498
499 <<<<<<< HEAD
500    _itc.item_style = "default";
501    _itc.func.text_get = _item_text_get;
502    _itc.func.content_get = _item_content_get;
503    _itc.func.state_get = NULL;
504    _itc.func.del = NULL;
505
506    _itp.item_style = "default";
507    _itp.func.text_get = _parent_text_get;
508    _itp.func.content_get = _parent_content_get;
509    _itp.func.state_get = NULL;
510    _itp.func.del = NULL;
511
512    _itfav.item_style = "default";
513    _itfav.func.text_get = _favorite_text_get;
514    _itfav.func.content_get = _favorite_content_get;
515    _itfav.func.state_get = NULL;
516    _itfav.func.del = NULL;
517 =======
518    if (!_itc)
519      {
520         _itc = elm_genlist_item_class_new();
521         _itc->item_style = "default";
522         _itc->func.text_get = _item_label_get;
523         _itc->func.content_get = _item_content_get;
524         _itc->func.state_get = NULL;
525         _itc->func.del = NULL;
526      }
527    
528    if (!_itp)
529      {
530         _itp = elm_genlist_item_class_new();
531         _itp->item_style = "default";
532         _itp->func.text_get = _parent_label_get;
533         _itp->func.content_get = _parent_content_get;
534         _itp->func.state_get = NULL;
535         _itp->func.del = NULL;
536      }
537    
538    if (!_itfav)
539      {
540         _itfav = elm_genlist_item_class_new();
541         _itfav->item_style = "default";
542         _itfav->func.text_get = _favorite_label_get;
543         _itfav->func.content_get = _favorite_content_get;
544         _itfav->func.state_get = NULL;
545         _itfav->func.del = NULL;
546      }
547 >>>>>>> remotes/origin/upstream
548
549    list = elm_genlist_add(win);
550
551    evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
552    evas_object_size_hint_align_set(list, EVAS_HINT_FILL, EVAS_HINT_FILL);
553    elm_box_pack_end(box, list);
554    evas_object_show(list);
555
556    fbox = elm_box_add(win);
557    elm_box_layout_set(fbox, evas_object_box_layout_flow_horizontal,
558 <<<<<<< HEAD
559                       NULL, NULL);
560 =======
561                       NULL, NULL);
562 >>>>>>> remotes/origin/upstream
563    evas_object_size_hint_weight_set(fbox, EVAS_HINT_EXPAND, 0);
564    evas_object_size_hint_align_set(fbox, EVAS_HINT_FILL, EVAS_HINT_FILL);
565    elm_box_pack_end(box, fbox);
566    evas_object_show(fbox);
567
568    _button_add(list, fbox, "append item", _append_cb);
569    _button_add(list, fbox, "favorite", _favorite_cb);
570    _button_add(list, fbox, "add child", _add_child_cb);
571    _button_add(list, fbox, "del item", _del_item_cb);
572
573    for (i = 0; i < N_ITEMS; i++)
574      {
575 <<<<<<< HEAD
576         Elm_Object_Item *gli, *glg;
577         Node_Data *data = malloc(sizeof(*data)); // data for this item
578         data->children = NULL;
579         data->value = i;
580         data->favorite = EINA_FALSE;
581         nitems++;
582
583         Node_Data *pdata; // data for the parent of the group
584
585         printf("creating item: #%d\n", data->value);
586         if (i % 3 == 0)
587           {
588              glg = gli = elm_genlist_item_append(list, &_itp, data, NULL,
589                                                  ELM_GENLIST_ITEM_SUBITEMS,
590                                                  _item_sel_cb, NULL);
591              elm_genlist_item_expanded_set(glg, EINA_TRUE);
592              pdata = data;
593              data->level = 0;
594           }
595         else
596           {
597              gli = elm_genlist_item_append(list, &_itc, data, glg,
598                                            ELM_GENLIST_ITEM_NONE,
599                                            _item_sel_cb, NULL);
600              pdata->children = eina_list_append(pdata->children, data);
601              data->level = 1;
602           }
603 =======
604         Elm_Object_Item *gli, *glg;
605         Node_Data *data = malloc(sizeof(*data)); // data for this item
606         data->children = NULL;
607         data->value = i;
608         data->favorite = EINA_FALSE;
609         nitems++;
610
611         Node_Data *pdata; // data for the parent of the group
612
613         printf("creating item: #%d\n", data->value);
614         if (i % 3 == 0)
615           {
616              glg = gli = elm_genlist_item_append(list, _itp, data, NULL,
617                                                  ELM_GENLIST_ITEM_TREE,
618                                                  _item_sel_cb, NULL);
619              elm_genlist_item_expanded_set(glg, EINA_TRUE);
620              pdata = data;
621              data->level = 0;
622           }
623         else
624           {
625              gli = elm_genlist_item_append(list, _itc, data, glg,
626                                            ELM_GENLIST_ITEM_NONE,
627                                            _item_sel_cb, NULL);
628              pdata->children = eina_list_append(pdata->children, data);
629              data->level = 1;
630           }
631 >>>>>>> remotes/origin/upstream
632      }
633
634    evas_object_smart_callback_add(list, "expand,request", _expand_request_cb, list);
635    evas_object_smart_callback_add(list, "contract,request", _contract_request_cb, list);
636    evas_object_smart_callback_add(list, "expanded", _expanded_cb, list);
637    evas_object_smart_callback_add(list, "contracted", _contracted_cb, list);
638
639    evas_object_size_hint_min_set(bg, 160, 160);
640    evas_object_size_hint_max_set(bg, 640, 800);
641    evas_object_resize(win, 420, 600);
642    evas_object_show(win);
643
644    elm_run();
645
646    return 0;
647 }
648
649 ELM_MAIN()