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
3 // found in the LICENSE file.
5 #include "chrome/browser/tab_contents/spellchecker_submenu_observer.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/spellchecker/spellcheck_platform_mac.h"
11 #include "chrome/browser/tab_contents/render_view_context_menu.h"
12 #include "chrome/browser/tab_contents/spelling_bubble_model.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/common/spellcheck_messages.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/render_widget_host_view.h"
18 #include "grit/generated_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/models/simple_menu_model.h"
22 using content::BrowserThread;
24 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver(
25 RenderViewContextMenuProxy* proxy,
26 ui::SimpleMenuModel::Delegate* delegate,
29 submenu_model_(delegate) {
33 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() {
36 void SpellCheckerSubMenuObserver::InitMenu(
37 const content::ContextMenuParams& params) {
38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
40 // Add an item that toggles the spelling panel.
41 submenu_model_.AddCheckItem(
42 IDC_SPELLPANEL_TOGGLE,
43 l10n_util::GetStringUTF16(
44 spellcheck_mac::SpellingPanelVisible() ?
45 IDS_CONTENT_CONTEXT_HIDE_SPELLING_PANEL :
46 IDS_CONTENT_CONTEXT_SHOW_SPELLING_PANEL));
47 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
49 // Add a 'Check Spelling While Typing' item in the sub menu.
50 submenu_model_.AddCheckItem(
51 IDC_CHECK_SPELLING_WHILE_TYPING,
52 l10n_util::GetStringUTF16(
53 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING));
57 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU),
61 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id) {
63 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS:
64 // Return false so RenderViewContextMenu can handle this item because it
65 // is hard for this class to handle it.
68 case IDC_CHECK_SPELLING_WHILE_TYPING:
69 case IDC_SPELLPANEL_TOGGLE:
70 case IDC_SPELLCHECK_MENU:
71 case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE:
78 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) {
79 DCHECK(IsCommandIdSupported(command_id));
81 // Check box for 'Check Spelling while typing'.
82 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) {
83 Profile* profile = proxy_->GetProfile();
85 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck);
91 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) {
92 DCHECK(IsCommandIdSupported(command_id));
95 case IDC_CHECK_SPELLING_WHILE_TYPING:
96 case IDC_SPELLPANEL_TOGGLE:
97 case IDC_SPELLCHECK_MENU:
98 case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE:
105 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) {
106 DCHECK(IsCommandIdSupported(command_id));
108 content::RenderViewHost* rvh = proxy_->GetRenderViewHost();
109 Profile* profile = proxy_->GetProfile();
111 switch (command_id) {
112 case IDC_CHECK_SPELLING_WHILE_TYPING:
113 profile->GetPrefs()->SetBoolean(
114 prefs::kEnableContinuousSpellcheck,
115 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck));
118 case IDC_SPELLPANEL_TOGGLE:
119 rvh->Send(new SpellCheckMsg_ToggleSpellPanel(
120 rvh->GetRoutingID(), spellcheck_mac::SpellingPanelVisible()));