File: WebKitContextMenu.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (436 lines) | stat: -rw-r--r-- 13,470 bytes parent folder | download | duplicates (8)
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
/*
 * Copyright (C) 2012 Igalia S.L.
 *
 * 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 "WebKitContextMenu.h"

#include "APIArray.h"
#include "WebContextMenuItem.h"
#include "WebKitContextMenuItem.h"
#include "WebKitContextMenuItemPrivate.h"
#include "WebKitContextMenuPrivate.h"
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/WTFGType.h>

#if PLATFORM(GTK)
#include <WebCore/GRefPtrGtk.h>
#include <WebCore/GUniquePtrGtk.h>
#endif

#if ENABLE(CONTEXT_MENUS)
using namespace WebKit;
using namespace WebCore;
#endif // ENABLE(CONTEXT_MENUS)

/**
 * WebKitContextMenu:
 *
 * Represents the context menu in a #WebKitWebView.
 *
 * #WebKitContextMenu represents a context menu containing
 * #WebKitContextMenuItem<!-- -->s in a #WebKitWebView.
 *
 * When a #WebKitWebView is about to display the context menu, it
 * emits the #WebKitWebView::context-menu signal, which has the
 * #WebKitContextMenu as an argument. You can modify it, adding new
 * submenus that you can create with webkit_context_menu_new(), adding
 * new #WebKitContextMenuItem<!-- -->s with
 * webkit_context_menu_prepend(), webkit_context_menu_append() or
 * webkit_context_menu_insert(), maybe after having removed the
 * existing ones with webkit_context_menu_remove_all().
 */

struct _WebKitContextMenuPrivate {
    GList* items;
    WebKitContextMenuItem* parentItem;
    GRefPtr<GVariant> userData;
#if PLATFORM(GTK)
#if USE(GTK4)
    GRefPtr<GdkEvent> event;
#else
    GUniquePtr<GdkEvent> event;
#endif
#endif
};

WEBKIT_DEFINE_FINAL_TYPE(WebKitContextMenu, webkit_context_menu, G_TYPE_OBJECT, GObject)

static void webkitContextMenuDispose(GObject* object)
{
    webkit_context_menu_remove_all(WEBKIT_CONTEXT_MENU(object));
    G_OBJECT_CLASS(webkit_context_menu_parent_class)->dispose(object);
}

static void webkit_context_menu_class_init(WebKitContextMenuClass* listClass)
{
    GObjectClass* gObjectClass = G_OBJECT_CLASS(listClass);
    gObjectClass->dispose = webkitContextMenuDispose;
}

#if ENABLE(CONTEXT_MENUS)
void webkitContextMenuPopulate(WebKitContextMenu* menu, Vector<WebContextMenuItemData>& contextMenuItems)
{
    for (GList* item = menu->priv->items; item; item = g_list_next(item)) {
        WebKitContextMenuItem* menuItem = WEBKIT_CONTEXT_MENU_ITEM(item->data);
        contextMenuItems.append(webkitContextMenuItemToWebContextMenuItemData(menuItem));
    }
}

void webkitContextMenuPopulate(WebKitContextMenu* menu, Vector<WebContextMenuItemGlib>& contextMenuItems)
{
    for (GList* item = menu->priv->items; item; item = g_list_next(item)) {
        WebKitContextMenuItem* menuItem = WEBKIT_CONTEXT_MENU_ITEM(item->data);
        contextMenuItems.append(webkitContextMenuItemToWebContextMenuItemGlib(menuItem));
    }
}

WebKitContextMenu* webkitContextMenuCreate(const Vector<WebContextMenuItemData>& items)
{
    WebKitContextMenu* menu = webkit_context_menu_new();
    for (const auto& item : items)
        webkit_context_menu_prepend(menu, webkitContextMenuItemCreate(item));
    menu->priv->items = g_list_reverse(menu->priv->items);

    return menu;
}

#if PLATFORM(GTK)
#if USE(GTK4)
void webkitContextMenuSetEvent(WebKitContextMenu* menu, GRefPtr<GdkEvent>&& event)
#else
void webkitContextMenuSetEvent(WebKitContextMenu* menu, GUniquePtr<GdkEvent>&& event)
#endif
{
    menu->priv->event = WTFMove(event);
}
#endif

void webkitContextMenuSetParentItem(WebKitContextMenu* menu, WebKitContextMenuItem* item)
{
    menu->priv->parentItem = item;
}

WebKitContextMenuItem* webkitContextMenuGetParentItem(WebKitContextMenu* menu)
{
    return menu->priv->parentItem;
}
#endif // ENABLE(CONTEXT_MENUS)

/**
 * webkit_context_menu_new:
 *
 * Creates a new #WebKitContextMenu object.
 *
 * Creates a new #WebKitContextMenu object to be used as a submenu of an existing
 * #WebKitContextMenu. The context menu of a #WebKitWebView is created by the view
 * and passed as an argument of #WebKitWebView::context-menu signal.
 * To add items to the menu use webkit_context_menu_prepend(),
 * webkit_context_menu_append() or webkit_context_menu_insert().
 * See also webkit_context_menu_new_with_items() to create a #WebKitContextMenu with
 * a list of initial items.
 *
 * Returns: The newly created #WebKitContextMenu object
 */
