Merge "[WK2] Small Caps font variant issue for Italic fonts" into tizen_2.2
[framework/web/webkit-efl.git] / Tools / EWebLauncher / url_bar.c
1 /*
2  * Copyright (C) 2012 Samsung Electronics
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *  * Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "url_bar.h"
27 #include "url_utils.h"
28
29 #include <Edje.h>
30 #include <Ecore_Evas.h>
31
32 #define PADDING_SIZE 5
33
34 static char *
35 _url_bar_url_get_with_protocol(Url_Bar *urlBar)
36 {
37     const char *url = edje_object_part_text_get(urlBar->entry, "url.text");
38
39     return url_from_user_input(url);
40 }
41
42 static void
43 on_urlbar_key_down(void *data, Evas *e, Evas_Object *obj, void *event_info)
44 {
45     Evas_Event_Key_Down *ev = event_info;
46     Url_Bar *urlBar = (Url_Bar *)data;
47
48     if (!ev->key || strcmp(ev->key, "Return"))
49         return;
50
51     char *url = _url_bar_url_get_with_protocol(urlBar);
52     if (url) {
53 #ifdef EFL_WK2
54         ewk_view_url_set(urlBar->webView, url);
55 #else
56         ewk_view_uri_set(urlBar->webView, url);
57 #endif
58         free(url);
59     }
60     evas_object_focus_set(urlBar->webView, EINA_TRUE);
61 }
62
63 static void
64 on_urlbar_mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info)
65 {
66     Evas_Event_Mouse_Down *ev = event_info;
67     Url_Bar *urlBar = (Url_Bar *)data;
68
69     if (ev->button == 1) {
70         evas_object_focus_set(urlBar->entry, EINA_TRUE);
71         edje_object_signal_emit(urlBar->entry, "entry,action,focus", "entry");
72     }
73 }
74
75 static void
76 on_urlbar_focus_out(void *data, Evas *e, Evas_Object *obj, void *event_info)
77 {
78     Url_Bar *urlBar = (Url_Bar *)data;
79
80     edje_object_signal_emit(urlBar->entry, "entry,action,unfocus", "entry");
81 }
82
83 void
84 url_bar_width_set(Url_Bar *urlBar, int width)
85 {
86     evas_object_move(urlBar->area, 0, 0);
87     evas_object_resize(urlBar->area, width, URL_BAR_HEIGHT);
88
89     evas_object_move(urlBar->entry, PADDING_SIZE, PADDING_SIZE);
90     evas_object_resize(urlBar->entry, width - PADDING_SIZE * 2, URL_BAR_HEIGHT - PADDING_SIZE * 2);
91 }
92
93 Url_Bar *
94 url_bar_add(Evas_Object *webView, int width)
95 {
96     Evas *evas;
97     Url_Bar *url_bar;
98     if (!webView)
99         return NULL;
100     evas = evas_object_evas_get(webView);
101
102     url_bar = (Url_Bar *)malloc(sizeof(Url_Bar));
103     url_bar->webView = webView;
104
105     url_bar->area = evas_object_rectangle_add(evas);
106     evas_object_name_set(url_bar->area, "url_barArea");
107     evas_object_color_set(url_bar->area, 255, 255, 255, 255);
108
109     url_bar->entry = edje_object_add(evas);
110     Eina_Bool ret = edje_object_file_set(url_bar->entry, THEME_DIR"/entry.edj", "control/entry/base/default");
111     if (!ret) {
112         evas_object_del(url_bar->area);
113
114         free(url_bar);
115         return NULL;
116     }
117
118     edje_object_part_text_set(url_bar->entry, "url.text", "");
119
120     /* Set URL bar dimensions and show it */
121     url_bar_width_set(url_bar, width);
122     evas_object_show(url_bar->area);
123     evas_object_show(url_bar->entry);
124
125     evas_object_event_callback_add(url_bar->entry, EVAS_CALLBACK_MOUSE_DOWN, on_urlbar_mouse_down, url_bar);
126     evas_object_event_callback_add(url_bar->entry, EVAS_CALLBACK_KEY_DOWN, on_urlbar_key_down, url_bar);
127     evas_object_event_callback_add(url_bar->entry, EVAS_CALLBACK_FOCUS_OUT, on_urlbar_focus_out, url_bar);
128
129     return url_bar;
130 }
131
132 void
133 url_bar_del(Url_Bar *urlBar)
134 {
135     if (!urlBar)
136         return;
137
138     evas_object_event_callback_del(urlBar->entry, EVAS_CALLBACK_KEY_DOWN, on_urlbar_key_down);
139     evas_object_event_callback_del(urlBar->entry, EVAS_CALLBACK_MOUSE_DOWN, on_urlbar_mouse_down);
140     evas_object_event_callback_del(urlBar->entry, EVAS_CALLBACK_FOCUS_OUT, on_urlbar_focus_out);
141
142     evas_object_del(urlBar->area);
143     evas_object_del(urlBar->entry);
144     free(urlBar);
145 }
146
147 void
148 url_bar_url_set(Url_Bar *urlBar, const char *url)
149 {
150     if (!urlBar || !url)
151         return;
152
153     edje_object_part_text_set(urlBar->entry, "url.text", url);
154 }