1/*
2 * Copyright (C) 2017 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitPopupMenu.h"
22
23#include "NativeWebMouseEvent.h"
24#include "WebKitOptionMenuPrivate.h"
25#include "WebKitWebViewPrivate.h"
26
27namespace WebKit {
28using namespace WebCore;
29
30WebKitPopupMenu::WebKitPopupMenu(GtkWidget* webView, WebPopupMenuProxy::Client& client)
31 : WebPopupMenuProxyGtk(webView, client)
32{
33}
34
35static void menuCloseCallback(WebKitPopupMenu* popupMenu)
36{
37 popupMenu->activateItem(WTF::nullopt);
38}
39
40void WebKitPopupMenu::showPopupMenu(const IntRect& rect, TextDirection direction, double pageScaleFactor, const Vector<WebPopupItem>& items, const PlatformPopupMenuData& platformData, int32_t selectedIndex)
41{
42 GRefPtr<WebKitOptionMenu> menu = adoptGRef(webkitOptionMenuCreate(*this, items, selectedIndex));
43 const GdkEvent* event = m_client->currentlyProcessedMouseDownEvent() ? m_client->currentlyProcessedMouseDownEvent()->nativeEvent() : nullptr;
44 if (webkitWebViewShowOptionMenu(WEBKIT_WEB_VIEW(m_webView), rect, menu.get(), event)) {
45 m_menu = WTFMove(menu);
46 g_signal_connect_swapped(m_menu.get(), "close", G_CALLBACK(menuCloseCallback), this);
47 } else
48 WebPopupMenuProxyGtk::showPopupMenu(rect, direction, pageScaleFactor, items, platformData, selectedIndex);
49}
50
51void WebKitPopupMenu::hidePopupMenu()
52{
53 if (!m_menu) {
54 WebPopupMenuProxyGtk::hidePopupMenu();
55 return;
56 }
57 g_signal_handlers_disconnect_matched(m_menu.get(), G_SIGNAL_MATCH_DATA, 0, 0, nullptr, nullptr, this);
58 webkit_option_menu_close(m_menu.get());
59}
60
61void WebKitPopupMenu::cancelTracking()
62{
63 if (!m_menu) {
64 WebPopupMenuProxyGtk::cancelTracking();
65 return;
66 }
67 hidePopupMenu();
68 m_menu = nullptr;
69}
70
71void WebKitPopupMenu::activateItem(Optional<unsigned> itemIndex)
72{
73 WebPopupMenuProxyGtk::activateItem(itemIndex);
74 if (m_menu) {
75 g_signal_handlers_disconnect_matched(m_menu.get(), G_SIGNAL_MATCH_DATA, 0, 0, nullptr, nullptr, this);
76 m_menu = nullptr;
77 }
78}
79
80} // namespace WebKit
81