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/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" | |
lazyboy
2016/02/01 22:58:12
Do you really need these? It seems like we have to
please use gerrit instead
2016/02/02 00:28:51
Done.
| |
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 size_t kMaxLanguages = static_cast<size_t>( | |
61 IDC_SPELLCHECK_LANGUAGES_FIRST - IDC_SPELLCHECK_LANGUAGES_LAST); | |
62 for (size_t i = 0; i < languages_.size() && i < kMaxLanguages; ++i) { | |
63 submenu_model_.AddRadioItem( | |
64 IDC_SPELLCHECK_LANGUAGES_FIRST + i, | |
65 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true), | |
66 language_group_id_); | |
67 } | |
68 | |
69 // Add an item that opens the 'fonts and languages options' page. | |
70 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | |
71 submenu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, | |
72 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); | |
73 | |
74 if (num_selected_languages_ > 0) { | |
75 // Add a 'Check spelling while typing' item in the sub menu. | |
76 submenu_model_.AddCheckItem( | |
77 IDC_CHECK_SPELLING_WHILE_TYPING, | |
78 l10n_util::GetStringUTF16( | |
79 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); | |
80 } | |
81 | |
82 // Add a check item "Ask Google for spelling suggestions" item. (This class | |
83 // does not handle this item because the SpellingMenuObserver class handles it | |
84 // on behalf of this class.) | |
85 submenu_model_.AddCheckItem( | |
86 IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, | |
87 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); | |
88 | |
89 proxy_->AddSubMenu( | |
90 IDC_SPELLCHECK_MENU, | |
91 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), | |
92 &submenu_model_); | |
93 } | |
94 | |
95 bool SpellingOptionsSubMenuObserver::IsCommandIdSupported(int command_id) { | |
96 // Allow Spell Check language items on sub menu for text area context menu. | |
97 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
98 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
99 return true; | |
100 } | |
101 | |
102 switch (command_id) { | |
103 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS: | |
104 // Return false so RenderViewContextMenu can handle this item because it | |
105 // is hard for this class to handle it. | |
106 return false; | |
107 | |
108 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
109 case IDC_SPELLCHECK_MENU: | |
110 case IDC_SPELLCHECK_MULTI_LINGUAL: | |
111 return true; | |
112 } | |
113 | |
114 return false; | |
115 } | |
116 | |
117 bool SpellingOptionsSubMenuObserver::IsCommandIdChecked(int command_id) { | |
118 DCHECK(IsCommandIdSupported(command_id)); | |
119 | |
120 if (command_id == IDC_SPELLCHECK_MULTI_LINGUAL) | |
121 return num_selected_languages_ == languages_.size(); | |
122 | |
123 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
124 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
125 if (languages_.size() == 1 && num_selected_languages_ == 1) | |
126 return true; | |
127 | |
128 if (languages_.size() == num_selected_languages_) | |
129 return false; | |
130 | |
131 size_t language_index = | |
132 static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST); | |
133 // The first |num_selected_languages_| are used for spellchecking. | |
134 return num_selected_languages_ > language_index; | |
135 } | |
136 | |
137 // Check box for 'Check Spelling while typing'. | |
138 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | |
139 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
140 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | |
141 } | |
142 | |
143 return false; | |
144 } | |
145 | |
146 bool SpellingOptionsSubMenuObserver::IsCommandIdEnabled(int command_id) { | |
147 DCHECK(IsCommandIdSupported(command_id)); | |
148 | |
149 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
150 DCHECK(profile); | |
151 const PrefService* pref = profile->GetPrefs(); | |
152 if ((command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
153 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) || | |
154 command_id == IDC_SPELLCHECK_MULTI_LINGUAL) { | |
155 return pref->GetBoolean(prefs::kEnableContinuousSpellcheck); | |
156 } | |
157 | |
158 switch (command_id) { | |
159 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
160 case IDC_SPELLCHECK_MENU: | |
161 return true; | |
162 } | |
163 | |
164 return false; | |
165 } | |
166 | |
167 void SpellingOptionsSubMenuObserver::ExecuteCommand(int command_id) { | |
168 DCHECK(IsCommandIdSupported(command_id)); | |
169 | |
170 // Check to see if one of the spell check language ids have been clicked. | |
171 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
172 DCHECK(profile); | |
173 | |
174 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
175 static_cast<size_t>(command_id) < | |
176 IDC_SPELLCHECK_LANGUAGES_FIRST + languages_.size()) { | |
177 size_t language_index = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | |
178 StringListPrefMember dictionaries_pref; | |
179 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs()); | |
180 dictionaries_pref.SetValue({languages_[language_index]}); | |
181 return; | |
182 } | |
183 | |
184 switch (command_id) { | |
185 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
186 profile->GetPrefs()->SetBoolean( | |
187 prefs::kEnableContinuousSpellcheck, | |
188 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | |
189 break; | |
190 | |
191 case IDC_SPELLCHECK_MULTI_LINGUAL: | |
192 StringListPrefMember dictionaries_pref; | |
193 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, | |
194 profile->GetPrefs()); | |
195 dictionaries_pref.SetValue(languages_); | |
196 break; | |
197 } | |
198 } | |
OLD | NEW |