| 1 | /* |
| 2 | * Copyright (C) 2011 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 "WebContextMenuProxyGtk.h" |
| 28 | |
| 29 | #if ENABLE(CONTEXT_MENUS) |
| 30 | |
| 31 | #include "APIContextMenuClient.h" |
| 32 | #include "NativeWebMouseEvent.h" |
| 33 | #include "WebContextMenuItem.h" |
| 34 | #include "WebContextMenuItemData.h" |
| 35 | #include "WebKitWebViewBasePrivate.h" |
| 36 | #include "WebPageProxy.h" |
| 37 | #include "WebProcessProxy.h" |
| 38 | #include <WebCore/GtkUtilities.h> |
| 39 | #include <gio/gio.h> |
| 40 | #include <gtk/gtk.h> |
| 41 | #include <wtf/text/CString.h> |
| 42 | |
| 43 | static const char* = "webkit-context-menu-action" ; |
| 44 | static const char* = "webkit-context-menu-title" ; |
| 45 | static const char* = "webkitContextMenu" ; |
| 46 | |
| 47 | namespace WebKit { |
| 48 | using namespace WebCore; |
| 49 | |
| 50 | static void (GAction* action, GVariant*, WebPageProxy* page) |
| 51 | { |
| 52 | auto* stateType = g_action_get_state_type(action); |
| 53 | gboolean isToggle = stateType && g_variant_type_equal(stateType, G_VARIANT_TYPE_BOOLEAN); |
| 54 | GRefPtr<GVariant> state = isToggle ? adoptGRef(g_action_get_state(action)) : nullptr; |
| 55 | WebContextMenuItemData item(isToggle ? CheckableActionType : ActionType, |
| 56 | static_cast<ContextMenuAction>(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), gContextMenuActionId))), |
| 57 | String::fromUTF8(static_cast<const char*>(g_object_get_data(G_OBJECT(action), gContextMenuTitle))), g_action_get_enabled(action), |
| 58 | state ? g_variant_get_boolean(state.get()) : false); |
| 59 | if (isToggle) |
| 60 | g_action_change_state(action, g_variant_new_boolean(!g_variant_get_boolean(state.get()))); |
| 61 | page->contextMenuItemSelected(item); |
| 62 | } |
| 63 | |
| 64 | void WebContextMenuProxyGtk::(GMenu* , const WebContextMenuItemGlib& ) |
| 65 | { |
| 66 | unsigned long signalHandlerId; |
| 67 | GRefPtr<GMenuItem> ; |
| 68 | GAction* action = menuItem.gAction(); |
| 69 | ASSERT(action); |
| 70 | #if GTK_CHECK_VERSION(3, 16, 0) |
| 71 | g_action_map_add_action(G_ACTION_MAP(gtk_widget_get_action_group(GTK_WIDGET(m_menu), gContextMenuItemGroup)), action); |
| 72 | #else |
| 73 | g_action_map_add_action(G_ACTION_MAP(g_object_get_data(G_OBJECT(m_menu), gContextMenuItemGroup)), action); |
| 74 | #endif |
| 75 | |
| 76 | switch (menuItem.type()) { |
| 77 | case ActionType: |
| 78 | case CheckableActionType: { |
| 79 | GUniquePtr<char> actionName(g_strdup_printf("%s.%s" , gContextMenuItemGroup, g_action_get_name(action))); |
| 80 | gMenuItem = adoptGRef(g_menu_item_new(menuItem.title().utf8().data(), nullptr)); |
| 81 | g_menu_item_set_action_and_target_value(gMenuItem.get(), actionName.get(), menuItem.gActionTarget()); |
| 82 | |
| 83 | if (menuItem.action() < ContextMenuItemBaseApplicationTag) { |
| 84 | g_object_set_data(G_OBJECT(action), gContextMenuActionId, GINT_TO_POINTER(menuItem.action())); |
| 85 | g_object_set_data_full(G_OBJECT(action), gContextMenuTitle, g_strdup(menuItem.title().utf8().data()), g_free); |
| 86 | signalHandlerId = g_signal_connect(action, "activate" , G_CALLBACK(contextMenuItemActivatedCallback), m_page); |
| 87 | m_signalHandlers.set(signalHandlerId, action); |
| 88 | } |
| 89 | break; |
| 90 | } |
| 91 | case SubmenuType: { |
| 92 | GRefPtr<GMenu> = buildMenu(menuItem.submenuItems()); |
| 93 | gMenuItem = adoptGRef(g_menu_item_new_submenu(menuItem.title().utf8().data(), G_MENU_MODEL(submenu.get()))); |
| 94 | break; |
| 95 | } |
| 96 | case SeparatorType: |
| 97 | ASSERT_NOT_REACHED(); |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | g_menu_append_item(menu, gMenuItem.get()); |
| 102 | } |
| 103 | |
| 104 | GRefPtr<GMenu> WebContextMenuProxyGtk::(const Vector<WebContextMenuItemGlib>& items) |
| 105 | { |
| 106 | GRefPtr<GMenu> = adoptGRef(g_menu_new()); |
| 107 | GMenu* = menu.get(); |
| 108 | for (const auto& item : items) { |
| 109 | if (item.type() == SeparatorType) { |
| 110 | GRefPtr<GMenu> section = adoptGRef(g_menu_new()); |
| 111 | g_menu_append_section(menu.get(), nullptr, G_MENU_MODEL(section.get())); |
| 112 | sectionMenu = section.get(); |
| 113 | } else |
| 114 | append(sectionMenu, item); |
| 115 | } |
| 116 | |
| 117 | return menu; |
| 118 | } |
| 119 | |
| 120 | Vector<WebContextMenuItemGlib> WebContextMenuProxyGtk::(const WebContextMenuItemData& ) |
| 121 | { |
| 122 | Vector<WebContextMenuItemGlib> items; |
| 123 | for (const auto& itemData : subMenuItemData.submenu()) { |
| 124 | if (itemData.type() == SubmenuType) |
| 125 | items.append(WebContextMenuItemGlib(itemData, populateSubMenu(itemData))); |
| 126 | else |
| 127 | items.append(itemData); |
| 128 | } |
| 129 | return items; |
| 130 | } |
| 131 | |
| 132 | void WebContextMenuProxyGtk::(const Vector<WebContextMenuItemGlib>& items) |
| 133 | { |
| 134 | GRefPtr<GMenu> = buildMenu(items); |
| 135 | gtk_menu_shell_bind_model(GTK_MENU_SHELL(m_menu), G_MENU_MODEL(menu.get()), nullptr, TRUE); |
| 136 | } |
| 137 | |
| 138 | void WebContextMenuProxyGtk::(const Vector<Ref<WebContextMenuItem>>& items) |
| 139 | { |
| 140 | GRefPtr<GMenu> = adoptGRef(g_menu_new()); |
| 141 | GMenu* = menu.get(); |
| 142 | for (const auto& item : items) { |
| 143 | switch (item->data().type()) { |
| 144 | case SeparatorType: { |
| 145 | GRefPtr<GMenu> section = adoptGRef(g_menu_new()); |
| 146 | g_menu_append_section(menu.get(), nullptr, G_MENU_MODEL(section.get())); |
| 147 | sectionMenu = section.get(); |
| 148 | break; |
| 149 | } |
| 150 | case SubmenuType: { |
| 151 | WebContextMenuItemGlib (item->data(), populateSubMenu(item->data())); |
| 152 | append(sectionMenu, menuitem); |
| 153 | break; |
| 154 | } |
| 155 | case ActionType: |
| 156 | case CheckableActionType: { |
| 157 | WebContextMenuItemGlib (item->data()); |
| 158 | append(sectionMenu, menuitem); |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | gtk_menu_shell_bind_model(GTK_MENU_SHELL(m_menu), G_MENU_MODEL(menu.get()), nullptr, TRUE); |
| 164 | } |
| 165 | |
| 166 | void WebContextMenuProxyGtk::() |
| 167 | { |
| 168 | Vector<Ref<WebContextMenuItem>> proposedAPIItems; |
| 169 | for (auto& item : m_context.menuItems()) { |
| 170 | if (item.action() != ContextMenuItemTagShareMenu) |
| 171 | proposedAPIItems.append(WebContextMenuItem::create(item)); |
| 172 | } |
| 173 | |
| 174 | m_page->contextMenuClient().getContextMenuFromProposedMenu(*m_page, WTFMove(proposedAPIItems), WebContextMenuListenerProxy::create(this).get(), m_context.webHitTestResultData(), m_page->process().transformHandlesToObjects(m_userData.object()).get()); |
| 175 | } |
| 176 | |
| 177 | void WebContextMenuProxyGtk::(Vector<Ref<WebContextMenuItem>>&& items) |
| 178 | { |
| 179 | if (!items.isEmpty()) |
| 180 | populate(items); |
| 181 | |
| 182 | unsigned childCount = 0; |
| 183 | gtk_container_foreach(GTK_CONTAINER(m_menu), [](GtkWidget*, gpointer data) { (*static_cast<unsigned*>(data))++; }, &childCount); |
| 184 | if (!childCount) |
| 185 | return; |
| 186 | |
| 187 | m_popupPosition = convertWidgetPointToScreenPoint(m_webView, m_context.menuLocation()); |
| 188 | |
| 189 | // Display menu initiated by right click (mouse button pressed = 3). |
| 190 | NativeWebMouseEvent* mouseEvent = m_page->currentlyProcessedMouseDownEvent(); |
| 191 | const GdkEvent* event = mouseEvent ? mouseEvent->nativeEvent() : 0; |
| 192 | gtk_menu_attach_to_widget(m_menu, GTK_WIDGET(m_webView), nullptr); |
| 193 | gtk_menu_popup(m_menu, nullptr, nullptr, reinterpret_cast<GtkMenuPositionFunc>(menuPositionFunction), this, event ? event->button.button : 3, event ? event->button.time : GDK_CURRENT_TIME); |
| 194 | } |
| 195 | |
| 196 | WebContextMenuProxyGtk::(GtkWidget* webView, WebPageProxy& page, ContextMenuContextData&& context, const UserData& userData) |
| 197 | : WebContextMenuProxy(WTFMove(context), userData) |
| 198 | , m_webView(webView) |
| 199 | , m_page(&page) |
| 200 | , m_menu(GTK_MENU(gtk_menu_new())) |
| 201 | { |
| 202 | GRefPtr<GSimpleActionGroup> group = adoptGRef(g_simple_action_group_new()); |
| 203 | gtk_widget_insert_action_group(GTK_WIDGET(m_menu), gContextMenuItemGroup, G_ACTION_GROUP(group.get())); |
| 204 | #if !GTK_CHECK_VERSION(3, 16, 0) |
| 205 | g_object_set_data(G_OBJECT(m_menu), gContextMenuItemGroup, group.get()); |
| 206 | #endif |
| 207 | webkitWebViewBaseSetActiveContextMenuProxy(WEBKIT_WEB_VIEW_BASE(m_webView), this); |
| 208 | } |
| 209 | |
| 210 | WebContextMenuProxyGtk::() |
| 211 | { |
| 212 | gtk_menu_popdown(m_menu); |
| 213 | |
| 214 | for (auto& handler : m_signalHandlers) |
| 215 | g_signal_handler_disconnect(handler.value, handler.key); |
| 216 | m_signalHandlers.clear(); |
| 217 | |
| 218 | gtk_widget_insert_action_group(GTK_WIDGET(m_menu), gContextMenuItemGroup, nullptr); |
| 219 | #if !GTK_CHECK_VERSION(3, 16, 0) |
| 220 | g_object_set_data(G_OBJECT(m_menu), gContextMenuItemGroup, nullptr); |
| 221 | #endif |
| 222 | gtk_widget_destroy(GTK_WIDGET(m_menu)); |
| 223 | } |
| 224 | |
| 225 | void WebContextMenuProxyGtk::(GtkMenu* , gint* x, gint* y, gboolean* pushIn, WebContextMenuProxyGtk* ) |
| 226 | { |
| 227 | GtkRequisition ; |
| 228 | gtk_widget_get_preferred_size(GTK_WIDGET(menu), &menuSize, 0); |
| 229 | |
| 230 | GdkScreen* screen = gtk_widget_get_screen(popupMenu->m_webView); |
| 231 | *x = popupMenu->m_popupPosition.x(); |
| 232 | if ((*x + menuSize.width) >= gdk_screen_get_width(screen)) |
| 233 | *x -= menuSize.width; |
| 234 | |
| 235 | *y = popupMenu->m_popupPosition.y(); |
| 236 | if ((*y + menuSize.height) >= gdk_screen_get_height(screen)) |
| 237 | *y -= menuSize.height; |
| 238 | |
| 239 | *pushIn = FALSE; |
| 240 | } |
| 241 | |
| 242 | } // namespace WebKit |
| 243 | #endif // ENABLE(CONTEXT_MENUS) |
| 244 | |