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 ContextMenuItem::ContextMenuItem(ContextMenuItemType type, ContextMenuAction action, const String& title, ContextMenu* subMenu)
63 if (type == SeparatorType) {
64 m_platformDescription = [NSMenuItem separatorItem];
68 NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:title action:nil keyEquivalent:@""];
69 m_platformDescription = item;
72 [m_platformDescription.get() setTag:action];
77 ContextMenuItem::~ContextMenuItem()
81 NSMenuItem* ContextMenuItem::releasePlatformDescription()
83 NSMenuItem* item = [m_platformDescription.get() retain];
84 m_platformDescription = 0;
88 ContextMenuItemType ContextMenuItem::type() const
90 if ([m_platformDescription.get() isSeparatorItem])
92 if ([m_platformDescription.get() hasSubmenu])
97 ContextMenuAction ContextMenuItem::action() const
99 return static_cast<ContextMenuAction>([m_platformDescription.get() tag]);
102 String ContextMenuItem::title() const
104 return [m_platformDescription.get() title];
107 NSMutableArray* ContextMenuItem::platformSubMenu() const
109 return menuToArray([m_platformDescription.get() submenu]);
112 void ContextMenuItem::setType(ContextMenuItemType type)
114 if (type == SeparatorType)
115 m_platformDescription = [NSMenuItem separatorItem];
118 void ContextMenuItem::setAction(ContextMenuAction action)
120 [m_platformDescription.get() setTag:action];
123 void ContextMenuItem::setTitle(const String& title)
125 [m_platformDescription.get() setTitle:title];
128 void ContextMenuItem::setSubMenu(ContextMenu* menu)
130 NSArray* subMenuArray = menu->platformDescription();
131 NSMenu* subMenu = [[NSMenu alloc] init];
132 [subMenu setAutoenablesItems:NO];
133 for (unsigned i = 0; i < [subMenuArray count]; i++)
134 [subMenu insertItem:[subMenuArray objectAtIndex:i] atIndex:i];
135 [m_platformDescription.get() setSubmenu:subMenu];
139 void ContextMenuItem::setChecked(bool checked)
142 [m_platformDescription.get() setState:NSOnState];
144 [m_platformDescription.get() setState:NSOffState];
147 void ContextMenuItem::setEnabled(bool enable)
149 [m_platformDescription.get() setEnabled:enable];
152 bool ContextMenuItem::enabled() const
154 return [m_platformDescription.get() isEnabled];
157 } // namespace WebCore
159 #endif // ENABLE(CONTEXT_MENUS)