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/command_line.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_member.h"
10 #include "base/prefs/pref_service.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
14 #include "chrome/browser/spellchecker/spellcheck_service.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/spellcheck_messages.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/render_widget_host_view.h"
21 #include "extensions/browser/view_type_utils.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/models/simple_menu_model.h"
25 using content::BrowserThread
;
27 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver(
28 RenderViewContextMenuProxy
* proxy
,
29 ui::SimpleMenuModel::Delegate
* delegate
,
32 submenu_model_(delegate
),
33 language_group_(group
),
34 language_selected_(0) {
38 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() {
41 void SpellCheckerSubMenuObserver::InitMenu(
42 const content::ContextMenuParams
& params
) {
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
45 // Add available spell-checker languages to the sub menu.
46 content::BrowserContext
* browser_context
= proxy_
->GetBrowserContext();
47 DCHECK(browser_context
);
49 SpellcheckService::GetSpellCheckLanguages(browser_context
, &languages_
);
50 DCHECK(languages_
.size() <
51 IDC_SPELLCHECK_LANGUAGES_LAST
- IDC_SPELLCHECK_LANGUAGES_FIRST
);
52 const std::string app_locale
= g_browser_process
->GetApplicationLocale();
53 for (size_t i
= 0; i
< languages_
.size(); ++i
) {
54 base::string16
display_name(
55 l10n_util::GetDisplayNameForLocale(languages_
[i
], app_locale
, true));
56 submenu_model_
.AddRadioItem(IDC_SPELLCHECK_LANGUAGES_FIRST
+ i
,
61 // Add an item that opens the 'fonts and languages options' page.
62 submenu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
63 submenu_model_
.AddItemWithStringId(
64 IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS
,
65 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS
);
67 // Add a 'Check spelling while typing' item in the sub menu.
68 submenu_model_
.AddCheckItem(
69 IDC_CHECK_SPELLING_WHILE_TYPING
,
70 l10n_util::GetStringUTF16(
71 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING
));
73 // Add a check item "Ask Google for spelling suggestions" item. (This class
74 // does not handle this item because the SpellingMenuObserver class handles it
75 // on behalf of this class.)
76 submenu_model_
.AddCheckItem(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
,
77 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE
));
79 // Add a check item "Automatically correct spelling".
80 const base::CommandLine
* command_line
=
81 base::CommandLine::ForCurrentProcess();
82 if (command_line
->HasSwitch(switches::kEnableSpellingAutoCorrect
)) {
83 submenu_model_
.AddCheckItem(IDC_CONTENT_CONTEXT_AUTOCORRECT_SPELLING_TOGGLE
,
84 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_AUTOCORRECT
));
89 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU
),
93 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id
) {
94 // Allow Spell Check language items on sub menu for text area context menu.
95 if (command_id
>= IDC_SPELLCHECK_LANGUAGES_FIRST
&&
96 command_id
< IDC_SPELLCHECK_LANGUAGES_LAST
) {
100 switch (command_id
) {
101 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS
:
102 // Return false so RenderViewContextMenu can handle this item because it
103 // is hard for this class to handle it.
106 case IDC_CHECK_SPELLING_WHILE_TYPING
:
107 case IDC_SPELLPANEL_TOGGLE
:
108 case IDC_SPELLCHECK_MENU
:
115 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id
) {
116 DCHECK(IsCommandIdSupported(command_id
));
118 if (command_id
>= IDC_SPELLCHECK_LANGUAGES_FIRST
&&
119 command_id
< IDC_SPELLCHECK_LANGUAGES_LAST
) {
120 return language_selected_
== command_id
- IDC_SPELLCHECK_LANGUAGES_FIRST
;
123 // Check box for 'Check Spelling while typing'.
124 if (command_id
== IDC_CHECK_SPELLING_WHILE_TYPING
) {
125 Profile
* profile
= Profile::FromBrowserContext(proxy_
->GetBrowserContext());
127 return profile
->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck
);
133 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id
) {
134 DCHECK(IsCommandIdSupported(command_id
));
136 Profile
* profile
= Profile::FromBrowserContext(proxy_
->GetBrowserContext());
138 const PrefService
* pref
= profile
->GetPrefs();
139 if (command_id
>= IDC_SPELLCHECK_LANGUAGES_FIRST
&&
140 command_id
< IDC_SPELLCHECK_LANGUAGES_LAST
) {
141 return pref
->GetBoolean(prefs::kEnableContinuousSpellcheck
);
144 switch (command_id
) {
145 case IDC_CHECK_SPELLING_WHILE_TYPING
:
146 case IDC_SPELLPANEL_TOGGLE
:
147 case IDC_SPELLCHECK_MENU
:
154 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id
) {
155 DCHECK(IsCommandIdSupported(command_id
));
157 // Check to see if one of the spell check language ids have been clicked.
158 Profile
* profile
= Profile::FromBrowserContext(proxy_
->GetBrowserContext());
160 if (command_id
>= IDC_SPELLCHECK_LANGUAGES_FIRST
&&
161 command_id
< IDC_SPELLCHECK_LANGUAGES_LAST
) {
162 const size_t language
= command_id
- IDC_SPELLCHECK_LANGUAGES_FIRST
;
163 if (profile
&& language
< languages_
.size()) {
164 StringPrefMember dictionary_language
;
165 dictionary_language
.Init(prefs::kSpellCheckDictionary
,
166 profile
->GetPrefs());
167 dictionary_language
.SetValue(languages_
[language
]);
172 switch (command_id
) {
173 case IDC_CHECK_SPELLING_WHILE_TYPING
:
174 profile
->GetPrefs()->SetBoolean(
175 prefs::kEnableContinuousSpellcheck
,
176 !profile
->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck
));