Remove some dead code.
[WebKit-https.git] / 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 #if PLATFORM(MAC) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
30 // Some platforms provide UI for suggesting autocorrection.
31 #define SUPPORT_AUTOCORRECTION_PANEL 1
32 // Some platforms use spelling and autocorrection markers to provide visual cue.
33 // On such platform, if word with marker is edited, we need to remove the marker.
34 #define REMOVE_MARKERS_UPON_EDITING 1
35 #else
36 #define SUPPORT_AUTOCORRECTION_PANEL 0
37 #define REMOVE_MARKERS_UPON_EDITING 0
38 #endif // #if PLATFORM(MAC) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
39
40 #include "DocumentMarker.h"
41 #include "EditCommand.h"
42 #include "FloatRect.h"
43 #include "Range.h"
44 #include "Timer.h"
45 #include <wtf/Noncopyable.h>
46 #include <wtf/UnusedParam.h>
47
48 namespace WebCore {
49
50 class EditorClient;
51 class Range;
52 class TextCheckerClient;
53 class VisibleSelection;
54
55 struct CorrectionPanelInfo {
56     enum PanelType {
57         PanelTypeCorrection = 0,
58         PanelTypeReversion,
59         PanelTypeSpellingSuggestions
60     };
61
62     RefPtr<Range> rangeToBeReplaced;
63     String replacedString;
64     String replacementString;
65     PanelType panelType;
66     bool isActive;
67 };
68
69 enum ReasonForDismissingCorrectionPanel {
70     ReasonForDismissingCorrectionPanelCancelled = 0,
71     ReasonForDismissingCorrectionPanelIgnored,
72     ReasonForDismissingCorrectionPanelAccepted
73 };
74
75 #if SUPPORT_AUTOCORRECTION_PANEL
76 #define UNLESS_ENABLED(functionBody) ;
77 #else
78 #define UNLESS_ENABLED(functionBody) functionBody
79 #endif
80
81 class SpellingCorrectionController {
82     WTF_MAKE_NONCOPYABLE(SpellingCorrectionController); WTF_MAKE_FAST_ALLOCATED;
83 public:
84     SpellingCorrectionController(Frame*) UNLESS_ENABLED({})
85     ~SpellingCorrectionController() UNLESS_ENABLED({})
86
87     void startCorrectionPanelTimer(CorrectionPanelInfo::PanelType) UNLESS_ENABLED({})
88     void stopCorrectionPanelTimer() UNLESS_ENABLED({})
89
90     void dismiss(ReasonForDismissingCorrectionPanel) UNLESS_ENABLED({})
91     String dismissSoon(ReasonForDismissingCorrectionPanel) UNLESS_ENABLED({ return String(); })
92     void show(PassRefPtr<Range> rangeToReplace, const String& replacement) UNLESS_ENABLED({ UNUSED_PARAM(rangeToReplace); UNUSED_PARAM(replacement); })
93
94     void applyCorrectionPanelInfo(const Vector<DocumentMarker::MarkerType>&) UNLESS_ENABLED({})
95     // Return true if correction was applied, false otherwise.
96     bool applyAutocorrectionBeforeTypingIfAppropriate() UNLESS_ENABLED({ return false; })
97
98     void respondToUnappliedSpellCorrection(const VisibleSelection&, const String& corrected, const String& correction) UNLESS_ENABLED({ UNUSED_PARAM(corrected); UNUSED_PARAM(correction); })
99     void respondToAppliedEditing(EditCommand*) UNLESS_ENABLED({})
100     void respondToUnappliedEditing(EditCommand*) UNLESS_ENABLED({})
101     void respondToChangedSelection(const VisibleSelection& oldSelection) UNLESS_ENABLED({ UNUSED_PARAM(oldSelection); })
102
103     void stopPendingCorrection(const VisibleSelection& oldSelection) UNLESS_ENABLED({ UNUSED_PARAM(oldSelection); })
104     void applyPendingCorrection(const VisibleSelection& selectionAfterTyping) UNLESS_ENABLED({ UNUSED_PARAM(selectionAfterTyping); })
105
106     void handleCorrectionPanelResult(const String& correction) UNLESS_ENABLED({ UNUSED_PARAM(correction); })
107     void handleCancelOperation() UNLESS_ENABLED({})
108
109     bool hasPendingCorrection() const UNLESS_ENABLED({ return false; })
110     bool isSpellingMarkerAllowed(PassRefPtr<Range> misspellingRange) const UNLESS_ENABLED({ UNUSED_PARAM(misspellingRange); return true; })
111     bool isAutomaticSpellingCorrectionEnabled() UNLESS_ENABLED({ return false; })
112     bool shouldRemoveMarkersUponEditing() { return REMOVE_MARKERS_UPON_EDITING; }
113
114     void correctionPanelTimerFired(Timer<SpellingCorrectionController>*) UNLESS_ENABLED({})
115     void recordAutocorrectionResponseReversed(const String& replacedString, PassRefPtr<Range> replacementRange) UNLESS_ENABLED({ UNUSED_PARAM(replacedString); UNUSED_PARAM(replacementRange); })
116     void markReversed(PassRefPtr<Range> changedRange) UNLESS_ENABLED({ UNUSED_PARAM(changedRange); })
117     void markCorrection(PassRefPtr<Range> replacedRange, const String& replacedString) UNLESS_ENABLED({ UNUSED_PARAM(replacedRange); UNUSED_PARAM(replacedString); })
118     void recordSpellcheckerResponseForModifiedCorrection(Range* rangeOfCorrection, const String& corrected, const String& correction) UNLESS_ENABLED({ UNUSED_PARAM(rangeOfCorrection); UNUSED_PARAM(corrected); UNUSED_PARAM(correction); })
119
120 #if SUPPORT_AUTOCORRECTION_PANEL
121 private:
122     void recordAutocorrectionResponseReversed(const String& replacedString, const String& replacementString);
123
124     bool shouldStartTimerFor(const DocumentMarker& marker, int endOffset) const
125     {
126         return (((marker.type == DocumentMarker::Replacement && !marker.description.isNull()) 
127                  || marker.type == DocumentMarker::Spelling) && static_cast<int>(marker.endOffset) == endOffset);
128     }
129
130     EditorClient* client();
131     TextCheckerClient* textChecker();
132     FloatRect windowRectForRange(const Range*) const;
133
134     EditorClient* m_client;
135     Frame* m_frame;
136
137     Timer<SpellingCorrectionController> m_correctionPanelTimer;
138     CorrectionPanelInfo m_correctionPanelInfo;
139     bool m_correctionPanelIsDismissedByEditor;
140 #endif
141 };
142
143 #undef UNLESS_ENABLED
144
145 } // namespace WebCore
146
147 #endif // SpellingCorrectionController_h