1 /*
2 * Copyright (C) 2007 Holger Hans Peter Freyther
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser 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 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "config.h"
20 #include "ContextMenu.h"
21
22 #include "ContextMenuController.h"
23
24 #include <gtk/gtk.h>
25
26 namespace WebCore {
27
28 // TODO: ref-counting correctness checking.
29 // See http://bugs.webkit.org/show_bug.cgi?id=16115
30
menuItemActivated(GtkMenuItem * item,ContextMenuController * controller)31 static void menuItemActivated(GtkMenuItem* item, ContextMenuController* controller)
32 {
33 ContextMenuItem contextItem(item);
34 controller->contextMenuItemSelected(&contextItem);
35 }
36
ContextMenu(const HitTestResult & result)37 ContextMenu::ContextMenu(const HitTestResult& result)
38 : m_hitTestResult(result)
39 {
40 m_platformDescription = GTK_MENU(gtk_menu_new());
41
42 g_object_ref_sink(G_OBJECT(m_platformDescription));
43 }
44
~ContextMenu()45 ContextMenu::~ContextMenu()
46 {
47 if (m_platformDescription)
48 g_object_unref(m_platformDescription);
49 }
50
appendItem(ContextMenuItem & item)51 void ContextMenu::appendItem(ContextMenuItem& item)
52 {
53 ASSERT(m_platformDescription);
54 checkOrEnableIfNeeded(item);
55
56 ContextMenuItemType type = item.type();
57 GtkMenuItem* platformItem = ContextMenuItem::createNativeMenuItem(item.releasePlatformDescription());
58 ASSERT(platformItem);
59
60 if (type == ActionType || type == CheckableActionType)
61 g_signal_connect(platformItem, "activate", G_CALLBACK(menuItemActivated), controller());
62
63 gtk_menu_shell_append(GTK_MENU_SHELL(m_platformDescription), GTK_WIDGET(platformItem));
64 gtk_widget_show(GTK_WIDGET(platformItem));
65 }
66
setPlatformDescription(PlatformMenuDescription menu)67 void ContextMenu::setPlatformDescription(PlatformMenuDescription menu)
68 {
69 ASSERT(menu);
70 if (m_platformDescription)
71 g_object_unref(m_platformDescription);
72
73 m_platformDescription = menu;
74 g_object_ref(m_platformDescription);
75 }
76
platformDescription() const77 PlatformMenuDescription ContextMenu::platformDescription() const
78 {
79 return m_platformDescription;
80 }
81
releasePlatformDescription()82 PlatformMenuDescription ContextMenu::releasePlatformDescription()
83 {
84 PlatformMenuDescription description = m_platformDescription;
85 m_platformDescription = 0;
86
87 return description;
88 }
89
90 }
91