2 * Copyright (C) 2006 Zack Rusin <zack@kde.org>
3 * Copyright (C) 2007 Ryan Leavengood <leavengood@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "ContextMenuItem.h"
30 #include "ContextMenu.h"
31 #include "NotImplemented.h"
39 using namespace WebCore;
41 ContextMenuItem::ContextMenuItem(PlatformMenuItemDescription item)
43 m_platformDescription = item;
46 ContextMenuItem::ContextMenuItem(ContextMenu* subMenu)
48 m_platformDescription = new BMenuItem(subMenu->platformDescription(),
49 new BMessage(ContextMenuItemTagNoAction));
52 ContextMenuItem::ContextMenuItem(ContextMenuItemType type, ContextMenuAction action,
53 const String& title, ContextMenu* subMenu)
55 if (type == ActionType)
56 m_platformDescription = new BMenuItem(BString(title).String(), new BMessage(action));
57 else if (type == SeparatorType)
58 m_platformDescription = new BSeparatorItem();
60 m_platformDescription = new BMenuItem(subMenu->platformDescription(), new BMessage(action));
61 m_platformDescription->SetLabel(BString(title).String());
65 ContextMenuItem::~ContextMenuItem()
67 delete m_platformDescription;
70 PlatformMenuItemDescription ContextMenuItem::releasePlatformDescription()
72 BMenuItem* item = m_platformDescription;
73 m_platformDescription = 0;
77 ContextMenuItemType ContextMenuItem::type() const
79 if (dynamic_cast<BSeparatorItem*>(m_platformDescription))
81 if (m_platformDescription->Submenu())
86 void ContextMenuItem::setType(ContextMenuItemType type)
88 ContextMenuAction theAction = action();
89 String theTitle = title();
90 BMenu* subMenu = platformSubMenu();
91 delete m_platformDescription;
93 if (type == ActionType)
94 m_platformDescription = new BMenuItem(BString(theTitle).String(), new BMessage(theAction));
95 else if (type == SeparatorType)
96 m_platformDescription = new BSeparatorItem();
99 m_platformDescription = new BMenuItem(subMenu, new BMessage(theAction));
100 m_platformDescription->SetLabel(BString(theTitle).String());
102 m_platformDescription = new BMenuItem(BString(theTitle).String(), new BMessage(theAction));
106 ContextMenuAction ContextMenuItem::action() const
108 if (!m_platformDescription)
109 return ContextMenuItemTagNoAction;
110 return static_cast<WebCore::ContextMenuAction>(m_platformDescription->Message()->what);
113 void ContextMenuItem::setAction(ContextMenuAction action)
115 if (m_platformDescription)
116 m_platformDescription->Message()->what = action;
119 String ContextMenuItem::title() const
121 if (m_platformDescription)
123 return BString(m_platformDescription->Label());
126 void ContextMenuItem::setTitle(const String& title)
128 // FIXME: We need to find a better way to convert WebKit Strings into c strings
129 m_platformDescription->SetLabel(BString(title).String());
132 PlatformMenuDescription ContextMenuItem::platformSubMenu() const
134 return m_platformDescription->Submenu();
137 void ContextMenuItem::setSubMenu(ContextMenu* menu)
139 // FIXME: We assume m_platformDescription is valid
140 const char* title = m_platformDescription->Label();
141 delete m_platformDescription;
142 m_platformDescription = new BMenuItem(menu->platformDescription(), new BMessage(action()));
143 m_platformDescription->SetLabel(title);
146 void ContextMenuItem::setChecked(bool checked)
148 if (m_platformDescription)
149 m_platformDescription->SetMarked(checked);
152 void ContextMenuItem::setEnabled(bool enable)
154 if (m_platformDescription)
155 m_platformDescription->SetEnabled(enable);
158 bool ContextMenuItem::enabled() const
160 if (!m_platformDescription)
162 return m_platformDescription->IsEnabled();