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.
5 #include "chrome/browser/renderer_context_menu/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/renderer_context_menu/render_view_context_menu.h"
11 #include "chrome/browser/renderer_context_menu/spelling_bubble_model.h"
12 #include "chrome/browser/spellchecker/spellcheck_platform.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/common/spellcheck_messages.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "components/user_prefs/user_prefs.h"
18 #include "content/public/browser/render_view_host.h"
19 #include "content/public/browser/render_widget_host_view.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/models/simple_menu_model.h"
23 using content::BrowserThread
;
27 PrefService
* GetPrefs(content::BrowserContext
* context
) {
28 return user_prefs::UserPrefs::Get(context
);
33 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver(
34 RenderViewContextMenuProxy
* proxy
,
35 ui::SimpleMenuModel::Delegate
* delegate
,
38 submenu_model_(delegate
) {
42 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() {
45 void SpellCheckerSubMenuObserver::InitMenu(
46 const content::ContextMenuParams
& params
) {
47 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
49 // Add an item that toggles the spelling panel.
50 submenu_model_
.AddCheckItem(
51 IDC_SPELLPANEL_TOGGLE
,
52 l10n_util::GetStringUTF16(
53 spellcheck_platform::SpellingPanelVisible() ?
54 IDS_CONTENT_CONTEXT_HIDE_SPELLING_PANEL
:
55 IDS_CONTENT_CONTEXT_SHOW_SPELLING_PANEL
));
56 submenu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
58 // Add a 'Check Spelling While Typing' item in the sub menu.
59 submenu_model_
.AddCheckItem(
60 IDC_CHECK_SPELLING_WHILE_TYPING
,
61 l10n_util::GetStringUTF16(
62 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING
));
66 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU
),
70 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id
) {
72 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS
:
73 // Return false so RenderViewContextMenu can handle this item because it
74 // is hard for this class to handle it.
77 case IDC_CHECK_SPELLING_WHILE_TYPING
:
78 case IDC_SPELLPANEL_TOGGLE
:
79 case IDC_SPELLCHECK_MENU
:
80 case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
:
87 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id
) {
88 DCHECK(IsCommandIdSupported(command_id
));
90 // Check box for 'Check Spelling while typing'.
91 if (command_id
== IDC_CHECK_SPELLING_WHILE_TYPING
) {
92 content::BrowserContext
* context
= proxy_
->GetBrowserContext();
94 return GetPrefs(context
)->GetBoolean(prefs::kEnableContinuousSpellcheck
);
100 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id
) {
101 DCHECK(IsCommandIdSupported(command_id
));
103 switch (command_id
) {
104 case IDC_CHECK_SPELLING_WHILE_TYPING
:
105 case IDC_SPELLPANEL_TOGGLE
:
106 case IDC_SPELLCHECK_MENU
:
107 case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
:
114 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id
) {
115 DCHECK(IsCommandIdSupported(command_id
));
117 content::RenderViewHost
* rvh
= proxy_
->GetRenderViewHost();
118 content::BrowserContext
* context
= proxy_
->GetBrowserContext();
120 switch (command_id
) {
121 case IDC_CHECK_SPELLING_WHILE_TYPING
:
122 GetPrefs(context
)->SetBoolean(
123 prefs::kEnableContinuousSpellcheck
,
124 !GetPrefs(context
)->GetBoolean(prefs::kEnableContinuousSpellcheck
));
127 case IDC_SPELLPANEL_TOGGLE
:
128 rvh
->Send(new SpellCheckMsg_ToggleSpellPanel(
129 rvh
->GetRoutingID(), spellcheck_platform::SpellingPanelVisible()));