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

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

Issue 1647723002: [win/cros/lin] Add back the spellcheck menu. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing includes. Created 4 years, 10 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/renderer_context_menu/spelling_options_submenu_observer .h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/prefs/pref_member.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
15 #include "chrome/browser/spellchecker/spellcheck_service.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/spellcheck_common.h"
19 #include "chrome/grit/generated_resources.h"
20 #include "content/public/browser/render_view_host.h"
21 #include "content/public/browser/render_widget_host_view.h"
22 #include "extensions/browser/view_type_utils.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/models/simple_menu_model.h"
25
26 using content::BrowserThread;
27
28 SpellingOptionsSubMenuObserver::SpellingOptionsSubMenuObserver(
29 RenderViewContextMenuProxy* proxy,
30 ui::SimpleMenuModel::Delegate* delegate,
31 int group_id)
32 : proxy_(proxy),
33 submenu_model_(delegate),
34 language_group_id_(group_id),
35 num_selected_languages_(0) {
36 DCHECK(proxy_);
37 }
38
39 SpellingOptionsSubMenuObserver::~SpellingOptionsSubMenuObserver() {}
40
41 void SpellingOptionsSubMenuObserver::InitMenu(
42 const content::ContextMenuParams& params) {
43 DCHECK_CURRENTLY_ON(BrowserThread::UI);
44
45 // Add available spell-checker languages to the sub menu.
46 content::BrowserContext* browser_context = proxy_->GetBrowserContext();
47 DCHECK(browser_context);
48 num_selected_languages_ =
49 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_);
50 DCHECK(languages_.size() <
51 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST);
52 const std::string app_locale = g_browser_process->GetApplicationLocale();
53
54 if (languages_.size() > 1) {
55 submenu_model_.AddRadioItemWithStringId(
56 IDC_SPELLCHECK_MULTI_LINGUAL,
57 IDS_CONTENT_CONTEXT_SPELLCHECK_MULTI_LINGUAL, language_group_id_);
58 }
59
60 const int kMaxLanguages = 100;
lazyboy 2016/02/01 19:23:33 kMaxLanguages = IDC_SPELLCHECK_LANGUAGES_LAST - ..
please use gerrit instead 2016/02/01 20:20:57 Done.
61 for (size_t i = 0; i < languages_.size() && i < kMaxLanguages; ++i) {
62 submenu_model_.AddRadioItem(
63 IDC_SPELLCHECK_LANGUAGES_FIRST + i,
64 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true),
65 language_group_id_);
66 }
67
68 // Add an item that opens the 'fonts and languages options' page.
69 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
70 submenu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS,
71 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS);
72
73 if (num_selected_languages_ > 0) {
74 // Add a 'Check spelling while typing' item in the sub menu.
75 submenu_model_.AddCheckItem(
76 IDC_CHECK_SPELLING_WHILE_TYPING,
77 l10n_util::GetStringUTF16(
78 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING));
79 }
80
81 // Add a check item "Ask Google for spelling suggestions" item. (This class
82 // does not handle this item because the SpellingMenuObserver class handles it
83 // on behalf of this class.)
84 submenu_model_.AddCheckItem(
85 IDC_CONTENT_CONTEXT_SPELLING_TOGGLE,
86 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE));
87
88 proxy_->AddSubMenu(
89 IDC_SPELLCHECK_MENU,
90 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU),
91 &submenu_model_);
92 }
93
94 bool SpellingOptionsSubMenuObserver::IsCommandIdSupported(int command_id) {
95 // Allow Spell Check language items on sub menu for text area context menu.
96 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
97 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) {
98 return true;
99 }
100
101 switch (command_id) {
102 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS:
103 // Return false so RenderViewContextMenu can handle this item because it
104 // is hard for this class to handle it.
105 return false;
106
107 case IDC_CHECK_SPELLING_WHILE_TYPING:
108 case IDC_SPELLCHECK_MENU:
109 case IDC_SPELLCHECK_MULTI_LINGUAL:
110 return true;
111 }
112
113 return false;
114 }
115
116 bool SpellingOptionsSubMenuObserver::IsCommandIdChecked(int command_id) {
117 DCHECK(IsCommandIdSupported(command_id));
118
119 if (command_id == IDC_SPELLCHECK_MULTI_LINGUAL)
120 return num_selected_languages_ == languages_.size();
121
122 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
123 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) {
124 if (languages_.size() == 1 && num_selected_languages_ == 1)
125 return true;
126
127 if (languages_.size() == num_selected_languages_)
128 return false;
129
130 size_t language_index =
131 static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST);
132 // The first |num_selected_languages_| are used for spellchecking.
133 return num_selected_languages_ > language_index;
134 }
135
136 // Check box for 'Check Spelling while typing'.
137 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) {
138 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext());
139 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck);
140 }
141
142 return false;
143 }
144
145 bool SpellingOptionsSubMenuObserver::IsCommandIdEnabled(int command_id) {
146 DCHECK(IsCommandIdSupported(command_id));
147
148 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext());
149 DCHECK(profile);
150 const PrefService* pref = profile->GetPrefs();
151 if ((command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
152 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) ||
153 command_id == IDC_SPELLCHECK_MULTI_LINGUAL) {
154 return pref->GetBoolean(prefs::kEnableContinuousSpellcheck);
155 }
156
157 switch (command_id) {
158 case IDC_CHECK_SPELLING_WHILE_TYPING:
159 case IDC_SPELLCHECK_MENU:
160 return true;
161 }
162
163 return false;
164 }
165
166 void SpellingOptionsSubMenuObserver::ExecuteCommand(int command_id) {
167 DCHECK(IsCommandIdSupported(command_id));
168
169 // Check to see if one of the spell check language ids have been clicked.
170 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext());
171 DCHECK(profile);
172
173 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
174 static_cast<size_t>(command_id) <
175 IDC_SPELLCHECK_LANGUAGES_FIRST + languages_.size()) {
176 size_t language_index = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST;
177 StringListPrefMember dictionaries_pref;
178 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs());
179 dictionaries_pref.SetValue({languages_[language_index]});
180 return;
181 }
182
183 switch (command_id) {
184 case IDC_CHECK_SPELLING_WHILE_TYPING:
185 profile->GetPrefs()->SetBoolean(
186 prefs::kEnableContinuousSpellcheck,
187 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck));
188 break;
189
190 case IDC_SPELLCHECK_MULTI_LINGUAL:
191 StringListPrefMember dictionaries_pref;
192 dictionaries_pref.Init(prefs::kSpellCheckDictionaries,
193 profile->GetPrefs());
194 dictionaries_pref.SetValue(languages_);
195 break;
196 }
197 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698