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 // |
| 41 spellcheck_enabled_ = params.spellcheck_enabled; |
| 42 |
| 43 // Add Spell Check languages to sub menu. |
| 44 Profile* profile = proxy_->GetProfile(); |
| 45 language_selected_ = |
| 46 SpellCheckHost::GetSpellCheckLanguages(profile, &languages_); |
| 47 DCHECK(languages_.size() < |
| 48 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); |
| 49 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
| 50 for (size_t i = 0; i < languages_.size(); ++i) { |
| 51 string16 display_name( |
| 52 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); |
| 53 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
| 54 display_name, |
| 55 language_group_); |
| 56 } |
| 57 |
| 58 // Add item in the sub menu to pop up the fonts and languages options menu. |
| 59 submenu_model_.AddSeparator(); |
| 60 submenu_model_.AddItemWithStringId( |
| 61 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, |
| 62 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); |
| 63 |
| 64 // Add 'Check the spelling of this field' item in the sub menu. |
| 65 submenu_model_.AddCheckItem( |
| 66 IDC_CHECK_SPELLING_OF_THIS_FIELD, |
| 67 l10n_util::GetStringUTF16( |
| 68 IDS_CONTENT_CONTEXT_CHECK_SPELLING_OF_THIS_FIELD)); |
| 69 |
| 70 // Add option for showing the spelling panel if the platform spellchecker |
| 71 // supports it. |
| 72 if (SpellCheckerPlatform::SpellCheckerAvailable() && |
| 73 SpellCheckerPlatform::SpellCheckerProvidesPanel()) { |
| 74 submenu_model_.AddCheckItem( |
| 75 IDC_SPELLPANEL_TOGGLE, |
| 76 l10n_util::GetStringUTF16( |
| 77 SpellCheckerPlatform::SpellingPanelVisible() ? |
| 78 IDS_CONTENT_CONTEXT_HIDE_SPELLING_PANEL : |
| 79 IDS_CONTENT_CONTEXT_SHOW_SPELLING_PANEL)); |
| 80 } |
| 81 |
| 82 proxy_->AddSubMenu( |
| 83 IDC_SPELLCHECK_MENU, |
| 84 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), |
| 85 &submenu_model_); |
| 86 } |
| 87 |
| 88 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id) { |
| 89 // Allow Spell Check language items on sub menu for text area context menu. |
| 90 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 91 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 92 return true; |
| 93 } |
| 94 |
| 95 switch (command_id) { |
| 96 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS: |
| 97 // Return false so RenderViewContextMenu can handle this item because it |
| 98 // is hard for this class to handle it. |
| 99 return false; |
| 100 |
| 101 case IDC_CHECK_SPELLING_OF_THIS_FIELD: |
| 102 case IDC_SPELLPANEL_TOGGLE: |
| 103 case IDC_SPELLCHECK_MENU: |
| 104 return true; |
| 105 } |
| 106 |
| 107 return false; |
| 108 } |
| 109 |
| 110 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { |
| 111 DCHECK(IsCommandIdSupported(command_id)); |
| 112 |
| 113 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 114 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 115 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
| 116 } |
| 117 |
| 118 // Check box for 'Check the Spelling of this field'. |
| 119 if (command_id == IDC_CHECK_SPELLING_OF_THIS_FIELD) { |
| 120 Profile* profile = proxy_->GetProfile(); |
| 121 if (!profile || !profile->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)) |
| 122 return false; |
| 123 return spellcheck_enabled_; |
| 124 } |
| 125 |
| 126 return false; |
| 127 } |
| 128 |
| 129 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) { |
| 130 DCHECK(IsCommandIdSupported(command_id)); |
| 131 |
| 132 Profile* profile = proxy_->GetProfile(); |
| 133 if (!profile) |
| 134 return false; |
| 135 |
| 136 const PrefService* pref = profile->GetPrefs(); |
| 137 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 138 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 139 return pref->GetBoolean(prefs::kEnableSpellCheck); |
| 140 } |
| 141 |
| 142 switch (command_id) { |
| 143 case IDC_CHECK_SPELLING_OF_THIS_FIELD: |
| 144 return pref->GetBoolean(prefs::kEnableSpellCheck); |
| 145 |
| 146 case IDC_SPELLPANEL_TOGGLE: |
| 147 return true; |
| 148 |
| 149 case IDC_SPELLCHECK_MENU: |
| 150 return true; |
| 151 } |
| 152 |
| 153 return false; |
| 154 } |
| 155 |
| 156 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
| 157 DCHECK(IsCommandIdSupported(command_id)); |
| 158 |
| 159 // Check to see if one of the spell check language ids have been clicked. |
| 160 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 161 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 162 Profile* profile = proxy_->GetProfile(); |
| 163 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
| 164 if (profile && language < languages_.size()) { |
| 165 StringPrefMember dictionary_language; |
| 166 dictionary_language.Init(prefs::kSpellCheckDictionary, |
| 167 profile->GetPrefs(), |
| 168 NULL); |
| 169 dictionary_language.SetValue(languages_[language]); |
| 170 } |
| 171 return; |
| 172 } |
| 173 |
| 174 RenderViewHost* rvh = proxy_->GetRenderViewHost(); |
| 175 switch (command_id) { |
| 176 case IDC_CHECK_SPELLING_OF_THIS_FIELD: |
| 177 rvh->Send(new SpellCheckMsg_ToggleSpellCheck(rvh->routing_id())); |
| 178 break; |
| 179 |
| 180 case IDC_SPELLPANEL_TOGGLE: |
| 181 rvh->Send(new SpellCheckMsg_ToggleSpellPanel( |
| 182 rvh->routing_id(), SpellCheckerPlatform::SpellingPanelVisible())); |
| 183 break; |
| 184 } |
| 185 } |
OLD | NEW |