OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "chrome/browser/renderer_context_menu/spellchecker_submenu_observer.h" | 5 #include "chrome/browser/renderer_context_menu/spellchecker_submenu_observer.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/prefs/pref_member.h" | 11 #include "base/prefs/pref_member.h" |
10 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" |
11 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
12 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 17 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
14 #include "chrome/browser/spellchecker/spellcheck_service.h" | 18 #include "chrome/browser/spellchecker/spellcheck_service.h" |
15 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 21 #include "chrome/common/spellcheck_common.h" |
17 #include "chrome/common/spellcheck_messages.h" | 22 #include "chrome/common/spellcheck_messages.h" |
18 #include "chrome/grit/generated_resources.h" | 23 #include "chrome/grit/generated_resources.h" |
19 #include "content/public/browser/render_view_host.h" | 24 #include "content/public/browser/render_view_host.h" |
20 #include "content/public/browser/render_widget_host_view.h" | 25 #include "content/public/browser/render_widget_host_view.h" |
21 #include "extensions/browser/view_type_utils.h" | 26 #include "extensions/browser/view_type_utils.h" |
22 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/base/models/simple_menu_model.h" | 28 #include "ui/base/models/simple_menu_model.h" |
24 | 29 |
25 using content::BrowserThread; | 30 using content::BrowserThread; |
26 | 31 |
27 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( | 32 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( |
28 RenderViewContextMenuProxy* proxy, | 33 RenderViewContextMenuProxy* proxy, |
29 ui::SimpleMenuModel::Delegate* delegate, | 34 ui::SimpleMenuModel::Delegate* delegate, |
30 int group) | 35 int group) |
31 : proxy_(proxy), | 36 : proxy_(proxy), |
32 submenu_model_(delegate), | 37 submenu_model_(delegate), |
33 language_group_(group), | 38 language_group_(group), |
34 language_selected_(0) { | 39 num_selected_languages_(0) { |
35 DCHECK(proxy_); | 40 DCHECK(proxy_); |
36 } | 41 } |
37 | 42 |
38 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { | 43 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { |
39 } | 44 } |
40 | 45 |
41 void SpellCheckerSubMenuObserver::InitMenu( | 46 void SpellCheckerSubMenuObserver::InitMenu( |
42 const content::ContextMenuParams& params) { | 47 const content::ContextMenuParams& params) { |
43 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 48 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
44 | 49 |
45 // Add available spell-checker languages to the sub menu. | 50 // Add available spell-checker languages to the sub menu. |
46 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 51 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
47 DCHECK(browser_context); | 52 DCHECK(browser_context); |
48 language_selected_ = | 53 num_selected_languages_ = |
49 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); | 54 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); |
50 DCHECK(languages_.size() < | 55 DCHECK(languages_.size() < |
51 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); | 56 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); |
52 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 57 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
53 for (size_t i = 0; i < languages_.size(); ++i) { | 58 |
54 base::string16 display_name( | 59 if (chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) { |
55 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); | 60 for (size_t i = 0; i < languages_.size(); ++i) { |
56 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, | 61 submenu_model_.AddCheckItem( |
57 display_name, | 62 IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
58 language_group_); | 63 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); |
| 64 } |
| 65 } else { |
| 66 for (size_t i = 0; i < languages_.size(); ++i) { |
| 67 submenu_model_.AddRadioItem( |
| 68 IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
| 69 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true), |
| 70 language_group_); |
| 71 } |
59 } | 72 } |
60 | 73 |
61 // Add an item that opens the 'fonts and languages options' page. | 74 // Add an item that opens the 'fonts and languages options' page. |
62 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | 75 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
63 submenu_model_.AddItemWithStringId( | 76 submenu_model_.AddItemWithStringId( |
64 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, | 77 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, |
65 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); | 78 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); |
66 | 79 |
67 // Add a 'Check spelling while typing' item in the sub menu. | 80 // Add a 'Check spelling while typing' item in the sub menu. |
68 submenu_model_.AddCheckItem( | 81 submenu_model_.AddCheckItem( |
69 IDC_CHECK_SPELLING_WHILE_TYPING, | 82 IDC_CHECK_SPELLING_WHILE_TYPING, |
70 l10n_util::GetStringUTF16( | 83 l10n_util::GetStringUTF16( |
71 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); | 84 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); |
72 | 85 |
73 // Add a check item "Ask Google for spelling suggestions" item. (This class | 86 // Add a check item "Ask Google for spelling suggestions" item. (This class |
74 // does not handle this item because the SpellingMenuObserver class handles it | 87 // does not handle this item because the SpellingMenuObserver class handles it |
75 // on behalf of this class.) | 88 // on behalf of this class.) |
76 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, | 89 if (!chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) { |
77 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); | 90 submenu_model_.AddCheckItem( |
| 91 IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, |
| 92 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); |
| 93 } |
78 | 94 |
79 // Add a check item "Automatically correct spelling". | 95 // Add a check item "Automatically correct spelling". |
80 const base::CommandLine* command_line = | 96 const base::CommandLine* command_line = |
81 base::CommandLine::ForCurrentProcess(); | 97 base::CommandLine::ForCurrentProcess(); |
82 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) { | 98 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) { |
83 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE, | 99 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE, |
84 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT)); | 100 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT)); |
85 } | 101 } |
86 | 102 |
87 proxy_->AddSubMenu( | 103 proxy_->AddSubMenu( |
(...skipping 22 matching lines...) Expand all Loading... |
110 } | 126 } |
111 | 127 |
112 return false; | 128 return false; |
113 } | 129 } |
114 | 130 |
115 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { | 131 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { |
116 DCHECK(IsCommandIdSupported(command_id)); | 132 DCHECK(IsCommandIdSupported(command_id)); |
117 | 133 |
118 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 134 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
119 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 135 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
120 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 136 return num_selected_languages_ > |
| 137 static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST); |
121 } | 138 } |
122 | 139 |
123 // Check box for 'Check Spelling while typing'. | 140 // Check box for 'Check Spelling while typing'. |
124 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | 141 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
125 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 142 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
126 DCHECK(profile); | 143 DCHECK(profile); |
127 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | 144 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); |
128 } | 145 } |
129 | 146 |
130 return false; | 147 return false; |
(...skipping 19 matching lines...) Expand all Loading... |
150 | 167 |
151 return false; | 168 return false; |
152 } | 169 } |
153 | 170 |
154 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { | 171 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
155 DCHECK(IsCommandIdSupported(command_id)); | 172 DCHECK(IsCommandIdSupported(command_id)); |
156 | 173 |
157 // Check to see if one of the spell check language ids have been clicked. | 174 // Check to see if one of the spell check language ids have been clicked. |
158 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 175 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
159 DCHECK(profile); | 176 DCHECK(profile); |
| 177 |
160 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 178 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
161 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 179 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
162 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 180 PrefService* prefs = profile->GetPrefs(); |
163 if (profile && language < languages_.size()) { | 181 size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
164 StringPrefMember dictionary_language; | 182 DCHECK_LT(language, languages_.size()); |
165 dictionary_language.Init(prefs::kSpellCheckDictionary, | 183 |
166 profile->GetPrefs()); | 184 if (chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) { |
167 dictionary_language.SetValue(languages_[language]); | 185 std::vector<std::string> dictionary_languages = |
| 186 chrome::spellcheck_common::GetDictionaryLanguagesPref(prefs); |
| 187 |
| 188 auto found_language = |
| 189 std::find(dictionary_languages.begin(), dictionary_languages.end(), |
| 190 languages_[language]); |
| 191 |
| 192 if (found_language != dictionary_languages.end()) |
| 193 dictionary_languages.erase(found_language); |
| 194 else |
| 195 dictionary_languages.push_back(languages_[language]); |
| 196 |
| 197 prefs->SetString( |
| 198 prefs::kSpellCheckDictionaries, |
| 199 JoinString(dictionary_languages, |
| 200 chrome::spellcheck_common::kDictionaryLanguagesSeparator)); |
| 201 } else { |
| 202 prefs->SetString(prefs::kSpellCheckDictionary, languages_[language]); |
168 } | 203 } |
169 return; | 204 return; |
170 } | 205 } |
171 | 206 |
172 switch (command_id) { | 207 switch (command_id) { |
173 case IDC_CHECK_SPELLING_WHILE_TYPING: | 208 case IDC_CHECK_SPELLING_WHILE_TYPING: |
174 profile->GetPrefs()->SetBoolean( | 209 profile->GetPrefs()->SetBoolean( |
175 prefs::kEnableContinuousSpellcheck, | 210 prefs::kEnableContinuousSpellcheck, |
176 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | 211 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); |
177 break; | 212 break; |
178 } | 213 } |
179 } | 214 } |
OLD | NEW |