[EFL][WK2] Add an API for getting context menu item's parent menu
[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)), this).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         if (EwkContextMenuItem* item = static_cast<EwkContextMenuItem*>(data)) {
65             item->setParentMenu(this);
66             m_contextMenuItems = eina_list_append(m_contextMenuItems, item);
67         }
68     }
69 }
70
71 EwkContextMenu::~EwkContextMenu()
72 {
73     void* data;
74     EINA_LIST_FREE(m_contextMenuItems, data)
75         delete static_cast<Ewk_Context_Menu_Item*>(data);
76 }
77
78 void EwkContextMenu::hide()
79 {
80     m_viewImpl->hideContextMenu();
81 }
82
83 void Ewk_Context_Menu::appendItem(EwkContextMenuItem* item)
84 {
85     m_contextMenuItems = eina_list_append(m_contextMenuItems, item);
86 }
87
88 void Ewk_Context_Menu::removeItem(EwkContextMenuItem* item)
89 {
90     m_contextMenuItems = eina_list_remove(m_contextMenuItems, item);
91 }
92
93 void EwkContextMenu::contextMenuItemSelected(WKContextMenuItemRef item)
94 {
95     WKPageSelectContextMenuItem(m_viewImpl->wkPage(), item);
96
97
98 Ewk_Context_Menu* ewk_context_menu_new()
99 {
100     return EwkContextMenu::create().leakPtr();
101 }
102
103 Ewk_Context_Menu* ewk_context_menu_new_with_items(Eina_List* items)
104 {
105     return EwkContextMenu::create(items).leakPtr();
106 }
107
108 Eina_Bool ewk_context_menu_item_append(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item* item)
109 {
110     EINA_SAFETY_ON_NULL_RETURN_VAL(menu, false);
111
112     menu->appendItem(item);
113
114     return true;
115 }
116
117 Eina_Bool ewk_context_menu_item_remove(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item* item)
118 {
119     EINA_SAFETY_ON_NULL_RETURN_VAL(menu, false);
120
121     menu->removeItem(item);
122
123     return true;
124 }
125
126 Eina_Bool ewk_context_menu_hide(Ewk_Context_Menu* menu)
127 {
128     EINA_SAFETY_ON_NULL_RETURN_VAL(menu, false);
129
130     menu->hide();
131
132     return true;
133 }
134
135 const Eina_List* ewk_context_menu_items_get(const Ewk_Context_Menu* menu)
136 {
137     EINA_SAFETY_ON_NULL_RETURN_VAL(menu, 0);
138
139     return menu->items();
140 }
141
142 Eina_Bool ewk_context_menu_item_select(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item* item)
143 {
144     EINA_SAFETY_ON_NULL_RETURN_VAL(menu, false);
145     EINA_SAFETY_ON_NULL_RETURN_VAL(item, false);
146
147     WKContextMenuItemRef wkItem;
148
149     switch (item->type()) {
150     case EWK_ACTION_TYPE:
151         wkItem = WKContextMenuItemCreateAsAction(getWKTagFromEwkAction(item->action()), adoptWK(WKStringCreateWithUTF8CString(item->title())).get(), item->enabled());
152         break;
153     case EWK_CHECKABLE_ACTION_TYPE:
154         wkItem = WKContextMenuItemCreateAsCheckableAction(getWKTagFromEwkAction(item->action()), adoptWK(WKStringCreateWithUTF8CString(item->title())).get(), item->enabled(), item->checked());
155         break;
156     default:
157         ASSERT_NOT_REACHED();
158         return false;
159     }
160
161     menu->contextMenuItemSelected(wkItem);
162
163     return true;
164 }
165
166 static WKContextMenuItemTag getWKTagFromEwkAction(Ewk_Context_Menu_Item_Action action)
167 {
168     switch (action) {
169     case EWK_CONTEXT_MENU_ITEM_TAG_NO_ACTION:
170         return kWKContextMenuItemTagNoAction;
171     case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_LINK_IN_NEW_WINDOW:
172         return kWKContextMenuItemTagOpenLinkInNewWindow;
173     case EWK_CONTEXT_MENU_ITEM_TAG_DOWNLOAD_LINK_TO_DISK:
174         return kWKContextMenuItemTagDownloadLinkToDisk;
175     case EWK_CONTEXT_MENU_ITEM_TAG_COPY_LINK_TO_CLIPBOARD:
176         return kWKContextMenuItemTagCopyLinkToClipboard;
177     case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_IMAGE_IN_NEW_WINDOW:
178         return kWKContextMenuItemTagOpenImageInNewWindow;
179     case EWK_CONTEXT_MENU_ITEM_TAG_DOWNLOAD_IMAGE_TO_DISK:
180         return kWKContextMenuItemTagDownloadImageToDisk;
181     case EWK_CONTEXT_MENU_ITEM_TAG_COPY_IMAGE_TO_CLIPBOARD:
182         return kWKContextMenuItemTagCopyImageToClipboard;
183     case EWK_CONTEXT_MENU_ITEM_TAG_COPY_IMAGE_URL_TO_CLIPBOARD:
184         return kWKContextMenuItemTagCopyImageUrlToClipboard;
185     case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_FRAME_IN_NEW_WINDOW:
186         return kWKContextMenuItemTagOpenFrameInNewWindow;
187     case EWK_CONTEXT_MENU_ITEM_TAG_COPY:
188         return kWKContextMenuItemTagCopy;
189     case EWK_CONTEXT_MENU_ITEM_TAG_GO_BACK:
190         return kWKContextMenuItemTagGoBack;
191     case EWK_CONTEXT_MENU_ITEM_TAG_GO_FORWARD:
192         return kWKContextMenuItemTagGoForward;
193     case EWK_CONTEXT_MENU_ITEM_TAG_STOP:
194         return kWKContextMenuItemTagStop;
195     case EWK_CONTEXT_MENU_ITEM_TAG_RELOAD:
196         return kWKContextMenuItemTagReload;
197     case EWK_CONTEXT_MENU_ITEM_TAG_CUT:
198         return kWKContextMenuItemTagCut;
199     case EWK_CONTEXT_MENU_ITEM_TAG_PASTE:
200         return kWKContextMenuItemTagPaste;
201     case EWK_CONTEXT_MENU_ITEM_TAG_SELECT_ALL:
202         return kWKContextMenuItemTagSelectAll;
203     case EWK_CONTEXT_MENU_ITEM_TAG_SPELLING_GUESS:
204         return kWKContextMenuItemTagSpellingGuess;
205     case EWK_CONTEXT_MENU_ITEM_TAG_NO_GUESSES_FOUND:
206         return kWKContextMenuItemTagNoGuessesFound;
207     case EWK_CONTEXT_MENU_ITEM_TAG_IGNORE_SPELLING:
208         return kWKContextMenuItemTagIgnoreSpelling;
209     case EWK_CONTEXT_MENU_ITEM_TAG_LEARN_SPELLING:
210         return kWKContextMenuItemTagLearnSpelling;
211     case EWK_CONTEXT_MENU_ITEM_TAG_OTHER:
212         return kWKContextMenuItemTagOther;
213     case EWK_CONTEXT_MENU_ITEM_TAG_SEARCH_IN_SPOTLIGHT:
214         return kWKContextMenuItemTagSearchInSpotlight;
215     case EWK_CONTEXT_MENU_ITEM_TAG_SEARCH_WEB:
216         return kWKContextMenuItemTagSearchWeb;
217     case EWK_CONTEXT_MENU_ITEM_TAG_LOOK_UP_IN_DICTIONARY:
218         return kWKContextMenuItemTagLookUpInDictionary;
219     case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_WITH_DEFAULT_APPLICATION:
220         return kWKContextMenuItemTagOpenWithDefaultApplication;
221     case EWK_CONTEXT_MENU_ITEM_PDFACTUAL_SIZE:
222         return kWKContextMenuItemTagPDFActualSize;
223     case EWK_CONTEXT_MENU_ITEM_PDFZOOM_IN:
224         return kWKContextMenuItemTagPDFZoomIn;
225     case EWK_CONTEXT_MENU_ITEM_PDFZOOM_OUT:
226         return kWKContextMenuItemTagPDFZoomOut;
227     case EWK_CONTEXT_MENU_ITEM_PDFAUTO_SIZE:
228         return kWKContextMenuItemTagPDFAutoSize;
229     case EWK_CONTEXT_MENU_ITEM_PDFSINGLE_PAGE:
230         return kWKContextMenuItemTagPDFSinglePage;
231     case EWK_CONTEXT_MENU_ITEM_PDFFACING_PAGES:
232         return kWKContextMenuItemTagPDFFacingPages;
233     case EWK_CONTEXT_MENU_ITEM_PDFCONTINUOUS:
234         return kWKContextMenuItemTagPDFContinuous;
235     case EWK_CONTEXT_MENU_ITEM_PDFNEXT_PAGE:
236         return kWKContextMenuItemTagPDFNextPage;
237     case EWK_CONTEXT_MENU_ITEM_PDFPREVIOUS_PAGE:
238         return kWKContextMenuItemTagPDFPreviousPage;
239     case EWK_CONTEXT_MENU_ITEM_TAG_OPEN_LINK:
240         return kWKContextMenuItemTagOpenLink;
241     case EWK_CONTEXT_MENU_ITEM_TAG_IGNORE_GRAMMAR:
242         return kWKContextMenuItemTagIgnoreGrammar;
243     case EWK_CONTEXT_MENU_ITEM_TAG_SPELLING_MENU:
244         return kWKContextMenuItemTagSpellingMenu;
245     case EWK_CONTEXT_MENU_ITEM_TAG_SHOW_SPELLING_PANEL:
246         return kWKContextMenuItemTagShowSpellingPanel;
247     case EWK_CONTEXT_MENU_ITEM_TAG_CHECK_SPELLING:
248         return kWKContextMenuItemTagCheckSpelling;
249     case EWK_CONTEXT_MENU_ITEM_TAG_CHECK_SPELLING_WHILE_TYPING:
250         return kWKContextMenuItemTagCheckSpellingWhileTyping;
251     case EWK_CONTEXT_MENU_ITEM_TAG_CHECK_GRAMMAR_WITH_SPELLING:
252         return kWKContextMenuItemTagCheckGrammarWithSpelling;
253     case EWK_CONTEXT_MENU_ITEM_TAG_FONT_MENU:
254         return kWKContextMenuItemTagFontMenu;
255     case EWK_CONTEXT_MENU_ITEM_TAG_SHOW_FONTS:
256         return kWKContextMenuItemTagShowFonts;
257     case EWK_CONTEXT_MENU_ITEM_TAG_BOLD:
258         return kWKContextMenuItemTagBold;
259     case EWK_CONTEXT_MENU_ITEM_TAG_ITALIC:
260         return kWKContextMenuItemTagItalic;
261     case EWK_CONTEXT_MENU_ITEM_TAG_UNDERLINE:
262         return kWKContextMenuItemTagUnderline;
263     case EWK_CONTEXT_MENU_ITEM_TAG_OUTLINE:
264         return kWKContextMenuItemTagOutline;
265     case EWK_CONTEXT_MENU_ITEM_TAG_STYLES:
266         return kWKContextMenuItemTagStyles;
267     case EWK_CONTEXT_MENU_ITEM_TAG_SHOW_COLORS:
268         return kWKContextMenuItemTagShowColors;
269     case EWK_CONTEXT_MENU_ITEM_TAG_SPEECH_MENU:
270         return kWKContextMenuItemTagSpeechMenu;
271     case EWK_CONTEXT_MENU_ITEM_TAG_START_SPEAKING:
272         return kWKContextMenuItemTagStartSpeaking;
273     case EWK_CONTEXT_MENU_ITEM_TAG_STOP_SPEAKING:
274         return kWKContextMenuItemTagStopSpeaking;
275     case EWK_CONTEXT_MENU_ITEM_TAG_WRITING_DIRECTION_MENU:
276         return kWKContextMenuItemTagWritingDirectionMenu;
277     case EWK_CONTEXT_MENU_ITEM_TAG_DEFAULT_DIRECTION:
278         return kWKContextMenuItemTagDefaultDirection;
279     case EWK_CONTEXT_MENU_ITEM_TAG_LEFT_TO_RIGHT:
280         return kWKContextMenuItemTagLeftToRight;
281     case EWK_CONTEXT_MENU_ITEM_TAG_RIGHT_TO_LEFT:
282         return kWKContextMenuItemTagRightToLeft;
283     case EWK_CONTEXT_MENU_ITEM_TAG_PDFSINGLE_PAGE_SCROLLING:
284         return kWKContextMenuItemTagPDFSinglePageScrolling;
285     case EWK_CONTEXT_MENU_ITEM_TAG_PDFFACING_PAGES_SCROLLING:
286         return kWKContextMenuItemTagPDFFacingPagesScrolling;
287     case EWK_CONTEXT_MENU_ITEM_TAG_INSPECT_ELEMENT:
288         return kWKContextMenuItemTagInspectElement;
289     case EWK_CONTEXT_MENU_ITEM_TAG_TEXT_DIRECTION_MENU:
290         return kWKContextMenuItemTagTextDirectionMenu;
291     case EWK_CONTEXT_MENU_ITEM_TAG_TEXT_DIRECTION_DEFAULT:
292         return kWKContextMenuItemTagTextDirectionDefault;
293     case EWK_CONTEXT_MENU_ITEM_TAG_TEXT_DIRECTION_LEFT_TO_RIGHT:
294         return kWKContextMenuItemTagTextDirectionLeftToRight;
295     case EWK_CONTEXT_MENU_ITEM_TAG_TEXT_DIRECTION_RIGHT_TO_LEFT:
296         return kWKContextMenuItemTagTextDirectionRightToLeft;
297     case EWK_CONTEXT_MENU_ITEM_OPEN_MEDIA_IN_NEW_WINDOW:
298         return kWKContextMenuItemTagOpenMediaInNewWindow;
299     case EWK_CONTEXT_MENU_ITEM_TAG_COPY_MEDIA_LINK_TO_CLIPBOARD:
300         return kWKContextMenuItemTagCopyMediaLinkToClipboard;
301     case EWK_CONTEXT_MENU_ITEM_TAG_TOGGLE_MEDIA_CONTROLS:
302         return kWKContextMenuItemTagToggleMediaControls;
303     case EWK_CONTEXT_MENU_ITEM_TAG_TOGGLE_MEDIA_LOOP:
304         return kWKContextMenuItemTagToggleMediaLoop;
305     case EWK_CONTEXT_MENU_ITEM_TAG_ENTER_VIDEO_FULLSCREEN:
306         return kWKContextMenuItemTagEnterVideoFullscreen;
307     case EWK_CONTEXT_MENU_ITEM_TAG_MEDIA_PLAY_PAUSE:
308         return kWKContextMenuItemTagMediaPlayPause;
309     case EWK_CONTEXT_MENU_ITEM_TAG_MEDIA_MUTE:
310         return kWKContextMenuItemTagMediaMute;
311     case EWK_CONTEXT_MENU_ITEM_BASE_APPLICATION_TAG:
312         return kWKContextMenuItemBaseApplicationTag;
313     default:
314         return static_cast<WKContextMenuItemTag>(action);
315     }
316 }