| 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" |
| 11 #include "chrome/app/chrome_command_ids.h" | 13 #include "chrome/app/chrome_command_ids.h" |
| 12 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 15 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
| 14 #include "chrome/browser/spellchecker/spellcheck_service.h" | 16 #include "chrome/browser/spellchecker/spellcheck_service.h" |
| 15 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/common/spellcheck_common.h" | 19 #include "chrome/common/spellcheck_common.h" |
| 18 #include "chrome/common/spellcheck_messages.h" | 20 #include "chrome/common/spellcheck_messages.h" |
| 19 #include "chrome/grit/generated_resources.h" | 21 #include "chrome/grit/generated_resources.h" |
| 20 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
| 21 #include "content/public/browser/render_widget_host_view.h" | 23 #include "content/public/browser/render_widget_host_view.h" |
| 22 #include "extensions/browser/view_type_utils.h" | 24 #include "extensions/browser/view_type_utils.h" |
| 23 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "ui/base/models/simple_menu_model.h" | 26 #include "ui/base/models/simple_menu_model.h" |
| 25 | 27 |
| 26 using content::BrowserThread; | 28 using content::BrowserThread; |
| 27 | 29 |
| 28 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( | 30 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( |
| 29 RenderViewContextMenuProxy* proxy, | 31 RenderViewContextMenuProxy* proxy, |
| 30 ui::SimpleMenuModel::Delegate* delegate, | 32 ui::SimpleMenuModel::Delegate* delegate, |
| 31 int group) | 33 int group) |
| 32 : proxy_(proxy), | 34 : proxy_(proxy), |
| 33 submenu_model_(delegate), | 35 submenu_model_(delegate), |
| 34 language_group_(group), | 36 language_group_(group), |
| 35 language_selected_(0) { | 37 num_selected_languages_(0) { |
| 36 DCHECK(proxy_); | 38 DCHECK(proxy_); |
| 37 } | 39 } |
| 38 | 40 |
| 39 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { | 41 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { |
| 40 } | 42 } |
| 41 | 43 |
| 42 void SpellCheckerSubMenuObserver::InitMenu( | 44 void SpellCheckerSubMenuObserver::InitMenu( |
| 43 const content::ContextMenuParams& params) { | 45 const content::ContextMenuParams& params) { |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 46 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 45 | 47 |
| 46 // Add available spell-checker languages to the sub menu. | 48 // Add available spell-checker languages to the sub menu. |
| 47 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | 49 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
| 48 DCHECK(browser_context); | 50 DCHECK(browser_context); |
| 49 language_selected_ = | 51 num_selected_languages_ = |
| 50 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); | 52 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); |
| 51 DCHECK(languages_.size() < | 53 DCHECK(languages_.size() < |
| 52 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); | 54 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); |
| 53 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 55 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
| 54 for (size_t i = 0; i < languages_.size(); ++i) { | 56 |
| 55 base::string16 display_name( | 57 if (chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) { |
| 56 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); | 58 for (size_t i = 0; i < languages_.size(); ++i) { |
| 57 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, | 59 submenu_model_.AddCheckItem( |
| 58 display_name, | 60 IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
| 59 language_group_); | 61 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); |
| 62 } |
| 63 } else { |
| 64 for (size_t i = 0; i < languages_.size(); ++i) { |
| 65 submenu_model_.AddRadioItem( |
| 66 IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
| 67 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true), |
| 68 language_group_); |
| 69 } |
| 60 } | 70 } |
| 61 | 71 |
| 62 // Add an item that opens the 'fonts and languages options' page. | 72 // Add an item that opens the 'fonts and languages options' page. |
| 63 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | 73 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
| 64 submenu_model_.AddItemWithStringId( | 74 submenu_model_.AddItemWithStringId( |
| 65 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, | 75 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, |
| 66 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); | 76 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); |
| 67 | 77 |
| 68 // Add a 'Check spelling while typing' item in the sub menu. | 78 // Add a 'Check spelling while typing' item in the sub menu. |
| 69 submenu_model_.AddCheckItem( | 79 submenu_model_.AddCheckItem( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 } | 124 } |
| 115 | 125 |
| 116 return false; | 126 return false; |
| 117 } | 127 } |
| 118 | 128 |
| 119 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { | 129 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { |
| 120 DCHECK(IsCommandIdSupported(command_id)); | 130 DCHECK(IsCommandIdSupported(command_id)); |
| 121 | 131 |
| 122 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 132 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 123 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 133 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 124 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 134 return num_selected_languages_ > |
| 135 static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST); |
| 125 } | 136 } |
| 126 | 137 |
| 127 // Check box for 'Check Spelling while typing'. | 138 // Check box for 'Check Spelling while typing'. |
| 128 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | 139 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
| 129 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 140 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| 130 DCHECK(profile); | 141 DCHECK(profile); |
| 131 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | 142 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); |
| 132 } | 143 } |
| 133 | 144 |
| 134 return false; | 145 return false; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 154 | 165 |
| 155 return false; | 166 return false; |
| 156 } | 167 } |
| 157 | 168 |
| 158 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { | 169 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
| 159 DCHECK(IsCommandIdSupported(command_id)); | 170 DCHECK(IsCommandIdSupported(command_id)); |
| 160 | 171 |
| 161 // Check to see if one of the spell check language ids have been clicked. | 172 // Check to see if one of the spell check language ids have been clicked. |
| 162 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | 173 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| 163 DCHECK(profile); | 174 DCHECK(profile); |
| 175 |
| 164 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 176 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 165 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 177 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 166 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 178 size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
| 167 if (profile && language < languages_.size()) { | 179 DCHECK_LT(language, languages_.size()); |
| 168 StringListPrefMember dictionary_language; | 180 StringListPrefMember dictionaries_pref; |
| 169 dictionary_language.Init(prefs::kSpellCheckDictionaries, | 181 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs()); |
| 170 profile->GetPrefs()); | 182 |
| 171 dictionary_language.SetValue( | 183 if (chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) { |
| 184 std::vector<std::string> dictionary_languages = |
| 185 dictionaries_pref.GetValue(); |
| 186 |
| 187 auto found_language = |
| 188 std::find(dictionary_languages.begin(), dictionary_languages.end(), |
| 189 languages_[language]); |
| 190 |
| 191 if (found_language != dictionary_languages.end()) |
| 192 dictionary_languages.erase(found_language); |
| 193 else |
| 194 dictionary_languages.push_back(languages_[language]); |
| 195 |
| 196 dictionaries_pref.SetValue(dictionary_languages); |
| 197 } else { |
| 198 dictionaries_pref.SetValue( |
| 172 std::vector<std::string>(1, languages_[language])); | 199 std::vector<std::string>(1, languages_[language])); |
| 173 } | 200 } |
| 174 return; | 201 return; |
| 175 } | 202 } |
| 176 | 203 |
| 177 switch (command_id) { | 204 switch (command_id) { |
| 178 case IDC_CHECK_SPELLING_WHILE_TYPING: | 205 case IDC_CHECK_SPELLING_WHILE_TYPING: |
| 179 profile->GetPrefs()->SetBoolean( | 206 profile->GetPrefs()->SetBoolean( |
| 180 prefs::kEnableContinuousSpellcheck, | 207 prefs::kEnableContinuousSpellcheck, |
| 181 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | 208 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); |
| 182 break; | 209 break; |
| 183 } | 210 } |
| 184 } | 211 } |
| OLD | NEW |