2 * Copyright (C) 2008 Nuanti Ltd.
3 * Copyright (C) 2009 Gustavo Noronha Silva <gns@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser 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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "ContextMenuClientGtk.h"
23 #include "ContextMenu.h"
24 #include "ContextMenuController.h"
25 #include "HitTestResult.h"
27 #include "NotImplemented.h"
29 #include "webkitwebviewprivate.h"
30 #include <glib-object.h>
31 #include <glib/gi18n-lib.h>
33 #include <wtf/text/CString.h>
35 using namespace WebCore;
39 ContextMenuClient::ContextMenuClient(WebKitWebView *webView)
44 void ContextMenuClient::contextMenuDestroyed()
49 static GtkWidget* inputMethodsMenuItem (WebKitWebView* webView)
51 if (gtk_major_version > 2 || (gtk_major_version == 2 && gtk_minor_version >= 10)) {
52 GtkSettings* settings = webView ? gtk_widget_get_settings(GTK_WIDGET(webView)) : gtk_settings_get_default();
54 gboolean showMenu = TRUE;
56 g_object_get(settings, "gtk-show-input-method-menu", &showMenu, NULL);
61 GtkWidget* menuitem = gtk_image_menu_item_new_with_mnemonic(
64 WebKitWebViewPrivate* priv = webView->priv;
65 GtkWidget* imContextMenu = gtk_menu_new();
66 gtk_im_multicontext_append_menuitems(GTK_IM_MULTICONTEXT(priv->imContext.get()), GTK_MENU_SHELL(imContextMenu));
68 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), imContextMenu);
73 // Values taken from gtktextutil.c
77 } GtkUnicodeMenuEntry;
78 static const GtkUnicodeMenuEntry bidi_menu_entries[] = {
79 { N_("LRM _Left-to-right mark"), 0x200E },
80 { N_("RLM _Right-to-left mark"), 0x200F },
81 { N_("LRE Left-to-right _embedding"), 0x202A },
82 { N_("RLE Right-to-left e_mbedding"), 0x202B },
83 { N_("LRO Left-to-right _override"), 0x202D },
84 { N_("RLO Right-to-left o_verride"), 0x202E },
85 { N_("PDF _Pop directional formatting"), 0x202C },
86 { N_("ZWS _Zero width space"), 0x200B },
87 { N_("ZWJ Zero width _joiner"), 0x200D },
88 { N_("ZWNJ Zero width _non-joiner"), 0x200C }
91 static void insertControlCharacter(GtkWidget* widget)
93 // GtkUnicodeMenuEntry* entry = (GtkUnicodeMenuEntry*)g_object_get_data(G_OBJECT(widget), "gtk-unicode-menu-entry");
97 static GtkWidget* unicodeMenuItem(WebKitWebView* webView)
99 if (gtk_major_version > 2 || (gtk_major_version == 2 && gtk_minor_version >= 10)) {
100 GtkSettings* settings = webView ? gtk_widget_get_settings(GTK_WIDGET(webView)) : gtk_settings_get_default();
102 gboolean showMenu = TRUE;
104 g_object_get(settings, "gtk-show-unicode-menu", &showMenu, NULL);
109 GtkWidget* menuitem = gtk_image_menu_item_new_with_mnemonic(
110 _("_Insert Unicode Control Character"));
112 GtkWidget* unicodeContextMenu = gtk_menu_new();
114 for (i = 0; i < G_N_ELEMENTS(bidi_menu_entries); i++) {
115 GtkWidget* menuitem = gtk_menu_item_new_with_mnemonic(_(bidi_menu_entries[i].label));
116 g_object_set_data(G_OBJECT(menuitem), "gtk-unicode-menu-entry", (gpointer)&bidi_menu_entries[i]);
117 g_signal_connect(menuitem, "activate", G_CALLBACK(insertControlCharacter), 0);
118 gtk_widget_show(menuitem);
119 gtk_menu_shell_append(GTK_MENU_SHELL(unicodeContextMenu), menuitem);
120 // FIXME: Make the item sensitive as insertControlCharacter() is implemented
121 gtk_widget_set_sensitive(menuitem, FALSE);
124 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), unicodeContextMenu);
129 PlatformMenuDescription ContextMenuClient::getCustomMenuFromDefaultItems(ContextMenu* menu)
131 GtkMenu* gtkmenu = menu->releasePlatformDescription();
133 WebKitWebView* webView = m_webView;
134 HitTestResult result = core(webView)->contextMenuController()->hitTestResult();
136 if (result.isContentEditable()) {
138 GtkWidget* imContextMenu = inputMethodsMenuItem(webView);
139 GtkWidget* unicodeContextMenu = unicodeMenuItem(webView);
141 if (imContextMenu || unicodeContextMenu) {
142 GtkWidget* separator = gtk_separator_menu_item_new();
143 gtk_menu_shell_append(GTK_MENU_SHELL(gtkmenu), separator);
144 gtk_widget_show(separator);
148 gtk_menu_shell_append(GTK_MENU_SHELL(gtkmenu), imContextMenu);
149 gtk_widget_show(imContextMenu);
152 if (unicodeContextMenu) {
153 gtk_menu_shell_append(GTK_MENU_SHELL(gtkmenu), unicodeContextMenu);
154 gtk_widget_show(unicodeContextMenu);
162 void ContextMenuClient::contextMenuItemSelected(ContextMenuItem*, const ContextMenu*)
167 void ContextMenuClient::downloadURL(const KURL& url)
169 WebKitNetworkRequest* networkRequest = webkit_network_request_new(url.string().utf8().data());
171 webkit_web_view_request_download(m_webView, networkRequest);
172 g_object_unref(networkRequest);
175 void ContextMenuClient::copyImageToClipboard(const HitTestResult&)
180 void ContextMenuClient::searchWithGoogle(const Frame*)
185 void ContextMenuClient::lookUpInDictionary(Frame*)
190 void ContextMenuClient::speak(const String&)
195 void ContextMenuClient::stopSpeaking()
200 bool ContextMenuClient::isSpeaking()