From: Jérémy Zurcher <jeremy@asynk.ch>
[framework/uifw/elementary.git] / src / bin / test_tooltip.c
1 #include <Elementary.h>
2 #ifdef HAVE_CONFIG_H
3 # include "elementary_config.h"
4 #endif
5 #ifndef ELM_LIB_QUICKLAUNCH
6
7 typedef struct _Testitem
8 {
9    Elm_Gengrid_Item *item;
10    const char       *path;
11    int               mode;
12    int               onoff;
13 } Testitem;
14
15 static Elm_Gengrid_Item_Class gic;
16
17 char *
18 grdt_lbl_get(void            *data,
19              Evas_Object *obj __UNUSED__,
20              const char *part __UNUSED__)
21 {
22    const Testitem *ti = data;
23    char buf[256];
24    snprintf(buf, sizeof(buf), "Photo %s", ti->path);
25    return strdup(buf);
26 }
27
28 Evas_Object *
29 grdt_content_get(void        *data,
30                  Evas_Object *obj,
31                  const char  *part)
32 {
33    const Testitem *ti = data;
34    if (!strcmp(part, "elm.swallow.icon"))
35      {
36         Evas_Object *icon = elm_bg_add(obj);
37         elm_bg_file_set(icon, ti->path, NULL);
38         evas_object_size_hint_aspect_set(icon,
39                                          EVAS_ASPECT_CONTROL_VERTICAL,
40                                          1, 1);
41         evas_object_show(icon);
42         return icon;
43      }
44    return NULL;
45 }
46
47 static Elm_Genlist_Item_Class itct;
48
49 static void
50 gltt_exp(void *data       __UNUSED__,
51          Evas_Object *obj __UNUSED__,
52          void            *event_info)
53 {
54    Elm_Genlist_Item *it = event_info;
55    Evas_Object *gl = elm_genlist_item_genlist_get(it);
56    int val = (int)(long)elm_genlist_item_data_get(it);
57    Elm_Genlist_Item *it1, *it2, *it3;
58
59    val *= 10;
60    it1 = elm_genlist_item_append(gl, &itct, (void *)(long)(val + 1), it,
61                                  ELM_GENLIST_ITEM_NONE, NULL, NULL);
62    it2 = elm_genlist_item_append(gl, &itct, (void *)(long)(val + 2), it,
63                                  ELM_GENLIST_ITEM_NONE, NULL, NULL);
64    it3 = elm_genlist_item_append(gl, &itct, (void *)(long)(val + 3), it,
65                                  ELM_GENLIST_ITEM_SUBITEMS, NULL, NULL);
66
67    elm_genlist_item_tooltip_text_set(it1, "Testing A");
68    elm_genlist_item_tooltip_text_set(it2, "Testing B");
69    elm_genlist_item_tooltip_text_set(it3, "Testing C");
70 }
71
72 static void
73 gltt_con(void *data       __UNUSED__,
74          Evas_Object *obj __UNUSED__,
75          void            *event_info)
76 {
77    Elm_Genlist_Item *it = event_info;
78    elm_genlist_item_subitems_clear(it);
79 }
80
81 static void
82 gltt_exp_req(void *data       __UNUSED__,
83              Evas_Object *obj __UNUSED__,
84              void            *event_info)
85 {
86    Elm_Genlist_Item *it = event_info;
87    elm_genlist_item_expanded_set(it, 1);
88 }
89
90 static void
91 gltt_con_req(void *data       __UNUSED__,
92              Evas_Object *obj __UNUSED__,
93              void            *event_info)
94 {
95    Elm_Genlist_Item *it = event_info;
96    elm_genlist_item_expanded_set(it, 0);
97 }
98
99 char *
100 gltt_label_get(void            *data,
101                Evas_Object *obj __UNUSED__,
102                const char *part __UNUSED__)
103 {
104    char buf[256];
105    snprintf(buf, sizeof(buf), "Item mode %i", (int)(long)data);
106    return strdup(buf);
107 }
108
109 static Evas_Object *
110 _tt_item_icon(void *data   __UNUSED__,
111               Evas_Object *obj,
112               void *item   __UNUSED__)
113 {
114    Evas_Object *ic = elm_icon_add(obj);
115    char buf[PATH_MAX];
116    snprintf(buf, sizeof(buf), "%s/images/logo_small.png",
117             elm_app_data_dir_get());
118    elm_icon_file_set(ic, buf, NULL);
119    elm_icon_scale_set(ic, 0, 0);
120    evas_object_resize(ic, 64, 64);
121    return ic;
122 }
123
124 static Evas_Object *
125 _tt_item_icon2(void *data   __UNUSED__,
126               Evas_Object *obj __UNUSED__,
127               Evas_Object *tt,
128               void *item   __UNUSED__)
129 {
130    Evas_Object *ic = elm_icon_add(tt);
131    char buf[PATH_MAX];
132    snprintf(buf, sizeof(buf), "%s/images/logo.png", elm_app_data_dir_get());
133    elm_icon_file_set(ic, buf, NULL);
134    elm_icon_scale_set(ic, 0, 0);
135    return ic;
136 }
137
138 static Evas_Object *
139 _tt_item_icon3(void *data   __UNUSED__,
140               Evas_Object *obj __UNUSED__,
141               Evas_Object *tt,
142               void *item   __UNUSED__)
143 {
144    Evas_Object *ic = elm_icon_add(tt);
145    char buf[PATH_MAX];
146    snprintf(buf, sizeof(buf), "%s/images/insanely_huge_test_image.jpg", elm_app_data_dir_get());
147    elm_icon_file_set(ic, buf, NULL);
148    elm_icon_scale_set(ic, 0, 0);
149    return ic;
150 }
151
152 static Evas_Object *
153 _tt_item_label(void *data   __UNUSED__,
154               Evas_Object *obj __UNUSED__,
155               Evas_Object *tt,
156               void *item   __UNUSED__)
157 {
158    Evas_Object *l = elm_label_add(tt);
159    elm_object_text_set(l, "Something useful here?<ps>"
160                           "No probably not, but this is a super long label<ps>"
161                           "which probably breaks on your system, now doesn't it?<ps>"
162                           "Yeah, I thought so.");
163    elm_label_line_wrap_set(l, ELM_WRAP_MIXED);
164    return l;
165 }
166
167 static void
168 _tt_item_icon_del(void            *data,
169                   Evas_Object *obj __UNUSED__,
170                   void            *event_info)
171 {
172    // test to check for del_cb behavior!
173    printf("_tt_icon_del: data=%ld (== 456?), event_info=%p\n",
174           (long)data, event_info);
175 }
176
177 static void
178 _tt_text_replace(void *data       __UNUSED__,
179                  Evas_Object     *obj,
180                  void *event_info __UNUSED__)
181 {
182    static int count = 0;
183    char buf[64];
184    snprintf(buf, sizeof(buf), "count=%d", count);
185    count++;
186    elm_object_tooltip_text_set(obj, buf);
187 }
188
189 static void
190 _tt_timer_del(void *data       __UNUSED__,
191               Evas *e          __UNUSED__,
192               Evas_Object     *obj,
193               void *event_info __UNUSED__)
194 {
195    Ecore_Timer *timer = evas_object_data_del(obj, "test-timer");
196    if (!timer) return;
197    ecore_timer_del(timer);
198 }
199
200 static Eina_Bool
201 _tt_text_replace_timer_cb(void *data)
202 {
203    _tt_text_replace(NULL, data, NULL);
204    return EINA_TRUE;
205 }
206
207 static void
208 _tt_text_replace_timed(void *data       __UNUSED__,
209                        Evas_Object     *obj,
210                        void *event_info __UNUSED__)
211 {
212    Ecore_Timer *timer = evas_object_data_get(obj, "test-timer");
213    if (timer)
214      {
215         ecore_timer_del(timer);
216         evas_object_data_del(obj, "test-timer");
217         elm_object_text_set(obj, "Simple text tooltip, click to start"
218                                   " changed timed");
219         return;
220      }
221
222    timer = ecore_timer_add(1.5, _tt_text_replace_timer_cb, obj);
223    evas_object_data_set(obj, "test-timer", timer);
224    elm_object_text_set(obj, "Simple text tooltip, click to stop changed"
225                              " timed");
226 }
227
228 static Evas_Object *
229 _tt_icon(void *data   __UNUSED__,
230          Evas_Object *obj)
231 {
232    Evas_Object *ic = elm_icon_add(obj);
233    char buf[PATH_MAX];
234    snprintf(buf, sizeof(buf), "%s/images/logo_small.png",
235             elm_app_data_dir_get());
236    elm_icon_file_set(ic, buf, NULL);
237    elm_icon_scale_set(ic, 0, 0);
238    evas_object_resize(ic, 64, 64);
239    return ic;
240 }
241
242 static Evas_Object *
243 _tt_icon2(void *data   __UNUSED__,
244           Evas_Object *obj)
245 {
246    Evas_Object *ic = elm_icon_add(obj);
247    char buf[PATH_MAX];
248    snprintf(buf, sizeof(buf), "%s/images/icon_00.png", elm_app_data_dir_get());
249    elm_icon_file_set(ic, buf, NULL);
250    elm_icon_scale_set(ic, 0, 0);
251    evas_object_resize(ic, 64, 64);
252    return ic;
253 }
254
255 static void
256 _tt_icon_del(void            *data,
257              Evas_Object *obj __UNUSED__,
258              void            *event_info)
259 {
260    // test to check for del_cb behavior!
261    printf("_tt_icon_del: data=%ld (== 123?), event_info=%p\n",
262           (long)data, event_info);
263 }
264
265 static Eina_Bool
266 _tt_icon_replace_timer_cb(void *data)
267 {
268    static int current = 0;
269
270    elm_object_tooltip_content_cb_set
271      (data, current ? _tt_icon2 : _tt_icon, NULL, NULL);
272
273    current = !current;
274    return EINA_TRUE;
275 }
276
277 static void
278 _tt_icon_replace_timed(void *data       __UNUSED__,
279                        Evas_Object     *obj,
280                        void *event_info __UNUSED__)
281 {
282    Ecore_Timer *timer = evas_object_data_get(obj, "test-timer");
283    if (timer)
284      {
285         ecore_timer_del(timer);
286         evas_object_data_del(obj, "test-timer");
287         elm_object_text_set(obj, "Icon tooltip, click to start changed"
288                                   " timed");
289         return;
290      }
291
292    timer = ecore_timer_add(1.5, _tt_icon_replace_timer_cb, obj);
293    evas_object_data_set(obj, "test-timer", timer);
294    elm_object_text_set(obj, "Icon tooltip, click to stop changed timed");
295 }
296
297 static Eina_Bool
298 _tt_style_replace_timer_cb(void *data)
299 {
300    static int current = 0;
301    elm_object_tooltip_style_set(data, current ? NULL : "transparent");
302    current = !current;
303    return EINA_TRUE;
304 }
305
306 static void
307 _tt_style_replace_timed(void *data       __UNUSED__,
308                         Evas_Object     *obj,
309                         void *event_info __UNUSED__)
310 {
311    Ecore_Timer *timer = evas_object_data_get(obj, "test-timer");
312    if (timer)
313      {
314         ecore_timer_del(timer);
315         evas_object_data_del(obj, "test-timer");
316         elm_object_text_set(obj, "Icon tooltip style, click to start"
317                                   " changed timed");
318         return;
319      }
320
321    timer = ecore_timer_add(1.5, _tt_style_replace_timer_cb, obj);
322    evas_object_data_set(obj, "test-timer", timer);
323    elm_object_text_set(obj, "Icon tooltip style, click to stop changed"
324                              " timed");
325 }
326
327 static void
328 _tt_visible_lock_toggle(void *data       __UNUSED__,
329                         Evas_Object     *obj,
330                         void *event_info __UNUSED__)
331 {
332    static int locked = 0;
333
334    locked = !locked;
335    if (locked)
336      {
337         elm_object_text_set(obj, "Locked tooltip visibility");
338         elm_object_tooltip_text_set(obj, "This tooltip is locked"
339                                          " visible,<br> click the button"
340                                          " to unlock!");
341         elm_object_tooltip_show(obj);
342      }
343    else
344      {
345         elm_object_text_set(obj, "Unlocked tooltip visibility");
346         elm_object_tooltip_text_set(obj, "This tooltip is unlocked"
347                                          " visible,<br> click the button"
348                                          " to lock!");
349         elm_object_tooltip_hide(obj);
350      }
351 }
352
353 void
354 test_tooltip(void *data       __UNUSED__,
355              Evas_Object *obj __UNUSED__,
356              void *event_info __UNUSED__)
357 {
358    Evas_Object *win, *bg, *bx, *tb, *bt, *se, *lst;
359    Elm_Toolbar_Item *ti;
360    Elm_List_Item *li;
361
362    win = elm_win_add(NULL, "tooltip", ELM_WIN_BASIC);
363    elm_win_title_set(win, "Tooltip");
364    elm_win_autodel_set(win, EINA_TRUE);
365
366    bg = elm_bg_add(win);
367    elm_win_resize_object_add(win, bg);
368    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND,
369                                     EVAS_HINT_EXPAND);
370    evas_object_show(bg);
371
372    bx = elm_box_add(win);
373    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND,
374                                     EVAS_HINT_EXPAND);
375    elm_win_resize_object_add(win, bx);
376    evas_object_show(bx);
377
378    tb = elm_toolbar_add(win);
379    elm_toolbar_homogeneous_set(tb, 0);
380    evas_object_size_hint_weight_set(tb, EVAS_HINT_EXPAND, 0.0);
381    evas_object_size_hint_align_set(tb, EVAS_HINT_FILL, 0.0);
382    elm_box_pack_end(bx, tb);
383    evas_object_show(tb);
384
385    ti = elm_toolbar_item_append(tb, NULL, "Open", NULL, NULL);
386    elm_toolbar_item_tooltip_text_set(ti, "Opens a file");
387
388    ti = elm_toolbar_item_append(tb, NULL, "Icon", NULL, NULL);
389    elm_toolbar_item_tooltip_content_cb_set
390      (ti, _tt_item_icon, (void *)456L, _tt_item_icon_del);
391    elm_toolbar_item_tooltip_style_set(ti, "transparent");
392
393    bt = elm_button_add(win);
394    elm_object_text_set(bt, "Simple text tooltip");
395    elm_object_tooltip_text_set(bt, "Simple text tooltip");
396    elm_box_pack_end(bx, bt);
397    evas_object_show(bt);
398
399    bt = elm_button_add(win);
400    elm_object_text_set(bt, "Simple text tooltip, click to change");
401    elm_object_tooltip_text_set(bt, "Initial");
402    evas_object_smart_callback_add(bt, "clicked", _tt_text_replace, NULL);
403    elm_box_pack_end(bx, bt);
404    evas_object_show(bt);
405
406    bt = elm_button_add(win);
407    elm_object_text_set(bt, "Simple text tooltip, click to start"
408                             " changed timed");
409    elm_object_tooltip_text_set(bt, "Initial");
410    evas_object_smart_callback_add(bt, "clicked", _tt_text_replace_timed,
411                                   NULL);
412    elm_box_pack_end(bx, bt);
413    evas_object_show(bt);
414    evas_object_event_callback_add(bt, EVAS_CALLBACK_DEL, _tt_timer_del,
415                                   NULL);
416
417    bt = elm_button_add(win);
418    elm_object_text_set(bt, "Icon tooltip");
419    elm_object_tooltip_content_cb_set(bt, _tt_icon, (void *)123L,
420                                      _tt_icon_del);
421    elm_box_pack_end(bx, bt);
422    evas_object_show(bt);
423
424    bt = elm_button_add(win);
425    elm_object_text_set(bt, "Icon tooltip, click to start changed timed");
426    elm_object_tooltip_content_cb_set(bt, _tt_icon, NULL, NULL);
427    evas_object_smart_callback_add(bt, "clicked", _tt_icon_replace_timed,
428                                   NULL);
429    elm_box_pack_end(bx, bt);
430    evas_object_show(bt);
431    evas_object_event_callback_add(bt, EVAS_CALLBACK_DEL, _tt_timer_del,
432                                   NULL);
433
434    bt = elm_button_add(win);
435    elm_object_text_set(bt, "Transparent Icon tooltip");
436    elm_object_tooltip_content_cb_set(bt, _tt_icon, NULL, NULL);
437    elm_object_tooltip_style_set(bt, "transparent");
438    elm_box_pack_end(bx, bt);
439    evas_object_show(bt);
440
441    bt = elm_button_add(win);
442    elm_object_text_set(bt, "Icon tooltip style, click to start changed"
443                             " timed");
444    elm_object_tooltip_content_cb_set(bt, _tt_icon, NULL, NULL);
445    evas_object_smart_callback_add(bt, "clicked", _tt_style_replace_timed,
446                                   NULL);
447    elm_box_pack_end(bx, bt);
448    evas_object_show(bt);
449    evas_object_event_callback_add(bt, EVAS_CALLBACK_DEL, _tt_timer_del,
450                                   NULL);
451
452    bt = elm_button_add(win);
453    elm_object_text_set(bt, "Unlocked tooltip visibility");
454    elm_object_tooltip_text_set(bt, "This tooltip is unlocked visible,<br>"
455                                    " click the button to lock!");
456    evas_object_smart_callback_add(bt, "clicked", _tt_visible_lock_toggle,
457                                   NULL);
458    elm_box_pack_end(bx, bt);
459    evas_object_show(bt);
460
461    se = elm_entry_add(win);
462    elm_entry_scrollable_set(se, EINA_TRUE);
463    evas_object_size_hint_weight_set(se, EVAS_HINT_EXPAND, 0.0);
464    evas_object_size_hint_align_set(se, EVAS_HINT_FILL, 0.5);
465    elm_entry_scrollbar_policy_set(se, ELM_SCROLLER_POLICY_OFF,
466                                            ELM_SCROLLER_POLICY_OFF);
467    elm_entry_entry_set(se, "Hello, some scrolled entry here!");
468    elm_object_tooltip_text_set(se, "Type something here!");
469    elm_entry_single_line_set(se, 1);
470    elm_box_pack_end(bx, se);
471    evas_object_show(se);
472
473    lst = elm_list_add(win);
474    li = elm_list_item_append(lst, "Hello", NULL, NULL, NULL, NULL);
475    elm_list_item_tooltip_text_set(li, "Something useful here?");
476    li = elm_list_item_append(lst, "Icon Tooltip", NULL, NULL, NULL, NULL);
477    elm_list_item_tooltip_content_cb_set(li, _tt_item_icon, NULL, NULL);
478    evas_object_size_hint_weight_set(lst, EVAS_HINT_EXPAND,
479                                     EVAS_HINT_EXPAND);
480    evas_object_size_hint_align_set(lst, EVAS_HINT_FILL, EVAS_HINT_FILL);
481    evas_object_size_hint_min_set(lst, 100, 100);
482    elm_list_go(lst);
483    elm_box_pack_end(bx, lst);
484    evas_object_show(lst);
485
486    evas_object_resize(win, 320, 580);
487    evas_object_show(win);
488 }
489
490 void
491 test_tooltip2(void *data       __UNUSED__,
492               Evas_Object *obj __UNUSED__,
493               void *event_info __UNUSED__)
494 {
495    Evas_Object *win, *bg, *bx, *grid, *gl;
496    Elm_Genlist_Item *it1, *it2, *it3;
497    static Testitem ti[144];
498    int i, n;
499    char buf[PATH_MAX];
500    const char *img[9] =
501    {
502       "panel_01.jpg",
503       "plant_01.jpg",
504       "rock_01.jpg",
505       "rock_02.jpg",
506       "sky_01.jpg",
507       "sky_02.jpg",
508       "sky_03.jpg",
509       "sky_04.jpg",
510       "wood_01.jpg",
511    };
512
513    win = elm_win_add(NULL, "tooltip2", ELM_WIN_BASIC);
514    elm_win_title_set(win, "Tooltip 2");
515    elm_win_autodel_set(win, EINA_TRUE);
516
517    bg = elm_bg_add(win);
518    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND,
519                                     EVAS_HINT_EXPAND);
520    elm_win_resize_object_add(win, bg);
521    evas_object_show(bg);
522
523    bx = elm_box_add(win);
524    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND,
525                                     EVAS_HINT_EXPAND);
526    elm_win_resize_object_add(win, bx);
527    evas_object_show(bx);
528
529    grid = elm_gengrid_add(win);
530    elm_gengrid_item_size_set(grid, 100, 100);
531    elm_gengrid_horizontal_set(grid, EINA_FALSE);
532    elm_gengrid_multi_select_set(grid, EINA_TRUE);
533    evas_object_size_hint_align_set(grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
534    evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND,
535                                     EVAS_HINT_EXPAND);
536
537    gic.item_style = "default";
538    gic.func.label_get = grdt_lbl_get;
539    gic.func.content_get = grdt_content_get;
540
541    n = 0;
542    for (i = 0; i < 9; i++)
543      {
544         snprintf(buf, sizeof(buf), "%s/images/%s", elm_app_data_dir_get(),
545                  img[n]);
546         n++;
547         ti[i].mode = i;
548         ti[i].path = eina_stringshare_add(buf);
549         ti[i].item = elm_gengrid_item_append(grid, &gic, &(ti[i]), NULL,
550                                              NULL);
551         if (n % 2)
552           elm_gengrid_item_tooltip_text_set(ti[i].item, "Testing X");
553         else
554           elm_gengrid_item_tooltip_text_set(ti[i].item, "Testing Y");
555         if (!(i % 5))
556           elm_gengrid_item_selected_set(ti[i].item, EINA_TRUE);
557      }
558
559    elm_box_pack_end(bx, grid);
560    evas_object_show(grid);
561
562    gl = elm_genlist_add(win);
563    evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
564    evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND,
565                                     EVAS_HINT_EXPAND);
566
567    itct.item_style = "default";
568    itct.func.label_get = gltt_label_get;
569
570    it1 = elm_genlist_item_append(gl, &itct, (void *)1, NULL,
571                                  ELM_GENLIST_ITEM_SUBITEMS, NULL, NULL);
572    it2 = elm_genlist_item_append(gl, &itct, (void *)2, NULL,
573                                  ELM_GENLIST_ITEM_SUBITEMS, NULL, NULL);
574    it3 = elm_genlist_item_append(gl, &itct, (void *)3, NULL,
575                                  ELM_GENLIST_ITEM_NONE, NULL, NULL);
576
577    elm_genlist_item_tooltip_text_set(it1, "Testing 1");
578    elm_genlist_item_tooltip_text_set(it2, "Testing 2");
579    elm_genlist_item_tooltip_text_set(it3, "Testing 3");
580
581    evas_object_smart_callback_add(gl, "expand,request", gltt_exp_req, gl);
582    evas_object_smart_callback_add(gl, "contract,request", gltt_con_req,
583                                   gl);
584    evas_object_smart_callback_add(gl, "expanded", gltt_exp, gl);
585    evas_object_smart_callback_add(gl, "contracted", gltt_con, gl);
586
587    elm_box_pack_end(bx, gl);
588    evas_object_show(gl);
589
590    evas_object_resize(win, 320, 480);
591    evas_object_show(win);
592 }
593
594 #endif