WebKitContextMenu* webkit_context_menu_new()
{
    return WEBKIT_CONTEXT_MENU(g_object_new(WEBKIT_TYPE_CONTEXT_MENU, NULL));
}

/**
 * webkit_context_menu_new_with_items:
 * @items: (element-type WebKitContextMenuItem): a #GList of #WebKitContextMenuItem
 *
 * Creates a new #WebKitContextMenu object with the given items.
 *
 * Creates a new #WebKitContextMenu object to be used as a submenu of an existing
 * #WebKitContextMenu with the given initial items.
 * See also webkit_context_menu_new()
 *
 * Returns: The newly created #WebKitContextMenu object
 */
WebKitContextMenu* webkit_context_menu_new_with_items(GList* items)
{
    WebKitContextMenu* menu = webkit_context_menu_new();
    g_list_foreach(items, reinterpret_cast<GFunc>(reinterpret_cast<GCallback>(g_object_ref_sink)), 0);
    menu->priv->items = g_list_copy(items);

    return menu;
}

/**
 * webkit_context_menu_prepend:
 * @menu: a #WebKitContextMenu
 * @item: the #WebKitContextMenuItem to add
 *
 * Adds @item at the beginning of the @menu.
 */
void webkit_context_menu_prepend(WebKitContextMenu* menu, WebKitContextMenuItem* item)
{
    webkit_context_menu_insert(menu, item, 0);
}

/**
 * webkit_context_menu_append:
 * @menu: a #WebKitContextMenu
 * @item: the #WebKitContextMenuItem to add
 *
 * Adds @item at the end of the @menu.
 */
void webkit_context_menu_append(WebKitContextMenu* menu, WebKitContextMenuItem* item)
{
    webkit_context_menu_insert(menu, item, -1);
}

/**
 * webkit_context_menu_insert:
 * @menu: a #WebKitContextMenu
 * @item: the #WebKitContextMenuItem to add
 * @position: the position to insert the item
 *
 * Inserts @item into the @menu at the given position.
 *
 * If @position is negative, or is larger than the number of items
 * in the #WebKitContextMenu, the item is added on to the end of
 * the @menu. The first position is 0.
 */
void webkit_context_menu_insert(WebKitContextMenu* menu, WebKitContextMenuItem* item, int position)
{
    g_return_if_fail(WEBKIT_IS_CONTEXT_MENU(menu));
    g_return_if_fail(WEBKIT_IS_CONTEXT_MENU_ITEM(item));

    g_object_ref_sink(item);
    menu->priv->items = g_list_insert(menu->priv->items, item, position);
}

/**
 * webkit_context_menu_move_item:
 * @menu: a #WebKitContextMenu
 * @item: the #WebKitContextMenuItem to add
 * @position: the new position to move the item
 *
 * Moves @item to the given position in the @menu.
 *
 * If @position is negative, or is larger than the number of items
 * in the #WebKitContextMenu, the item is added on to the end of
 * the @menu.
 * The first position is 0.
 */
void webkit_context_menu_move_item(WebKitContextMenu* menu, WebKitContextMenuItem* item, int position)
{
    g_return_if_fail(WEBKIT_IS_CONTEXT_MENU(menu));
    g_return_if_fail(WEBKIT_IS_CONTEXT_MENU_ITEM(item));

    if (!g_list_find(menu->priv->items, item))
        return;

    menu->priv->items = g_list_remove(menu->priv->items, item);
    menu->priv->items = g_list_insert(menu->priv->items, item, position);
}

/**
 * webkit_context_menu_get_items:
 * @menu: a #WebKitContextMenu
 *
 * Returns the item list of @menu.
 *
 * Returns: (element-type WebKitContextMenuItem) (transfer none): a #GList of
 *    #WebKitContextMenuItem<!-- -->s
 */
GList* webkit_context_menu_get_items(WebKitContextMenu* menu)
{
    g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU(menu), 0);

    return menu->priv->items;
}

/**
 * webkit_context_menu_get_n_items:
 * @menu: a #WebKitContextMenu
 *
 * Gets the length of the @menu.
 *
 * Returns: the number of #WebKitContextMenuItem<!-- -->s in @menu
 */
guint webkit_context_menu_get_n_items(WebKitContextMenu* menu)
{
    g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU(menu), 0);

    return g_list_length(menu->priv->items);
}

/**
 * webkit_context_menu_first:
 * @menu: a #WebKitContextMenu
 *
 * Gets the first item in the @menu.
 *
 * Returns: (transfer none): the first #WebKitContextMenuItem of @menu,
 *    or %NULL if the #WebKitContextMenu is empty.
 */
WebKitContextMenuItem* webkit_context_menu_first(WebKitContextMenu* menu)
{
    g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU(menu), 0);

    return menu->priv->items ? WEBKIT_CONTEXT_MENU_ITEM(menu->priv->items->data) : 0;
}

