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