1/* 2 * Copyright (C) 2011 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 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. 24 */ 25 26#import "config.h" 27#import "CorrectionPanel.h" 28 29#if USE(AUTOCORRECTION_PANEL) 30 31#import "WebPageProxy.h" 32#import "WKView.h" 33#import "WKViewInternal.h" 34 35using namespace WebCore; 36 37static inline NSCorrectionIndicatorType correctionIndicatorType(AlternativeTextType alternativeTextType) 38{ 39 switch (alternativeTextType) { 40 case AlternativeTextTypeCorrection: 41 return NSCorrectionIndicatorTypeDefault; 42 case AlternativeTextTypeReversion: 43 return NSCorrectionIndicatorTypeReversion; 44 case AlternativeTextTypeSpellingSuggestions: 45 return NSCorrectionIndicatorTypeGuesses; 46 case AlternativeTextTypeDictationAlternatives: 47 ASSERT_NOT_REACHED(); 48 break; 49 } 50 ASSERT_NOT_REACHED(); 51 return NSCorrectionIndicatorTypeDefault; 52} 53 54namespace WebKit { 55 56CorrectionPanel::CorrectionPanel() 57 : m_wasDismissedExternally(false) 58 , m_reasonForDismissing(ReasonForDismissingAlternativeTextIgnored) 59{ 60} 61 62CorrectionPanel::~CorrectionPanel() 63{ 64 dismissInternal(ReasonForDismissingAlternativeTextIgnored, false); 65} 66 67void CorrectionPanel::show(WKView* view, AlternativeTextType type, const FloatRect& boundingBoxOfReplacedString, const String& replacedString, const String& replacementString, const Vector<String>& alternativeReplacementStrings) 68{ 69 dismissInternal(ReasonForDismissingAlternativeTextIgnored, false); 70 71 if (!view) 72 return; 73 74 NSString* replacedStringAsNSString = replacedString; 75 NSString* replacementStringAsNSString = replacementString; 76 m_view = view; 77 NSCorrectionIndicatorType indicatorType = correctionIndicatorType(type); 78 79 NSMutableArray* alternativeStrings = 0; 80 if (!alternativeReplacementStrings.isEmpty()) { 81 size_t size = alternativeReplacementStrings.size(); 82 alternativeStrings = [NSMutableArray arrayWithCapacity:size]; 83 for (size_t i = 0; i < size; ++i) 84 [alternativeStrings addObject:(NSString*)alternativeReplacementStrings[i]]; 85 } 86 87 NSSpellChecker* spellChecker = [NSSpellChecker sharedSpellChecker]; 88 [spellChecker showCorrectionIndicatorOfType:indicatorType primaryString:replacementStringAsNSString alternativeStrings:alternativeStrings forStringInRect:boundingBoxOfReplacedString view:m_view.get() completionHandler:^(NSString* acceptedString) { 89 handleAcceptedReplacement(acceptedString, replacedStringAsNSString, replacementStringAsNSString, indicatorType); 90 }]; 91} 92 93String CorrectionPanel::dismiss(ReasonForDismissingAlternativeText reason) 94{ 95 return dismissInternal(reason, true); 96} 97 98String CorrectionPanel::dismissInternal(ReasonForDismissingAlternativeText reason, bool dismissingExternally) 99{ 100 if (!isShowing()) 101 return String(); 102 103 m_wasDismissedExternally = dismissingExternally; 104 m_reasonForDismissing = reason; 105 m_resultForDismissal.clear(); 106 [[NSSpellChecker sharedSpellChecker] dismissCorrectionIndicatorForView:m_view.get()]; 107 return m_resultForDismissal.get(); 108} 109 110void CorrectionPanel::recordAutocorrectionResponse(WKView* view, NSCorrectionResponse response, const String& replacedString, const String& replacementString) 111{ 112 [[NSSpellChecker sharedSpellChecker] recordResponse:response toCorrection:replacementString forWord:replacedString language:nil inSpellDocumentWithTag:[view spellCheckerDocumentTag]]; 113} 114 115void CorrectionPanel::handleAcceptedReplacement(NSString* acceptedReplacement, NSString* replaced, NSString* proposedReplacement, NSCorrectionIndicatorType correctionIndicatorType) 116{ 117 if (!m_view) 118 return; 119 120 NSSpellChecker* spellChecker = [NSSpellChecker sharedSpellChecker]; 121 NSInteger documentTag = [m_view spellCheckerDocumentTag]; 122 123 switch (correctionIndicatorType) { 124 case NSCorrectionIndicatorTypeDefault: 125 if (acceptedReplacement) 126 [spellChecker recordResponse:NSCorrectionResponseAccepted toCorrection:acceptedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag]; 127 else { 128 if (!m_wasDismissedExternally || m_reasonForDismissing == ReasonForDismissingAlternativeTextCancelled) 129 [spellChecker recordResponse:NSCorrectionResponseRejected toCorrection:proposedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag]; 130 else 131 [spellChecker recordResponse:NSCorrectionResponseIgnored toCorrection:proposedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag]; 132 } 133 break; 134 case NSCorrectionIndicatorTypeReversion: 135 if (acceptedReplacement) 136 [spellChecker recordResponse:NSCorrectionResponseReverted toCorrection:replaced forWord:acceptedReplacement language:nil inSpellDocumentWithTag:documentTag]; 137 break; 138 case NSCorrectionIndicatorTypeGuesses: 139 if (acceptedReplacement) 140 [spellChecker recordResponse:NSCorrectionResponseAccepted toCorrection:acceptedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag]; 141 break; 142 } 143 144 [m_view handleAcceptedAlternativeText:acceptedReplacement]; 145 m_view.clear(); 146 if (acceptedReplacement) 147 m_resultForDismissal = adoptNS([acceptedReplacement copy]); 148} 149 150} // namespace WebKit 151 152#endif // USE(AUTOCORRECTION_PANEL) 153