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 "WebViewPrivate.h"
29 #if !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
30 using namespace WebCore;
32 static inline NSCorrectionIndicatorType correctionIndicatorType(CorrectionPanelInfo::PanelType panelType)
35 case CorrectionPanelInfo::PanelTypeCorrection:
36 return NSCorrectionIndicatorTypeDefault;
37 case CorrectionPanelInfo::PanelTypeReversion:
38 return NSCorrectionIndicatorTypeReversion;
39 case CorrectionPanelInfo::PanelTypeSpellingSuggestions:
40 return NSCorrectionIndicatorTypeGuesses;
43 return NSCorrectionIndicatorTypeDefault;
46 CorrectionPanel::CorrectionPanel()
47 : m_wasDismissedExternally(false)
48 , m_reasonForDismissing(ReasonForDismissingCorrectionPanelIgnored)
52 CorrectionPanel::~CorrectionPanel()
54 dismissInternal(ReasonForDismissingCorrectionPanelIgnored, false);
57 void CorrectionPanel::show(WebView* view, CorrectionPanelInfo::PanelType type, const FloatRect& boundingBoxOfReplacedString, const String& replacedString, const String& replacementString, const Vector<String>& alternativeReplacementStrings)
59 dismissInternal(ReasonForDismissingCorrectionPanelIgnored, false);
64 NSString* replacedStringAsNSString = replacedString;
65 NSString* replacementStringAsNSString = replacementString;
67 NSCorrectionIndicatorType indicatorType = correctionIndicatorType(type);
69 NSMutableArray* alternativeStrings = 0;
70 if (!alternativeReplacementStrings.isEmpty()) {
71 size_t size = alternativeReplacementStrings.size();
72 alternativeStrings = [NSMutableArray arrayWithCapacity:size];
73 for (size_t i = 0; i < size; ++i)
74 [alternativeStrings addObject:(NSString*)alternativeReplacementStrings[i]];
77 [[NSSpellChecker sharedSpellChecker] showCorrectionIndicatorOfType:indicatorType primaryString:replacementStringAsNSString alternativeStrings:alternativeStrings forStringInRect:[view convertRect:boundingBoxOfReplacedString fromView:nil] view:m_view.get() completionHandler:^(NSString* acceptedString) {
78 handleAcceptedReplacement(acceptedString, replacedStringAsNSString, replacementStringAsNSString, indicatorType);
82 String CorrectionPanel::dismiss(ReasonForDismissingCorrectionPanel reason)
84 return dismissInternal(reason, true);
87 String CorrectionPanel::dismissInternal(ReasonForDismissingCorrectionPanel reason, bool dismissingExternally)
92 m_wasDismissedExternally = dismissingExternally;
93 m_reasonForDismissing = reason;
94 m_resultForDismissal.clear();
95 [[NSSpellChecker sharedSpellChecker] dismissCorrectionIndicatorForView:m_view.get()];
96 return m_resultForDismissal.get();
99 void CorrectionPanel::recordAutocorrectionResponse(WebView* view, NSCorrectionResponse response, const String& replacedString, const String& replacementString)
101 [[NSSpellChecker sharedSpellChecker] recordResponse:response toCorrection:replacementString forWord:replacedString language:nil inSpellDocumentWithTag:[view spellCheckerDocumentTag]];
104 void CorrectionPanel::handleAcceptedReplacement(NSString* acceptedReplacement, NSString* replaced, NSString* proposedReplacement, NSCorrectionIndicatorType correctionIndicatorType)
109 NSSpellChecker* spellChecker = [NSSpellChecker sharedSpellChecker];
110 NSInteger documentTag = [m_view.get() spellCheckerDocumentTag];
112 switch (correctionIndicatorType) {
113 case NSCorrectionIndicatorTypeDefault:
114 if (acceptedReplacement)
115 [spellChecker recordResponse:NSCorrectionResponseAccepted toCorrection:acceptedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
117 if (!m_wasDismissedExternally || m_reasonForDismissing == ReasonForDismissingCorrectionPanelCancelled)
118 [spellChecker recordResponse:NSCorrectionResponseRejected toCorrection:proposedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
120 [spellChecker recordResponse:NSCorrectionResponseIgnored toCorrection:proposedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
123 case NSCorrectionIndicatorTypeReversion:
124 if (acceptedReplacement)
125 [spellChecker recordResponse:NSCorrectionResponseReverted toCorrection:replaced forWord:acceptedReplacement language:nil inSpellDocumentWithTag:documentTag];
127 case NSCorrectionIndicatorTypeGuesses:
128 if (acceptedReplacement)
129 [spellChecker recordResponse:NSCorrectionResponseAccepted toCorrection:acceptedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
133 [m_view.get() handleCorrectionPanelResult:acceptedReplacement];
135 if (acceptedReplacement)
136 m_resultForDismissal.adoptNS([acceptedReplacement copy]);
139 #endif // !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)