OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/tab_contents/spellchecker_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/prefs/pref_member.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/spellchecker/spellcheck_host.h" |
| 13 #include "chrome/browser/spellchecker/spellchecker_platform_engine.h" |
| 14 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 15 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/common/spellcheck_messages.h" |
| 17 #include "content/browser/renderer_host/render_view_host.h" |
| 18 #include "grit/generated_resources.h" |
| 19 #include "ui/base/l10n/l10n_util.h" |
| 20 #include "ui/base/models/simple_menu_model.h" |
| 21 |
| 22 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( |
| 23 RenderViewContextMenuProxy* proxy, |
| 24 ui::SimpleMenuModel::Delegate* delegate, |
| 25 int group) |
| 26 : proxy_(proxy), |
| 27 submenu_model_(delegate), |
| 28 spellcheck_enabled_(false), |
| 29 language_group_(group), |
| 30 language_selected_(0) { |
| 31 DCHECK(proxy_); |
| 32 } |
| 33 |
| 34 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { |
| 35 } |
| 36 |
| 37 void SpellCheckerSubMenuObserver::InitMenu(const ContextMenuParams& params) { |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 39 |
| 40 spellcheck_enabled_ = params.spellcheck_enabled; |
| 41 |
| 42 // Add available spell-checker languages to the sub menu. |
| 43 Profile* profile = proxy_->GetProfile(); |
| 44 language_selected_ = |
| 45 SpellCheckHost::GetSpellCheckLanguages(profile, &languages_); |
| 46 DCHECK(languages_.size() < |
| 47 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); |
| 48 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
| 49 for (size_t i = 0; i < languages_.size(); ++i) { |
| 50 string16 display_name( |
| 51 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); |
| 52 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
| 53 display_name, |
| 54 language_group_); |
| 55 } |
| 56 |
| 57 // Add an item that opens the 'fonts and languages options' page. |
| 58 submenu_model_.AddSeparator(); |
| 59 submenu_model_.AddItemWithStringId( |
| 60 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, |
| 61 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); |
| 62 |
| 63 // Add a 'Check the spelling of this field' item in the sub menu. |
| 64 submenu_model_.AddCheckItem( |
| 65 IDC_CHECK_SPELLING_OF_THIS_FIELD, |
| 66 l10n_util::GetStringUTF16( |
| 67 IDS_CONTENT_CONTEXT_CHECK_SPELLING_OF_THIS_FIELD)); |
| 68 |
| 69 // Add an item that shows the spelling panel if the platform spellchecker |
| 70 // supports it. |
| 71 if (SpellCheckerPlatform::SpellCheckerAvailable() && |
| 72 SpellCheckerPlatform::SpellCheckerProvidesPanel()) { |
| 73 submenu_model_.AddCheckItem( |
| 74 IDC_SPELLPANEL_TOGGLE, |
| 75 l10n_util::GetStringUTF16( |
| 76 SpellCheckerPlatform::SpellingPanelVisible() ? |
| 77 IDS_CONTENT_CONTEXT_HIDE_SPELLING_PANEL : |
| 78 IDS_CONTENT_CONTEXT_SHOW_SPELLING_PANEL)); |
| 79 } |
| 80 |
| 81 proxy_->AddSubMenu( |
| 82 IDC_SPELLCHECK_MENU, |
| 83 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), |
| 84 &submenu_model_); |
| 85 } |
| 86 |
| 87 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id) { |
| 88 // Allow Spell Check language items on sub menu for text area context menu. |
| 89 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 90 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 91 return true; |
| 92 } |
| 93 |
| 94 switch (command_id) { |
| 95 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS: |
| 96 // Return false so RenderViewContextMenu can handle this item because it |
| 97 // is hard for this class to handle it. |
| 98 return false; |
| 99 |
| 100 case IDC_CHECK_SPELLING_OF_THIS_FIELD: |
| 101 case IDC_SPELLPANEL_TOGGLE: |
| 102 case IDC_SPELLCHECK_MENU: |
| 103 return true; |
| 104 } |
| 105 |
| 106 return false; |
| 107 } |
| 108 |
| 109 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { |
| 110 DCHECK(IsCommandIdSupported(command_id)); |
| 111 |
| 112 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 113 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 114 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
| 115 } |
| 116 |
| 117 // Check box for 'Check the Spelling of this field'. |
| 118 if (command_id == IDC_CHECK_SPELLING_OF_THIS_FIELD) { |
| 119 Profile* profile = proxy_->GetProfile(); |
| 120 if (!profile || !profile->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)) |
| 121 return false; |
| 122 return spellcheck_enabled_; |
| 123 } |
| 124 |
| 125 return false; |
| 126 } |
| 127 |
| 128 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) { |
| 129 DCHECK(IsCommandIdSupported(command_id)); |
| 130 |
| 131 Profile* profile = proxy_->GetProfile(); |
| 132 if (!profile) |
| 133 return false; |
| 134 |
| 135 const PrefService* pref = profile->GetPrefs(); |
| 136 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 137 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 138 return pref->GetBoolean(prefs::kEnableSpellCheck); |
| 139 } |
| 140 |
| 141 switch (command_id) { |
| 142 case IDC_CHECK_SPELLING_OF_THIS_FIELD: |
| 143 return pref->GetBoolean(prefs::kEnableSpellCheck); |
| 144 |
| 145 case IDC_SPELLPANEL_TOGGLE: |
| 146 return true; |
| 147 |
| 148 case IDC_SPELLCHECK_MENU: |
| 149 return true; |
| 150 } |
| 151 |
| 152 return false; |
| 153 } |
| 154 |
| 155 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
| 156 DCHECK(IsCommandIdSupported(command_id)); |
| 157 |
| 158 // Check to see if one of the spell check language ids have been clicked. |
| 159 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 160 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 161 Profile* profile = proxy_->GetProfile(); |
| 162 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
| 163 if (profile && language < languages_.size()) { |
| 164 StringPrefMember dictionary_language; |
| 165 dictionary_language.Init(prefs::kSpellCheckDictionary, |
| 166 profile->GetPrefs(), |
| 167 NULL); |
| 168 dictionary_language.SetValue(languages_[language]); |
| 169 } |
| 170 return; |
| 171 } |
| 172 |
| 173 RenderViewHost* rvh = proxy_->GetRenderViewHost(); |
| 174 switch (command_id) { |
| 175 case IDC_CHECK_SPELLING_OF_THIS_FIELD: |
| 176 rvh->Send(new SpellCheckMsg_ToggleSpellCheck(rvh->routing_id())); |
| 177 break; |
| 178 |
| 179 case IDC_SPELLPANEL_TOGGLE: |
| 180 rvh->Send(new SpellCheckMsg_ToggleSpellPanel( |
| 181 rvh->routing_id(), SpellCheckerPlatform::SpellingPanelVisible())); |
| 182 break; |
| 183 } |
| 184 } |
OLD | NEW |