added another test for "big" tooltips, showcases obvious bug where tooltip is cut...
[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_icon_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             PACKAGE_DATA_DIR);
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,
127               void *item   __UNUSED__)
128 {
129    Evas_Object *ic = elm_icon_add(obj);
130    char buf[PATH_MAX];
131    snprintf(buf, sizeof(buf), "%s/images/logo.png", PACKAGE_DATA_DIR);
132    elm_icon_file_set(ic, buf, NULL);
133    elm_icon_scale_set(ic, 0, 0);
134    return ic;
135 }
136
137 static void
138 _tt_item_icon_del(void            *data,
139                   Evas_Object *obj __UNUSED__,
140                   void            *event_info)
141 {
142    // test to check for del_cb behavior!
143    printf("_tt_icon_del: data=%ld (== 456?), event_info=%p\n",
144           (long)data, event_info);
145 }
146
147 static void
148 _tt_text_replace(void *data       __UNUSED__,
149                  Evas_Object     *obj,
150                  void *event_info __UNUSED__)
151 {
152    static int count = 0;
153    char buf[64];
154    snprintf(buf, sizeof(buf), "count=%d", count);
155    count++;
156    elm_object_tooltip_text_set(obj, buf);
157 }
158
159 static void
160 _tt_timer_del(void *data       __UNUSED__,
161               Evas *e          __UNUSED__,
162               Evas_Object     *obj,
163               void *event_info __UNUSED__)
164 {
165    Ecore_Timer *timer = evas_object_data_del(obj, "test-timer");
166    if (!timer) return;
167    ecore_timer_del(timer);
168 }
169
170 static Eina_Bool
171 _tt_text_replace_timer_cb(void *data)
172 {
173    _tt_text_replace(NULL, data, NULL);
174    return EINA_TRUE;
175 }
176
177 static void
178 _tt_text_replace_timed(void *data       __UNUSED__,
179                        Evas_Object     *obj,
180                        void *event_info __UNUSED__)
181 {
182    Ecore_Timer *timer = evas_object_data_get(obj, "test-timer");
183    if (timer)
184      {
185         ecore_timer_del(timer);
186         evas_object_data_del(obj, "test-timer");
187         elm_object_text_set(obj, "Simple text tooltip, click to start"
188                                   " changed timed");
189         return;
190      }
191
192    timer = ecore_timer_add(1.5, _tt_text_replace_timer_cb, obj);
193    evas_object_data_set(obj, "test-timer", timer);
194    elm_object_text_set(obj, "Simple text tooltip, click to stop changed"
195                              " timed");
196 }
197
198 static Evas_Object *
199 _tt_icon(void *data   __UNUSED__,
200          Evas_Object *obj)
201 {
202    Evas_Object *ic = elm_icon_add(obj);
203    char buf[PATH_MAX];
204    snprintf(buf, sizeof(buf), "%s/images/logo_small.png",
205             PACKAGE_DATA_DIR);
206    elm_icon_file_set(ic, buf, NULL);
207    elm_icon_scale_set(ic, 0, 0);
208    evas_object_resize(ic, 64, 64);
209    return ic;
210 }
211
212 static Evas_Object *
213 _tt_icon2(void *data   __UNUSED__,
214           Evas_Object *obj)
215 {
216    Evas_Object *ic = elm_icon_add(obj);
217    char buf[PATH_MAX];
218    snprintf(buf, sizeof(buf), "%s/images/icon_00.png", PACKAGE_DATA_DIR);
219    elm_icon_file_set(ic, buf, NULL);
220    elm_icon_scale_set(ic, 0, 0);
221    evas_object_resize(ic, 64, 64);
222    return ic;
223 }
224
225 static void
226 _tt_icon_del(void            *data,
227              Evas_Object *obj __UNUSED__,
228              void            *event_info)
229 {
230    // test to check for del_cb behavior!
231    printf("_tt_icon_del: data=%ld (== 123?), event_info=%p\n",
232           (long)data, event_info);
233 }
234
235 static Eina_Bool
236 _tt_icon_replace_timer_cb(void *data)
237 {
238    static int current = 0;
239
240    elm_object_tooltip_content_cb_set
241      (data, current ? _tt_icon2 : _tt_icon, NULL, NULL);
242
243    current = !current;
244    return EINA_TRUE;
245 }
246
247 static void
248 _tt_icon_replace_timed(void *data       __UNUSED__,
249                        Evas_Object     *obj,
250                        void *event_info __UNUSED__)
251 {
252    Ecore_Timer *timer = evas_object_data_get(obj, "test-timer");
253    if (timer)
254      {
255         ecore_timer_del(timer);
256         evas_object_data_del(obj, "test-timer");
257         elm_object_text_set(obj, "Icon tooltip, click to start changed"
258                                   " timed");
259         return;
260      }
261
262    timer = ecore_timer_add(1.5, _tt_icon_replace_timer_cb, obj);
263    evas_object_data_set(obj, "test-timer", timer);
264    elm_object_text_set(obj, "Icon tooltip, click to stop changed timed");
265 }
266
267 static Eina_Bool
268 _tt_style_replace_timer_cb(void *data)
269 {
270    static int current = 0;
271    elm_object_tooltip_style_set(data, current ? NULL : "transparent");
272    current = !current;
273    return EINA_TRUE;
274 }
275
276 static void
277 _tt_style_replace_timed(void *data       __UNUSED__,
278                         Evas_Object     *obj,
279                         void *event_info __UNUSED__)
280 {
281    Ecore_Timer *timer = evas_object_data_get(obj, "test-timer");
282    if (timer)
283      {
284         ecore_timer_del(timer);
285         evas_object_data_del(obj, "test-timer");
286         elm_object_text_set(obj, "Icon tooltip style, click to start"
287                                   " changed timed");
288         return;
289      }
290
291    timer = ecore_timer_add(1.5, _tt_style_replace_timer_cb, obj);
292    evas_object_data_set(obj, "test-timer", timer);
293    elm_object_text_set(obj, "Icon tooltip style, click to stop changed"
294                              " timed");
295 }
296
297 static void
298 _tt_visible_lock_toggle(void *data       __UNUSED__,
299                         Evas_Object     *obj,
300                         void *event_info __UNUSED__)
301 {
302    static int locked = 0;
303
304    locked = !locked;
305    if (locked)
306      {
307         elm_object_text_set(obj, "Locked tooltip visibility");
308         elm_object_tooltip_text_set(obj, "This tooltip is locked"
309                                          " visible,<br> click the button"
310                                          " to unlock!");
311         elm_object_tooltip_show(obj);
312      }
313    else
314      {
315         elm_object_text_set(obj, "Unlocked tooltip visibility");
316         elm_object_tooltip_text_set(obj, "This tooltip is unlocked"
317                                          " visible,<br> click the button"
318                                          " to lock!");
319         elm_object_tooltip_hide(obj);
320      }
321 }
322
323 void
324 test_tooltip(void *data       __UNUSED__,
325              Evas_Object *obj __UNUSED__,
326              void *event_info __UNUSED__)
327 {
328    Evas_Object *win, *bg, *bx, *tb, *bt, *se, *lst;
329    Elm_Toolbar_Item *ti;
330    Elm_List_Item *li;
331
332    win = elm_win_add(NULL, "tooltip", ELM_WIN_BASIC);
333    elm_win_title_set(win, "Tooltip");
334    elm_win_autodel_set(win, 1);
335
336    bg = elm_bg_add(win);
337    elm_win_resize_object_add(win, bg);
338    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND,
339                                     EVAS_HINT_EXPAND);
340    evas_object_show(bg);
341
342    bx = elm_box_add(win);
343    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND,
344                                     EVAS_HINT_EXPAND);
345    elm_win_resize_object_add(win, bx);
346    evas_object_show(bx);
347
348    tb = elm_toolbar_add(win);
349    elm_toolbar_homogeneous_set(tb, 0);
350    evas_object_size_hint_weight_set(tb, EVAS_HINT_EXPAND, 0.0);
351    evas_object_size_hint_align_set(tb, EVAS_HINT_FILL, 0.0);
352    elm_box_pack_end(bx, tb);
353    evas_object_show(tb);
354
355    ti = elm_toolbar_item_append(tb, NULL, "Open", NULL, NULL);
356    elm_toolbar_item_tooltip_text_set(ti, "Opens a file");
357
358    ti = elm_toolbar_item_append(tb, NULL, "Icon", NULL, NULL);
359    elm_toolbar_item_tooltip_content_cb_set
360      (ti, _tt_item_icon, (void *)456L, _tt_item_icon_del);
361    elm_toolbar_item_tooltip_style_set(ti, "transparent");
362
363    bt = elm_button_add(win);
364    elm_object_text_set(bt, "Simple text tooltip");
365    elm_object_tooltip_text_set(bt, "Simple text tooltip");
366    elm_box_pack_end(bx, bt);
367    evas_object_show(bt);
368
369    bt = elm_button_add(win);
370    elm_object_text_set(bt, "Simple text tooltip, click to change");
371    elm_object_tooltip_text_set(bt, "Initial");
372    evas_object_smart_callback_add(bt, "clicked", _tt_text_replace, NULL);
373    elm_box_pack_end(bx, bt);
374    evas_object_show(bt);
375
376    bt = elm_button_add(win);
377    elm_object_text_set(bt, "Simple text tooltip, click to start"
378                             " changed timed");
379    elm_object_tooltip_text_set(bt, "Initial");
380    evas_object_smart_callback_add(bt, "clicked", _tt_text_replace_timed,
381                                   NULL);
382    elm_box_pack_end(bx, bt);
383    evas_object_show(bt);
384    evas_object_event_callback_add(bt, EVAS_CALLBACK_DEL, _tt_timer_del,
385                                   NULL);
386
387    bt = elm_button_add(win);
388    elm_object_text_set(bt, "Icon tooltip");
389    elm_object_tooltip_content_cb_set(bt, _tt_icon, (void *)123L,
390                                      _tt_icon_del);
391    elm_box_pack_end(bx, bt);
392    evas_object_show(bt);
393
394    bt = elm_button_add(win);
395    elm_object_text_set(bt, "Icon tooltip, click to start changed timed");
396    elm_object_tooltip_content_cb_set(bt, _tt_icon, NULL, NULL);
397    evas_object_smart_callback_add(bt, "clicked", _tt_icon_replace_timed,
398                                   NULL);
399    elm_box_pack_end(bx, bt);
400    evas_object_show(bt);
401    evas_object_event_callback_add(bt, EVAS_CALLBACK_DEL, _tt_timer_del,
402                                   NULL);
403
404    bt = elm_button_add(win);
405    elm_object_text_set(bt, "Transparent Icon tooltip");
406    elm_object_tooltip_content_cb_set(bt, _tt_icon, NULL, NULL);
407    elm_object_tooltip_style_set(bt, "transparent");
408    elm_box_pack_end(bx, bt);
409    evas_object_show(bt);
410
411    bt = elm_button_add(win);
412    elm_object_text_set(bt, "Icon tooltip style, click to start changed"
413                             " timed");
414    elm_object_tooltip_content_cb_set(bt, _tt_icon, NULL, NULL);
415    evas_object_smart_callback_add(bt, "clicked", _tt_style_replace_timed,
416                                   NULL);
417    elm_box_pack_end(bx, bt);
418    evas_object_show(bt);
419    evas_object_event_callback_add(bt, EVAS_CALLBACK_DEL, _tt_timer_del,
420                                   NULL);
421
422    bt = elm_button_add(win);
423    elm_object_text_set(bt, "Unlocked tooltip visibility");
424    elm_object_tooltip_text_set(bt, "This tooltip is unlocked visible,<br>"
425                                    " click the button to lock!");
426    evas_object_smart_callback_add(bt, "clicked", _tt_visible_lock_toggle,
427                                   NULL);
428    elm_box_pack_end(bx, bt);
429    evas_object_show(bt);
430
431    se = elm_entry_add(win);
432    elm_entry_scrollable_set(se, EINA_TRUE);
433    evas_object_size_hint_weight_set(se, EVAS_HINT_EXPAND, 0.0);
434    evas_object_size_hint_align_set(se, EVAS_HINT_FILL, 0.5);
435    elm_entry_scrollbar_policy_set(se, ELM_SCROLLER_POLICY_OFF,
436                                            ELM_SCROLLER_POLICY_OFF);
437    elm_entry_entry_set(se, "Hello, some scrolled entry here!");
438    elm_object_tooltip_text_set(se, "Type something here!");
439    elm_entry_single_line_set(se, 1);
440    elm_box_pack_end(bx, se);
441    evas_object_show(se);
442
443    lst = elm_list_add(win);
444    li = elm_list_item_append(lst, "Hello", NULL, NULL, NULL, NULL);
445    elm_list_item_tooltip_text_set(li, "Something useful here?");
446    li = elm_list_item_append(lst, "Icon Tooltip", NULL, NULL, NULL, NULL);
447    elm_list_item_tooltip_content_cb_set(li, _tt_item_icon, NULL, NULL);
448    li = elm_list_item_append(lst, "Big Icon Tooltip", NULL, NULL, NULL, NULL);
449    elm_list_item_tooltip_content_cb_set(li, _tt_item_icon2, NULL, NULL);
450    evas_object_size_hint_weight_set(lst, EVAS_HINT_EXPAND,
451                                     EVAS_HINT_EXPAND);
452    evas_object_size_hint_align_set(lst, EVAS_HINT_FILL, EVAS_HINT_FILL);
453    evas_object_size_hint_min_set(lst, 100, 100);
454    elm_list_go(lst);
455    elm_box_pack_end(bx, lst);
456    evas_object_show(lst);
457
458    evas_object_resize(win, 320, 580);
459    evas_object_show(win);
460 }
461
462 void
463 test_tooltip2(void *data       __UNUSED__,
464               Evas_Object *obj __UNUSED__,
465               void *event_info __UNUSED__)
466 {
467    Evas_Object *win, *bg, *bx, *grid, *gl;
468    Elm_Genlist_Item *it1, *it2, *it3;
469    static Testitem ti[144];
470    int i, n;
471    char buf[PATH_MAX];
472    const char *img[9] =
473    {
474       "panel_01.jpg",
475       "plant_01.jpg",
476       "rock_01.jpg",
477       "rock_02.jpg",
478       "sky_01.jpg",
479       "sky_02.jpg",
480       "sky_03.jpg",
481       "sky_04.jpg",
482       "wood_01.jpg",
483    };
484
485    win = elm_win_add(NULL, "tooltip2", ELM_WIN_BASIC);
486    elm_win_title_set(win, "Tooltip 2");
487    elm_win_autodel_set(win, 1);
488
489    bg = elm_bg_add(win);
490    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND,
491                                     EVAS_HINT_EXPAND);
492    elm_win_resize_object_add(win, bg);
493    evas_object_show(bg);
494
495    bx = elm_box_add(win);
496    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND,
497                                     EVAS_HINT_EXPAND);
498    elm_win_resize_object_add(win, bx);
499    evas_object_show(bx);
500
501    grid = elm_gengrid_add(win);
502    elm_gengrid_item_size_set(grid, 100, 100);
503    elm_gengrid_horizontal_set(grid, EINA_FALSE);
504    elm_gengrid_multi_select_set(grid, EINA_TRUE);
505    evas_object_size_hint_align_set(grid, EVAS_HINT_FILL, EVAS_HINT_FILL);
506    evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND,
507                                     EVAS_HINT_EXPAND);
508
509    gic.item_style = "default";
510    gic.func.label_get = grdt_lbl_get;
511    gic.func.icon_get = grdt_icon_get;
512
513    n = 0;
514    for (i = 0; i < 9; i++)
515      {
516         snprintf(buf, sizeof(buf), "%s/images/%s", PACKAGE_DATA_DIR,
517                  img[n]);
518         n++;
519         ti[i].mode = i;
520         ti[i].path = eina_stringshare_add(buf);
521         ti[i].item = elm_gengrid_item_append(grid, &gic, &(ti[i]), NULL,
522                                              NULL);
523         if (n % 2)
524           elm_gengrid_item_tooltip_text_set(ti[i].item, "Testing X");
525         else
526           elm_gengrid_item_tooltip_text_set(ti[i].item, "Testing Y");
527         if (!(i % 5))
528           elm_gengrid_item_selected_set(ti[i].item, EINA_TRUE);
529      }
530
531    elm_box_pack_end(bx, grid);
532    evas_object_show(grid);
533
534    gl = elm_genlist_add(win);
535    evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
536    evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND,
537                                     EVAS_HINT_EXPAND);
538
539    itct.item_style = "default";
540    itct.func.label_get = gltt_label_get;
541
542    it1 = elm_genlist_item_append(gl, &itct, (void *)1, NULL,
543                                  ELM_GENLIST_ITEM_SUBITEMS, NULL, NULL);
544    it2 = elm_genlist_item_append(gl, &itct, (void *)2, NULL,
545                                  ELM_GENLIST_ITEM_SUBITEMS, NULL, NULL);
546    it3 = elm_genlist_item_append(gl, &itct, (void *)3, NULL,
547                                  ELM_GENLIST_ITEM_NONE, NULL, NULL);
548
549    elm_genlist_item_tooltip_text_set(it1, "Testing 1");
550    elm_genlist_item_tooltip_text_set(it2, "Testing 2");
551    elm_genlist_item_tooltip_text_set(it3, "Testing 3");
552
553    evas_object_smart_callback_add(gl, "expand,request", gltt_exp_req, gl);
554    evas_object_smart_callback_add(gl, "contract,request", gltt_con_req,
555                                   gl);
556    evas_object_smart_callback_add(gl, "expanded", gltt_exp, gl);
557    evas_object_smart_callback_add(gl, "contracted", gltt_con, gl);
558
559    elm_box_pack_end(bx, gl);
560    evas_object_show(gl);
561
562    evas_object_resize(win, 320, 480);
563    evas_object_show(win);
564 }
565
566 #endif