WinCairo build fails to link.
[WebKit-https.git] / Source / WebKit / chromium / src / EditorClientImpl.cpp
index 46686a5..1d13c8e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2006, 2007 Apple, Inc.  All rights reserved.
- * Copyright (C) 2010 Google, Inc.  All rights reserved.
+ * Copyright (C) 2012 Google, Inc.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -28,7 +28,6 @@
 #include "EditorClientImpl.h"
 
 #include "Document.h"
-#include "EditCommand.h"
 #include "Editor.h"
 #include "EventHandler.h"
 #include "EventNames.h"
 #include "HTMLNames.h"
 #include "KeyboardCodes.h"
 #include "KeyboardEvent.h"
+#include "NotImplemented.h"
+#include "Page.h"
 #include "PlatformKeyboardEvent.h"
-#include "PlatformString.h"
 #include "RenderObject.h"
+#include "Settings.h"
 #include "SpellChecker.h"
+#include "UndoStep.h"
 
 #include "DOMUtilitiesPrivate.h"
 #include "WebAutofillClient.h"
 #include "WebElement.h"
 #include "WebFrameClient.h"
 #include "WebFrameImpl.h"
-#include "WebKit.h"
 #include "WebInputElement.h"
 #include "WebInputEventConversion.h"
+#include "WebKit.h"
 #include "WebNode.h"
 #include "WebPermissionClient.h"
 #include "WebRange.h"
 #include "WebSpellCheckClient.h"
 #include "WebTextAffinity.h"
 #include "WebTextCheckingCompletionImpl.h"
+#include "WebTextCheckingResult.h"
 #include "WebViewClient.h"
 #include "WebViewImpl.h"
+#include <wtf/text/WTFString.h>
 
 using namespace WebCore;
 
@@ -86,31 +90,22 @@ void EditorClientImpl::pageDestroyed()
     // Our lifetime is bound to the WebViewImpl.
 }
 
-bool EditorClientImpl::shouldShowDeleteInterface(HTMLElement* elem)
+void EditorClientImpl::frameWillDetachPage(WebCore::Frame* frame)
 {
-    // Normally, we don't care to show WebCore's deletion UI, so we only enable
-    // it if in testing mode and the test specifically requests it by using this
-    // magic class name.
-    return layoutTestMode()
-           && elem->getAttribute(HTMLNames::classAttr) == "needsDeletionUI";
 }
 
 bool EditorClientImpl::smartInsertDeleteEnabled()
 {
-    if (m_webView->client())
-        return m_webView->client()->isSmartInsertDeleteEnabled();
-    return true;
+    if (m_webView->page())
+        return m_webView->page()->settings()->smartInsertDeleteEnabled();
+    return false;
 }
 
 bool EditorClientImpl::isSelectTrailingWhitespaceEnabled()
 {
-    if (m_webView->client())
-        return m_webView->client()->isSelectTrailingWhitespaceEnabled();
-#if OS(WINDOWS)
-    return true;
-#else
+    if (m_webView->page())
+        return m_webView->page()->settings()->selectTrailingWhitespaceEnabled();
     return false;
-#endif
 }
 
 bool EditorClientImpl::shouldSpellcheckByDefault()
@@ -163,7 +158,8 @@ void EditorClientImpl::toggleContinuousSpellChecking()
 
 bool EditorClientImpl::isGrammarCheckingEnabled()
 {
-    return false;
+    const Frame* frame = m_webView->focusedWebCoreFrame();
+    return frame && frame->settings() && (frame->settings()->asynchronousSpellCheckingEnabled() || frame->settings()->unifiedTextCheckerEnabled());
 }
 
 void EditorClientImpl::toggleGrammarChecking()
@@ -237,13 +233,11 @@ bool EditorClientImpl::shouldChangeSelectedRange(Range* fromRange,
     return true;
 }
 
-bool EditorClientImpl::shouldApplyStyle(CSSStyleDeclaration* style,
-                                        Range* range)
+bool EditorClientImpl::shouldApplyStyle(StylePropertySet* style, Range* range)
 {
     if (m_webView->client()) {
         // FIXME: Pass a reference to the CSSStyleDeclaration somehow.
-        return m_webView->client()->shouldApplyStyle(WebString(),
-                                                     WebRange(range));
+        return m_webView->client()->shouldApplyStyle(WebString(), WebRange(range));
     }
     return true;
 }
