790e513455623c41f53eb109e74788bd76bf36fa
[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_CONTEXT_MENU_TEXT_SELECTION_MODE)
68     , m_positionForSelection()
69 #endif
70 {
71 }
72
73 WebContextMenuProxyTizen::~WebContextMenuProxyTizen()
74 {
75 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
76     if (m_popup)
77         evas_object_del(m_popup);
78 #endif
79 }
80
81 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
82 void WebContextMenuProxyTizen::contextMenuItemSelectedCallback(void* data, Evas_Object* obj, void* eventInfo)
83 {
84     WebContextMenuItemData itemData = *(static_cast<WebContextMenuItemData*>(data));
85     WebContextMenuProxyTizen* menuProxy = static_cast<WebContextMenuProxyTizen*>(evas_object_data_get(obj, "WebContextMenuProxyTizen"));
86
87 #if ENABLE(TIZEN_WEBKIT2_FOCUS_RING)
88     menuProxy->m_pageClientImpl->hideFocusRing();
89 #endif
90
91 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
92     menuProxy->m_pageClientImpl->initTextSelectionHandlesMouseDownedStatus();
93 #endif
94
95     menuProxy->m_page->contextMenuItemSelected(itemData);
96     menuProxy->hideContextMenu();
97
98     evas_object_del(menuProxy->m_popup);
99     menuProxy->m_popup = 0;
100 }
101
102 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
103 static void contextMenuHwBackKeyCallback(void* data, Evas_Object* obj, void* eventInfo)
104 {
105     elm_ctxpopup_dismiss(obj);
106 }
107 #endif
108
109 void WebContextMenuProxyTizen::createEflMenu(const Vector<WebContextMenuItemData>& items)
110 {
111     if (m_popup)
112         evas_object_del(m_popup);
113
114     Evas_Object* topWidget = elm_object_top_widget_get(elm_object_parent_widget_get(m_webView));
115     if (!topWidget)
116         topWidget = m_webView;
117
118     m_popup = elm_ctxpopup_add(topWidget);
119     if (!m_popup)
120         return;
121
122 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
123     if (EflAssistHandle) {
124         void (*webkit_ea_object_event_callback_add)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *);
125         webkit_ea_object_event_callback_add = (void (*)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *))dlsym(EflAssistHandle, "ea_object_event_callback_add");
126         (*webkit_ea_object_event_callback_add)(m_popup, EA_CALLBACK_BACK, contextMenuHwBackKeyCallback, 0);
127     }
128 #endif
129
130     m_items = items;
131     evas_object_data_set(m_popup, "WebContextMenuProxyTizen", this);
132     elm_object_tree_focus_allow_set(m_popup, false);
133
134     size_t size = m_items.size();
135 #if ENABLE(TIZEN_WEBKIT2_CLIPBOARD_HELPER)
136     int clipboardItemSize = ClipboardHelper::numberOfItems();
137     TIZEN_LOGI("clipboardItemSize : %d", clipboardItemSize);
138 #endif
139     bool isContextMenuForTextSelection = false;
140     for (size_t i = 0; i < size; i++) {
141 #if ENABLE(TIZEN_WEBKIT2_CLIPBOARD_HELPER)
142         if ((m_items.at(i).action() == ContextMenuItemTagPaste || m_items.at(i).action() == ContextMenuItemTagClipboard) && !clipboardItemSize)
143             continue;
144 #endif
145
146 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
147         if (m_items.at(i).action() == ContextMenuItemTagCopy || m_items.at(i).action() == ContextMenuItemTagSelectAll
148             || m_items.at(i).action() == ContextMenuItemTagSelectWord || m_items.at(i).action() == ContextMenuItemTagPaste)
149             isContextMenuForTextSelection = true;
150 #endif
151
152 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_ICON_TYPE_SUPPORT)
153         if (!m_items.at(i).title().isEmpty() || !m_items.at(i).iconFile().isEmpty()) {
154             Evas_Object* icon = 0;
155             if (!m_items.at(i).iconFile().isEmpty()) {
156                 icon = elm_icon_add(m_popup);
157                 TIZEN_LOGI("icon file path : %s", m_items.at(i).iconFile().utf8().data());
158                 if (!elm_image_file_set(icon, m_items.at(i).iconFile().utf8().data(), NULL)) {
159                     TIZEN_LOGE("elm_image_file_set is failed");
160                     icon = 0;
161                 }
162             }
163
164             if (!m_items.at(i).title().isEmpty())
165                 elm_ctxpopup_item_append(m_popup, m_items.at(i).title().utf8().data(), icon,
166                                          contextMenuItemSelectedCallback, &(m_items.at(i)));
167             else
168                 elm_ctxpopup_item_append(m_popup, 0, icon,
169                                          contextMenuItemSelectedCallback, &(m_items.at(i)));
170         }
171 #else
172         if (!m_items.at(i).title().isEmpty())
173             elm_ctxpopup_item_append(m_popup, m_items.at(i).title().utf8().data(), 0,
174                                      contextMenuItemSelectedCallback, &(m_items.at(i)));
175 #endif
176     }
177
178 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
179     if (isContextMenuForTextSelection) {
180         if (!m_pageClientImpl->isTextSelectionMode())
181             m_pageClientImpl->setIsTextSelectionMode(true);
182         elm_object_style_set(m_popup,"copypaste");
183         elm_ctxpopup_horizontal_set(m_popup, EINA_TRUE);
184         elm_ctxpopup_direction_priority_set(m_popup, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP);
185     }
186 #endif
187 }
188
189 static void contextMenuPopupDismissedCallback(void* data, Evas_Object* obj, void* eventInfo)
190 {
191 #if ENABLE(TIZEN_WEBKIT2_FOCUS_RING)
192     static_cast<PageClientImpl*>(data)->hideFocusRing();
193 #endif
194     static_cast<PageClientImpl*>(data)->setIsContextMenuVisible(false);
195 }
196 #else
197 void WebContextMenuProxyTizen::createEflMenu()
198 {
199     notImplemented();
200 }
201 #endif
202
203 void WebContextMenuProxyTizen::showContextMenu(const WebCore::IntPoint& position, const Vector<WebContextMenuItemData>& items)
204 {
205 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
206     m_positionForSelection = position;
207 #endif
208
209 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
210     if (items.isEmpty()) {
211 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
212         if (m_pageClientImpl->isTextSelectionMode())
213             m_pageClientImpl->setIsTextSelectionMode(false);
214 #endif
215         return;
216     }
217
218     createEflMenu(items);
219
220     if (m_popup) {
221         int webViewX, webViewY;
222         evas_object_geometry_get(m_webView, &webViewX, &webViewY, 0, 0);
223         IntPoint popupPosition = position;
224 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
225         if (m_pageClientImpl) {
226             popupPosition.scale(m_pageClientImpl->scaleFactor(), m_pageClientImpl->scaleFactor());
227             IntPoint scrollPosition = m_pageClientImpl->scrollPosition();
228             popupPosition.move(-scrollPosition.x(), -scrollPosition.y());
229         }
230 #endif
231         popupPosition.setX(popupPosition.x() + webViewX);
232         popupPosition.setY(popupPosition.y() + webViewY);
233
234         evas_object_move(m_popup, popupPosition.x(), popupPosition.y());
235         evas_object_show(m_popup);
236
237         evas_object_smart_callback_add(m_popup, "dismissed", contextMenuPopupDismissedCallback, m_pageClientImpl);
238
239         m_pageClientImpl->setIsContextMenuVisible(true);
240 #if ENABLE(TOUCH_EVENTS) && ENABLE(TIZEN_GESTURE)
241         // Cancel touch event when ContextMenu is shown.
242         EwkViewImpl::fromEvasObject(m_webView)->feedTouchEventsByType(EWK_TOUCH_CANCEL);
243 #endif
244     }
245 #else
246     createEflMenu();
247     notImplemented();
248 #endif
249 }
250
251 void WebContextMenuProxyTizen::hideContextMenu()
252 {
253 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
254     if (m_popup)
255         evas_object_hide(m_popup);
256     m_pageClientImpl->setIsContextMenuVisible(false);
257 #else
258     notImplemented();
259 #endif
260 }
261
262 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
263 WebCore::IntPoint& WebContextMenuProxyTizen::positionForSelection()
264 {
265     return m_positionForSelection;
266 }
267 #endif
268
269 } // namespace WebKit