Elementary migration revision 69922
[framework/uifw/elementary.git] / src / bin / test_web.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4 #include <Elementary.h>
5 #ifndef ELM_LIB_QUICKLAUNCH
6
7 typedef struct
8 {
9    Evas_Object *web;
10    Evas_Object *btn_back;
11    Evas_Object *btn_fwd;
12    Evas_Object *url_entry;
13    Eina_List *sub_wins;
14    const char* user_agent;
15    Eina_Bool js_hooks : 1;
16 } Web_Test;
17
18 static void
19 _btn_back_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
20 {
21    Evas_Object *web = data;
22
23    elm_web_back(web);
24 }
25
26 static void
27 _btn_fwd_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
28 {
29    Evas_Object *web = data;
30
31    elm_web_forward(web);
32 }
33
34 static void
35 _btn_reload_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
36 {
37    Evas_Object *web = data;
38
39    elm_web_reload(web);
40 }
41
42 static void
43 _url_change_cb(void *data, Evas_Object *obj, void *event_info __UNUSED__)
44 {
45    Evas_Object *web = data;
46    const char *uri = elm_object_text_get(obj);
47
48    elm_web_uri_set(web, uri);
49 }
50
51 static void
52 _toggle_inwin_mode_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
53 {
54    elm_web_inwin_mode_set(data, !elm_web_inwin_mode_get(data));
55 }
56
57 static void
58 _title_changed_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info)
59 {
60    char buf[512];
61    snprintf(buf, sizeof(buf), "Web - %s", (const char *)event_info);
62    elm_win_title_set(data, buf);
63 }
64
65 static void
66 _uri_changed_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info)
67 {
68    Web_Test *wt = data;
69
70    elm_object_text_set(wt->url_entry, event_info);
71
72    elm_object_disabled_set(wt->btn_back, !elm_web_back_possible_get(wt->web));
73    elm_object_disabled_set(wt->btn_fwd, !elm_web_forward_possible_get(wt->web));
74 }
75
76 static void
77 _new_win_del_cb(void *data, Evas_Object *obj, void *event_info __UNUSED__)
78 {
79    Web_Test *wt = data;
80    wt->sub_wins = eina_list_remove(wt->sub_wins, obj);
81 }
82
83 static void
84 _web_win_close_request_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
85 {
86    evas_object_del(data);
87 }
88
89 static Evas_Object *
90 _new_window_hook(void *data, Evas_Object *obj __UNUSED__, Eina_Bool js __UNUSED__, const Elm_Web_Window_Features *wf __UNUSED__)
91 {
92    Web_Test *wt = data;
93    Evas_Object *new_win, *new_web;
94
95    new_win = elm_win_util_standard_add("elm-web-test-popup", "Elm Web Test Popup");
96    elm_win_autodel_set(new_win, EINA_TRUE);
97    evas_object_resize(new_win, 300, 300);
98    evas_object_show(new_win);
99
100    new_web = elm_web_add(new_win);
101    elm_web_useragent_set(new_web, wt->user_agent);
102    evas_object_size_hint_weight_set(new_web, EVAS_HINT_EXPAND,
103                                     EVAS_HINT_EXPAND);
104    elm_win_resize_object_add(new_win, new_web);
105    evas_object_show(new_web);
106
107    evas_object_smart_callback_add(new_win, "delete,request", _new_win_del_cb,
108                                   wt);
109    evas_object_smart_callback_add(new_web, "windows,close,request",
110                                   _web_win_close_request_cb, new_win);
111    wt->sub_wins = eina_list_append(wt->sub_wins, new_win);
112
113    return new_web;
114 }
115
116 static void
117 _alert_del(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
118 {
119    evas_object_del(obj);
120 }
121
122 static Evas_Object *
123 _alert_hook(void *data __UNUSED__, Evas_Object *obj, const char *message)
124 {
125    Evas_Object *popup, *label;
126
127    popup = elm_notify_add(obj);
128    elm_notify_orient_set(popup, ELM_NOTIFY_ORIENT_CENTER);
129    // Using the timeout doesn't seem to go well with the second main loop
130    //elm_notify_timeout_set(popup, 2.0);
131    elm_notify_allow_events_set(popup, EINA_FALSE);
132    evas_object_show(popup);
133
134    evas_object_smart_callback_add(popup, "block,clicked", _alert_del, NULL);
135
136    label = elm_label_add(obj);
137    elm_object_text_set(label, message);
138    elm_object_content_set(popup, label);
139    evas_object_show(label);
140
141    return popup;
142 }
143
144 static void
145 _confirm_ok_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
146 {
147    Eina_Bool *response = data;
148    *response = EINA_TRUE;
149 }
150
151 static void
152 _confirm_cancel_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
153 {
154    Eina_Bool *response = data;
155    *response = EINA_FALSE;
156 }
157
158 static void
159 _confirm_dismiss_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
160 {
161    evas_object_del(data);
162 }
163
164 static Evas_Object *
165 _confirm_hook(void *data __UNUSED__, Evas_Object *obj, const char *message, Eina_Bool *response)
166 {
167    Evas_Object *popup, *box, *box2, *label, *btn_ok, *btn_cancel;
168
169    popup = elm_notify_add(obj);
170    elm_notify_orient_set(popup, ELM_NOTIFY_ORIENT_CENTER);
171    elm_notify_allow_events_set(popup, EINA_FALSE);
172    evas_object_show(popup);
173
174    box = elm_box_add(obj);
175    elm_object_content_set(popup, box);
176    evas_object_show(box);
177
178    label = elm_label_add(obj);
179    elm_object_text_set(label, message);
180    elm_box_pack_end(box, label);
181    evas_object_show(label);
182
183    box2 = elm_box_add(obj);
184    elm_box_horizontal_set(box2, EINA_TRUE);
185    elm_box_pack_end(box, box2);
186    evas_object_show(box2);
187
188    btn_ok = elm_button_add(obj);
189    elm_object_text_set(btn_ok, "Ok");
190    elm_box_pack_end(box2, btn_ok);
191    evas_object_show(btn_ok);
192
193    btn_cancel = elm_button_add(obj);
194    elm_object_text_set(btn_cancel, "Cancel");
195    elm_box_pack_end(box2, btn_cancel);
196    evas_object_show(btn_cancel);
197
198    evas_object_smart_callback_add(btn_ok, "clicked", _confirm_dismiss_cb,
199                                   popup);
200    evas_object_smart_callback_add(btn_cancel, "clicked", _confirm_dismiss_cb,
201                                   popup);
202    evas_object_smart_callback_add(btn_ok, "clicked", _confirm_ok_cb, response);
203    evas_object_smart_callback_add(btn_cancel, "clicked", _confirm_cancel_cb,
204                                   response);
205
206    return popup;
207 }
208
209 static Evas_Object *
210 _prompt_hook(void *data __UNUSED__, Evas_Object *obj __UNUSED__, const char *message __UNUSED__, const char *default_value, char **value, Eina_Bool *response)
211 {
212    *response = EINA_TRUE;
213    *value = default_value ? strdup(default_value) : "No default!";
214    return NULL;
215 }
216
217 static Evas_Object *
218 _file_selector_hook(void *data __UNUSED__, Evas_Object *obj __UNUSED__, Eina_Bool allow_multiple __UNUSED__, Eina_List *accept_types __UNUSED__, Eina_List **selected_files, Eina_Bool *response)
219 {
220    *selected_files = eina_list_append(NULL,
221                                       strdup("/path/to/non_existing_file"));
222    *response = EINA_TRUE;
223    return NULL;
224 }
225
226 static void
227 _console_message_hook(void *data __UNUSED__, Evas_Object *obj __UNUSED__, const char *message, unsigned int line_number, const char *source_id)
228 {
229    printf("CONSOLE: %s:%d:%s\n", source_id, line_number, message);
230 }
231
232 static void
233 _js_popup_hooks_set(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
234 {
235    Web_Test *wt = data;
236
237    wt->js_hooks = !wt->js_hooks;
238    if (wt->js_hooks)
239      {
240         elm_web_dialog_alert_hook_set(wt->web, _alert_hook, NULL);
241         elm_web_dialog_confirm_hook_set(wt->web, _confirm_hook, NULL);
242         elm_web_dialog_prompt_hook_set(wt->web, _prompt_hook, NULL);
243         elm_web_dialog_file_selector_hook_set(wt->web, _file_selector_hook,
244                                               NULL);
245         elm_web_console_message_hook_set(wt->web, _console_message_hook, NULL);
246      }
247    else
248      {
249         elm_web_dialog_alert_hook_set(wt->web, NULL, NULL);
250         elm_web_dialog_confirm_hook_set(wt->web, NULL, NULL);
251         elm_web_dialog_prompt_hook_set(wt->web, NULL, NULL);
252         elm_web_dialog_file_selector_hook_set(wt->web, NULL, NULL);
253         elm_web_console_message_hook_set(wt->web, NULL, NULL);
254      }
255 }
256
257 static void
258 _zoom_out_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
259 {
260    Web_Test *wt = data;
261    double zoom;
262
263    zoom = elm_web_zoom_get(wt->web);
264    if (zoom > 1)
265      zoom -= .5;
266    else
267      zoom /= 2;
268    if (zoom < .05)
269      zoom = .05;
270    elm_web_zoom_set(wt->web, zoom);
271 }
272
273 static void
274 _zoom_in_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
275 {
276    Web_Test *wt = data;
277    double zoom;
278
279    zoom = elm_web_zoom_get(wt->web);
280
281    if (zoom < 1)
282      zoom *= 2;
283    else
284      zoom += .5;
285    if (zoom > 4)
286      zoom = 4;
287    elm_web_zoom_set(wt->web, zoom);
288 }
289
290 static void
291 _zoom_mode_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info)
292 {
293    Web_Test *wt = data;
294    Elm_Object_Item *hoversel_it = event_info;
295    const char *lbl = elm_object_item_text_get(hoversel_it);
296
297    if (!strcmp(lbl, "Manual"))
298      elm_web_zoom_mode_set(wt->web, ELM_WEB_ZOOM_MODE_MANUAL);
299    else if (!strcmp(lbl, "Fit"))
300      elm_web_zoom_mode_set(wt->web, ELM_WEB_ZOOM_MODE_AUTO_FIT);
301    else
302      elm_web_zoom_mode_set(wt->web, ELM_WEB_ZOOM_MODE_AUTO_FILL);
303 }
304
305 static void
306 _show_region_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
307 {
308    Web_Test *wt = data;
309    elm_web_region_show(wt->web, 300, 300, 1, 1);
310 }
311
312 static void
313 _bring_in_region_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
314 {
315    Web_Test *wt = data;
316    elm_web_region_bring_in(wt->web, 50, 0, 1, 1);
317 }
318
319 static void
320 _main_web_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
321 {
322    Web_Test *wt = data;
323    Evas_Object *sub_win;
324
325    EINA_LIST_FREE(wt->sub_wins, sub_win)
326       evas_object_del(sub_win);
327
328    free(wt);
329 }
330
331 void
332 test_web(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__, Eina_Bool mobile)
333 {
334    const char user_agent_firefox[] = "Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1";
335    const char user_agent_mobile[] = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3";
336    Evas_Object *win, *bx, *bx2, *bt, *web, *url;
337    Web_Test *wt;
338
339    elm_need_web();
340
341    wt = calloc(1, sizeof(*wt));
342
343    if (mobile == EINA_TRUE)
344      {
345         win = elm_win_util_standard_add("web-mobile", "Web Mobile");
346         wt->user_agent = user_agent_mobile;
347      }
348    else
349      {
350         win = elm_win_util_standard_add("web", "Web");
351         wt->user_agent = user_agent_firefox;
352      }
353
354    elm_win_autodel_set(win, EINA_TRUE);
355
356    bx = elm_box_add(win);
357    elm_win_resize_object_add(win, bx);
358    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
359    evas_object_show(bx);
360
361    bx2 = elm_box_add(win);
362    elm_box_horizontal_set(bx2, EINA_TRUE);
363    evas_object_size_hint_weight_set(bx2, EVAS_HINT_EXPAND, 0);
364    evas_object_size_hint_align_set(bx2, EVAS_HINT_FILL, 0.0);
365    elm_box_pack_end(bx, bx2);
366    evas_object_show(bx2);
367
368    web = elm_web_add(win);
369    elm_web_useragent_set(web, wt->user_agent);
370    printf("elm_web useragent: %s\n", elm_web_useragent_get(web));
371    evas_object_size_hint_weight_set(web, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
372    evas_object_size_hint_align_set(web, EVAS_HINT_FILL, EVAS_HINT_FILL);
373    elm_box_pack_end(bx, web);
374    evas_object_show(web);
375
376    bt = elm_button_add(win);
377    elm_object_text_set(bt, "<");
378    elm_box_pack_end(bx2, bt);
379    evas_object_show(bt);
380
381    evas_object_smart_callback_add(bt, "clicked", _btn_back_cb, web);
382    wt->btn_back = bt;
383
384    bt = elm_button_add(win);
385    elm_object_text_set(bt, "R");
386    elm_box_pack_end(bx2, bt);
387    evas_object_show(bt);
388
389    evas_object_smart_callback_add(bt, "clicked", _btn_reload_cb, web);
390
391    bt = elm_button_add(win);
392    elm_object_text_set(bt, ">");
393    elm_box_pack_end(bx2, bt);
394    evas_object_show(bt);
395
396    evas_object_smart_callback_add(bt, "clicked", _btn_fwd_cb, web);
397    wt->btn_fwd = bt;
398
399    url = elm_entry_add(win);
400    elm_entry_single_line_set(url, EINA_TRUE);
401    elm_entry_scrollable_set(url, EINA_TRUE);
402    evas_object_size_hint_weight_set(url, EVAS_HINT_EXPAND, 0);
403    evas_object_size_hint_align_set(url, EVAS_HINT_FILL, EVAS_HINT_FILL);
404    elm_box_pack_end(bx2, url);
405    evas_object_show(url);
406
407    evas_object_smart_callback_add(url, "activated", _url_change_cb, web);
408    wt->url_entry = url;
409
410    bx2 = elm_box_add(win);
411    elm_box_horizontal_set(bx2, EINA_TRUE);
412    evas_object_size_hint_weight_set(bx2, EVAS_HINT_EXPAND, 0);
413    evas_object_size_hint_align_set(bx2, EVAS_HINT_FILL, 0);
414    elm_box_pack_end(bx, bx2);
415    evas_object_show(bx2);
416
417    bt = elm_button_add(win);
418    elm_object_text_set(bt, "Inwin Mode");
419    elm_box_pack_end(bx2, bt);
420    evas_object_show(bt);
421
422    evas_object_smart_callback_add(bt, "clicked", _toggle_inwin_mode_cb, web);
423
424    bt = elm_button_add(win);
425    elm_object_text_set(bt, "Custom Hooks");
426    elm_box_pack_end(bx2, bt);
427    evas_object_show(bt);
428
429    evas_object_smart_callback_add(bt, "clicked", _js_popup_hooks_set, wt);
430
431    bt = elm_button_add(win);
432    elm_object_text_set(bt, "-");
433    elm_box_pack_end(bx2, bt);
434    evas_object_show(bt);
435
436    evas_object_smart_callback_add(bt, "clicked", _zoom_out_cb, wt);
437
438    bt = elm_button_add(win);
439    elm_object_text_set(bt, "+");
440    elm_box_pack_end(bx2, bt);
441    evas_object_show(bt);
442
443    evas_object_smart_callback_add(bt, "clicked", _zoom_in_cb, wt);
444
445    bt = elm_hoversel_add(win);
446    elm_object_text_set(bt, "Zoom Mode");
447    elm_box_pack_end(bx2, bt);
448    evas_object_show(bt);
449
450    elm_hoversel_item_add(bt, "Manual", NULL, ELM_ICON_NONE, _zoom_mode_cb, wt);
451    elm_hoversel_item_add(bt, "Fit", NULL, ELM_ICON_NONE, _zoom_mode_cb, wt);
452    elm_hoversel_item_add(bt, "Fill", NULL, ELM_ICON_NONE, _zoom_mode_cb, wt);
453
454    bx2 = elm_box_add(win);
455    elm_box_horizontal_set(bx2, EINA_TRUE);
456    evas_object_size_hint_weight_set(bx2, EVAS_HINT_EXPAND, 0);
457    evas_object_size_hint_align_set(bx2, EVAS_HINT_FILL, 0);
458    elm_box_pack_end(bx, bx2);
459    evas_object_show(bx2);
460
461    bt = elm_button_add(win);
462    elm_object_text_set(bt, "Show 300, 300");
463    elm_box_pack_end(bx2, bt);
464    evas_object_show(bt);
465
466    evas_object_smart_callback_add(bt, "clicked", _show_region_cb, wt);
467
468    bt = elm_button_add(win);
469    elm_object_text_set(bt, "Bring in 50, 0");
470    elm_box_pack_end(bx2, bt);
471    evas_object_show(bt);
472
473    evas_object_smart_callback_add(bt, "clicked", _bring_in_region_cb, wt);
474
475    evas_object_smart_callback_add(web, "title,changed", _title_changed_cb, win);
476    evas_object_smart_callback_add(web, "uri,changed", _uri_changed_cb, wt);
477
478    evas_object_event_callback_add(web, EVAS_CALLBACK_DEL, _main_web_del_cb, wt);
479
480    wt->web = web;
481
482    elm_web_uri_set(web, "http://www.enlightenment.org");
483
484    elm_web_window_create_hook_set(web, _new_window_hook, wt);
485
486    evas_object_resize(win, 320, 480);
487    evas_object_show(win);
488 }
489
490 void
491 test_web_normal(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
492 {
493    test_web(data, obj, event_info, EINA_FALSE);
494 }
495
496 void
497 test_web_mobile(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
498 {
499    test_web(data, obj, event_info, EINA_TRUE);
500 }
501 #endif