Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(457)

Side by Side Diff: chrome/browser/renderer_context_menu/spellchecker_submenu_observer_hunspell.cc

Issue 1156473007: Enables the user to select multiple languages for spellchecking (UI) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Replied to old comments and fixed small issues. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
please use gerrit instead 2015/07/06 23:28:15 Unused include.
Julius 2015/07/07 01:14:20 Done.
14 #include "base/strings/string_util.h"
please use gerrit instead 2015/07/06 23:28:15 Unused include.
Julius 2015/07/07 01:14:20 Done.
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"
17 #include "chrome/common/spellcheck_common.h" 21 #include "chrome/common/spellcheck_common.h"
18 #include "chrome/common/spellcheck_messages.h" 22 #include "chrome/common/spellcheck_messages.h"
19 #include "chrome/grit/generated_resources.h" 23 #include "chrome/grit/generated_resources.h"
20 #include "content/public/browser/render_view_host.h" 24 #include "content/public/browser/render_view_host.h"
21 #include "content/public/browser/render_widget_host_view.h" 25 #include "content/public/browser/render_widget_host_view.h"
22 #include "extensions/browser/view_type_utils.h" 26 #include "extensions/browser/view_type_utils.h"
23 #include "ui/base/l10n/l10n_util.h" 27 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/models/simple_menu_model.h" 28 #include "ui/base/models/simple_menu_model.h"
25 29
26 using content::BrowserThread; 30 using content::BrowserThread;
27 31
28 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( 32 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver(
29 RenderViewContextMenuProxy* proxy, 33 RenderViewContextMenuProxy* proxy,
30 ui::SimpleMenuModel::Delegate* delegate, 34 ui::SimpleMenuModel::Delegate* delegate,
31 int group) 35 int group)
32 : proxy_(proxy), 36 : proxy_(proxy),
33 submenu_model_(delegate), 37 submenu_model_(delegate),
34 language_group_(group), 38 language_group_(group),
35 language_selected_(0) { 39 num_selected_languages_(0) {
36 DCHECK(proxy_); 40 DCHECK(proxy_);
37 } 41 }
38 42
39 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { 43 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() {
40 } 44 }
41 45
42 void SpellCheckerSubMenuObserver::InitMenu( 46 void SpellCheckerSubMenuObserver::InitMenu(
43 const content::ContextMenuParams& params) { 47 const content::ContextMenuParams& params) {
44 DCHECK_CURRENTLY_ON(BrowserThread::UI); 48 DCHECK_CURRENTLY_ON(BrowserThread::UI);
45 49
46 // Add available spell-checker languages to the sub menu. 50 // Add available spell-checker languages to the sub menu.
47 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); 51 content::BrowserContext* browser_context = proxy_->GetBrowserContext();
48 DCHECK(browser_context); 52 DCHECK(browser_context);
49 language_selected_ = 53 num_selected_languages_ =
50 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); 54 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_);
51 DCHECK(languages_.size() < 55 DCHECK(languages_.size() <
52 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); 56 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST);
53 const std::string app_locale = g_browser_process->GetApplicationLocale(); 57 const std::string app_locale = g_browser_process->GetApplicationLocale();
54 for (size_t i = 0; i < languages_.size(); ++i) { 58
55 base::string16 display_name( 59 if (chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) {
56 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); 60 for (size_t i = 0; i < languages_.size(); ++i) {
57 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, 61 submenu_model_.AddCheckItem(
58 display_name, 62 IDC_SPELLCHECK_LANGUAGES_FIRST + i,
59 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 }
60 } 72 }
61 73
62 // Add an item that opens the 'fonts and languages options' page. 74 // Add an item that opens the 'fonts and languages options' page.
63 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); 75 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
64 submenu_model_.AddItemWithStringId( 76 submenu_model_.AddItemWithStringId(
65 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, 77 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS,
66 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); 78 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS);
67 79
68 // Add a 'Check spelling while typing' item in the sub menu. 80 // Add a 'Check spelling while typing' item in the sub menu.
69 submenu_model_.AddCheckItem( 81 submenu_model_.AddCheckItem(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 } 126 }
115 127
116 return false; 128 return false;
117 } 129 }
118 130
119 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { 131 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) {
120 DCHECK(IsCommandIdSupported(command_id)); 132 DCHECK(IsCommandIdSupported(command_id));
121 133
122 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && 134 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
123 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { 135 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) {
124 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);
125 } 138 }
126 139
127 // Check box for 'Check Spelling while typing'. 140 // Check box for 'Check Spelling while typing'.
128 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { 141 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) {
129 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); 142 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext());
130 DCHECK(profile); 143 DCHECK(profile);
131 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); 144 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck);
132 } 145 }
133 146
134 return false; 147 return false;
(...skipping 19 matching lines...) Expand all
154 167
155 return false; 168 return false;
156 } 169 }
157 170
158 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { 171 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) {
159 DCHECK(IsCommandIdSupported(command_id)); 172 DCHECK(IsCommandIdSupported(command_id));
160 173
161 // 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.
162 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); 175 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext());
163 DCHECK(profile); 176 DCHECK(profile);
177
164 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && 178 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
165 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { 179 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) {
166 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; 180 size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST;
167 if (profile && language < languages_.size()) { 181 DCHECK_LT(language, languages_.size());
168 StringListPrefMember dictionary_language; 182 StringListPrefMember dictionaries_pref;
169 dictionary_language.Init(prefs::kSpellCheckDictionaries, 183 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs());
170 profile->GetPrefs()); 184
171 dictionary_language.SetValue( 185 if (chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()) {
186 std::vector<std::string> dictionary_languages =
187 chrome::spellcheck_common::GetDictionaryLanguagesPref(
188 profile->GetPrefs());
189
190 auto found_language =
191 std::find(dictionary_languages.begin(), dictionary_languages.end(),
192 languages_[language]);
193
194 if (found_language != dictionary_languages.end())
195 dictionary_languages.erase(found_language);
196 else
197 dictionary_languages.push_back(languages_[language]);
198
199 dictionaries_pref.SetValue(dictionary_languages);
200 } else {
201 dictionaries_pref.SetValue(
172 std::vector<std::string>(1, languages_[language])); 202 std::vector<std::string>(1, languages_[language]));
173 } 203 }
174 return; 204 return;
175 } 205 }
176 206
177 switch (command_id) { 207 switch (command_id) {
178 case IDC_CHECK_SPELLING_WHILE_TYPING: 208 case IDC_CHECK_SPELLING_WHILE_TYPING:
179 profile->GetPrefs()->SetBoolean( 209 profile->GetPrefs()->SetBoolean(
180 prefs::kEnableContinuousSpellcheck, 210 prefs::kEnableContinuousSpellcheck,
181 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); 211 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck));
182 break; 212 break;
183 } 213 }
184 } 214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698