OLD | NEW |
---|---|
(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/spellchecker_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 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( | |
29 RenderViewContextMenuProxy* proxy, | |
30 ui::SimpleMenuModel::Delegate* delegate, | |
31 int group) | |
32 : proxy_(proxy), | |
33 submenu_model_(delegate), | |
34 language_group_(group), | |
35 num_selected_languages_(0) { | |
36 DCHECK(proxy_); | |
37 DCHECK(!chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()); | |
38 } | |
39 | |
40 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { | |
41 } | |
42 | |
43 void SpellCheckerSubMenuObserver::InitMenu( | |
44 const content::ContextMenuParams& params) { | |
45 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
46 | |
47 submenu_model_.AddRadioItemWithStringId( | |
lazyboy
2016/01/28 20:36:24
Do we not want to guard this with >= 2 languages?
please use gerrit instead
2016/01/28 21:41:59
Done in patch 2.
| |
48 IDC_SPELLCHECK_MULTI_LINGUAL, | |
49 IDS_CONTENT_CONTEXT_SPELLCHECK_MULTI_LINGUAL, language_group_); | |
50 | |
51 // Add available spell-checker languages to the sub menu. | |
52 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | |
53 DCHECK(browser_context); | |
54 num_selected_languages_ = | |
55 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); | |
56 DCHECK(languages_.size() < | |
57 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); | |
58 const std::string app_locale = g_browser_process->GetApplicationLocale(); | |
59 | |
60 for (size_t i = 0; i < languages_.size(); ++i) { | |
lazyboy
2016/01/28 20:36:25
Should we skip items beyond 100 if languages_.size
please use gerrit instead
2016/01/28 21:41:59
Done.
| |
61 submenu_model_.AddRadioItem( | |
62 IDC_SPELLCHECK_LANGUAGES_FIRST + i, | |
63 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true), | |
64 language_group_); | |
65 } | |
66 | |
67 // Add an item that opens the 'fonts and languages options' page. | |
68 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | |
69 submenu_model_.AddItemWithStringId( | |
70 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 SpellCheckerSubMenuObserver::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 SpellCheckerSubMenuObserver::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 return num_selected_languages_ != languages_.size() && | |
125 num_selected_languages_ > | |
126 static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST); | |
127 } | |
128 | |
129 // Check box for 'Check Spelling while typing'. | |
130 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | |
131 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
132 DCHECK(profile); | |
lazyboy
2016/01/28 20:36:25
This is probably not necessary, we'll crash on the
please use gerrit instead
2016/01/28 21:41:59
Done.
| |
133 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | |
134 } | |
135 | |
136 return false; | |
137 } | |
138 | |
139 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) { | |
140 DCHECK(IsCommandIdSupported(command_id)); | |
141 | |
142 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
143 DCHECK(profile); | |
144 const PrefService* pref = profile->GetPrefs(); | |
145 if ((command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
146 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) || | |
147 command_id == IDC_SPELLCHECK_MULTI_LINGUAL) { | |
148 return pref->GetBoolean(prefs::kEnableContinuousSpellcheck); | |
149 } | |
150 | |
151 switch (command_id) { | |
152 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
153 case IDC_SPELLCHECK_MENU: | |
154 return true; | |
155 } | |
156 | |
157 return false; | |
158 } | |
159 | |
160 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { | |
161 DCHECK(IsCommandIdSupported(command_id)); | |
162 | |
163 // Check to see if one of the spell check language ids have been clicked. | |
164 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
165 DCHECK(profile); | |
166 | |
167 if (command_id == IDC_SPELLCHECK_MULTI_LINGUAL) { | |
lazyboy
2016/01/28 20:36:25
Can we fold this into the switch below on line 186
please use gerrit instead
2016/01/28 21:41:59
Done.
| |
168 StringListPrefMember dictionaries_pref; | |
169 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs()); | |
170 dictionaries_pref.SetValue(languages_); | |
lazyboy
2016/01/28 20:36:25
return?
please use gerrit instead
2016/01/28 21:41:59
using "break" inside of the switch.
| |
171 } | |
172 | |
173 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
174 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
175 size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | |
lazyboy
2016/01/28 20:36:25
language_index
please use gerrit instead
2016/01/28 21:41:59
Done.
| |
176 DCHECK_LT(language, languages_.size()); | |
177 StringListPrefMember dictionaries_pref; | |
178 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs()); | |
179 | |
180 dictionaries_pref.SetValue( | |
181 std::vector<std::string>(1, languages_[language])); | |
182 | |
183 return; | |
184 } | |
185 | |
186 switch (command_id) { | |
187 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
188 profile->GetPrefs()->SetBoolean( | |
189 prefs::kEnableContinuousSpellcheck, | |
190 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | |
191 break; | |
192 } | |
193 } | |
OLD | NEW |