Tizen 2.1 base
[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     menuProxy->m_page->contextMenuItemSelected(itemData);
82     menuProxy->hideContextMenu();
83
84     evas_object_del(menuProxy->m_popup);
85     menuProxy->m_popup = 0;
86 }
87
88 void WebContextMenuProxyTizen::createEflMenu(const Vector<WebContextMenuItemData>& items)
89 {
90     if (m_popup)
91         evas_object_del(m_popup);
92
93     Evas_Object* topWidget = elm_object_top_widget_get(elm_object_parent_widget_get(m_webView));
94     if (!topWidget)
95         topWidget = m_webView;
96
97     m_popup = elm_ctxpopup_add(topWidget);
98     if (!m_popup)
99         return;
100
101     m_items = items;
102     evas_object_data_set(m_popup, "WebContextMenuProxyTizen", this);
103     elm_object_tree_focus_allow_set(m_popup, false);
104
105     size_t size = m_items.size();
106     for (size_t i = 0; i < size; i++) {
107 #if ENABLE(TIZEN_WEBKIT2_CLIPBOARD_HELPER)
108         if (m_items.at(i).action() == ContextMenuItemTagPaste && !ClipboardHelper::numberOfItems())
109             continue;
110 #endif
111
112 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_ICON_TYPE_SUPPORT)
113         if (!m_items.at(i).title().isEmpty() || !m_items.at(i).iconFile().isEmpty()) {
114             Evas_Object* icon = 0;
115             if (!m_items.at(i).iconFile().isEmpty()) {
116                 icon = elm_icon_add(m_popup);
117                 if (!elm_icon_file_set(icon, m_items.at(i).iconFile().utf8().data(), NULL))
118                     icon = 0;
119             }
120
121             if (!m_items.at(i).title().isEmpty())
122                 elm_ctxpopup_item_append(m_popup, m_items.at(i).title().utf8().data(), icon,
123                                          contextMenuItemSelectedCallback, &(m_items.at(i)));
124             else
125                 elm_ctxpopup_item_append(m_popup, 0, icon,
126                                          contextMenuItemSelectedCallback, &(m_items.at(i)));
127         }
128 #else
129         if (!m_items.at(i).title().isEmpty())
130             elm_ctxpopup_item_append(m_popup, m_items.at(i).title().utf8().data(), 0,
131                                      contextMenuItemSelectedCallback, &(m_items.at(i)));
132 #endif
133     }
134 }
135
136 static void contextMenuPopupDismissedCallback(void* data, Evas_Object* obj, void* eventInfo)
137 {
138     static_cast<PageClientImpl*>(data)->setIsContextMenuVisible(false);
139 }
140 #else
141 void WebContextMenuProxyTizen::createEflMenu()
142 {
143     notImplemented();
144 }
145 #endif
146
147 void WebContextMenuProxyTizen::showContextMenu(const WebCore::IntPoint& position, const Vector<WebContextMenuItemData>& items)
148 {
149 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
150     m_positionForSelection = position;
151 #endif
152
153 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
154     if (items.isEmpty())
155         return;
156
157     createEflMenu(items);
158
159     if (m_popup) {
160         int webViewX, webViewY;
161         evas_object_geometry_get(m_webView, &webViewX, &webViewY, 0, 0);
162         IntPoint popupPosition = position;
163 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
164         if (m_pageClientImpl) {
165             popupPosition.scale(m_pageClientImpl->scaleFactor(), m_pageClientImpl->scaleFactor());
166             IntPoint scrollPosition = m_pageClientImpl->scrollPosition();
167             popupPosition.move(-scrollPosition.x(), -scrollPosition.y());
168         }
169 #endif
170
171 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
172         if ( m_pageClientImpl->isTextSelectionMode()) {
173             elm_ctxpopup_horizontal_set(m_popup, EINA_TRUE);
174             elm_ctxpopup_direction_priority_set(m_popup, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP, ELM_CTXPOPUP_DIRECTION_UP);
175         }
176 #endif
177
178         evas_object_move(m_popup, popupPosition.x() + webViewX, popupPosition.y() + webViewY);
179         evas_object_show(m_popup);
180
181         evas_object_smart_callback_add(m_popup, "dismissed", contextMenuPopupDismissedCallback, m_pageClientImpl);
182
183         m_pageClientImpl->setIsContextMenuVisible(true);
184     }
185 #else
186     createEflMenu();
187     notImplemented();
188 #endif
189 }
190
191 void WebContextMenuProxyTizen::hideContextMenu()
192 {
193 #if ENABLE(TIZEN_CONTEXT_MENU_WEBKIT_2)
194     if (m_popup)
195         evas_object_hide(m_popup);
196     m_pageClientImpl->setIsContextMenuVisible(false);
197 #else
198     notImplemented();
199 #endif
200 }
201
202 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_TEXT_SELECTION_MODE)
203 WebCore::IntPoint& WebContextMenuProxyTizen::positionForSelection()
204 {
205     return m_positionForSelection;
206 }
207 #endif
208
209 } // namespace WebKit