e8cdffd04f1b225acc7ea53489646bbaf14adef7
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / gtk / WebKitContextMenuActions.cpp
1 /*
2  * Copyright (C) 2012 Igalia S.L.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "WebKitContextMenuActions.h"
22
23 #include "WebKitContextMenuActionsPrivate.h"
24 #include <WebCore/LocalizedStrings.h>
25
26 using namespace WebCore;
27
28 bool webkitContextMenuActionIsCheckable(WebKitContextMenuAction action)
29 {
30     switch (action) {
31     case WEBKIT_CONTEXT_MENU_ACTION_BOLD:
32     case WEBKIT_CONTEXT_MENU_ACTION_ITALIC:
33     case WEBKIT_CONTEXT_MENU_ACTION_UNDERLINE:
34     case WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_CONTROLS:
35     case WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_LOOP:
36         return true;
37     default:
38         return false;
39     }
40 }
41
42 ContextMenuAction webkitContextMenuActionGetActionTag(WebKitContextMenuAction action)
43 {
44     switch (action) {
45     case WEBKIT_CONTEXT_MENU_ACTION_NO_ACTION:
46         return ContextMenuItemTagNoAction;
47     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK:
48         return ContextMenuItemTagOpenLink;
49     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK_IN_NEW_WINDOW:
50         return ContextMenuItemTagOpenLinkInNewWindow;
51     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_LINK_TO_DISK:
52         return ContextMenuItemTagDownloadLinkToDisk;
53     case WEBKIT_CONTEXT_MENU_ACTION_COPY_LINK_TO_CLIPBOARD:
54         return ContextMenuItemTagCopyLinkToClipboard;
55     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_IMAGE_IN_NEW_WINDOW:
56         return ContextMenuItemTagOpenImageInNewWindow;
57     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_IMAGE_TO_DISK:
58         return ContextMenuItemTagDownloadImageToDisk;
59     case WEBKIT_CONTEXT_MENU_ACTION_COPY_IMAGE_TO_CLIPBOARD:
60         return ContextMenuItemTagCopyImageToClipboard;
61     case WEBKIT_CONTEXT_MENU_ACTION_COPY_IMAGE_URL_TO_CLIPBOARD:
62         return ContextMenuItemTagCopyImageUrlToClipboard;
63     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_FRAME_IN_NEW_WINDOW:
64         return ContextMenuItemTagOpenFrameInNewWindow;
65     case WEBKIT_CONTEXT_MENU_ACTION_GO_BACK:
66         return ContextMenuItemTagGoBack;
67     case WEBKIT_CONTEXT_MENU_ACTION_GO_FORWARD:
68         return ContextMenuItemTagGoForward;
69     case WEBKIT_CONTEXT_MENU_ACTION_STOP:
70         return ContextMenuItemTagStop;
71     case WEBKIT_CONTEXT_MENU_ACTION_RELOAD:
72         return ContextMenuItemTagReload;
73     case WEBKIT_CONTEXT_MENU_ACTION_COPY:
74         return ContextMenuItemTagCopy;
75     case WEBKIT_CONTEXT_MENU_ACTION_CUT:
76         return ContextMenuItemTagCut;
77     case WEBKIT_CONTEXT_MENU_ACTION_PASTE:
78         return ContextMenuItemTagPaste;
79     case WEBKIT_CONTEXT_MENU_ACTION_DELETE:
80         return ContextMenuItemTagDelete;
81     case WEBKIT_CONTEXT_MENU_ACTION_SELECT_ALL:
82         return ContextMenuItemTagSelectAll;
83     case WEBKIT_CONTEXT_MENU_ACTION_INPUT_METHODS:
84         return ContextMenuItemTagInputMethods;
85     case WEBKIT_CONTEXT_MENU_ACTION_UNICODE:
86         return ContextMenuItemTagUnicode;
87     case WEBKIT_CONTEXT_MENU_ACTION_SPELLING_GUESS:
88         return ContextMenuItemTagSpellingGuess;
89     case WEBKIT_CONTEXT_MENU_ACTION_NO_GUESSES_FOUND:
90         return ContextMenuItemTagNoGuessesFound;
91     case WEBKIT_CONTEXT_MENU_ACTION_IGNORE_SPELLING:
92         return ContextMenuItemTagIgnoreSpelling;
93     case WEBKIT_CONTEXT_MENU_ACTION_LEARN_SPELLING:
94         return ContextMenuItemTagLearnSpelling;
95     case WEBKIT_CONTEXT_MENU_ACTION_IGNORE_GRAMMAR:
96         return ContextMenuItemTagIgnoreGrammar;
97     case WEBKIT_CONTEXT_MENU_ACTION_FONT_MENU:
98         return ContextMenuItemTagFontMenu;
99     case WEBKIT_CONTEXT_MENU_ACTION_BOLD:
100         return ContextMenuItemTagBold;
101     case WEBKIT_CONTEXT_MENU_ACTION_ITALIC:
102         return ContextMenuItemTagItalic;
103     case WEBKIT_CONTEXT_MENU_ACTION_UNDERLINE:
104         return ContextMenuItemTagUnderline;
105     case WEBKIT_CONTEXT_MENU_ACTION_OUTLINE:
106         return ContextMenuItemTagOutline;
107     case WEBKIT_CONTEXT_MENU_ACTION_INSPECT_ELEMENT:
108         return ContextMenuItemTagInspectElement;
109     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_VIDEO_IN_NEW_WINDOW:
110     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_AUDIO_IN_NEW_WINDOW:
111         return ContextMenuItemTagOpenMediaInNewWindow;
112     case WEBKIT_CONTEXT_MENU_ACTION_COPY_VIDEO_LINK_TO_CLIPBOARD:
113     case WEBKIT_CONTEXT_MENU_ACTION_COPY_AUDIO_LINK_TO_CLIPBOARD:
114         return ContextMenuItemTagCopyMediaLinkToClipboard;
115     case WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_CONTROLS:
116         return ContextMenuItemTagToggleMediaControls;
117     case WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_LOOP:
118         return ContextMenuItemTagToggleMediaLoop;
119     case WEBKIT_CONTEXT_MENU_ACTION_ENTER_VIDEO_FULLSCREEN:
120         return ContextMenuItemTagEnterVideoFullscreen;
121     case WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PLAY:
122     case WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PAUSE:
123         return ContextMenuItemTagMediaPlayPause;
124     case WEBKIT_CONTEXT_MENU_ACTION_MEDIA_MUTE:
125         return ContextMenuItemTagMediaMute;
126     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_VIDEO_TO_DISK:
127     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_AUDIO_TO_DISK:
128         return ContextMenuItemTagDownloadMediaToDisk;
129     case WEBKIT_CONTEXT_MENU_ACTION_CUSTOM:
130         return ContextMenuItemBaseApplicationTag;
131     default:
132         ASSERT_NOT_REACHED();
133     }
134
135     return ContextMenuItemBaseApplicationTag;
136 }
137
138 WebKitContextMenuAction webkitContextMenuActionGetForContextMenuItem(ContextMenuItem* menuItem)
139 {
140     switch (menuItem->action()) {
141     case ContextMenuItemTagNoAction:
142         return WEBKIT_CONTEXT_MENU_ACTION_NO_ACTION;
143     case ContextMenuItemTagOpenLink:
144         return WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK;
145     case ContextMenuItemTagOpenLinkInNewWindow:
146         return WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK_IN_NEW_WINDOW;
147     case ContextMenuItemTagDownloadLinkToDisk:
148         return WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_LINK_TO_DISK;
149     case ContextMenuItemTagCopyLinkToClipboard:
150         return WEBKIT_CONTEXT_MENU_ACTION_COPY_LINK_TO_CLIPBOARD;
151     case ContextMenuItemTagOpenImageInNewWindow:
152         return WEBKIT_CONTEXT_MENU_ACTION_OPEN_IMAGE_IN_NEW_WINDOW;
153     case ContextMenuItemTagDownloadImageToDisk:
154         return WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_IMAGE_TO_DISK;
155     case ContextMenuItemTagCopyImageToClipboard:
156         return WEBKIT_CONTEXT_MENU_ACTION_COPY_IMAGE_TO_CLIPBOARD;
157     case ContextMenuItemTagCopyImageUrlToClipboard:
158         return WEBKIT_CONTEXT_MENU_ACTION_COPY_IMAGE_URL_TO_CLIPBOARD;
159     case ContextMenuItemTagOpenFrameInNewWindow:
160         return WEBKIT_CONTEXT_MENU_ACTION_OPEN_FRAME_IN_NEW_WINDOW;
161     case ContextMenuItemTagGoBack:
162         return WEBKIT_CONTEXT_MENU_ACTION_GO_BACK;
163     case ContextMenuItemTagGoForward:
164         return WEBKIT_CONTEXT_MENU_ACTION_GO_FORWARD;
165     case ContextMenuItemTagStop:
166         return WEBKIT_CONTEXT_MENU_ACTION_STOP;
167     case ContextMenuItemTagReload:
168         return WEBKIT_CONTEXT_MENU_ACTION_RELOAD;
169     case ContextMenuItemTagCopy:
170         return WEBKIT_CONTEXT_MENU_ACTION_COPY;
171     case ContextMenuItemTagCut:
172         return WEBKIT_CONTEXT_MENU_ACTION_CUT;
173     case ContextMenuItemTagPaste:
174         return WEBKIT_CONTEXT_MENU_ACTION_PASTE;
175     case ContextMenuItemTagDelete:
176         return WEBKIT_CONTEXT_MENU_ACTION_DELETE;
177     case ContextMenuItemTagSelectAll:
178         return WEBKIT_CONTEXT_MENU_ACTION_SELECT_ALL;
179     case ContextMenuItemTagInputMethods:
180         return WEBKIT_CONTEXT_MENU_ACTION_INPUT_METHODS;
181     case ContextMenuItemTagUnicode:
182         return WEBKIT_CONTEXT_MENU_ACTION_UNICODE;
183     case ContextMenuItemTagSpellingGuess:
184         return WEBKIT_CONTEXT_MENU_ACTION_SPELLING_GUESS;
185     case ContextMenuItemTagIgnoreSpelling:
186         return WEBKIT_CONTEXT_MENU_ACTION_IGNORE_SPELLING;
187     case ContextMenuItemTagLearnSpelling:
188         return WEBKIT_CONTEXT_MENU_ACTION_LEARN_SPELLING;
189     case ContextMenuItemTagIgnoreGrammar:
190         return WEBKIT_CONTEXT_MENU_ACTION_IGNORE_GRAMMAR;
191     case ContextMenuItemTagFontMenu:
192         return WEBKIT_CONTEXT_MENU_ACTION_FONT_MENU;
193     case ContextMenuItemTagBold:
194         return WEBKIT_CONTEXT_MENU_ACTION_BOLD;
195     case ContextMenuItemTagItalic:
196         return WEBKIT_CONTEXT_MENU_ACTION_ITALIC;
197     case ContextMenuItemTagUnderline:
198         return WEBKIT_CONTEXT_MENU_ACTION_UNDERLINE;
199     case ContextMenuItemTagOutline:
200         return WEBKIT_CONTEXT_MENU_ACTION_OUTLINE;
201     case ContextMenuItemTagInspectElement:
202         return WEBKIT_CONTEXT_MENU_ACTION_INSPECT_ELEMENT;
203     case ContextMenuItemTagOpenMediaInNewWindow:
204         return menuItem->title() == contextMenuItemTagOpenVideoInNewWindow() ?
205             WEBKIT_CONTEXT_MENU_ACTION_OPEN_VIDEO_IN_NEW_WINDOW : WEBKIT_CONTEXT_MENU_ACTION_OPEN_AUDIO_IN_NEW_WINDOW;
206     case ContextMenuItemTagCopyMediaLinkToClipboard:
207         return menuItem->title() == contextMenuItemTagCopyVideoLinkToClipboard() ?
208             WEBKIT_CONTEXT_MENU_ACTION_COPY_VIDEO_LINK_TO_CLIPBOARD : WEBKIT_CONTEXT_MENU_ACTION_COPY_AUDIO_LINK_TO_CLIPBOARD;
209     case ContextMenuItemTagToggleMediaControls:
210         return WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_CONTROLS;
211     case ContextMenuItemTagToggleMediaLoop:
212         return WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_LOOP;
213     case ContextMenuItemTagEnterVideoFullscreen:
214         return WEBKIT_CONTEXT_MENU_ACTION_ENTER_VIDEO_FULLSCREEN;
215     case ContextMenuItemTagMediaPlayPause:
216         return menuItem->title() == contextMenuItemTagMediaPlay() ?
217             WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PLAY : WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PAUSE;
218     case ContextMenuItemTagMediaMute:
219         return WEBKIT_CONTEXT_MENU_ACTION_MEDIA_MUTE;
220     case ContextMenuItemTagDownloadMediaToDisk:
221         return menuItem->title() == contextMenuItemTagDownloadVideoToDisk() ?
222             WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_VIDEO_TO_DISK : WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_AUDIO_TO_DISK;
223     case ContextMenuItemBaseApplicationTag:
224         return WEBKIT_CONTEXT_MENU_ACTION_CUSTOM;
225     default:
226         ASSERT_NOT_REACHED();
227     }
228
229     return WEBKIT_CONTEXT_MENU_ACTION_CUSTOM;
230 }
231
232 String webkitContextMenuActionGetLabel(WebKitContextMenuAction action)
233 {
234     switch (action) {
235     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK:
236         return contextMenuItemTagOpenLink();
237     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK_IN_NEW_WINDOW:
238         return contextMenuItemTagOpenLinkInNewWindow();
239     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_LINK_TO_DISK:
240         return contextMenuItemTagDownloadLinkToDisk();
241     case WEBKIT_CONTEXT_MENU_ACTION_COPY_LINK_TO_CLIPBOARD:
242         return contextMenuItemTagCopyLinkToClipboard();
243     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_IMAGE_IN_NEW_WINDOW:
244         return contextMenuItemTagOpenImageInNewWindow();
245     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_IMAGE_TO_DISK:
246         return contextMenuItemTagDownloadImageToDisk();
247     case WEBKIT_CONTEXT_MENU_ACTION_COPY_IMAGE_TO_CLIPBOARD:
248         return contextMenuItemTagCopyImageToClipboard();
249     case WEBKIT_CONTEXT_MENU_ACTION_COPY_IMAGE_URL_TO_CLIPBOARD:
250         return contextMenuItemTagCopyImageUrlToClipboard();
251     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_FRAME_IN_NEW_WINDOW:
252         return contextMenuItemTagOpenFrameInNewWindow();
253     case WEBKIT_CONTEXT_MENU_ACTION_GO_BACK:
254         return contextMenuItemTagGoBack();
255     case WEBKIT_CONTEXT_MENU_ACTION_GO_FORWARD:
256         return contextMenuItemTagGoForward();
257     case WEBKIT_CONTEXT_MENU_ACTION_STOP:
258         return contextMenuItemTagStop();
259     case WEBKIT_CONTEXT_MENU_ACTION_RELOAD:
260         return contextMenuItemTagReload();
261     case WEBKIT_CONTEXT_MENU_ACTION_COPY:
262         return contextMenuItemTagCopy();
263     case WEBKIT_CONTEXT_MENU_ACTION_CUT:
264         return contextMenuItemTagCut();
265     case WEBKIT_CONTEXT_MENU_ACTION_PASTE:
266         return contextMenuItemTagPaste();
267     case WEBKIT_CONTEXT_MENU_ACTION_DELETE:
268         return contextMenuItemTagDelete();
269     case WEBKIT_CONTEXT_MENU_ACTION_SELECT_ALL:
270         return contextMenuItemTagSelectAll();
271     case WEBKIT_CONTEXT_MENU_ACTION_INPUT_METHODS:
272         return contextMenuItemTagInputMethods();
273     case WEBKIT_CONTEXT_MENU_ACTION_UNICODE:
274         return contextMenuItemTagUnicode();
275     case WEBKIT_CONTEXT_MENU_ACTION_NO_GUESSES_FOUND:
276         return contextMenuItemTagNoGuessesFound();
277     case WEBKIT_CONTEXT_MENU_ACTION_IGNORE_SPELLING:
278         return contextMenuItemTagIgnoreSpelling();
279     case WEBKIT_CONTEXT_MENU_ACTION_LEARN_SPELLING:
280         return contextMenuItemTagLearnSpelling();
281     case WEBKIT_CONTEXT_MENU_ACTION_IGNORE_GRAMMAR:
282         return contextMenuItemTagIgnoreGrammar();
283     case WEBKIT_CONTEXT_MENU_ACTION_FONT_MENU:
284         return contextMenuItemTagFontMenu();
285     case WEBKIT_CONTEXT_MENU_ACTION_BOLD:
286         return contextMenuItemTagBold();
287     case WEBKIT_CONTEXT_MENU_ACTION_ITALIC:
288         return contextMenuItemTagItalic();
289     case WEBKIT_CONTEXT_MENU_ACTION_UNDERLINE:
290         return contextMenuItemTagUnderline();
291     case WEBKIT_CONTEXT_MENU_ACTION_OUTLINE:
292         return contextMenuItemTagOutline();
293     case WEBKIT_CONTEXT_MENU_ACTION_INSPECT_ELEMENT:
294         return contextMenuItemTagInspectElement();
295     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_VIDEO_IN_NEW_WINDOW:
296         return contextMenuItemTagOpenVideoInNewWindow();
297     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_AUDIO_IN_NEW_WINDOW:
298         return contextMenuItemTagOpenAudioInNewWindow();
299     case WEBKIT_CONTEXT_MENU_ACTION_COPY_VIDEO_LINK_TO_CLIPBOARD:
300         return contextMenuItemTagCopyVideoLinkToClipboard();
301     case WEBKIT_CONTEXT_MENU_ACTION_COPY_AUDIO_LINK_TO_CLIPBOARD:
302         return contextMenuItemTagCopyAudioLinkToClipboard();
303     case WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_CONTROLS:
304         return contextMenuItemTagToggleMediaControls();
305     case WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_LOOP:
306         return contextMenuItemTagToggleMediaLoop();
307     case WEBKIT_CONTEXT_MENU_ACTION_ENTER_VIDEO_FULLSCREEN:
308         return contextMenuItemTagEnterVideoFullscreen();
309     case WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PLAY:
310         return contextMenuItemTagMediaPlay();
311     case WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PAUSE:
312         return contextMenuItemTagMediaPause();
313     case WEBKIT_CONTEXT_MENU_ACTION_MEDIA_MUTE:
314         return contextMenuItemTagMediaMute();
315     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_VIDEO_TO_DISK:
316         return contextMenuItemTagDownloadVideoToDisk();
317     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_AUDIO_TO_DISK:
318         return contextMenuItemTagDownloadAudioToDisk();
319     case WEBKIT_CONTEXT_MENU_ACTION_NO_ACTION:
320     case WEBKIT_CONTEXT_MENU_ACTION_CUSTOM:
321     case WEBKIT_CONTEXT_MENU_ACTION_SPELLING_GUESS:
322         return String();
323     default:
324         ASSERT_NOT_REACHED();
325     }
326
327     return String();
328 }