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.
4
5#include "chrome/browser/renderer_context_menu/spellchecker_submenu_observer.h"
6
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_mac.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"
22
23using content::BrowserThread;
24
25namespace {
26
27PrefService* GetPrefs(content::BrowserContext* context) {
28  return user_prefs::UserPrefs::Get(context);
29}
30
31}
32
33SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver(
34    RenderViewContextMenuProxy* proxy,
35    ui::SimpleMenuModel::Delegate* delegate,
36    int group)
37    : proxy_(proxy),
38      submenu_model_(delegate) {
39  DCHECK(proxy_);
40}
41
42SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() {
43}
44
45void SpellCheckerSubMenuObserver::InitMenu(
46    const content::ContextMenuParams& params) {
47  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
48
49  // Add an item that toggles the spelling panel.
50  submenu_model_.AddCheckItem(
51      IDC_SPELLPANEL_TOGGLE,
52      l10n_util::GetStringUTF16(
53          spellcheck_mac::SpellingPanelVisible() ?
54              IDS_CONTENT_CONTEXT_HIDE_SPELLING_PANEL :
55              IDS_CONTENT_CONTEXT_SHOW_SPELLING_PANEL));
56  submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
57
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));
63
64  proxy_->AddSubMenu(
65      IDC_SPELLCHECK_MENU,
66      l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU),
67      &submenu_model_);
68}
69
70bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id) {
71  switch (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.
75      return false;
76
77    case IDC_CHECK_SPELLING_WHILE_TYPING:
78    case IDC_SPELLPANEL_TOGGLE:
79    case IDC_SPELLCHECK_MENU:
80    case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE:
81      return true;
82  }
83
84  return false;
85}
86
87bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) {
88  DCHECK(IsCommandIdSupported(command_id));
89
90  // Check box for 'Check Spelling while typing'.
91  if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) {
92    content::BrowserContext* context = proxy_->GetBrowserContext();
93    DCHECK(context);
94    return GetPrefs(context)->GetBoolean(prefs::kEnableContinuousSpellcheck);
95  }
96
97  return false;
98}
99
100bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) {
101  DCHECK(IsCommandIdSupported(command_id));
102
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:
108      return true;
109  }
110
111  return false;
112}
113
114void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) {
115  DCHECK(IsCommandIdSupported(command_id));
116
117  content::RenderViewHost* rvh = proxy_->GetRenderViewHost();
118  content::BrowserContext* context = proxy_->GetBrowserContext();
119  DCHECK(context);
120  switch (command_id) {
121    case IDC_CHECK_SPELLING_WHILE_TYPING:
122      GetPrefs(context)->SetBoolean(
123          prefs::kEnableContinuousSpellcheck,
124          !GetPrefs(context)->GetBoolean(prefs::kEnableContinuousSpellcheck));
125      break;
126
127    case IDC_SPELLPANEL_TOGGLE:
128      rvh->Send(new SpellCheckMsg_ToggleSpellPanel(
129          rvh->GetRoutingID(), spellcheck_mac::SpellingPanelVisible()));
130      break;
131  }
132}
133