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>
38OBJC_CLASS NSMenuItem;
39#elif PLATFORM(GTK)
40typedef struct _GtkMenuItem GtkMenuItem;
41typedef struct _GtkAction GtkAction;
42#endif
43#endif // ENABLE(CONTEXT_MENUS)
44
45namespace WebCore {
46
47    class ContextMenu;
48
49    // This enum needs to be in sync with the WebMenuItemTag enum in WebUIDelegate.h and the
50    // extra values in WebUIDelegatePrivate.h
51    enum ContextMenuAction {
52        ContextMenuItemTagNoAction=0, // This item is not actually in WebUIDelegate.h
53        ContextMenuItemTagOpenLinkInNewWindow=1,
54        ContextMenuItemTagDownloadLinkToDisk,
55        ContextMenuItemTagCopyLinkToClipboard,
56        ContextMenuItemTagOpenImageInNewWindow,
57        ContextMenuItemTagDownloadImageToDisk,
58        ContextMenuItemTagCopyImageToClipboard,
59#if PLATFORM(QT) || PLATFORM(GTK) || PLATFORM(EFL)
60        ContextMenuItemTagCopyImageUrlToClipboard,
61#endif
62        ContextMenuItemTagOpenFrameInNewWindow,
63        ContextMenuItemTagCopy,
64        ContextMenuItemTagGoBack,
65        ContextMenuItemTagGoForward,
66        ContextMenuItemTagStop,
67        ContextMenuItemTagReload,
68        ContextMenuItemTagCut,
69        ContextMenuItemTagPaste,
70#if PLATFORM(GTK)
71        ContextMenuItemTagDelete,
72#endif
73#if PLATFORM(GTK) || PLATFORM(QT) || PLATFORM (EFL)
74        ContextMenuItemTagSelectAll,
75#endif
76#if PLATFORM(GTK)
77        ContextMenuItemTagInputMethods,
78        ContextMenuItemTagUnicode,
79        ContextMenuItemTagUnicodeInsertLRMMark,
80        ContextMenuItemTagUnicodeInsertRLMMark,
81        ContextMenuItemTagUnicodeInsertLREMark,
82        ContextMenuItemTagUnicodeInsertRLEMark,
83        ContextMenuItemTagUnicodeInsertLROMark,
84        ContextMenuItemTagUnicodeInsertRLOMark,
85        ContextMenuItemTagUnicodeInsertPDFMark,
86        ContextMenuItemTagUnicodeInsertZWSMark,
87        ContextMenuItemTagUnicodeInsertZWJMark,
88        ContextMenuItemTagUnicodeInsertZWNJMark,
89#endif
90        ContextMenuItemTagSpellingGuess,
91        ContextMenuItemTagNoGuessesFound,
92        ContextMenuItemTagIgnoreSpelling,
93        ContextMenuItemTagLearnSpelling,
94        ContextMenuItemTagOther,
95        ContextMenuItemTagSearchInSpotlight,
96        ContextMenuItemTagSearchWeb,
97        ContextMenuItemTagLookUpInDictionary,
98        ContextMenuItemTagOpenWithDefaultApplication,
99        ContextMenuItemPDFActualSize,
100        ContextMenuItemPDFZoomIn,
101        ContextMenuItemPDFZoomOut,
102        ContextMenuItemPDFAutoSize,
103        ContextMenuItemPDFSinglePage,
104        ContextMenuItemPDFFacingPages,
105        ContextMenuItemPDFContinuous,
106        ContextMenuItemPDFNextPage,
107        ContextMenuItemPDFPreviousPage,
108        // These are new tags! Not a part of API!!!!
109        ContextMenuItemTagOpenLink = 2000,
110        ContextMenuItemTagIgnoreGrammar,
111        ContextMenuItemTagSpellingMenu, // Spelling or Spelling/Grammar sub-menu
112        ContextMenuItemTagShowSpellingPanel,
113        ContextMenuItemTagCheckSpelling,
114        ContextMenuItemTagCheckSpellingWhileTyping,
115        ContextMenuItemTagCheckGrammarWithSpelling,
116        ContextMenuItemTagFontMenu, // Font sub-menu
117        ContextMenuItemTagShowFonts,
118        ContextMenuItemTagBold,
119        ContextMenuItemTagItalic,
120        ContextMenuItemTagUnderline,
121        ContextMenuItemTagOutline,
122        ContextMenuItemTagStyles,
123        ContextMenuItemTagShowColors,
124        ContextMenuItemTagSpeechMenu, // Speech sub-menu
125        ContextMenuItemTagStartSpeaking,
126        ContextMenuItemTagStopSpeaking,
127        ContextMenuItemTagWritingDirectionMenu, // Writing Direction sub-menu
128        ContextMenuItemTagDefaultDirection,
129        ContextMenuItemTagLeftToRight,
130        ContextMenuItemTagRightToLeft,
131        ContextMenuItemTagPDFSinglePageScrolling,
132        ContextMenuItemTagPDFFacingPagesScrolling,
133#if ENABLE(INSPECTOR)
134        ContextMenuItemTagInspectElement,
135#endif
136        ContextMenuItemTagTextDirectionMenu, // Text Direction sub-menu
137        ContextMenuItemTagTextDirectionDefault,
138        ContextMenuItemTagTextDirectionLeftToRight,
139        ContextMenuItemTagTextDirectionRightToLeft,
140#if PLATFORM(MAC)
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        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#else
185    typedef void* PlatformMenuItemDescription;
186#endif
187
188    class ContextMenuItem {
189        WTF_MAKE_FAST_ALLOCATED;
190    public:
191        ContextMenuItem(ContextMenuItemType, ContextMenuAction, const String&, ContextMenu* subMenu = 0);
192        ContextMenuItem(ContextMenuItemType, ContextMenuAction, const String&, bool enabled, bool checked);
193
194        ~ContextMenuItem();
195
196        void setType(ContextMenuItemType);
197        ContextMenuItemType type() const;
198
199        void setAction(ContextMenuAction);
200        ContextMenuAction action() const;
201
202        void setChecked(bool = true);
203        bool checked() const;
204
205        void setEnabled(bool = true);
206        bool enabled() const;
207
208        void setSubMenu(ContextMenu*);
209
210#if PLATFORM(GTK)
211        GtkAction* gtkAction() const;
212#endif
213
214#if USE(CROSS_PLATFORM_CONTEXT_MENUS)
215        ContextMenuItem(ContextMenuAction, const String&, bool enabled, bool checked, const Vector<ContextMenuItem>& subMenuItems);
216        explicit ContextMenuItem(const PlatformContextMenuItem&);
217
218        // On Windows, the title (dwTypeData of the MENUITEMINFO) is not set in this function. Callers can set the title themselves,
219        // and handle the lifetime of the title, if they need it.
220        PlatformContextMenuItem platformContextMenuItem() const;
221
222        void setTitle(const String& title) { m_title = title; }
223        const String& title() const { return m_title; }
224
225        const Vector<ContextMenuItem>& subMenuItems() const { return m_subMenuItems; }
226#else
227    public:
228        explicit ContextMenuItem(PlatformMenuItemDescription);
229        explicit ContextMenuItem(ContextMenu* subMenu = 0);
230        ContextMenuItem(ContextMenuAction, const String&, bool enabled, bool checked, Vector<ContextMenuItem>& submenuItems);
231
232        PlatformMenuItemDescription releasePlatformDescription();
233
234        String title() const;
235        void setTitle(const String&);
236
237        PlatformMenuDescription platformSubMenu() const;
238        void setSubMenu(Vector<ContextMenuItem>&);
239
240#endif // USE(CROSS_PLATFORM_CONTEXT_MENUS)
241    private:
242#if USE(CROSS_PLATFORM_CONTEXT_MENUS)
243        ContextMenuItemType m_type;
244        ContextMenuAction m_action;
245        String m_title;
246        bool m_enabled;
247        bool m_checked;
248        Vector<ContextMenuItem> m_subMenuItems;
249#else
250#if PLATFORM(MAC)
251        RetainPtr<NSMenuItem> m_platformDescription;
252#else
253        PlatformMenuItemDescription m_platformDescription;
254#endif
255#endif // USE(CROSS_PLATFORM_CONTEXT_MENUS)
256    };
257
258#endif // ENABLE(CONTEXT_MENUS)
259}
260
261#endif // ContextMenuItem_h
262