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" |
11 #include "chrome/browser/spellchecker_platform_engine.h" | 11 #include "chrome/browser/spellchecker_platform_engine.h" |
12 #include "base/time.h" | 12 #include "base/time.h" |
13 #include "base/histogram.h" | 13 #include "base/histogram.h" |
14 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
15 | 15 |
16 using base::TimeTicks; | 16 using base::TimeTicks; |
17 namespace { | 17 namespace { |
18 // The number of characters in the first part of the language code. | 18 // The number of characters in the first part of the language code. |
19 const unsigned int kShortLanguageCodeSize = 2; | 19 const unsigned int kShortLanguageCodeSize = 2; |
20 | 20 |
21 // A private utility function to convert hunspell language codes to os x | 21 // A private utility function to convert hunspell language codes to os x |
22 // language codes. | 22 // language codes. |
23 NSString* ConvertLanguageCodeToMac(const Language& lang_code) { | 23 NSString* ConvertLanguageCodeToMac(const std::string& lang_code) { |
24 NSString* whole_code = base::SysUTF8ToNSString(lang_code); | 24 NSString* whole_code = base::SysUTF8ToNSString(lang_code); |
25 | 25 |
26 if ([whole_code length] > kShortLanguageCodeSize) { | 26 if ([whole_code length] > kShortLanguageCodeSize) { |
27 NSString* lang_code = [whole_code | 27 NSString* lang_code = [whole_code |
28 substringToIndex:kShortLanguageCodeSize]; | 28 substringToIndex:kShortLanguageCodeSize]; |
29 // Add 1 here to skip the underscore. | 29 // Add 1 here to skip the underscore. |
30 NSString* region_code = [whole_code | 30 NSString* region_code = [whole_code |
31 substringFromIndex:(kShortLanguageCodeSize + 1)]; | 31 substringFromIndex:(kShortLanguageCodeSize + 1)]; |
32 | 32 |
33 // Check for the special case of en-US and pt-PT, since os x lists these | 33 // Check for the special case of en-US and pt-PT, since os x lists these |
(...skipping 28 matching lines...) Expand all Loading... |
62 // and can safely return true here. | 62 // and can safely return true here. |
63 return true; | 63 return true; |
64 } | 64 } |
65 | 65 |
66 void Init() { | 66 void Init() { |
67 // This call must be made before the call to | 67 // This call must be made before the call to |
68 // [NSSpellchecker sharedSpellChecker] or it will return nil. | 68 // [NSSpellchecker sharedSpellChecker] or it will return nil. |
69 [NSApplication sharedApplication]; | 69 [NSApplication sharedApplication]; |
70 } | 70 } |
71 | 71 |
72 bool PlatformSupportsLanguage(const Language& current_language) { | 72 bool PlatformSupportsLanguage(const std::string& current_language) { |
73 // First, convert Language to an osx lang code NSString. | 73 // First, convert Language to an osx lang code NSString. |
74 NSString* mac_lang_code = ConvertLanguageCodeToMac(current_language); | 74 NSString* mac_lang_code = ConvertLanguageCodeToMac(current_language); |
75 | 75 |
76 // Then grab the languages available. | 76 // Then grab the languages available. |
77 NSArray* availableLanguages; | 77 NSArray* availableLanguages; |
78 availableLanguages = [[NSSpellChecker sharedSpellChecker] | 78 availableLanguages = [[NSSpellChecker sharedSpellChecker] |
79 availableLanguages]; | 79 availableLanguages]; |
80 | 80 |
81 // Return true if the given languange is supported by os x. | 81 // Return true if the given languange is supported by os x. |
82 return [availableLanguages containsObject:mac_lang_code]; | 82 return [availableLanguages containsObject:mac_lang_code]; |
83 } | 83 } |
84 | 84 |
85 void SetLanguage(const Language& lang_to_set) { | 85 void SetLanguage(const std::string& lang_to_set) { |
86 NSString* NS_lang_to_set = ConvertLanguageCodeToMac(lang_to_set); | 86 NSString* NS_lang_to_set = ConvertLanguageCodeToMac(lang_to_set); |
87 [[NSSpellChecker sharedSpellChecker] setLanguage:NS_lang_to_set]; | 87 [[NSSpellChecker sharedSpellChecker] setLanguage:NS_lang_to_set]; |
88 } | 88 } |
89 | 89 |
90 bool CheckSpelling(const std::string& word_to_check) { | 90 bool CheckSpelling(const std::string& word_to_check) { |
91 // [[NSSpellChecker sharedSpellChecker] checkSpellingOfString] returns an | 91 // [[NSSpellChecker sharedSpellChecker] checkSpellingOfString] returns an |
92 // NSRange that we can look at to determine if a word is misspelled. | 92 // NSRange that we can look at to determine if a word is misspelled. |
93 NSRange spell_range = {0,0}; | 93 NSRange spell_range = {0,0}; |
94 | 94 |
95 // Convert the word to an NSString. | 95 // Convert the word to an NSString. |
(...skipping 30 matching lines...) Expand all Loading... |
126 NSString* word_to_add = base::SysWideToNSString(word); | 126 NSString* word_to_add = base::SysWideToNSString(word); |
127 [[NSSpellChecker sharedSpellChecker] learnWord:word_to_add]; | 127 [[NSSpellChecker sharedSpellChecker] learnWord:word_to_add]; |
128 } | 128 } |
129 | 129 |
130 void RemoveWord(const std::wstring& word) { | 130 void RemoveWord(const std::wstring& word) { |
131 NSString *word_to_remove = base::SysWideToNSString(word); | 131 NSString *word_to_remove = base::SysWideToNSString(word); |
132 [[NSSpellChecker sharedSpellChecker] unlearnWord:word_to_remove]; | 132 [[NSSpellChecker sharedSpellChecker] unlearnWord:word_to_remove]; |
133 } | 133 } |
134 } // namespace SpellCheckerPlatform | 134 } // namespace SpellCheckerPlatform |
135 | 135 |
OLD | NEW |