EditorClientGtk.cpp [plain text]
#include "config.h"
#include "EditorClientGtk.h"
#include "DataObjectGtk.h"
#include "DumpRenderTreeSupportGtk.h"
#include "EditCommand.h"
#include "Editor.h"
#include "EventNames.h"
#include "FocusController.h"
#include "Frame.h"
#include <glib.h>
#include "KeyboardEvent.h"
#include "markup.h"
#include "NotImplemented.h"
#include "Page.h"
#include "PasteboardHelperGtk.h"
#include "PlatformKeyboardEvent.h"
#include "WebKitDOMBinding.h"
#include "WebKitDOMCSSStyleDeclarationPrivate.h"
#include "WebKitDOMHTMLElementPrivate.h"
#include "WebKitDOMNodePrivate.h"
#include "WebKitDOMRangePrivate.h"
#include "WindowsKeyboardCodes.h"
#include "webkitglobalsprivate.h"
#include "webkitmarshal.h"
#include "webkitwebsettingsprivate.h"
#include "webkitwebviewprivate.h"
#include <wtf/text/CString.h>
#define maximumUndoStackDepth 1000
using namespace WebCore;
namespace WebKit {
static void imContextCommitted(GtkIMContext* context, const gchar* compositionString, EditorClient* client)
{
Frame* frame = core(static_cast<WebKitWebView*>(client->webView()))->focusController()->focusedOrMainFrame();
if (!frame || !frame->editor()->canEdit())
return;
if (client->treatContextCommitAsKeyEvent()) {
client->updatePendingComposition(compositionString);
return;
}
if (client->preventNextCompositionCommit())
return;
frame->editor()->confirmComposition(String::fromUTF8(compositionString));
client->clearPendingComposition();
}
static void imContextPreeditChanged(GtkIMContext* context, EditorClient* client)
{
Frame* frame = core(static_cast<WebKitWebView*>(client->webView()))->focusController()->focusedOrMainFrame();
if (!frame || !frame->editor()->canEdit())
return;
GOwnPtr<gchar> newPreedit(0);
gtk_im_context_get_preedit_string(context, &newPreedit.outPtr(), 0, 0);
String preeditString = String::fromUTF8(newPreedit.get());
Vector<CompositionUnderline> underlines;
underlines.append(CompositionUnderline(0, preeditString.length(), Color(0, 0, 0), false));
frame->editor()->setComposition(preeditString, underlines, 0, 0);
}
void EditorClient::updatePendingComposition(const gchar* newComposition)
{
if (!m_pendingComposition)
m_pendingComposition.set(g_strdup(newComposition));
else
m_pendingComposition.set(g_strconcat(m_pendingComposition.get(), newComposition, NULL));
}
void EditorClient::willSetInputMethodState()
{
}
void EditorClient::setInputMethodState(bool active)
{
WebKitWebViewPrivate* priv = m_webView->priv;
if (active)
gtk_im_context_focus_in(priv->imContext.get());
else
gtk_im_context_focus_out(priv->imContext.get());
#ifdef MAEMO_CHANGES
if (active)
hildon_gtk_im_context_show(priv->imContext.get());
else
hildon_gtk_im_context_hide(priv->imContext.get());
#endif
}
bool EditorClient::shouldDeleteRange(Range* range)
{
gboolean accept = TRUE;
GRefPtr<WebKitDOMRange> kitRange(adoptGRef(kit(range)));
g_signal_emit_by_name(m_webView, "should-delete-range", kitRange.get(), &accept);
return accept;
}
bool EditorClient::shouldShowDeleteInterface(HTMLElement* element)
{
gboolean accept = TRUE;
GRefPtr<WebKitDOMHTMLElement> kitElement(adoptGRef(kit(element)));
g_signal_emit_by_name(m_webView, "should-show-delete-interface-for-element", kitElement.get(), &accept);
return accept;
}
bool EditorClient::isContinuousSpellCheckingEnabled()
{
WebKitWebSettings* settings = webkit_web_view_get_settings(m_webView);
gboolean enabled;
g_object_get(settings, "enable-spell-checking", &enabled, NULL);
return enabled;
}
bool EditorClient::isGrammarCheckingEnabled()
{
notImplemented();
return false;
}
int EditorClient::spellCheckerDocumentTag()
{
notImplemented();
return 0;
}
bool EditorClient::shouldBeginEditing(WebCore::Range* range)
{
clearPendingComposition();
gboolean accept = TRUE;
GRefPtr<WebKitDOMRange> kitRange(adoptGRef(kit(range)));
g_signal_emit_by_name(m_webView, "should-begin-editing", kitRange.get(), &accept);
return accept;
}
bool EditorClient::shouldEndEditing(WebCore::Range* range)
{
clearPendingComposition();
gboolean accept = TRUE;
GRefPtr<WebKitDOMRange> kitRange(adoptGRef(kit(range)));
g_signal_emit_by_name(m_webView, "should-end-editing", kitRange.get(), &accept);
return accept;
}
static WebKitInsertAction kit(EditorInsertAction action)
{
switch (action) {
case EditorInsertActionTyped:
return WEBKIT_INSERT_ACTION_TYPED;
case EditorInsertActionPasted:
return WEBKIT_INSERT_ACTION_PASTED;
case EditorInsertActionDropped:
return WEBKIT_INSERT_ACTION_DROPPED;
}
ASSERT_NOT_REACHED();
return WEBKIT_INSERT_ACTION_TYPED;
}
bool EditorClient::shouldInsertText(const String& string, Range* range, EditorInsertAction action)
{
gboolean accept = TRUE;
GRefPtr<WebKitDOMRange> kitRange(adoptGRef(kit(range)));
g_signal_emit_by_name(m_webView, "should-insert-text", string.utf8().data(), kitRange.get(), kit(action), &accept);
return accept;
}
static WebKitSelectionAffinity kit(EAffinity affinity)
{
switch (affinity) {
case UPSTREAM:
return WEBKIT_SELECTION_AFFINITY_UPSTREAM;
case DOWNSTREAM:
return WEBKIT_SELECTION_AFFINITY_DOWNSTREAM;
}
ASSERT_NOT_REACHED();
return WEBKIT_SELECTION_AFFINITY_UPSTREAM;
}
bool EditorClient::shouldChangeSelectedRange(Range* fromRange, Range* toRange, EAffinity affinity, bool stillSelecting)
{
gboolean accept = TRUE;
GRefPtr<WebKitDOMRange> kitFromRange(fromRange ? adoptGRef(kit(fromRange)) : 0);
GRefPtr<WebKitDOMRange> kitToRange(toRange ? adoptGRef(kit(toRange)) : 0);
g_signal_emit_by_name(m_webView, "should-change-selected-range", kitFromRange.get(), kitToRange.get(),
kit(affinity), stillSelecting, &accept);
return accept;
}
bool EditorClient::shouldApplyStyle(WebCore::CSSStyleDeclaration* declaration, WebCore::Range* range)
{
gboolean accept = TRUE;
GRefPtr<WebKitDOMCSSStyleDeclaration> kitDeclaration(kit(declaration));
GRefPtr<WebKitDOMRange> kitRange(adoptGRef(kit(range)));
g_signal_emit_by_name(m_webView, "should-apply-style", kitDeclaration.get(), kitRange.get(), &accept);
return accept;
}
bool EditorClient::shouldMoveRangeAfterDelete(WebCore::Range*, WebCore::Range*)
{
notImplemented();
return true;
}
void EditorClient::didBeginEditing()
{
g_signal_emit_by_name(m_webView, "editing-began");
}
void EditorClient::respondToChangedContents()
{
g_signal_emit_by_name(m_webView, "user-changed-contents");
}
static WebKitWebView* viewSettingClipboard = 0;
static void collapseSelection(GtkClipboard* clipboard, WebKitWebView* webView)
{
if (viewSettingClipboard && viewSettingClipboard == webView)
return;
WebCore::Page* corePage = core(webView);
if (!corePage || !corePage->focusController())
return;
Frame* frame = corePage->focusController()->focusedOrMainFrame();
ASSERT(frame);
frame->selection()->setBase(frame->selection()->extent(), frame->selection()->affinity());
}
#if PLATFORM(X11)
static void setSelectionPrimaryClipboardIfNeeded(WebKitWebView* webView)
{
if (!gtk_widget_has_screen(GTK_WIDGET(webView)))
return;
GtkClipboard* clipboard = gtk_widget_get_clipboard(GTK_WIDGET(webView), GDK_SELECTION_PRIMARY);
DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
WebCore::Page* corePage = core(webView);
Frame* targetFrame = corePage->focusController()->focusedOrMainFrame();
if (!targetFrame->selection()->isRange())
return;
dataObject->clear();
dataObject->setRange(targetFrame->selection()->toNormalizedRange());
viewSettingClipboard = webView;
GClosure* callback = g_cclosure_new_object(G_CALLBACK(collapseSelection), G_OBJECT(webView));
g_closure_set_marshal(callback, g_cclosure_marshal_VOID__VOID);
pasteboardHelperInstance()->writeClipboardContents(clipboard, callback);
viewSettingClipboard = 0;
}
#endif
void EditorClient::respondToChangedSelection()
{
g_signal_emit_by_name(m_webView, "selection-changed");
WebKitWebViewPrivate* priv = m_webView->priv;
WebCore::Page* corePage = core(m_webView);
Frame* targetFrame = corePage->focusController()->focusedOrMainFrame();
if (!targetFrame)
return;
if (targetFrame->editor()->ignoreCompositionSelectionChange())
return;
#if PLATFORM(X11)
setSelectionPrimaryClipboardIfNeeded(m_webView);
#endif
if (!targetFrame->editor()->hasComposition())
return;
unsigned start;
unsigned end;
if (!targetFrame->editor()->getCompositionSelection(start, end)) {
gtk_im_context_reset(priv->imContext.get());
targetFrame->editor()->confirmCompositionWithoutDisturbingSelection();
}
}
void EditorClient::didEndEditing()
{
g_signal_emit_by_name(m_webView, "editing-ended");
}
void EditorClient::didWriteSelectionToPasteboard()
{
notImplemented();
}
void EditorClient::didSetSelectionTypesForPasteboard()
{
notImplemented();
}
void EditorClient::registerCommandForUndo(WTF::PassRefPtr<WebCore::EditCommand> command)
{
if (undoStack.size() == maximumUndoStackDepth)
undoStack.removeFirst();
if (!m_isInRedo)
redoStack.clear();
undoStack.append(command);
}
void EditorClient::registerCommandForRedo(WTF::PassRefPtr<WebCore::EditCommand> command)
{
redoStack.append(command);
}
void EditorClient::clearUndoRedoOperations()
{
undoStack.clear();
redoStack.clear();
}
bool EditorClient::canCopyCut(WebCore::Frame*, bool defaultValue) const
{
return defaultValue;
}
bool EditorClient::canPaste(WebCore::Frame*, bool defaultValue) const
{
return defaultValue;
}
bool EditorClient::canUndo() const
{
return !undoStack.isEmpty();
}
bool EditorClient::canRedo() const
{
return !redoStack.isEmpty();
}
void EditorClient::undo()
{
if (canUndo()) {
RefPtr<WebCore::EditCommand> command(*(--undoStack.end()));
undoStack.remove(--undoStack.end());
command->unapply();
}
}
void EditorClient::redo()
{
if (canRedo()) {
RefPtr<WebCore::EditCommand> command(*(--redoStack.end()));
redoStack.remove(--redoStack.end());
ASSERT(!m_isInRedo);
m_isInRedo = true;
command->reapply();
m_isInRedo = false;
}
}
bool EditorClient::shouldInsertNode(Node* node, Range* range, EditorInsertAction action)
{
gboolean accept = TRUE;
GRefPtr<WebKitDOMRange> kitRange(adoptGRef(kit(range)));
GRefPtr<WebKitDOMNode> kitNode(adoptGRef(kit(node)));
g_signal_emit_by_name(m_webView, "should-insert-node", kitNode.get(), kitRange.get(), kit(action), &accept);
return accept;
}
void EditorClient::pageDestroyed()
{
delete this;
}
bool EditorClient::smartInsertDeleteEnabled()
{
notImplemented();
return false;
}
bool EditorClient::isSelectTrailingWhitespaceEnabled()
{
if (!DumpRenderTreeSupportGtk::dumpRenderTreeModeEnabled())
return false;
return DumpRenderTreeSupportGtk::selectTrailingWhitespaceEnabled();
}
void EditorClient::toggleContinuousSpellChecking()
{
WebKitWebSettings* settings = webkit_web_view_get_settings(m_webView);
gboolean enabled;
g_object_get(settings, "enable-spell-checking", &enabled, NULL);
g_object_set(settings, "enable-spell-checking", !enabled, NULL);
}
void EditorClient::toggleGrammarChecking()
{
}
bool EditorClient::executePendingEditorCommands(Frame* frame, bool allowTextInsertion)
{
Vector<Editor::Command> commands;
for (size_t i = 0; i < m_pendingEditorCommands.size(); i++) {
Editor::Command command = frame->editor()->command(m_pendingEditorCommands.at(i).utf8().data());
if (command.isTextInsertion() && !allowTextInsertion)
return false;
commands.append(command);
}
bool success = true;
for (size_t i = 0; i < commands.size(); i++) {
if (!commands.at(i).execute()) {
success = false;
break;
}
}
m_pendingEditorCommands.clear();
if (success)
clearPendingComposition();
return success;
}
void EditorClient::handleKeyboardEvent(KeyboardEvent* event)
{
Node* node = event->target()->toNode();
ASSERT(node);
Frame* frame = node->document()->frame();
ASSERT(frame);
const PlatformKeyboardEvent* platformEvent = event->keyEvent();
if (!platformEvent)
return;
KeyBindingTranslator::EventType type = event->type() == eventNames().keydownEvent ?
KeyBindingTranslator::KeyDown : KeyBindingTranslator::KeyPress;
m_keyBindingTranslator.getEditorCommandsForKeyEvent(platformEvent->gdkEventKey(), type, m_pendingEditorCommands);
if (m_pendingEditorCommands.size() > 0) {
if (platformEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
if (executePendingEditorCommands(frame, false))
event->setDefaultHandled();
return;
}
if (executePendingEditorCommands(frame, frame->editor()->canEdit())) {
event->setDefaultHandled();
return;
}
}
if (!frame->editor()->canEdit())
return;
if (event->type() == eventNames().keypressEvent) {
if (m_pendingComposition) {
frame->editor()->insertText(String::fromUTF8(m_pendingComposition.get()), event);
clearPendingComposition();
event->setDefaultHandled();
} else {
if (event->charCode() < ' ')
return;
if (platformEvent->ctrlKey() || platformEvent->altKey())
return;
if (frame->editor()->insertText(platformEvent->text(), event))
event->setDefaultHandled();
}
}
}
void EditorClient::handleInputMethodKeydown(KeyboardEvent* event)
{
Frame* targetFrame = core(m_webView)->focusController()->focusedOrMainFrame();
if (!targetFrame || !targetFrame->editor()->canEdit())
return;
WebKitWebViewPrivate* priv = m_webView->priv;
m_preventNextCompositionCommit = false;
m_treatContextCommitAsKeyEvent = (!targetFrame->editor()->hasComposition())
&& event->keyEvent()->gdkEventKey()->keyval;
clearPendingComposition();
if ((gtk_im_context_filter_keypress(priv->imContext.get(), event->keyEvent()->gdkEventKey()) && !m_pendingComposition)
|| (!m_treatContextCommitAsKeyEvent && !targetFrame->editor()->hasComposition()))
event->preventDefault();
m_treatContextCommitAsKeyEvent = false;
}
void EditorClient::handleInputMethodMousePress()
{
Frame* targetFrame = core(m_webView)->focusController()->focusedOrMainFrame();
if (!targetFrame || !targetFrame->editor()->canEdit())
return;
WebKitWebViewPrivate* priv = m_webView->priv;
GOwnPtr<gchar> newPreedit(0);
gtk_im_context_get_preedit_string(priv->imContext.get(), &newPreedit.outPtr(), 0, 0);
if (g_utf8_strlen(newPreedit.get(), -1)) {
targetFrame->editor()->confirmComposition();
m_preventNextCompositionCommit = true;
gtk_im_context_reset(priv->imContext.get());
}
}
EditorClient::EditorClient(WebKitWebView* webView)
: m_isInRedo(false)
#if ENABLE(SPELLCHECK)
, m_textCheckerClient(webView)
#endif
, m_webView(webView)
, m_preventNextCompositionCommit(false)
, m_treatContextCommitAsKeyEvent(false)
{
WebKitWebViewPrivate* priv = m_webView->priv;
g_signal_connect(priv->imContext.get(), "commit", G_CALLBACK(imContextCommitted), this);
g_signal_connect(priv->imContext.get(), "preedit-changed", G_CALLBACK(imContextPreeditChanged), this);
}
EditorClient::~EditorClient()
{
WebKitWebViewPrivate* priv = m_webView->priv;
g_signal_handlers_disconnect_by_func(priv->imContext.get(), (gpointer)imContextCommitted, this);
g_signal_handlers_disconnect_by_func(priv->imContext.get(), (gpointer)imContextPreeditChanged, this);
}
void EditorClient::textFieldDidBeginEditing(Element*)
{
}
void EditorClient::textFieldDidEndEditing(Element*)
{
}
void EditorClient::textDidChangeInTextField(Element*)
{
}
bool EditorClient::doTextFieldCommandFromEvent(Element*, KeyboardEvent*)
{
return false;
}
void EditorClient::textWillBeDeletedInTextField(Element*)
{
notImplemented();
}
void EditorClient::textDidChangeInTextArea(Element*)
{
notImplemented();
}
void EditorClient::updateSpellingUIWithGrammarString(const String&, const GrammarDetail&)
{
notImplemented();
}
void EditorClient::updateSpellingUIWithMisspelledWord(const String&)
{
notImplemented();
}
void EditorClient::showSpellingUI(bool)
{
notImplemented();
}
bool EditorClient::spellingUIIsShowing()
{
notImplemented();
return false;
}
}