@@ -263,8 +257,11 @@ void EditorClientImpl::didBeginEditing()
 void EditorClientImpl::respondToChangedSelection(Frame* frame)
 {
     if (m_webView->client()) {
-        if (frame)
+        if (frame) {
             m_webView->client()->didChangeSelection(!frame->selection()->isRange());
+            if (frame->editor()->cancelCompositionIfSelectionIsInvalid())
+                m_webView->client()->didCancelCompositionOnSelectionChange();
+        }
     }
 }
 
@@ -284,22 +281,30 @@ void EditorClientImpl::didWriteSelectionToPasteboard()
 {
 }
 
+void EditorClientImpl::willWriteSelectionToPasteboard(WebCore::Range*)
+{
+}
+
+void EditorClientImpl::getClientPasteboardDataForRange(WebCore::Range*, Vector<String>&, Vector<RefPtr<WebCore::SharedBuffer> >&)
+{
+}
+
 void EditorClientImpl::didSetSelectionTypesForPasteboard()
 {
 }
 
-void EditorClientImpl::registerCommandForUndo(PassRefPtr<EditCommand> command)
+void EditorClientImpl::registerUndoStep(PassRefPtr<UndoStep> step)
 {
     if (m_undoStack.size() == maximumUndoStackDepth)
         m_undoStack.removeFirst(); // drop oldest item off the far end
     if (!m_inRedo)
         m_redoStack.clear();
-    m_undoStack.append(command);
+    m_undoStack.append(step);
 }
 
-void EditorClientImpl::registerCommandForRedo(PassRefPtr<EditCommand> command)
+void EditorClientImpl::registerRedoStep(PassRefPtr<UndoStep> step)
 {
-    m_redoStack.append(command);
+    m_redoStack.append(step);
 }
 
 void EditorClientImpl::clearUndoRedoOperations()
@@ -335,10 +340,10 @@ bool EditorClientImpl::canRedo() const
 void EditorClientImpl::undo()
 {
     if (canUndo()) {
-        EditCommandStack::iterator back = --m_undoStack.end();
-        RefPtr<EditCommand> command(*back);
+        UndoManagerStack::iterator back = --m_undoStack.end();
+        RefPtr<UndoStep> step(*back);
         m_undoStack.remove(back);
-        command->unapply();
+        step->unapply();
         // unapply will call us back to push this command onto the redo stack.
     }
 }
