1/*
2 * Copyright (C) 2010 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WKContextMenuItem.h"
28
29#include "APIArray.h"
30#include "WebContextMenuItem.h"
31#include "WebContextMenuItemData.h"
32#include "WKAPICast.h"
33#include "WKContextMenuItemTypes.h"
34
35using namespace WebCore;
36using namespace WebKit;
37
38WKTypeID WKContextMenuItemGetTypeID()
39{
40#if ENABLE(CONTEXT_MENUS)
41    return toAPI(WebContextMenuItem::APIType);
42#else
43    return toAPI(API::Object::Type::Null);
44#endif
45}
46
47WKContextMenuItemRef WKContextMenuItemCreateAsAction(WKContextMenuItemTag tag, WKStringRef title, bool enabled)
48{
49#if ENABLE(CONTEXT_MENUS)
50    return toAPI(WebContextMenuItem::create(WebContextMenuItemData(ActionType, toImpl(tag), toImpl(title)->string(), enabled, false)).leakRef());
51#else
52    UNUSED_PARAM(tag);
53    UNUSED_PARAM(title);
54    UNUSED_PARAM(enabled);
55    return 0;
56#endif
57}
58
59WKContextMenuItemRef WKContextMenuItemCreateAsCheckableAction(WKContextMenuItemTag tag, WKStringRef title, bool enabled, bool checked)
60{
61#if ENABLE(CONTEXT_MENUS)
62    return toAPI(WebContextMenuItem::create(WebContextMenuItemData(CheckableActionType, toImpl(tag), toImpl(title)->string(), enabled, checked)).leakRef());
63#else
64    UNUSED_PARAM(tag);
65    UNUSED_PARAM(title);
66    UNUSED_PARAM(enabled);
67    UNUSED_PARAM(checked);
68    return 0;
69#endif
70}
71
72WKContextMenuItemRef WKContextMenuItemCreateAsSubmenu(WKStringRef title, bool enabled, WKArrayRef submenuItems)
73{
74#if ENABLE(CONTEXT_MENUS)
75    return toAPI(WebContextMenuItem::create(toImpl(title)->string(), enabled, toImpl(submenuItems)).leakRef());
76#else
77    UNUSED_PARAM(title);
78    UNUSED_PARAM(enabled);
79    UNUSED_PARAM(submenuItems);
80    return 0;
81#endif
82}
83
84WKContextMenuItemRef WKContextMenuItemSeparatorItem()
85{
86#if ENABLE(CONTEXT_MENUS)
87    return toAPI(WebContextMenuItem::separatorItem());
88#else
89    return 0;
90#endif
91}
92
93WKContextMenuItemTag WKContextMenuItemGetTag(WKContextMenuItemRef itemRef)
94{
95#if ENABLE(CONTEXT_MENUS)
96    return toAPI(toImpl(itemRef)->data()->action());
97#else
98    UNUSED_PARAM(itemRef);
99    return toAPI(ContextMenuItemTagNoAction);
100#endif
101}
102
103WKContextMenuItemType WKContextMenuItemGetType(WKContextMenuItemRef itemRef)
104{
105#if ENABLE(CONTEXT_MENUS)
106    return toAPI(toImpl(itemRef)->data()->type());
107#else
108    UNUSED_PARAM(itemRef);
109    return toAPI(ActionType);
110#endif
111}
112
113WKStringRef WKContextMenuItemCopyTitle(WKContextMenuItemRef itemRef)
114{
115#if ENABLE(CONTEXT_MENUS)
116    return toCopiedAPI(toImpl(itemRef)->data()->title().impl());
117#else
118    UNUSED_PARAM(itemRef);
119    return 0;
120#endif
121}
122
123bool WKContextMenuItemGetEnabled(WKContextMenuItemRef itemRef)
124{
125#if ENABLE(CONTEXT_MENUS)
126    return toImpl(itemRef)->data()->enabled();
127#else
128    UNUSED_PARAM(itemRef);
129    return false;
130#endif
131}
132
133bool WKContextMenuItemGetChecked(WKContextMenuItemRef itemRef)
134{
135#if ENABLE(CONTEXT_MENUS)
136    return toImpl(itemRef)->data()->checked();
137#else
138    UNUSED_PARAM(itemRef);
139    return false;
140#endif
141}
142
143WKArrayRef WKContextMenuCopySubmenuItems(WKContextMenuItemRef itemRef)
144{
145#if ENABLE(CONTEXT_MENUS)
146    return toAPI(toImpl(itemRef)->submenuItemsAsAPIArray().leakRef());
147#else
148    UNUSED_PARAM(itemRef);
149    return 0;
150#endif
151}
152
153WKTypeRef WKContextMenuItemGetUserData(WKContextMenuItemRef itemRef)
154{
155#if ENABLE(CONTEXT_MENUS)
156    return toAPI(toImpl(itemRef)->userData());
157#else
158    UNUSED_PARAM(itemRef);
159    return 0;
160#endif
161}
162
163void WKContextMenuItemSetUserData(WKContextMenuItemRef itemRef, WKTypeRef userDataRef)
164{
165#if ENABLE(CONTEXT_MENUS)
166    toImpl(itemRef)->setUserData(toImpl(userDataRef));
167#else
168    UNUSED_PARAM(itemRef);
169    UNUSED_PARAM(userDataRef);
170#endif
171}
172