2 * Copyright (C) 2012 Samsung Electronics
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "ewk_context_menu.h"
24 #include "WKAPICast.h"
26 #include "WKContextMenuItem.h"
27 #include "WKRetainPtr.h"
29 #include "ewk_context_menu_private.h"
30 #include <wtf/OwnArrayPtr.h>
31 #include <wtf/text/CString.h>
33 using namespace WebKit;
36 * \struct _Ewk_Context_Menu
37 * @brief Contains the context menu data.
39 struct _Ewk_Context_Menu {
40 Eina_List* items; /**< the list of Ewk_Context_Menu_Item */
44 * \struct _Ewk_Context_Menu_Item
45 * @brief Represents one item of the context menu object.
47 struct _Ewk_Context_Menu_Item {
48 WKRetainPtr<WKContextMenuItemRef> itemRef;
53 static Eina_Bool appendItemToContextMenu(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item* item)
58 menu->items = eina_list_append(menu->items, item);
62 static Ewk_Context_Menu* createContextMenu()
64 Ewk_Context_Menu* menu = new Ewk_Context_Menu;
69 Ewk_Context_Menu* ewkContextMenuCreateFromWKArray(WKArrayRef menu)
74 Ewk_Context_Menu* ewkContextMenu = createContextMenu();
75 int size = WKArrayGetSize(menu);
76 WKContextMenuItemRef item;
77 for (int i = 0; i < size; i++) {
78 item = static_cast<WKContextMenuItemRef>(WKArrayGetItemAtIndex(menu, i));
79 if (WKGetTypeID(item) == WKContextMenuItemGetTypeID())
80 appendItemToContextMenu(ewkContextMenu, ewkContextMenuItemCreateFromWKContextMenuItem(item));
83 return ewkContextMenu;
86 void ewkContextMenuDelete(Ewk_Context_Menu* menu)
92 EINA_LIST_FREE(menu->items, data)
93 ewkContextMenuItemDelete(static_cast<Ewk_Context_Menu_Item*>(data));
97 WKArrayRef ewkContextMenuConvertToWKArray(Ewk_Context_Menu* menu)
102 unsigned count = eina_list_count(menu->items);
106 Ewk_Context_Menu_Item* item = 0;
107 OwnArrayPtr<WKTypeRef> items = adoptArrayPtr(new WKTypeRef[count]);
108 for (unsigned i = 0; i < count; i++) {
109 item = static_cast<Ewk_Context_Menu_Item*>(eina_list_nth(menu->items, i));
110 items[i] = item->itemRef.get();
113 return WKArrayCreate(items.get(), count);
116 Ewk_Context_Menu_Item* ewkContextMenuItemCreateFromWKContextMenuItem(WKContextMenuItemRef wkContextMenuItem, WKStringRef linkURLString, WKStringRef imageURLString)
118 if (!wkContextMenuItem)
121 Ewk_Context_Menu_Item* item = new Ewk_Context_Menu_Item;
122 item->itemRef = wkContextMenuItem;
124 item->linkURL = toImpl(linkURLString)->string().utf8();
126 item->imageURL = toImpl(imageURLString)->string().utf8();
131 void ewkContextMenuItemDelete(Ewk_Context_Menu_Item* item)
139 unsigned ewk_context_menu_item_count(Ewk_Context_Menu* menu)
144 return eina_list_count(menu->items);
147 Ewk_Context_Menu_Item* ewk_context_menu_nth_item_get(Ewk_Context_Menu* menu, unsigned int n)
152 return static_cast<Ewk_Context_Menu_Item*>(eina_list_nth(menu->items, n));
155 Eina_Bool ewk_context_menu_item_remove(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item* item)
160 ewkContextMenuItemDelete(item);
161 menu->items = eina_list_remove(menu->items, static_cast<const void*>(item));
166 Eina_Bool ewk_context_menu_item_append_as_action(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item_Tag tag, const char* title, Eina_Bool enabled)
171 WKRetainPtr<WKStringRef> titleString(AdoptWK, WKStringCreateWithUTF8CString(title));
172 WKContextMenuItemRef item = WKContextMenuItemCreateAsAction(static_cast<WKContextMenuItemTag>(tag), titleString.get(), enabled);
173 return appendItemToContextMenu(menu, ewkContextMenuItemCreateFromWKContextMenuItem(item));
176 Eina_Bool ewk_context_menu_item_append(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item_Tag tag, const char* title, const char* iconFile, Eina_Bool enabled)
178 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_ICON_TYPE_SUPPORT)
182 WKRetainPtr<WKStringRef> titleString(AdoptWK, WKStringCreateWithUTF8CString(title));
183 WKRetainPtr<WKStringRef> iconString(AdoptWK, WKStringCreateWithUTF8CString(iconFile));
184 WKContextMenuItemRef item = WKContextMenuItemCreateWithTitleAndIconAsAction(static_cast<WKContextMenuItemTag>(tag), titleString.get(), iconString.get(), enabled);
185 return appendItemToContextMenu(menu, ewkContextMenuItemCreateFromWKContextMenuItem(item));
191 Ewk_Context_Menu_Item_Tag ewk_context_menu_item_tag_get(Ewk_Context_Menu_Item* item)
194 return EWK_CONTEXT_MENU_ITEM_TAG_NO_ACTION;
196 return static_cast<Ewk_Context_Menu_Item_Tag>(WKContextMenuItemGetTag(item->itemRef.get()));
199 Ewk_Context_Menu_Item_Type ewk_context_menu_item_type_get(Ewk_Context_Menu_Item* item)
202 return EWK_CONTEXT_MENU_ITEM_TYPE_ACTION;
204 return static_cast<Ewk_Context_Menu_Item_Type>(WKContextMenuItemGetType(item->itemRef.get()));
207 Eina_Bool ewk_context_menu_item_enabled_get(const Ewk_Context_Menu_Item* item)
212 return WKContextMenuItemGetEnabled(item->itemRef.get());
215 const char* ewk_context_menu_item_link_url_get(Ewk_Context_Menu_Item* item)
220 return item->linkURL.data();
223 const char* ewk_context_menu_item_image_url_get(Ewk_Context_Menu_Item* item)
228 return item->imageURL.data();
230 #endif // #if OS(TIZEN)