[GTK] Crash when spell checker returns no guesses
[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 ContextMenuItemTagNoGuessesFound:
186         return WEBKIT_CONTEXT_MENU_ACTION_NO_GUESSES_FOUND;
187     case ContextMenuItemTagIgnoreSpelling:
188         return WEBKIT_CONTEXT_MENU_ACTION_IGNORE_SPELLING;
189     case ContextMenuItemTagLearnSpelling:
190         return WEBKIT_CONTEXT_MENU_ACTION_LEARN_SPELLING;
191     case ContextMenuItemTagIgnoreGrammar:
192         return WEBKIT_CONTEXT_MENU_ACTION_IGNORE_GRAMMAR;
193     case ContextMenuItemTagFontMenu:
194         return WEBKIT_CONTEXT_MENU_ACTION_FONT_MENU;
195     case ContextMenuItemTagBold:
196         return WEBKIT_CONTEXT_MENU_ACTION_BOLD;
197     case ContextMenuItemTagItalic:
198         return WEBKIT_CONTEXT_MENU_ACTION_ITALIC;
199     case ContextMenuItemTagUnderline:
200         return WEBKIT_CONTEXT_MENU_ACTION_UNDERLINE;
201     case ContextMenuItemTagOutline:
202         return WEBKIT_CONTEXT_MENU_ACTION_OUTLINE;
203     case ContextMenuItemTagInspectElement:
204         return WEBKIT_CONTEXT_MENU_ACTION_INSPECT_ELEMENT;
205     case ContextMenuItemTagOpenMediaInNewWindow:
206         return menuItem->title() == contextMenuItemTagOpenVideoInNewWindow() ?
207             WEBKIT_CONTEXT_MENU_ACTION_OPEN_VIDEO_IN_NEW_WINDOW : WEBKIT_CONTEXT_MENU_ACTION_OPEN_AUDIO_IN_NEW_WINDOW;
208     case ContextMenuItemTagCopyMediaLinkToClipboard:
209         return menuItem->title() == contextMenuItemTagCopyVideoLinkToClipboard() ?
210             WEBKIT_CONTEXT_MENU_ACTION_COPY_VIDEO_LINK_TO_CLIPBOARD : WEBKIT_CONTEXT_MENU_ACTION_COPY_AUDIO_LINK_TO_CLIPBOARD;
211     case ContextMenuItemTagToggleMediaControls:
212         return WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_CONTROLS;
213     case ContextMenuItemTagToggleMediaLoop:
214         return WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_LOOP;
215     case ContextMenuItemTagEnterVideoFullscreen:
216         return WEBKIT_CONTEXT_MENU_ACTION_ENTER_VIDEO_FULLSCREEN;
217     case ContextMenuItemTagMediaPlayPause:
218         return menuItem->title() == contextMenuItemTagMediaPlay() ?
219             WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PLAY : WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PAUSE;
220     case ContextMenuItemTagMediaMute:
221         return WEBKIT_CONTEXT_MENU_ACTION_MEDIA_MUTE;
222     case ContextMenuItemTagDownloadMediaToDisk:
223         return menuItem->title() == contextMenuItemTagDownloadVideoToDisk() ?
224             WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_VIDEO_TO_DISK : WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_AUDIO_TO_DISK;
225     case ContextMenuItemBaseApplicationTag:
226         return WEBKIT_CONTEXT_MENU_ACTION_CUSTOM;
227     default:
228         ASSERT_NOT_REACHED();
229     }
230
231     return WEBKIT_CONTEXT_MENU_ACTION_CUSTOM;
232 }
233
234 String webkitContextMenuActionGetLabel(WebKitContextMenuAction action)
235 {
236     switch (action) {
237     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK:
238         return contextMenuItemTagOpenLink();
239     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_LINK_IN_NEW_WINDOW:
240         return contextMenuItemTagOpenLinkInNewWindow();
241     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_LINK_TO_DISK:
242         return contextMenuItemTagDownloadLinkToDisk();
243     case WEBKIT_CONTEXT_MENU_ACTION_COPY_LINK_TO_CLIPBOARD:
244         return contextMenuItemTagCopyLinkToClipboard();
245     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_IMAGE_IN_NEW_WINDOW:
246         return contextMenuItemTagOpenImageInNewWindow();
247     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_IMAGE_TO_DISK:
248         return contextMenuItemTagDownloadImageToDisk();
249     case WEBKIT_CONTEXT_MENU_ACTION_COPY_IMAGE_TO_CLIPBOARD:
250         return contextMenuItemTagCopyImageToClipboard();
251     case WEBKIT_CONTEXT_MENU_ACTION_COPY_IMAGE_URL_TO_CLIPBOARD:
252         return contextMenuItemTagCopyImageUrlToClipboard();
253     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_FRAME_IN_NEW_WINDOW:
254         return contextMenuItemTagOpenFrameInNewWindow();
255     case WEBKIT_CONTEXT_MENU_ACTION_GO_BACK:
256         return contextMenuItemTagGoBack();
257     case WEBKIT_CONTEXT_MENU_ACTION_GO_FORWARD:
258         return contextMenuItemTagGoForward();
259     case WEBKIT_CONTEXT_MENU_ACTION_STOP:
260         return contextMenuItemTagStop();
261     case WEBKIT_CONTEXT_MENU_ACTION_RELOAD:
262         return contextMenuItemTagReload();
263     case WEBKIT_CONTEXT_MENU_ACTION_COPY:
264         return contextMenuItemTagCopy();
265     case WEBKIT_CONTEXT_MENU_ACTION_CUT:
266         return contextMenuItemTagCut();
267     case WEBKIT_CONTEXT_MENU_ACTION_PASTE:
268         return contextMenuItemTagPaste();
269     case WEBKIT_CONTEXT_MENU_ACTION_DELETE:
270         return contextMenuItemTagDelete();
271     case WEBKIT_CONTEXT_MENU_ACTION_SELECT_ALL:
272         return contextMenuItemTagSelectAll();
273     case WEBKIT_CONTEXT_MENU_ACTION_INPUT_METHODS:
274         return contextMenuItemTagInputMethods();
275     case WEBKIT_CONTEXT_MENU_ACTION_UNICODE:
276         return contextMenuItemTagUnicode();
277     case WEBKIT_CONTEXT_MENU_ACTION_NO_GUESSES_FOUND:
278         return contextMenuItemTagNoGuessesFound();
279     case WEBKIT_CONTEXT_MENU_ACTION_IGNORE_SPELLING:
280         return contextMenuItemTagIgnoreSpelling();
281     case WEBKIT_CONTEXT_MENU_ACTION_LEARN_SPELLING:
282         return contextMenuItemTagLearnSpelling();
283     case WEBKIT_CONTEXT_MENU_ACTION_IGNORE_GRAMMAR:
284         return contextMenuItemTagIgnoreGrammar();
285     case WEBKIT_CONTEXT_MENU_ACTION_FONT_MENU:
286         return contextMenuItemTagFontMenu();
287     case WEBKIT_CONTEXT_MENU_ACTION_BOLD:
288         return contextMenuItemTagBold();
289     case WEBKIT_CONTEXT_MENU_ACTION_ITALIC:
290         return contextMenuItemTagItalic();
291     case WEBKIT_CONTEXT_MENU_ACTION_UNDERLINE:
292         return contextMenuItemTagUnderline();
293     case WEBKIT_CONTEXT_MENU_ACTION_OUTLINE:
294         return contextMenuItemTagOutline();
295     case WEBKIT_CONTEXT_MENU_ACTION_INSPECT_ELEMENT:
296         return contextMenuItemTagInspectElement();
297     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_VIDEO_IN_NEW_WINDOW:
298         return contextMenuItemTagOpenVideoInNewWindow();
299     case WEBKIT_CONTEXT_MENU_ACTION_OPEN_AUDIO_IN_NEW_WINDOW:
300         return contextMenuItemTagOpenAudioInNewWindow();
301     case WEBKIT_CONTEXT_MENU_ACTION_COPY_VIDEO_LINK_TO_CLIPBOARD:
302         return contextMenuItemTagCopyVideoLinkToClipboard();
303     case WEBKIT_CONTEXT_MENU_ACTION_COPY_AUDIO_LINK_TO_CLIPBOARD:
304         return contextMenuItemTagCopyAudioLinkToClipboard();
305     case WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_CONTROLS:
306         return contextMenuItemTagToggleMediaControls();
307     case WEBKIT_CONTEXT_MENU_ACTION_TOGGLE_MEDIA_LOOP:
308         return contextMenuItemTagToggleMediaLoop();
309     case WEBKIT_CONTEXT_MENU_ACTION_ENTER_VIDEO_FULLSCREEN:
310         return contextMenuItemTagEnterVideoFullscreen();
311     case WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PLAY:
312         return contextMenuItemTagMediaPlay();
313     case WEBKIT_CONTEXT_MENU_ACTION_MEDIA_PAUSE:
314         return contextMenuItemTagMediaPause();
315     case WEBKIT_CONTEXT_MENU_ACTION_MEDIA_MUTE:
316         return contextMenuItemTagMediaMute();
317     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_VIDEO_TO_DISK:
318         return contextMenuItemTagDownloadVideoToDisk();
319     case WEBKIT_CONTEXT_MENU_ACTION_DOWNLOAD_AUDIO_TO_DISK:
320         return contextMenuItemTagDownloadAudioToDisk();
321     case WEBKIT_CONTEXT_MENU_ACTION_NO_ACTION:
322     case WEBKIT_CONTEXT_MENU_ACTION_CUSTOM:
323     case WEBKIT_CONTEXT_MENU_ACTION_SPELLING_GUESS:
324         return String();
325     default:
326         ASSERT_NOT_REACHED();
327     }
328
329     return String();
330 }