Merge "[CherryPick] [WEBGL] Rename WEBKIT_WEBGL_lose_context to WEBGL_lose_context...
[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 using namespace WebCore;
49
50 namespace WebKit {
51
52 WebContextMenuProxyTizen::WebContextMenuProxyTizen(Evas_Object* webView, WebPageProxy* page, PageClientImpl* pageClientImpl)
53     : m_page(page)
54     , m_pageClientImpl(pageClientImpl)
55     , m_popupPosition()
56 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
57     , m_popup(0)
58     , m_webView(webView)
59     , m_items()
60 #endif
61 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
62     , m_positionForSelection()
63 #endif
64 {
65 }
66
67 WebContextMenuProxyTizen::~WebContextMenuProxyTizen()
68 {
69 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
70     if (m_popup)
71         evas_object_del(m_popup);
72 #endif
73 }
74
75 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
76 void WebContextMenuProxyTizen::contextMenuItemSelectedCallback(void* data, Evas_Object* obj, void* eventInfo)
77 {
78     WebContextMenuItemData itemData = *(static_cast<WebContextMenuItemData*>(data));
79     WebContextMenuProxyTizen* menuProxy = static_cast<WebContextMenuProxyTizen*>(evas_object_data_get(obj, "WebContextMenuProxyTizen"));
80
81 #if ENABLE(TIZEN_WEBKIT2_FOCUS_RING)
82     menuProxy->m_pageClientImpl->hideFocusRing();
83 #endif
84
85 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
86     menuProxy->m_pageClientImpl->initTextSelectionHandlesMouseDownedStatus();
87 #endif
88
89     menuProxy->m_page->contextMenuItemSelected(itemData);
90     menuProxy->hideContextMenu();
91
92     evas_object_del(menuProxy->m_popup);
93     menuProxy->m_popup = 0;
94 }
95
96 void WebContextMenuProxyTizen::createEflMenu(const Vector<WebContextMenuItemData>& items)
97 {
98     if (m_popup)
99         evas_object_del(m_popup);
100
101     Evas_Object* topWidget = elm_object_top_widget_get(elm_object_parent_widget_get(m_webView));
102     if (!topWidget)
103         topWidget = m_webView;
104
105     m_popup = elm_ctxpopup_add(topWidget);
106     if (!m_popup)
107         return;
108
109     m_items = items;
110     evas_object_data_set(m_popup, "WebContextMenuProxyTizen", this);
111     elm_object_tree_focus_allow_set(m_popup, false);
112
113     size_t size = m_items.size();
114 #if ENABLE(TIZEN_WEBKIT2_CLIPBOARD_HELPER)
115     int clipboardItemSize = ClipboardHelper::numberOfItems();
116     TIZEN_LOGI("clipboardItemSize : %d", clipboardItemSize);
117 #endif
118     for (size_t i = 0; i < size; i++) {
119 #if ENABLE(TIZEN_WEBKIT2_CLIPBOARD_HELPER)
120         if ((m_items.at(i).action() == ContextMenuItemTagPaste || m_items.at(i).action() == ContextMenuItemTagClipboard) && !clipboardItemSize)
121             continue;
122 #endif
123
124 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_ICON_TYPE_SUPPORT)
125         if (!m_items.at(i).title().isEmpty() || !m_items.at(i).iconFile().isEmpty()) {
126             Evas_Object* icon = 0;
127             if (!m_items.at(i).iconFile().isEmpty()) {
128                 icon = elm_icon_add(m_popup);
129                 TIZEN_LOGI("icon file path : %s", m_items.at(i).iconFile().utf8().data());
130                 if (!elm_image_file_set(icon, m_items.at(i).iconFile().utf8().data(), NULL)) {
131                     TIZEN_LOGE("elm_image_file_set is failed");
132                     icon = 0;
133                 }
134             }
135
136             if (!m_items.at(i).title().isEmpty())
137                 elm_ctxpopup_item_append(m_popup, m_items.at(i).title().utf8().data(), icon,
138                                          contextMenuItemSelectedCallback, &(m_items.at(i)));
139             else
140                 elm_ctxpopup_item_append(m_popup, 0, icon,
141                                          contextMenuItemSelectedCallback, &(m_items.at(i)));
142         }
143 #else
144         if (!m_items.at(i).title().isEmpty())
145             elm_ctxpopup_item_append(m_popup, m_items.at(i).title().utf8().data(), 0,
146                                      contextMenuItemSelectedCallback, &(m_items.at(i)));
147 #endif
148     }
149 }
150
151 static void contextMenuPopupDismissedCallback(void* data, Evas_Object* obj, void* eventInfo)
152 {
153 #if ENABLE(TIZEN_WEBKIT2_FOCUS_RING)
154     static_cast<PageClientImpl*>(data)->hideFocusRing();
155 #endif
156     static_cast<PageClientImpl*>(data)->setIsContextMenuVisible(false);
157 }
158 #else
159 void WebContextMenuProxyTizen::createEflMenu()
160 {
161     notImplemented();
162 }
163 #endif
164
165 void WebContextMenuProxyTizen::showContextMenu(const WebCore::IntPoint& position, const Vector<WebContextMenuItemData>& items)
166 {
167 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
168     m_positionForSelection = position;
169 #endif
170
171 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
172     if (items.isEmpty())
173         return;
174
175     createEflMenu(items);
176
177     if (m_popup) {
178         int webViewX, webViewY;
179         evas_object_geometry_get(m_webView, &webViewX, &webViewY, 0, 0);
180         IntPoint popupPosition = position;
181 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
182         if (m_pageClientImpl) {
183             popupPosition.scale(m_pageClientImpl->scaleFactor(), m_pageClientImpl->scaleFactor());
184             IntPoint scrollPosition = m_pageClientImpl->scrollPosition();
185             popupPosition.move(-scrollPosition.x(), -scrollPosition.y());
186         }
187 #endif
188         popupPosition.setX(popupPosition.x() + webViewX);
189         popupPosition.setY(popupPosition.y() + webViewY);
190
191 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
192         if (m_pageClientImpl->isTextSelectionMode()) {
193             elm_object_style_set(m_popup,"copypaste");
194             elm_ctxpopup_horizontal_set(m_popup, EINA_TRUE);
195             elm_ctxpopup_direction_priority_set(m_popup, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP);
196
197             m_pageClientImpl->changeContextMenuPosition(popupPosition);
198         }
199 #endif
200
201         evas_object_move(m_popup, popupPosition.x(), popupPosition.y());
202         evas_object_show(m_popup);
203
204         evas_object_smart_callback_add(m_popup, "dismissed", contextMenuPopupDismissedCallback, m_pageClientImpl);
205
206         m_pageClientImpl->setIsContextMenuVisible(true);
207 #if ENABLE(TOUCH_EVENTS) && ENABLE(TIZEN_GESTURE)
208         // Cancel touch event when ContextMenu is shown.
209         EwkViewImpl::fromEvasObject(m_webView)->feedTouchEventsByType(EWK_TOUCH_CANCEL);
210 #endif
211     }
212 #else
213     createEflMenu();
214     notImplemented();
215 #endif
216 }
217
218 void WebContextMenuProxyTizen::hideContextMenu()
219 {
220 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
221     if (m_popup)
222         evas_object_hide(m_popup);
223     m_pageClientImpl->setIsContextMenuVisible(false);
224 #else
225     notImplemented();
226 #endif
227 }
228
229 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
230 WebCore::IntPoint& WebContextMenuProxyTizen::positionForSelection()
231 {
232     return m_positionForSelection;
233 }
234 #endif
235
236 } // namespace WebKit