2
* Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
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.
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.
27
#include "ContextMenuItem.h"
29
#if ENABLE(CONTEXT_MENUS)
31
#include "ContextMenu.h"
35
static NSMutableArray* menuToArray(NSMenu* menu)
37
NSMutableArray* itemsArray = [NSMutableArray array];
38
int total = [menu numberOfItems];
39
for (int i = 0; i < total; i++)
40
[itemsArray addObject:[menu itemAtIndex:i]];
45
ContextMenuItem::ContextMenuItem(NSMenuItem* item)
47
m_platformDescription = item;
50
ContextMenuItem::ContextMenuItem(ContextMenu* subMenu)
52
NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
53
m_platformDescription = item;
56
[m_platformDescription.get() setTag:ContextMenuItemTagNoAction];
61
static PlatformMenuItemDescription createPlatformMenuItemDescription(ContextMenuItemType type, ContextMenuAction action, const String& title, bool enabled, bool checked)
63
if (type == SeparatorType)
64
return [[NSMenuItem separatorItem] retain];
66
NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:title action:nil keyEquivalent:@""];
67
[item setEnabled:enabled];
68
[item setState:checked ? NSOnState : NSOffState];
74
ContextMenuItem::ContextMenuItem(ContextMenuItemType type, ContextMenuAction action, const String& title, ContextMenu* subMenu)
76
m_platformDescription.adoptNS(createPlatformMenuItemDescription(type, action, title, true, false));
82
ContextMenuItem::ContextMenuItem(ContextMenuItemType type, ContextMenuAction action, const String& title, bool enabled, bool checked)
84
m_platformDescription.adoptNS(createPlatformMenuItemDescription(type, action, title, enabled, checked));
87
ContextMenuItem::ContextMenuItem(ContextMenuAction action, const String& title, bool enabled, bool checked, Vector<ContextMenuItem>& subMenuItems)
89
m_platformDescription.adoptNS(createPlatformMenuItemDescription(SubmenuType, action, title, enabled, checked));
91
setSubMenu(subMenuItems);
94
ContextMenuItem::~ContextMenuItem()
98
NSMenuItem* ContextMenuItem::releasePlatformDescription()
100
NSMenuItem* item = [m_platformDescription.get() retain];
101
m_platformDescription = 0;
105
ContextMenuItemType ContextMenuItem::type() const
107
if ([m_platformDescription.get() isSeparatorItem])
108
return SeparatorType;
109
if ([m_platformDescription.get() hasSubmenu])
114
ContextMenuAction ContextMenuItem::action() const
116
return static_cast<ContextMenuAction>([m_platformDescription.get() tag]);
119
String ContextMenuItem::title() const
121
return [m_platformDescription.get() title];
124
NSMutableArray* ContextMenuItem::platformSubMenu() const
126
return menuToArray([m_platformDescription.get() submenu]);
129
void ContextMenuItem::setType(ContextMenuItemType type)
131
if (type == SeparatorType)
132
m_platformDescription = [NSMenuItem separatorItem];
135
void ContextMenuItem::setAction(ContextMenuAction action)
137
[m_platformDescription.get() setTag:action];
140
void ContextMenuItem::setTitle(const String& title)
142
[m_platformDescription.get() setTitle:title];
145
void ContextMenuItem::setSubMenu(ContextMenu* menu)
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];
156
void ContextMenuItem::setSubMenu(Vector<ContextMenuItem>& subMenuItems)
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()];
163
[m_platformDescription.get() setSubmenu:subMenu];
167
void ContextMenuItem::setChecked(bool checked)
170
[m_platformDescription.get() setState:NSOnState];
172
[m_platformDescription.get() setState:NSOffState];
175
void ContextMenuItem::setEnabled(bool enable)
177
[m_platformDescription.get() setEnabled:enable];
180
bool ContextMenuItem::enabled() const
182
return [m_platformDescription.get() isEnabled];
185
bool ContextMenuItem::checked() const
187
return [m_platformDescription.get() state] == NSOnState;
190
} // namespace WebCore
192
#endif // ENABLE(CONTEXT_MENUS)