• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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#include "ContextMenu.h"
30
31namespace WebCore {
32
33static NSMutableArray* menuToArray(NSMenu* menu)
34{
35    NSMutableArray* itemsArray = [NSMutableArray array];
36    int total = [menu numberOfItems];
37    for (int i = 0; i < total; i++)
38        [itemsArray addObject:[menu itemAtIndex:i]];
39
40    return itemsArray;
41}
42
43ContextMenuItem::ContextMenuItem(NSMenuItem* item)
44{
45    m_platformDescription = item;
46}
47
48ContextMenuItem::ContextMenuItem(ContextMenu* subMenu)
49{
50    NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
51    m_platformDescription = item;
52    [item release];
53
54    [m_platformDescription.get() setTag:ContextMenuItemTagNoAction];
55    if (subMenu)
56        setSubMenu(subMenu);
57}
58
59ContextMenuItem::ContextMenuItem(ContextMenuItemType type, ContextMenuAction action, const String& title, ContextMenu* subMenu)
60{
61    if (type == SeparatorType) {
62        m_platformDescription = [NSMenuItem separatorItem];
63        return;
64    }
65
66    NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:title action:nil keyEquivalent:@""];
67    m_platformDescription = item;
68    [item release];
69
70    [m_platformDescription.get() setTag:action];
71    if (subMenu)
72        setSubMenu(subMenu);
73}
74
75ContextMenuItem::~ContextMenuItem()
76{
77}
78
79NSMenuItem* ContextMenuItem::releasePlatformDescription()
80{
81    NSMenuItem* item = [m_platformDescription.get() retain];
82    m_platformDescription = 0;
83    return item;
84}
85
86ContextMenuItemType ContextMenuItem::type() const
87{
88    if ([m_platformDescription.get() isSeparatorItem])
89        return SeparatorType;
90    if ([m_platformDescription.get() hasSubmenu])
91        return SubmenuType;
92    return ActionType;
93}
94
95ContextMenuAction ContextMenuItem::action() const
96{
97    return static_cast<ContextMenuAction>([m_platformDescription.get() tag]);
98}
99
100String ContextMenuItem::title() const
101{
102    return [m_platformDescription.get() title];
103}
104
105NSMutableArray* ContextMenuItem::platformSubMenu() const
106{
107    return menuToArray([m_platformDescription.get() submenu]);
108}
109
110void ContextMenuItem::setType(ContextMenuItemType type)
111{
112    if (type == SeparatorType)
113        m_platformDescription = [NSMenuItem separatorItem];
114}
115
116void ContextMenuItem::setAction(ContextMenuAction action)
117{
118    [m_platformDescription.get() setTag:action];
119}
120
121void ContextMenuItem::setTitle(const String& title)
122{
123    [m_platformDescription.get() setTitle:title];
124}
125
126void ContextMenuItem::setSubMenu(ContextMenu* menu)
127{
128    NSArray* subMenuArray = menu->platformDescription();
129    NSMenu* subMenu = [[NSMenu alloc] init];
130    [subMenu setAutoenablesItems:NO];
131    for (unsigned i = 0; i < [subMenuArray count]; i++)
132        [subMenu insertItem:[subMenuArray objectAtIndex:i] atIndex:i];
133    [m_platformDescription.get() setSubmenu:subMenu];
134    [subMenu release];
135}
136
137void ContextMenuItem::setChecked(bool checked)
138{
139    if (checked)
140        [m_platformDescription.get() setState:NSOnState];
141    else
142        [m_platformDescription.get() setState:NSOffState];
143}
144
145void ContextMenuItem::setEnabled(bool enable)
146{
147    [m_platformDescription.get() setEnabled:enable];
148}
149
150bool ContextMenuItem::enabled() const
151{
152    return [m_platformDescription.get() isEnabled];
153}
154
155}
156