| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/tab_contents/spellchecker_submenu_observer.h" | 5 #include "chrome/browser/tab_contents/spellchecker_submenu_observer.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/app/chrome_command_ids.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/prefs/pref_member.h" | 10 #include "chrome/browser/prefs/pref_member.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "ui/base/models/simple_menu_model.h" | 21 #include "ui/base/models/simple_menu_model.h" |
| 22 | 22 |
| 23 using content::BrowserThread; | 23 using content::BrowserThread; |
| 24 | 24 |
| 25 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( | 25 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( |
| 26 RenderViewContextMenuProxy* proxy, | 26 RenderViewContextMenuProxy* proxy, |
| 27 ui::SimpleMenuModel::Delegate* delegate, | 27 ui::SimpleMenuModel::Delegate* delegate, |
| 28 int group) | 28 int group) |
| 29 : proxy_(proxy), | 29 : proxy_(proxy), |
| 30 submenu_model_(delegate), | 30 submenu_model_(delegate), |
| 31 spellcheck_enabled_(false), | |
| 32 language_group_(group), | 31 language_group_(group), |
| 33 language_selected_(0) { | 32 language_selected_(0) { |
| 34 DCHECK(proxy_); | 33 DCHECK(proxy_); |
| 35 } | 34 } |
| 36 | 35 |
| 37 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { | 36 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { |
| 38 } | 37 } |
| 39 | 38 |
| 40 void SpellCheckerSubMenuObserver::InitMenu( | 39 void SpellCheckerSubMenuObserver::InitMenu( |
| 41 const content::ContextMenuParams& params) { | 40 const content::ContextMenuParams& params) { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 43 | 42 |
| 44 spellcheck_enabled_ = params.spellcheck_enabled; | |
| 45 | |
| 46 // Add available spell-checker languages to the sub menu. | 43 // Add available spell-checker languages to the sub menu. |
| 47 Profile* profile = proxy_->GetProfile(); | 44 Profile* profile = proxy_->GetProfile(); |
| 45 DCHECK(profile); |
| 48 language_selected_ = | 46 language_selected_ = |
| 49 SpellCheckHost::GetSpellCheckLanguages(profile, &languages_); | 47 SpellCheckHost::GetSpellCheckLanguages(profile, &languages_); |
| 50 DCHECK(languages_.size() < | 48 DCHECK(languages_.size() < |
| 51 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); | 49 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); |
| 52 const std::string app_locale = g_browser_process->GetApplicationLocale(); | 50 const std::string app_locale = g_browser_process->GetApplicationLocale(); |
| 53 for (size_t i = 0; i < languages_.size(); ++i) { | 51 for (size_t i = 0; i < languages_.size(); ++i) { |
| 54 string16 display_name( | 52 string16 display_name( |
| 55 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); | 53 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true)); |
| 56 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, | 54 submenu_model_.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
| 57 display_name, | 55 display_name, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 DCHECK(IsCommandIdSupported(command_id)); | 106 DCHECK(IsCommandIdSupported(command_id)); |
| 109 | 107 |
| 110 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 108 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 111 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 109 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 112 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 110 return language_selected_ == command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
| 113 } | 111 } |
| 114 | 112 |
| 115 // Check box for 'Check Spelling while typing'. | 113 // Check box for 'Check Spelling while typing'. |
| 116 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | 114 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
| 117 Profile* profile = proxy_->GetProfile(); | 115 Profile* profile = proxy_->GetProfile(); |
| 118 if (!profile || !profile->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)) | 116 DCHECK(profile); |
| 119 return false; | 117 return profile->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck); |
| 120 return spellcheck_enabled_; | |
| 121 } | 118 } |
| 122 | 119 |
| 123 return false; | 120 return false; |
| 124 } | 121 } |
| 125 | 122 |
| 126 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) { | 123 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) { |
| 127 DCHECK(IsCommandIdSupported(command_id)); | 124 DCHECK(IsCommandIdSupported(command_id)); |
| 128 | 125 |
| 129 Profile* profile = proxy_->GetProfile(); | 126 Profile* profile = proxy_->GetProfile(); |
| 130 if (!profile) | 127 DCHECK(profile); |
| 131 return false; | |
| 132 | |
| 133 const PrefService* pref = profile->GetPrefs(); | 128 const PrefService* pref = profile->GetPrefs(); |
| 134 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 129 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 135 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 130 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 136 return pref->GetBoolean(prefs::kEnableSpellCheck); | 131 return pref->GetBoolean(prefs::kEnableSpellCheck); |
| 137 } | 132 } |
| 138 | 133 |
| 139 switch (command_id) { | 134 switch (command_id) { |
| 140 case IDC_CHECK_SPELLING_WHILE_TYPING: | 135 case IDC_CHECK_SPELLING_WHILE_TYPING: |
| 141 return pref->GetBoolean(prefs::kEnableSpellCheck); | |
| 142 | |
| 143 case IDC_SPELLPANEL_TOGGLE: | 136 case IDC_SPELLPANEL_TOGGLE: |
| 144 case IDC_SPELLCHECK_MENU: | 137 case IDC_SPELLCHECK_MENU: |
| 145 return true; | 138 return true; |
| 146 } | 139 } |
| 147 | 140 |
| 148 return false; | 141 return false; |
| 149 } | 142 } |
| 150 | 143 |
| 151 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { | 144 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
| 152 DCHECK(IsCommandIdSupported(command_id)); | 145 DCHECK(IsCommandIdSupported(command_id)); |
| 153 | 146 |
| 154 // Check to see if one of the spell check language ids have been clicked. | 147 // Check to see if one of the spell check language ids have been clicked. |
| 148 Profile* profile = proxy_->GetProfile(); |
| 149 DCHECK(profile); |
| 155 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | 150 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| 156 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | 151 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| 157 Profile* profile = proxy_->GetProfile(); | |
| 158 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | 152 const size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
| 159 if (profile && language < languages_.size()) { | 153 if (profile && language < languages_.size()) { |
| 160 StringPrefMember dictionary_language; | 154 StringPrefMember dictionary_language; |
| 161 dictionary_language.Init(prefs::kSpellCheckDictionary, | 155 dictionary_language.Init(prefs::kSpellCheckDictionary, |
| 162 profile->GetPrefs(), | 156 profile->GetPrefs(), |
| 163 NULL); | 157 NULL); |
| 164 dictionary_language.SetValue(languages_[language]); | 158 dictionary_language.SetValue(languages_[language]); |
| 165 } | 159 } |
| 166 return; | 160 return; |
| 167 } | 161 } |
| 168 | 162 |
| 169 content::RenderViewHost* rvh = proxy_->GetRenderViewHost(); | 163 content::RenderViewHost* rvh = proxy_->GetRenderViewHost(); |
| 170 switch (command_id) { | 164 switch (command_id) { |
| 171 case IDC_CHECK_SPELLING_WHILE_TYPING: | 165 case IDC_CHECK_SPELLING_WHILE_TYPING: |
| 172 rvh->Send(new SpellCheckMsg_ToggleSpellCheck(rvh->GetRoutingID())); | 166 profile->GetPrefs()->SetBoolean( |
| 167 prefs::kEnableSpellCheck, |
| 168 !profile->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck)); |
| 169 if (rvh) |
| 170 rvh->Send(new SpellCheckMsg_ToggleSpellCheck(rvh->GetRoutingID())); |
| 173 break; | 171 break; |
| 174 } | 172 } |
| 175 } | 173 } |
| OLD | NEW |