1/*
2 * Copyright (C) 2006, 2007, 2008 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef SpellChecker_h
27#define SpellChecker_h
28
29#include "core/dom/DocumentMarker.h"
30#include "core/editing/FrameSelection.h"
31#include "core/editing/VisibleSelection.h"
32#include "platform/heap/Handle.h"
33#include "platform/text/TextChecking.h"
34
35namespace blink {
36
37class LocalFrame;
38class SpellCheckerClient;
39class SpellCheckRequest;
40class SpellCheckRequester;
41class TextCheckerClient;
42class TextCheckingParagraph;
43struct TextCheckingResult;
44
45class SpellChecker FINAL : public NoBaseWillBeGarbageCollectedFinalized<SpellChecker> {
46    WTF_MAKE_NONCOPYABLE(SpellChecker);
47public:
48    static PassOwnPtrWillBeRawPtr<SpellChecker> create(LocalFrame&);
49
50    ~SpellChecker();
51    void trace(Visitor*);
52
53    SpellCheckerClient& spellCheckerClient() const;
54    TextCheckerClient& textChecker() const;
55
56    bool isContinuousSpellCheckingEnabled() const;
57    void toggleContinuousSpellChecking();
58    bool isGrammarCheckingEnabled();
59    void ignoreSpelling();
60    bool isSpellCheckingEnabledInFocusedNode() const;
61    bool isSpellCheckingEnabledFor(Node*) const;
62    void markMisspellingsAfterLineBreak(const VisibleSelection& wordSelection);
63    void markMisspellingsAfterTypingToWord(const VisiblePosition &wordStart, const VisibleSelection& selectionAfterTyping);
64    void markMisspellings(const VisibleSelection&, RefPtrWillBeRawPtr<Range>& firstMisspellingRange);
65    void markBadGrammar(const VisibleSelection&);
66    void markMisspellingsAndBadGrammar(const VisibleSelection& spellingSelection, bool markGrammar, const VisibleSelection& grammarSelection);
67    void markAndReplaceFor(PassRefPtrWillBeRawPtr<SpellCheckRequest>, const Vector<TextCheckingResult>&);
68    void markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask, Range* spellingRange, Range* grammarRange);
69    void advanceToNextMisspelling(bool startBeforeSelection = false);
70    void showSpellingGuessPanel();
71    void didBeginEditing(Element*);
72    void clearMisspellingsAndBadGrammar(const VisibleSelection&);
73    void markMisspellingsAndBadGrammar(const VisibleSelection&);
74    void respondToChangedSelection(const VisibleSelection& oldSelection, FrameSelection::SetSelectionOptions);
75    void replaceMisspelledRange(const String&);
76    void removeSpellingMarkers();
77    void removeSpellingMarkersUnderWords(const Vector<String>& words);
78    void spellCheckAfterBlur();
79    void spellCheckOldSelection(const VisibleSelection& oldSelection, const VisibleSelection& newAdjacentWords);
80
81    void didEndEditingOnTextField(Element*);
82    bool selectionStartHasMarkerFor(DocumentMarker::MarkerType, int from, int length) const;
83    bool selectionStartHasSpellingMarkerFor(int from, int length) const;
84    void updateMarkersForWordsAffectedByEditing(bool onlyHandleWordsContainingSelection);
85    void cancelCheck();
86    void chunkAndMarkAllMisspellingsAndBadGrammar(Node*);
87    void requestTextChecking(const Element&);
88
89    // Exposed for testing only
90    SpellCheckRequester& spellCheckRequester() const { return *m_spellCheckRequester; }
91
92private:
93    explicit SpellChecker(LocalFrame&);
94
95    LocalFrame& frame() const
96    {
97        ASSERT(m_frame);
98        return *m_frame;
99    }
100
101    void markMisspellingsOrBadGrammar(const VisibleSelection&, bool checkSpelling, RefPtrWillBeRawPtr<Range>& firstMisspellingRange);
102    TextCheckingTypeMask resolveTextCheckingTypeMask(TextCheckingTypeMask);
103
104    bool unifiedTextCheckerEnabled() const;
105
106    void chunkAndMarkAllMisspellingsAndBadGrammar(TextCheckingTypeMask textCheckingOptions, const TextCheckingParagraph& fullParagraphToCheck, bool asynchronous);
107    void markAllMisspellingsAndBadGrammarInRanges(TextCheckingTypeMask textCheckingOptions, Range* checkingRange, Range* paragraphRange, bool asynchronous, int requestNumber, int* checkingLength = 0);
108
109    RawPtrWillBeMember<LocalFrame> m_frame;
110    const OwnPtrWillBeMember<SpellCheckRequester> m_spellCheckRequester;
111};
112
113} // namespace blink
114
115#endif // SpellChecker_h
116