Expose the "Share" menu for links, images, and media.
[WebKit-https.git] / Source / WebCore / platform / ContextMenuItem.h
1 /*
2  * Copyright (C) 2006 Apple 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 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 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(COCOA)
37 #include <wtf/RetainPtr.h>
38 OBJC_CLASS NSMenuItem;
39 #elif PLATFORM(GTK)
40 typedef struct _GtkMenuItem GtkMenuItem;
41 typedef struct _GtkAction GtkAction;
42 #endif
43 #endif // ENABLE(CONTEXT_MENUS)
44
45 namespace WebCore {
46
47     class ContextMenu;
48     class Image;
49     class URL;
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(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 (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         ContextMenuItemTagInspectElement,
136         ContextMenuItemTagTextDirectionMenu, // Text Direction sub-menu
137         ContextMenuItemTagTextDirectionDefault,
138         ContextMenuItemTagTextDirectionLeftToRight,
139         ContextMenuItemTagTextDirectionRightToLeft,
140 #if PLATFORM(COCOA)
141         ContextMenuItemTagCorrectSpellingAutomatically,
142         ContextMenuItemTagSubstitutionsMenu,
143         ContextMenuItemTagShowSubstitutions,
144         ContextMenuItemTagSmartCopyPaste,
145         ContextMenuItemTagSmartQuotes,
146         ContextMenuItemTagSmartDashes,
147         ContextMenuItemTagSmartLinks,
148         ContextMenuItemTagTextReplacement,
149         ContextMenuItemTagTransformationsMenu,
150         ContextMenuItemTagMakeUpperCase,
151         ContextMenuItemTagMakeLowerCase,
152         ContextMenuItemTagCapitalize,
153         ContextMenuItemTagChangeBack,
154 #endif
155         ContextMenuItemTagOpenMediaInNewWindow,
156         ContextMenuItemTagDownloadMediaToDisk,
157         ContextMenuItemTagCopyMediaLinkToClipboard,
158         ContextMenuItemTagToggleMediaControls,
159         ContextMenuItemTagToggleMediaLoop,
160         ContextMenuItemTagEnterVideoFullscreen,
161         ContextMenuItemTagMediaPlayPause,
162         ContextMenuItemTagMediaMute,
163         ContextMenuItemTagDictationAlternative,
164         ContextMenuItemTagOpenLinkInThisWindow,
165         ContextMenuItemTagToggleVideoFullscreen,
166         ContextMenuItemTagShareMenu, 
167         ContextMenuItemBaseCustomTag = 5000,
168         ContextMenuItemCustomTagNoAction = 5998,
169         ContextMenuItemLastCustomTag = 5999,
170         ContextMenuItemBaseApplicationTag = 10000
171     };
172
173     enum ContextMenuItemType {
174         ActionType,
175         CheckableActionType,
176         SeparatorType,
177         SubmenuType
178     };
179
180 #if ENABLE(CONTEXT_MENUS)
181 #if PLATFORM(COCOA)
182     typedef NSMenuItem* PlatformMenuItemDescription;
183 #elif PLATFORM(GTK)
184     typedef GtkMenuItem* PlatformMenuItemDescription;
185 #else
186     typedef void* PlatformMenuItemDescription;
187 #endif
188
189     class ContextMenuItem {
190         WTF_MAKE_FAST_ALLOCATED;
191     public:
192         WEBCORE_EXPORT ContextMenuItem(ContextMenuItemType, ContextMenuAction, const String&, ContextMenu* subMenu = 0);
193         WEBCORE_EXPORT ContextMenuItem(ContextMenuItemType, ContextMenuAction, const String&, bool enabled, bool checked);
194
195         WEBCORE_EXPORT ~ContextMenuItem();
196
197         void setType(ContextMenuItemType);
198         WEBCORE_EXPORT ContextMenuItemType type() const;
199
200         void setAction(ContextMenuAction);
201         WEBCORE_EXPORT ContextMenuAction action() const;
202
203         void setChecked(bool = true);
204         WEBCORE_EXPORT bool checked() const;
205
206         void setEnabled(bool = true);
207         WEBCORE_EXPORT bool enabled() const;
208
209         void setSubMenu(ContextMenu*);
210
211         WEBCORE_EXPORT static ContextMenuItem shareMenuItem(const URL& absoluteLinkURL, const URL& downloadableMediaURL, Image*, const String& selectedText);
212
213 #if PLATFORM(GTK)
214         GtkAction* gtkAction() const;
215 #endif
216
217 #if USE(CROSS_PLATFORM_CONTEXT_MENUS)
218         ContextMenuItem(ContextMenuAction, const String&, bool enabled, bool checked, const Vector<ContextMenuItem>& subMenuItems);
219         explicit ContextMenuItem(const PlatformContextMenuItem&);
220         ContextMenuItem();
221
222         bool isNull() const;
223
224         // On Windows, the title (dwTypeData of the MENUITEMINFO) is not set in this function. Callers can set the title themselves,
225         // and handle the lifetime of the title, if they need it.
226         PlatformContextMenuItem platformContextMenuItem() const;
227
228         void setTitle(const String& title) { m_title = title; }
229         const String& title() const { return m_title; }
230
231         const Vector<ContextMenuItem>& subMenuItems() const { return m_subMenuItems; }
232 #else
233     public:
234         WEBCORE_EXPORT explicit ContextMenuItem(PlatformMenuItemDescription);
235         explicit ContextMenuItem(ContextMenu* subMenu);
236         ContextMenuItem(ContextMenuAction, const String&, bool enabled, bool checked, Vector<ContextMenuItem>& submenuItems);
237         WEBCORE_EXPORT ContextMenuItem();
238
239         bool isNull() const { return !m_platformDescription; }
240
241 #if PLATFORM(GTK)
242         WEBCORE_EXPORT PlatformMenuItemDescription releasePlatformDescription();
243 #endif
244
245         WEBCORE_EXPORT PlatformMenuItemDescription platformDescription() const;
246
247         WEBCORE_EXPORT String title() const;
248         void setTitle(const String&);
249
250         WEBCORE_EXPORT PlatformMenuDescription platformSubMenu() const;
251         void setSubMenu(Vector<ContextMenuItem>&);
252
253 #endif // USE(CROSS_PLATFORM_CONTEXT_MENUS)
254     private:
255 #if USE(CROSS_PLATFORM_CONTEXT_MENUS)
256         ContextMenuItemType m_type;
257         ContextMenuAction m_action;
258         String m_title;
259         bool m_enabled;
260         bool m_checked;
261         Vector<ContextMenuItem> m_subMenuItems;
262 #else
263 #if PLATFORM(COCOA)
264         RetainPtr<NSMenuItem> m_platformDescription;
265 #else
266         PlatformMenuItemDescription m_platformDescription;
267 #endif
268 #endif // USE(CROSS_PLATFORM_CONTEXT_MENUS)
269     };
270
271 #endif // ENABLE(CONTEXT_MENUS)
272 }
273
274 #endif // ContextMenuItem_h