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 AlternativeTextController_h
27#define AlternativeTextController_h
28
29#include "AlternativeTextClient.h"
30#include "DocumentMarker.h"
31#include "FrameSelection.h"
32#include "Range.h"
33#include "TextChecking.h"
34#include "Timer.h"
35#include "VisibleSelection.h"
36#include <wtf/Noncopyable.h>
37
38namespace WebCore {
39
40class CompositeEditCommand;
41class EditorClient;
42class EditCommand;
43class EditCommandComposition;
44class Event;
45class Frame;
46class TextCheckerClient;
47struct DictationAlternative;
48
49class AlternativeTextDetails : public RefCounted<AlternativeTextDetails> {
50public:
51    AlternativeTextDetails() { }
52    virtual ~AlternativeTextDetails() { }
53};
54
55struct AlternativeTextInfo {
56    RefPtr<Range> rangeWithAlternative;
57    bool isActive;
58    AlternativeTextType type;
59    String originalText;
60    RefPtr<AlternativeTextDetails> details;
61};
62
63class DictationMarkerDetails : public DocumentMarkerDetails {
64public:
65    static PassRefPtr<DictationMarkerDetails> create(const String& originalText, uint64_t dictationContext)
66    {
67        return adoptRef(new DictationMarkerDetails(originalText, dictationContext));
68    }
69    const String& originalText() const { return m_originalText; }
70    uint64_t dictationContext() const { return m_dictationContext; }
71private:
72    DictationMarkerDetails(const String& originalText, uint64_t dictationContext)
73    : m_dictationContext(dictationContext)
74    , m_originalText(originalText)
75    { }
76
77    uint64_t m_dictationContext;
78    String m_originalText;
79};
80
81struct TextCheckingResult;
82
83#if USE(AUTOCORRECTION_PANEL)
84// These backslashes are for making style checker happy.
85#define UNLESS_ENABLED(functionBody) \
86;\
87
88#else
89#define UNLESS_ENABLED(functionBody) functionBody
90#endif
91
92class AlternativeTextController {
93    WTF_MAKE_NONCOPYABLE(AlternativeTextController);
94    WTF_MAKE_FAST_ALLOCATED;
95public:
96    explicit AlternativeTextController(Frame*) UNLESS_ENABLED({ })
97    ~AlternativeTextController() UNLESS_ENABLED({ })
98
99    void startAlternativeTextUITimer(AlternativeTextType) UNLESS_ENABLED({ })
100    void stopAlternativeTextUITimer() UNLESS_ENABLED({ })
101
102    void dismiss(ReasonForDismissingAlternativeText) UNLESS_ENABLED({ })
103    void show(PassRefPtr<Range> rangeToReplace, const String& replacement) UNLESS_ENABLED({ UNUSED_PARAM(rangeToReplace); UNUSED_PARAM(replacement); })
104
105    // Return true if correction was applied, false otherwise.
106    bool applyAutocorrectionBeforeTypingIfAppropriate() UNLESS_ENABLED({ return false; })
107
108    void respondToUnappliedSpellCorrection(const VisibleSelection&, const String& corrected, const String& correction) UNLESS_ENABLED({ UNUSED_PARAM(corrected); UNUSED_PARAM(correction); })
109    void respondToAppliedEditing(CompositeEditCommand*) UNLESS_ENABLED({ })
110    void respondToUnappliedEditing(EditCommandComposition*) UNLESS_ENABLED({ })
111    void respondToChangedSelection(const VisibleSelection& oldSelection, FrameSelection::SetSelectionOptions) UNLESS_ENABLED({ UNUSED_PARAM(oldSelection); })
112
113    void stopPendingCorrection(const VisibleSelection& oldSelection) UNLESS_ENABLED({ UNUSED_PARAM(oldSelection); })
114    void applyPendingCorrection(const VisibleSelection& selectionAfterTyping) UNLESS_ENABLED({ UNUSED_PARAM(selectionAfterTyping); })
115
116    void handleAlternativeTextUIResult(const String& result) UNLESS_ENABLED({ UNUSED_PARAM(result); })
117    void handleCancelOperation() UNLESS_ENABLED({ })
118
119    bool hasPendingCorrection() const UNLESS_ENABLED({ return false; })
120    bool isSpellingMarkerAllowed(PassRefPtr<Range> misspellingRange) const UNLESS_ENABLED({ UNUSED_PARAM(misspellingRange); return true; })
121    bool isAutomaticSpellingCorrectionEnabled() UNLESS_ENABLED({ return false; })
122    bool shouldRemoveMarkersUponEditing();
123
124    void recordAutocorrectionResponseReversed(const String& replacedString, PassRefPtr<Range> replacementRange) UNLESS_ENABLED({ UNUSED_PARAM(replacedString); UNUSED_PARAM(replacementRange); })
125    void markReversed(PassRefPtr<Range> changedRange) UNLESS_ENABLED({ UNUSED_PARAM(changedRange); })
126    void markCorrection(PassRefPtr<Range> replacedRange, const String& replacedString) UNLESS_ENABLED({ UNUSED_PARAM(replacedRange); UNUSED_PARAM(replacedString); })
127
128    // This function returns false if the replacement should not be carried out.
129    bool processMarkersOnTextToBeReplacedByResult(const TextCheckingResult*, Range* rangeToBeReplaced, const String& stringToBeReplaced) UNLESS_ENABLED({ UNUSED_PARAM(rangeToBeReplaced); UNUSED_PARAM(stringToBeReplaced); return true; });
130    void deletedAutocorrectionAtPosition(const Position&, const String& originalString) UNLESS_ENABLED({ UNUSED_PARAM(originalString); })
131
132    bool insertDictatedText(const String&, const Vector<DictationAlternative>&, Event*);
133    void removeDictationAlternativesForMarker(const DocumentMarker*);
134    Vector<String> dictationAlternativesForMarker(const DocumentMarker*);
135    void applyDictationAlternative(const String& alternativeString);
136
137private:
138#if USE(AUTOCORRECTION_PANEL)
139    String dismissSoon(ReasonForDismissingAlternativeText);
140    void applyAlternativeTextToRange(const Range*, const String& alternative, AlternativeTextType, const Vector<DocumentMarker::MarkerType>&);
141    void timerFired(Timer<AlternativeTextController>*);
142    void recordAutocorrectionResponseReversed(const String& replacedString, const String& replacementString);
143    void recordSpellcheckerResponseForModifiedCorrection(Range* rangeOfCorrection, const String& corrected, const String& correction);
144    String markerDescriptionForAppliedAlternativeText(AlternativeTextType, DocumentMarker::MarkerType);
145
146    bool shouldStartTimerFor(const DocumentMarker&, int endOffset) const;
147    bool respondToMarkerAtEndOfWord(const DocumentMarker&, const Position& endOfWordPosition, FrameSelection::SetSelectionOptions);
148
149    AlternativeTextClient* alternativeTextClient();
150    EditorClient* editorClient();
151
152    TextCheckerClient* textChecker();
153    FloatRect rootViewRectForRange(const Range*) const;
154    void markPrecedingWhitespaceForDeletedAutocorrectionAfterCommand(EditCommand*);
155
156    Timer<AlternativeTextController> m_timer;
157    AlternativeTextInfo m_alternativeTextInfo;
158    bool m_isDismissedByEditing;
159
160    String m_originalStringForLastDeletedAutocorrection;
161    Position m_positionForLastDeletedAutocorrection;
162#endif
163
164    Frame* m_frame;
165};
166
167#undef UNLESS_ENABLED
168
169inline bool AlternativeTextController::shouldRemoveMarkersUponEditing()
170{
171#if USE(MARKER_REMOVAL_UPON_EDITING)
172    return true;
173#else
174    return false;
175#endif
176}
177
178} // namespace WebCore
179
180#endif // AlternativeTextController_h
181