a54cbba88a2e07839e65b019274e2b31c6f9cbb4
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / efl / ewk_context_menu.cpp
1 /*
2  * Copyright (C) 2012 Samsung Electronics. All rights reserved.
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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" 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 THE COPYRIGHT HOLDER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ewk_context_menu.h"
28
29 #include "APIObject.h"
30 #include "EwkView.h"
31 #include "WKArray.h"
32 #include "WKContextMenuItem.h"
33 #include "WKString.h"
34 #include "ewk_context_menu_item.h"
35 #include "ewk_context_menu_item_private.h"
36 #include "ewk_context_menu_private.h"
37
38 using namespace WebKit;
39
40 static WKContextMenuItemTag getWKTagFromEwkAction(Ewk_Context_Menu_Item_Action action);
41
42 EwkContextMenu::EwkContextMenu(EwkView* view, WKArrayRef items)
43     : m_viewImpl(view)
44     , m_contextMenuItems(0)
45 {
46     size_t size = WKArrayGetSize(items);
47     for (size_t i = 0; i < size; ++i)
48         m_contextMenuItems = eina_list_append(m_contextMenuItems, Ewk_Context_Menu_Item::create(static_cast<WKContextMenuItemRef>(WKArrayGetItemAtIndex(items, i))).leakPtr());
49 }
50
51 EwkContextMenu::EwkContextMenu()
52     : m_viewImpl(0)
53     , m_contextMenuItems(0)
54 {
55 }
56
57 EwkContextMenu::EwkContextMenu(Eina_List* items)
58     : m_viewImpl(0)
59     , m_contextMenuItems(0)
60 {
61     Eina_List* l;
62     void* data;
63     EINA_LIST_FOREACH(items, l, data)
64         m_contextMenuItems = eina_list_append(m_contextMenuItems, static_cast<EwkContextMenuItem*>(data));
65 }
66
67 EwkContextMenu::~EwkContextMenu()
68 {
69     void* data;
70     EINA_LIST_FREE(m_contextMenuItems, data)
71         delete static_cast<Ewk_Context_Menu_Item*>(data);
72 }
73
74 void EwkContextMenu::hide()
75 {
76     m_viewImpl->hideContextMenu();
77 }
78
79 void Ewk_Context_Menu::appendItem(EwkContextMenuItem* item)
80 {
81     m_contextMenuItems = eina_list_append(m_contextMenuItems, item);
82 }
83
84 void Ewk_Context_Menu::removeItem(EwkContextMenuItem* item)
85 {
86     m_contextMenuItems = eina_list_remove(m_contextMenuItems, item);
87 }
88
89 void EwkContextMenu::contextMenuItemSelected(WKContextMenuItemRef item)
90 {
91     WKPageSelectContextMenuItem(m_viewImpl->wkPage(), item);
92
93
94 Ewk_Context_Menu* ewk_context_menu_new()
95 {
96     return EwkContextMenu::create().leakPtr();
97 }
98
99 Ewk_Context_Menu* ewk_context_menu_new_with_items(Eina_List* items)
100 {
101     return EwkContextMenu::create(items).leakPtr();
102 }
103
104 Eina_Bool ewk_context_menu_item_append(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item* item)
105 {
106     EINA_SAFETY_ON_NULL_RETURN_VAL(menu, false);
107
108     menu->appendItem(item);
109
110     return true;
111 }
112
113 Eina_Bool ewk_context_menu_item_remove(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item* item)
114 {
115     EINA_SAFETY_ON_NULL_RETURN_VAL(menu, false);
116
117     menu->removeItem(item);
118
119     return true;
120 }
121
122 Eina_Bool ewk_context_menu_hide(Ewk_Context_Menu* menu)
123 {
124     EINA_SAFETY_ON_NULL_RETURN_VAL(menu, false);
125
126     menu->hide();
127
128     return true;
129 }
130
131 const Eina_List* ewk_context_menu_items_get(const Ewk_Context_Menu* menu)
132 {
133     EINA_SAFETY_ON_NULL_RETURN_VAL(menu, 0);
134
135     return menu->items();
136 }
137
138 Eina_Bool ewk_context_menu_item_select(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item* item)
139 {
140     EINA_SAFETY_ON_NULL_RETURN_VAL(menu, false);
141     EINA_SAFETY_ON_NULL_RETURN_VAL(item, false);
142
143     WKContextMenuItemRef wkItem;
144
145     switch (item->type()) {
146     case EWK_ACTION_TYPE:
147         wkItem = WKContextMenuItemCreateAsAction(getWKTagFromEwkAction(item->action()), adoptWK(WKStringCreateWithUTF8CString(item->title())).get(), item->enabled());
148         break;
149     case EWK_CHECKABLE_ACTION_TYPE:
150         wkItem = WKContextMenuItemCreateAsCheckableAction(getWKTagFromEwkAction(item->action()), adoptWK(WKStringCreateWithUTF8CString(item->title())).get(), item->enabled(), item->checked());
151         break;
152     default:
153         ASSERT_NOT_REACHED();
154         return false;
155     }
156
157     menu->contextMenuItemSelected(wkItem);
158
159     return true;
160 }
161
162 static WKContextMenuItemTag getWKTagFromEwkAction(Ewk_Context_Menu_Item_Action action)
163 {
164     switch (action) {
165     case EWK_CONTEXT_MENU_ITEM_TAG_NO_ACTION:
166         return kWKContextMenuItemTagNoAction;
167     case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_LINK_IN_NEW_WINDOW:
168         return kWKContextMenuItemTagOpenLinkInNewWindow;
169     case EWK_CONTEXT_MENU_ITEM_TAG_DOWNLOAD_LINK_TO_DISK:
170         return kWKContextMenuItemTagDownloadLinkToDisk;
171     case EWK_CONTEXT_MENU_ITEM_TAG_COPY_LINK_TO_CLIPBOARD:
172         return kWKContextMenuItemTagCopyLinkToClipboard;
173     case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_IMAGE_IN_NEW_WINDOW:
174         return kWKContextMenuItemTagOpenImageInNewWindow;
175     case EWK_CONTEXT_MENU_ITEM_TAG_DOWNLOAD_IMAGE_TO_DISK:
176         return kWKContextMenuItemTagDownloadImageToDisk;
177     case EWK_CONTEXT_MENU_ITEM_TAG_COPY_IMAGE_TO_CLIPBOARD:
178         return kWKContextMenuItemTagCopyImageToClipboard;
179     case EWK_CONTEXT_MENU_ITEM_TAG_COPY_IMAGE_URL_TO_CLIPBOARD:
180         return kWKContextMenuItemTagCopyImageUrlToClipboard;
181     case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_FRAME_IN_NEW_WINDOW:
182         return kWKContextMenuItemTagOpenFrameInNewWindow;
183     case EWK_CONTEXT_MENU_ITEM_TAG_COPY:
184         return kWKContextMenuItemTagCopy;
185     case EWK_CONTEXT_MENU_ITEM_TAG_GO_BACK:
186         return kWKContextMenuItemTagGoBack;
187     case EWK_CONTEXT_MENU_ITEM_TAG_GO_FORWARD:
188         return kWKContextMenuItemTagGoForward;
189     case EWK_CONTEXT_MENU_ITEM_TAG_STOP:
190         return kWKContextMenuItemTagStop;
191     case EWK_CONTEXT_MENU_ITEM_TAG_RELOAD:
192         return kWKContextMenuItemTagReload;
193     case EWK_CONTEXT_MENU_ITEM_TAG_CUT:
194         return kWKContextMenuItemTagCut;
195     case EWK_CONTEXT_MENU_ITEM_TAG_PASTE:
196         return kWKContextMenuItemTagPaste;
197     case EWK_CONTEXT_MENU_ITEM_TAG_SELECT_ALL:
198         return kWKContextMenuItemTagSelectAll;
199     case EWK_CONTEXT_MENU_ITEM_TAG_SPELLING_GUESS:
200         return kWKContextMenuItemTagSpellingGuess;
201     case EWK_CONTEXT_MENU_ITEM_TAG_NO_GUESSES_FOUND:
202         return kWKContextMenuItemTagNoGuessesFound;
203     case EWK_CONTEXT_MENU_ITEM_TAG_IGNORE_SPELLING:
204         return kWKContextMenuItemTagIgnoreSpelling;
205     case EWK_CONTEXT_MENU_ITEM_TAG_LEARN_SPELLING:
206         return kWKContextMenuItemTagLearnSpelling;
207     case EWK_CONTEXT_MENU_ITEM_TAG_OTHER:
208         return kWKContextMenuItemTagOther;
209     case EWK_CONTEXT_MENU_ITEM_TAG_SEARCH_IN_SPOTLIGHT:
210         return kWKContextMenuItemTagSearchInSpotlight;
211     case EWK_CONTEXT_MENU_ITEM_TAG_SEARCH_WEB:
212         return kWKContextMenuItemTagSearchWeb;
213     case EWK_CONTEXT_MENU_ITEM_TAG_LOOK_UP_IN_DICTIONARY:
214         return kWKContextMenuItemTagLookUpInDictionary;
215     case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_WITH_DEFAULT_APPLICATION:
216         return kWKContextMenuItemTagOpenWithDefaultApplication;
217     case EWK_CONTEXT_MENU_ITEM_PDFACTUAL_SIZE:
218         return kWKContextMenuItemTagPDFActualSize;
219     case EWK_CONTEXT_MENU_ITEM_PDFZOOM_IN:
220         return kWKContextMenuItemTagPDFZoomIn;
221     case EWK_CONTEXT_MENU_ITEM_PDFZOOM_OUT:
222         return kWKContextMenuItemTagPDFZoomOut;
223     case EWK_CONTEXT_MENU_ITEM_PDFAUTO_SIZE:
224         return kWKContextMenuItemTagPDFAutoSize;
225     case EWK_CONTEXT_MENU_ITEM_PDFSINGLE_PAGE:
226         return kWKContextMenuItemTagPDFSinglePage;
227     case EWK_CONTEXT_MENU_ITEM_PDFFACING_PAGES:
228         return kWKContextMenuItemTagPDFFacingPages;
229     case EWK_CONTEXT_MENU_ITEM_PDFCONTINUOUS:
230         return kWKContextMenuItemTagPDFContinuous;
231     case EWK_CONTEXT_MENU_ITEM_PDFNEXT_PAGE:
232         return kWKContextMenuItemTagPDFNextPage;
233     case EWK_CONTEXT_MENU_ITEM_PDFPREVIOUS_PAGE:
234         return kWKContextMenuItemTagPDFPreviousPage;
235     case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_LINK:
236         return kWKContextMenuItemTagOpenLink;
237     case EWK_CONTEXT_MENU_ITEM_TAG_IGNORE_GRAMMAR:
238         return kWKContextMenuItemTagIgnoreGrammar;
239     case EWK_CONTEXT_MENU_ITEM_TAG_SPELLING_MENU:
240         return kWKContextMenuItemTagSpellingMenu;
241     case EWK_CONTEXT_MENU_ITEM_TAG_SHOW_SPELLING_PANEL:
242         return kWKContextMenuItemTagShowSpellingPanel;
243     case EWK_CONTEXT_MENU_ITEM_TAG_CHECK_SPELLING:
244         return kWKContextMenuItemTagCheckSpelling;
245     case EWK_CONTEXT_MENU_ITEM_TAG_CHECK_SPELLING_WHILE_TYPING:
246         return kWKContextMenuItemTagCheckSpellingWhileTyping;
247     case EWK_CONTEXT_MENU_ITEM_TAG_CHECK_GRAMMAR_WITH_SPELLING:
248         return kWKContextMenuItemTagCheckGrammarWithSpelling;
249     case EWK_CONTEXT_MENU_ITEM_TAG_FONT_MENU:
250         return kWKContextMenuItemTagFontMenu;
251     case EWK_CONTEXT_MENU_ITEM_TAG_SHOW_FONTS:
252         return kWKContextMenuItemTagShowFonts;
253     case EWK_CONTEXT_MENU_ITEM_TAG_BOLD:
254         return kWKContextMenuItemTagBold;
255     case EWK_CONTEXT_MENU_ITEM_TAG_ITALIC:
256         return kWKContextMenuItemTagItalic;
257     case EWK_CONTEXT_MENU_ITEM_TAG_UNDERLINE:
258         return kWKContextMenuItemTagUnderline;
259     case EWK_CONTEXT_MENU_ITEM_TAG_OUTLINE:
260         return kWKContextMenuItemTagOutline;
261     case EWK_CONTEXT_MENU_ITEM_TAG_STYLES:
262         return kWKContextMenuItemTagStyles;
263     case EWK_CONTEXT_MENU_ITEM_TAG_SHOW_COLORS:
264         return kWKContextMenuItemTagShowColors;
265     case EWK_CONTEXT_MENU_ITEM_TAG_SPEECH_MENU:
266         return kWKContextMenuItemTagSpeechMenu;
267     case EWK_CONTEXT_MENU_ITEM_TAG_START_SPEAKING:
268         return kWKContextMenuItemTagStartSpeaking;
269     case EWK_CONTEXT_MENU_ITEM_TAG_STOP_SPEAKING:
270         return kWKContextMenuItemTagStopSpeaking;
271     case EWK_CONTEXT_MENU_ITEM_TAG_WRITING_DIRECTION_MENU:
272         return kWKContextMenuItemTagWritingDirectionMenu;
273     case EWK_CONTEXT_MENU_ITEM_TAG_DEFAULT_DIRECTION:
274         return kWKContextMenuItemTagDefaultDirection;
275     case EWK_CONTEXT_MENU_ITEM_TAG_LEFT_TO_RIGHT:
276         return kWKContextMenuItemTagLeftToRight;
277     case EWK_CONTEXT_MENU_ITEM_TAG_RIGHT_TO_LEFT:
278         return kWKContextMenuItemTagRightToLeft;
279     case EWK_CONTEXT_MENU_ITEM_TAG_PDFSINGLE_PAGE_SCROLLING:
280         return kWKContextMenuItemTagPDFSinglePageScrolling;
281     case EWK_CONTEXT_MENU_ITEM_TAG_PDFFACING_PAGES_SCROLLING:
282         return kWKContextMenuItemTagPDFFacingPagesScrolling;
283     case EWK_CONTEXT_MENU_ITEM_TAG_INSPECT_ELEMENT:
284         return kWKContextMenuItemTagInspectElement;
285     case EWK_CONTEXT_MENU_ITEM_TAG_TEXT_DIRECTION_MENU:
286         return kWKContextMenuItemTagTextDirectionMenu;
287     case EWK_CONTEXT_MENU_ITEM_TAG_TEXT_DIRECTION_DEFAULT:
288         return kWKContextMenuItemTagTextDirectionDefault;
289     case EWK_CONTEXT_MENU_ITEM_TAG_TEXT_DIRECTION_LEFT_TO_RIGHT:
290         return kWKContextMenuItemTagTextDirectionLeftToRight;
291     case EWK_CONTEXT_MENU_ITEM_TAG_TEXT_DIRECTION_RIGHT_TO_LEFT:
292         return kWKContextMenuItemTagTextDirectionRightToLeft;
293     case EWK_CONTEXT_MENU_ITEM_OPEN_MEDIA_IN_NEW_WINDOW:
294         return kWKContextMenuItemTagOpenMediaInNewWindow;
295     case EWK_CONTEXT_MENU_ITEM_TAG_COPY_MEDIA_LINK_TO_CLIPBOARD:
296         return kWKContextMenuItemTagCopyMediaLinkToClipboard;
297     case EWK_CONTEXT_MENU_ITEM_TAG_TOGGLE_MEDIA_CONTROLS:
298         return kWKContextMenuItemTagToggleMediaControls;
299     case EWK_CONTEXT_MENU_ITEM_TAG_TOGGLE_MEDIA_LOOP:
300         return kWKContextMenuItemTagToggleMediaLoop;
301     case EWK_CONTEXT_MENU_ITEM_TAG_ENTER_VIDEO_FULLSCREEN:
302         return kWKContextMenuItemTagEnterVideoFullscreen;
303     case EWK_CONTEXT_MENU_ITEM_TAG_MEDIA_PLAY_PAUSE:
304         return kWKContextMenuItemTagMediaPlayPause;
305     case EWK_CONTEXT_MENU_ITEM_TAG_MEDIA_MUTE:
306         return kWKContextMenuItemTagMediaMute;
307     case EWK_CONTEXT_MENU_ITEM_BASE_APPLICATION_TAG:
308         return kWKContextMenuItemBaseApplicationTag;
309     default:
310         return static_cast<WKContextMenuItemTag>(action);
311     }
312 }