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