initial import
[vuplus_webkit] / Source / WebCore / editing / SpellingCorrectionController.h
1 /*
2  * Copyright (C) 2010 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 SpellingCorrectionController_h
27 #define SpellingCorrectionController_h
28
29 #include "DocumentMarker.h"
30 #include "Range.h"
31 #include "TextChecking.h"
32 #include "Timer.h"
33 #include "VisibleSelection.h"
34 #include <wtf/Noncopyable.h>
35 #include <wtf/UnusedParam.h>
36
37 namespace WebCore {
38
39 class EditorClient;
40 class EditCommand;
41 class Frame;
42 class TextCheckerClient;
43
44 struct CorrectionPanelInfo {
45     enum PanelType {
46         PanelTypeCorrection = 0,
47         PanelTypeReversion,
48         PanelTypeSpellingSuggestions
49     };
50
51     RefPtr<Range> rangeToBeReplaced;
52     String replacedString;
53     String replacementString;
54     PanelType panelType;
55     bool isActive;
56 };
57
58 struct TextCheckingResult;
59
60 enum ReasonForDismissingCorrectionPanel {
61     ReasonForDismissingCorrectionPanelCancelled = 0,
62     ReasonForDismissingCorrectionPanelIgnored,
63     ReasonForDismissingCorrectionPanelAccepted
64 };
65
66 #if USE(AUTOCORRECTION_PANEL)
67 #define UNLESS_ENABLED(functionBody) ;
68 #else
69 #define UNLESS_ENABLED(functionBody) functionBody
70 #endif
71
72 class SpellingCorrectionController {
73     WTF_MAKE_NONCOPYABLE(SpellingCorrectionController); WTF_MAKE_FAST_ALLOCATED;
74 public:
75     SpellingCorrectionController(Frame*) UNLESS_ENABLED({})
76     ~SpellingCorrectionController() UNLESS_ENABLED({})
77
78     void startCorrectionPanelTimer(CorrectionPanelInfo::PanelType) UNLESS_ENABLED({})
79     void stopCorrectionPanelTimer() UNLESS_ENABLED({})
80
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); })
84
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; })
88
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); })
93
94     void stopPendingCorrection(const VisibleSelection& oldSelection) UNLESS_ENABLED({ UNUSED_PARAM(oldSelection); })
95     void applyPendingCorrection(const VisibleSelection& selectionAfterTyping) UNLESS_ENABLED({ UNUSED_PARAM(selectionAfterTyping); })
96
97     void handleCorrectionPanelResult(const String& correction) UNLESS_ENABLED({ UNUSED_PARAM(correction); })
98     void handleCancelOperation() UNLESS_ENABLED({})
99
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();
104
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); })
110
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); })
114
115 #if USE(AUTOCORRECTION_PANEL)
116 private:
117     void recordAutocorrectionResponseReversed(const String& replacedString, const String& replacementString);
118
119     bool shouldStartTimerFor(const DocumentMarker* marker, int endOffset) const
120     {
121         return (((marker->type() == DocumentMarker::Replacement && !marker->description().isNull()) 
122                  || marker->type() == DocumentMarker::Spelling) && static_cast<int>(marker->endOffset()) == endOffset);
123     }
124
125     EditorClient* client();
126     TextCheckerClient* textChecker();
127     FloatRect windowRectForRange(const Range*) const;
128     void markPrecedingWhitespaceForDeletedAutocorrectionAfterCommand(EditCommand*);
129
130     EditorClient* m_client;
131     Frame* m_frame;
132
133     Timer<SpellingCorrectionController> m_correctionPanelTimer;
134     CorrectionPanelInfo m_correctionPanelInfo;
135     bool m_correctionPanelIsDismissedByEditor;
136
137     String m_originalStringForLastDeletedAutocorrection;
138     Position m_positionForLastDeletedAutocorrection;
139 #endif
140 };
141
142 #undef UNLESS_ENABLED
143
144 inline bool SpellingCorrectionController::shouldRemoveMarkersUponEditing()
145 {
146 #if USE(MARKER_REMOVAL_UPON_EDITING)
147     return true;
148 #else
149     return false;
150 #endif
151 }
152
153 } // namespace WebCore
154
155 #endif // SpellingCorrectionController_h