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 } |
| 38 |
| 39 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { |
| 40 } |
| 41 |
| 42 void SpellCheckerSubMenuObserver::InitMenu( |
| 43 const content::ContextMenuParams& params) { |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 45 |
| 46 // Add available spell-checker languages to the sub menu. |
| 47 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
| 48 DCHECK(browser_context); |
| 49 num_selected_languages_ = |
| 50 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); |
| 51 DCHECK(languages_.size() < |
| 52 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); |
| 53 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
| 54 |
| 55 if (languages_.size() > 1) { |
| 56 submenu_model_.AddRadioItemWithStringId( |
| 57 IDC_SPELLCHECK_MULTI_LINGUAL, |
| 58 IDS_CONTENT_CONTEXT_SPELLCHECK_MULTI_LINGUAL, language_group_); |
| 59 } |
| 60 |
| 61 for (size_t i = 0; i < languages_.size(); ++i) { |
| 62 submenu_model_.AddRadioItem( |
| 63 IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
| 64 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true), |
| 65 language_group_); |
| 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( |
| 71 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 SpellCheckerSubMenuObserver::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 SpellCheckerSubMenuObserver::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 return num_selected_languages_ > |
| 132 static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST); |
| 133 } |
| 134 |
| 135 // Check box for 'Check Spelling while typing'. |
| 136 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
| 137 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| 138 DCHECK(profile); |
| 139 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); |
| 140 } |
| 141 |
| 142 return false; |
| 143 } |
| 144 |
| 145 bool SpellCheckerSubMenuObserver::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 SpellCheckerSubMenuObserver::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_MULTI_LINGUAL) { |
| 174 StringListPrefMember dictionaries_pref; |
| 175 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs()); |
| 176 dictionaries_pref.SetValue(languages_); |
| 177 } |
| 178 |
| 179 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 180 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 181 size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
| 182 DCHECK_LT(language, languages_.size()); |
| 183 StringListPrefMember dictionaries_pref; |
| 184 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs()); |
| 185 |
| 186 dictionaries_pref.SetValue( |
| 187 std::vector<std::string>(1, languages_[language])); |
| 188 |
| 189 return; |
| 190 } |
| 191 |
| 192 switch (command_id) { |
| 193 case IDC_CHECK_SPELLING_WHILE_TYPING: |
| 194 profile->GetPrefs()->SetBoolean( |
| 195 prefs::kEnableContinuousSpellcheck, |
| 196 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); |
| 197 break; |
| 198 } |
| 199 } |
OLD | NEW |