[webkit-gtk] Context menu API in WebKit2

Carlos Garcia Campos cgarcia at igalia.com
Wed Mar 7 10:52:49 PST 2012

El mié, 07-03-2012 a las 17:42 +0100, Carlos Garcia Campos escribió:

> We have, at least, the following options:
>  a) Use a GtkMenu and use GtkMenuShell api to add/remove items. Items in
> the default context menu have a GtkAction associated with a name that
> allows us to identify it. This is the approach we decided to follow in
> webkit1, see bug https://bugs.webkit.org/show_bug.cgi?id=67660 
>  b) Use a GtkUIManager. The default context menu would define a XML
> using placeholders for the different action groups (navigation actions,
> editing actions, etc.). The user can add new items to the menu by
> merging their own xml, and adding actions and action groups to the ui
> manager. 
>  c) Use new GMenu API. We would pass a GMenuModel so that user can
> add/remove items using the GMenu API. Default menu would contain
> GActions with a name that allows us to identify it. We could have
> actions groups using GActionGroup.
>  d) Use our thing. Add WebKitContexMenu and WebKitContextMenuIem objects
> with the API needed to meet all the requirements.

I'm replying myself to propose a solution based in option d)

WebContextMenuProxy object is created by WebPageProxy when
ShowContextMenu message is received. The context menu proxy object is
created by the PageClientImpl giving a WebKitWebViewBase and
WebPageProxy as constructor parameters. So we can add a method to
WebKitWebViewBase to set the active context menu. Then
WebContextMenuProxy calls webkitWebViewBaseSetActiveContextMenu(webView,
this); in the constructor and
webkitWebViewBaseSetActiveContextMenu(webView, 0); in the destructor.
When getContextMenuFromProposedMenu callback is called in WebKitWebView
context menu client implementation, the newly created context menu proxy
has already been set in WebKitWebViewBase, so we can just get it with

This would allow us to use the WebContextMenuProxy as backend to create
the GtkMenu. We create a menu with the default items using our custom
API. Then we add the options depending on hit test result (the unicode
menu for editable content, like wk1 does, for example) and pass that to
the user in the context-menu signal. The user can modify the menu in the
signal callback and after the signal has been emitted we have the final
menu to show. We need to add API to WebContextMenuProxy to be able to
create the GtkMenu. So, when getContextMenuFromProposedMenu callback
returns, the WebContextMenuProxy already contains the menu, so that when
showContextMenu is called, the given items are ignored and it simply
calls gtk_menu_popup(). 

The custom API would be something like this:

void webkit_context_menu_append (WebKitContextMenu *context_menu, WebKitContextMenuItem, *item);
void webkit_context_menu_prepend (WebKitContextMenu *context_menu, WebKitContextMenuItem, *item);
void webkit_context_menu_insert (WebKitContextMenu *context_menu, WebKitContextMenuItem, *item, guint position);
GList *webkit_context_menu_get_items (WebKitContextMenu *context_menu);

WebKitContextMenuItem *webkit_context_menu_item_new (GtkAction *action);
WebKitContextMenuItem *webkit_context_menu_item_from_stock (const gchar *stock_id);
WebKitContextMenuItem *webkit_context_menu_item_new_separator ();
GtkAction *webkit_context_menu_item_get_action (WebKitContextMenuItem *item);
gboolean webkit_context_menu_item_is_separator (WebKitContextMenuItem *item);
void webkit_context_menu_item_set_submenu (WebKitContextMenuItem *item, WebKitContextMenu *sub_menu);
WebKitContextMenuItem *webkit_context_menu_item_get_submenu (WebKitContextMenuItem *item);

The user can create custom menu items creating a new GtkAction and
connect to "activate" signal, or create a predefined menu item and get
the associated action to handle it. That would be the basic API, we
could add convenient methods to check whether a given action is present
in the menu and things like that.

Carlos Garcia Campos
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://lists.webkit.org/pipermail/webkit-gtk/attachments/20120307/48182cfe/attachment.bin>

More information about the webkit-gtk mailing list