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 "WebPopupMenuProxyGtk.h" 28 29#include "NativeWebMouseEvent.h" 30#include "WebPopupItem.h" 31#include <WebCore/GtkUtilities.h> 32#include <gtk/gtk.h> 33#include <wtf/gobject/GUniquePtr.h> 34#include <wtf/text/CString.h> 35 36using namespace WebCore; 37 38namespace WebKit { 39 40WebPopupMenuProxyGtk::WebPopupMenuProxyGtk(GtkWidget* webView, WebPopupMenuProxy::Client* client) 41 : WebPopupMenuProxy(client) 42 , m_webView(webView) 43 , m_activeItem(-1) 44{ 45} 46 47WebPopupMenuProxyGtk::~WebPopupMenuProxyGtk() 48{ 49 if (m_popup) { 50 g_signal_handlers_disconnect_matched(m_popup->platformMenu(), G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this); 51 hidePopupMenu(); 52 } 53} 54 55GtkAction* WebPopupMenuProxyGtk::createGtkActionForMenuItem(const WebPopupItem& item, int itemIndex) 56{ 57 GUniquePtr<char> actionName(g_strdup_printf("popup-menu-action-%d", itemIndex)); 58 GtkAction* action = gtk_action_new(actionName.get(), item.m_text.utf8().data(), item.m_toolTip.utf8().data(), 0); 59 g_object_set_data(G_OBJECT(action), "popup-menu-action-index", GINT_TO_POINTER(itemIndex)); 60 g_signal_connect(action, "activate", G_CALLBACK(menuItemActivated), this); 61 gtk_action_set_sensitive(action, item.m_isEnabled); 62 63 return action; 64} 65 66void WebPopupMenuProxyGtk::showPopupMenu(const IntRect& rect, TextDirection, double /* pageScaleFactor */, const Vector<WebPopupItem>& items, const PlatformPopupMenuData&, int32_t selectedIndex) 67{ 68 if (m_popup) 69 m_popup->clear(); 70 else 71 m_popup = GtkPopupMenu::create(); 72 73 const int size = items.size(); 74 for (int i = 0; i < size; i++) { 75 if (items[i].m_type == WebPopupItem::Separator) 76 m_popup->appendSeparator(); 77 else { 78 GRefPtr<GtkAction> action = adoptGRef(createGtkActionForMenuItem(items[i], i)); 79 m_popup->appendItem(action.get()); 80 } 81 } 82 83 IntPoint menuPosition = convertWidgetPointToScreenPoint(m_webView, rect.location()); 84 menuPosition.move(0, rect.height()); 85 86 gulong unmapHandler = g_signal_connect(m_popup->platformMenu(), "unmap", G_CALLBACK(menuUnmapped), this); 87 m_popup->popUp(rect.size(), menuPosition, size, selectedIndex, m_client->currentlyProcessedMouseDownEvent() ? m_client->currentlyProcessedMouseDownEvent()->nativeEvent() : 0); 88 89 // PopupMenu can fail to open when there is no mouse grab. 90 // Ensure WebCore does not go into some pesky state. 91 if (!gtk_widget_get_visible(m_popup->platformMenu())) { 92 m_client->failedToShowPopupMenu(); 93 return; 94 } 95 96 // WebPageProxy expects the menu to run in a nested run loop, since it invalidates the 97 // menu right after calling WebPopupMenuProxy::showPopupMenu(). 98 m_runLoop = adoptGRef(g_main_loop_new(0, FALSE)); 99 100// This is to suppress warnings about gdk_threads_leave and gdk_threads_enter. 101#pragma GCC diagnostic push 102#pragma GCC diagnostic ignored "-Wdeprecated-declarations" 103 gdk_threads_leave(); 104 g_main_loop_run(m_runLoop.get()); 105 gdk_threads_enter(); 106#pragma GCC diagnostic pop 107 108 m_runLoop.clear(); 109 110 g_signal_handler_disconnect(m_popup->platformMenu(), unmapHandler); 111 112 if (!m_client) 113 return; 114 115 m_client->valueChangedForPopupMenu(this, m_activeItem); 116} 117 118void WebPopupMenuProxyGtk::hidePopupMenu() 119{ 120 m_popup->popDown(); 121} 122 123void WebPopupMenuProxyGtk::shutdownRunLoop() 124{ 125 if (g_main_loop_is_running(m_runLoop.get())) 126 g_main_loop_quit(m_runLoop.get()); 127} 128 129void WebPopupMenuProxyGtk::menuItemActivated(GtkAction* action, WebPopupMenuProxyGtk* popupMenu) 130{ 131 popupMenu->setActiveItem(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "popup-menu-action-index"))); 132 popupMenu->shutdownRunLoop(); 133} 134 135void WebPopupMenuProxyGtk::menuUnmapped(GtkWidget*, WebPopupMenuProxyGtk* popupMenu) 136{ 137 popupMenu->shutdownRunLoop(); 138} 139 140} // namespace WebKit 141