2 * gcc -o web_example web_example.c `pkg-config --cflags --libs elementary ewebkit` -D_GNU_SOURCE
4 #include <Elementary.h>
5 #ifdef HAVE_ELEMENTARY_WEB
9 # include "elementary_config.h"
11 # define __UNUSED__ __attribute__((unused))
14 typedef struct _Tab_Data Tab_Data;
19 Evas_Object *main_box;
22 Evas_Object *default_web;
24 Evas_Object *close_tab;
25 Evas_Object *search_box;
26 Evas_Object *search_entry;
34 Tab_Data *current_tab;
36 Eina_Bool exiting : 1;
46 static Evas_Object * _web_create_window_cb(void *data, Evas_Object *obj, Eina_Bool js, const Elm_Web_Window_Features *wf);
49 nav_button_update(App_Data *ad)
53 back = !elm_web_back_possible(ad->current_tab->web);
54 fwd = !elm_web_forward_possible(ad->current_tab->web);
56 elm_object_disabled_set(ad->nav.back, back);
57 elm_object_disabled_set(ad->nav.fwd, fwd);
61 tab_current_set(Tab_Data *td)
65 if (td == td->app->current_tab)
68 td->app->current_tab = td;
70 uri = elm_web_uri_get(td->web);
71 elm_object_text_set(td->app->url, uri);
73 nav_button_update(td->app);
74 elm_entry_icon_visible_set(td->app->url, EINA_TRUE);
76 elm_pager_content_promote(td->app->pager, td->web);
80 _tab_clicked_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info)
83 /* the first toolbar_item_append() calls the select callback before the item
84 * is assigned, so we need a workaround for that. */
91 _title_changed_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info)
94 const char *title = event_info;
98 strncpy(buf, title, sizeof(buf) - 1);
99 elm_object_item_text_set(td->tab, buf);
103 _uri_changed_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info)
106 const char *uri = event_info;
108 if (td != td->app->current_tab)
111 nav_button_update(td->app);
112 elm_object_text_set(td->app->url, uri);
116 _web_free_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
121 elm_toolbar_item_del(td->tab);
127 _tb_item_del_cb(void *data, Evas_Object *obj, void *event_info __UNUSED__)
130 if (!td->app->exiting && !elm_toolbar_selected_item_get(obj))
132 td->app->current_tab = NULL;
133 elm_entry_icon_visible_set(td->app->url, EINA_FALSE);
134 if (td->app->search_box)
135 evas_object_del(td->app->search_box);
141 tab_add(App_Data *ad)
145 td = calloc(1, sizeof(Tab_Data));
146 if (!td) return NULL;
148 td->web = elm_web_add(ad->win);
149 elm_web_window_create_hook_set(td->web, _web_create_window_cb, ad);
150 elm_web_inwin_mode_set(td->web, EINA_TRUE);
151 evas_object_size_hint_weight_set(td->web, EVAS_HINT_EXPAND,
153 evas_object_size_hint_align_set(td->web, EVAS_HINT_FILL, EVAS_HINT_FILL);
154 elm_pager_content_push(ad->pager, td->web);
157 td->tab = elm_toolbar_item_append(td->app->tabs, NULL, "New tab",
158 _tab_clicked_cb, td);
159 elm_object_item_del_cb_set(td->tab, _tb_item_del_cb);
161 evas_object_data_set(td->web, "tab_data", td);
163 evas_object_smart_callback_add(td->web, "title,changed", _title_changed_cb,
165 evas_object_smart_callback_add(td->web, "uri,changed", _uri_changed_cb, td);
166 evas_object_event_callback_add(td->web, EVAS_CALLBACK_FREE, _web_free_cb,
169 elm_toolbar_item_selected_set(td->tab, EINA_TRUE);
174 uri_sanitize(const char *uri)
180 if (!uri || !*uri) return NULL;
182 tmp = strstr(uri, "://");
183 if (!tmp || (tmp == uri) || (tmp > (uri + 15)))
185 char *new_uri = NULL;
186 if (ecore_file_exists(uri))
189 new_uri = ecore_file_realpath(uri);
194 if (asprintf(&fixed_uri, "%s://%s", schema, new_uri ? new_uri : uri) >
209 tab_uri_set(Tab_Data *td, const char *uri)
211 char *sane_uri = uri_sanitize(uri);
212 elm_web_uri_set(td->web, sane_uri);
217 _url_activated_cb(void *data, Evas_Object *obj, void *event_info __UNUSED__)
221 const char *uri = eina_stringshare_ref(elm_object_text_get(obj));
223 if (!ad->current_tab)
226 td = ad->current_tab;
227 tab_uri_set(td, uri);
228 eina_stringshare_del(uri);
232 _nav_back_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
236 elm_web_back(ad->current_tab->web);
240 _nav_refresh_cb(void *data, Evas_Object *obj, void *event_info __UNUSED__)
243 const Evas_Modifier *mods = evas_key_modifier_get(evas_object_evas_get(obj));
245 if (evas_key_modifier_is_set(mods, "Shift"))
246 elm_web_reload_full(ad->current_tab->web);
248 elm_web_reload(ad->current_tab->web);
252 _nav_fwd_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
256 elm_web_forward(ad->current_tab->web);
260 _close_tab_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
264 if (!ad->current_tab)
266 evas_object_del(ad->current_tab->web);
270 _add_tab_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
274 elm_object_focus_set(ad->url, EINA_TRUE);
278 _web_create_window_cb(void *data, Evas_Object *obj __UNUSED__, Eina_Bool js __UNUSED__, const Elm_Web_Window_Features *wf __UNUSED__)
288 _win_del_request_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
291 ad->exiting = EINA_TRUE;
295 _win_free_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
301 _search_entry_changed_cb(void *data, Evas_Object *obj, void *event_info __UNUSED__)
306 text = elm_object_text_get(obj);
307 elm_web_text_search(ad->current_tab->web, text, EINA_FALSE, EINA_TRUE,
309 elm_web_text_matches_unmark_all(ad->current_tab->web);
310 elm_web_text_matches_mark(ad->current_tab->web, text, EINA_FALSE, EINA_TRUE,
315 _search_entry_activate_cb(void *data, Evas_Object *obj, void *event_info __UNUSED__)
320 text = elm_object_text_get(obj);
321 elm_web_text_search(ad->current_tab->web, text, EINA_FALSE, EINA_TRUE,
326 _search_next_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
331 text = elm_object_text_get(ad->search_entry);
332 elm_web_text_search(ad->current_tab->web, text, EINA_FALSE, EINA_TRUE,
337 _search_prev_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
342 text = elm_object_text_get(ad->search_entry);
343 elm_web_text_search(ad->current_tab->web, text, EINA_FALSE, EINA_FALSE,
348 _search_close_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
351 evas_object_del(ad->search_box);
355 _search_box_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
358 ad->search_box = NULL;
359 ad->search_entry = NULL;
363 _win_search_trigger_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
365 Evas_Event_Key_Down *ev = event_info;
367 Evas_Object *box, *box2, *entry, *btn, *ic;
369 if (strcmp(ev->keyname, "f") ||
370 !evas_key_modifier_is_set(ev->modifiers, "Control"))
372 if (ad->search_box || !ad->current_tab)
375 box = elm_box_add(ad->win);
376 elm_box_horizontal_set(box, EINA_TRUE);
377 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, 0.0);
378 evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
379 elm_box_pack_after(ad->main_box, box, ad->url);
380 evas_object_show(box);
382 evas_object_event_callback_add(box, EVAS_CALLBACK_DEL, _search_box_del_cb,
385 entry = elm_entry_add(ad->win);
386 elm_entry_single_line_set(entry, EINA_TRUE);
387 elm_entry_scrollable_set(entry, EINA_TRUE);
388 evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
389 evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
390 elm_box_pack_end(box, entry);
391 evas_object_show(entry);
393 evas_object_smart_callback_add(entry, "changed", _search_entry_changed_cb,
395 evas_object_smart_callback_add(entry, "activated", _search_entry_activate_cb,
398 box2 = elm_box_add(ad->win);
399 elm_box_horizontal_set(box2, EINA_TRUE);
400 elm_object_part_content_set(entry, "end", box2);
402 btn = elm_button_add(ad->win);
403 elm_box_pack_end(box2, btn);
404 evas_object_show(btn);
406 ic = elm_icon_add(ad->win);
407 elm_icon_standard_set(ic, "arrow_up");
408 elm_object_part_content_set(btn, "icon", ic);
410 evas_object_smart_callback_add(btn, "clicked", _search_prev_cb, ad);
412 btn = elm_button_add(ad->win);
413 elm_box_pack_end(box2, btn);
414 evas_object_show(btn);
416 ic = elm_icon_add(ad->win);
417 elm_icon_standard_set(ic, "arrow_down");
418 elm_object_part_content_set(btn, "icon", ic);
420 evas_object_smart_callback_add(btn, "clicked", _search_next_cb, ad);
422 btn = elm_button_add(ad->win);
423 elm_box_pack_end(box, btn);
424 evas_object_show(btn);
426 ic = elm_icon_add(ad->win);
427 elm_icon_standard_set(ic, "close");
428 elm_object_part_content_set(btn, "icon", ic);
430 evas_object_smart_callback_add(btn, "clicked", _search_close_cb, ad);
432 ad->search_box = box;
433 ad->search_entry = entry;
435 elm_object_focus_set(entry, EINA_TRUE);
439 default_content_set(Evas_Object *web)
441 Evas_Object *view, *frame;
442 const char contents[] = ""
445 " <title>Nothing to see here, move along</title>\n"
448 " <a href=\"http://www.enlightenment.org\" target=\"_blank\">E</a>\n"
450 " <a href=\"http://www.google.com\" target=\"_blank\">Google</a>\n"
455 #ifdef HAVE_ELEMENTARY_WEB
456 view = elm_web_webkit_view_get(web);
457 frame = ewk_view_frame_main_get(view);
458 ewk_frame_contents_set(frame, contents, sizeof(contents) - 1, "text/html",
464 elm_main(int argc __UNUSED__, char *argv[] __UNUSED__)
466 Evas_Object *win, *bg, *box, *box2, *btn, *ic, *url, *pager, *tabs, *web;
468 Evas_Modifier_Mask ctrl_mask;
474 ad = calloc(1, sizeof(App_Data));
477 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
479 win = elm_win_add(NULL, "example-web", ELM_WIN_BASIC);
480 elm_win_autodel_set(win, EINA_TRUE);
482 e = evas_object_evas_get(win);
483 ctrl_mask = evas_key_modifier_mask_get(e, "Control");
484 if (!evas_object_key_grab(win, "f", ctrl_mask, 0, EINA_TRUE))
485 fprintf(stderr, "Could not grab trigger for search dialog\n");
487 evas_object_smart_callback_add(win, "delete,request", _win_del_request_cb,
489 evas_object_event_callback_add(win, EVAS_CALLBACK_KEY_DOWN,
490 _win_search_trigger_cb, ad);
491 evas_object_event_callback_add(win, EVAS_CALLBACK_FREE, _win_free_cb, ad);
493 bg = elm_bg_add(win);
494 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
495 elm_win_resize_object_add(win, bg);
496 evas_object_show(bg);
498 box = elm_box_add(win);
499 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
500 elm_win_resize_object_add(win, box);
501 evas_object_show(box);
503 url = elm_entry_add(win);
504 elm_entry_single_line_set(url, EINA_TRUE);
505 elm_entry_scrollable_set(url, EINA_TRUE);
506 evas_object_size_hint_weight_set(url, EVAS_HINT_EXPAND, 0.0);
507 evas_object_size_hint_align_set(url, EVAS_HINT_FILL, EVAS_HINT_FILL);
508 elm_box_pack_end(box, url);
509 evas_object_show(url);
511 evas_object_smart_callback_add(url, "activated", _url_activated_cb, ad);
513 box2 = elm_box_add(win);
514 elm_box_horizontal_set(box2, EINA_TRUE);
515 elm_object_part_content_set(url, "icon", box2);
516 elm_entry_icon_visible_set(url, EINA_FALSE);
518 btn = elm_button_add(win);
519 elm_box_pack_end(box2, btn);
520 evas_object_show(btn);
524 ic = elm_icon_add(win);
525 elm_icon_standard_set(ic, "arrow_left");
526 elm_object_part_content_set(btn, "icon", ic);
528 evas_object_smart_callback_add(btn, "clicked", _nav_back_cb, ad);
530 btn = elm_button_add(win);
531 elm_box_pack_end(box2, btn);
532 evas_object_show(btn);
534 ad->nav.refresh = btn;
536 ic = elm_icon_add(win);
537 elm_icon_standard_set(ic, "refresh");
538 elm_object_part_content_set(btn, "icon", ic);
540 evas_object_smart_callback_add(btn, "clicked", _nav_refresh_cb, ad);
542 btn = elm_button_add(win);
543 elm_box_pack_end(box2, btn);
544 evas_object_show(btn);
548 ic = elm_icon_add(win);
549 elm_icon_standard_set(ic, "arrow_right");
550 elm_object_part_content_set(btn, "icon", ic);
552 evas_object_smart_callback_add(btn, "clicked", _nav_fwd_cb, ad);
554 box2 = elm_box_add(win);
555 elm_box_horizontal_set(box2, EINA_TRUE);
556 evas_object_size_hint_weight_set(box2, EVAS_HINT_EXPAND, 0.0);
557 evas_object_size_hint_align_set(box2, EVAS_HINT_FILL, EVAS_HINT_FILL);
558 elm_box_pack_end(box, box2);
559 evas_object_show(box2);
561 btn = elm_button_add(win);
562 elm_box_pack_end(box2, btn);
563 evas_object_show(btn);
565 ic = elm_icon_add(win);
566 elm_icon_standard_set(ic, "file");
567 elm_object_part_content_set(btn, "icon", ic);
569 evas_object_smart_callback_add(btn, "clicked", _add_tab_cb, ad);
571 tabs = elm_toolbar_add(win);
572 elm_toolbar_align_set(tabs, 0.0);
573 elm_toolbar_always_select_mode_set(tabs, EINA_TRUE);
574 elm_toolbar_homogeneous_set(tabs, EINA_FALSE);
575 elm_toolbar_shrink_mode_set(tabs, ELM_TOOLBAR_SHRINK_MENU);
576 evas_object_size_hint_weight_set(tabs, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
577 evas_object_size_hint_align_set(tabs, EVAS_HINT_FILL, EVAS_HINT_FILL);
578 elm_box_pack_end(box2, tabs);
579 evas_object_show(tabs);
581 btn = elm_button_add(win);
582 elm_box_pack_end(box2, btn);
583 evas_object_show(btn);
585 evas_object_smart_callback_add(btn, "clicked", _close_tab_cb, ad);
587 ic = elm_icon_add(win);
588 elm_icon_standard_set(ic, "close");
589 elm_object_part_content_set(btn, "icon", ic);
591 pager = elm_pager_add(win);
592 evas_object_size_hint_weight_set(pager, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
593 evas_object_size_hint_align_set(pager, EVAS_HINT_FILL, EVAS_HINT_FILL);
594 elm_box_pack_end(box, pager);
595 evas_object_show(pager);
597 elm_toolbar_menu_parent_set(tabs, pager);
599 web = elm_web_add(win);
600 elm_web_window_create_hook_set(web, _web_create_window_cb, ad);
601 elm_web_history_enable_set(web, EINA_FALSE);
602 evas_object_size_hint_weight_set(web, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
603 evas_object_size_hint_align_set(web, EVAS_HINT_FILL, EVAS_HINT_FILL);
604 elm_pager_content_push(pager, web);
606 default_content_set(web);
612 ad->default_web = web;
616 evas_object_resize(win, 480, 640);
617 evas_object_show(win);