1/*
2 * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ContextMenuItem.h"
28
29#if ENABLE(CONTEXT_MENUS)
30
31#include "ContextMenu.h"
32
33namespace WebCore {
34
35static NSMutableArray* menuToArray(NSMenu* menu)
36{
37    NSMutableArray* itemsArray = [NSMutableArray array];
38    int total = [menu numberOfItems];
39    for (int i = 0; i < total; i++)
40        [itemsArray addObject:[menu itemAtIndex:i]];
41
42    return itemsArray;
43}
44
45ContextMenuItem::ContextMenuItem(NSMenuItem* item)
46{
47    m_platformDescription = item;
48}
49
50ContextMenuItem::ContextMenuItem(ContextMenu* subMenu)
51{
52    NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
53    m_platformDescription = item;
54    [item release];
55
56    [m_platformDescription.get() setTag:ContextMenuItemTagNoAction];
57    if (subMenu)
58        setSubMenu(subMenu);
59}
60
61static PlatformMenuItemDescription createPlatformMenuItemDescription(ContextMenuItemType type, ContextMenuAction action, const String& title, bool enabled, bool checked)
62{
63    if (type == SeparatorType)
64        return [[NSMenuItem separatorItem] retain];
65
66    NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:title action:nil keyEquivalent:@""];
67    [item setEnabled:enabled];
68    [item setState:checked ? NSOnState : NSOffState];
69    [item setTag:action];
70
71    return item;
72}
73
74ContextMenuItem::ContextMenuItem(ContextMenuItemType type, ContextMenuAction action, const String& title, ContextMenu* subMenu)
75{
76    m_platformDescription = adoptNS(createPlatformMenuItemDescription(type, action, title, true, false));
77
78    if (subMenu)
79        setSubMenu(subMenu);
80}
81
82ContextMenuItem::ContextMenuItem(ContextMenuItemType type, ContextMenuAction action, const String& title, bool enabled, bool checked)
83{
84    m_platformDescription = adoptNS(createPlatformMenuItemDescription(type, action, title, enabled, checked));
85}
86
87ContextMenuItem::ContextMenuItem(ContextMenuAction action, const String& title, bool enabled, bool checked, Vector<ContextMenuItem>& subMenuItems)
88{
89    m_platformDescription = adoptNS(createPlatformMenuItemDescription(SubmenuType, action, title, enabled, checked));
90
91    setSubMenu(subMenuItems);
92}
93
94ContextMenuItem::~ContextMenuItem()
95{
96}
97
98NSMenuItem* ContextMenuItem::releasePlatformDescription()
99{
100    NSMenuItem* item = [m_platformDescription.get() retain];
101    m_platformDescription = 0;
102    return item;
103}
104
105ContextMenuItemType ContextMenuItem::type() const
106{
107    if ([m_platformDescription.get() isSeparatorItem])
108        return SeparatorType;
109    if ([m_platformDescription.get() hasSubmenu])
110        return SubmenuType;
111    return ActionType;
112}
113
114ContextMenuAction ContextMenuItem::action() const
115{
116    return static_cast<ContextMenuAction>([m_platformDescription.get() tag]);
117}
118
119String ContextMenuItem::title() const
120{
121    return [m_platformDescription.get() title];
122}
123
124NSMutableArray* ContextMenuItem::platformSubMenu() const
125{
126    return menuToArray([m_platformDescription.get() submenu]);
127}
128
129void ContextMenuItem::setType(ContextMenuItemType type)
130{
131    if (type == SeparatorType)
132        m_platformDescription = [NSMenuItem separatorItem];
133}
134
135void ContextMenuItem::setAction(ContextMenuAction action)
136{
137    [m_platformDescription.get() setTag:action];
138}
139
140void ContextMenuItem::setTitle(const String& title)
141{
142    [m_platformDescription.get() setTitle:title];
143}
144
145void ContextMenuItem::setSubMenu(ContextMenu* menu)
146{
147    NSArray* subMenuArray = menu->platformDescription();
148    NSMenu* subMenu = [[NSMenu alloc] init];
149    [subMenu setAutoenablesItems:NO];
150    for (unsigned i = 0; i < [subMenuArray count]; i++)
151        [subMenu insertItem:[subMenuArray objectAtIndex:i] atIndex:i];
152    [m_platformDescription.get() setSubmenu:subMenu];
153    [subMenu release];
154}
155
156void ContextMenuItem::setSubMenu(Vector<ContextMenuItem>& subMenuItems)
157{
158    NSMenu* subMenu = [[NSMenu alloc] init];
159    [subMenu setAutoenablesItems:NO];
160    for (unsigned i = 0; i < subMenuItems.size(); ++i)
161        [subMenu addItem:subMenuItems[i].releasePlatformDescription()];
162
163    [m_platformDescription.get() setSubmenu:subMenu];
164    [subMenu release];
165}
166
167void ContextMenuItem::setChecked(bool checked)
168{
169    if (checked)
170        [m_platformDescription.get() setState:NSOnState];
171    else
172        [m_platformDescription.get() setState:NSOffState];
173}
174
175void ContextMenuItem::setEnabled(bool enable)
176{
177    [m_platformDescription.get() setEnabled:enable];
178}
179
180bool ContextMenuItem::enabled() const
181{
182    return [m_platformDescription.get() isEnabled];
183}
184
185bool ContextMenuItem::checked() const
186{
187    return [m_platformDescription.get() state] == NSOnState;
188}
189
190} // namespace WebCore
191
192#endif // ENABLE(CONTEXT_MENUS)
193