elementray/index - some test code(should not be commited yet) reverted
[framework/uifw/elementary.git] / src / bin / test_index.c
1 #include <Elementary.h>
2 #include "test.h"
3 #ifdef HAVE_CONFIG_H
4 # include "elementary_config.h"
5 #endif
6 #ifndef ELM_LIB_QUICKLAUNCH
7 struct _Idx_Data_Type
8 {
9    Evas_Object *id;  /* Pointer to Index */
10    Elm_Object_Item *item; /* Item we use for search */
11 };
12 typedef struct _Idx_Data_Type Idx_Data_Type;
13
14 struct _api_data
15 {
16    unsigned int state;  /* What state we are testing       */
17    Idx_Data_Type dt;
18 };
19 typedef struct _api_data api_data;
20
21 enum _api_state
22 {
23    INDEX_LEVEL_SET,
24    INDEX_ACTIVE_SET,
25    INDEX_APPEND_RELATIVE,
26    INDEX_PREPEND,
27    INDEX_ITEM_DEL,
28    INDEX_ITEM_FIND,
29    INDEX_HORIZONTAL,
30    INDEX_INDICATOR_DISABLED,
31    INDEX_CLEAR,
32    API_STATE_LAST
33 };
34 typedef enum _api_state api_state;
35
36 static void
37 set_api_state(api_data *api)
38 {
39    Idx_Data_Type *d = &api->dt;
40    switch(api->state)
41      { /* Put all api-changes under switch */
42       case INDEX_LEVEL_SET:
43          elm_index_autohide_disabled_set(d->id, EINA_TRUE);
44          elm_index_item_level_set(d->id, (elm_index_item_level_get(d->id) ? 0 : 1));
45          break;
46
47       case INDEX_ACTIVE_SET:
48          elm_index_autohide_disabled_set(d->id, EINA_FALSE);
49          break;
50
51       case INDEX_APPEND_RELATIVE:
52          elm_index_item_insert_after(d->id,
53                                      elm_index_item_find(d->id, d->item),
54                                      "W", NULL, d->item);
55          elm_index_item_insert_before(d->id,
56                                       elm_index_item_find(d->id, d->item),
57                                       "V", NULL, d->item);
58          break;
59
60       case INDEX_PREPEND:
61          elm_index_item_prepend(d->id, "D", NULL, d->item);
62          break;
63
64       case INDEX_ITEM_DEL:
65          elm_object_item_del(elm_index_item_find(d->id, d->item));
66          break;
67
68       case INDEX_ITEM_FIND:
69            {
70               Elm_Object_Item *i = elm_index_item_find(d->id, d->item);
71               if(i)
72                 {
73                    printf("Item Find - Found Item.\n");
74                    elm_object_item_del(i);
75                 }
76            }
77          break;
78
79       case INDEX_HORIZONTAL:
80          elm_index_horizontal_set(d->id, EINA_TRUE);
81          break;
82
83       case INDEX_INDICATOR_DISABLED:
84          elm_index_indicator_disabled_set(d->id, EINA_TRUE);
85          break;
86
87       case INDEX_CLEAR:
88          elm_index_item_clear(d->id);
89          break;
90
91       case API_STATE_LAST:
92          break;
93
94       default:
95          return;
96      }
97 }
98
99 static void
100 _api_bt_clicked(void *data, Evas_Object *obj, void *event_info __UNUSED__)
101 {  /* Will add here a SWITCH command containing code to modify test-object */
102    /* in accordance a->state value. */
103    api_data *a = data;
104    char str[128];
105
106    printf("clicked event on API Button: api_state=<%d>\n", a->state);
107    set_api_state(a);
108    a->state++;
109    sprintf(str, "Next API function (%u)", a->state);
110    elm_object_text_set(obj, str);
111    elm_object_disabled_set(obj, a->state == API_STATE_LAST);
112 }
113
114 static Elm_Genlist_Item_Class itci;
115 char *gli_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part __UNUSED__)
116 {
117    char buf[256];
118    int j = (long)data;
119    snprintf(buf, sizeof(buf), "%c%c",
120             'A' + ((j >> 4) & 0xf),
121             'a' + ((j     ) & 0xf)
122             );
123    return strdup(buf);
124 }
125
126 void
127 _index_delay_changed_cb(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
128 {
129    // called on a change but delayed in case multiple changes happen in a
130    // short timespan
131    elm_genlist_item_top_bring_in(elm_object_item_data_get(event_info));
132 }
133
134 void
135 _index_changed_cb(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
136 {
137    // this is calld on every change, no matter how often
138    // elm_genlist_item_bring_in(event_info);
139 }
140
141 void
142 _index_selected_cb(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
143 {
144    // called on final select
145    elm_genlist_item_top_bring_in(elm_object_item_data_get(event_info));
146 }
147
148 static void
149 _cleanup_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
150 {
151    free(data);
152 }
153
154 static void
155 id_cb(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
156 {
157    printf("Current Index : %s\n", elm_index_item_letter_get((const Elm_Object_Item *)event_info));
158 }
159
160 void
161 test_index(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
162 {
163    Evas_Object *win, *bg, *bxx, *gl, *id, *bt;
164    Elm_Object_Item *glit;
165    int i, j;
166    api_data *api = calloc(1, sizeof(api_data));
167
168    win = elm_win_add(NULL, "index", ELM_WIN_BASIC);
169    elm_win_title_set(win, "Index");
170    elm_win_autodel_set(win, EINA_TRUE);
171    evas_object_event_callback_add(win, EVAS_CALLBACK_FREE, _cleanup_cb, api);
172
173    bg = elm_bg_add(win);
174    elm_win_resize_object_add(win, bg);
175    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
176    evas_object_show(bg);
177
178    bxx = elm_box_add(win);
179    elm_win_resize_object_add(win, bxx);
180    evas_object_size_hint_weight_set(bxx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
181    evas_object_show(bxx);
182
183    gl = elm_genlist_add(win);
184    evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
185    evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
186    evas_object_show(gl);
187
188    api->dt.id = id = elm_index_add(win);
189    evas_object_size_hint_weight_set(id, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
190    evas_object_size_hint_align_set(id, EVAS_HINT_FILL, EVAS_HINT_FILL);
191    elm_win_resize_object_add(win, id);
192
193    bt = elm_button_add(win);
194    elm_object_text_set(bt, "Next API function");
195    evas_object_smart_callback_add(bt, "clicked", _api_bt_clicked, (void *) api);
196    elm_box_pack_end(bxx, bt);
197    elm_object_disabled_set(bt, api->state == API_STATE_LAST);
198    evas_object_show(bt);
199
200    elm_box_pack_end(bxx, gl);
201
202    evas_object_show(id);
203
204    itci.item_style     = "default";
205    itci.func.text_get = gli_text_get;
206    itci.func.content_get  = NULL;
207    itci.func.state_get = NULL;
208    itci.func.del       = NULL;
209
210    j = 0;
211    for (i = 0; i < 100; i++)
212      {
213         glit = elm_genlist_item_append(gl, &itci,
214                                        (void *)(long)j/* item data */,
215                                        NULL/* parent */,
216                                        ELM_GENLIST_ITEM_NONE,
217                                        NULL/* func */, NULL/* func data */);
218         if (!(j & 0xf))
219           {
220              char buf[32];
221
222              snprintf(buf, sizeof(buf), "%c", 'A' + ((j >> 4) & 0xf));
223              elm_index_item_append(id, buf, id_cb, glit);
224              if (*buf == 'G')  /* Just init dt->item later used in API test */
225                api->dt.item = glit;
226           }
227         j += 2;
228      }
229    evas_object_smart_callback_add(id, "delay,changed", _index_delay_changed_cb, NULL);
230    evas_object_smart_callback_add(id, "changed", _index_changed_cb, NULL);
231    evas_object_smart_callback_add(id, "selected", _index_selected_cb, NULL);
232    elm_index_level_go(id, 0);
233    evas_object_resize(win, 320, 480);
234    evas_object_show(win);
235 }
236
237 /***********/
238
239 typedef struct _Test_Index2_Elements
240 {
241    Evas_Object *entry, *lst, *id;
242 } Test_Index2_Elements;
243
244 void
245 test_index2_del(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
246 {
247    /* FIXME it won't be called if elm_test main window is closed */
248    free(data);
249 }
250
251 int
252 test_index2_cmp(const void *data1, const void *data2)
253 {
254    const char *label1, *label2;
255    const Elm_Object_Item *li_it1 = data1;
256    const Elm_Object_Item *li_it2 = data2;
257
258    label1 = elm_object_item_text_get(li_it1);
259    label2 = elm_object_item_text_get(li_it2);
260
261    return strcasecmp(label1, label2);
262 }
263
264 int
265 test_index2_icmp(const void *data1, const void *data2)
266 {
267    const char *label1, *label2;
268    const Elm_Object_Item *index_it1 = data1;
269    const Elm_Object_Item *index_it2 = data2;
270
271    label1 = elm_index_item_letter_get(index_it1);
272    label2 = elm_index_item_letter_get(index_it2);
273
274    return strcasecmp(label1, label2);
275 }
276
277 void
278 test_index2_it_add(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
279 {
280    Test_Index2_Elements *gui = data;
281    Elm_Object_Item *list_it;
282    const char *label;
283    char letter[2];
284
285    label = elm_object_text_get(gui->entry);
286    snprintf(letter, sizeof(letter), "%c", label[0]);
287    list_it = elm_list_item_sorted_insert(gui->lst, label, NULL, NULL, NULL,
288                                          NULL, test_index2_cmp);
289    elm_index_item_sorted_insert(gui->id, letter, NULL, list_it, test_index2_icmp,
290                                 test_index2_cmp);
291    elm_list_go(gui->lst);
292    /* FIXME it's not showing the recently added item */
293    elm_list_item_show(list_it);
294 }
295
296 void
297 test_index2_it_del(void *data, Evas_Object *obj, void *event_info __UNUSED__)
298 {
299    Test_Index2_Elements *gui = data;
300    const char *label, *label_next;
301    Elm_Object_Item *list_it, *list_it_next;
302    Elm_Object_Item *iit;
303
304    list_it = elm_list_selected_item_get(obj);
305    list_it_next = elm_list_item_next(list_it);
306
307    if (!list_it_next)
308      {
309         iit = elm_index_item_find(gui->id, list_it);
310         if (iit) elm_object_item_del(iit);
311         elm_object_item_del(list_it);
312         return;
313      }
314
315    label = elm_object_item_text_get(list_it);
316    label_next = elm_object_item_text_get(list_it_next);
317
318    iit = elm_index_item_find(gui->id, list_it);
319    if (label[0] == label_next[0])
320      elm_object_item_data_set(iit, list_it_next);
321    else
322      elm_object_item_del(iit);
323
324    elm_object_item_del(list_it);
325 }
326
327 void
328 test_index2_id_changed(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
329 {
330    elm_list_item_show(elm_object_item_data_get(event_info));
331 }
332
333 void
334 test_index2(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
335 {
336    Evas_Object *win, *bg, *box, *bt;
337    Test_Index2_Elements *gui;
338
339    gui = malloc(sizeof(*gui));
340
341    win = elm_win_add(NULL, "sorted-index-list", ELM_WIN_BASIC);
342    elm_win_title_set(win, "Sorted Index and List");
343    evas_object_smart_callback_add(win, "delete,request", test_index2_del, gui);
344    elm_win_autodel_set(win, EINA_TRUE);
345
346    bg = elm_bg_add(win);
347    elm_win_resize_object_add(win, bg);
348    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
349    evas_object_show(bg);
350
351    box = elm_box_add(win);
352    evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
353    elm_win_resize_object_add(win, box);
354    evas_object_show(box);
355
356    gui->id = elm_index_add(win);
357    evas_object_size_hint_weight_set(gui->id, EVAS_HINT_EXPAND,
358                                     EVAS_HINT_EXPAND);
359    elm_win_resize_object_add(win, gui->id);
360    evas_object_smart_callback_add(gui->id, "delay,changed",
361                                   test_index2_id_changed, NULL);
362    evas_object_show(gui->id);
363
364    gui->entry = elm_entry_add(win);
365    elm_entry_scrollable_set(gui->entry, EINA_TRUE);
366    elm_object_text_set(gui->entry, "Label");
367    elm_entry_single_line_set(gui->entry, EINA_TRUE);
368    evas_object_size_hint_weight_set(gui->entry, EVAS_HINT_EXPAND, 0);
369    evas_object_size_hint_fill_set(gui->entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
370    elm_box_pack_end(box, gui->entry);
371    evas_object_show(gui->entry);
372
373    bt = elm_button_add(win);
374    elm_object_text_set(bt, "Add");
375    evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, 0);
376    evas_object_size_hint_fill_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
377    elm_box_pack_end(box, bt);
378    evas_object_smart_callback_add(bt, "clicked", test_index2_it_add, gui);
379    evas_object_show(bt);
380
381    gui->lst = elm_list_add(win);
382    elm_box_pack_end(box, gui->lst);
383    evas_object_size_hint_weight_set(gui->lst, EVAS_HINT_EXPAND,
384                                     EVAS_HINT_EXPAND);
385    evas_object_size_hint_fill_set(gui->lst, EVAS_HINT_FILL, EVAS_HINT_FILL);
386    evas_object_smart_callback_add(gui->lst, "selected", test_index2_it_del,
387                                   gui);
388    elm_list_go(gui->lst);
389    evas_object_show(gui->lst);
390
391    evas_object_resize(win, 320, 480);
392    evas_object_show(win);
393 }
394
395 #endif