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 "base/logging.h" | |
8 #include "chrome/app/chrome_command_ids.h" | |
9 #include "chrome/browser/browser_process.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/spellchecker/spellcheck_service.h" | |
12 #include "chrome/common/pref_names.h" | |
13 #include "chrome/grit/generated_resources.h" | |
14 #include "components/prefs/pref_member.h" | |
15 #include "components/prefs/pref_service.h" | |
16 #include "components/renderer_context_menu/render_view_context_menu_proxy.h" | |
17 #include "content/public/browser/browser_context.h" | |
18 #include "content/public/browser/browser_thread.h" | |
19 #include "ui/base/l10n/l10n_util.h" | |
20 #include "ui/base/models/menu_separator_types.h" | |
21 | |
22 using content::BrowserThread; | |
23 | |
24 SpellingOptionsSubMenuObserver::SpellingOptionsSubMenuObserver( | |
25 RenderViewContextMenuProxy* proxy, | |
26 ui::SimpleMenuModel::Delegate* delegate, | |
27 int group_id) | |
28 : proxy_(proxy), | |
29 submenu_model_(delegate), | |
30 language_group_id_(group_id), | |
31 num_selected_dictionaries_(0) { | |
32 DCHECK(proxy_); | |
33 } | |
34 | |
35 SpellingOptionsSubMenuObserver::~SpellingOptionsSubMenuObserver() {} | |
36 | |
37 void SpellingOptionsSubMenuObserver::InitMenu( | |
38 const content::ContextMenuParams& params) { | |
39 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
40 | |
41 // Add available spell-checker languages to the sub menu. | |
42 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | |
43 DCHECK(browser_context); | |
44 SpellcheckService::GetDictionaries(browser_context, &dictionaries_); | |
please use gerrit instead
2016/02/16 22:56:54
Using the new GetDictionaries() call.
| |
45 DCHECK(dictionaries_.size() < | |
46 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); | |
47 const std::string app_locale = g_browser_process->GetApplicationLocale(); | |
48 | |
49 if (dictionaries_.size() > 1) { | |
50 submenu_model_.AddRadioItemWithStringId( | |
51 IDC_SPELLCHECK_MULTI_LINGUAL, | |
52 IDS_CONTENT_CONTEXT_SPELLCHECK_MULTI_LINGUAL, language_group_id_); | |
53 } | |
54 | |
55 const size_t kMaxLanguages = static_cast<size_t>( | |
56 IDC_SPELLCHECK_LANGUAGES_FIRST - IDC_SPELLCHECK_LANGUAGES_LAST); | |
57 for (size_t i = 0; i < dictionaries_.size() && i < kMaxLanguages; ++i) { | |
58 submenu_model_.AddRadioItem( | |
59 IDC_SPELLCHECK_LANGUAGES_FIRST + i, | |
60 l10n_util::GetDisplayNameForLocale(dictionaries_[i].language, | |
61 app_locale, true), | |
62 language_group_id_); | |
63 if (dictionaries_[i].used_for_spellcheck) | |
64 ++num_selected_dictionaries_; | |
65 } | |
66 | |
67 // Add an item that opens the 'Settings - Languages' page. This item is | |
68 // handled in RenderViewContextMenu. | |
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_dictionaries_ > 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'. This item is | |
82 // handled in SpellingMenuObserver. | |
83 submenu_model_.AddCheckItem( | |
84 IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, | |
85 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); | |
86 | |
87 proxy_->AddSubMenu( | |
88 IDC_SPELLCHECK_MENU, | |
89 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), | |
90 &submenu_model_); | |
91 } | |
92 | |
93 bool SpellingOptionsSubMenuObserver::IsCommandIdSupported(int command_id) { | |
94 // Allow Spell Check language items on sub menu for text area context menu. | |
95 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
96 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
97 DCHECK_GT(IDC_SPELLCHECK_LANGUAGES_FIRST + dictionaries_.size(), | |
98 static_cast<size_t>(command_id)); | |
99 return true; | |
100 } | |
101 | |
102 switch (command_id) { | |
103 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
104 case IDC_SPELLCHECK_MENU: | |
105 case IDC_SPELLCHECK_MULTI_LINGUAL: | |
106 return true; | |
107 } | |
108 | |
109 return false; | |
110 } | |
111 | |
112 bool SpellingOptionsSubMenuObserver::IsCommandIdChecked(int command_id) { | |
113 DCHECK(IsCommandIdSupported(command_id)); | |
114 | |
115 if (command_id == IDC_SPELLCHECK_MULTI_LINGUAL) | |
116 return num_selected_dictionaries_ == dictionaries_.size(); | |
117 | |
118 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
119 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
120 if (num_selected_dictionaries_ == dictionaries_.size()) | |
121 return dictionaries_.size() == 1; | |
122 | |
123 size_t dictionary_index = | |
124 static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST); | |
125 return dictionaries_[dictionary_index].used_for_spellcheck; | |
126 } | |
127 | |
128 // Check box for 'Check Spelling while typing'. | |
129 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | |
130 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
131 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | |
132 } | |
133 | |
134 return false; | |
135 } | |
136 | |
137 bool SpellingOptionsSubMenuObserver::IsCommandIdEnabled(int command_id) { | |
138 DCHECK(IsCommandIdSupported(command_id)); | |
139 | |
140 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
141 DCHECK(profile); | |
142 const PrefService* pref = profile->GetPrefs(); | |
143 if ((command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
144 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) || | |
145 command_id == IDC_SPELLCHECK_MULTI_LINGUAL) { | |
146 return pref->GetBoolean(prefs::kEnableContinuousSpellcheck); | |
147 } | |
148 | |
149 switch (command_id) { | |
150 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
151 case IDC_SPELLCHECK_MENU: | |
152 return true; | |
153 } | |
154 | |
155 return false; | |
156 } | |
157 | |
158 void SpellingOptionsSubMenuObserver::ExecuteCommand(int command_id) { | |
159 DCHECK(IsCommandIdSupported(command_id)); | |
160 | |
161 // Check to see if one of the spell check language ids have been clicked. | |
162 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
163 DCHECK(profile); | |
164 | |
165 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
166 static_cast<size_t>(command_id) < | |
167 IDC_SPELLCHECK_LANGUAGES_FIRST + dictionaries_.size()) { | |
168 size_t dictionary_index = | |
169 static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST); | |
170 StringListPrefMember dictionaries_pref; | |
171 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs()); | |
172 dictionaries_pref.SetValue({dictionaries_[dictionary_index].language}); | |
173 return; | |
174 } | |
175 | |
176 switch (command_id) { | |
177 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
178 profile->GetPrefs()->SetBoolean( | |
179 prefs::kEnableContinuousSpellcheck, | |
180 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | |
181 break; | |
182 | |
183 case IDC_SPELLCHECK_MULTI_LINGUAL: | |
184 StringListPrefMember dictionaries_pref; | |
185 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, | |
186 profile->GetPrefs()); | |
187 std::vector<std::string> all_languages; | |
188 for (const auto& dictionary : dictionaries_) | |
189 all_languages.push_back(dictionary.language); | |
190 dictionaries_pref.SetValue(all_languages); | |
191 break; | |
192 } | |
193 } | |
OLD | NEW |