From 40736c5763bf61337c8c14e16d8587db021a87d4 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Fri, 6 Jan 2012 14:44:00 +0100 Subject: Imported WebKit commit 2ea9d364d0f6efa8fa64acf19f451504c59be0e4 (http://svn.webkit.org/repository/webkit/trunk@104285) --- .../WebKit2/UIProcess/gtk/WebPopupMenuProxyGtk.cpp | 138 +++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 Source/WebKit2/UIProcess/gtk/WebPopupMenuProxyGtk.cpp (limited to 'Source/WebKit2/UIProcess/gtk/WebPopupMenuProxyGtk.cpp') diff --git a/Source/WebKit2/UIProcess/gtk/WebPopupMenuProxyGtk.cpp b/Source/WebKit2/UIProcess/gtk/WebPopupMenuProxyGtk.cpp new file mode 100644 index 000000000..521cf5320 --- /dev/null +++ b/Source/WebKit2/UIProcess/gtk/WebPopupMenuProxyGtk.cpp @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2011 Igalia S.L. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "WebPopupMenuProxyGtk.h" + +#include "NativeWebMouseEvent.h" +#include "WebPopupItem.h" +#include +#include +#include +#include + +using namespace WebCore; + +namespace WebKit { + +WebPopupMenuProxyGtk::WebPopupMenuProxyGtk(GtkWidget* webView, WebPopupMenuProxy::Client* client) + : WebPopupMenuProxy(client) + , m_webView(webView) + , m_activeItem(-1) + , m_runLoop(0) +{ +} + +WebPopupMenuProxyGtk::~WebPopupMenuProxyGtk() +{ + if (m_popup) { + g_signal_handlers_disconnect_matched(m_popup->platformMenu(), G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this); + hidePopupMenu(); + } +} + +GtkAction* WebPopupMenuProxyGtk::createGtkActionForMenuItem(const WebPopupItem& item, int itemIndex) +{ + GOwnPtr actionName(g_strdup_printf("popup-menu-action-%d", itemIndex)); + GtkAction* action = gtk_action_new(actionName.get(), item.m_text.utf8().data(), item.m_toolTip.utf8().data(), 0); + g_object_set_data(G_OBJECT(action), "popup-menu-action-index", GINT_TO_POINTER(itemIndex)); + g_signal_connect(action, "activate", G_CALLBACK(menuItemActivated), this); + gtk_action_set_sensitive(action, item.m_isEnabled); + + return action; +} + +void WebPopupMenuProxyGtk::showPopupMenu(const IntRect& rect, TextDirection textDirection, double pageScaleFactor, const Vector& items, const PlatformPopupMenuData& data, int32_t selectedIndex) +{ + if (m_popup) + m_popup->clear(); + else + m_popup = GtkPopupMenu::create(); + + const int size = items.size(); + for (int i = 0; i < size; i++) { + if (items[i].m_type == WebPopupItem::Separator) + m_popup->appendSeparator(); + else { + GRefPtr action = adoptGRef(createGtkActionForMenuItem(items[i], i)); + m_popup->appendItem(action.get()); + } + } + + IntPoint menuPosition = convertWidgetPointToScreenPoint(m_webView, rect.location()); + menuPosition.move(0, rect.height()); + + gulong unmapHandler = g_signal_connect(m_popup->platformMenu(), "unmap", G_CALLBACK(menuUnmapped), this); + m_popup->popUp(rect.size(), menuPosition, size, selectedIndex, m_client->currentlyProcessedMouseDownEvent() ? m_client->currentlyProcessedMouseDownEvent()->nativeEvent() : 0); + + // PopupMenu can fail to open when there is no mouse grab. + // Ensure WebCore does not go into some pesky state. + if (!gtk_widget_get_visible(m_popup->platformMenu())) { + m_client->failedToShowPopupMenu(); + return; + } + + // WebPageProxy expects the menu to run in a nested run loop, since it invalidates the + // menu right after calling WebPopupMenuProxy::showPopupMenu(). + m_runLoop = g_main_loop_new(0, FALSE); + + GDK_THREADS_LEAVE(); + g_main_loop_run(m_runLoop); + GDK_THREADS_ENTER(); + + g_main_loop_unref(m_runLoop); + m_runLoop = 0; + + g_signal_handler_disconnect(m_popup->platformMenu(), unmapHandler); + + if (!m_client) + return; + + m_client->valueChangedForPopupMenu(this, m_activeItem); +} + +void WebPopupMenuProxyGtk::hidePopupMenu() +{ + m_popup->popDown(); +} + +void WebPopupMenuProxyGtk::shutdownRunLoop() +{ + if (g_main_loop_is_running(m_runLoop)) + g_main_loop_quit(m_runLoop); +} + +void WebPopupMenuProxyGtk::menuItemActivated(GtkAction* action, WebPopupMenuProxyGtk* popupMenu) +{ + popupMenu->setActiveItem(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "popup-menu-action-index"))); + popupMenu->shutdownRunLoop(); +} + +void WebPopupMenuProxyGtk::menuUnmapped(GtkWidget*, WebPopupMenuProxyGtk* popupMenu) +{ + popupMenu->shutdownRunLoop(); +} + +} // namespace WebKit -- cgit v1.2.1