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_TIGER) && !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_TIGER) && !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
48namespace WebCore {
49
50class EditorClient;
51class Range;
52class TextCheckerClient;
53class VisibleSelection;
54
55struct 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
69enum 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
81class SpellingCorrectionController {
82    WTF_MAKE_NONCOPYABLE(SpellingCorrectionController); WTF_MAKE_FAST_ALLOCATED;
83public:
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(PassRefPtr<EditCommand>) UNLESS_ENABLED({})
100    void respondToChangedSelection(const VisibleSelection& oldSelection) UNLESS_ENABLED({ UNUSED_PARAM(oldSelection); })
101
102    void stopPendingCorrection(const VisibleSelection& oldSelection) UNLESS_ENABLED({ UNUSED_PARAM(oldSelection); })
103    void applyPendingCorrection(const VisibleSelection& selectionAfterTyping) UNLESS_ENABLED({ UNUSED_PARAM(selectionAfterTyping); })
104
105    void handleCorrectionPanelResult(const String& correction) UNLESS_ENABLED({ UNUSED_PARAM(correction); })
106    void handleCancelOperation() UNLESS_ENABLED({})
107
108    bool hasPendingCorrection() const UNLESS_ENABLED({ return false; })
109    bool isSpellingMarkerAllowed(PassRefPtr<Range> misspellingRange) const UNLESS_ENABLED({ UNUSED_PARAM(misspellingRange); return true; })
110    bool isAutomaticSpellingCorrectionEnabled() UNLESS_ENABLED({ return false; })
111    bool shouldRemoveMarkersUponEditing() { return REMOVE_MARKERS_UPON_EDITING; }
112
113    void correctionPanelTimerFired(Timer<SpellingCorrectionController>*) UNLESS_ENABLED({})
114    void recordAutocorrectionResponseReversed(const String& replacedString, PassRefPtr<Range> replacementRange) UNLESS_ENABLED({ UNUSED_PARAM(replacedString); UNUSED_PARAM(replacementRange); })
115    void markReversed(PassRefPtr<Range> changedRange) UNLESS_ENABLED({ UNUSED_PARAM(changedRange); })
116    void markCorrection(PassRefPtr<Range> replacedRange, const String& replacedString) UNLESS_ENABLED({ UNUSED_PARAM(replacedRange); UNUSED_PARAM(replacedString); })
117    void recordSpellcheckerResponseForModifiedCorrection(Range* rangeOfCorrection, const String& corrected, const String& correction) UNLESS_ENABLED({ UNUSED_PARAM(rangeOfCorrection); UNUSED_PARAM(corrected); UNUSED_PARAM(correction); })
118
119#if SUPPORT_AUTOCORRECTION_PANEL
120private:
121    void recordAutocorrectionResponseReversed(const String& replacedString, const String& replacementString);
122
123    bool shouldStartTimeFor(const DocumentMarker& marker, int endOffset) const
124    {
125        return (((marker.type == DocumentMarker::Replacement && !marker.description.isNull())
126                 || marker.type == DocumentMarker::Spelling) && static_cast<int>(marker.endOffset) == endOffset);
127    }
128
129    EditorClient* client();
130    TextCheckerClient* textChecker();
131    FloatRect windowRectForRange(const Range*) const;
132
133    EditorClient* m_client;
134    Frame* m_frame;
135
136    Timer<SpellingCorrectionController> m_correctionPanelTimer;
137    CorrectionPanelInfo m_correctionPanelInfo;
138    bool m_correctionPanelIsDismissedByEditor;
139#endif
140};
141
142#undef UNLESS_ENABLED
143
144} // namespace WebCore
145
146#endif // SpellingCorrectionController_h
147