OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file implements the interface defined in spellchecker_platform_engine.h | 5 // This file implements the interface defined in spellchecker_platform_engine.h |
6 // for the OS X platform. | 6 // for the OS X platform. |
7 | 7 |
8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
9 | 9 |
10 #include "chrome/browser/spellchecker_common.h" | 10 #include "chrome/browser/spellchecker_common.h" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 // If this file was compiled, then we know that we are on OS X 10.5 at least | 85 // If this file was compiled, then we know that we are on OS X 10.5 at least |
86 // and can safely return true here. | 86 // and can safely return true here. |
87 return true; | 87 return true; |
88 } | 88 } |
89 | 89 |
90 bool SpellCheckerProvidesPanel() { | 90 bool SpellCheckerProvidesPanel() { |
91 // OS X has a Spelling Panel, so we can return true here. | 91 // OS X has a Spelling Panel, so we can return true here. |
92 return true; | 92 return true; |
93 } | 93 } |
94 | 94 |
95 bool SpellCheckerPanelVisible() { | 95 bool SpellingPanelVisible() { |
96 return [[[NSSpellChecker sharedSpellChecker] spellingPanel] | 96 // This should only be called from the main thread. |
97 isVisible] ? true : false; | 97 return [[[NSSpellChecker sharedSpellChecker] spellingPanel] isVisible]; |
| 98 } |
| 99 |
| 100 void ShowSpellingPanel(bool show) { |
| 101 if (show) { |
| 102 [[[NSSpellChecker sharedSpellChecker] spellingPanel] |
| 103 performSelectorOnMainThread:@selector(makeKeyAndOrderFront:) withObject:nil |
| 104 waitUntilDone:YES]; |
| 105 } else { |
| 106 [[[NSSpellChecker sharedSpellChecker] spellingPanel] |
| 107 performSelectorOnMainThread:@selector(close) withObject:nil |
| 108 waitUntilDone:YES]; |
| 109 } |
| 110 } |
| 111 |
| 112 void UpdateSpellingPanelWithMisspelledWord(const std::wstring& word) { |
| 113 NSString * word_to_display = base::SysWideToNSString(word); |
| 114 [[NSSpellChecker sharedSpellChecker] |
| 115 updateSpellingPanelWithMisspelledWord:word_to_display]; |
98 } | 116 } |
99 | 117 |
100 void Init() { | 118 void Init() { |
101 // This call must be made before the call to | 119 // This call must be made before the call to |
102 // [NSSpellchecker sharedSpellChecker] or it will return nil. | 120 // [NSSpellchecker sharedSpellChecker] or it will return nil. |
103 [NSApplication sharedApplication]; | 121 [NSApplication sharedApplication]; |
104 } | 122 } |
105 | 123 |
106 bool PlatformSupportsLanguage(const std::string& current_language) { | 124 bool PlatformSupportsLanguage(const std::string& current_language) { |
107 // First, convert Language to an osx lang code NSString. | 125 // First, convert Language to an osx lang code NSString. |
108 NSString* mac_lang_code = ConvertLanguageCodeToMac(current_language); | 126 NSString* mac_lang_code = ConvertLanguageCodeToMac(current_language); |
109 | 127 |
110 // Then grab the languages available. | 128 // Then grab the languages available. |
111 NSArray* availableLanguages; | 129 NSArray* availableLanguages; |
112 availableLanguages = [[NSSpellChecker sharedSpellChecker] | 130 availableLanguages = [[NSSpellChecker sharedSpellChecker] |
113 availableLanguages]; | 131 availableLanguages]; |
114 | 132 |
115 // Return true if the given languange is supported by os x. | 133 // Return true if the given languange is supported by os x. |
116 return [availableLanguages containsObject:mac_lang_code]; | 134 return [availableLanguages containsObject:mac_lang_code]; |
117 } | 135 } |
118 | 136 |
119 void SetLanguage(const std::string& lang_to_set) { | 137 void SetLanguage(const std::string& lang_to_set) { |
120 NSString* NS_lang_to_set = ConvertLanguageCodeToMac(lang_to_set); | 138 NSString* NS_lang_to_set = ConvertLanguageCodeToMac(lang_to_set); |
121 [[NSSpellChecker sharedSpellChecker] setLanguage:NS_lang_to_set]; | 139 [[NSSpellChecker sharedSpellChecker] setLanguage:NS_lang_to_set]; |
122 } | 140 } |
123 | 141 |
124 bool CheckSpelling(const std::string& word_to_check) { | 142 static int last_seen_tag_; |
| 143 |
| 144 bool CheckSpelling(const std::string& word_to_check, int tag) { |
| 145 last_seen_tag_ = tag; |
| 146 |
125 // [[NSSpellChecker sharedSpellChecker] checkSpellingOfString] returns an | 147 // [[NSSpellChecker sharedSpellChecker] checkSpellingOfString] returns an |
126 // NSRange that we can look at to determine if a word is misspelled. | 148 // NSRange that we can look at to determine if a word is misspelled. |
127 NSRange spell_range = {0,0}; | 149 NSRange spell_range = {0,0}; |
128 | 150 |
129 // Convert the word to an NSString. | 151 // Convert the word to an NSString. |
130 NSString* NS_word_to_check = base::SysUTF8ToNSString(word_to_check); | 152 NSString* NS_word_to_check = base::SysUTF8ToNSString(word_to_check); |
131 // Check the spelling, starting at the beginning of the word. | 153 // Check the spelling, starting at the beginning of the word. |
132 spell_range = [[NSSpellChecker sharedSpellChecker] | 154 spell_range = [[NSSpellChecker sharedSpellChecker] |
133 checkSpellingOfString:NS_word_to_check startingAt:0]; | 155 checkSpellingOfString:NS_word_to_check startingAt:0 |
| 156 language:nil wrap:NO inSpellDocumentWithTag:tag |
| 157 wordCount:NULL]; |
134 | 158 |
135 // If the length of the misspelled word == 0, | 159 // If the length of the misspelled word == 0, |
136 // then there is no misspelled word. | 160 // then there is no misspelled word. |
137 bool word_correct = (spell_range.length == 0); | 161 bool word_correct = (spell_range.length == 0); |
138 return word_correct; | 162 return word_correct; |
139 } | 163 } |
140 | 164 |
141 void FillSuggestionList(const std::string& wrong_word, | 165 void FillSuggestionList(const std::string& wrong_word, |
142 std::vector<std::wstring>* optional_suggestions) { | 166 std::vector<std::wstring>* optional_suggestions) { |
143 NSString* NS_wrong_word = base::SysUTF8ToNSString(wrong_word); | 167 NSString* NS_wrong_word = base::SysUTF8ToNSString(wrong_word); |
(...skipping 14 matching lines...) Expand all Loading... |
158 | 182 |
159 void AddWord(const std::wstring& word) { | 183 void AddWord(const std::wstring& word) { |
160 NSString* word_to_add = base::SysWideToNSString(word); | 184 NSString* word_to_add = base::SysWideToNSString(word); |
161 [[NSSpellChecker sharedSpellChecker] learnWord:word_to_add]; | 185 [[NSSpellChecker sharedSpellChecker] learnWord:word_to_add]; |
162 } | 186 } |
163 | 187 |
164 void RemoveWord(const std::wstring& word) { | 188 void RemoveWord(const std::wstring& word) { |
165 NSString *word_to_remove = base::SysWideToNSString(word); | 189 NSString *word_to_remove = base::SysWideToNSString(word); |
166 [[NSSpellChecker sharedSpellChecker] unlearnWord:word_to_remove]; | 190 [[NSSpellChecker sharedSpellChecker] unlearnWord:word_to_remove]; |
167 } | 191 } |
| 192 |
| 193 int GetDocumentTag() { |
| 194 NSInteger doc_tag = [NSSpellChecker uniqueSpellDocumentTag]; |
| 195 return static_cast<int>(doc_tag); |
| 196 } |
| 197 |
| 198 void IgnoreWord(const std::string& word) { |
| 199 [[NSSpellChecker sharedSpellChecker] ignoreWord:base::SysUTF8ToNSString(word) |
| 200 inSpellDocumentWithTag:last_seen_tag_]; |
| 201 } |
| 202 |
| 203 void CloseDocumentWithTag(int tag) { |
| 204 [[NSSpellChecker sharedSpellChecker] |
| 205 closeSpellDocumentWithTag:static_cast<NSInteger>(tag)]; |
| 206 } |
168 } // namespace SpellCheckerPlatform | 207 } // namespace SpellCheckerPlatform |
169 | 208 |
OLD | NEW |