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
));
64 // Add a check item "Ask Google for spelling suggestions" item. (This class
65 // does not handle this item because the SpellingMenuObserver class handles it
66 // on behalf of this class.)
67 submenu_model_
.AddCheckItem(
68 IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
,
69 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE
));
73 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU
),
77 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id
) {
79 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS
:
80 // Return false so RenderViewContextMenu can handle this item because it
81 // is hard for this class to handle it.
84 case IDC_CHECK_SPELLING_WHILE_TYPING
:
85 case IDC_SPELLPANEL_TOGGLE
:
86 case IDC_SPELLCHECK_MENU
:
87 case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
:
94 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id
) {
95 DCHECK(IsCommandIdSupported(command_id
));
97 // Check box for 'Check Spelling while typing'.
98 if (command_id
== IDC_CHECK_SPELLING_WHILE_TYPING
) {
99 content::BrowserContext
* context
= proxy_
->GetBrowserContext();
101 return GetPrefs(context
)->GetBoolean(prefs::kEnableContinuousSpellcheck
);
107 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id
) {
108 DCHECK(IsCommandIdSupported(command_id
));
110 switch (command_id
) {
111 case IDC_CHECK_SPELLING_WHILE_TYPING
:
112 case IDC_SPELLPANEL_TOGGLE
:
113 case IDC_SPELLCHECK_MENU
:
114 case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
:
121 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id
) {
122 DCHECK(IsCommandIdSupported(command_id
));
124 content::RenderViewHost
* rvh
= proxy_
->GetRenderViewHost();
125 content::BrowserContext
* context
= proxy_
->GetBrowserContext();
127 switch (command_id
) {
128 case IDC_CHECK_SPELLING_WHILE_TYPING
:
129 GetPrefs(context
)->SetBoolean(
130 prefs::kEnableContinuousSpellcheck
,
131 !GetPrefs(context
)->GetBoolean(prefs::kEnableContinuousSpellcheck
));
134 case IDC_SPELLPANEL_TOGGLE
:
135 rvh
->Send(new SpellCheckMsg_ToggleSpellPanel(
136 rvh
->GetRoutingID(), spellcheck_platform::SpellingPanelVisible()));