2 Copyright (C) 2010 ProFUSION embedded systems
3 Copyright (C) 2010 Samsung Electronics
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 #include "ewk_contextmenu.h"
24 #include "ContextMenu.h"
25 #include "ContextMenuController.h"
26 #include "ContextMenuItem.h"
28 #include "ewk_private.h"
31 #include <eina_safety_checks.h>
32 #include <wtf/text/CString.h>
34 #if ENABLE(TIZEN_SAVE_IMAGE)
35 #include "ContextMenuClient.h"
36 #include "HTMLImageElement.h"
37 #include "InstallContentEfl.h"
41 * \struct _Ewk_Context_Menu
42 * @brief Contains the context menu data.
44 struct _Ewk_Context_Menu {
45 unsigned int __ref; /**< the reference count of the object */
46 #if ENABLE(CONTEXT_MENUS)
47 WebCore::ContextMenuController* controller; /**< the WebCore's object which is responsible for the context menu */
49 Evas_Object* view; /**< the view object */
51 Eina_List* items; /**< the list of items */
55 * \struct _Ewk_Context_Menu_Item
56 * @brief Represents one item of the context menu object.
58 struct _Ewk_Context_Menu_Item {
59 Ewk_Context_Menu_Item_Type type; /**< contains the type of the item */
60 Ewk_Context_Menu_Action action; /**< contains the action of the item */
62 const char* title; /**< contains the title of the item */
63 Ewk_Context_Menu* submenu; /**< contains the pointer to the submenu of the item */
64 #if ENABLE(TIZEN_CONTEXT_MENU) /* Restore ContextMenuClientEfl */
65 /// contains the pointer to the parent menu of the item
66 Ewk_Context_Menu* parentmenu;
73 void ewk_context_menu_ref(Ewk_Context_Menu* menu)
75 EINA_SAFETY_ON_NULL_RETURN(menu);
79 void ewk_context_menu_unref(Ewk_Context_Menu* menu)
81 EINA_SAFETY_ON_NULL_RETURN(menu);
87 EINA_LIST_FREE(menu->items, item)
88 ewk_context_menu_item_free(static_cast<Ewk_Context_Menu_Item*>(item));
93 Eina_Bool ewk_context_menu_destroy(Ewk_Context_Menu* menu)
95 #if ENABLE(CONTEXT_MENUS)
96 EINA_SAFETY_ON_NULL_RETURN_VAL(menu, false);
97 EINA_SAFETY_ON_NULL_RETURN_VAL(menu->controller, false);
98 menu->controller->clearContextMenu();
105 const Eina_List* ewk_context_menu_item_list_get(const Ewk_Context_Menu* menu)
107 EINA_SAFETY_ON_NULL_RETURN_VAL(menu, 0);
112 Ewk_Context_Menu_Item* ewk_context_menu_item_new(Ewk_Context_Menu_Item_Type type,
113 Ewk_Context_Menu_Action action, Ewk_Context_Menu* submenu,
114 #if ENABLE(TIZEN_CONTEXT_MENU)
115 Ewk_Context_Menu* parentmenu,
117 const char* title, Eina_Bool checked, Eina_Bool enabled)
119 Ewk_Context_Menu_Item* item = static_cast<Ewk_Context_Menu_Item*>(malloc(sizeof(*item)));
124 item->action = action;
125 item->title = eina_stringshare_add(title);
126 item->submenu = submenu;
127 #if ENABLE(TIZEN_CONTEXT_MENU)
128 item->parentmenu=parentmenu;
130 item->checked = checked;
131 item->enabled = enabled;
136 Eina_Bool ewk_context_menu_item_select(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item* item)
138 #if ENABLE(CONTEXT_MENUS)
139 EINA_SAFETY_ON_NULL_RETURN_VAL(menu, false);
140 EINA_SAFETY_ON_NULL_RETURN_VAL(item, false);
141 WebCore::ContextMenuAction action = static_cast<WebCore::ContextMenuAction>(item->action);
142 WebCore::ContextMenuItemType type = static_cast<WebCore::ContextMenuItemType>(item->type);
144 // Don't care about title and submenu as they're not used after this point.
145 WebCore::ContextMenuItem core(type, action, WTF::String());
146 menu->controller->contextMenuItemSelected(&core);
153 void ewk_context_menu_item_free(Ewk_Context_Menu_Item* item)
155 EINA_SAFETY_ON_NULL_RETURN(item);
157 eina_stringshare_del(item->title);
161 Ewk_Context_Menu_Item_Type ewk_context_menu_item_type_get(const Ewk_Context_Menu_Item* item)
163 EINA_SAFETY_ON_NULL_RETURN_VAL(item, EWK_ACTION_TYPE);
167 Eina_Bool ewk_context_menu_item_type_set(Ewk_Context_Menu_Item* item, Ewk_Context_Menu_Item_Type type)
169 EINA_SAFETY_ON_NULL_RETURN_VAL(item, false);
174 Ewk_Context_Menu_Action ewk_context_menu_item_action_get(const Ewk_Context_Menu_Item* item)
176 EINA_SAFETY_ON_NULL_RETURN_VAL(item, EWK_CONTEXT_MENU_ITEM_TAG_NO_ACTION);
180 Eina_Bool ewk_context_menu_item_action_set(Ewk_Context_Menu_Item* item, Ewk_Context_Menu_Action action)
182 EINA_SAFETY_ON_NULL_RETURN_VAL(item, false);
183 item->action = action;
187 const char* ewk_context_menu_item_title_get(const Ewk_Context_Menu_Item* item)
189 EINA_SAFETY_ON_NULL_RETURN_VAL(item, 0);
193 const char* ewk_context_menu_item_title_set(Ewk_Context_Menu_Item* item, const char* title)
195 EINA_SAFETY_ON_NULL_RETURN_VAL(item, 0);
196 eina_stringshare_replace(&item->title, title);
200 Eina_Bool ewk_context_menu_item_checked_get(const Ewk_Context_Menu_Item* item)
202 EINA_SAFETY_ON_NULL_RETURN_VAL(item, false);
203 return item->checked;
206 Eina_Bool ewk_context_menu_item_checked_set(Ewk_Context_Menu_Item* item, Eina_Bool checked)
208 EINA_SAFETY_ON_NULL_RETURN_VAL(item, false);
209 item->checked = checked;
213 Eina_Bool ewk_context_menu_item_enabled_get(const Ewk_Context_Menu_Item* item)
215 EINA_SAFETY_ON_NULL_RETURN_VAL(item, false);
216 return item->enabled;
219 Eina_Bool ewk_context_menu_item_enabled_set(Ewk_Context_Menu_Item* item, Eina_Bool enabled)
221 EINA_SAFETY_ON_NULL_RETURN_VAL(item, false);
222 item->enabled = enabled;
227 * Gets the parent menu of the item object.
229 * @param o the item of the context menu to get the parent menu
230 * @return the parent context menu of the item or @c 0 if error occured
232 Ewk_Context_Menu* ewk_context_menu_item_parent_get(Ewk_Context_Menu_Item* o)
234 #if ENABLE(TIZEN_CONTEXT_MENU)
235 EINA_SAFETY_ON_NULL_RETURN_VAL(o, EINA_FALSE);
237 return o->parentmenu;
244 * Saves cached image to file manager.
245 * This API can only save cached image which is selected by context menu.
246 * In order to use this API, application should register callback for "contextmenu,save,as"
247 * signal. Please refer to ewk_context_menu_image_save_as_set() in ewk_contextmenu.cpp.
249 * @param file_name image name.
250 * @return @c integer result if the image file is saved successfully.
252 int ewk_context_menu_cached_image_save(const char* file_name)
254 #if ENABLE(TIZEN_SAVE_IMAGE)
256 WebCore::InstallContent *installContent = WebCore::InstallContent::getInstance();
257 ret = installContent->saveImage(file_name);
258 installContent->setHTMLImageElement(0);
261 LOG_ERROR("TIZEN_SAVE_IMAGE is disabled. \n");
266 /* internal methods ****************************************************/
268 #if ENABLE(CONTEXT_MENUS)
272 * Creates an empty context menu on view.
274 * @param view the view object
275 * @param controller the WebCore's context menu controller
276 * @return newly allocated the context menu on success or @c 0 on errors
278 * @note emits a signal "contextmenu,new"
280 Ewk_Context_Menu* ewk_context_menu_new(Evas_Object* view, WebCore::ContextMenuController* controller)
282 Ewk_Context_Menu* menu;
283 EINA_SAFETY_ON_NULL_RETURN_VAL(view, 0);
284 EINA_SAFETY_ON_NULL_RETURN_VAL(controller, 0);
286 menu = static_cast<Ewk_Context_Menu*>(malloc(sizeof(*menu)));
288 CRITICAL("Could not allocate context menu memory.");
294 menu->controller = controller;
296 evas_object_smart_callback_call(menu->view, "contextmenu,new", menu);
304 * Frees the context menu.
306 * @param menu the view object
307 * @return @c true on success, or @c false on failure
309 * @note emits a signal "contextmenu,free"
311 * @see ewk_context_menu_unref
312 * @see ewk_context_menu_destroy
314 bool ewk_context_menu_free(Ewk_Context_Menu* menu)
316 EINA_SAFETY_ON_NULL_RETURN_VAL(menu, false);
317 evas_object_smart_callback_call(menu->view, "contextmenu,free", menu);
318 ewk_context_menu_unref(menu);
325 * Appends the WebCore's item to the context menu object.
327 * @param menu the context menu object
328 * @param core the WebCore's context menu item that will be added to the context menu
329 * @note emits a signal "contextmenu,item,appended"
331 * @see ewk_context_menu_item_new
333 void ewk_context_menu_item_append(Ewk_Context_Menu* menu, WebCore::ContextMenuItem& core)
335 Ewk_Context_Menu_Item_Type type = static_cast<Ewk_Context_Menu_Item_Type>(core.type());
336 Ewk_Context_Menu_Action action = static_cast<Ewk_Context_Menu_Action>(core.action());
337 #if ENABLE(TIZEN_CONTEXT_MENU)
338 Ewk_Context_Menu* submenu = static_cast<Ewk_Context_Menu*>(core.platformSubMenu());
340 Ewk_Context_Menu_Item* menu_item = ewk_context_menu_item_new
341 (type, action, submenu, menu, core.title().utf8().data(), core.checked(),
344 Ewk_Context_Menu_Item *menu_item = ewk_context_menu_item_new
345 (type, action, 0, core.title().utf8().data(), core.checked(),
348 EINA_SAFETY_ON_NULL_RETURN(menu_item);
350 menu->items = eina_list_append(menu->items, menu_item);
351 evas_object_smart_callback_call(menu->view, "contextmenu,item,appended", menu);
357 * Emits a signal with the items of the context menu.
359 * @param menu the context menu object
360 * @return the same context menu object that was given through parameter
362 * @note emits a signal "contextmenu,customize"
364 * @see ewk_context_menu_item_list_get
366 Ewk_Context_Menu* ewk_context_menu_customize(Ewk_Context_Menu* menu)
368 EINA_SAFETY_ON_NULL_RETURN_VAL(menu, 0);
370 evas_object_smart_callback_call(menu->view, "contextmenu,customize", menu->items);
377 * Emits a signal "contextmenu,show"
379 * @param menu the context menu object
381 void ewk_context_menu_show(Ewk_Context_Menu* menu)
383 EINA_SAFETY_ON_NULL_RETURN(menu);
385 evas_object_smart_callback_call(menu->view, "contextmenu,show", menu);
388 #if ENABLE(TIZEN_SAVE_IMAGE)
391 * Requests to save image to user with new name.
396 * Emits signal: "load,started" on main frame with no parameters.
398 void ewk_context_menu_image_save_as_set(Ewk_Context_Menu* o, const char* image_name)
400 EINA_SAFETY_ON_NULL_RETURN(o);
401 evas_object_smart_callback_call(o->view, "contextmenu,save,as", (void*)image_name);
406 * Send image path which is saved to temp directory.
409 * @param file_path saved file full path
410 * @param Ewk_SendVia data
412 * Emits signal: "contextmenu,sendvia,email" on main frame with file path
414 void ewk_context_menu_image_send_via_email_set(Ewk_Context_Menu* o, const char* file_path)
416 EINA_SAFETY_ON_NULL_RETURN(o);
417 evas_object_smart_callback_call(o->view, "contextmenu,sendvia,email", (void*)file_path);
422 * Send image data which is saved to temp directory.
425 * @param file_path saved file full path
426 * @param Ewk_SendVia data
428 * Emits signal: "context,sendvia,message" on main frame with file path.
430 void ewk_context_menu_image_send_via_message_set(Ewk_Context_Menu* o, const char* file_path)
432 EINA_SAFETY_ON_NULL_RETURN(o);
433 evas_object_smart_callback_call(o->view, "contextmenu,sendvia,message", (void*)file_path);