• 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 "ContextMenu.h"
28
29#include "ContextMenuController.h"
30
31@interface WebCoreMenuTarget : NSObject {
32    WebCore::ContextMenuController* _menuController;
33}
34+ (WebCoreMenuTarget*)sharedMenuTarget;
35- (WebCore::ContextMenuController*)menuController;
36- (void)setMenuController:(WebCore::ContextMenuController*)menuController;
37- (void)forwardContextMenuAction:(id)sender;
38- (BOOL)validateMenuItem:(NSMenuItem *)item;
39@end
40
41static WebCoreMenuTarget* target;
42
43@implementation WebCoreMenuTarget
44
45+ (WebCoreMenuTarget*)sharedMenuTarget
46{
47    if (!target)
48        target = [[WebCoreMenuTarget alloc] init];
49    return target;
50}
51
52- (WebCore::ContextMenuController*)menuController
53{
54    return _menuController;
55}
56
57- (void)setMenuController:(WebCore::ContextMenuController*)menuController
58{
59    _menuController = menuController;
60}
61
62- (void)forwardContextMenuAction:(id)sender
63{
64    WebCore::ContextMenuItem item(WebCore::ActionType, static_cast<WebCore::ContextMenuAction>([sender tag]), [sender title]);
65    _menuController->contextMenuItemSelected(&item);
66}
67
68- (BOOL)validateMenuItem:(NSMenuItem *)item
69{
70    WebCore::ContextMenuItem coreItem(item);
71    ASSERT(_menuController->contextMenu());
72    _menuController->contextMenu()->checkOrEnableIfNeeded(coreItem);
73    return coreItem.enabled();
74}
75
76@end
77
78namespace WebCore {
79
80ContextMenu::ContextMenu(const HitTestResult& result)
81    : m_hitTestResult(result)
82{
83    NSMutableArray* array = [[NSMutableArray alloc] init];
84    m_platformDescription = array;
85    [array release];
86
87    [[WebCoreMenuTarget sharedMenuTarget] setMenuController:controller()];
88}
89
90ContextMenu::ContextMenu(const HitTestResult& result, const PlatformMenuDescription menu)
91    : m_hitTestResult(result)
92    , m_platformDescription(menu)
93{
94    [[WebCoreMenuTarget sharedMenuTarget] setMenuController:controller()];
95}
96
97ContextMenu::~ContextMenu()
98{
99}
100
101static void setMenuItemTarget(NSMenuItem* menuItem)
102{
103    [menuItem setTarget:[WebCoreMenuTarget sharedMenuTarget]];
104    [menuItem setAction:@selector(forwardContextMenuAction:)];
105}
106
107void ContextMenu::appendItem(ContextMenuItem& item)
108{
109    checkOrEnableIfNeeded(item);
110
111    ContextMenuItemType type = item.type();
112    NSMenuItem* platformItem = item.releasePlatformDescription();
113    if (type == ActionType)
114        setMenuItemTarget(platformItem);
115
116    [m_platformDescription.get() addObject:platformItem];
117    [platformItem release];
118}
119
120void ContextMenu::insertItem(unsigned position, ContextMenuItem& item)
121{
122    checkOrEnableIfNeeded(item);
123
124    ContextMenuItemType type = item.type();
125    NSMenuItem* platformItem = item.releasePlatformDescription();
126    if (type == ActionType)
127        setMenuItemTarget(platformItem);
128
129    [m_platformDescription.get() insertObject:platformItem atIndex:position];
130    [platformItem release];
131}
132
133unsigned ContextMenu::itemCount() const
134{
135    return [m_platformDescription.get() count];
136}
137
138void ContextMenu::setPlatformDescription(NSMutableArray* menu)
139{
140    if (m_platformDescription.get() != menu)
141        m_platformDescription = menu;
142}
143
144NSMutableArray* ContextMenu::platformDescription() const
145{
146    return m_platformDescription.get();
147}
148
149NSMutableArray* ContextMenu::releasePlatformDescription()
150{
151    return m_platformDescription.releaseRef();
152}
153
154}
155