diff options
Diffstat (limited to 'libraries/elementary/src/bin/test_index.c')
-rw-r--r-- | libraries/elementary/src/bin/test_index.c | 385 |
1 files changed, 385 insertions, 0 deletions
diff --git a/libraries/elementary/src/bin/test_index.c b/libraries/elementary/src/bin/test_index.c new file mode 100644 index 0000000..0e3928a --- /dev/null +++ b/libraries/elementary/src/bin/test_index.c | |||
@@ -0,0 +1,385 @@ | |||
1 | #include "test.h" | ||
2 | #ifdef HAVE_CONFIG_H | ||
3 | # include "elementary_config.h" | ||
4 | #endif | ||
5 | #include <Elementary.h> | ||
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_bring_in(elm_object_item_data_get(event_info), | ||
132 | ELM_GENLIST_ITEM_SCROLLTO_TOP); | ||
133 | } | ||
134 | |||
135 | void | ||
136 | _index_changed_cb(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__) | ||
137 | { | ||
138 | // this is calld on every change, no matter how often | ||
139 | // elm_genlist_item_bring_in(event_info); | ||
140 | } | ||
141 | |||
142 | void | ||
143 | _index_selected_cb(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info) | ||
144 | { | ||
145 | // called on final select | ||
146 | elm_genlist_item_bring_in(elm_object_item_data_get(event_info), | ||
147 | ELM_GENLIST_ITEM_SCROLLTO_TOP); | ||
148 | } | ||
149 | |||
150 | static void | ||
151 | _cleanup_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__) | ||
152 | { | ||
153 | free(data); | ||
154 | } | ||
155 | |||
156 | static void | ||
157 | id_cb(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info) | ||
158 | { | ||
159 | printf("Current Index : %s\n", elm_index_item_letter_get((const Elm_Object_Item *)event_info)); | ||
160 | } | ||
161 | |||
162 | void | ||
163 | test_index(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__) | ||
164 | { | ||
165 | Evas_Object *win, *bxx, *gl, *id, *bt; | ||
166 | Elm_Object_Item *glit; | ||
167 | int i, j; | ||
168 | api_data *api = calloc(1, sizeof(api_data)); | ||
169 | |||
170 | win = elm_win_util_standard_add("index", "Index"); | ||
171 | elm_win_autodel_set(win, EINA_TRUE); | ||
172 | evas_object_event_callback_add(win, EVAS_CALLBACK_FREE, _cleanup_cb, api); | ||
173 | |||
174 | bxx = elm_box_add(win); | ||
175 | elm_win_resize_object_add(win, bxx); | ||
176 | evas_object_size_hint_weight_set(bxx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); | ||
177 | evas_object_show(bxx); | ||
178 | |||
179 | gl = elm_genlist_add(win); | ||
180 | evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); | ||
181 | evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL); | ||
182 | evas_object_show(gl); | ||
183 | |||
184 | api->dt.id = id = elm_index_add(win); | ||
185 | evas_object_size_hint_weight_set(id, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); | ||
186 | evas_object_size_hint_align_set(id, EVAS_HINT_FILL, EVAS_HINT_FILL); | ||
187 | elm_win_resize_object_add(win, id); | ||
188 | |||
189 | bt = elm_button_add(win); | ||
190 | elm_object_text_set(bt, "Next API function"); | ||
191 | evas_object_smart_callback_add(bt, "clicked", _api_bt_clicked, (void *) api); | ||
192 | elm_box_pack_end(bxx, bt); | ||
193 | elm_object_disabled_set(bt, api->state == API_STATE_LAST); | ||
194 | evas_object_show(bt); | ||
195 | |||
196 | elm_box_pack_end(bxx, gl); | ||
197 | |||
198 | evas_object_show(id); | ||
199 | |||
200 | itci.item_style = "default"; | ||
201 | itci.func.text_get = gli_text_get; | ||
202 | itci.func.content_get = NULL; | ||
203 | itci.func.state_get = NULL; | ||
204 | itci.func.del = NULL; | ||
205 | |||
206 | j = 0; | ||
207 | for (i = 0; i < 100; i++) | ||
208 | { | ||
209 | glit = elm_genlist_item_append(gl, &itci, | ||
210 | (void *)(long)j/* item data */, | ||
211 | NULL/* parent */, | ||
212 | ELM_GENLIST_ITEM_NONE, | ||
213 | NULL/* func */, NULL/* func data */); | ||
214 | if (!(j & 0xf)) | ||
215 | { | ||
216 | char buf[32]; | ||
217 | |||
218 | snprintf(buf, sizeof(buf), "%c", 'A' + ((j >> 4) & 0xf)); | ||
219 | elm_index_item_append(id, buf, id_cb, glit); | ||
220 | if (*buf == 'G') /* Just init dt->item later used in API test */ | ||
221 | api->dt.item = glit; | ||
222 | } | ||
223 | j += 2; | ||
224 | } | ||
225 | evas_object_smart_callback_add(id, "delay,changed", _index_delay_changed_cb, NULL); | ||
226 | evas_object_smart_callback_add(id, "changed", _index_changed_cb, NULL); | ||
227 | evas_object_smart_callback_add(id, "selected", _index_selected_cb, NULL); | ||
228 | elm_index_level_go(id, 0); | ||
229 | evas_object_resize(win, 320, 480); | ||
230 | evas_object_show(win); | ||
231 | } | ||
232 | |||
233 | /***********/ | ||
234 | |||
235 | typedef struct _Test_Index2_Elements | ||
236 | { | ||
237 | Evas_Object *entry, *lst, *id; | ||
238 | } Test_Index2_Elements; | ||
239 | |||
240 | void | ||
241 | test_index2_del(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__) | ||
242 | { | ||
243 | /* FIXME it won't be called if elm_test main window is closed */ | ||
244 | free(data); | ||
245 | } | ||
246 | |||
247 | int | ||
248 | test_index2_cmp(const void *data1, const void *data2) | ||
249 | { | ||
250 | const char *label1, *label2; | ||
251 | const Elm_Object_Item *li_it1 = data1; | ||
252 | const Elm_Object_Item *li_it2 = data2; | ||
253 | |||
254 | label1 = elm_object_item_text_get(li_it1); | ||
255 | label2 = elm_object_item_text_get(li_it2); | ||
256 | |||
257 | return strcasecmp(label1, label2); | ||
258 | } | ||
259 | |||
260 | int | ||
261 | test_index2_icmp(const void *data1, const void *data2) | ||
262 | { | ||
263 | const char *label1, *label2; | ||
264 | const Elm_Object_Item *index_it1 = data1; | ||
265 | const Elm_Object_Item *index_it2 = data2; | ||
266 | |||
267 | label1 = elm_index_item_letter_get(index_it1); | ||
268 | label2 = elm_index_item_letter_get(index_it2); | ||
269 | |||
270 | return strcasecmp(label1, label2); | ||
271 | } | ||
272 | |||
273 | void | ||
274 | test_index2_it_add(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__) | ||
275 | { | ||
276 | Test_Index2_Elements *gui = data; | ||
277 | Elm_Object_Item *list_it; | ||
278 | const char *label; | ||
279 | char letter[2]; | ||
280 | |||
281 | label = elm_object_text_get(gui->entry); | ||
282 | snprintf(letter, sizeof(letter), "%c", label[0]); | ||
283 | list_it = elm_list_item_sorted_insert(gui->lst, label, NULL, NULL, NULL, | ||
284 | NULL, test_index2_cmp); | ||
285 | elm_index_item_sorted_insert(gui->id, letter, NULL, list_it, test_index2_icmp, | ||
286 | test_index2_cmp); | ||
287 | elm_list_go(gui->lst); | ||
288 | /* FIXME it's not showing the recently added item */ | ||
289 | elm_list_item_show(list_it); | ||
290 | } | ||
291 | |||
292 | void | ||
293 | test_index2_it_del(void *data, Evas_Object *obj, void *event_info __UNUSED__) | ||
294 | { | ||
295 | Test_Index2_Elements *gui = data; | ||
296 | const char *label, *label_next; | ||
297 | Elm_Object_Item *list_it, *list_it_next; | ||
298 | Elm_Object_Item *iit; | ||
299 | |||
300 | list_it = elm_list_selected_item_get(obj); | ||
301 | list_it_next = elm_list_item_next(list_it); | ||
302 | |||
303 | if (!list_it_next) | ||
304 | { | ||
305 | iit = elm_index_item_find(gui->id, list_it); | ||
306 | if (iit) elm_object_item_del(iit); | ||
307 | elm_object_item_del(list_it); | ||
308 | return; | ||
309 | } | ||
310 | |||
311 | label = elm_object_item_text_get(list_it); | ||
312 | label_next = elm_object_item_text_get(list_it_next); | ||
313 | |||
314 | iit = elm_index_item_find(gui->id, list_it); | ||
315 | if (label[0] == label_next[0]) | ||
316 | elm_object_item_data_set(iit, list_it_next); | ||
317 | else | ||
318 | elm_object_item_del(iit); | ||
319 | |||
320 | elm_object_item_del(list_it); | ||
321 | } | ||
322 | |||
323 | void | ||
324 | test_index2_id_changed(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info) | ||
325 | { | ||
326 | elm_list_item_show(elm_object_item_data_get(event_info)); | ||
327 | } | ||
328 | |||
329 | void | ||
330 | test_index2(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__) | ||
331 | { | ||
332 | Evas_Object *win, *box, *bt; | ||
333 | Test_Index2_Elements *gui; | ||
334 | |||
335 | gui = malloc(sizeof(*gui)); | ||
336 | |||
337 | win = elm_win_util_standard_add("sorted-index-list", "Sorted Index and List"); | ||
338 | evas_object_smart_callback_add(win, "delete,request", test_index2_del, gui); | ||
339 | elm_win_autodel_set(win, EINA_TRUE); | ||
340 | |||
341 | box = elm_box_add(win); | ||
342 | evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); | ||
343 | elm_win_resize_object_add(win, box); | ||
344 | evas_object_show(box); | ||
345 | |||
346 | gui->id = elm_index_add(win); | ||
347 | evas_object_size_hint_weight_set(gui->id, EVAS_HINT_EXPAND, | ||
348 | EVAS_HINT_EXPAND); | ||
349 | elm_win_resize_object_add(win, gui->id); | ||
350 | evas_object_smart_callback_add(gui->id, "delay,changed", | ||
351 | test_index2_id_changed, NULL); | ||
352 | evas_object_show(gui->id); | ||
353 | |||
354 | gui->entry = elm_entry_add(win); | ||
355 | elm_entry_scrollable_set(gui->entry, EINA_TRUE); | ||
356 | elm_object_text_set(gui->entry, "Label"); | ||
357 | elm_entry_single_line_set(gui->entry, EINA_TRUE); | ||
358 | evas_object_size_hint_weight_set(gui->entry, EVAS_HINT_EXPAND, 0); | ||
359 | evas_object_size_hint_fill_set(gui->entry, EVAS_HINT_FILL, EVAS_HINT_FILL); | ||
360 | elm_box_pack_end(box, gui->entry); | ||
361 | evas_object_show(gui->entry); | ||
362 | |||
363 | bt = elm_button_add(win); | ||
364 | elm_object_text_set(bt, "Add"); | ||
365 | evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, 0); | ||
366 | evas_object_size_hint_fill_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL); | ||
367 | elm_box_pack_end(box, bt); | ||
368 | evas_object_smart_callback_add(bt, "clicked", test_index2_it_add, gui); | ||
369 | evas_object_show(bt); | ||
370 | |||
371 | gui->lst = elm_list_add(win); | ||
372 | elm_box_pack_end(box, gui->lst); | ||
373 | evas_object_size_hint_weight_set(gui->lst, EVAS_HINT_EXPAND, | ||
374 | EVAS_HINT_EXPAND); | ||
375 | evas_object_size_hint_fill_set(gui->lst, EVAS_HINT_FILL, EVAS_HINT_FILL); | ||
376 | evas_object_smart_callback_add(gui->lst, "selected", test_index2_it_del, | ||
377 | gui); | ||
378 | elm_list_go(gui->lst); | ||
379 | evas_object_show(gui->lst); | ||
380 | |||
381 | evas_object_resize(win, 320, 480); | ||
382 | evas_object_show(win); | ||
383 | } | ||
384 | |||
385 | #endif | ||