1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/*
Copyright (C) 2010 ProFUSION embedded systems
Copyright (C) 2010 Samsung Electronics
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "ewk_contextmenu.h"
#include "ContextMenu.h"
#include "ContextMenuController.h"
#include "ContextMenuItem.h"
#include "EWebKit.h"
#include "ewk_private.h"
#include <Eina.h>
#include <eina_safety_checks.h>
#include <wtf/text/CString.h>
/**
* \struct _Ewk_Context_Menu
* @brief Contains the context menu data.
*/
struct _Ewk_Context_Menu {
unsigned int __ref; /**< the reference count of the object */
#if ENABLE(CONTEXT_MENUS)
WebCore::ContextMenuController* controller; /**< the WebCore's object which is responsible for the context menu */
#endif
Evas_Object* view; /**< the view object */
Eina_List* items; /**< the list of items */
};
/**
* \struct _Ewk_Context_Menu_Item
* @brief Represents one item of the context menu object.
*/
struct _Ewk_Context_Menu_Item {
Ewk_Context_Menu_Item_Type type; /**< contains the type of the item */
Ewk_Context_Menu_Action action; /**< contains the action of the item */
const char* title; /**< contains the title of the item */
Ewk_Context_Menu* submenu; /**< contains the pointer to the submenu of the item */
Eina_Bool checked:1;
Eina_Bool enabled:1;
};
/**
* Increases the reference count of the given object.
*
* @param menu the context menu object to increase the reference count
*/
void ewk_context_menu_ref(Ewk_Context_Menu* menu)
{
EINA_SAFETY_ON_NULL_RETURN(menu);
menu->__ref++;
}
/**
* Decreases the reference count of the given object, possibly freeing it.
*
* When the reference count it's reached 0, the menu with all items are freed.
*
* @param menu the context menu object to decrease the reference count
*/
void ewk_context_menu_unref(Ewk_Context_Menu* menu)
{
EINA_SAFETY_ON_NULL_RETURN(menu);
void* item;
if (--menu->__ref)
return;
EINA_LIST_FREE(menu->items, item)
ewk_context_menu_item_free(static_cast<Ewk_Context_Menu_Item*>(item));
free(menu);
}
/**
* Destroys the context menu object.
*
* @param menu the context menu object to destroy
* @return @c EINA_TRUE on success, @c EINA_FALSE on failure
*
* @see ewk_context_menu_item_free
*/
Eina_Bool ewk_context_menu_destroy(Ewk_Context_Menu* menu)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(menu, EINA_FALSE);
#if ENABLE(CONTEXT_MENUS)
EINA_SAFETY_ON_NULL_RETURN_VAL(menu->controller, EINA_FALSE);
menu->controller->clearContextMenu();
#endif
return EINA_TRUE;
}
/**
* Gets the list of items.
*
* @param o the context menu object to get list of the items
* @return the list of the items on success or @c 0 on failure
*/
const Eina_List* ewk_context_menu_item_list_get(Ewk_Context_Menu* o)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, 0);
return o->items;
}
/**
* Creates a new item of the context menu.
*
* @param type specifies a type of the item
* @param action specifies a action of the item
* @param submenu specifies a submenu of the item
* @param title specifies a title of the item
* @param checked
* @param enabled @c EINA_TRUE to enable the item or @c EINA_FALSE to disable
* @return the pointer to the new item on success or @c 0 on failure
*
* @note The return value @b should @b be freed after use.
*/
Ewk_Context_Menu_Item* ewk_context_menu_item_new(Ewk_Context_Menu_Item_Type type,
Ewk_Context_Menu_Action action, Ewk_Context_Menu* submenu,
const char* title, Eina_Bool checked, Eina_Bool enabled)
{
Ewk_Context_Menu_Item* item = (Ewk_Context_Menu_Item*) malloc(sizeof(*item));
if (!item)
return 0;
item->type = type;
item->action = action;
item->title = eina_stringshare_add(title);
item->submenu = submenu;
item->checked = checked;
item->enabled = enabled;
return item;
}
/**
* Selects the item from the context menu object.
*
* @param menu the context menu object
* @param item the item is selected
* @return @c EINA_TRUE on success or @c EINA_FALSE on failure
*/
Eina_Bool ewk_context_menu_item_select(Ewk_Context_Menu* menu, Ewk_Context_Menu_Item* item)
{
#if ENABLE(CONTEXT_MENUS)
EINA_SAFETY_ON_NULL_RETURN_VAL(menu, EINA_FALSE);
EINA_SAFETY_ON_NULL_RETURN_VAL(item, EINA_FALSE);
WebCore::ContextMenuAction action = static_cast<WebCore::ContextMenuAction>(item->action);
WebCore::ContextMenuItemType type = static_cast<WebCore::ContextMenuItemType>(item->type);
// Don't care about title and submenu as they're not used after this point.
WebCore::ContextMenuItem core(type, action, WTF::String());
menu->controller->contextMenuItemSelected(&core);
return EINA_TRUE;
#else
return EINA_FALSE;
#endif
}
/**
* Destroys the item of the context menu object.
*
* @param item the item to destroy
*
* @see ewk_context_menu_destroy
* @see ewk_context_menu_unref
*/
void ewk_context_menu_item_free(Ewk_Context_Menu_Item* item)
{
EINA_SAFETY_ON_NULL_RETURN(item);
eina_stringshare_del(item->title);
free(item);
}
/**
* Gets type of the item.
*
* @param o the item to get the type
* @return type of the item on success or @c EWK_ACTION_TYPE on failure
*
* @see ewk_context_menu_item_type_set
*/
Ewk_Context_Menu_Item_Type ewk_context_menu_item_type_get(Ewk_Context_Menu_Item* o)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, EWK_ACTION_TYPE);
return o->type;
}
/**
* Sets the type of item.
*
* @param o the item to set the type
* @param type a new type for the item object
* @return @c EINA_TRUE on success, or @c EINA_FALSE on failure
*
* @see ewk_context_menu_item_type_get
*/
Eina_Bool ewk_context_menu_item_type_set(Ewk_Context_Menu_Item* o, Ewk_Context_Menu_Item_Type type)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, EINA_FALSE);
o->type = type;
return EINA_TRUE;
}
/**
* Gets an action of the item.
*
* @param o the item to get the action
* @return an action of the item on success or @c EWK_CONTEXT_MENU_ITEM_TAG_NO_ACTION on failure
*
* @see ewk_context_menu_item_action_set
*/
Ewk_Context_Menu_Action ewk_context_menu_item_action_get(Ewk_Context_Menu_Item* o)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, EWK_CONTEXT_MENU_ITEM_TAG_NO_ACTION);
return o->action;
}
/**
* Sets an action of the item.
*
* @param o the item to set the action
* @param action a new action for the item object
* @return @c EINA_TRUE on success, or @c EINA_FALSE on failure
*
* @see ewk_context_menu_item_action_get
*/
Eina_Bool ewk_context_menu_item_action_set(Ewk_Context_Menu_Item* o, Ewk_Context_Menu_Action action)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, EINA_FALSE);
o->action = action;
return EINA_TRUE;
}
/**
* Gets a title of the item.
*
* @param o the item to get the title
* @return a title of the item on success, or @c 0 on failure
*
* @see ewk_context_menu_item_title_set
*/
const char* ewk_context_menu_item_title_get(Ewk_Context_Menu_Item* o)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, 0);
return o->title;
}
/**
* Sets a title of the item.
*
* @param o the item to set the title
* @param title a new title for the item object
* @return a new title of the item on success or @c 0 on failure
*
* @see ewk_context_menu_item_title_get
*/
const char* ewk_context_menu_item_title_set(Ewk_Context_Menu_Item* o, const char* title)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, 0);
eina_stringshare_replace(&o->title, title);
return o->title;
}
Eina_Bool ewk_context_menu_item_checked_get(Ewk_Context_Menu_Item* o)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, EINA_FALSE);
return o->checked;
}
Eina_Bool ewk_context_menu_item_checked_set(Ewk_Context_Menu_Item* o, Eina_Bool checked)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, EINA_FALSE);
o->checked = checked;
return EINA_TRUE;
}
/**
* Gets if the item is enabled.
*
* @param o the item to get enabled state
* @return @c EINA_TRUE if it's enabled, @c EINA_FALSE if not or on failure
*
* @see ewk_context_menu_item_enabled_set
*/
Eina_Bool ewk_context_menu_item_enabled_get(Ewk_Context_Menu_Item* o)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, EINA_FALSE);
return o->enabled;
}
/**
* Enables/disables the item.
*
* @param o the item to enable/disable
* @param enabled @c EINA_TRUE to enable the item or @c EINA_FALSE to disable
* @return @c EINA_TRUE on success, or @c EINA_FALSE on failure
*
* @see ewk_context_menu_item_enabled_get
*/
Eina_Bool ewk_context_menu_item_enabled_set(Ewk_Context_Menu_Item *o, Eina_Bool enabled)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, EINA_FALSE);
o->enabled = enabled;
return EINA_TRUE;
}
/* internal methods ****************************************************/
#if ENABLE(CONTEXT_MENUS)
/**
* @internal
*
* Creates an empty context menu on view.
*
* @param view the view object
* @param controller the WebCore's context menu controller
* @return newly allocated the context menu on success or @c 0 on errors
*
* @note emits a signal "contextmenu,new"
*/
Ewk_Context_Menu* ewk_context_menu_new(Evas_Object* view, WebCore::ContextMenuController* controller)
{
Ewk_Context_Menu* menu;
EINA_SAFETY_ON_NULL_RETURN_VAL(view, 0);
EINA_SAFETY_ON_NULL_RETURN_VAL(controller, 0);
menu = static_cast<Ewk_Context_Menu*>(malloc(sizeof(*menu)));
if (!menu) {
CRITICAL("Could not allocate context menu memory.");
return 0;
}
menu->__ref = 1;
menu->view = view;
menu->controller = controller;
menu->items = 0;
evas_object_smart_callback_call(menu->view, "contextmenu,new", menu);
return menu;
}
/**
* @internal
*
* Frees the context menu.
*
* @param o the view object
* @return @c EINA_TRUE on success, or @c EINA_FALSE on failure
*
* @note emits a signal "contextmenu,free"
*
* @see ewk_context_menu_unref
* @see ewk_context_menu_destroy
*/
Eina_Bool ewk_context_menu_free(Ewk_Context_Menu* o)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, EINA_FALSE);
evas_object_smart_callback_call(o->view, "contextmenu,free", o);
ewk_context_menu_unref(o);
return EINA_TRUE;
}
/**
* @internal
*
* Appends the WebCore's item to the context menu object.
*
* @param o the context menu object
* @param core the WebCore's context menu item that will be added to the context menu
* @note emits a signal "contextmenu,item,appended"
*
* @see ewk_context_menu_item_new
*/
void ewk_context_menu_item_append(Ewk_Context_Menu* o, WebCore::ContextMenuItem& core)
{
Ewk_Context_Menu_Item_Type type = static_cast<Ewk_Context_Menu_Item_Type>(core.type());
Ewk_Context_Menu_Action action = static_cast<Ewk_Context_Menu_Action>(core.action());
Ewk_Context_Menu_Item* menu_item = ewk_context_menu_item_new
(type, action, 0, core.title().utf8().data(), core.checked(),
core.enabled());
EINA_SAFETY_ON_NULL_RETURN(menu_item);
o->items = eina_list_append(o->items, menu_item);
evas_object_smart_callback_call(o->view, "contextmenu,item,appended", o);
}
/**
* @internal
*
* Emits a signal with the items of the context menu.
*
* @param o the context menu object
* @return the same context menu object that was given through parameter
*
* @note emits a signal "contextmenu,customize"
*
* @see ewk_context_menu_item_list_get
*/
Ewk_Context_Menu* ewk_context_menu_custom_get(Ewk_Context_Menu* o)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(o, 0);
evas_object_smart_callback_call(o->view, "contextmenu,customize", o->items);
return o;
}
/**
* @internal
*
* Emits a signal "contextmenu,show"
*
* @param o the context menu object
*/
void ewk_context_menu_show(Ewk_Context_Menu* o)
{
EINA_SAFETY_ON_NULL_RETURN(o);
evas_object_smart_callback_call(o->view, "contextmenu,show", o);
}
#endif
|