2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef SpellingCorrectionController_h
27 #define SpellingCorrectionController_h
29 #include "DocumentMarker.h"
31 #include "TextChecking.h"
33 #include "VisibleSelection.h"
34 #include <wtf/Noncopyable.h>
35 #include <wtf/UnusedParam.h>
42 class TextCheckerClient;
44 struct CorrectionPanelInfo {
46 PanelTypeCorrection = 0,
48 PanelTypeSpellingSuggestions
51 RefPtr<Range> rangeToBeReplaced;
52 String replacedString;
53 String replacementString;
58 struct TextCheckingResult;
60 enum ReasonForDismissingCorrectionPanel {
61 ReasonForDismissingCorrectionPanelCancelled = 0,
62 ReasonForDismissingCorrectionPanelIgnored,
63 ReasonForDismissingCorrectionPanelAccepted
66 #if USE(AUTOCORRECTION_PANEL)
67 #define UNLESS_ENABLED(functionBody) ;
69 #define UNLESS_ENABLED(functionBody) functionBody
72 class SpellingCorrectionController {
73 WTF_MAKE_NONCOPYABLE(SpellingCorrectionController); WTF_MAKE_FAST_ALLOCATED;
75 SpellingCorrectionController(Frame*) UNLESS_ENABLED({})
76 ~SpellingCorrectionController() UNLESS_ENABLED({})
78 void startCorrectionPanelTimer(CorrectionPanelInfo::PanelType) UNLESS_ENABLED({})
79 void stopCorrectionPanelTimer() UNLESS_ENABLED({})
81 void dismiss(ReasonForDismissingCorrectionPanel) UNLESS_ENABLED({})
82 String dismissSoon(ReasonForDismissingCorrectionPanel) UNLESS_ENABLED({ return String(); })
83 void show(PassRefPtr<Range> rangeToReplace, const String& replacement) UNLESS_ENABLED({ UNUSED_PARAM(rangeToReplace); UNUSED_PARAM(replacement); })
85 void applyCorrectionPanelInfo(const Vector<DocumentMarker::MarkerType>&) UNLESS_ENABLED({})
86 // Return true if correction was applied, false otherwise.
87 bool applyAutocorrectionBeforeTypingIfAppropriate() UNLESS_ENABLED({ return false; })
89 void respondToUnappliedSpellCorrection(const VisibleSelection&, const String& corrected, const String& correction) UNLESS_ENABLED({ UNUSED_PARAM(corrected); UNUSED_PARAM(correction); })
90 void respondToAppliedEditing(EditCommand*) UNLESS_ENABLED({})
91 void respondToUnappliedEditing(EditCommand*) UNLESS_ENABLED({})
92 void respondToChangedSelection(const VisibleSelection& oldSelection) UNLESS_ENABLED({ UNUSED_PARAM(oldSelection); })
94 void stopPendingCorrection(const VisibleSelection& oldSelection) UNLESS_ENABLED({ UNUSED_PARAM(oldSelection); })
95 void applyPendingCorrection(const VisibleSelection& selectionAfterTyping) UNLESS_ENABLED({ UNUSED_PARAM(selectionAfterTyping); })
97 void handleCorrectionPanelResult(const String& correction) UNLESS_ENABLED({ UNUSED_PARAM(correction); })
98 void handleCancelOperation() UNLESS_ENABLED({})
100 bool hasPendingCorrection() const UNLESS_ENABLED({ return false; })
101 bool isSpellingMarkerAllowed(PassRefPtr<Range> misspellingRange) const UNLESS_ENABLED({ UNUSED_PARAM(misspellingRange); return true; })
102 bool isAutomaticSpellingCorrectionEnabled() UNLESS_ENABLED({ return false; })
103 bool shouldRemoveMarkersUponEditing();
105 void correctionPanelTimerFired(Timer<SpellingCorrectionController>*) UNLESS_ENABLED({})
106 void recordAutocorrectionResponseReversed(const String& replacedString, PassRefPtr<Range> replacementRange) UNLESS_ENABLED({ UNUSED_PARAM(replacedString); UNUSED_PARAM(replacementRange); })
107 void markReversed(PassRefPtr<Range> changedRange) UNLESS_ENABLED({ UNUSED_PARAM(changedRange); })
108 void markCorrection(PassRefPtr<Range> replacedRange, const String& replacedString) UNLESS_ENABLED({ UNUSED_PARAM(replacedRange); UNUSED_PARAM(replacedString); })
109 void recordSpellcheckerResponseForModifiedCorrection(Range* rangeOfCorrection, const String& corrected, const String& correction) UNLESS_ENABLED({ UNUSED_PARAM(rangeOfCorrection); UNUSED_PARAM(corrected); UNUSED_PARAM(correction); })
111 // This function returns false if the replacement should not be carried out.
112 bool processMarkersOnTextToBeReplacedByResult(const TextCheckingResult*, Range* rangeToBeReplaced, const String& stringToBeReplaced) UNLESS_ENABLED({ UNUSED_PARAM(rangeToBeReplaced); UNUSED_PARAM(stringToBeReplaced); return true; });
113 void deletedAutocorrectionAtPosition(const Position&, const String& originalString) UNLESS_ENABLED({ UNUSED_PARAM(originalString); })
115 #if USE(AUTOCORRECTION_PANEL)
117 void recordAutocorrectionResponseReversed(const String& replacedString, const String& replacementString);
119 bool shouldStartTimerFor(const DocumentMarker* marker, int endOffset) const
121 return (((marker->type() == DocumentMarker::Replacement && !marker->description().isNull())
122 || marker->type() == DocumentMarker::Spelling) && static_cast<int>(marker->endOffset()) == endOffset);
125 EditorClient* client();
126 TextCheckerClient* textChecker();
127 FloatRect windowRectForRange(const Range*) const;
128 void markPrecedingWhitespaceForDeletedAutocorrectionAfterCommand(EditCommand*);
130 EditorClient* m_client;
133 Timer<SpellingCorrectionController> m_correctionPanelTimer;
134 CorrectionPanelInfo m_correctionPanelInfo;
135 bool m_correctionPanelIsDismissedByEditor;
137 String m_originalStringForLastDeletedAutocorrection;
138 Position m_positionForLastDeletedAutocorrection;
142 #undef UNLESS_ENABLED
144 inline bool SpellingCorrectionController::shouldRemoveMarkersUponEditing()
146 #if USE(MARKER_REMOVAL_UPON_EDITING)
153 } // namespace WebCore
155 #endif // SpellingCorrectionController_h