2 * Copyright (C) 2011 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
25 #import "CorrectionPanel.h"
27 #import "WebViewInternal.h"
28 #import "WebViewPrivate.h"
30 #if !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
31 using namespace WebCore;
33 static inline NSCorrectionIndicatorType correctionIndicatorType(CorrectionPanelInfo::PanelType panelType)
36 case CorrectionPanelInfo::PanelTypeCorrection:
37 return NSCorrectionIndicatorTypeDefault;
38 case CorrectionPanelInfo::PanelTypeReversion:
39 return NSCorrectionIndicatorTypeReversion;
40 case CorrectionPanelInfo::PanelTypeSpellingSuggestions:
41 return NSCorrectionIndicatorTypeGuesses;
44 return NSCorrectionIndicatorTypeDefault;
47 CorrectionPanel::CorrectionPanel()
48 : m_wasDismissedExternally(false)
49 , m_reasonForDismissing(ReasonForDismissingCorrectionPanelIgnored)
53 CorrectionPanel::~CorrectionPanel()
55 dismissInternal(ReasonForDismissingCorrectionPanelIgnored, false);
58 void CorrectionPanel::show(WebView* view, CorrectionPanelInfo::PanelType type, const FloatRect& boundingBoxOfReplacedString, const String& replacedString, const String& replacementString, const Vector<String>& alternativeReplacementStrings)
60 dismissInternal(ReasonForDismissingCorrectionPanelIgnored, false);
65 NSString* replacedStringAsNSString = replacedString;
66 NSString* replacementStringAsNSString = replacementString;
68 NSCorrectionIndicatorType indicatorType = correctionIndicatorType(type);
70 NSMutableArray* alternativeStrings = 0;
71 if (!alternativeReplacementStrings.isEmpty()) {
72 size_t size = alternativeReplacementStrings.size();
73 alternativeStrings = [NSMutableArray arrayWithCapacity:size];
74 for (size_t i = 0; i < size; ++i)
75 [alternativeStrings addObject:(NSString*)alternativeReplacementStrings[i]];
78 [[NSSpellChecker sharedSpellChecker] showCorrectionIndicatorOfType:indicatorType primaryString:replacementStringAsNSString alternativeStrings:alternativeStrings forStringInRect:[view _convertRectFromRootView:boundingBoxOfReplacedString] view:m_view.get() completionHandler:^(NSString* acceptedString) {
79 handleAcceptedReplacement(acceptedString, replacedStringAsNSString, replacementStringAsNSString, indicatorType);
83 String CorrectionPanel::dismiss(ReasonForDismissingCorrectionPanel reason)
85 return dismissInternal(reason, true);
88 String CorrectionPanel::dismissInternal(ReasonForDismissingCorrectionPanel reason, bool dismissingExternally)
93 m_wasDismissedExternally = dismissingExternally;
94 m_reasonForDismissing = reason;
95 m_resultForDismissal.clear();
96 [[NSSpellChecker sharedSpellChecker] dismissCorrectionIndicatorForView:m_view.get()];
97 return m_resultForDismissal.get();
100 void CorrectionPanel::recordAutocorrectionResponse(WebView* view, NSCorrectionResponse response, const String& replacedString, const String& replacementString)
102 [[NSSpellChecker sharedSpellChecker] recordResponse:response toCorrection:replacementString forWord:replacedString language:nil inSpellDocumentWithTag:[view spellCheckerDocumentTag]];
105 void CorrectionPanel::handleAcceptedReplacement(NSString* acceptedReplacement, NSString* replaced, NSString* proposedReplacement, NSCorrectionIndicatorType correctionIndicatorType)
110 NSSpellChecker* spellChecker = [NSSpellChecker sharedSpellChecker];
111 NSInteger documentTag = [m_view.get() spellCheckerDocumentTag];
113 switch (correctionIndicatorType) {
114 case NSCorrectionIndicatorTypeDefault:
115 if (acceptedReplacement)
116 [spellChecker recordResponse:NSCorrectionResponseAccepted toCorrection:acceptedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
118 if (!m_wasDismissedExternally || m_reasonForDismissing == ReasonForDismissingCorrectionPanelCancelled)
119 [spellChecker recordResponse:NSCorrectionResponseRejected toCorrection:proposedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
121 [spellChecker recordResponse:NSCorrectionResponseIgnored toCorrection:proposedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
124 case NSCorrectionIndicatorTypeReversion:
125 if (acceptedReplacement)
126 [spellChecker recordResponse:NSCorrectionResponseReverted toCorrection:replaced forWord:acceptedReplacement language:nil inSpellDocumentWithTag:documentTag];
128 case NSCorrectionIndicatorTypeGuesses:
129 if (acceptedReplacement)
130 [spellChecker recordResponse:NSCorrectionResponseAccepted toCorrection:acceptedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
134 [m_view.get() handleCorrectionPanelResult:acceptedReplacement];
136 if (acceptedReplacement)
137 m_resultForDismissal.adoptNS([acceptedReplacement copy]);
140 #endif // !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)