/**
 * webkit_context_menu_last:
 * @menu: a #WebKitContextMenu
 *
 * Gets the last item in the @menu.
 *
 * Returns: (transfer none): the last #WebKitContextMenuItem of @menu,
 *    or %NULL if the #WebKitContextMenu is empty.
 */
WebKitContextMenuItem* webkit_context_menu_last(WebKitContextMenu* menu)
{
    g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU(menu), 0);

    GList* last = g_list_last(menu->priv->items);
    return last ? WEBKIT_CONTEXT_MENU_ITEM(last->data) : 0;
}

/**
 * webkit_context_menu_get_item_at_position:
 * @menu: a #WebKitContextMenu
 * @position: the position of the item, counting from 0
 *
 * Gets the item at the given position in the @menu.
 *
 * Returns: (transfer none): the #WebKitContextMenuItem at position @position in @menu,
 *    or %NULL if the position is off the end of the @menu.
 */
WebKitContextMenuItem* webkit_context_menu_get_item_at_position(WebKitContextMenu* menu, unsigned position)
{
    g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU(menu), 0);

    gpointer item = g_list_nth_data(menu->priv->items, position);
    return item ? WEBKIT_CONTEXT_MENU_ITEM(item) : 0;
}

/**
 * webkit_context_menu_remove:
 * @menu: a #WebKitContextMenu
 * @item: the #WebKitContextMenuItem to remove
 *
 * Removes @item from the @menu.
 *
 * See also webkit_context_menu_remove_all() to remove all items.
 */
void webkit_context_menu_remove(WebKitContextMenu* menu, WebKitContextMenuItem* item)
{
    g_return_if_fail(WEBKIT_IS_CONTEXT_MENU(menu));
    g_return_if_fail(WEBKIT_IS_CONTEXT_MENU_ITEM(item));

    if (!g_list_find(menu->priv->items, item))
        return;

    menu->priv->items = g_list_remove(menu->priv->items, item);
    g_object_unref(item);
}

/**
 * webkit_context_menu_remove_all:
 * @menu: a #WebKitContextMenu
 *
 * Removes all items of the @menu.
 */
void webkit_context_menu_remove_all(WebKitContextMenu* menu)
{
    g_return_if_fail(WEBKIT_IS_CONTEXT_MENU(menu));

    g_list_free_full(menu->priv->items, reinterpret_cast<GDestroyNotify>(g_object_unref));
    menu->priv->items = 0;
}

/**
 * webkit_context_menu_set_user_data:
 * @menu: a #WebKitContextMenu
 * @user_data: a #GVariant
 *
 * Sets user data to @menu.
 *
 * This function can be used from a Web Process extension to set user data
 * that can be retrieved from the UI Process using webkit_context_menu_get_user_data().
 * If the @user_data #GVariant is floating, it is consumed.
 *
 * Since: 2.8
 */
void webkit_context_menu_set_user_data(WebKitContextMenu* menu, GVariant* userData)
{
    g_return_if_fail(WEBKIT_IS_CONTEXT_MENU(menu));
    g_return_if_fail(userData);

    menu->priv->userData = userData;
}

/**
 * webkit_context_menu_get_user_data:
 * @menu: a #WebKitContextMenu
 *
 * Gets the user data of @menu.
 *
 * This function can be used from the UI Process to get user data previously set
 * from the Web Process with webkit_context_menu_set_user_data().
 *
 * Returns: (transfer none): the user data of @menu, or %NULL if @menu doesn't have user data
 *
 * Since: 2.8
 */
GVariant* webkit_context_menu_get_user_data(WebKitContextMenu* menu)
{
    g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU(menu), nullptr);

    return menu->priv->userData.get();
}

#if PLATFORM(GTK)
/**
 * webkit_context_menu_get_event:
 * @menu: a #WebKitContextMenu
 *
 * Gets the #GdkEvent that triggered the context menu. This function only returns a valid
 * #GdkEvent when called for a #WebKitContextMenu passed to #WebKitWebView::context-menu
 * signal; in all other cases, %NULL is returned.
 *
 * The returned #GdkEvent is expected to be one of the following types:
 * <itemizedlist>
 * <listitem><para>
 * a #GdkEventButton of type %GDK_BUTTON_PRESS when the context menu was triggered with mouse.
 * </para></listitem>
 * <listitem><para>
 * a #GdkEventKey of type %GDK_KEY_PRESS if the keyboard was used to show the menu.
 * </para></listitem>
 * <listitem><para>
 * a generic #GdkEvent of type %GDK_NOTHING when the #GtkWidget::popup-menu signal was used to show the context menu.
 * </para></listitem>
 * </itemizedlist>
 *
 * Returns: (transfer none): the menu event or %NULL.
 *
 * Since: 2.40
 */
GdkEvent* webkit_context_menu_get_event(WebKitContextMenu* menu)
{
    g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU(menu), nullptr);

    return menu->priv->event.get();
}
#endif