Merge "gengrid TC modified"
[framework/uifw/elementary.git] / tests / src / examples / genlist_example_04.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 300
12
13 static Elm_Genlist_Item_Class _itc;
14 static Elm_Genlist_Item_Class _itc_group;
15 static int nitems = 0;
16
17 static char *
18 _item_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part)
19 {
20    time_t t = (time_t)ecore_time_unix_get();
21    char buf[256];
22    int i = (int)(long)data;
23
24    if (!strcmp(part, "elm.text"))
25      snprintf(buf, sizeof(buf), "Item # %i", i);
26    else
27      {
28         int n;
29         snprintf(buf, sizeof(buf), "realized at %s", ctime(&t));
30         n = strlen(buf);
31         buf[n - 1] = '\0';
32      }
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, "clock");
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 _group_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part __UNUSED__)
58 {
59    char buf[256];
60    int i = (int)(long)data;
61
62    snprintf(buf, sizeof(buf), "Group %d (item #%d)", i / 7, i);
63
64    return strdup(buf);
65 }
66
67 static Evas_Object *
68 _group_content_get(void *data __UNUSED__, Evas_Object *obj, const char *part __UNUSED__)
69 {
70    Evas_Object *ic = elm_icon_add(obj);
71
72    if (!strcmp(part, "elm.swallow.icon"))
73      elm_icon_standard_set(ic, "home");
74
75    evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
76    return ic;
77 }
78
79 static void
80 _append_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
81 {
82    Evas_Object *list = data;
83
84    elm_genlist_item_append(list, &_itc,
85                            (void *)(long)nitems++, NULL,
86                            ELM_GENLIST_ITEM_NONE,
87                            _item_sel_cb, NULL);
88
89 }
90
91 static void
92 _prepend_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
93 {
94    Evas_Object *list = data;
95
96    elm_genlist_item_prepend(list, &_itc,
97                            (void *)(long)nitems++, NULL,
98                            ELM_GENLIST_ITEM_NONE,
99                            _item_sel_cb, NULL);
100
101 }
102
103 static void
104 _insert_before_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
105 {
106    Evas_Object *list = data;
107    Elm_Object_Item *it = elm_genlist_selected_item_get(list);
108
109    if (!it)
110      return;
111
112    elm_genlist_item_insert_before(list, &_itc,
113                                   (void *)(long)nitems++, NULL,
114                                   it, ELM_GENLIST_ITEM_NONE,
115                                   _item_sel_cb, NULL);
116 }
117
118 static void
119 _insert_after_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
120 {
121    Evas_Object *list = data;
122    Elm_Object_Item *it = elm_genlist_selected_item_get(list);
123
124    if (!it)
125      return;
126
127    elm_genlist_item_insert_after(list, &_itc,
128                                  (void *)(long)nitems++, NULL,
129                                  it, ELM_GENLIST_ITEM_NONE,
130                                  _item_sel_cb, NULL);
131 }
132
133 static void
134 _next_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
135 {
136    Evas_Object *list = data;
137    Elm_Object_Item *it;
138
139    it = elm_genlist_selected_item_get(list);
140    if (it)
141      it = elm_genlist_item_next_get(it);
142
143    if (!it)
144      it = elm_genlist_first_item_get(list);
145
146    elm_genlist_item_selected_set(it, EINA_TRUE);
147    elm_genlist_item_show(it);
148 }
149
150 static void
151 _prev_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
152 {
153    Evas_Object *list = data;
154    Elm_Object_Item *it;
155
156    it = elm_genlist_selected_item_get(list);
157    if (it)
158      it = elm_genlist_item_prev_get(it);
159
160    if (!it)
161      it = elm_genlist_last_item_get(list);
162
163    elm_genlist_item_selected_set(it, EINA_TRUE);
164    elm_genlist_item_show(it);
165 }
166
167 static void
168 _bring_in_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
169 {
170    Elm_Object_Item *it = data;
171
172    if (!it)
173      return;
174
175    elm_genlist_item_bring_in(it);
176 }
177
178 static void
179 _show_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
180 {
181    Elm_Object_Item *it = data;
182
183    if (!it)
184      return;
185
186    elm_genlist_item_show(it);
187 }
188
189 static void
190 _middle_in_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
191 {
192    Elm_Object_Item *it = data;
193
194    if (!it)
195      return;
196
197    elm_genlist_item_middle_bring_in(it);
198 }
199
200 static void
201 _middle_show_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
202 {
203    Elm_Object_Item *it = data;
204
205    if (!it)
206      return;
207
208    elm_genlist_item_middle_show(it);
209 }
210
211 static void
212 _top_in_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
213 {
214    Elm_Object_Item *it = data;
215
216    if (!it)
217      return;
218
219    elm_genlist_item_top_bring_in(it);
220 }
221
222 static void
223 _top_show_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
224 {
225    Elm_Object_Item *it = data;
226
227    if (!it)
228      return;
229
230    elm_genlist_item_top_show(it);
231 }
232
233 static void
234 _realize_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
235 {
236    Evas_Object *list = data;
237    Elm_Object_Item *it = elm_genlist_selected_item_get(list);
238
239    if (!it)
240      return;
241
242    elm_genlist_item_update(it);
243 }
244
245 static Evas_Object *
246 _button_add(Evas_Object *list, Evas_Object *box, const char *label, Evas_Smart_Cb cb)
247 {
248    Evas_Object *bt;
249
250    bt = elm_button_add(elm_object_parent_widget_get(list));
251    elm_object_text_set(bt, label);
252    elm_box_pack_end(box, bt);
253    evas_object_show(bt);
254
255    if (cb)
256      evas_object_smart_callback_add(bt, "clicked", cb, list);
257
258    return bt;
259 }
260
261 int
262 elm_main(int argc __UNUSED__, char **argv __UNUSED__)
263 {
264    Evas_Object *win, *bg, *box, *fbox;
265    Evas_Object *list;
266    Evas_Object *bt_bring_in, *bt_show;
267    Evas_Object *bt_middle_in, *bt_middle_show;
268    Evas_Object *bt_top_in, *bt_top_show;
269    int i;
270
271    win = elm_win_add(NULL, "icon", ELM_WIN_BASIC);
272    elm_win_title_set(win, "Icon");
273    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
274    elm_win_autodel_set(win, EINA_TRUE);
275
276    bg = elm_bg_add(win);
277    elm_bg_color_set(bg, 255,255 ,255);
278    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
279    elm_win_resize_object_add(win, bg);
280    evas_object_show(bg);
281
282    box = elm_box_add(win);
283    evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
284    evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
285    elm_win_resize_object_add(win, box);
286    evas_object_show(box);
287
288    _itc.item_style = "double_label";
289    _itc.func.text_get = _item_text_get;
290    _itc.func.content_get = _item_content_get;
291    _itc.func.state_get = NULL;
292    _itc.func.del = NULL;
293
294    _itc_group.item_style = "group_index";
295    _itc_group.func.text_get = _group_text_get;
296    _itc_group.func.content_get = _group_content_get;
297    _itc_group.func.state_get = NULL;
298    _itc_group.func.del = NULL;
299
300    list = elm_genlist_add(win);
301
302    evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
303    evas_object_size_hint_align_set(list, EVAS_HINT_FILL, EVAS_HINT_FILL);
304    elm_box_pack_end(box, list);
305    evas_object_show(list);
306
307    fbox = elm_box_add(win);
308    elm_box_layout_set(fbox, evas_object_box_layout_flow_horizontal,
309                       NULL, NULL);
310    evas_object_size_hint_weight_set(fbox, EVAS_HINT_EXPAND, 0);
311    evas_object_size_hint_align_set(fbox, EVAS_HINT_FILL, EVAS_HINT_FILL);
312    elm_box_pack_end(box, fbox);
313    evas_object_show(fbox);
314
315    _button_add(list, fbox, "prepend", _prepend_cb);
316    _button_add(list, fbox, "append", _append_cb);
317    _button_add(list, fbox, "insert before", _insert_before_cb);
318    _button_add(list, fbox, "insert after", _insert_after_cb);
319    _button_add(list, fbox, "prev", _prev_cb);
320    _button_add(list, fbox, "next", _next_cb);
321    _button_add(list, fbox, "realize", _realize_cb);
322    bt_bring_in = _button_add(list, fbox, "bring #50", NULL);
323    bt_show = _button_add(list, fbox, "show #50", NULL);
324    bt_middle_in = _button_add(list, fbox, "bring to middle #200", NULL);
325    bt_middle_show = _button_add(list, fbox, "show in middle #200", NULL);
326    bt_top_in = _button_add(list, fbox, "bring to top #250", NULL);
327    bt_top_show = _button_add(list, fbox, "show in top #250", NULL);
328
329    for (i = 0; i < N_ITEMS; i++)
330      {
331         Elm_Object_Item *gli, *glg;
332
333         if (i % 7 == 0)
334           {
335              glg = gli = elm_genlist_item_append(list, &_itc_group,
336                                                  (void *)(long)nitems++, NULL,
337                                                  ELM_GENLIST_ITEM_GROUP,
338                                                  _item_sel_cb, NULL);
339              elm_genlist_item_display_only_set(glg, EINA_TRUE);
340           }
341         else
342           {
343              gli = elm_genlist_item_append(list, &_itc,
344                                            (void *)(long)nitems++, glg,
345                                            ELM_GENLIST_ITEM_NONE,
346                                            _item_sel_cb, NULL);
347           }
348
349         switch (i)
350           {
351            case 3:
352               elm_genlist_item_disabled_set(gli, EINA_TRUE);
353               break;
354            case 50:
355               evas_object_smart_callback_add(
356                  bt_bring_in, "clicked", _bring_in_cb, gli);
357               evas_object_smart_callback_add(
358                  bt_show, "clicked", _show_cb, gli);
359               break;
360            case 200:
361               evas_object_smart_callback_add(
362                  bt_middle_in, "clicked", _middle_in_cb, gli);
363               evas_object_smart_callback_add(
364                  bt_middle_show, "clicked", _middle_show_cb, gli);
365               break;
366            case 250:
367               evas_object_smart_callback_add(
368                  bt_top_in, "clicked", _top_in_cb, gli);
369               evas_object_smart_callback_add(
370                  bt_top_show, "clicked", _top_show_cb, gli);
371           }
372      }
373
374    evas_object_size_hint_min_set(bg, 160, 160);
375    evas_object_size_hint_max_set(bg, 640, 640);
376    evas_object_resize(win, 420, 320);
377    evas_object_show(win);
378
379    elm_run();
380
381    return 0;
382 }
383
384 ELM_MAIN()