2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2006 Zack Rusin <zack@kde.org>
4 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
5 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "EditorClientQt.h"
34 #include "CSSStyleDeclaration.h"
36 #include "EditCommandQt.h"
38 #include "FocusController.h"
40 #include "HTMLElement.h"
41 #include "HTMLInputElement.h"
42 #include "HTMLNames.h"
43 #include "KeyboardEvent.h"
44 #include "NotImplemented.h"
46 #include "PlatformKeyboardEvent.h"
47 #include "QWebPageClient.h"
50 #include "SpatialNavigation.h"
51 #include "WindowsKeyboardCodes.h"
53 #include "qwebpage_p.h"
57 #include <wtf/OwnPtr.h>
60 static QString dumpPath(WebCore::Node *node)
62 QString str = node->nodeName();
64 WebCore::Node *parent = node->parentNode();
66 str.append(QLatin1String(" > "));
67 str.append(parent->nodeName());
68 parent = parent->parentNode();
73 static QString dumpRange(WebCore::Range *range)
76 return QLatin1String("(null)");
77 WebCore::ExceptionCode code;
79 QString str = QString::fromLatin1("range from %1 of %2 to %3 of %4")
80 .arg(range->startOffset(code)).arg(dumpPath(range->startContainer(code)))
81 .arg(range->endOffset(code)).arg(dumpPath(range->endContainer(code)));
89 bool EditorClientQt::dumpEditingCallbacks = false;
90 bool EditorClientQt::acceptsEditing = true;
92 using namespace HTMLNames;
94 bool EditorClientQt::shouldDeleteRange(Range* range)
96 if (dumpEditingCallbacks)
97 printf("EDITING DELEGATE: shouldDeleteDOMRange:%s\n", dumpRange(range).toUtf8().constData());
102 bool EditorClientQt::shouldShowDeleteInterface(HTMLElement* element)
104 if (QWebPagePrivate::drtRun)
105 return element->getAttribute(classAttr) == "needsDeletionUI";
109 bool EditorClientQt::isContinuousSpellCheckingEnabled()
111 return m_textCheckerClient.isContinousSpellCheckingEnabled();
114 bool EditorClientQt::isGrammarCheckingEnabled()
116 return m_textCheckerClient.isGrammarCheckingEnabled();
119 int EditorClientQt::spellCheckerDocumentTag()
124 bool EditorClientQt::shouldBeginEditing(WebCore::Range* range)
126 if (dumpEditingCallbacks)
127 printf("EDITING DELEGATE: shouldBeginEditingInDOMRange:%s\n", dumpRange(range).toUtf8().constData());
131 bool EditorClientQt::shouldEndEditing(WebCore::Range* range)
133 if (dumpEditingCallbacks)
134 printf("EDITING DELEGATE: shouldEndEditingInDOMRange:%s\n", dumpRange(range).toUtf8().constData());
138 bool EditorClientQt::shouldInsertText(const String& string, Range* range, EditorInsertAction action)
140 if (dumpEditingCallbacks) {
141 static const char *insertactionstring[] = {
142 "WebViewInsertActionTyped",
143 "WebViewInsertActionPasted",
144 "WebViewInsertActionDropped",
147 printf("EDITING DELEGATE: shouldInsertText:%s replacingDOMRange:%s givenAction:%s\n",
148 QString(string).toUtf8().constData(), dumpRange(range).toUtf8().constData(), insertactionstring[action]);
150 return acceptsEditing;
153 bool EditorClientQt::shouldChangeSelectedRange(Range* currentRange, Range* proposedRange, EAffinity selectionAffinity, bool stillSelecting)
155 if (dumpEditingCallbacks) {
156 static const char *affinitystring[] = {
157 "NSSelectionAffinityUpstream",
158 "NSSelectionAffinityDownstream"
160 static const char *boolstring[] = {
165 printf("EDITING DELEGATE: shouldChangeSelectedDOMRange:%s toDOMRange:%s affinity:%s stillSelecting:%s\n",
166 dumpRange(currentRange).toUtf8().constData(),
167 dumpRange(proposedRange).toUtf8().constData(),
168 affinitystring[selectionAffinity], boolstring[stillSelecting]);
170 return acceptsEditing;
173 bool EditorClientQt::shouldApplyStyle(WebCore::CSSStyleDeclaration* style,
174 WebCore::Range* range)
176 if (dumpEditingCallbacks)
177 printf("EDITING DELEGATE: shouldApplyStyle:%s toElementsInDOMRange:%s\n",
178 QString(style->cssText()).toUtf8().constData(), dumpRange(range).toUtf8().constData());
179 return acceptsEditing;
182 bool EditorClientQt::shouldMoveRangeAfterDelete(WebCore::Range*, WebCore::Range*)
188 void EditorClientQt::didBeginEditing()
190 if (dumpEditingCallbacks)
191 printf("EDITING DELEGATE: webViewDidBeginEditing:WebViewDidBeginEditingNotification\n");
195 void EditorClientQt::respondToChangedContents()
197 if (dumpEditingCallbacks)
198 printf("EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification\n");
199 m_page->d->updateEditorActions();
201 emit m_page->contentsChanged();
204 void EditorClientQt::respondToChangedSelection()
206 if (dumpEditingCallbacks)
207 printf("EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification\n");
208 // const Selection &selection = m_page->d->page->selection();
209 // char buffer[1024];
210 // selection.formatForDebugger(buffer, sizeof(buffer));
211 // printf("%s\n", buffer);
213 m_page->d->updateEditorActions();
214 emit m_page->selectionChanged();
215 Frame* frame = m_page->d->page->focusController()->focusedOrMainFrame();
216 if (!frame->editor()->ignoreCompositionSelectionChange())
217 emit m_page->microFocusChanged();
220 void EditorClientQt::didEndEditing()
222 if (dumpEditingCallbacks)
223 printf("EDITING DELEGATE: webViewDidEndEditing:WebViewDidEndEditingNotification\n");
227 void EditorClientQt::didWriteSelectionToPasteboard()
231 void EditorClientQt::didSetSelectionTypesForPasteboard()
235 bool EditorClientQt::selectWordBeforeMenuEvent()
241 void EditorClientQt::registerCommandForUndo(WTF::PassRefPtr<WebCore::EditCommand> cmd)
243 #ifndef QT_NO_UNDOSTACK
244 Frame* frame = m_page->d->page->focusController()->focusedOrMainFrame();
245 if (m_inUndoRedo || (frame && !frame->editor()->lastEditCommand() /* HACK!! Don't recreate undos */))
247 m_page->undoStack()->push(new EditCommandQt(cmd));
248 #endif // QT_NO_UNDOSTACK
251 void EditorClientQt::registerCommandForRedo(WTF::PassRefPtr<WebCore::EditCommand>)
255 void EditorClientQt::clearUndoRedoOperations()
257 #ifndef QT_NO_UNDOSTACK
258 return m_page->undoStack()->clear();
262 bool EditorClientQt::canCopyCut(WebCore::Frame*, bool defaultValue) const
267 bool EditorClientQt::canPaste(WebCore::Frame*, bool defaultValue) const
272 bool EditorClientQt::canUndo() const
274 #ifdef QT_NO_UNDOSTACK
277 return m_page->undoStack()->canUndo();
281 bool EditorClientQt::canRedo() const
283 #ifdef QT_NO_UNDOSTACK
286 return m_page->undoStack()->canRedo();
290 void EditorClientQt::undo()
292 #ifndef QT_NO_UNDOSTACK
294 m_page->undoStack()->undo();
295 m_inUndoRedo = false;
299 void EditorClientQt::redo()
301 #ifndef QT_NO_UNDOSTACK
303 m_page->undoStack()->redo();
304 m_inUndoRedo = false;
308 bool EditorClientQt::shouldInsertNode(Node* node, Range* range, EditorInsertAction action)
310 if (dumpEditingCallbacks) {
311 static const char *insertactionstring[] = {
312 "WebViewInsertActionTyped",
313 "WebViewInsertActionPasted",
314 "WebViewInsertActionDropped",
317 printf("EDITING DELEGATE: shouldInsertNode:%s replacingDOMRange:%s givenAction:%s\n", dumpPath(node).toUtf8().constData(),
318 dumpRange(range).toUtf8().constData(), insertactionstring[action]);
320 return acceptsEditing;
323 void EditorClientQt::pageDestroyed()
328 bool EditorClientQt::smartInsertDeleteEnabled()
330 return m_page->d->smartInsertDeleteEnabled;
333 void EditorClientQt::toggleSmartInsertDelete()
335 bool current = m_page->d->smartInsertDeleteEnabled;
336 m_page->d->smartInsertDeleteEnabled = !current;
339 bool EditorClientQt::isSelectTrailingWhitespaceEnabled()
341 return m_page->d->selectTrailingWhitespaceEnabled;
344 void EditorClientQt::toggleContinuousSpellChecking()
346 m_textCheckerClient.toggleContinousSpellChecking();
349 void EditorClientQt::toggleGrammarChecking()
351 return m_textCheckerClient.toggleGrammarChecking();
354 static const unsigned CtrlKey = 1 << 0;
355 static const unsigned AltKey = 1 << 1;
356 static const unsigned ShiftKey = 1 << 2;
358 struct KeyDownEntry {
361 const char* editorCommand;
364 // Handle here key down events that are needed for spatial navigation and caret browsing, or
365 // are not handled by QWebPage.
366 static const KeyDownEntry keyDownEntries[] = {
367 // Ones that do not have an associated QAction:
368 { VK_DELETE, 0, "DeleteForward" },
369 { VK_BACK, ShiftKey, "DeleteBackward" },
370 { VK_BACK, 0, "DeleteBackward" },
371 // Ones that need special handling for caret browsing:
372 { VK_PRIOR, 0, "MovePageUp" },
373 { VK_PRIOR, ShiftKey, "MovePageUpAndModifySelection" },
374 { VK_NEXT, 0, "MovePageDown" },
375 { VK_NEXT, ShiftKey, "MovePageDownAndModifySelection" },
376 // Ones that need special handling for spatial navigation:
377 { VK_LEFT, 0, "MoveLeft" },
378 { VK_RIGHT, 0, "MoveRight" },
379 { VK_UP, 0, "MoveUp" },
380 { VK_DOWN, 0, "MoveDown" },
383 const char* editorCommandForKeyDownEvent(const KeyboardEvent* event)
385 if (event->type() != eventNames().keydownEvent)
388 static HashMap<int, const char*> keyDownCommandsMap;
389 if (keyDownCommandsMap.isEmpty()) {
391 unsigned numEntries = sizeof(keyDownEntries) / sizeof((keyDownEntries)[0]);
392 for (unsigned i = 0; i < numEntries; i++)
393 keyDownCommandsMap.set(keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey, keyDownEntries[i].editorCommand);
396 unsigned modifiers = 0;
397 if (event->shiftKey())
398 modifiers |= ShiftKey;
401 if (event->ctrlKey())
402 modifiers |= CtrlKey;
404 int mapKey = modifiers << 16 | event->keyCode();
405 return mapKey ? keyDownCommandsMap.get(mapKey) : 0;
408 void EditorClientQt::handleKeyboardEvent(KeyboardEvent* event)
410 Frame* frame = m_page->d->page->focusController()->focusedOrMainFrame();
414 const PlatformKeyboardEvent* kevent = event->keyEvent();
415 if (!kevent || kevent->type() == PlatformKeyboardEvent::KeyUp)
418 Node* start = frame->selection()->start().containerNode();
422 // FIXME: refactor all of this to use Actions or something like them
423 if (start->isContentEditable()) {
424 bool doSpatialNavigation = false;
425 if (isSpatialNavigationEnabled(frame)) {
426 if (!kevent->modifiers()) {
427 switch (kevent->windowsVirtualKeyCode()) {
432 doSpatialNavigation = true;
437 #ifndef QT_NO_SHORTCUT
438 QWebPage::WebAction action = QWebPagePrivate::editorActionForKeyEvent(kevent->qtEvent());
439 if (action != QWebPage::NoWebAction && !doSpatialNavigation) {
440 const char* cmd = QWebPagePrivate::editorCommandForWebActions(action);
441 // WebKit doesn't have enough information about mode to decide how commands that just insert text if executed via Editor should be treated,
442 // so we leave it upon WebCore to either handle them immediately (e.g. Tab that changes focus) or let a keypress event be generated
443 // (e.g. Tab that inserts a Tab character, or Enter).
444 if (cmd && frame->editor()->command(cmd).isTextInsertion()
445 && kevent->type() == PlatformKeyboardEvent::RawKeyDown)
448 m_page->triggerAction(action);
449 event->setDefaultHandled();
452 #endif // QT_NO_SHORTCUT
454 String commandName = editorCommandForKeyDownEvent(event);
455 if (!commandName.isEmpty()) {
456 if (frame->editor()->command(commandName).execute()) // Event handled.
457 event->setDefaultHandled();
461 if (kevent->windowsVirtualKeyCode() == VK_TAB) {
462 // Do not handle TAB text insertion here.
467 bool shouldInsertText = false;
468 if (kevent->type() != PlatformKeyboardEvent::KeyDown && !kevent->text().isEmpty()) {
470 if (kevent->ctrlKey()) {
471 if (kevent->altKey())
472 shouldInsertText = true;
475 // We need to exclude checking for Alt because it is just a different Shift
476 if (!kevent->altKey())
478 shouldInsertText = true;
483 if (shouldInsertText) {
484 frame->editor()->insertText(kevent->text(), event);
485 event->setDefaultHandled();
490 // Event not handled.
494 // Non editable content.
495 if (m_page->handle()->page->settings()->caretBrowsingEnabled()) {
496 switch (kevent->windowsVirtualKeyCode()) {
504 #ifndef QT_NO_SHORTCUT
505 QWebPage::WebAction action = QWebPagePrivate::editorActionForKeyEvent(kevent->qtEvent());
506 ASSERT(action != QWebPage::NoWebAction);
507 m_page->triggerAction(action);
508 event->setDefaultHandled();
512 case VK_PRIOR: // PageUp
513 case VK_NEXT: // PageDown
515 String commandName = editorCommandForKeyDownEvent(event);
516 ASSERT(!commandName.isEmpty());
517 frame->editor()->command(commandName).execute();
518 event->setDefaultHandled();
524 #ifndef QT_NO_SHORTCUT
525 if (kevent->qtEvent() == QKeySequence::Copy) {
526 m_page->triggerAction(QWebPage::Copy);
527 event->setDefaultHandled();
530 #endif // QT_NO_SHORTCUT
533 void EditorClientQt::handleInputMethodKeydown(KeyboardEvent*)
537 EditorClientQt::EditorClientQt(QWebPage* page)
538 : m_page(page), m_editing(false), m_inUndoRedo(false)
542 void EditorClientQt::textFieldDidBeginEditing(Element*)
547 void EditorClientQt::textFieldDidEndEditing(Element*)
552 void EditorClientQt::textDidChangeInTextField(Element*)
556 bool EditorClientQt::doTextFieldCommandFromEvent(Element*, KeyboardEvent*)
561 void EditorClientQt::textWillBeDeletedInTextField(Element*)
565 void EditorClientQt::textDidChangeInTextArea(Element*)
569 void EditorClientQt::updateSpellingUIWithGrammarString(const String&, const GrammarDetail&)
574 void EditorClientQt::updateSpellingUIWithMisspelledWord(const String&)
579 void EditorClientQt::showSpellingUI(bool)
584 bool EditorClientQt::spellingUIIsShowing()
590 bool EditorClientQt::isEditing() const
595 void EditorClientQt::willSetInputMethodState()
599 void EditorClientQt::setInputMethodState(bool active)
601 QWebPageClient* webPageClient = m_page->d->client.get();
603 Qt::InputMethodHints hints;
605 HTMLInputElement* inputElement = 0;
606 Frame* frame = m_page->d->page->focusController()->focusedOrMainFrame();
607 if (frame && frame->document() && frame->document()->focusedNode())
608 if (frame->document()->focusedNode()->hasTagName(HTMLNames::inputTag))
609 inputElement = static_cast<HTMLInputElement*>(frame->document()->focusedNode());
612 // Set input method hints for "number", "tel", "email", "url" and "password" input elements.
613 if (inputElement->isTelephoneField())
614 hints |= Qt::ImhDialableCharactersOnly;
615 if (inputElement->isNumberField())
616 hints |= Qt::ImhDigitsOnly;
617 if (inputElement->isEmailField())
618 hints |= Qt::ImhEmailCharactersOnly;
619 if (inputElement->isURLField())
620 hints |= Qt::ImhUrlCharactersOnly;
621 // Setting the Qt::WA_InputMethodEnabled attribute true and Qt::ImhHiddenText flag
622 // for password fields. The Qt platform is responsible for determining which widget
623 // will receive input method events for password fields.
624 if (inputElement->isPasswordField()) {
626 hints |= Qt::ImhHiddenText;
630 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) || defined(Q_OS_SYMBIAN)
631 // disables auto-uppercase and predictive text for mobile devices
632 hints |= Qt::ImhNoAutoUppercase;
633 hints |= Qt::ImhNoPredictiveText;
634 #endif // Q_WS_MAEMO_5 || Q_WS_MAEMO_6 || Q_OS_SYMBIAN
635 webPageClient->setInputMethodHints(hints);
636 webPageClient->setInputMethodEnabled(active);
638 emit m_page->microFocusChanged();