2 * Copyright (C) 2011 Igalia S.L.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WebPopupMenuProxyGtk.h"
29 #include "NativeWebMouseEvent.h"
30 #include "WebPopupItem.h"
31 #include <WebCore/GtkUtilities.h>
33 #include <wtf/gobject/GOwnPtr.h>
34 #include <wtf/text/CString.h>
36 using namespace WebCore;
40 WebPopupMenuProxyGtk::WebPopupMenuProxyGtk(GtkWidget* webView, WebPopupMenuProxy::Client* client)
41 : WebPopupMenuProxy(client)
47 WebPopupMenuProxyGtk::~WebPopupMenuProxyGtk()
50 g_signal_handlers_disconnect_matched(m_popup->platformMenu(), G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this);
55 GtkAction* WebPopupMenuProxyGtk::createGtkActionForMenuItem(const WebPopupItem& item, int itemIndex)
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);
66 void WebPopupMenuProxyGtk::showPopupMenu(const IntRect& rect, TextDirection textDirection, double pageScaleFactor, const Vector<WebPopupItem>& items, const PlatformPopupMenuData& data, int32_t selectedIndex)
71 m_popup = GtkPopupMenu::create();
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();
78 GRefPtr<GtkAction> action = adoptGRef(createGtkActionForMenuItem(items[i], i));
79 m_popup->appendItem(action.get());
83 IntPoint menuPosition = convertWidgetPointToScreenPoint(m_webView, rect.location());
84 menuPosition.move(0, rect.height());
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);
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();
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));
100 g_main_loop_run(m_runLoop.get());
104 g_signal_handler_disconnect(m_popup->platformMenu(), unmapHandler);
109 m_client->valueChangedForPopupMenu(this, m_activeItem);
112 void WebPopupMenuProxyGtk::hidePopupMenu()
117 void WebPopupMenuProxyGtk::shutdownRunLoop()
119 if (g_main_loop_is_running(m_runLoop.get()))
120 g_main_loop_quit(m_runLoop.get());
123 void WebPopupMenuProxyGtk::menuItemActivated(GtkAction* action, WebPopupMenuProxyGtk* popupMenu)
125 popupMenu->setActiveItem(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "popup-menu-action-index")));
126 popupMenu->shutdownRunLoop();
129 void WebPopupMenuProxyGtk::menuUnmapped(GtkWidget*, WebPopupMenuProxyGtk* popupMenu)
131 popupMenu->shutdownRunLoop();
134 } // namespace WebKit