731716f9384e8735bd07710f6705c90d668184a0
[WebKit-https.git] / Source / WebCore / platform / ContextMenuItem.h
1 /*
2  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2010 Igalia S.L
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
26
27 #ifndef ContextMenuItem_h
28 #define ContextMenuItem_h
29
30 #if ENABLE(CONTEXT_MENUS)
31
32 #include "PlatformMenuDescription.h"
33 #include <wtf/OwnPtr.h>
34 #include <wtf/text/WTFString.h>
35
36 #if PLATFORM(MAC)
37 #include <wtf/RetainPtr.h>
38 OBJC_CLASS NSMenuItem;
39 #elif PLATFORM(GTK)
40 typedef struct _GtkMenuItem GtkMenuItem;
41 typedef struct _GtkAction GtkAction;
42 #elif PLATFORM(WX)
43 class wxMenuItem;
44 #endif
45 #endif // ENABLE(CONTEXT_MENUS)
46
47 namespace WebCore {
48
49     class ContextMenu;
50
51     // This enum needs to be in sync with the WebMenuItemTag enum in WebUIDelegate.h and the
52     // extra values in WebUIDelegatePrivate.h
53     enum ContextMenuAction {
54         ContextMenuItemTagNoAction=0, // This item is not actually in WebUIDelegate.h
55         ContextMenuItemTagOpenLinkInNewWindow=1,
56         ContextMenuItemTagDownloadLinkToDisk,
57         ContextMenuItemTagCopyLinkToClipboard,
58         ContextMenuItemTagOpenImageInNewWindow,
59         ContextMenuItemTagDownloadImageToDisk,
60         ContextMenuItemTagCopyImageToClipboard,
61 #if PLATFORM(QT) || PLATFORM(GTK) || PLATFORM(EFL)
62         ContextMenuItemTagCopyImageUrlToClipboard,
63 #endif
64         ContextMenuItemTagOpenFrameInNewWindow,
65         ContextMenuItemTagCopy,
66         ContextMenuItemTagGoBack,
67         ContextMenuItemTagGoForward,
68         ContextMenuItemTagStop,
69         ContextMenuItemTagReload,
70         ContextMenuItemTagCut,
71         ContextMenuItemTagPaste,
72 #if PLATFORM(GTK)
73         ContextMenuItemTagDelete,
74 #endif
75 #if PLATFORM(GTK) || PLATFORM(QT) || PLATFORM (EFL)
76         ContextMenuItemTagSelectAll,
77 #endif
78 #if PLATFORM(GTK)
79         ContextMenuItemTagInputMethods,
80         ContextMenuItemTagUnicode,
81         ContextMenuItemTagUnicodeInsertLRMMark,
82         ContextMenuItemTagUnicodeInsertRLMMark,
83         ContextMenuItemTagUnicodeInsertLREMark,
84         ContextMenuItemTagUnicodeInsertRLEMark,
85         ContextMenuItemTagUnicodeInsertLROMark,
86         ContextMenuItemTagUnicodeInsertRLOMark,
87         ContextMenuItemTagUnicodeInsertPDFMark,
88         ContextMenuItemTagUnicodeInsertZWSMark,
89         ContextMenuItemTagUnicodeInsertZWJMark,
90         ContextMenuItemTagUnicodeInsertZWNJMark,
91 #endif
92         ContextMenuItemTagSpellingGuess,
93         ContextMenuItemTagNoGuessesFound,
94         ContextMenuItemTagIgnoreSpelling,
95         ContextMenuItemTagLearnSpelling,
96         ContextMenuItemTagOther,
97         ContextMenuItemTagSearchInSpotlight,
98         ContextMenuItemTagSearchWeb,
99         ContextMenuItemTagLookUpInDictionary,
100         ContextMenuItemTagOpenWithDefaultApplication,
101         ContextMenuItemPDFActualSize,
102         ContextMenuItemPDFZoomIn,
103         ContextMenuItemPDFZoomOut,
104         ContextMenuItemPDFAutoSize,
105         ContextMenuItemPDFSinglePage,
106         ContextMenuItemPDFFacingPages,
107         ContextMenuItemPDFContinuous,
108         ContextMenuItemPDFNextPage,
109         ContextMenuItemPDFPreviousPage,
110         // These are new tags! Not a part of API!!!!
111         ContextMenuItemTagOpenLink = 2000,
112         ContextMenuItemTagIgnoreGrammar,
113         ContextMenuItemTagSpellingMenu, // Spelling or Spelling/Grammar sub-menu
114         ContextMenuItemTagShowSpellingPanel,
115         ContextMenuItemTagCheckSpelling,
116         ContextMenuItemTagCheckSpellingWhileTyping,
117         ContextMenuItemTagCheckGrammarWithSpelling,
118         ContextMenuItemTagFontMenu, // Font sub-menu
119         ContextMenuItemTagShowFonts,
120         ContextMenuItemTagBold,
121         ContextMenuItemTagItalic,
122         ContextMenuItemTagUnderline,
123         ContextMenuItemTagOutline,
124         ContextMenuItemTagStyles,
125         ContextMenuItemTagShowColors,
126         ContextMenuItemTagSpeechMenu, // Speech sub-menu
127         ContextMenuItemTagStartSpeaking,
128         ContextMenuItemTagStopSpeaking,
129         ContextMenuItemTagWritingDirectionMenu, // Writing Direction sub-menu
130         ContextMenuItemTagDefaultDirection,
131         ContextMenuItemTagLeftToRight,
132         ContextMenuItemTagRightToLeft,
133         ContextMenuItemTagPDFSinglePageScrolling,
134         ContextMenuItemTagPDFFacingPagesScrolling,
135 #if ENABLE(INSPECTOR)
136         ContextMenuItemTagInspectElement,
137 #endif
138         ContextMenuItemTagTextDirectionMenu, // Text Direction sub-menu
139         ContextMenuItemTagTextDirectionDefault,
140         ContextMenuItemTagTextDirectionLeftToRight,
141         ContextMenuItemTagTextDirectionRightToLeft,
142 #if PLATFORM(MAC)
143         ContextMenuItemTagCorrectSpellingAutomatically,
144         ContextMenuItemTagSubstitutionsMenu,
145         ContextMenuItemTagShowSubstitutions,
146         ContextMenuItemTagSmartCopyPaste,
147         ContextMenuItemTagSmartQuotes,
148         ContextMenuItemTagSmartDashes,
149         ContextMenuItemTagSmartLinks,
150         ContextMenuItemTagTextReplacement,
151         ContextMenuItemTagTransformationsMenu,
152         ContextMenuItemTagMakeUpperCase,
153         ContextMenuItemTagMakeLowerCase,
154         ContextMenuItemTagCapitalize,
155         ContextMenuItemTagChangeBack,
156 #endif
157         ContextMenuItemTagOpenMediaInNewWindow,
158         ContextMenuItemTagCopyMediaLinkToClipboard,
159         ContextMenuItemTagToggleMediaControls,
160         ContextMenuItemTagToggleMediaLoop,
161         ContextMenuItemTagEnterVideoFullscreen,
162         ContextMenuItemTagMediaPlayPause,
163         ContextMenuItemTagMediaMute,
164         ContextMenuItemTagDictationAlternative,
165         ContextMenuItemTagOpenLinkInThisWindow,
166         ContextMenuItemBaseCustomTag = 5000,
167         ContextMenuItemCustomTagNoAction = 5998,
168         ContextMenuItemLastCustomTag = 5999,
169         ContextMenuItemBaseApplicationTag = 10000
170     };
171
172     enum ContextMenuItemType {
173         ActionType,
174         CheckableActionType,
175         SeparatorType,
176         SubmenuType
177     };
178
179 #if ENABLE(CONTEXT_MENUS)
180 #if PLATFORM(MAC)
181     typedef NSMenuItem* PlatformMenuItemDescription;
182 #elif PLATFORM(GTK)
183     typedef GtkMenuItem* PlatformMenuItemDescription;
184 #elif PLATFORM(WX)
185     struct PlatformMenuItemDescription {
186         PlatformMenuItemDescription()
187             : type(ActionType),
188               action(ContextMenuItemTagNoAction),
189               checked(false),
190               enabled(true)
191         {}
192
193         ContextMenuItemType type;
194         ContextMenuAction action;
195         String title;
196         wxMenu * subMenu;
197         bool checked;
198         bool enabled;
199     };
200 #elif PLATFORM(CHROMIUM)
201     struct PlatformMenuItemDescription {
202         PlatformMenuItemDescription()
203             : type(ActionType)
204             , action(ContextMenuItemTagNoAction)
205             , checked(false)
206             , enabled(true) { }
207         ContextMenuItemType type;
208         ContextMenuAction action;
209         String title;
210         Vector<ContextMenuItem> subMenuItems;
211         bool checked;
212         bool enabled;
213     };
214 #else
215     typedef void* PlatformMenuItemDescription;
216 #endif
217
218     class ContextMenuItem {
219         WTF_MAKE_FAST_ALLOCATED;
220     public:
221         ContextMenuItem(ContextMenuItemType, ContextMenuAction, const String&, ContextMenu* subMenu = 0);
222         ContextMenuItem(ContextMenuItemType, ContextMenuAction, const String&, bool enabled, bool checked);
223
224         ~ContextMenuItem();
225
226         void setType(ContextMenuItemType);
227         ContextMenuItemType type() const;
228
229         void setAction(ContextMenuAction);
230         ContextMenuAction action() const;
231
232         void setChecked(bool = true);
233         bool checked() const;
234
235         void setEnabled(bool = true);
236         bool enabled() const;
237
238         void setSubMenu(ContextMenu*);
239
240 #if PLATFORM(GTK)
241         GtkAction* gtkAction() const;
242 #endif
243
244 #if USE(CROSS_PLATFORM_CONTEXT_MENUS)
245         ContextMenuItem(ContextMenuAction, const String&, bool enabled, bool checked, const Vector<ContextMenuItem>& subMenuItems);
246         explicit ContextMenuItem(const PlatformContextMenuItem&);
247
248         // On Windows, the title (dwTypeData of the MENUITEMINFO) is not set in this function. Callers can set the title themselves,
249         // and handle the lifetime of the title, if they need it.
250         PlatformContextMenuItem platformContextMenuItem() const;
251
252         void setTitle(const String& title) { m_title = title; }
253         const String& title() const { return m_title; }
254
255         const Vector<ContextMenuItem>& subMenuItems() const { return m_subMenuItems; }
256 #else
257     public:
258         explicit ContextMenuItem(PlatformMenuItemDescription);
259         explicit ContextMenuItem(ContextMenu* subMenu = 0);
260         ContextMenuItem(ContextMenuAction, const String&, bool enabled, bool checked, Vector<ContextMenuItem>& submenuItems);
261
262         PlatformMenuItemDescription releasePlatformDescription();
263
264         String title() const;
265         void setTitle(const String&);
266
267         PlatformMenuDescription platformSubMenu() const;
268         void setSubMenu(Vector<ContextMenuItem>&);
269
270 #endif // USE(CROSS_PLATFORM_CONTEXT_MENUS)
271     private:
272 #if USE(CROSS_PLATFORM_CONTEXT_MENUS)
273         ContextMenuItemType m_type;
274         ContextMenuAction m_action;
275         String m_title;
276         bool m_enabled;
277         bool m_checked;
278         Vector<ContextMenuItem> m_subMenuItems;
279 #else
280 #if PLATFORM(MAC)
281         RetainPtr<NSMenuItem> m_platformDescription;
282 #else
283         PlatformMenuItemDescription m_platformDescription;
284 #endif
285 #endif // USE(CROSS_PLATFORM_CONTEXT_MENUS)
286     };
287
288 #endif // ENABLE(CONTEXT_MENUS)
289 }
290
291 #endif // ContextMenuItem_h