1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/*
* Copyright (C) 2011 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#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/WindowsKeyboardCodes.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);
/* First try to interpret the command in the UI and get the commands.
UI needs to receive event type because only knows current NativeWebKeyboardEvent.*/
WebProcess::shared().parentProcessConnection()->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;
// If this was an IME event don't do anything.
if (platformEvent->windowsVirtualKeyCode() == VK_PROCESSKEY)
return;
Vector<WTF::String> pendingEditorCommands;
getEditorCommandsForKeyEvent(event, pendingEditorCommands);
if (!pendingEditorCommands.isEmpty()) {
// During RawKeyDown events if an editor command will insert text, defer
// the insertion until the keypress event. We want keydown to bubble up
// through the DOM first.
if (platformEvent->type() == PlatformEvent::RawKeyDown) {
if (executePendingEditorCommands(frame, pendingEditorCommands, false))
event->setDefaultHandled();
return;
}
// Only allow text insertion commands if the current node is editable.
if (executePendingEditorCommands(frame, pendingEditorCommands, frame->editor().canEdit())) {
event->setDefaultHandled();
return;
}
}
// Don't allow text insertion for nodes that cannot edit.
if (!frame->editor().canEdit())
return;
// This is just a normal text insertion, so wait to execute the insertion
// until a keypress event happens. This will ensure that the insertion will not
// be reflected in the contents of the field until the keyup DOM event.
if (event->type() != eventNames().keypressEvent)
return;
// Don't insert null or control characters as they can result in unexpected behaviour
if (event->charCode() < ' ')
return;
// Don't insert anything if a modifier is pressed
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->windowsVirtualKeyCode() == VK_PROCESSKEY)
event->preventDefault();
}
#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)
{
// Calling delete void* will free the memory but won't invoke
// the destructor, something that is a must for us.
EditorClientFrameDestructionObserver* observer = static_cast<EditorClientFrameDestructionObserver*>(data);
delete observer;
}
};
static Frame* frameSettingClipboard;
static void collapseSelection(GtkClipboard* clipboard, Frame* frame)
{
if (frameSettingClipboard && frameSettingClipboard == frame)
return;
// Collapse the selection without clearing it.
ASSERT(frame);
frame->selection()->setBase(frame->selection()->extent(), frame->selection()->affinity());
}
#endif
void WebEditorClient::updateGlobalSelection(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);
// This observer will be self-destroyed on closure finalization,
// that will happen either after closure execution or after
// closure invalidation.
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;
}
}
|