Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2009 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY 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 #ifndef EditorClientImpl_h
     32 #define EditorClientImpl_h
     33 
     34 #include "core/page/EditorClient.h"
     35 #include "core/platform/Timer.h"
     36 #include "core/platform/text/TextCheckerClient.h"
     37 #include "wtf/Deque.h"
     38 #include "wtf/HashSet.h"
     39 
     40 namespace WebCore {
     41 class Frame;
     42 class HTMLInputElement;
     43 }
     44 
     45 namespace WebKit {
     46 class WebViewImpl;
     47 class WebTextCheckingCompletionImpl;
     48 
     49 class EditorClientImpl : public WebCore::EditorClient, public WebCore::TextCheckerClient {
     50 public:
     51     EditorClientImpl(WebViewImpl* webView);
     52 
     53     virtual ~EditorClientImpl();
     54 
     55     virtual bool smartInsertDeleteEnabled() OVERRIDE;
     56     virtual bool isSelectTrailingWhitespaceEnabled() OVERRIDE;
     57     virtual bool isContinuousSpellCheckingEnabled() OVERRIDE;
     58     virtual void toggleContinuousSpellChecking() OVERRIDE;
     59     virtual bool isGrammarCheckingEnabled() OVERRIDE;
     60     virtual bool shouldBeginEditing(WebCore::Range*) OVERRIDE;
     61     virtual bool shouldEndEditing(WebCore::Range*) OVERRIDE;
     62     virtual bool shouldInsertNode(WebCore::Node*, WebCore::Range*, WebCore::EditorInsertAction) OVERRIDE;
     63     virtual bool shouldInsertText(const WTF::String&, WebCore::Range*, WebCore::EditorInsertAction) OVERRIDE;
     64     virtual bool shouldDeleteRange(WebCore::Range*) OVERRIDE;
     65     virtual bool shouldChangeSelectedRange(WebCore::Range* fromRange, WebCore::Range* toRange,
     66         WebCore::EAffinity, bool stillSelecting) OVERRIDE;
     67     virtual bool shouldApplyStyle(WebCore::StylePropertySet*, WebCore::Range*) OVERRIDE;
     68     virtual void didBeginEditing() OVERRIDE;
     69     virtual void respondToChangedContents() OVERRIDE;
     70     virtual void respondToChangedSelection(WebCore::Frame*) OVERRIDE;
     71     virtual void didEndEditing() OVERRIDE;
     72     virtual void didCancelCompositionOnSelectionChange() OVERRIDE;
     73     virtual void registerUndoStep(PassRefPtr<WebCore::UndoStep>) OVERRIDE;
     74     virtual void registerRedoStep(PassRefPtr<WebCore::UndoStep>) OVERRIDE;
     75     virtual void clearUndoRedoOperations() OVERRIDE;
     76     virtual bool canCopyCut(WebCore::Frame*, bool defaultValue) const OVERRIDE;
     77     virtual bool canPaste(WebCore::Frame*, bool defaultValue) const OVERRIDE;
     78     virtual bool canUndo() const OVERRIDE;
     79     virtual bool canRedo() const OVERRIDE;
     80     virtual void undo() OVERRIDE;
     81     virtual void redo() OVERRIDE;
     82     virtual void handleKeyboardEvent(WebCore::KeyboardEvent*) OVERRIDE;
     83     virtual void textFieldDidEndEditing(WebCore::Element*) OVERRIDE;
     84     virtual void textDidChangeInTextField(WebCore::Element*) OVERRIDE;
     85     virtual bool doTextFieldCommandFromEvent(WebCore::Element*, WebCore::KeyboardEvent*) OVERRIDE;
     86     virtual bool shouldEraseMarkersAfterChangeSelection(WebCore::TextCheckingType) const OVERRIDE;
     87     virtual void checkSpellingOfString(const String&, int* misspellingLocation, int* misspellingLength) OVERRIDE;
     88     virtual void checkGrammarOfString(const String&, WTF::Vector<WebCore::GrammarDetail>&,
     89         int* badGrammarLocation, int* badGrammarLength) OVERRIDE;
     90     virtual WTF::String getAutoCorrectSuggestionForMisspelledWord(const WTF::String&) OVERRIDE;
     91     virtual void updateSpellingUIWithMisspelledWord(const WTF::String&) OVERRIDE;
     92     virtual void showSpellingUI(bool show) OVERRIDE;
     93     virtual bool spellingUIIsShowing() OVERRIDE;
     94     virtual void willSetInputMethodState() OVERRIDE;
     95     virtual void requestCheckingOfString(WTF::PassRefPtr<WebCore::TextCheckingRequest>) OVERRIDE;
     96     virtual bool supportsGlobalSelection() OVERRIDE;
     97 
     98     virtual WebCore::TextCheckerClient* textChecker() { return this; }
     99 
    100     const char* interpretKeyEvent(const WebCore::KeyboardEvent*);
    101 
    102 private:
    103     bool handleEditingKeyboardEvent(WebCore::KeyboardEvent*);
    104     void modifySelection(WebCore::Frame*, WebCore::KeyboardEvent*);
    105 
    106     // Returns whether or not the focused control needs spell-checking.
    107     // Currently, this function just retrieves the focused node and determines
    108     // whether or not it is a <textarea> element or an element whose
    109     // contenteditable attribute is true.
    110     // FIXME: Bug 740540: This code just implements the default behavior
    111     // proposed in this issue. We should also retrieve "spellcheck" attributes
    112     // for text fields and create a flag to over-write the default behavior.
    113     bool shouldSpellcheckByDefault();
    114 
    115     WebViewImpl* m_webView;
    116     bool m_inRedo;
    117 
    118     typedef Deque<RefPtr<WebCore::UndoStep> > UndoManagerStack;
    119     UndoManagerStack m_undoStack;
    120     UndoManagerStack m_redoStack;
    121 
    122     // This flag is set to false if spell check for this editor is manually
    123     // turned off. The default setting is SpellCheckAutomatic.
    124     enum {
    125         SpellCheckAutomatic,
    126         SpellCheckForcedOn,
    127         SpellCheckForcedOff
    128     };
    129     int m_spellCheckThisFieldStatus;
    130 };
    131 
    132 } // namespace WebKit
    133 
    134 #endif
    135