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