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

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

Issue 654653002: Enables the user to select multiple languages for spellchecking (UI) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed the value of 'enable-multilingual-spellchecker' in histograms.xml Created 5 years, 9 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"
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) {
35 DCHECK(proxy_); 39 DCHECK(proxy_);
36 } 40 }
37 41
38 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { 42 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() {
39 } 43 }
40 44
41 void SpellCheckerSubMenuObserver::InitMenu( 45 void SpellCheckerSubMenuObserver::InitMenu(
42 const content::ContextMenuParams& params) { 46 const content::ContextMenuParams& params) {
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
44 48
45 // Add available spell-checker languages to the sub menu. 49 // Add available spell-checker languages to the sub menu.
46 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); 50 content::BrowserContext* browser_context = proxy_->GetBrowserContext();
47 DCHECK(browser_context); 51 DCHECK(browser_context);
48 language_selected_ = 52 spellcheck_languages_ =
49 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); 53 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_);
50 DCHECK(languages_.size() < 54 DCHECK(languages_.size() <
51 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); 55 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST);
52 const std::string app_locale = g_browser_process->GetApplicationLocale(); 56 const std::string app_locale = g_browser_process->GetApplicationLocale();
57 const base::CommandLine* command_line =
58 base::CommandLine::ForCurrentProcess();
59
53 for (size_t i = 0; i < languages_.size(); ++i) { 60 for (size_t i = 0; i < languages_.size(); ++i) {
54 base::string16 display_name( 61 base::string16 display_name(
55 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); 62 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true));
56 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, 63 if (command_line->HasSwitch(switches::kEnableMultilingualSpellChecker)) {
57 display_name, 64 submenu_model_.AddCheckItem(
58 language_group_); 65 IDC_SPELLCHECK_LANGUAGES_FIRST + i, display_name);
66 } else {
67 submenu_model_.AddRadioItem(
68 IDC_SPELLCHECK_LANGUAGES_FIRST + i, display_name, language_group_);
69 }
59 } 70 }
60 71
61 // Add an item that opens the 'fonts and languages options' page. 72 // Add an item that opens the 'fonts and languages options' page.
62 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); 73 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
63 submenu_model_.AddItemWithStringId( 74 submenu_model_.AddItemWithStringId(
64 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, 75 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS,
65 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); 76 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS);
66 77
67 // Add a 'Check spelling while typing' item in the sub menu. 78 // Add a 'Check spelling while typing' item in the sub menu.
68 submenu_model_.AddCheckItem( 79 submenu_model_.AddCheckItem(
69 IDC_CHECK_SPELLING_WHILE_TYPING, 80 IDC_CHECK_SPELLING_WHILE_TYPING,
70 l10n_util::GetStringUTF16( 81 l10n_util::GetStringUTF16(
71 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); 82 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING));
72 83
73 // Add a check item "Ask Google for spelling suggestions" item. (This class 84 // Add a check item "Ask Google for spelling suggestions" item. (This class
74 // does not handle this item because the SpellingMenuObserver class handles it 85 // does not handle this item because the SpellingMenuObserver class handles it
75 // on behalf of this class.) 86 // on behalf of this class.)
76 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, 87 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE,
77 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); 88 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE));
78 89
79 // Add a check item "Automatically correct spelling". 90 // Add a check item "Automatically correct spelling".
80 const base::CommandLine* command_line =
81 base::CommandLine::ForCurrentProcess();
82 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) { 91 if (command_line->HasSwitch(switches::kEnableSpellingAutoCorrect)) {
83 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE, 92 submenu_model_.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE,
84 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT)); 93 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT));
85 } 94 }
86 95
87 proxy_->AddSubMenu( 96 proxy_->AddSubMenu(
88 IDC_SPELLCHECK_MENU, 97 IDC_SPELLCHECK_MENU,
89 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), 98 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU),
90 &submenu_model_); 99 &submenu_model_);
91 } 100 }
(...skipping 18 matching lines...) Expand all
110 } 119 }
111 120
112 return false; 121 return false;
113 } 122 }
114 123
115 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { 124 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) {
116 DCHECK(IsCommandIdSupported(command_id)); 125 DCHECK(IsCommandIdSupported(command_id));
117 126
118 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && 127 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
119 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { 128 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) {
120 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; 129 return spellcheck_languages_.find(command_id -
130 IDC_SPELLCHECK_LANGUAGES_FIRST) != spellcheck_languages_.end();
please use gerrit instead 2015/02/25 18:16:15 Please run the clang-format on these two lines as
Klemen Forstnerič 2015/02/26 18:08:32 Done.
121 } 131 }
122 132
123 // Check box for 'Check Spelling while typing'. 133 // Check box for 'Check Spelling while typing'.
124 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { 134 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) {
125 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); 135 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext());
126 DCHECK(profile); 136 DCHECK(profile);
127 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); 137 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck);
128 } 138 }
129 139
130 return false; 140 return false;
(...skipping 19 matching lines...) Expand all
150 160
151 return false; 161 return false;
152 } 162 }
153 163
154 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { 164 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) {
155 DCHECK(IsCommandIdSupported(command_id)); 165 DCHECK(IsCommandIdSupported(command_id));
156 166
157 // Check to see if one of the spell check language ids have been clicked. 167 // Check to see if one of the spell check language ids have been clicked.
158 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); 168 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext());
159 DCHECK(profile); 169 DCHECK(profile);
170
171 PrefService* prefs = profile->GetPrefs();
160 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && 172 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
161 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { 173 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) {
162 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; 174 size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST;
163 if (profile && language < languages_.size()) { 175
please use gerrit instead 2015/02/25 18:16:15 No need for this newline.
Klemen Forstnerič 2015/02/26 18:08:32 Done.
164 StringPrefMember dictionary_language; 176 DCHECK_LT(language, languages_.size());
165 dictionary_language.Init(prefs::kSpellCheckDictionary, 177
166 profile->GetPrefs()); 178 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
167 dictionary_language.SetValue(languages_[language]); 179 switches::kEnableMultilingualSpellChecker)) {
please use gerrit instead 2015/02/25 18:16:15 These two lines would be a good place to run clang
Klemen Forstnerič 2015/02/26 18:08:32 I got it all set up in Sublime Text, and I usually
180 std::vector<std::string> languages_split;
181 base::SplitString(
182 prefs->GetString(prefs::kSpellCheckDictionaries),
183 chrome::spellcheck_common::kDictionaryLanguagesSeparator,
184 &languages_split);
185
186 auto found_language = std::find(languages_split.begin(),
187 languages_split.end(),
188 languages_[language]);
189
190 if (found_language != languages_split.end())
191 languages_split.erase(found_language);
192 else
193 languages_split.push_back(languages_[language]);
194
195 prefs->SetString(
196 prefs::kSpellCheckDictionaries,
197 JoinString(languages_split,
198 chrome::spellcheck_common::kDictionaryLanguagesSeparator));
199 } else {
200 prefs->SetString(prefs::kSpellCheckDictionary, languages_[language]);
168 } 201 }
169 return; 202 return;
170 } 203 }
171 204
172 switch (command_id) { 205 switch (command_id) {
173 case IDC_CHECK_SPELLING_WHILE_TYPING: 206 case IDC_CHECK_SPELLING_WHILE_TYPING:
174 profile->GetPrefs()->SetBoolean( 207 profile->GetPrefs()->SetBoolean(
175 prefs::kEnableContinuousSpellcheck, 208 prefs::kEnableContinuousSpellcheck,
176 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); 209 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck));
177 break; 210 break;
178 } 211 }
179 } 212 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698