WebEditorClientGtk.cpp [plain text]
#include "config.h"
#include "WebEditorClient.h"
#include "Frame.h"
#include "FrameDestructionObserver.h"
#include "PlatformKeyboardEvent.h"
#include "WebPage.h"
#include "WebPageProxyMessages.h"
#include "WebProcess.h"
#include <WebCore/DataObjectGtk.h>
#include <WebCore/KeyboardEvent.h>
#include <WebCore/PasteboardHelper.h>
#include <WebCore/NotImplemented.h>
using namespace WebCore;
namespace WebKit {
void WebEditorClient::getEditorCommandsForKeyEvent(const KeyboardEvent* event, Vector<WTF::String>& pendingEditorCommands)
{
ASSERT(event->type() == eventNames().keydownEvent || event->type() == eventNames().keypressEvent);
WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::GetEditorCommandsForKeyEvent(event->type()),
Messages::WebPageProxy::GetEditorCommandsForKeyEvent::Reply(pendingEditorCommands),
m_page->pageID(), CoreIPC::Connection::NoTimeout);
}
bool WebEditorClient::executePendingEditorCommands(Frame* frame, Vector<WTF::String> pendingEditorCommands, bool allowTextInsertion)
{
Vector<Editor::Command> commands;
for (size_t i = 0; i < pendingEditorCommands.size(); i++) {
Editor::Command command = frame->editor()->command(pendingEditorCommands.at(i).utf8().data());
if (command.isTextInsertion() && !allowTextInsertion)
return false;
commands.append(command);
}
for (size_t i = 0; i < commands.size(); i++) {
if (!commands.at(i).execute())
return false;
}
return true;
}
void WebEditorClient::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;
Vector<WTF::String> pendingEditorCommands;
getEditorCommandsForKeyEvent(event, pendingEditorCommands);
if (!pendingEditorCommands.isEmpty()) {
if (platformEvent->type() == PlatformEvent::RawKeyDown) {
if (executePendingEditorCommands(frame, pendingEditorCommands, false))
event->setDefaultHandled();
return;
}
if (executePendingEditorCommands(frame, pendingEditorCommands, frame->editor()->canEdit())) {
event->setDefaultHandled();
return;
}
}
if (!frame->editor()->canEdit())
return;
if (event->type() == eventNames().keypressEvent) {
frame->editor()->insertText(platformEvent->text(), event);
event->setDefaultHandled();
} else {
if (event->charCode() < ' ')
return;
if (platformEvent->ctrlKey() || platformEvent->altKey())
return;
if (frame->editor()->insertText(platformEvent->text(), event))
event->setDefaultHandled();
}
}
void WebEditorClient::handleInputMethodKeydown(KeyboardEvent*)
{
notImplemented();
}
#if PLATFORM(X11)
class EditorClientFrameDestructionObserver : FrameDestructionObserver {
public:
EditorClientFrameDestructionObserver(Frame* frame, GClosure* closure)
: FrameDestructionObserver(frame)
, m_closure(closure)
{
g_closure_add_finalize_notifier(m_closure, this, destroyOnClosureFinalization);
}
void frameDestroyed()
{
g_closure_invalidate(m_closure);
FrameDestructionObserver::frameDestroyed();
}
private:
GClosure* m_closure;
static void destroyOnClosureFinalization(gpointer data, GClosure* closure)
{
EditorClientFrameDestructionObserver* observer = static_cast<EditorClientFrameDestructionObserver*>(data);
delete observer;
}
};
static Frame* frameSettingClipboard;
static void collapseSelection(GtkClipboard* clipboard, Frame* frame)
{
if (frameSettingClipboard && frameSettingClipboard == frame)
return;
ASSERT(frame);
frame->selection()->setBase(frame->selection()->extent(), frame->selection()->affinity());
}
#endif
void WebEditorClient::setSelectionPrimaryClipboardIfNeeded(Frame* frame)
{
#if PLATFORM(X11)
GtkClipboard* clipboard = PasteboardHelper::defaultPasteboardHelper()->getPrimarySelectionClipboard(frame);
DataObjectGtk* dataObject = DataObjectGtk::forClipboard(clipboard);
if (!frame->selection()->isRange())
return;
dataObject->clearAll();
dataObject->setRange(frame->selection()->toNormalizedRange());
frameSettingClipboard = frame;
GClosure* callback = g_cclosure_new(G_CALLBACK(collapseSelection), frame, 0);
new EditorClientFrameDestructionObserver(frame, callback);
g_closure_set_marshal(callback, g_cclosure_marshal_VOID__VOID);
PasteboardHelper::defaultPasteboardHelper()->writeClipboardContents(clipboard, PasteboardHelper::DoNotIncludeSmartPaste, callback);
frameSettingClipboard = 0;
#endif
}
bool WebEditorClient::shouldShowUnicodeMenu()
{
return true;
}
}