2 * Copyright (C) 2006, 2007 Apple 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 "ContextMenu.h"
31 #include "FrameView.h"
33 #include "NotImplemented.h"
35 #include <wtf/Vector.h>
36 #include <wtf/text/CString.h>
39 #define MIIM_FTYPE MIIM_TYPE
42 #define MIIM_STRING MIIM_TYPE
47 ContextMenu::ContextMenu(HMENU menu)
49 getContextMenuItems(menu, m_items);
52 void ContextMenu::getContextMenuItems(HMENU menu, Vector<ContextMenuItem>& items)
57 int count = ::GetMenuItemCount(menu);
61 for (int i = 0; i < count; ++i) {
62 MENUITEMINFO info = {0};
63 info.cbSize = sizeof(MENUITEMINFO);
64 info.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING | MIIM_STATE | MIIM_SUBMENU;
66 if (!::GetMenuItemInfo(menu, i, TRUE, &info))
69 if (info.fType == MFT_SEPARATOR) {
70 items.append(ContextMenuItem(SeparatorType, ContextMenuItemTagNoAction, String()));
74 int menuStringLength = info.cch + 1;
75 OwnArrayPtr<WCHAR> menuString = adoptArrayPtr(new WCHAR[menuStringLength]);
76 info.dwTypeData = menuString.get();
77 info.cch = menuStringLength;
79 if (::GetMenuItemInfo(menu, i, TRUE, &info))
80 items.append(ContextMenuItem(info));
85 HMENU ContextMenu::createNativeMenuFromItems(const Vector<ContextMenuItem>& items)
87 HMENU menu = ::CreatePopupMenu();
89 for (size_t i = 0; i < items.size(); ++i) {
90 const ContextMenuItem& item = items[i];
92 MENUITEMINFO menuItem = item.nativeMenuItem();
95 UINT flags = MF_BYPOSITION;
99 if (item.type() == SeparatorType)
100 flags |= MF_SEPARATOR;
103 flags |= item.checked() ? MF_CHECKED : MF_UNCHECKED;
104 flags |= item.enabled() ? MF_ENABLED : MF_GRAYED;
106 title = menuItem.dwTypeData;
107 menuItem.dwTypeData = 0;
109 if (menuItem.hSubMenu) {
111 newItem = reinterpret_cast<UINT>(menuItem.hSubMenu);
112 menuItem.hSubMenu = 0;
114 newItem = menuItem.wID;
117 ::InsertMenuW(menu, i, flags, newItem, title);
119 // ContextMenuItem::nativeMenuItem doesn't set the title of the MENUITEMINFO to make the
120 // lifetime handling easier for callers.
121 String itemTitle = item.title();
122 if (item.type() != SeparatorType) {
123 menuItem.fMask |= MIIM_STRING;
124 menuItem.cch = itemTitle.length();
125 menuItem.dwTypeData = const_cast<LPWSTR>(itemTitle.charactersWithNullTermination());
128 ::InsertMenuItem(menu, i, TRUE, &menuItem);
135 HMENU ContextMenu::nativeMenu() const
137 return createNativeMenuFromItems(m_items);
140 } // namespace WebCore