ec9830b99a1af940d77b10902f34f6e3cd88bfab
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / tizen / WebContextMenuProxyTizen.cpp
1 /*
2  * Copyright (C) 2011 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. 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 APPLE INC. AND ITS CONTRIBUTORS AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WebContextMenuProxyTizen.h"
28
29 #include "WebContextMenuItemData.h"
30 #include "WebPageProxy.h"
31 #include "ewk_view_private.h"
32
33 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
34 #include <Elementary.h>
35 #include "ewk_util.h"
36 #endif
37
38 #if ENABLE(TIZEN_WEBKIT2_CLIPBOARD_HELPER)
39 #include "ClipboardHelper.h"
40 #endif
41
42 #include <WebCore/ContextMenu.h>
43 #include <WebCore/NotImplemented.h>
44 #include <wtf/text/CString.h>
45
46 #include <stdio.h>
47
48 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
49 #include <dlfcn.h>
50 #include <efl_assist.h>
51 extern void* EflAssistHandle;
52 #endif
53
54 using namespace WebCore;
55
56 namespace WebKit {
57
58 WebContextMenuProxyTizen::WebContextMenuProxyTizen(Evas_Object* webView, WebPageProxy* page, PageClientImpl* pageClientImpl)
59     : m_page(page)
60     , m_pageClientImpl(pageClientImpl)
61     , m_popupPosition()
62 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
63     , m_popup(0)
64     , m_webView(webView)
65     , m_items()
66 #endif
67 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
68     , m_contextShowTimer(0)
69 #endif
70 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
71     , m_positionForSelection()
72 #endif
73 {
74 }
75
76 WebContextMenuProxyTizen::~WebContextMenuProxyTizen()
77 {
78 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
79     if (m_popup)
80         evas_object_del(m_popup);
81 #endif
82 }
83
84 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
85 void WebContextMenuProxyTizen::contextMenuItemSelectedCallback(void* data, Evas_Object* obj, void* eventInfo)
86 {
87     WebContextMenuItemData itemData = *(static_cast<WebContextMenuItemData*>(data));
88     WebContextMenuProxyTizen* menuProxy = static_cast<WebContextMenuProxyTizen*>(evas_object_data_get(obj, "WebContextMenuProxyTizen"));
89
90 #if ENABLE(TIZEN_WEBKIT2_FOCUS_RING)
91     menuProxy->m_pageClientImpl->hideFocusRing();
92 #endif
93
94 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
95     menuProxy->m_pageClientImpl->initTextSelectionHandlesMouseDownedStatus();
96 #endif
97
98     menuProxy->m_page->contextMenuItemSelected(itemData);
99     menuProxy->hideContextMenu();
100
101     evas_object_del(menuProxy->m_popup);
102     menuProxy->m_popup = 0;
103 }
104
105 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
106 static void contextMenuHwBackKeyCallback(void* data, Evas_Object* obj, void* eventInfo)
107 {
108     elm_ctxpopup_dismiss(obj);
109 }
110 #endif
111
112 void WebContextMenuProxyTizen::createEflMenu(const Vector<WebContextMenuItemData>& items)
113 {
114     if (m_popup)
115         evas_object_del(m_popup);
116
117     Evas_Object* topWidget = elm_object_top_widget_get(elm_object_parent_widget_get(m_webView));
118     if (!topWidget)
119         topWidget = m_webView;
120
121     m_popup = elm_ctxpopup_add(topWidget);
122     if (!m_popup)
123         return;
124
125 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
126     if (EflAssistHandle) {
127         void (*webkit_ea_object_event_callback_add)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *);
128         webkit_ea_object_event_callback_add = (void (*)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *))dlsym(EflAssistHandle, "ea_object_event_callback_add");
129         (*webkit_ea_object_event_callback_add)(m_popup, EA_CALLBACK_BACK, contextMenuHwBackKeyCallback, 0);
130     }
131 #endif
132
133     m_items = items;
134     evas_object_data_set(m_popup, "WebContextMenuProxyTizen", this);
135     elm_object_tree_focus_allow_set(m_popup, false);
136
137     size_t size = m_items.size();
138 #if ENABLE(TIZEN_WEBKIT2_CLIPBOARD_HELPER)
139     int clipboardItemSize = ClipboardHelper::numberOfItems();
140     TIZEN_LOGI("clipboardItemSize : %d", clipboardItemSize);
141 #endif
142
143 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
144     m_isContextMenuForTextSelection = false;
145 #endif
146
147     for (size_t i = 0; i < size; i++) {
148 #if ENABLE(TIZEN_WEBKIT2_CLIPBOARD_HELPER)
149         if ((m_items.at(i).action() == ContextMenuItemTagPaste || m_items.at(i).action() == ContextMenuItemTagClipboard) && !clipboardItemSize)
150             continue;
151 #endif
152
153 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
154         if (m_items.at(i).action() == ContextMenuItemTagCopy || m_items.at(i).action() == ContextMenuItemTagSelectAll
155             || m_items.at(i).action() == ContextMenuItemTagSelectWord || m_items.at(i).action() == ContextMenuItemTagPaste)
156             m_isContextMenuForTextSelection = true;
157 #endif
158
159 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_ICON_TYPE_SUPPORT)
160         if (!m_items.at(i).title().isEmpty() || !m_items.at(i).iconFile().isEmpty()) {
161             Evas_Object* icon = 0;
162             if (!m_items.at(i).iconFile().isEmpty()) {
163                 icon = elm_icon_add(m_popup);
164                 TIZEN_LOGI("icon file path : %s", m_items.at(i).iconFile().utf8().data());
165                 if (!elm_image_file_set(icon, m_items.at(i).iconFile().utf8().data(), NULL)) {
166                     TIZEN_LOGE("elm_image_file_set is failed");
167                     icon = 0;
168                 }
169             }
170
171             if (!m_items.at(i).title().isEmpty())
172                 elm_ctxpopup_item_append(m_popup, m_items.at(i).title().utf8().data(), icon,
173                                          contextMenuItemSelectedCallback, &(m_items.at(i)));
174             else
175                 elm_ctxpopup_item_append(m_popup, 0, icon,
176                                          contextMenuItemSelectedCallback, &(m_items.at(i)));
177         }
178 #else
179         if (!m_items.at(i).title().isEmpty())
180             elm_ctxpopup_item_append(m_popup, m_items.at(i).title().utf8().data(), 0,
181                                      contextMenuItemSelectedCallback, &(m_items.at(i)));
182 #endif
183     }
184 }
185
186 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
187 void WebContextMenuProxyTizen::contextMenuPopupDismissedCallback(void* data, Evas_Object* obj, void* eventInfo)
188 {
189     WebContextMenuProxyTizen* menuProxy = static_cast<WebContextMenuProxyTizen*>(data);
190     if (menuProxy) {
191 #if ENABLE(TIZEN_WEBKIT2_FOCUS_RING)
192         menuProxy->m_pageClientImpl->hideFocusRing();
193 #endif
194         menuProxy->m_pageClientImpl->setIsContextMenuVisible(false);
195
196 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
197         if (menuProxy->m_contextShowTimer) {
198             ecore_timer_del(menuProxy->m_contextShowTimer);
199             menuProxy->m_contextShowTimer = 0;
200         }
201 #endif
202     }
203 }
204 #endif
205
206 #else
207 void WebContextMenuProxyTizen::createEflMenu()
208 {
209     notImplemented();
210 }
211 #endif
212
213 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
214 Eina_Bool WebContextMenuProxyTizen::contextMenuTimerCallback(void* data)
215 {
216     WebContextMenuProxyTizen *contextMenuProxy = static_cast<WebContextMenuProxyTizen*>(data);
217     if (contextMenuProxy) {
218         if (contextMenuProxy->m_popup)
219             evas_object_hide(contextMenuProxy->m_popup);
220
221         contextMenuProxy->m_pageClientImpl->setIsContextMenuVisible(false);
222 #if ENABLE(TIZEN_WEBKIT2_FOCUS_RING)
223         contextMenuProxy->m_pageClientImpl->hideFocusRing();
224 #endif
225     }
226      return ECORE_CALLBACK_CANCEL;
227 }
228
229 void WebContextMenuProxyTizen::startContextMenuTimer()
230 {
231     double contextMenuDispTime = 3.0;
232     if (m_contextShowTimer) {
233         ecore_timer_del(m_contextShowTimer);
234         m_contextShowTimer = 0;
235     }
236     m_contextShowTimer = ecore_timer_add(contextMenuDispTime, contextMenuTimerCallback, this);
237 }
238 #endif // TIZEN_WEBKIT2_TEXT_SELECTION
239
240 void WebContextMenuProxyTizen::showContextMenu(const WebCore::IntPoint& position, const Vector<WebContextMenuItemData>& items)
241 {
242 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
243     m_positionForSelection = position;
244 #endif
245
246 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
247     if (items.isEmpty()) {
248 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
249         if (m_pageClientImpl->isTextSelectionMode())
250             m_pageClientImpl->setIsTextSelectionMode(false);
251 #endif
252         return;
253     }
254
255     createEflMenu(items);
256
257 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
258     if (m_isContextMenuForTextSelection && !evas_object_focus_get(m_webView))
259         return;
260 #endif
261
262     if (m_popup) {
263         int webViewX, webViewY;
264         evas_object_geometry_get(m_webView, &webViewX, &webViewY, 0, 0);
265         IntPoint popupPosition = position;
266 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
267         if (m_pageClientImpl) {
268             popupPosition.scale(m_pageClientImpl->scaleFactor(), m_pageClientImpl->scaleFactor());
269             IntPoint scrollPosition = m_pageClientImpl->scrollPosition();
270             popupPosition.move(-scrollPosition.x(), -scrollPosition.y());
271         }
272 #endif
273         popupPosition.setX(popupPosition.x() + webViewX);
274         popupPosition.setY(popupPosition.y() + webViewY);
275
276 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
277         if (m_isContextMenuForTextSelection) {
278             if (!m_pageClientImpl->isTextSelectionMode())
279                 m_pageClientImpl->setIsTextSelectionMode(true);
280             elm_object_style_set(m_popup,"copypaste");
281             elm_ctxpopup_horizontal_set(m_popup, EINA_TRUE);
282             elm_ctxpopup_direction_priority_set(m_popup, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP);
283         }
284 #endif
285
286         evas_object_move(m_popup, popupPosition.x(), popupPosition.y());
287         evas_object_show(m_popup);
288
289         evas_object_smart_callback_add(m_popup, "dismissed", contextMenuPopupDismissedCallback, this);
290
291
292         m_pageClientImpl->setIsContextMenuVisible(true);
293 #if ENABLE(TOUCH_EVENTS) && ENABLE(TIZEN_GESTURE)
294         // Cancel touch event when ContextMenu is shown.
295         EwkViewImpl::fromEvasObject(m_webView)->feedTouchEventsByType(EWK_TOUCH_CANCEL);
296 #endif
297
298 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
299         // Start the timer at the end
300         if (m_isContextMenuForTextSelection)
301             startContextMenuTimer();
302 #endif
303     }
304 #else
305     createEflMenu();
306     notImplemented();
307 #endif
308 }
309
310 void WebContextMenuProxyTizen::hideContextMenu()
311 {
312 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
313     if (m_popup)
314         evas_object_hide(m_popup);
315     m_pageClientImpl->setIsContextMenuVisible(false);
316 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
317     if (m_contextShowTimer) {
318         ecore_timer_del(m_contextShowTimer);
319         m_contextShowTimer = 0;
320     }
321 #endif
322 #else
323     notImplemented();
324 #endif
325 }
326
327 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
328 WebCore::IntPoint& WebContextMenuProxyTizen::positionForSelection()
329 {
330     return m_positionForSelection;
331 }
332 #endif
333
334 } // namespace WebKit