summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorsidchat@google.com <sidchat@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-01 21:08:11 +0000
committersidchat@google.com <sidchat@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-01 21:08:11 +0000
commitc5f9f1854f1cac2a4e7c459e86bc46a44365f7cf (patch)
treecdcd7a41d9fe730b0f704d153bdbf47b51a7c09c /chrome
parent8bbe7efe110336916c819fdc7f5566afa3a97495 (diff)
downloadchromium_src-c5f9f1854f1cac2a4e7c459e86bc46a44365f7cf.zip
chromium_src-c5f9f1854f1cac2a4e7c459e86bc46a44365f7cf.tar.gz
chromium_src-c5f9f1854f1cac2a4e7c459e86bc46a44365f7cf.tar.bz2
Add a new submenu in the context menu for a text box. This submenu displays some spell check languages that Chrome supports, derived from the accept languages list and the current spell check language. The spell check language which is currently set is shown to be selected. The user can click on some other spell check language, and it will become effective immediately (this part of the code was addressed in a previous CL, and has been checked in).
Review URL: http://codereview.chromium.org/12614 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6162 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/app/chrome_dll_resource.h4
-rw-r--r--chrome/browser/render_view_context_menu.cc22
-rw-r--r--chrome/browser/render_view_context_menu.h1
-rw-r--r--chrome/browser/render_view_context_menu_controller.cc40
-rw-r--r--chrome/browser/render_view_context_menu_controller.h4
-rw-r--r--chrome/browser/spellchecker.cc118
-rw-r--r--chrome/browser/spellchecker.h20
-rw-r--r--chrome/browser/views/options/languages_page_view.cc47
8 files changed, 215 insertions, 41 deletions
diff --git a/chrome/app/chrome_dll_resource.h b/chrome/app/chrome_dll_resource.h
index b9af6d6..e26b47c 100644
--- a/chrome/app/chrome_dll_resource.h
+++ b/chrome/app/chrome_dll_resource.h
@@ -150,6 +150,10 @@
// Free space 32947 - 32950
#define IDR_CRASHED_PLUGIN 32951
#define IDC_SHOW_BOOKMARK_MANAGER 32952
+#define IDC_SHOW_SPELLCHECKER_SUBMENU 32953
+// Reserve space for 100 Spell Check languages - currently we have 30.
+#define IDC_SPELLCHECKER_LANGUAGE_FIRST 32954
+#define IDC_SPELLCHECKER_LANGUAGE_LAST 33054
// Next default values for new objects
//
diff --git a/chrome/browser/render_view_context_menu.cc b/chrome/browser/render_view_context_menu.cc
index 81cf1ad..b50dc9f 100644
--- a/chrome/browser/render_view_context_menu.cc
+++ b/chrome/browser/render_view_context_menu.cc
@@ -6,7 +6,9 @@
#include "base/logging.h"
#include "chrome/app/chrome_dll_resource.h"
+#include "chrome/browser/browser_process.h"
#include "chrome/browser/profile.h"
+#include "chrome/browser/spellchecker.h"
#include "chrome/browser/template_url_model.h"
#include "chrome/common/l10n_util.h"
#include "webkit/glue/context_node_types.h"
@@ -143,6 +145,24 @@ void RenderViewContextMenu::AppendEditableItems() {
AppendDelegateMenuItem(IDS_CONTENT_CONTEXT_PASTE);
AppendDelegateMenuItem(IDS_CONTENT_CONTEXT_DELETE);
AppendSeparator();
+
+ // Add Spell Check options sub menu.
+ spellchecker_sub_menu_ = AppendSubMenu(IDC_SHOW_SPELLCHECKER_SUBMENU,
+ l10n_util::GetString(IDS_CONTENT_CONTEXT_SPELLCHECK_OPTIONS));
+
+ // Add Spell Check languages to sub menu.
+ std::vector<std::wstring> language_vector;
+ int current_language_index = SpellChecker::
+ GetSpellCheckLanguagesToDisplayInContextMenu(profile_, &language_vector);
+ DCHECK(language_vector.size() <=
+ IDC_SPELLCHECKER_LANGUAGE_LAST - IDC_SPELLCHECKER_LANGUAGE_FIRST);
+ const std::wstring app_locale = g_browser_process->GetApplicationLocale();
+ for (size_t i = 0; i < language_vector.size(); ++i) {
+ std::wstring local_language(l10n_util::GetLocalName(
+ language_vector.at(i), app_locale, true));
+ spellchecker_sub_menu_->AppendMenuItem(
+ IDC_SPELLCHECKER_LANGUAGE_FIRST + i, local_language, RADIO);
+ }
+ AppendSeparator();
AppendDelegateMenuItem(IDS_CONTENT_CONTEXT_SELECTALL);
}
-
diff --git a/chrome/browser/render_view_context_menu.h b/chrome/browser/render_view_context_menu.h
index a306d7f..a38cbe8 100644
--- a/chrome/browser/render_view_context_menu.h
+++ b/chrome/browser/render_view_context_menu.h
@@ -35,6 +35,7 @@ class RenderViewContextMenu : public Menu {
std::wstring misspelled_word_;
std::vector<std::wstring> misspelled_word_suggestions_;
Profile* profile_;
+ Menu* spellchecker_sub_menu_;
DISALLOW_EVIL_CONSTRUCTORS(RenderViewContextMenu);
};
diff --git a/chrome/browser/render_view_context_menu_controller.cc b/chrome/browser/render_view_context_menu_controller.cc
index 0e1b8f0..4943333 100644
--- a/chrome/browser/render_view_context_menu_controller.cc
+++ b/chrome/browser/render_view_context_menu_controller.cc
@@ -13,6 +13,7 @@
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "chrome/browser/browser_process.h"
+#include "chrome/browser/spellchecker.h"
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/download/save_package.h"
#include "chrome/browser/navigation_controller.h"
@@ -101,6 +102,12 @@ std::wstring RenderViewContextMenuController::GetLabel(int id) const {
}
bool RenderViewContextMenuController::IsCommandEnabled(int id) const {
+ // Allow Spell Check language items on sub menu for text area context menu.
+ if (id >= IDC_SPELLCHECKER_LANGUAGE_FIRST
+ && id <= IDC_SPELLCHECKER_LANGUAGE_LAST) {
+ return true;
+ }
+
switch (id) {
case IDS_CONTENT_CONTEXT_BACK:
return source_web_contents_->controller()->CanGoBack();
@@ -173,12 +180,16 @@ bool RenderViewContextMenuController::IsCommandEnabled(int id) const {
case IDC_USESPELLCHECKSUGGESTION_3:
case IDC_USESPELLCHECKSUGGESTION_4:
return true;
+ case IDC_SHOW_SPELLCHECKER_SUBMENU:
+ return true;
case IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY:
return !params_.misspelled_word.empty();
case IDS_CONTENT_CONTEXT_VIEWPAGEINFO:
return (source_web_contents_->controller()->GetActiveEntry() != NULL);
case IDS_CONTENT_CONTEXT_VIEWFRAMEINFO:
return true;
+ case IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS:
+ return true;
case IDS_CONTENT_CONTEXT_SAVEFRAMEAS:
case IDS_CONTENT_CONTEXT_PRINTFRAME:
case IDS_CONTENT_CONTEXT_ADDSEARCHENGINE: // Not implemented.
@@ -187,6 +198,17 @@ bool RenderViewContextMenuController::IsCommandEnabled(int id) const {
}
}
+bool RenderViewContextMenuController::IsItemChecked(int id) const {
+ std::vector<std::wstring> display_language_vector;
+ int spellcheck_language_index = SpellChecker::
+ GetSpellCheckLanguagesToDisplayInContextMenu(
+ source_web_contents_->profile(), &display_language_vector);
+ if (id - IDC_SPELLCHECKER_LANGUAGE_FIRST == spellcheck_language_index)
+ return true;
+
+ return false;
+}
+
bool RenderViewContextMenuController::GetAcceleratorInfo(
int id,
views::Accelerator* accel) {
@@ -222,6 +244,24 @@ bool RenderViewContextMenuController::GetAcceleratorInfo(
}
void RenderViewContextMenuController::ExecuteCommand(int id) {
+ // Check to see if one of the spell check language ids have been clicked.
+ if (id >= IDC_SPELLCHECKER_LANGUAGE_FIRST &&
+ id <= IDC_SPELLCHECKER_LANGUAGE_LAST) {
+ std::vector<std::wstring> display_language_vector;
+ int current_language = SpellChecker::
+ GetSpellCheckLanguagesToDisplayInContextMenu(
+ source_web_contents_->profile(), &display_language_vector);
+ if (id - IDC_SPELLCHECKER_LANGUAGE_FIRST <
+ static_cast<int>(display_language_vector.size())) {
+ StringPrefMember dictionary_language;
+ dictionary_language.Init(prefs::kSpellCheckDictionary,
+ source_web_contents_->profile()->GetPrefs(), NULL);
+ dictionary_language.SetValue(display_language_vector.at(
+ id - IDC_SPELLCHECKER_LANGUAGE_FIRST));
+ }
+
+ return;
+ }
switch (id) {
case IDS_CONTENT_CONTEXT_OPENLINKNEWTAB:
OpenURL(params_.link_url, NEW_BACKGROUND_TAB, PageTransition::LINK);
diff --git a/chrome/browser/render_view_context_menu_controller.h b/chrome/browser/render_view_context_menu_controller.h
index c742b82..b03fd3b 100644
--- a/chrome/browser/render_view_context_menu_controller.h
+++ b/chrome/browser/render_view_context_menu_controller.h
@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_RENDER_VIEW_CONTEXT_MENU_CONTROLLER_H__
#define CHROME_BROWSER_RENDER_VIEW_CONTEXT_MENU_CONTROLLER_H__
+#include "chrome/common/pref_member.h"
#include "chrome/views/menu.h"
#include "chrome/common/render_messages.h"
@@ -19,6 +20,7 @@ class RenderViewContextMenuController : public Menu::Delegate {
// Overridden from Menu::Delegate
virtual std::wstring GetLabel(int id) const;
virtual bool IsCommandEnabled(int id) const;
+ virtual bool IsItemChecked(int id) const;
virtual void ExecuteCommand(int id);
virtual bool GetAcceleratorInfo(int id, views::Accelerator* accel);
@@ -46,6 +48,8 @@ class RenderViewContextMenuController : public Menu::Delegate {
private:
WebContents* source_web_contents_;
ViewHostMsg_ContextMenu_Params params_;
+ StringPrefMember dictionary_language_;
+ int current_dictionary_language_index_;
};
#endif // CHROME_BROWSER_RENDER_VIEW_CONTEXT_MENU_CONTROLLER_H__
diff --git a/chrome/browser/spellchecker.cc b/chrome/browser/spellchecker.cc
index 86ede3c..04a0855 100644
--- a/chrome/browser/spellchecker.cc
+++ b/chrome/browser/spellchecker.cc
@@ -34,6 +34,124 @@ using base::TimeTicks;
static const int kMaxSuggestions = 5; // Max number of dictionary suggestions.
+namespace {
+const wchar_t* const g_supported_spellchecker_languages[] = {
+ L"en-US",
+ L"en-GB",
+ L"fr-FR",
+ L"it-IT",
+ L"de-DE",
+ L"es-ES",
+ L"nl-NL",
+ L"pt-BR",
+ L"ru-RU",
+ L"pl-PL",
+ // L"th-TH", // Not to be included in Spellchecker as per B=1277824
+ L"sv-SE",
+ L"da-DK",
+ L"pt-PT",
+ L"ro-RO",
+ // L"hu-HU", // Not to be included in Spellchecker as per B=1277824
+ // L"he-IL", // Not to be included in Spellchecker as per B=1252241
+ L"id-ID",
+ L"cs-CZ",
+ L"el-GR",
+ L"nb-NO",
+ L"vi-VN",
+ // L"bg-BG", // Not to be included in Spellchecker as per B=1277824
+ L"hr-HR",
+ L"lt-LT",
+ L"sk-SK",
+ L"sl-SI",
+ L"ca-ES",
+ L"lv-LV",
+ // L"uk-UA", // Not to be included in Spellchecker as per B=1277824
+ L"hi-IN",
+ //
+ // TODO(Sidchat): Uncomment/remove languages as and when they get resolved.
+ //
+};
+
+}
+
+void SpellChecker::SpellCheckLanguages(std::vector<std::wstring>* languages) {
+ for (size_t i = 0;
+ i < arraysize(g_supported_spellchecker_languages);
+ i++) {
+ languages->push_back(g_supported_spellchecker_languages[i]);
+ }
+}
+
+std::wstring SpellChecker::GetCorrespondingSpellCheckLanguage(
+ const std::wstring& language) {
+ // Look for exact match in the Spell Check language list.
+ for (int i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) {
+ std::wstring spellcheck_language(g_supported_spellchecker_languages[i]);
+ if (spellcheck_language == language)
+ return language;
+ }
+
+ // Find match for the language. For example, for "hi", the corresponding
+ // Spell Check language is "hi-IN". The input language has to be 2 letter.
+ if (language.length() == 2) {
+ for (int i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) {
+ std::wstring spellcheck_language(g_supported_spellchecker_languages[i]);
+ std::wstring spellcheck_country(spellcheck_language.substr(0, 2));
+ if (spellcheck_country == language)
+ return spellcheck_language;
+ }
+ }
+
+ // No match found - return blank.
+ return std::wstring();
+}
+
+int SpellChecker::GetSpellCheckLanguagesToDisplayInContextMenu(
+ Profile* profile, std::vector<std::wstring>* display_language_list) {
+ std::vector<std::wstring> language_list;
+ StringPrefMember accept_languages;
+ StringPrefMember dictionary_language;
+ accept_languages.Init(prefs::kAcceptLanguages,
+ profile->GetPrefs(), NULL);
+ dictionary_language.Init(prefs::kSpellCheckDictionary,
+ profile->GetPrefs(), NULL);
+
+ // The current dictionary language should be there.
+ display_language_list->push_back(dictionary_language.GetValue().c_str());
+
+ // Now scan through the list of accept languages, and find possible mappings
+ // from this list to the existing list of spell check languages.
+ std::vector<std::wstring> accept_language_vector;
+ SplitString(accept_languages.GetValue(), L',', &accept_language_vector);
+ for (size_t i = 0; i < accept_language_vector.size(); i++) {
+ std::wstring language = GetCorrespondingSpellCheckLanguage(
+ accept_language_vector.at(i));
+ if (!language.empty()) {
+ // Check for duplication.
+ bool duplicate = false;
+ for (size_t j = 0; j < display_language_list->size(); j++) {
+ if (display_language_list->at(j) == language)
+ duplicate = true;
+ }
+
+ if (!duplicate)
+ display_language_list->push_back(language.c_str());
+ }
+ }
+
+ // Sort using locale specific sorter.
+ l10n_util::SortStrings(g_browser_process->GetApplicationLocale(),
+ display_language_list);
+
+ int current_spell_check_language_index = -1;
+ for (size_t i = 0; i < display_language_list->size(); i++) {
+ if (dictionary_language.GetValue() == display_language_list->at(i))
+ current_spell_check_language_index = i;
+ }
+
+ return current_spell_check_language_index;
+}
+
// This is a helper class which acts as a proxy for invoking a task from the
// file loop back to the IO loop. Invoking a task from file loop to the IO
// loop directly is not safe as during browser shutdown, the IO loop tears
diff --git a/chrome/browser/spellchecker.h b/chrome/browser/spellchecker.h
index bfe3121..07be6120 100644
--- a/chrome/browser/spellchecker.h
+++ b/chrome/browser/spellchecker.h
@@ -7,7 +7,13 @@
#include <vector>
+#include "base/string_util.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/profile.h"
#include "chrome/browser/spellcheck_worditerator.h"
+#include "chrome/common/l10n_util.h"
+#include "chrome/common/pref_names.h"
+#include "chrome/common/pref_member.h"
#include "base/task.h"
#include "unicode/uscript.h"
@@ -18,6 +24,7 @@ class Profile;
class MessageLoop;
class URLRequestContext;
+
// The Browser's Spell Checker. It checks and suggests corrections.
//
// This object is not threadsafe. In normal usage (not unit tests) it lives on
@@ -65,7 +72,15 @@ class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> {
// b) Add the word to a file in disk for custom dictionary.
void AddWord(const std::wstring& word);
- private:
+ // Get SpellChecker supported languages.
+ static void SpellCheckLanguages(std::vector<std::wstring>* languages);
+
+ // This function computes a vector of strings which are to be displayed in
+ // the context menu over a text area for changing spell check languages. It
+ // returns the index of the current spell check language in the vector.
+ static int SpellChecker::GetSpellCheckLanguagesToDisplayInContextMenu(
+ Profile* profile, std::vector<std::wstring>* display_language_list);
+
// Download dictionary files when required.
class DictionaryDownloadController;
@@ -93,6 +108,9 @@ class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> {
std::wstring GetVersionedFileName(const std::wstring& language,
const std::wstring& dict_dir);
+ static std::wstring GetCorrespondingSpellCheckLanguage(
+ const std::wstring& language);
+
// Path to the spellchecker file.
std::wstring bdict_file_name_;
diff --git a/chrome/browser/views/options/languages_page_view.cc b/chrome/browser/views/options/languages_page_view.cc
index 78d5235..118ac10 100644
--- a/chrome/browser/views/options/languages_page_view.cc
+++ b/chrome/browser/views/options/languages_page_view.cc
@@ -16,6 +16,7 @@
#include "chrome/app/theme/theme_resources.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/shell_dialogs.h"
+#include "chrome/browser/spellchecker.h"
#include "chrome/browser/views/options/language_combobox_model.h"
#include "chrome/browser/views/password_manager_view.h"
#include "chrome/browser/views/restart_message_box.h"
@@ -40,43 +41,6 @@
#include "chromium_strings.h"
#include "generated_resources.h"
-static const wchar_t* const g_supported_spellchecker_languages[] = {
- L"en-US",
- L"en-GB",
- L"fr-FR",
- L"it-IT",
- L"de-DE",
- L"es-ES",
- L"nl-NL",
- L"pt-BR",
- L"ru-RU",
- L"pl-PL",
- // L"th-TH", // Not to be included in Spellchecker as per B=1277824
- L"sv-SE",
- L"da-DK",
- L"pt-PT",
- L"ro-RO",
- // L"hu-HU", // Not to be included in Spellchecker as per B=1277824
- // L"he-IL", // Not to be included in Spellchecker as per B=1252241
- L"id-ID",
- L"cs-CZ",
- L"el-GR",
- L"nb-NO",
- L"vi-VN",
- // L"bg-BG", // Not to be included in Spellchecker as per B=1277824
- L"hr-HR",
- L"lt-LT",
- L"sk-SK",
- L"sl-SI",
- L"ca-ES",
- L"lv-LV",
- // L"uk-UA", // Not to be included in Spellchecker as per B=1277824
- L"hi-IN",
- //
- // TODO(Sidchat): Uncomment/remove languages as and when they get resolved.
- //
-};
-
static const wchar_t* const accept_language_list[] = {
L"af", // Afrikaans
L"am", // Amharic
@@ -665,8 +629,13 @@ void LanguagesPageView::InitControlLayout() {
// Determine Locale Codes.
std::vector<std::wstring> locale_codes;
- for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); ++i)
- locale_codes.push_back(g_supported_spellchecker_languages[i]);
+ std::vector<std::wstring> spell_check_languages;
+ SpellChecker::SpellCheckLanguages(&spell_check_languages);
+ for (size_t i = 0;
+ i < spell_check_languages.size();
+ ++i) {
+ locale_codes.push_back(spell_check_languages.at(i));
+ }
dictionary_language_model_.reset(new LanguageComboboxModel(profile(),
locale_codes));
change_dictionary_language_combobox_ =