1/*
2 *  Copyright (C) 2007 Holger Hans Peter Freyther
3 * Portions Copyright (c) 2010 Motorola Mobility, Inc.  All rights reserved.
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Lesser General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public
16 *  License along with this library; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#include "config.h"
21
22#if ENABLE(CONTEXT_MENUS)
23
24#include "ContextMenu.h"
25
26#include <gtk/gtk.h>
27#include <wtf/gobject/GRefPtr.h>
28#include <wtf/gobject/GUniquePtr.h>
29
30namespace WebCore {
31
32ContextMenu::ContextMenu()
33{
34    m_platformDescription = GTK_MENU(gtk_menu_new());
35}
36
37ContextMenu::ContextMenu(const PlatformMenuDescription menu)
38    : m_platformDescription(menu)
39{
40}
41
42ContextMenu::~ContextMenu()
43{
44    if (m_platformDescription)
45        gtk_widget_destroy(GTK_WIDGET(m_platformDescription));
46}
47
48void ContextMenu::appendItem(ContextMenuItem& item)
49{
50    ASSERT(m_platformDescription);
51
52    GRefPtr<GtkWidget> platformItem = GTK_WIDGET(item.releasePlatformDescription());
53    ASSERT(platformItem);
54
55    if (GtkWidget* parent = gtk_widget_get_parent(platformItem.get()))
56        gtk_container_remove(GTK_CONTAINER(parent), platformItem.get());
57
58    gtk_menu_shell_append(GTK_MENU_SHELL(m_platformDescription), platformItem.get());
59    gtk_widget_show(platformItem.get());
60}
61
62void ContextMenu::setPlatformDescription(PlatformMenuDescription menu)
63{
64    ASSERT(menu);
65    if (m_platformDescription == menu)
66        return;
67    if (m_platformDescription)
68        gtk_widget_destroy(GTK_WIDGET(m_platformDescription));
69
70    m_platformDescription = menu;
71}
72
73PlatformMenuDescription ContextMenu::platformDescription() const
74{
75    return m_platformDescription;
76}
77
78PlatformMenuDescription ContextMenu::releasePlatformDescription()
79{
80    PlatformMenuDescription description = m_platformDescription;
81    m_platformDescription = 0;
82
83    return description;
84}
85
86unsigned ContextMenu::itemCount() const
87{
88    ASSERT(m_platformDescription);
89
90    GUniquePtr<GList> children(gtk_container_get_children(GTK_CONTAINER(m_platformDescription)));
91    return g_list_length(children.get());
92}
93
94Vector<ContextMenuItem> contextMenuItemVector(const PlatformMenuDescription menu)
95{
96    Vector<ContextMenuItem> menuItemVector;
97
98    GUniquePtr<GList> children(gtk_container_get_children(GTK_CONTAINER(menu)));
99    int itemCount = g_list_length(children.get());
100    menuItemVector.reserveCapacity(itemCount);
101
102    for (GList* item = children.get(); item; item = g_list_next(item)) {
103        GtkWidget* widget = static_cast<GtkWidget*>(item->data);
104        if (!GTK_IS_MENU_ITEM(widget))
105            continue;
106        menuItemVector.append(GTK_MENU_ITEM(widget));
107    }
108
109    return menuItemVector;
110}
111
112PlatformMenuDescription platformMenuDescription(Vector<ContextMenuItem>& subMenuItems)
113{
114    GtkMenu* menu = GTK_MENU(gtk_menu_new());
115    for (size_t i = 0; i < subMenuItems.size(); i++) {
116        GtkWidget* platformItem = GTK_WIDGET(subMenuItems[i].releasePlatformDescription());
117        gtk_menu_shell_append(GTK_MENU_SHELL(menu), platformItem);
118        gtk_widget_show(platformItem);
119    }
120    return menu;
121}
122
123}
124
125#endif // ENABLE(CONTEXT_MENUS)
126