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/OwnArrayPtr.h>
36#include <wtf/Vector.h>
37#include <wtf/text/CString.h>
38
39#ifndef MIIM_FTYPE
40#define MIIM_FTYPE MIIM_TYPE
41#endif
42#ifndef MIIM_STRING
43#define MIIM_STRING MIIM_TYPE
44#endif
45
46namespace WebCore {
47
48ContextMenu::ContextMenu(HMENU menu)
49{
50    getContextMenuItems(menu, m_items);
51}
52
53void ContextMenu::getContextMenuItems(HMENU menu, Vector<ContextMenuItem>& items)
54{
55#if OS(WINCE)
56    notImplemented();
57#else
58    int count = ::GetMenuItemCount(menu);
59    if (count <= 0)
60        return;
61
62    for (int i = 0; i < count; ++i) {
63        MENUITEMINFO info = {0};
64        info.cbSize = sizeof(MENUITEMINFO);
65        info.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING | MIIM_STATE | MIIM_SUBMENU;
66
67        if (!::GetMenuItemInfo(menu, i, TRUE, &info))
68            continue;
69
70        if (info.fType == MFT_SEPARATOR) {
71            items.append(ContextMenuItem(SeparatorType, ContextMenuItemTagNoAction, String()));
72            continue;
73        }
74
75        int menuStringLength = info.cch + 1;
76        OwnArrayPtr<WCHAR> menuString = adoptArrayPtr(new WCHAR[menuStringLength]);
77        info.dwTypeData = menuString.get();
78        info.cch = menuStringLength;
79
80        if (::GetMenuItemInfo(menu, i, TRUE, &info))
81           items.append(ContextMenuItem(info));
82    }
83#endif
84}
85
86HMENU ContextMenu::createPlatformContextMenuFromItems(const Vector<ContextMenuItem>& items)
87{
88    HMENU menu = ::CreatePopupMenu();
89
90    for (size_t i = 0; i < items.size(); ++i) {
91        const ContextMenuItem& item = items[i];
92
93        MENUITEMINFO menuItem = item.platformContextMenuItem();
94
95#if OS(WINCE)
96        UINT flags = MF_BYPOSITION;
97        UINT newItem = 0;
98        LPCWSTR title = 0;
99
100        if (item.type() == SeparatorType)
101            flags |= MF_SEPARATOR;
102        else {
103            flags |= MF_STRING;
104            flags |= item.checked() ? MF_CHECKED : MF_UNCHECKED;
105            flags |= item.enabled() ? MF_ENABLED : MF_GRAYED;
106
107            title = menuItem.dwTypeData;
108            menuItem.dwTypeData = 0;
109
110            if (menuItem.hSubMenu) {
111                flags |= MF_POPUP;
112                newItem = reinterpret_cast<UINT>(menuItem.hSubMenu);
113                menuItem.hSubMenu = 0;
114            } else
115                newItem = menuItem.wID;
116        }
117
118        ::InsertMenuW(menu, i, flags, newItem, title);
119#else
120        // ContextMenuItem::platformContextMenuItem doesn't set the title of the MENUITEMINFO to make the
121        // lifetime handling easier for callers.
122        String itemTitle = item.title();
123        if (item.type() != SeparatorType) {
124            menuItem.fMask |= MIIM_STRING;
125            menuItem.cch = itemTitle.length();
126            menuItem.dwTypeData = const_cast<LPWSTR>(itemTitle.charactersWithNullTermination());
127        }
128
129        ::InsertMenuItem(menu, i, TRUE, &menuItem);
130#endif
131    }
132
133    return menu;
134}
135
136HMENU ContextMenu::platformContextMenu() const
137{
138    return createPlatformContextMenuFromItems(m_items);
139}
140
141} // namespace WebCore
142