- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / tab_contents / spellchecker_submenu_observer_mac.cc
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.
4
5 #include "chrome/browser/tab_contents/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/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"
21
22 using content::BrowserThread;
23
24 SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver(
25     RenderViewContextMenuProxy* proxy,
26     ui::SimpleMenuModel::Delegate* delegate,
27     int group)
28     : proxy_(proxy),
29       submenu_model_(delegate) {
30   DCHECK(proxy_);
31 }
32
33 SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() {
34 }
35
36 void SpellCheckerSubMenuObserver::InitMenu(
37     const content::ContextMenuParams& params) {
38   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
39
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);
48
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));
54
55   proxy_->AddSubMenu(
56       IDC_SPELLCHECK_MENU,
57       l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU),
58       &submenu_model_);
59 }
60
61 bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id) {
62   switch (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.
66       return false;
67
68     case IDC_CHECK_SPELLING_WHILE_TYPING:
69     case IDC_SPELLPANEL_TOGGLE:
70     case IDC_SPELLCHECK_MENU:
71     case IDC_CONTENT_CONTEXT_SPELLING_TOGGLE:
72       return true;
73   }
74
75   return false;
76 }
77
78 bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) {
79   DCHECK(IsCommandIdSupported(command_id));
80
81   // Check box for 'Check Spelling while typing'.
82   if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) {
83     Profile* profile = proxy_->GetProfile();
84     DCHECK(profile);
85     return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck);
86   }
87
88   return false;
89 }
90
91 bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) {
92   DCHECK(IsCommandIdSupported(command_id));
93
94   switch (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:
99       return true;
100   }
101
102   return false;
103 }
104
105 void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) {
106   DCHECK(IsCommandIdSupported(command_id));
107
108   content::RenderViewHost* rvh = proxy_->GetRenderViewHost();
109   Profile* profile = proxy_->GetProfile();
110   DCHECK(profile);
111   switch (command_id) {
112     case IDC_CHECK_SPELLING_WHILE_TYPING:
113       profile->GetPrefs()->SetBoolean(
114           prefs::kEnableContinuousSpellcheck,
115           !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck));
116       break;
117
118     case IDC_SPELLPANEL_TOGGLE:
119       rvh->Send(new SpellCheckMsg_ToggleSpellPanel(
120           rvh->GetRoutingID(), spellcheck_mac::SpellingPanelVisible()));
121       break;
122   }
123 }