Changed the postion of context menu to avaliable space
[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         EditorState editorState = contextMenuProxy->m_page->editorState();
223         if (!editorState.selectionIsRange && editorState.isContentEditable)
224             contextMenuProxy->m_pageClientImpl->setIsTextSelectionMode(false);
225 #if ENABLE(TIZEN_WEBKIT2_FOCUS_RING)
226         contextMenuProxy->m_pageClientImpl->hideFocusRing();
227 #endif
228     }
229      return ECORE_CALLBACK_CANCEL;
230 }
231
232 void WebContextMenuProxyTizen::startContextMenuTimer()
233 {
234     double contextMenuDispTime = 3.0;
235     if (m_contextShowTimer) {
236         ecore_timer_del(m_contextShowTimer);
237         m_contextShowTimer = 0;
238     }
239     m_contextShowTimer = ecore_timer_add(contextMenuDispTime, contextMenuTimerCallback, this);
240 }
241 #endif // TIZEN_WEBKIT2_TEXT_SELECTION
242
243 void WebContextMenuProxyTizen::showContextMenu(const WebCore::IntPoint& position, const Vector<WebContextMenuItemData>& items)
244 {
245 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
246     m_positionForSelection = position;
247 #endif
248
249 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
250     if (items.isEmpty()) {
251 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
252         if (m_pageClientImpl->isTextSelectionMode())
253             m_pageClientImpl->setIsTextSelectionMode(false);
254 #endif
255         return;
256     }
257
258     createEflMenu(items);
259
260 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
261     if (m_isContextMenuForTextSelection && !evas_object_focus_get(m_webView))
262         return;
263 #endif
264
265     if (m_popup) {
266         int webViewX, webViewY;
267         evas_object_geometry_get(m_webView, &webViewX, &webViewY, 0, 0);
268         IntPoint popupPosition = position;
269 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
270         if (m_pageClientImpl) {
271             popupPosition.scale(m_pageClientImpl->scaleFactor(), m_pageClientImpl->scaleFactor());
272             IntPoint scrollPosition = m_pageClientImpl->scrollPosition();
273             popupPosition.move(-scrollPosition.x(), -scrollPosition.y());
274         }
275 #endif
276         popupPosition.setX(popupPosition.x() + webViewX);
277         popupPosition.setY(popupPosition.y() + webViewY);
278
279 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
280         if (m_isContextMenuForTextSelection) {
281             if (!m_pageClientImpl->isTextSelectionMode())
282                 m_pageClientImpl->setIsTextSelectionMode(true);
283
284             elm_object_style_set(m_popup,"copypaste");
285             elm_ctxpopup_horizontal_set(m_popup, EINA_TRUE);
286             elm_ctxpopup_direction_priority_set(m_popup, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_LEFT, ELM_CTXPOPUP_DIRECTION_RIGHT, ELM_CTXPOPUP_DIRECTION_DOWN);
287
288             m_pageClientImpl->changeContextMenuPosition(popupPosition);
289         }
290 #endif
291
292         evas_object_move(m_popup, popupPosition.x(), popupPosition.y());
293         evas_object_show(m_popup);
294
295         evas_object_smart_callback_add(m_popup, "dismissed", contextMenuPopupDismissedCallback, this);
296
297
298         m_pageClientImpl->setIsContextMenuVisible(true);
299 #if ENABLE(TOUCH_EVENTS) && ENABLE(TIZEN_GESTURE)
300         // Cancel touch event when ContextMenu is shown.
301         EwkViewImpl::fromEvasObject(m_webView)->feedTouchEventsByType(EWK_TOUCH_CANCEL);
302 #endif
303
304 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
305         // Start the timer at the end
306         if (m_isContextMenuForTextSelection)
307             startContextMenuTimer();
308 #endif
309     }
310 #else
311     createEflMenu();
312     notImplemented();
313 #endif
314 }
315
316 void WebContextMenuProxyTizen::hideContextMenu()
317 {
318 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
319     if (m_popup)
320         evas_object_hide(m_popup);
321     m_pageClientImpl->setIsContextMenuVisible(false);
322 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
323     if (m_contextShowTimer) {
324         ecore_timer_del(m_contextShowTimer);
325         m_contextShowTimer = 0;
326     }
327 #endif
328 #else
329     notImplemented();
330 #endif
331 }
332
333 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
334 WebCore::IntPoint& WebContextMenuProxyTizen::positionForSelection()
335 {
336     return m_positionForSelection;
337 }
338 #endif
339
340 } // namespace WebKit