@@ -346,13 +351,13 @@ void EditorClientImpl::undo()
 void EditorClientImpl::redo()
 {
     if (canRedo()) {
-        EditCommandStack::iterator back = --m_redoStack.end();
-        RefPtr<EditCommand> command(*back);
+        UndoManagerStack::iterator back = --m_redoStack.end();
+        RefPtr<UndoStep> step(*back);
         m_redoStack.remove(back);
 
         ASSERT(!m_inRedo);
         m_inRedo = true;
-        command->reapply();
+        step->reapply();
         // reapply will call us back to push this command onto the undo stack.
         m_inRedo = false;
     }
@@ -544,7 +549,7 @@ const char* EditorClientImpl::interpretKeyEvent(const KeyboardEvent* evt)
     if (keyEvent->metaKey())
         modifiers |= MetaKey;
 
-    if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
+    if (keyEvent->type() == PlatformEvent::RawKeyDown) {
         int mapKey = modifiers << 16 | evt->keyCode();
         return mapKey ? keyDownCommandsMap->get(mapKey) : 0;
     }
@@ -567,7 +572,7 @@ bool EditorClientImpl::handleEditingKeyboardEvent(KeyboardEvent* evt)
     String commandName = interpretKeyEvent(evt);
     Editor::Command command = frame->editor()->command(commandName);
 
-    if (keyEvent->type() == PlatformKeyboardEvent::RawKeyDown) {
+    if (keyEvent->type() == PlatformEvent::RawKeyDown) {
         // WebKit doesn't have enough information about mode to decide how
         // commands that just insert text if executed via Editor should be treated,
         // so we leave it upon WebCore to either handle them immediately
@@ -695,6 +700,12 @@ void EditorClientImpl::textDidChangeInTextArea(Element*)
 {
 }
 
+bool EditorClientImpl::shouldEraseMarkersAfterChangeSelection(TextCheckingType type) const
+{
+    const Frame* frame = m_webView->focusedWebCoreFrame();
+    return !frame || !frame->settings() || (!frame->settings()->asynchronousSpellCheckingEnabled() && !frame->settings()->unifiedTextCheckerEnabled());
+}
+
 void EditorClientImpl::ignoreWordInSpellDocument(const String&)
 {
     notImplemented();
@@ -715,7 +726,7 @@ void EditorClientImpl::checkSpellingOfString(const UChar* text, int length,
     int spellLength = 0;
 
     // Check to see if the provided text is spelled correctly.
-    if (isContinuousSpellCheckingEnabled() && m_webView->spellCheckClient())
+    if (m_webView->spellCheckClient())
         m_webView->spellCheckClient()->spellCheck(WebString(text, length), spellLocation, spellLength, 0);
     else {
         spellLocation = 0;
@@ -730,10 +741,12 @@ void EditorClientImpl::checkSpellingOfString(const UChar* text, int length,
         *misspellingLength = spellLength;
 }
 
-void EditorClientImpl::requestCheckingOfString(SpellChecker* sender, int identifier, TextCheckingTypeMask, const String& text)
+void EditorClientImpl::requestCheckingOfString(WTF::PassRefPtr<WebCore::TextCheckingRequest> request)
 {
-    if (m_webView->spellCheckClient())
-        m_webView->spellCheckClient()->requestCheckingOfText(text, new WebTextCheckingCompletionImpl(identifier, sender));
+    if (m_webView->spellCheckClient()) {
+        String text = request->data().text();
+        m_webView->spellCheckClient()->requestCheckingOfText(text, new WebTextCheckingCompletionImpl(request));
+    }
 }
 
 String EditorClientImpl::getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord)
@@ -753,16 +766,55 @@ String EditorClientImpl::getAutoCorrectSuggestionForMisspelledWord(const String&
     return String();
 }
 
-void EditorClientImpl::checkGrammarOfString(const UChar*, int length,
-                                            WTF::Vector<GrammarDetail>&,
-                                            int* badGrammarLocation,
-                                            int* badGrammarLength)
+void EditorClientImpl::checkGrammarOfString(const UChar* text, int length, WTF::Vector<GrammarDetail>& details, int* badGrammarLocation, int* badGrammarLength)
 {
-    notImplemented();
     if (badGrammarLocation)
-        *badGrammarLocation = 0;
+        *badGrammarLocation = -1;
     if (badGrammarLength)
         *badGrammarLength = 0;
+
+    if (!m_webView->spellCheckClient())
+        return;
+    WebVector<WebTextCheckingResult> webResults;
+    m_webView->spellCheckClient()->checkTextOfParagraph(WebString(text, length), WebTextCheckingTypeGrammar, &webResults);
+    if (!webResults.size())
+        return;
+
+    // Convert a list of WebTextCheckingResults to a list of GrammarDetails. If
+    // the converted vector of GrammarDetails has grammar errors, we set
+    // badGrammarLocation and badGrammarLength to tell WebKit that the input
+    // text has grammar errors.
+    for (size_t i = 0; i < webResults.size(); ++i) {
+        if (webResults[i].type == WebTextCheckingTypeGrammar) {
+            GrammarDetail detail;
+            detail.location = webResults[i].location;
+            detail.length = webResults[i].length;
+            detail.userDescription = webResults[i].replacement;
+            details.append(detail);
+        }
+    }
+    if (!details.size())
+        return;
+    if (badGrammarLocation)
+        *badGrammarLocation = 0;
+    if (badGrammarLength)
+        *badGrammarLength = length;
+}
+
+void EditorClientImpl::checkTextOfParagraph(const UChar* text, int length,
+                                            TextCheckingTypeMask mask,
+                                            WTF::Vector<TextCheckingResult>& results)
+{
+    if (!m_webView->spellCheckClient())
+        return;
+
+    WebTextCheckingTypeMask webMask = static_cast<WebTextCheckingTypeMask>(mask);
+    WebVector<WebTextCheckingResult> webResults;
+    m_webView->spellCheckClient()->checkTextOfParagraph(WebString(text, length), webMask, &webResults);
+
+    results.resize(webResults.size());
+    for (size_t i = 0; i < webResults.size(); ++i)
+        results[i] = webResults[i];
 }
 
 void EditorClientImpl::updateSpellingUIWithGrammarString(const String&,
@@ -797,6 +849,15 @@ void EditorClientImpl::getGuessesForWord(const String& word,
     notImplemented();
 }
 
+bool EditorClientImpl::supportsGlobalSelection()
+{
+#if OS(UNIX) && !OS(DARWIN)
+    return true;
+#else
+    return false;
+#endif
+}
+
 void EditorClientImpl::willSetInputMethodState()
 {
     if (m_webView->client())