[GTK] Rename WebContextMenuItemGtk as WebContextMenuItemGlib
[WebKit.git] / Source / WebKit2 / Shared / glib / WebContextMenuItemGlib.cpp
1 /*
2  * Copyright (C) 2015 Igalia S.L.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WebContextMenuItemGlib.h"
28
29 #include "APIObject.h"
30 #include <gio/gio.h>
31
32 #if PLATFORM(GTK)
33 #include <gtk/gtk.h>
34 #endif
35
36 using namespace WebCore;
37
38 namespace WebKit {
39
40 #if PLATFORM(GTK)
41 static const char* gtkStockIDFromContextMenuAction(ContextMenuAction action)
42 {
43     switch (action) {
44     case ContextMenuItemTagCopyLinkToClipboard:
45     case ContextMenuItemTagCopyImageToClipboard:
46     case ContextMenuItemTagCopyMediaLinkToClipboard:
47     case ContextMenuItemTagCopy:
48         return GTK_STOCK_COPY;
49     case ContextMenuItemTagOpenLinkInNewWindow:
50     case ContextMenuItemTagOpenImageInNewWindow:
51     case ContextMenuItemTagOpenFrameInNewWindow:
52     case ContextMenuItemTagOpenMediaInNewWindow:
53         return GTK_STOCK_OPEN;
54     case ContextMenuItemTagDownloadLinkToDisk:
55     case ContextMenuItemTagDownloadImageToDisk:
56         return GTK_STOCK_SAVE;
57     case ContextMenuItemTagGoBack:
58         return GTK_STOCK_GO_BACK;
59     case ContextMenuItemTagGoForward:
60         return GTK_STOCK_GO_FORWARD;
61     case ContextMenuItemTagStop:
62         return GTK_STOCK_STOP;
63     case ContextMenuItemTagReload:
64         return GTK_STOCK_REFRESH;
65     case ContextMenuItemTagCut:
66         return GTK_STOCK_CUT;
67     case ContextMenuItemTagPaste:
68         return GTK_STOCK_PASTE;
69     case ContextMenuItemTagDelete:
70         return GTK_STOCK_DELETE;
71     case ContextMenuItemTagSelectAll:
72         return GTK_STOCK_SELECT_ALL;
73     case ContextMenuItemTagSpellingGuess:
74         return nullptr;
75     case ContextMenuItemTagIgnoreSpelling:
76         return GTK_STOCK_NO;
77     case ContextMenuItemTagLearnSpelling:
78         return GTK_STOCK_OK;
79     case ContextMenuItemTagOther:
80         return GTK_STOCK_MISSING_IMAGE;
81     case ContextMenuItemTagSearchInSpotlight:
82         return GTK_STOCK_FIND;
83     case ContextMenuItemTagSearchWeb:
84         return GTK_STOCK_FIND;
85     case ContextMenuItemTagOpenWithDefaultApplication:
86         return GTK_STOCK_OPEN;
87     case ContextMenuItemPDFZoomIn:
88         return GTK_STOCK_ZOOM_IN;
89     case ContextMenuItemPDFZoomOut:
90         return GTK_STOCK_ZOOM_OUT;
91     case ContextMenuItemPDFAutoSize:
92         return GTK_STOCK_ZOOM_FIT;
93     case ContextMenuItemPDFNextPage:
94         return GTK_STOCK_GO_FORWARD;
95     case ContextMenuItemPDFPreviousPage:
96         return GTK_STOCK_GO_BACK;
97     // New tags, not part of API
98     case ContextMenuItemTagOpenLink:
99         return GTK_STOCK_OPEN;
100     case ContextMenuItemTagCheckSpelling:
101         return GTK_STOCK_SPELL_CHECK;
102     case ContextMenuItemTagFontMenu:
103         return GTK_STOCK_SELECT_FONT;
104     case ContextMenuItemTagShowFonts:
105         return GTK_STOCK_SELECT_FONT;
106     case ContextMenuItemTagBold:
107         return GTK_STOCK_BOLD;
108     case ContextMenuItemTagItalic:
109         return GTK_STOCK_ITALIC;
110     case ContextMenuItemTagUnderline:
111         return GTK_STOCK_UNDERLINE;
112     case ContextMenuItemTagShowColors:
113         return GTK_STOCK_SELECT_COLOR;
114     case ContextMenuItemTagToggleMediaControls:
115     case ContextMenuItemTagToggleMediaLoop:
116     case ContextMenuItemTagCopyImageUrlToClipboard:
117         // No icon for this.
118         return nullptr;
119     case ContextMenuItemTagEnterVideoFullscreen:
120         return GTK_STOCK_FULLSCREEN;
121     default:
122         return nullptr;
123     }
124 }
125 #endif // PLATFORM(GTK)
126
127 WebContextMenuItemGlib::WebContextMenuItemGlib(ContextMenuItemType type, ContextMenuAction action, const String& title, bool enabled, bool checked)
128     : WebContextMenuItemData(type, action, title, enabled, checked)
129 {
130     ASSERT(type != SubmenuType);
131     createActionIfNeeded();
132 }
133
134 WebContextMenuItemGlib::WebContextMenuItemGlib(const WebContextMenuItemData& data)
135     : WebContextMenuItemData(data.type() == SubmenuType ? ActionType : data.type(), data.action(), data.title(), data.enabled(), data.checked())
136 {
137     createActionIfNeeded();
138 }
139
140 WebContextMenuItemGlib::WebContextMenuItemGlib(const WebContextMenuItemGlib& data, Vector<WebContextMenuItemGlib>&& submenu)
141     : WebContextMenuItemData(ActionType, data.action(), data.title(), data.enabled(), false)
142 {
143     m_gAction = data.gAction();
144     m_submenuItems = WTFMove(submenu);
145 #if PLATFORM(GTK)
146     m_gtkAction = data.gtkAction();
147 #endif
148 }
149
150 static bool isGActionChecked(GAction* action)
151 {
152     if (!g_action_get_state_type(action))
153         return false;
154
155     ASSERT(g_variant_type_equal(g_action_get_state_type(action), G_VARIANT_TYPE_BOOLEAN));
156     GRefPtr<GVariant> state = adoptGRef(g_action_get_state(action));
157     return g_variant_get_boolean(state.get());
158 }
159
160 WebContextMenuItemGlib::WebContextMenuItemGlib(GAction* action, const String& title, GVariant* target)
161     : WebContextMenuItemData(g_action_get_state_type(action) ? CheckableActionType : ActionType, ContextMenuItemBaseApplicationTag, title, g_action_get_enabled(action), isGActionChecked(action))
162     , m_gAction(action)
163     , m_gActionTarget(target)
164 {
165     createActionIfNeeded();
166 }
167
168 #if PLATFORM(GTK)
169 WebContextMenuItemGlib::WebContextMenuItemGlib(GtkAction* action)
170     : WebContextMenuItemData(GTK_IS_TOGGLE_ACTION(action) ? CheckableActionType : ActionType, ContextMenuItemBaseApplicationTag, String::fromUTF8(gtk_action_get_label(action)), gtk_action_get_sensitive(action), GTK_IS_TOGGLE_ACTION(action) ? gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action)) : false)
171 {
172     m_gtkAction = action;
173     createActionIfNeeded();
174     g_object_set_data_full(G_OBJECT(m_gAction.get()), "webkit-gtk-action", g_object_ref(m_gtkAction), g_object_unref);
175 }
176 #endif
177
178 WebContextMenuItemGlib::~WebContextMenuItemGlib()
179 {
180 }
181
182 GUniquePtr<char> WebContextMenuItemGlib::buildActionName() const
183 {
184 #if PLATFORM(GTK)
185     if (m_gtkAction)
186         return GUniquePtr<char>(g_strdup(gtk_action_get_name(m_gtkAction)));
187 #endif
188
189     static uint64_t actionID = 0;
190     return GUniquePtr<char>(g_strdup_printf("action-%" PRIu64, ++actionID));
191 }
192
193 void WebContextMenuItemGlib::createActionIfNeeded()
194 {
195     if (type() == SeparatorType)
196         return;
197
198     if (!m_gAction) {
199         auto actionName = buildActionName();
200         if (type() == CheckableActionType)
201             m_gAction = adoptGRef(G_ACTION(g_simple_action_new_stateful(actionName.get(), nullptr, g_variant_new_boolean(checked()))));
202         else
203             m_gAction = adoptGRef(G_ACTION(g_simple_action_new(actionName.get(), nullptr)));
204         g_simple_action_set_enabled(G_SIMPLE_ACTION(m_gAction.get()), enabled());
205     }
206
207 #if PLATFORM(GTK)
208     // Create the GtkAction for backwards compatibility only.
209     if (!m_gtkAction) {
210         if (type() == CheckableActionType) {
211             m_gtkAction = GTK_ACTION(gtk_toggle_action_new(g_action_get_name(m_gAction.get()), title().utf8().data(), nullptr, gtkStockIDFromContextMenuAction(action())));
212             gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(m_gtkAction), checked());
213         } else
214             m_gtkAction = gtk_action_new(g_action_get_name(m_gAction.get()), title().utf8().data(), 0, gtkStockIDFromContextMenuAction(action()));
215         gtk_action_set_sensitive(m_gtkAction, enabled());
216         g_object_set_data_full(G_OBJECT(m_gAction.get()), "webkit-gtk-action", m_gtkAction, g_object_unref);
217     }
218
219     g_signal_connect_object(m_gAction.get(), "activate", G_CALLBACK(gtk_action_activate), m_gtkAction, G_CONNECT_SWAPPED);
220 #endif
221 }
222
223 } // namespace WebKit