summaryrefslogtreecommitdiff
path: root/Source/WebKit2/UIProcess/gtk/WebPopupMenuProxyGtk.cpp
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@nokia.com>2012-01-06 14:44:00 +0100
committerSimon Hausmann <simon.hausmann@nokia.com>2012-01-06 14:44:00 +0100
commit40736c5763bf61337c8c14e16d8587db021a87d4 (patch)
treeb17a9c00042ad89cb1308e2484491799aa14e9f8 /Source/WebKit2/UIProcess/gtk/WebPopupMenuProxyGtk.cpp
downloadqtwebkit-40736c5763bf61337c8c14e16d8587db021a87d4.tar.gz
Imported WebKit commit 2ea9d364d0f6efa8fa64acf19f451504c59be0e4 (http://svn.webkit.org/repository/webkit/trunk@104285)
Diffstat (limited to 'Source/WebKit2/UIProcess/gtk/WebPopupMenuProxyGtk.cpp')
-rw-r--r--Source/WebKit2/UIProcess/gtk/WebPopupMenuProxyGtk.cpp138
1 files changed, 138 insertions, 0 deletions
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 <WebCore/GtkUtilities.h>
+#include <gtk/gtk.h>
+#include <wtf/gobject/GOwnPtr.h>
+#include <wtf/text/CString.h>
+
+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<char> 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<WebPopupItem>& 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<GtkAction> 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