a6887790ddc381cbe707e3265ad500c657d2f12b
[WebKit-https.git] / WebKit / qt / WebCoreSupport / EditorClientQt.cpp
1 /*
2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2006 Zack Rusin <zack@kde.org>
4  * Copyright (C) 2006, 2008 Apple Computer, Inc.
5  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "EditorClientQt.h"
33
34 #include "qwebpage.h"
35 #include "qwebpage_p.h"
36
37 #include "Document.h"
38 #include "EditCommandQt.h"
39 #include "Page.h"
40 #include "Editor.h"
41 #include "FocusController.h"
42 #include "Frame.h"
43 #include "KeyboardCodes.h"
44 #include "KeyboardEvent.h"
45 #include "Page.h"
46 #include "PlatformKeyboardEvent.h"
47 #include "NotImplemented.h"
48 #include "Node.h"
49 #include "Range.h"
50
51 #include <stdio.h>
52
53 #include <QUndoStack>
54 #define methodDebug() qDebug("EditorClientQt: %s", __FUNCTION__);
55
56 static bool dumpEditingCallbacks = false;
57 static bool acceptsEditing = true;
58 void QWEBKIT_EXPORT qt_dump_editing_callbacks(bool b)
59 {
60     dumpEditingCallbacks = b;
61 }
62
63 void QWEBKIT_EXPORT qt_dump_set_accepts_editing(bool b)
64 {
65     acceptsEditing = b;
66 }
67
68
69 static QString dumpPath(WebCore::Node *node)
70 {
71     QString str = node->nodeName();
72
73     WebCore::Node *parent = node->parentNode();
74     while (parent) {
75         str.append(QLatin1String(" > "));
76         str.append(parent->nodeName());
77         parent = parent->parentNode();
78     }
79     return str;
80 }
81
82 static QString dumpRange(WebCore::Range *range)
83 {
84     if (!range)
85         return QLatin1String("(null)");
86     QString str;
87     WebCore::ExceptionCode code;
88     str.sprintf("range from %ld of %ls to %ld of %ls",
89                 range->startOffset(code), dumpPath(range->startContainer(code)).unicode(),
90                 range->endOffset(code), dumpPath(range->endContainer(code)).unicode());
91     return str;
92 }
93
94
95 namespace WebCore {
96
97
98 bool EditorClientQt::shouldDeleteRange(Range* range)
99 {
100     if (dumpEditingCallbacks)
101         printf("EDITING DELEGATE: shouldDeleteDOMRange:%s\n", dumpRange(range).toUtf8().constData());
102
103     return true;
104 }
105
106 bool EditorClientQt::shouldShowDeleteInterface(HTMLElement* element)
107 {
108     if (QWebPagePrivate::drtRun)
109         return element->className() == "needsDeletionUI";
110     return false;
111 }
112
113 bool EditorClientQt::isContinuousSpellCheckingEnabled()
114 {
115     return false;
116 }
117
118 bool EditorClientQt::isGrammarCheckingEnabled()
119 {
120     return false;
121 }
122
123 int EditorClientQt::spellCheckerDocumentTag()
124 {
125     return 0;
126 }
127
128 bool EditorClientQt::shouldBeginEditing(WebCore::Range* range)
129 {
130     if (dumpEditingCallbacks)
131         printf("EDITING DELEGATE: shouldBeginEditingInDOMRange:%s\n", dumpRange(range).toUtf8().constData());
132     return true;
133 }
134
135 bool EditorClientQt::shouldEndEditing(WebCore::Range* range)
136 {
137     if (dumpEditingCallbacks)
138         printf("EDITING DELEGATE: shouldEndEditingInDOMRange:%s\n", dumpRange(range).toUtf8().constData());
139     return true;
140 }
141
142 bool EditorClientQt::shouldInsertText(const String& string, Range* range, EditorInsertAction action)
143 {
144     if (dumpEditingCallbacks) {
145         static const char *insertactionstring[] = {
146             "WebViewInsertActionTyped",
147             "WebViewInsertActionPasted",
148             "WebViewInsertActionDropped",
149         };
150
151         printf("EDITING DELEGATE: shouldInsertText:%s replacingDOMRange:%s givenAction:%s\n",
152                QString(string).toUtf8().constData(), dumpRange(range).toUtf8().constData(), insertactionstring[action]);
153     }
154     return acceptsEditing;
155 }
156
157 bool EditorClientQt::shouldChangeSelectedRange(Range* currentRange, Range* proposedRange, EAffinity selectionAffinity, bool stillSelecting)
158 {
159     if (dumpEditingCallbacks) {
160         static const char *affinitystring[] = {
161             "NSSelectionAffinityUpstream",
162             "NSSelectionAffinityDownstream"
163         };
164         static const char *boolstring[] = {
165             "FALSE",
166             "TRUE"
167         };
168
169         printf("EDITING DELEGATE: shouldChangeSelectedDOMRange:%s toDOMRange:%s affinity:%s stillSelecting:%s\n",
170                dumpRange(currentRange).toUtf8().constData(),
171                dumpRange(proposedRange).toUtf8().constData(),
172                affinitystring[selectionAffinity], boolstring[stillSelecting]);
173     }
174     return acceptsEditing;
175 }
176
177 bool EditorClientQt::shouldApplyStyle(WebCore::CSSStyleDeclaration* style,
178                                       WebCore::Range* range)
179 {
180     if (dumpEditingCallbacks)
181         printf("EDITING DELEGATE: shouldApplyStyle:%s toElementsInDOMRange:%s\n",
182                QString(style->cssText()).toUtf8().constData(), dumpRange(range).toUtf8().constData());
183     return acceptsEditing;
184 }
185
186 bool EditorClientQt::shouldMoveRangeAfterDelete(WebCore::Range*, WebCore::Range*)
187 {
188     notImplemented();
189     return true;
190 }
191
192 void EditorClientQt::didBeginEditing()
193 {
194     if (dumpEditingCallbacks)
195         printf("EDITING DELEGATE: webViewDidBeginEditing:WebViewDidBeginEditingNotification\n");
196     m_editing = true;
197 }
198
199 void EditorClientQt::respondToChangedContents()
200 {
201     if (dumpEditingCallbacks)
202         printf("EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification\n");
203     m_page->d->updateEditorActions();
204
205     emit m_page->contentsChanged();
206 }
207
208 void EditorClientQt::respondToChangedSelection()
209 {
210     if (dumpEditingCallbacks)
211         printf("EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification\n");
212 //     const Selection &selection = m_page->d->page->selection();
213 //     char buffer[1024];
214 //     selection.formatForDebugger(buffer, sizeof(buffer));
215 //     printf("%s\n", buffer);
216
217     m_page->d->updateEditorActions();
218     emit m_page->selectionChanged();
219     emit m_page->microFocusChanged();
220 }
221
222 void EditorClientQt::didEndEditing()
223 {
224     if (dumpEditingCallbacks)
225         printf("EDITING DELEGATE: webViewDidEndEditing:WebViewDidEndEditingNotification\n");
226     m_editing = false;
227 }
228
229 void EditorClientQt::didWriteSelectionToPasteboard()
230 {
231 }
232
233 void EditorClientQt::didSetSelectionTypesForPasteboard()
234 {
235 }
236
237 bool EditorClientQt::selectWordBeforeMenuEvent()
238 {
239     notImplemented();
240     return false;
241 }
242
243 bool EditorClientQt::isEditable()
244
245     return m_page->isContentEditable();
246 }
247
248 void EditorClientQt::registerCommandForUndo(WTF::PassRefPtr<WebCore::EditCommand> cmd)
249 {
250 #ifndef QT_NO_UNDOSTACK
251     Frame* frame = m_page->d->page->focusController()->focusedOrMainFrame();
252     if (m_inUndoRedo || (frame && !frame->editor()->lastEditCommand() /* HACK!! Don't recreate undos */)) {
253         return;
254     }
255     m_page->undoStack()->push(new EditCommandQt(cmd));
256 #endif // QT_NO_UNDOSTACK
257 }
258
259 void EditorClientQt::registerCommandForRedo(WTF::PassRefPtr<WebCore::EditCommand>)
260 {
261 }
262
263 void EditorClientQt::clearUndoRedoOperations()
264 {
265 #ifndef QT_NO_UNDOSTACK
266     return m_page->undoStack()->clear();
267 #endif
268 }
269
270 bool EditorClientQt::canUndo() const
271 {
272 #ifdef QT_NO_UNDOSTACK
273     return false;
274 #else
275     return m_page->undoStack()->canUndo();
276 #endif
277 }
278
279 bool EditorClientQt::canRedo() const
280 {
281 #ifdef QT_NO_UNDOSTACK
282     return false;
283 #else
284     return m_page->undoStack()->canRedo();
285 #endif
286 }
287
288 void EditorClientQt::undo()
289 {
290 #ifndef QT_NO_UNDOSTACK
291     m_inUndoRedo = true;
292     m_page->undoStack()->undo();
293     m_inUndoRedo = false;
294 #endif
295 }
296
297 void EditorClientQt::redo()
298 {
299 #ifndef QT_NO_UNDOSTACK
300     m_inUndoRedo = true;
301     m_page->undoStack()->redo();
302     m_inUndoRedo = false;
303 #endif
304 }
305
306 bool EditorClientQt::shouldInsertNode(Node* node, Range* range, EditorInsertAction action)
307 {
308     if (dumpEditingCallbacks) {
309         static const char *insertactionstring[] = {
310             "WebViewInsertActionTyped",
311             "WebViewInsertActionPasted",
312             "WebViewInsertActionDropped",
313         };
314
315         printf("EDITING DELEGATE: shouldInsertNode:%s replacingDOMRange:%s givenAction:%s\n", dumpPath(node).toUtf8().constData(),
316                dumpRange(range).toUtf8().constData(), insertactionstring[action]);
317     }
318     return acceptsEditing;
319 }
320
321 void EditorClientQt::pageDestroyed()
322 {
323     delete this;
324 }
325
326 bool EditorClientQt::smartInsertDeleteEnabled()
327 {
328     notImplemented();
329     return false;
330 }
331
332 bool EditorClientQt::isSelectTrailingWhitespaceEnabled()
333 {
334     notImplemented();
335     return false;
336 }
337
338 void EditorClientQt::toggleContinuousSpellChecking()
339 {
340     notImplemented();
341 }
342
343 void EditorClientQt::toggleGrammarChecking()
344 {
345     notImplemented();
346 }
347
348 void EditorClientQt::handleKeyboardEvent(KeyboardEvent* event)
349 {
350     Frame* frame = m_page->d->page->focusController()->focusedOrMainFrame();
351     if (!frame || !frame->document()->focusedNode())
352         return;
353
354     const PlatformKeyboardEvent* kevent = event->keyEvent();
355     if (!kevent || kevent->type() == PlatformKeyboardEvent::KeyUp)
356         return;
357
358     Node* start = frame->selection()->start().node();
359     if (!start)
360         return;
361
362     // FIXME: refactor all of this to use Actions or something like them
363     if (start->isContentEditable()) {
364 #ifndef QT_NO_SHORTCUT
365         QWebPage::WebAction action = QWebPagePrivate::editorActionForKeyEvent(kevent->qtEvent());
366         if (action != QWebPage::NoWebAction) {
367             const char* cmd = QWebPagePrivate::editorCommandForWebActions(action);
368             // WebKit doesn't have enough information about mode to decide how commands that just insert text if executed via Editor should be treated,
369             // so we leave it upon WebCore to either handle them immediately (e.g. Tab that changes focus) or let a keypress event be generated
370             // (e.g. Tab that inserts a Tab character, or Enter).
371             if (cmd && frame->editor()->command(cmd).isTextInsertion()
372                 && kevent->type() == PlatformKeyboardEvent::RawKeyDown)
373                 return;
374
375             m_page->triggerAction(action);
376         } else
377 #endif // QT_NO_SHORTCUT
378         switch (kevent->windowsVirtualKeyCode()) {
379 #if QT_VERSION < 0x040500
380             case VK_RETURN:
381 #ifdef QT_WS_MAC
382                 if (kevent->shiftKey() || kevent->metaKey())
383 #else
384                 if (kevent->shiftKey())
385 #endif
386                     frame->editor()->command("InsertLineBreak").execute();
387                 else
388                     frame->editor()->command("InsertNewline").execute();
389                 break;
390 #endif
391             case VK_BACK:
392                 frame->editor()->deleteWithDirection(SelectionController::BACKWARD,
393                         CharacterGranularity, false, true);
394                 break;
395             case VK_DELETE:
396                 frame->editor()->deleteWithDirection(SelectionController::FORWARD,
397                         CharacterGranularity, false, true);
398                 break;
399             case VK_LEFT:
400                 if (kevent->shiftKey())
401                     frame->editor()->command("MoveLeftAndModifySelection").execute();
402                 else frame->editor()->command("MoveLeft").execute();
403                 break;
404             case VK_RIGHT:
405                 if (kevent->shiftKey())
406                     frame->editor()->command("MoveRightAndModifySelection").execute();
407                 else frame->editor()->command("MoveRight").execute();
408                 break;
409             case VK_UP:
410                 if (kevent->shiftKey())
411                     frame->editor()->command("MoveUpAndModifySelection").execute();
412                 else frame->editor()->command("MoveUp").execute();
413                 break;
414             case VK_DOWN:
415                 if (kevent->shiftKey())
416                     frame->editor()->command("MoveDownAndModifySelection").execute();
417                 else frame->editor()->command("MoveDown").execute();
418                 break;
419             case VK_PRIOR:  // PageUp
420                 frame->editor()->command("MovePageUp").execute();
421                 break;
422             case VK_NEXT:  // PageDown
423                 frame->editor()->command("MovePageDown").execute();
424                 break;
425             case VK_TAB:
426                 return;
427             default:
428                 if (kevent->type() != PlatformKeyboardEvent::KeyDown && !kevent->ctrlKey()
429 #ifndef Q_WS_MAC
430                     // We need to exclude checking for Alt because it is just a different Shift
431                     && !kevent->altKey()
432 #endif
433                     && !kevent->text().isEmpty()) {
434                     frame->editor()->insertText(kevent->text(), event);
435                 } else if (kevent->ctrlKey()) {
436                     switch (kevent->windowsVirtualKeyCode()) {
437                         case VK_A:
438                             frame->editor()->command("SelectAll").execute();
439                             break;
440                         case VK_B:
441                             frame->editor()->command("ToggleBold").execute();
442                             break;
443                         case VK_I:
444                             frame->editor()->command("ToggleItalic").execute();
445                             break;
446                         default:
447                             // catch combination AltGr+key or Ctrl+Alt+key
448                             if (kevent->type() != PlatformKeyboardEvent::KeyDown && kevent->altKey() && !kevent->text().isEmpty()) {
449                                 frame->editor()->insertText(kevent->text(), event);
450                                 break;
451                             }
452                             return;
453                     }
454                 } else return;
455         }
456     } else {
457 #ifndef QT_NO_SHORTCUT
458         if (kevent->qtEvent() == QKeySequence::Copy) {
459             m_page->triggerAction(QWebPage::Copy);
460         } else
461 #endif // QT_NO_SHORTCUT
462         switch (kevent->windowsVirtualKeyCode()) {
463             case VK_UP:
464                 frame->editor()->command("MoveUp").execute();
465                 break;
466             case VK_DOWN:
467                 frame->editor()->command("MoveDown").execute();
468                 break;
469             case VK_PRIOR:  // PageUp
470                 frame->editor()->command("MovePageUp").execute();
471                 break;
472             case VK_NEXT:  // PageDown
473                 frame->editor()->command("MovePageDown").execute();
474                 break;
475             case VK_HOME:
476                 if (kevent->ctrlKey())
477                     frame->editor()->command("MoveToBeginningOfDocument").execute();
478                 break;
479             case VK_END:
480                 if (kevent->ctrlKey())
481                     frame->editor()->command("MoveToEndOfDocument").execute();
482                 break;
483             default:
484                 if (kevent->ctrlKey()) {
485                     switch (kevent->windowsVirtualKeyCode()) {
486                         case VK_A:
487                             frame->editor()->command("SelectAll").execute();
488                             break;
489                         default:
490                             return;
491                     }
492                 } else return;
493         }
494     }
495     event->setDefaultHandled();
496 }
497
498 void EditorClientQt::handleInputMethodKeydown(KeyboardEvent*)
499 {
500 }
501
502 EditorClientQt::EditorClientQt(QWebPage* page)
503     : m_page(page), m_editing(false), m_inUndoRedo(false)
504 {
505 }
506
507 void EditorClientQt::textFieldDidBeginEditing(Element*)
508 {
509     m_editing = true;
510 }
511
512 void EditorClientQt::textFieldDidEndEditing(Element*)
513 {
514     m_editing = false;
515 }
516
517 void EditorClientQt::textDidChangeInTextField(Element*)
518 {
519 }
520
521 bool EditorClientQt::doTextFieldCommandFromEvent(Element*, KeyboardEvent*)
522 {
523     return false;
524 }
525
526 void EditorClientQt::textWillBeDeletedInTextField(Element*)
527 {
528 }
529
530 void EditorClientQt::textDidChangeInTextArea(Element*)
531 {
532 }
533
534 void EditorClientQt::ignoreWordInSpellDocument(const String&)
535 {
536     notImplemented();
537 }
538
539 void EditorClientQt::learnWord(const String&)
540 {
541     notImplemented();
542 }
543
544 void EditorClientQt::checkSpellingOfString(const UChar*, int, int*, int*)
545 {
546     notImplemented();
547 }
548
549 void EditorClientQt::checkGrammarOfString(const UChar*, int, Vector<GrammarDetail>&, int*, int*)
550 {
551     notImplemented();
552 }
553
554 void EditorClientQt::updateSpellingUIWithGrammarString(const String&, const GrammarDetail&)
555 {
556     notImplemented();
557 }
558
559 void EditorClientQt::updateSpellingUIWithMisspelledWord(const String&)
560 {
561     notImplemented();
562 }
563
564 void EditorClientQt::showSpellingUI(bool)
565 {
566     notImplemented();
567 }
568
569 bool EditorClientQt::spellingUIIsShowing()
570 {
571     notImplemented();
572     return false;
573 }
574
575 void EditorClientQt::getGuessesForWord(const String&, Vector<String>&)
576 {
577     notImplemented();
578 }
579
580 bool EditorClientQt::isEditing() const
581 {
582     return m_editing;
583 }
584
585 void EditorClientQt::setInputMethodState(bool active)
586 {
587     QWidget *view = m_page->view();
588     if (view) {
589         view->setAttribute(Qt::WA_InputMethodEnabled, active);
590         emit m_page->microFocusChanged();
591     }
592 }
593
594 }
595
596 // vim: ts=4 sw=4 et