WebEditorClientGtk.cpp [plain text]
#include "config.h"
#include "WebEditorClient.h"
#include "PlatformKeyboardEvent.h"
#include <WebCore/DataObjectGtk.h>
#include <WebCore/Document.h>
#include <WebCore/Editor.h>
#include <WebCore/Frame.h>
#include <WebCore/FrameDestructionObserver.h>
#include <WebCore/KeyboardEvent.h>
#include <WebCore/Pasteboard.h>
#include <WebCore/markup.h>
#include <wtf/glib/GRefPtr.h>
using namespace WebCore;
namespace WebKit {
bool WebEditorClient::executePendingEditorCommands(Frame* frame, const Vector<WTF::String>& pendingEditorCommands, bool allowTextInsertion)
{
Vector<Editor::Command> commands;
for (auto& commandString : pendingEditorCommands) {
Editor::Command command = frame->editor().command(commandString.utf8().data());
if (command.isTextInsertion() && !allowTextInsertion)
return false;
commands.append(WTF::move(command));
}
for (auto& command : commands) {
if (!command.execute())
return false;
}
return true;
}
void WebEditorClient::handleKeyboardEvent(KeyboardEvent* event)
{
const PlatformKeyboardEvent* platformEvent = event->keyEvent();
if (!platformEvent)
return;
if (platformEvent->handledByInputMethod())
return;
Node* node = event->target()->toNode();
ASSERT(node);
Frame* frame = node->document().frame();
ASSERT(frame);
const Vector<String> pendingEditorCommands = platformEvent->commands();
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)
return;
if (event->charCode() < ' ')
return;
if (platformEvent->ctrlKey() || platformEvent->altKey())
return;
if (frame->editor().insertText(platformEvent->text(), event))
event->setDefaultHandled();
}
void WebEditorClient::handleInputMethodKeydown(KeyboardEvent* event)
{
const PlatformKeyboardEvent* platformEvent = event->keyEvent();
if (platformEvent && platformEvent->handledByInputMethod())
event->setDefaultHandled();
}
#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*)
{
EditorClientFrameDestructionObserver* observer = static_cast<EditorClientFrameDestructionObserver*>(data);
delete observer;
}
};
static Frame* frameSettingClipboard;
static void collapseSelection(GtkClipboard*, Frame* frame)
{
if (frameSettingClipboard && frameSettingClipboard == frame)
return;
ASSERT(frame);
const VisibleSelection& selection = frame->selection().selection();
frame->selection().setBase(selection.extent(), selection.affinity());
}
#endif
void WebEditorClient::updateGlobalSelection(Frame* frame)
{
#if PLATFORM(X11)
if (!frame->selection().isRange())
return;
frameSettingClipboard = frame;
GRefPtr<GClosure> callback = adoptGRef(g_cclosure_new(G_CALLBACK(collapseSelection), frame, nullptr));
new EditorClientFrameDestructionObserver(frame, callback.get());
g_closure_set_marshal(callback.get(), g_cclosure_marshal_VOID__VOID);
RefPtr<Range> range = frame->selection().toNormalizedRange();
PasteboardWebContent pasteboardContent;
pasteboardContent.canSmartCopyOrDelete = false;
pasteboardContent.text = range->text();
pasteboardContent.markup = createMarkup(*range, nullptr, AnnotateForInterchange, false, ResolveNonLocalURLs);
pasteboardContent.callback = callback;
Pasteboard::createForGlobalSelection()->write(pasteboardContent);
frameSettingClipboard = nullptr;
#endif
}
bool WebEditorClient::shouldShowUnicodeMenu()
{
return true;
}
}