initial import
[vuplus_webkit] / Source / WebKit / mac / WebCoreSupport / CorrectionPanel.mm
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 #import "CorrectionPanel.h"
26
27 #import "WebViewPrivate.h"
28
29 #if !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
30 using namespace WebCore;
31
32 static inline NSCorrectionIndicatorType correctionIndicatorType(CorrectionPanelInfo::PanelType panelType)
33 {
34     switch (panelType) {
35     case CorrectionPanelInfo::PanelTypeCorrection:
36         return NSCorrectionIndicatorTypeDefault;
37     case CorrectionPanelInfo::PanelTypeReversion:
38         return NSCorrectionIndicatorTypeReversion;
39     case CorrectionPanelInfo::PanelTypeSpellingSuggestions:
40         return NSCorrectionIndicatorTypeGuesses;
41     }
42     ASSERT_NOT_REACHED();
43     return NSCorrectionIndicatorTypeDefault;
44 }
45
46 CorrectionPanel::CorrectionPanel()
47     : m_wasDismissedExternally(false)
48     , m_reasonForDismissing(ReasonForDismissingCorrectionPanelIgnored)
49 {
50 }
51
52 CorrectionPanel::~CorrectionPanel()
53 {
54     dismissInternal(ReasonForDismissingCorrectionPanelIgnored, false);
55 }
56
57 void CorrectionPanel::show(WebView* view, CorrectionPanelInfo::PanelType type, const FloatRect& boundingBoxOfReplacedString, const String& replacedString, const String& replacementString, const Vector<String>& alternativeReplacementStrings)
58 {
59     dismissInternal(ReasonForDismissingCorrectionPanelIgnored, false);
60     
61     if (!view)
62         return;
63
64     NSString* replacedStringAsNSString = replacedString;
65     NSString* replacementStringAsNSString = replacementString;
66     m_view = view;
67     NSCorrectionIndicatorType indicatorType = correctionIndicatorType(type);
68     
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]];
75     }
76
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);
79     }];
80 }
81
82 String CorrectionPanel::dismiss(ReasonForDismissingCorrectionPanel reason)
83 {
84     return dismissInternal(reason, true);
85 }
86
87 String CorrectionPanel::dismissInternal(ReasonForDismissingCorrectionPanel reason, bool dismissingExternally)
88 {
89     if (!isShowing())
90         return String();
91     
92     m_wasDismissedExternally = dismissingExternally;
93     m_reasonForDismissing = reason;
94     m_resultForDismissal.clear();
95     [[NSSpellChecker sharedSpellChecker] dismissCorrectionIndicatorForView:m_view.get()];
96     return m_resultForDismissal.get();
97 }
98
99 void CorrectionPanel::recordAutocorrectionResponse(WebView* view, NSCorrectionResponse response, const String& replacedString, const String& replacementString)
100 {
101     [[NSSpellChecker sharedSpellChecker] recordResponse:response toCorrection:replacementString forWord:replacedString language:nil inSpellDocumentWithTag:[view spellCheckerDocumentTag]];
102 }
103
104 void CorrectionPanel::handleAcceptedReplacement(NSString* acceptedReplacement, NSString* replaced, NSString* proposedReplacement,  NSCorrectionIndicatorType correctionIndicatorType)
105 {    
106     if (!m_view)
107         return;
108     
109     NSSpellChecker* spellChecker = [NSSpellChecker sharedSpellChecker];
110     NSInteger documentTag = [m_view.get() spellCheckerDocumentTag];
111
112     switch (correctionIndicatorType) {
113     case NSCorrectionIndicatorTypeDefault:
114         if (acceptedReplacement)
115             [spellChecker recordResponse:NSCorrectionResponseAccepted toCorrection:acceptedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
116         else {
117             if (!m_wasDismissedExternally || m_reasonForDismissing == ReasonForDismissingCorrectionPanelCancelled)
118                 [spellChecker recordResponse:NSCorrectionResponseRejected toCorrection:proposedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
119             else
120                 [spellChecker recordResponse:NSCorrectionResponseIgnored toCorrection:proposedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
121         }
122         break;
123     case NSCorrectionIndicatorTypeReversion:
124         if (acceptedReplacement)
125             [spellChecker recordResponse:NSCorrectionResponseReverted toCorrection:replaced forWord:acceptedReplacement language:nil inSpellDocumentWithTag:documentTag];
126         break;
127     case NSCorrectionIndicatorTypeGuesses:
128         if (acceptedReplacement)
129             [spellChecker recordResponse:NSCorrectionResponseAccepted toCorrection:acceptedReplacement forWord:replaced language:nil inSpellDocumentWithTag:documentTag];
130         break;
131     }
132     
133     [m_view.get() handleCorrectionPanelResult:acceptedReplacement];
134     m_view.clear();
135     if (acceptedReplacement)
136         m_resultForDismissal.adoptNS([acceptedReplacement copy]);
137 }
138
139 #endif // !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
140