1/*
2 *  Copyright (C) 2008 Nuanti Ltd.
3 *  Copyright (C) 2009 Gustavo Noronha Silva <gns@gnome.org>
4 *
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.
9 *
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.
14 *
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
18 */
19
20#include "config.h"
21#include "ContextMenu.h"
22#include "ContextMenuClientGtk.h"
23
24#include "CString.h"
25#include "HitTestResult.h"
26#include "KURL.h"
27#include "NotImplemented.h"
28
29#include <glib/gi18n-lib.h>
30#include <glib-object.h>
31#include <gtk/gtk.h>
32#include "webkitprivate.h"
33
34using namespace WebCore;
35
36namespace WebKit {
37
38ContextMenuClient::ContextMenuClient(WebKitWebView *webView)
39    : m_webView(webView)
40{
41}
42
43void ContextMenuClient::contextMenuDestroyed()
44{
45    delete this;
46}
47
48static GtkWidget* inputMethodsMenuItem (WebKitWebView* webView)
49{
50    if (gtk_major_version > 2 || (gtk_major_version == 2 && gtk_minor_version >= 10)) {
51        GtkSettings* settings = webView ? gtk_widget_get_settings(GTK_WIDGET(webView)) : gtk_settings_get_default();
52
53        gboolean showMenu = TRUE;
54        if (settings)
55            g_object_get(settings, "gtk-show-input-method-menu", &showMenu, NULL);
56        if (!showMenu)
57            return 0;
58    }
59
60    GtkWidget* menuitem = gtk_image_menu_item_new_with_mnemonic(
61        _("Input _Methods"));
62
63    WebKitWebViewPrivate* priv = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
64    GtkWidget* imContextMenu = gtk_menu_new();
65    gtk_im_multicontext_append_menuitems(GTK_IM_MULTICONTEXT(priv->imContext), GTK_MENU_SHELL(imContextMenu));
66
67    gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), imContextMenu);
68
69    return menuitem;
70}
71
72// Values taken from gtktextutil.c
73typedef struct {
74  const char *label;
75  gunichar ch;
76} GtkUnicodeMenuEntry;
77static const GtkUnicodeMenuEntry bidi_menu_entries[] = {
78  { N_("LRM _Left-to-right mark"), 0x200E },
79  { N_("RLM _Right-to-left mark"), 0x200F },
80  { N_("LRE Left-to-right _embedding"), 0x202A },
81  { N_("RLE Right-to-left e_mbedding"), 0x202B },
82  { N_("LRO Left-to-right _override"), 0x202D },
83  { N_("RLO Right-to-left o_verride"), 0x202E },
84  { N_("PDF _Pop directional formatting"), 0x202C },
85  { N_("ZWS _Zero width space"), 0x200B },
86  { N_("ZWJ Zero width _joiner"), 0x200D },
87  { N_("ZWNJ Zero width _non-joiner"), 0x200C }
88};
89
90static void insertControlCharacter(GtkWidget* widget)
91{
92    // GtkUnicodeMenuEntry* entry = (GtkUnicodeMenuEntry*)g_object_get_data(G_OBJECT(widget), "gtk-unicode-menu-entry");
93    notImplemented();
94}
95
96static GtkWidget* unicodeMenuItem(WebKitWebView* webView)
97{
98    if (gtk_major_version > 2 || (gtk_major_version == 2 && gtk_minor_version >= 10)) {
99        GtkSettings* settings = webView ? gtk_widget_get_settings(GTK_WIDGET(webView)) : gtk_settings_get_default();
100
101        gboolean showMenu = TRUE;
102        if (settings)
103            g_object_get(settings, "gtk-show-unicode-menu", &showMenu, NULL);
104        if (!showMenu)
105            return 0;
106    }
107
108    GtkWidget* menuitem = gtk_image_menu_item_new_with_mnemonic(
109        _("_Insert Unicode Control Character"));
110
111    GtkWidget* unicodeContextMenu = gtk_menu_new();
112    unsigned i;
113    for (i = 0; i < G_N_ELEMENTS(bidi_menu_entries); i++) {
114        GtkWidget* menuitem = gtk_menu_item_new_with_mnemonic(_(bidi_menu_entries[i].label));
115        g_object_set_data(G_OBJECT(menuitem), "gtk-unicode-menu-entry", (gpointer)&bidi_menu_entries[i]);
116        g_signal_connect(menuitem, "activate", G_CALLBACK(insertControlCharacter), 0);
117        gtk_widget_show(menuitem);
118        gtk_menu_shell_append(GTK_MENU_SHELL(unicodeContextMenu), menuitem);
119        // FIXME: Make the item sensitive as insertControlCharacter() is implemented
120        gtk_widget_set_sensitive(menuitem, FALSE);
121    }
122
123    gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), unicodeContextMenu);
124
125    return menuitem;
126}
127
128PlatformMenuDescription ContextMenuClient::getCustomMenuFromDefaultItems(ContextMenu* menu)
129{
130    GtkMenu* gtkmenu = menu->releasePlatformDescription();
131
132    HitTestResult result = menu->hitTestResult();
133    WebKitWebView* webView = m_webView;
134
135    if (result.isContentEditable()) {
136
137        GtkWidget* imContextMenu = inputMethodsMenuItem(webView);
138        GtkWidget* unicodeContextMenu = unicodeMenuItem(webView);
139
140        if (imContextMenu || unicodeContextMenu) {
141            GtkWidget* separator = gtk_separator_menu_item_new();
142            gtk_menu_shell_append(GTK_MENU_SHELL(gtkmenu), separator);
143            gtk_widget_show(separator);
144        }
145
146        if (imContextMenu) {
147            gtk_menu_shell_append(GTK_MENU_SHELL(gtkmenu), imContextMenu);
148            gtk_widget_show(imContextMenu);
149        }
150
151        if (unicodeContextMenu) {
152            gtk_menu_shell_append(GTK_MENU_SHELL(gtkmenu), unicodeContextMenu);
153            gtk_widget_show(unicodeContextMenu);
154        }
155
156    }
157
158    return gtkmenu;
159}
160
161void ContextMenuClient::contextMenuItemSelected(ContextMenuItem*, const ContextMenu*)
162{
163    notImplemented();
164}
165
166void ContextMenuClient::downloadURL(const KURL& url)
167{
168    WebKitNetworkRequest* networkRequest = webkit_network_request_new(url.string().utf8().data());
169
170    webkit_web_view_request_download(m_webView, networkRequest);
171    g_object_unref(networkRequest);
172}
173
174void ContextMenuClient::copyImageToClipboard(const HitTestResult&)
175{
176    notImplemented();
177}
178
179void ContextMenuClient::searchWithGoogle(const Frame*)
180{
181    notImplemented();
182}
183
184void ContextMenuClient::lookUpInDictionary(Frame*)
185{
186    notImplemented();
187}
188
189void ContextMenuClient::speak(const String&)
190{
191    notImplemented();
192}
193
194void ContextMenuClient::stopSpeaking()
195{
196    notImplemented();
197}
198
199bool ContextMenuClient::isSpeaking()
200{
201    notImplemented();
202    return false;
203}
204
205}
206
207