OSDN Git Service

Merge webkit.org at R49305 : Automatic merge by git.
[android-x86/external-webkit.git] / WebCore / platform / mac / ContextMenuItemMac.mm
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
33 namespace WebCore {
34
35 static 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
45 ContextMenuItem::ContextMenuItem(NSMenuItem* item)
46 {
47     m_platformDescription = item;
48 }
49
50 ContextMenuItem::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
61 ContextMenuItem::ContextMenuItem(ContextMenuItemType type, ContextMenuAction action, const String& title, ContextMenu* subMenu)
62 {
63     if (type == SeparatorType) {
64         m_platformDescription = [NSMenuItem separatorItem];
65         return;
66     }
67
68     NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:title action:nil keyEquivalent:@""];
69     m_platformDescription = item;
70     [item release];
71
72     [m_platformDescription.get() setTag:action];
73     if (subMenu)
74         setSubMenu(subMenu);
75 }
76
77 ContextMenuItem::~ContextMenuItem()
78 {
79 }
80
81 NSMenuItem* ContextMenuItem::releasePlatformDescription()
82 {
83     NSMenuItem* item = [m_platformDescription.get() retain];
84     m_platformDescription = 0;
85     return item;
86 }
87
88 ContextMenuItemType ContextMenuItem::type() const
89 {
90     if ([m_platformDescription.get() isSeparatorItem])
91         return SeparatorType;
92     if ([m_platformDescription.get() hasSubmenu])
93         return SubmenuType;
94     return ActionType;
95 }
96
97 ContextMenuAction ContextMenuItem::action() const
98
99     return static_cast<ContextMenuAction>([m_platformDescription.get() tag]);
100 }
101
102 String ContextMenuItem::title() const 
103 {
104     return [m_platformDescription.get() title];
105 }
106
107 NSMutableArray* ContextMenuItem::platformSubMenu() const
108 {
109     return menuToArray([m_platformDescription.get() submenu]);
110 }
111
112 void ContextMenuItem::setType(ContextMenuItemType type)
113 {
114     if (type == SeparatorType)
115         m_platformDescription = [NSMenuItem separatorItem];
116 }
117
118 void ContextMenuItem::setAction(ContextMenuAction action)
119 {
120     [m_platformDescription.get() setTag:action]; 
121 }
122
123 void ContextMenuItem::setTitle(const String& title)
124 {
125     [m_platformDescription.get() setTitle:title];
126 }
127
128 void ContextMenuItem::setSubMenu(ContextMenu* menu)
129 {
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];
136     [subMenu release];
137 }
138
139 void ContextMenuItem::setChecked(bool checked)
140 {
141     if (checked)
142         [m_platformDescription.get() setState:NSOnState];
143     else
144         [m_platformDescription.get() setState:NSOffState];
145 }
146
147 void ContextMenuItem::setEnabled(bool enable)
148 {
149     [m_platformDescription.get() setEnabled:enable];
150 }
151
152 bool ContextMenuItem::enabled() const
153 {
154     return [m_platformDescription.get() isEnabled];
155 }
156
157 } // namespace WebCore
158
159 #endif // ENABLE(CONTEXT_MENUS)