Unreviewed, rolling out r154989.
[WebKit-https.git] / Source / WebKit2 / UIProcess / gtk / WebPopupMenuProxyGtk.cpp
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/GOwnPtr.h>
34 #include <wtf/text/CString.h>
35
36 using namespace WebCore;
37
38 namespace WebKit {
39
40 WebPopupMenuProxyGtk::WebPopupMenuProxyGtk(GtkWidget* webView, WebPopupMenuProxy::Client* client)
41     : WebPopupMenuProxy(client)
42     , m_webView(webView)
43     , m_activeItem(-1)
44 {
45 }
46
47 WebPopupMenuProxyGtk::~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
55 GtkAction* WebPopupMenuProxyGtk::createGtkActionForMenuItem(const WebPopupItem& item, int itemIndex)
56 {
57     GOwnPtr<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
66 void WebPopupMenuProxyGtk::showPopupMenu(const IntRect& rect, TextDirection textDirection, double pageScaleFactor, const Vector<WebPopupItem>& items, const PlatformPopupMenuData& data, 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     gdk_threads_leave();
101     g_main_loop_run(m_runLoop.get());
102     gdk_threads_enter();
103
104     m_runLoop.clear();
105
106     g_signal_handler_disconnect(m_popup->platformMenu(), unmapHandler);
107
108     if (!m_client)
109         return;
110
111     m_client->valueChangedForPopupMenu(this, m_activeItem);
112 }
113
114 void WebPopupMenuProxyGtk::hidePopupMenu()
115 {
116     m_popup->popDown();
117 }
118
119 void WebPopupMenuProxyGtk::shutdownRunLoop()
120 {
121     if (g_main_loop_is_running(m_runLoop.get()))
122         g_main_loop_quit(m_runLoop.get());
123 }
124
125 void WebPopupMenuProxyGtk::menuItemActivated(GtkAction* action, WebPopupMenuProxyGtk* popupMenu)
126 {
127     popupMenu->setActiveItem(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "popup-menu-action-index")));
128     popupMenu->shutdownRunLoop();
129 }
130
131 void WebPopupMenuProxyGtk::menuUnmapped(GtkWidget*, WebPopupMenuProxyGtk* popupMenu)
132 {
133     popupMenu->shutdownRunLoop();
134 }
135
136 } // namespace WebKit