1 /*
2 * Copyright (C) 2006, 2007 Apple 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 "Document.h"
30 #include "Frame.h"
31 #include "FrameView.h"
32 #include "Node.h"
33 #include "NotImplemented.h"
34 #include <windows.h>
35 #include <wtf/Vector.h>
36 #include <wtf/text/CString.h>
37
38 #ifndef MIIM_FTYPE
39 #define MIIM_FTYPE MIIM_TYPE
40 #endif
41 #ifndef MIIM_STRING
42 #define MIIM_STRING MIIM_TYPE
43 #endif
44
45 namespace WebCore {
46
ContextMenu(HMENU menu)47 ContextMenu::ContextMenu(HMENU menu)
48 {
49 getContextMenuItems(menu, m_items);
50 }
51
getContextMenuItems(HMENU menu,Vector<ContextMenuItem> & items)52 void ContextMenu::getContextMenuItems(HMENU menu, Vector<ContextMenuItem>& items)
53 {
54 #if OS(WINCE)
55 notImplemented();
56 #else
57 int count = ::GetMenuItemCount(menu);
58 if (count <= 0)
59 return;
60
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;
65
66 if (!::GetMenuItemInfo(menu, i, TRUE, &info))
67 continue;
68
69 if (info.fType == MFT_SEPARATOR) {
70 items.append(ContextMenuItem(SeparatorType, ContextMenuItemTagNoAction, String()));
71 continue;
72 }
73
74 int menuStringLength = info.cch + 1;
75 OwnArrayPtr<WCHAR> menuString = adoptArrayPtr(new WCHAR[menuStringLength]);
76 info.dwTypeData = menuString.get();
77 info.cch = menuStringLength;
78
79 if (::GetMenuItemInfo(menu, i, TRUE, &info))
80 items.append(ContextMenuItem(info));
81 }
82 #endif
83 }
84
createNativeMenuFromItems(const Vector<ContextMenuItem> & items)85 HMENU ContextMenu::createNativeMenuFromItems(const Vector<ContextMenuItem>& items)
86 {
87 HMENU menu = ::CreatePopupMenu();
88
89 for (size_t i = 0; i < items.size(); ++i) {
90 const ContextMenuItem& item = items[i];
91
92 MENUITEMINFO menuItem = item.nativeMenuItem();
93
94 #if OS(WINCE)
95 UINT flags = MF_BYPOSITION;
96 UINT newItem = 0;
97 LPCWSTR title = 0;
98
99 if (item.type() == SeparatorType)
100 flags |= MF_SEPARATOR;
101 else {
102 flags |= MF_STRING;
103 flags |= item.checked() ? MF_CHECKED : MF_UNCHECKED;
104 flags |= item.enabled() ? MF_ENABLED : MF_GRAYED;
105
106 title = menuItem.dwTypeData;
107 menuItem.dwTypeData = 0;
108
109 if (menuItem.hSubMenu) {
110 flags |= MF_POPUP;
111 newItem = reinterpret_cast<UINT>(menuItem.hSubMenu);
112 menuItem.hSubMenu = 0;
113 } else
114 newItem = menuItem.wID;
115 }
116
117 ::InsertMenuW(menu, i, flags, newItem, title);
118 #else
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());
126 }
127
128 ::InsertMenuItem(menu, i, TRUE, &menuItem);
129 #endif
130 }
131
132 return menu;
133 }
134
nativeMenu() const135 HMENU ContextMenu::nativeMenu() const
136 {
137 return createNativeMenuFromItems(m_items);
138 }
139
140 } // namespace WebCore
141