[GLIB] Use GUniquePtr instead of GOwnPtr
[WebKit-https.git] / Source / WebCore / platform / gtk / PopupMenuGtk.cpp
1 /*
2  * This file is part of the popup menu implementation for <select> elements in WebCore.
3  *
4  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
5  * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
6  * Copyright (C) 2008 Collabora Ltd.
7  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8  * Copyright (C) 2010 Igalia S.L.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public License
21  * along with this library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  *
25  */
26
27 #include "config.h"
28 #include "PopupMenuGtk.h"
29
30 #include "FrameView.h"
31 #include "GUniquePtrGtk.h"
32 #include "GtkUtilities.h"
33 #include "HostWindow.h"
34 #include <gtk/gtk.h>
35 #include <wtf/text/CString.h>
36
37 namespace WebCore {
38
39 PopupMenuGtk::PopupMenuGtk(PopupMenuClient* client)
40     : m_popupClient(client)
41 {
42 }
43
44 PopupMenuGtk::~PopupMenuGtk()
45 {
46     if (m_popup) {
47         g_signal_handlers_disconnect_matched(m_popup->platformMenu(), G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this);
48         hide();
49     }
50 }
51
52 GtkAction* PopupMenuGtk::createGtkActionForMenuItem(int itemIndex)
53 {
54     GUniquePtr<char> actionName(g_strdup_printf("popup-menu-action-%d", itemIndex));
55     GtkAction* action = gtk_action_new(actionName.get(), client()->itemText(itemIndex).utf8().data(), client()->itemToolTip(itemIndex).utf8().data(), 0);
56     g_object_set_data(G_OBJECT(action), "popup-menu-action-index", GINT_TO_POINTER(itemIndex));
57     g_signal_connect(action, "activate", G_CALLBACK(menuItemActivated), this);
58     // FIXME: Apply the PopupMenuStyle from client()->itemStyle(i)
59     gtk_action_set_visible(action, !client()->itemStyle(itemIndex).isDisplayNone());
60     gtk_action_set_sensitive(action, client()->itemIsEnabled(itemIndex));
61
62     return action;
63 }
64
65 void PopupMenuGtk::show(const IntRect& rect, FrameView* view, int index)
66 {
67     ASSERT(client());
68
69     if (!m_popup) {
70         m_popup = GtkPopupMenu::create();
71         g_signal_connect(m_popup->platformMenu(), "unmap", G_CALLBACK(PopupMenuGtk::menuUnmapped), this);
72     } else
73         m_popup->clear();
74
75     const int size = client()->listSize();
76     for (int i = 0; i < size; ++i) {
77         if (client()->itemIsSeparator(i))
78             m_popup->appendSeparator();
79         else {
80             GRefPtr<GtkAction> action = adoptGRef(createGtkActionForMenuItem(i));
81             m_popup->appendItem(action.get());
82         }
83     }
84
85     IntPoint menuPosition = convertWidgetPointToScreenPoint(GTK_WIDGET(view->hostWindow()->platformPageClient()), view->contentsToWindow(rect.location()));
86     menuPosition.move(0, rect.height());
87
88     GUniquePtr<GdkEvent> currentEvent(gtk_get_current_event());
89     m_popup->popUp(rect.size(), menuPosition, size, index, currentEvent.get());
90
91     // GTK can refuse to actually open the menu when mouse grabs fails.
92     // Ensure WebCore does not go into some pesky state.
93     if (!gtk_widget_get_visible(m_popup->platformMenu()))
94         client()->popupDidHide();
95 }
96
97 void PopupMenuGtk::hide()
98 {
99     ASSERT(m_popup);
100     m_popup->popDown();
101 }
102
103 void PopupMenuGtk::updateFromElement()
104 {
105     client()->setTextFromItem(client()->selectedIndex());
106 }
107
108 void PopupMenuGtk::disconnectClient()
109 {
110     m_popupClient = 0;
111 }
112
113 void PopupMenuGtk::menuItemActivated(GtkAction* action, PopupMenuGtk* that)
114 {
115     ASSERT(that->client());
116     that->client()->valueChanged(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(action), "popup-menu-action-index")));
117 }
118
119 void PopupMenuGtk::menuUnmapped(GtkWidget*, PopupMenuGtk* that)
120 {
121     ASSERT(that->client());
122     that->client()->popupDidHide();
123 }
124
125 }
126