1 | /* |
2 | * Copyright (C) 2012 Igalia S.L. |
3 | * |
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. |
8 | * |
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. |
13 | * |
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. |
18 | */ |
19 | |
20 | #include "config.h" |
21 | #include "WebKitContextMenuItem.h" |
22 | |
23 | #include "APIArray.h" |
24 | #include "WebContextMenuItem.h" |
25 | #include "WebContextMenuItemGlib.h" |
26 | #include "WebKitContextMenuActionsPrivate.h" |
27 | #include "WebKitContextMenuItemPrivate.h" |
28 | #include "WebKitContextMenuPrivate.h" |
29 | #include <WebCore/ContextMenu.h> |
30 | #include <WebCore/ContextMenuItem.h> |
31 | #include <memory> |
32 | #include <wtf/glib/GRefPtr.h> |
33 | #include <wtf/glib/GUniquePtr.h> |
34 | #include <wtf/glib/WTFGType.h> |
35 | |
36 | using namespace WebKit; |
37 | using namespace WebCore; |
38 | |
39 | /** |
40 | * SECTION: WebKitContextMenuItem |
41 | * @Short_description: One item of the #WebKitContextMenu |
42 | * @Title: WebKitContextMenuItem |
43 | * |
44 | * The #WebKitContextMenu is composed of #WebKitContextMenuItem<!-- |
45 | * -->s. These items can be created from a #GtkAction, from a |
46 | * #WebKitContextMenuAction or from a #WebKitContextMenuAction and a |
47 | * label. These #WebKitContextMenuAction<!-- -->s denote stock actions |
48 | * for the items. You can also create separators and submenus. |
49 | * |
50 | */ |
51 | |
52 | struct { |
53 | () |
54 | { |
55 | if (subMenu) |
56 | webkitContextMenuSetParentItem(subMenu.get(), 0); |
57 | } |
58 | |
59 | std::unique_ptr<WebContextMenuItemGlib> ; |
60 | GRefPtr<WebKitContextMenu> ; |
61 | }; |
62 | |
63 | WEBKIT_DEFINE_TYPE(WebKitContextMenuItem, webkit_context_menu_item, G_TYPE_INITIALLY_UNOWNED) |
64 | |
65 | static void (WebKitContextMenuItemClass*) |
66 | { |
67 | } |
68 | |
69 | static bool checkAndWarnIfMenuHasParentItem(WebKitContextMenu* ) |
70 | { |
71 | if (menu && webkitContextMenuGetParentItem(menu)) { |
72 | g_warning("Attempting to set a WebKitContextMenu as submenu of " |
73 | "a WebKitContextMenuItem, but the menu is already " |
74 | "a submenu of a WebKitContextMenuItem" ); |
75 | return true; |
76 | } |
77 | |
78 | return false; |
79 | } |
80 | |
81 | static void (WebKitContextMenuItem* item, GRefPtr<WebKitContextMenu> ) |
82 | { |
83 | if (checkAndWarnIfMenuHasParentItem(subMenu.get())) |
84 | return; |
85 | |
86 | if (item->priv->subMenu) |
87 | webkitContextMenuSetParentItem(item->priv->subMenu.get(), nullptr); |
88 | item->priv->subMenu = subMenu; |
89 | if (subMenu) |
90 | webkitContextMenuSetParentItem(subMenu.get(), item); |
91 | } |
92 | |
93 | WebKitContextMenuItem* (const WebContextMenuItemData& itemData) |
94 | { |
95 | WebKitContextMenuItem* item = WEBKIT_CONTEXT_MENU_ITEM(g_object_new(WEBKIT_TYPE_CONTEXT_MENU_ITEM, NULL)); |
96 | |
97 | item->priv->menuItem = std::make_unique<WebContextMenuItemGlib>(itemData); |
98 | const Vector<WebContextMenuItemData>& = itemData.submenu(); |
99 | if (!subMenu.isEmpty()) |
100 | webkitContextMenuItemSetSubMenu(item, adoptGRef(webkitContextMenuCreate(subMenu))); |
101 | |
102 | return item; |
103 | } |
104 | |
105 | WebContextMenuItemGlib (WebKitContextMenuItem* item) |
106 | { |
107 | if (item->priv->subMenu) { |
108 | Vector<WebContextMenuItemGlib> ; |
109 | webkitContextMenuPopulate(item->priv->subMenu.get(), subMenuItems); |
110 | return WebContextMenuItemGlib(*item->priv->menuItem, WTFMove(subMenuItems)); |
111 | } |
112 | |
113 | return *item->priv->menuItem; |
114 | } |
115 | |
116 | WebContextMenuItemData (WebKitContextMenuItem* item) |
117 | { |
118 | if (item->priv->subMenu) { |
119 | Vector<WebContextMenuItemData> ; |
120 | webkitContextMenuPopulate(item->priv->subMenu.get(), subMenuItems); |
121 | return WebContextMenuItemData(item->priv->menuItem->action(), item->priv->menuItem->title(), item->priv->menuItem->enabled(), subMenuItems); |
122 | } |
123 | |
124 | return WebContextMenuItemData(item->priv->menuItem->type(), item->priv->menuItem->action(), item->priv->menuItem->title(), item->priv->menuItem->enabled(), item->priv->menuItem->checked()); |
125 | } |
126 | |
127 | #if PLATFORM(GTK) |
128 | /** |
129 | * webkit_context_menu_item_new: |
130 | * @action: a #GtkAction |
131 | * |
132 | * Creates a new #WebKitContextMenuItem for the given @action. |
133 | * |
134 | * Returns: the newly created #WebKitContextMenuItem object. |
135 | * |
136 | * Deprecated: 2.18: Use webkit_context_menu_item_new_from_gaction() instead. |
137 | */ |
138 | WebKitContextMenuItem* (GtkAction* action) |
139 | { |
140 | g_return_val_if_fail(GTK_IS_ACTION(action), nullptr); |
141 | |
142 | WebKitContextMenuItem* item = WEBKIT_CONTEXT_MENU_ITEM(g_object_new(WEBKIT_TYPE_CONTEXT_MENU_ITEM, nullptr)); |
143 | item->priv->menuItem = std::make_unique<WebContextMenuItemGlib>(action); |
144 | |
145 | return item; |
146 | } |
147 | #endif |
148 | |
149 | /** |
150 | * webkit_context_menu_item_new_from_gaction: |
151 | * @action: a #GAction |
152 | * @label: the menu item label text |
153 | * @target: (allow-none): a #GVariant to use as the action target |
154 | * |
155 | * Creates a new #WebKitContextMenuItem for the given @action and @label. On activation |
156 | * @target will be passed as parameter to the callback. |
157 | * |
158 | * Returns: the newly created #WebKitContextMenuItem object. |
159 | * |
160 | * Since: 2.18 |
161 | */ |
162 | WebKitContextMenuItem* (GAction* action, const gchar* label, GVariant* target) |
163 | { |
164 | g_return_val_if_fail(G_IS_ACTION(action), nullptr); |
165 | g_return_val_if_fail(!g_action_get_state_type(action) || g_variant_type_equal(g_action_get_state_type(action), G_VARIANT_TYPE_BOOLEAN), nullptr); |
166 | g_return_val_if_fail(label, nullptr); |
167 | g_return_val_if_fail(!target || g_variant_is_of_type(target, g_action_get_parameter_type(action)), nullptr); |
168 | |
169 | WebKitContextMenuItem* item = WEBKIT_CONTEXT_MENU_ITEM(g_object_new(WEBKIT_TYPE_CONTEXT_MENU_ITEM, nullptr)); |
170 | item->priv->menuItem = std::make_unique<WebContextMenuItemGlib>(action, String::fromUTF8(label), target); |
171 | |
172 | return item; |
173 | } |
174 | |
175 | /** |
176 | * webkit_context_menu_item_new_from_stock_action: |
177 | * @action: a #WebKitContextMenuAction stock action |
178 | * |
179 | * Creates a new #WebKitContextMenuItem for the given stock action. |
180 | * Stock actions are handled automatically by WebKit so that, for example, |
181 | * when a menu item created with a %WEBKIT_CONTEXT_MENU_ACTION_STOP is |
182 | * activated the action associated will be handled by WebKit and the current |
183 | * load operation will be stopped. You can get the #GtkAction of a |
184 | * #WebKitContextMenuItem created with a #WebKitContextMenuAction with |
185 | * webkit_context_menu_item_get_action() and connect to #GtkAction::activate signal |
186 | * to be notified when the item is activated. But you can't prevent the associated |
187 | * action from being performed. |
188 | * |
189 | * Returns: the newly created #WebKitContextMenuItem object. |
190 | */ |
191 | WebKitContextMenuItem* (WebKitContextMenuAction action) |
192 | { |
193 | g_return_val_if_fail(action > WEBKIT_CONTEXT_MENU_ACTION_NO_ACTION && action < WEBKIT_CONTEXT_MENU_ACTION_CUSTOM, nullptr); |
194 | |
195 | WebKitContextMenuItem* item = WEBKIT_CONTEXT_MENU_ITEM(g_object_new(WEBKIT_TYPE_CONTEXT_MENU_ITEM, nullptr)); |
196 | ContextMenuItemType type = webkitContextMenuActionIsCheckable(action) ? CheckableActionType : ActionType; |
197 | item->priv->menuItem = std::make_unique<WebContextMenuItemGlib>(type, webkitContextMenuActionGetActionTag(action), webkitContextMenuActionGetLabel(action)); |
198 | |
199 | return item; |
200 | } |
201 | |
202 | /** |
203 | * webkit_context_menu_item_new_from_stock_action_with_label: |
204 | * @action: a #WebKitContextMenuAction stock action |
205 | * @label: a custom label text to use instead of the predefined one |
206 | * |
207 | * Creates a new #WebKitContextMenuItem for the given stock action using the given @label. |
208 | * Stock actions have a predefined label, this method can be used to create a |
209 | * #WebKitContextMenuItem for a #WebKitContextMenuAction but using a custom label. |
210 | * |
211 | * Returns: the newly created #WebKitContextMenuItem object. |
212 | */ |
213 | WebKitContextMenuItem* (WebKitContextMenuAction action, const gchar* label) |
214 | { |
215 | g_return_val_if_fail(action > WEBKIT_CONTEXT_MENU_ACTION_NO_ACTION && action < WEBKIT_CONTEXT_MENU_ACTION_CUSTOM, nullptr); |
216 | |
217 | WebKitContextMenuItem* item = WEBKIT_CONTEXT_MENU_ITEM(g_object_new(WEBKIT_TYPE_CONTEXT_MENU_ITEM, nullptr)); |
218 | ContextMenuItemType type = webkitContextMenuActionIsCheckable(action) ? CheckableActionType : ActionType; |
219 | item->priv->menuItem = std::make_unique<WebContextMenuItemGlib>(type, webkitContextMenuActionGetActionTag(action), String::fromUTF8(label)); |
220 | |
221 | return item; |
222 | } |
223 | |
224 | /** |
225 | * webkit_context_menu_item_new_with_submenu: |
226 | * @label: the menu item label text |
227 | * @submenu: a #WebKitContextMenu to set |
228 | * |
229 | * Creates a new #WebKitContextMenuItem using the given @label with a submenu. |
230 | * |
231 | * Returns: the newly created #WebKitContextMenuItem object. |
232 | */ |
233 | WebKitContextMenuItem* (const gchar* label, WebKitContextMenu* ) |
234 | { |
235 | g_return_val_if_fail(label, nullptr); |
236 | g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU(submenu), nullptr); |
237 | |
238 | if (checkAndWarnIfMenuHasParentItem(submenu)) |
239 | return nullptr; |
240 | |
241 | WebKitContextMenuItem* item = WEBKIT_CONTEXT_MENU_ITEM(g_object_new(WEBKIT_TYPE_CONTEXT_MENU_ITEM, nullptr)); |
242 | item->priv->menuItem = std::make_unique<WebContextMenuItemGlib>(ActionType, ContextMenuItemBaseApplicationTag, String::fromUTF8(label)); |
243 | item->priv->subMenu = submenu; |
244 | webkitContextMenuSetParentItem(submenu, item); |
245 | |
246 | return item; |
247 | } |
248 | |
249 | /** |
250 | * webkit_context_menu_item_new_separator: |
251 | * |
252 | * Creates a new #WebKitContextMenuItem representing a separator. |
253 | * |
254 | * Returns: the newly created #WebKitContextMenuItem object. |
255 | */ |
256 | WebKitContextMenuItem* (void) |
257 | { |
258 | WebKitContextMenuItem* item = WEBKIT_CONTEXT_MENU_ITEM(g_object_new(WEBKIT_TYPE_CONTEXT_MENU_ITEM, nullptr)); |
259 | item->priv->menuItem = std::make_unique<WebContextMenuItemGlib>(SeparatorType, ContextMenuItemTagNoAction, String()); |
260 | |
261 | return item; |
262 | } |
263 | |
264 | #if PLATFORM(GTK) |
265 | /** |
266 | * webkit_context_menu_item_get_action: |
267 | * @item: a #WebKitContextMenuItem |
268 | * |
269 | * Gets the action associated to @item as a #GtkAction. |
270 | * |
271 | * Returns: (transfer none): the #GtkAction associated to the #WebKitContextMenuItem, |
272 | * or %NULL if @item is a separator. |
273 | * |
274 | * Deprecated: 2.18: Use webkit_context_menu_item_get_gaction() instead. |
275 | */ |
276 | GtkAction* (WebKitContextMenuItem* item) |
277 | { |
278 | g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU_ITEM(item), nullptr); |
279 | |
280 | return item->priv->menuItem->gtkAction(); |
281 | } |
282 | #endif |
283 | |
284 | /** |
285 | * webkit_context_menu_item_get_gaction: |
286 | * @item: a #WebKitContextMenuItem |
287 | * |
288 | * Gets the action associated to @item as a #GAction. |
289 | * |
290 | * Returns: (transfer none): the #GAction associated to the #WebKitContextMenuItem, |
291 | * or %NULL if @item is a separator. |
292 | * |
293 | * Since: 2.18 |
294 | */ |
295 | GAction* (WebKitContextMenuItem* item) |
296 | { |
297 | g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU_ITEM(item), nullptr); |
298 | |
299 | return item->priv->menuItem->gAction(); |
300 | } |
301 | |
302 | /** |
303 | * webkit_context_menu_item_get_stock_action: |
304 | * @item: a #WebKitContextMenuItem |
305 | * |
306 | * Gets the #WebKitContextMenuAction of @item. If the #WebKitContextMenuItem was not |
307 | * created for a stock action %WEBKIT_CONTEXT_MENU_ACTION_CUSTOM will be |
308 | * returned. If the #WebKitContextMenuItem is a separator %WEBKIT_CONTEXT_MENU_ACTION_NO_ACTION |
309 | * will be returned. |
310 | * |
311 | * Returns: the #WebKitContextMenuAction of @item |
312 | */ |
313 | WebKitContextMenuAction (WebKitContextMenuItem* item) |
314 | { |
315 | g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU_ITEM(item), WEBKIT_CONTEXT_MENU_ACTION_NO_ACTION); |
316 | |
317 | return webkitContextMenuActionGetForContextMenuItem(*item->priv->menuItem); |
318 | } |
319 | |
320 | /** |
321 | * webkit_context_menu_item_is_separator: |
322 | * @item: a #WebKitContextMenuItem |
323 | * |
324 | * Checks whether @item is a separator. |
325 | * |
326 | * Returns: %TRUE is @item is a separator or %FALSE otherwise |
327 | */ |
328 | gboolean (WebKitContextMenuItem* item) |
329 | { |
330 | g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU_ITEM(item), FALSE); |
331 | |
332 | return item->priv->menuItem->type() == SeparatorType; |
333 | } |
334 | |
335 | /** |
336 | * webkit_context_menu_item_set_submenu: |
337 | * @item: a #WebKitContextMenuItem |
338 | * @submenu: (allow-none): a #WebKitContextMenu |
339 | * |
340 | * Sets or replaces the @item submenu. If @submenu is %NULL the current |
341 | * submenu of @item is removed. |
342 | */ |
343 | void (WebKitContextMenuItem* item, WebKitContextMenu* ) |
344 | { |
345 | g_return_if_fail(WEBKIT_IS_CONTEXT_MENU_ITEM(item)); |
346 | |
347 | if (item->priv->subMenu == submenu) |
348 | return; |
349 | |
350 | webkitContextMenuItemSetSubMenu(item, submenu); |
351 | } |
352 | |
353 | /** |
354 | * webkit_context_menu_item_get_submenu: |
355 | * @item: a #WebKitContextMenuItem |
356 | * |
357 | * Gets the submenu of @item. |
358 | * |
359 | * Returns: (transfer none): the #WebKitContextMenu representing the submenu of |
360 | * @item or %NULL if @item doesn't have a submenu. |
361 | */ |
362 | WebKitContextMenu* (WebKitContextMenuItem* item) |
363 | { |
364 | g_return_val_if_fail(WEBKIT_IS_CONTEXT_MENU_ITEM(item), 0); |
365 | |
366 | return item->priv->subMenu.get(); |
367 | } |
368 | |
369 | |