a6100505440f0f8fdc6ae81e27cf1268562aa738
[WebKit.git] / Source / WebKit2 / Shared / gtk / WebContextMenuItemGtk.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 "WebContextMenuItemGtk.h"
28
29 #include <gtk/gtk.h>
30 #include <wtf/glib/GUniquePtr.h>
31
32 using namespace WebCore;
33
34 namespace WebKit {
35
36 static const char* gtkStockIDFromContextMenuAction(ContextMenuAction action)
37 {
38     switch (action) {
39     case ContextMenuItemTagCopyLinkToClipboard:
40     case ContextMenuItemTagCopyImageToClipboard:
41     case ContextMenuItemTagCopyMediaLinkToClipboard:
42     case ContextMenuItemTagCopy:
43         return GTK_STOCK_COPY;
44     case ContextMenuItemTagOpenLinkInNewWindow:
45     case ContextMenuItemTagOpenImageInNewWindow:
46     case ContextMenuItemTagOpenFrameInNewWindow:
47     case ContextMenuItemTagOpenMediaInNewWindow:
48         return GTK_STOCK_OPEN;
49     case ContextMenuItemTagDownloadLinkToDisk:
50     case ContextMenuItemTagDownloadImageToDisk:
51         return GTK_STOCK_SAVE;
52     case ContextMenuItemTagGoBack:
53         return GTK_STOCK_GO_BACK;
54     case ContextMenuItemTagGoForward:
55         return GTK_STOCK_GO_FORWARD;
56     case ContextMenuItemTagStop:
57         return GTK_STOCK_STOP;
58     case ContextMenuItemTagReload:
59         return GTK_STOCK_REFRESH;
60     case ContextMenuItemTagCut:
61         return GTK_STOCK_CUT;
62     case ContextMenuItemTagPaste:
63         return GTK_STOCK_PASTE;
64     case ContextMenuItemTagDelete:
65         return GTK_STOCK_DELETE;
66     case ContextMenuItemTagSelectAll:
67         return GTK_STOCK_SELECT_ALL;
68     case ContextMenuItemTagSpellingGuess:
69         return 0;
70     case ContextMenuItemTagIgnoreSpelling:
71         return GTK_STOCK_NO;
72     case ContextMenuItemTagLearnSpelling:
73         return GTK_STOCK_OK;
74     case ContextMenuItemTagOther:
75         return GTK_STOCK_MISSING_IMAGE;
76     case ContextMenuItemTagSearchInSpotlight:
77         return GTK_STOCK_FIND;
78     case ContextMenuItemTagSearchWeb:
79         return GTK_STOCK_FIND;
80     case ContextMenuItemTagOpenWithDefaultApplication:
81         return GTK_STOCK_OPEN;
82     case ContextMenuItemPDFZoomIn:
83         return GTK_STOCK_ZOOM_IN;
84     case ContextMenuItemPDFZoomOut:
85         return GTK_STOCK_ZOOM_OUT;
86     case ContextMenuItemPDFAutoSize:
87         return GTK_STOCK_ZOOM_FIT;
88     case ContextMenuItemPDFNextPage:
89         return GTK_STOCK_GO_FORWARD;
90     case ContextMenuItemPDFPreviousPage:
91         return GTK_STOCK_GO_BACK;
92     // New tags, not part of API
93     case ContextMenuItemTagOpenLink:
94         return GTK_STOCK_OPEN;
95     case ContextMenuItemTagCheckSpelling:
96         return GTK_STOCK_SPELL_CHECK;
97     case ContextMenuItemTagFontMenu:
98         return GTK_STOCK_SELECT_FONT;
99     case ContextMenuItemTagShowFonts:
100         return GTK_STOCK_SELECT_FONT;
101     case ContextMenuItemTagBold:
102         return GTK_STOCK_BOLD;
103     case ContextMenuItemTagItalic:
104         return GTK_STOCK_ITALIC;
105     case ContextMenuItemTagUnderline:
106         return GTK_STOCK_UNDERLINE;
107     case ContextMenuItemTagShowColors:
108         return GTK_STOCK_SELECT_COLOR;
109     case ContextMenuItemTagToggleMediaControls:
110     case ContextMenuItemTagToggleMediaLoop:
111     case ContextMenuItemTagCopyImageUrlToClipboard:
112         // No icon for this.
113         return 0;
114     case ContextMenuItemTagEnterVideoFullscreen:
115         return GTK_STOCK_FULLSCREEN;
116     default:
117         return 0;
118     }
119 }
120
121 WebContextMenuItemGtk::WebContextMenuItemGtk(ContextMenuItemType type, ContextMenuAction action, const String& title, bool enabled, bool checked)
122     : WebContextMenuItemData(type, action, title, enabled, checked)
123 {
124     ASSERT(type != SubmenuType);
125     createActionIfNeeded();
126 }
127
128 WebContextMenuItemGtk::WebContextMenuItemGtk(const WebContextMenuItemData& data)
129     : WebContextMenuItemData(data.type() == SubmenuType ? ActionType : data.type(), data.action(), data.title(), data.enabled(), data.checked())
130 {
131     createActionIfNeeded();
132 }
133
134 WebContextMenuItemGtk::WebContextMenuItemGtk(const WebContextMenuItemGtk& data, Vector<WebContextMenuItemGtk>&& submenu)
135     : WebContextMenuItemData(ActionType, data.action(), data.title(), data.enabled(), false)
136 {
137     m_gAction = data.gAction();
138     m_gtkAction = data.gtkAction();
139     m_submenuItems = WTFMove(submenu);
140 }
141
142 WebContextMenuItemGtk::WebContextMenuItemGtk(GtkAction* action)
143     : 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)
144 {
145     m_gtkAction = action;
146     createActionIfNeeded();
147     g_object_set_data_full(G_OBJECT(m_gAction.get()), "webkit-gtk-action", g_object_ref(m_gtkAction), g_object_unref);
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 WebContextMenuItemGtk::WebContextMenuItemGtk(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 WebContextMenuItemGtk::~WebContextMenuItemGtk()
169 {
170 }
171
172 void WebContextMenuItemGtk::createActionIfNeeded()
173 {
174     if (type() == SeparatorType)
175         return;
176
177     static uint64_t actionID = 0;
178     if (!m_gAction) {
179         GUniquePtr<char> actionName;
180         if (m_gtkAction)
181             actionName.reset(g_strdup(gtk_action_get_name(m_gtkAction)));
182         else
183             actionName.reset(g_strdup_printf("action-%" PRIu64, ++actionID));
184         if (type() == CheckableActionType)
185             m_gAction = adoptGRef(G_ACTION(g_simple_action_new_stateful(actionName.get(), nullptr, g_variant_new_boolean(checked()))));
186         else
187             m_gAction = adoptGRef(G_ACTION(g_simple_action_new(actionName.get(), nullptr)));
188         g_simple_action_set_enabled(G_SIMPLE_ACTION(m_gAction.get()), enabled());
189     }
190
191     // Create the GtkAction for backwards compatibility only.
192     if (!m_gtkAction) {
193         if (type() == CheckableActionType) {
194             m_gtkAction = GTK_ACTION(gtk_toggle_action_new(g_action_get_name(m_gAction.get()), title().utf8().data(), nullptr, gtkStockIDFromContextMenuAction(action())));
195             gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(m_gtkAction), checked());
196         } else
197             m_gtkAction = gtk_action_new(g_action_get_name(m_gAction.get()), title().utf8().data(), 0, gtkStockIDFromContextMenuAction(action()));
198         gtk_action_set_sensitive(m_gtkAction, enabled());
199         g_object_set_data_full(G_OBJECT(m_gAction.get()), "webkit-gtk-action", m_gtkAction, g_object_unref);
200     }
201
202     g_signal_connect_object(m_gAction.get(), "activate", G_CALLBACK(gtk_action_activate), m_gtkAction, G_CONNECT_SWAPPED);
203 }
204
205 } // namespace WebKit