diff options
author | csilv@chromium.org <csilv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-09 18:56:54 +0000 |
---|---|---|
committer | csilv@chromium.org <csilv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-09 18:56:54 +0000 |
commit | fe944a7847676efd6672773b6c48ef3f8b2f55f0 (patch) | |
tree | 92a724c62e8b4392f2c556bec247f831f0d10317 /chrome | |
parent | 9925e36fdbe0830c2f838bfb8d452599069e2e51 (diff) | |
download | chromium_src-fe944a7847676efd6672773b6c48ef3f8b2f55f0.zip chromium_src-fe944a7847676efd6672773b6c48ef3f8b2f55f0.tar.gz chromium_src-fe944a7847676efd6672773b6c48ef3f8b2f55f0.tar.bz2 |
dom-ui options: Implement font settings overlay dialog.
BUG=48039
TEST=Exercise font settings in dom-ui options window using --enable-tabbed-options
Review URL: http://codereview.chromium.org/3073006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@55428 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/app/generated_resources.grd | 4 | ||||
-rw-r--r-- | chrome/browser/dom_ui/font_settings_handler.cc | 133 | ||||
-rw-r--r-- | chrome/browser/dom_ui/font_settings_handler.h | 22 | ||||
-rw-r--r-- | chrome/browser/dom_ui/font_settings_utils.h | 26 | ||||
-rw-r--r-- | chrome/browser/dom_ui/font_settings_utils_gtk.cc | 36 | ||||
-rw-r--r-- | chrome/browser/dom_ui/font_settings_utils_mac.mm | 53 | ||||
-rw-r--r-- | chrome/browser/dom_ui/font_settings_utils_win.cc | 47 | ||||
-rw-r--r-- | chrome/browser/resources/options/font_settings_overlay.html | 58 | ||||
-rw-r--r-- | chrome/browser/resources/options/font_settings_overlay.js | 35 | ||||
-rw-r--r-- | chrome/browser/resources/options/pref_ui.js | 3 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 4 |
11 files changed, 417 insertions, 4 deletions
diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd index c4051f1..6e2a3a6 100644 --- a/chrome/app/generated_resources.grd +++ b/chrome/app/generated_resources.grd @@ -5356,7 +5356,9 @@ Keep your key file in a safe place. You will need it to create new versions of y <message name="IDS_FONT_SETTING_WINDOWS_TITLE" desc="Title that appears in the dialogue title bar for advanced font/encoding settings"> Fonts and Encoding </message> - + <message name="IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_SELECTOR_LABEL" desc="Dialog label for font size selection"> + Size: + </message> <!-- HTTP POST Warning --> <message name="IDS_HTTP_POST_WARNING_TITLE" desc="Title for dialog that warns users about a navigation that results in a repost"> diff --git a/chrome/browser/dom_ui/font_settings_handler.cc b/chrome/browser/dom_ui/font_settings_handler.cc index deabdc6..e6402fe 100644 --- a/chrome/browser/dom_ui/font_settings_handler.cc +++ b/chrome/browser/dom_ui/font_settings_handler.cc @@ -6,7 +6,16 @@ #include "app/l10n_util.h" #include "base/basictypes.h" +#include "base/string_number_conversions.h" +#include "base/string_util.h" #include "base/values.h" +#include "chrome/browser/character_encoding.h" +#include "chrome/browser/dom_ui/font_settings_utils.h" +#include "chrome/browser/pref_service.h" +#include "chrome/browser/profile.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/notification_type.h" +#include "chrome/common/pref_names.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" @@ -22,5 +31,129 @@ void FontSettingsHandler::GetLocalizedValues( localized_strings->SetString("fontSettingsTitle", l10n_util::GetStringUTF16(IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE)); + localized_strings->SetString(L"fontSettingsFontTitle", + l10n_util::GetString( + IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_FONT_TITLE)); + localized_strings->SetString(L"fontSettingsSerifLabel", + l10n_util::GetString( + IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SERIF_LABEL)); + localized_strings->SetString(L"fontSettingsSansSerifLabel", + l10n_util::GetString( + IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SANS_SERIF_LABEL)); + localized_strings->SetString(L"fontSettingsFixedWidthLabel", + l10n_util::GetString( + IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_FIXED_WIDTH_LABEL)); + localized_strings->SetString(L"fontSettingsSizeLabel", + l10n_util::GetString( + IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_SELECTOR_LABEL)); + localized_strings->SetString(L"fontSettingsEncodingTitle", + l10n_util::GetString( + IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE)); + localized_strings->SetString(L"fontSettingsEncodingLabel", + l10n_util::GetString( + IDS_FONT_LANGUAGE_SETTING_FONT_DEFAULT_ENCODING_SELECTOR_LABEL)); + + // Fonts + ListValue* font_list = FontSettingsUtilities::GetFontsList(); + if (font_list) localized_strings->Set(L"fontSettingsFontList", font_list); + + // Font sizes + int font_sizes[] = { 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, + 28, 30, 32, 34, 36, 40, 44, 48, 56, 64, 72 }; + int count = arraysize(font_sizes); + ListValue* font_size_list = new ListValue; + for (int i = 0; i < count; i++) { + ListValue* option = new ListValue(); + option->Append(Value::CreateIntegerValue(font_sizes[i])); + option->Append(Value::CreateStringValue(base::IntToString(font_sizes[i]))); + font_size_list->Append(option); + } + localized_strings->Set(L"fontSettingsFontSizeList", font_size_list); + + // Encodings + count = CharacterEncoding::GetSupportCanonicalEncodingCount(); + ListValue* encoding_list = new ListValue; + for (int i = 0; i < count; ++i) { + int cmd_id = CharacterEncoding::GetEncodingCommandIdByIndex(i); + std::string encoding = + CharacterEncoding::GetCanonicalEncodingNameByCommandId(cmd_id); + string16 name = + CharacterEncoding::GetCanonicalEncodingDisplayNameByCommandId(cmd_id); + + ListValue* option = new ListValue(); + option->Append(Value::CreateStringValue(encoding)); + option->Append(Value::CreateStringValue(name)); + encoding_list->Append(option); + } + localized_strings->Set(L"fontSettingsEncodingList", encoding_list); +} + +void FontSettingsHandler::Initialize() { + SetupSerifFontPreview(); + SetupSansSerifFontPreview(); + SetupFixedFontPreview(); +} + +DOMMessageHandler* FontSettingsHandler::Attach(DOMUI* dom_ui) { + // Call through to superclass. + DOMMessageHandler* handler = OptionsPageUIHandler::Attach(dom_ui); + + // Perform validation for saved fonts. + DCHECK(dom_ui_); + PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); + FontSettingsUtilities::ValidateSavedFonts(pref_service); + + // Register for preferences that we need to observe manually. + serif_font_.Init(prefs::kWebKitSerifFontFamily, pref_service, this); + sans_serif_font_.Init(prefs::kWebKitSansSerifFontFamily, pref_service, this); + fixed_font_.Init(prefs::kWebKitFixedFontFamily, pref_service, this); + default_font_size_.Init(prefs::kWebKitDefaultFontSize, pref_service, this); + default_fixed_font_size_.Init(prefs::kWebKitDefaultFixedFontSize, + pref_service, this); + + // Return result from the superclass. + return handler; +} + +void FontSettingsHandler::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (type == NotificationType::PREF_CHANGED) { + std::wstring* pref_name = Details<std::wstring>(details).ptr(); + if (*pref_name == prefs::kWebKitSerifFontFamily || + *pref_name == prefs::kWebKitDefaultFontSize) { + SetupSerifFontPreview(); + } else if (*pref_name == prefs::kWebKitSansSerifFontFamily || + *pref_name == prefs::kWebKitDefaultFontSize) { + SetupSansSerifFontPreview(); + } else if (*pref_name == prefs::kWebKitFixedFontFamily || + *pref_name == prefs::kWebKitDefaultFixedFontSize) { + SetupFixedFontPreview(); + } + } +} + +void FontSettingsHandler::SetupSerifFontPreview() { + DCHECK(dom_ui_); + StringValue font_value(serif_font_.GetValue()); + FundamentalValue size_value(default_font_size_.GetValue()); + dom_ui_->CallJavascriptFunction( + L"FontSettingsOverlay.setupSerifFontPreview", font_value, size_value); +} + +void FontSettingsHandler::SetupSansSerifFontPreview() { + DCHECK(dom_ui_); + StringValue font_value(sans_serif_font_.GetValue()); + FundamentalValue size_value(default_font_size_.GetValue()); + dom_ui_->CallJavascriptFunction( + L"FontSettingsOverlay.setupSansSerifFontPreview", font_value, size_value); +} + +void FontSettingsHandler::SetupFixedFontPreview() { + DCHECK(dom_ui_); + StringValue font_value(fixed_font_.GetValue()); + FundamentalValue size_value(default_fixed_font_size_.GetValue()); + dom_ui_->CallJavascriptFunction( + L"FontSettingsOverlay.setupFixedFontPreview", font_value, size_value); } diff --git a/chrome/browser/dom_ui/font_settings_handler.h b/chrome/browser/dom_ui/font_settings_handler.h index a515afa..d5974c3 100644 --- a/chrome/browser/dom_ui/font_settings_handler.h +++ b/chrome/browser/dom_ui/font_settings_handler.h @@ -7,8 +7,9 @@ #pragma once #include "chrome/browser/dom_ui/options_ui.h" +#include "chrome/browser/pref_member.h" -// Chrome personal options page UI handler. +// Font settings overlay page UI handler. class FontSettingsHandler : public OptionsPageUIHandler { public: FontSettingsHandler(); @@ -16,8 +17,27 @@ class FontSettingsHandler : public OptionsPageUIHandler { // OptionsUIHandler implementation. virtual void GetLocalizedValues(DictionaryValue* localized_strings); + virtual void Initialize(); + + // DOMMessageHandler implementation. + virtual DOMMessageHandler* Attach(DOMUI* dom_ui); + + // NotificationObserver implementation. + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); private: + void SetupSerifFontPreview(); + void SetupSansSerifFontPreview(); + void SetupFixedFontPreview(); + + StringPrefMember serif_font_; + StringPrefMember sans_serif_font_; + StringPrefMember fixed_font_; + IntegerPrefMember default_font_size_; + IntegerPrefMember default_fixed_font_size_; + DISALLOW_COPY_AND_ASSIGN(FontSettingsHandler); }; diff --git a/chrome/browser/dom_ui/font_settings_utils.h b/chrome/browser/dom_ui/font_settings_utils.h new file mode 100644 index 0000000..2d0d036 --- /dev/null +++ b/chrome/browser/dom_ui/font_settings_utils.h @@ -0,0 +1,26 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_DOM_UI_FONT_SETTINGS_UTILS_H_ +#define CHROME_BROWSER_DOM_UI_FONT_SETTINGS_UTILS_H_ +#pragma once + +#include "base/basictypes.h" + +class ListValue; +class PrefService; + +// Chrome advanced options utility methods. +class FontSettingsUtilities { + public: + static ListValue* GetFontsList(); + + static void ValidateSavedFonts(PrefService* prefs); + + private: + DISALLOW_IMPLICIT_CONSTRUCTORS(FontSettingsUtilities); +}; + +#endif // CHROME_BROWSER_DOM_UI_FONT_SETTINGS_UTILS_H_ + diff --git a/chrome/browser/dom_ui/font_settings_utils_gtk.cc b/chrome/browser/dom_ui/font_settings_utils_gtk.cc new file mode 100644 index 0000000..82917b4 --- /dev/null +++ b/chrome/browser/dom_ui/font_settings_utils_gtk.cc @@ -0,0 +1,36 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/dom_ui/font_settings_utils.h" + +#include <pango/pango.h> +#include <pango/pangocairo.h> + +#include "base/values.h" + +ListValue* FontSettingsUtilities::GetFontsList() { + ListValue* font_list = new ListValue; + + PangoFontMap* font_map = ::pango_cairo_font_map_get_default(); + PangoFontFamily** families = NULL; + int num_families = 0; + ::pango_font_map_list_families(font_map, &families, &num_families); + + for (int i = 0; i < num_families; i++) { + ListValue* font_item = new ListValue(); + const char* family_name = ::pango_font_family_get_name(families[i]); + font_item->Append(Value::CreateStringValue(family_name)); + font_item->Append(Value::CreateStringValue(family_name)); + font_list->Append(font_item); + } + + g_free(families); + + return font_list; +} + +void FontSettingsUtilities::ValidateSavedFonts(PrefService* prefs) { + // nothing to do for GTK. +} + diff --git a/chrome/browser/dom_ui/font_settings_utils_mac.mm b/chrome/browser/dom_ui/font_settings_utils_mac.mm new file mode 100644 index 0000000..33eb468 --- /dev/null +++ b/chrome/browser/dom_ui/font_settings_utils_mac.mm @@ -0,0 +1,53 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/dom_ui/font_settings_utils.h" + +#import <Cocoa/Cocoa.h> + +#include "base/sys_string_conversions.h" +#include "base/values.h" +#include "chrome/browser/pref_service.h" +#include "chrome/common/pref_names.h" + +static void ValidateFontFamily(PrefService* prefs, + const wchar_t* family_pref_name) { + // The native font settings dialog saved fonts by the font name, rather + // than the family name. This worked for the old dialog since + // -[NSFont fontWithName:size] accepted a font or family name, but the + // behavior was technically wrong. Since we really need the family name for + // the dom-ui options window, we will fix the saved preference if necessary. + NSString *family_name = + base::SysUTF8ToNSString(prefs->GetString(family_pref_name)); + NSFont *font = [NSFont fontWithName:family_name + size:[NSFont systemFontSize]]; + if (font && + [[font familyName] caseInsensitiveCompare:family_name] != NSOrderedSame) { + std::string new_family_name = base::SysNSStringToUTF8([font familyName]); + prefs->SetString(family_pref_name, new_family_name); + } +} + +ListValue* FontSettingsUtilities::GetFontsList() { + ListValue* font_list = new ListValue; + NSFontManager* fontManager = [NSFontManager sharedFontManager]; + NSArray* fonts = [fontManager availableFontFamilies]; + for (NSString* family_name in fonts) { + NSString* localized_family_name = + [fontManager localizedNameForFamily:family_name face:nil]; + ListValue* font_item = new ListValue(); + string16 family = base::SysNSStringToUTF16(family_name); + font_item->Append(Value::CreateStringValue(family)); + string16 loc_family = base::SysNSStringToUTF16(localized_family_name); + font_item->Append(Value::CreateStringValue(loc_family)); + font_list->Append(font_item); + } + return font_list; +} + +void FontSettingsUtilities::ValidateSavedFonts(PrefService* prefs) { + ValidateFontFamily(prefs, prefs::kWebKitSerifFontFamily); + ValidateFontFamily(prefs, prefs::kWebKitSansSerifFontFamily); + ValidateFontFamily(prefs, prefs::kWebKitFixedFontFamily); +} diff --git a/chrome/browser/dom_ui/font_settings_utils_win.cc b/chrome/browser/dom_ui/font_settings_utils_win.cc new file mode 100644 index 0000000..66be7b5 --- /dev/null +++ b/chrome/browser/dom_ui/font_settings_utils_win.cc @@ -0,0 +1,47 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/dom_ui/font_settings_utils.h" + +#include <windows.h> + +#include "base/values.h" + +static int CALLBACK EnumFontFamExProc(ENUMLOGFONTEXW *logical_font, + NEWTEXTMETRICEXW *physical_font, + DWORD font_type, + LPARAM lparam) { + ListValue* font_list = reinterpret_cast<ListValue*>(lparam); + if (font_list) { + const LOGFONTW& lf = logical_font->elfLogFont; + if (lf.lfFaceName[0] && lf.lfFaceName[0] != '@') { + ListValue* font_item = new ListValue(); + std::wstring face_name(lf.lfFaceName); + font_item->Append(Value::CreateStringValue(face_name)); + font_item->Append(Value::CreateStringValue(face_name)); + font_list->Append(font_item); + } + } + return 1; +} + +ListValue* FontSettingsUtilities::GetFontsList() { + ListValue* font_list = new ListValue; + + LOGFONTW logfont; + memset(&logfont, 0, sizeof(logfont)); + logfont.lfCharSet = DEFAULT_CHARSET; + + HDC hdc = ::GetDC(NULL); + ::EnumFontFamiliesExW(hdc, &logfont, (FONTENUMPROCW)&EnumFontFamExProc, + (LPARAM)font_list, 0); + ::ReleaseDC(NULL, hdc); + + return font_list; +} + +void FontSettingsUtilities::ValidateSavedFonts(PrefService* prefs) { + // nothing to do for Windows. +} + diff --git a/chrome/browser/resources/options/font_settings_overlay.html b/chrome/browser/resources/options/font_settings_overlay.html index 3edc53e..4a6757c 100644 --- a/chrome/browser/resources/options/font_settings_overlay.html +++ b/chrome/browser/resources/options/font_settings_overlay.html @@ -1,3 +1,61 @@ <div class="page hidden" id="fontSettingsOverlay"> <h1 i18n-content="fontSettingsTitle"></h1> + <section> + <h3 i18n-content="fontSettingsFontTitle"></h3> + <div> + <div> + <label style="display:inline;"> + <span i18n-content="fontSettingsSerifLabel"></span> + <select id="fontSettingsSerifSelector" + pref="webkit.webprefs.serif_font_family"></select> + </label> + <label style="display:inline;"> + <span i18n-content="fontSettingsSizeLabel"></span> + <select id="fontSettingsSerifSizeSelector" + pref="webkit.webprefs.default_font_size"></select> + </label> + <div id="fontSettingsSerifPreview"></div> + </div> + <div> + <label style="display:inline;"> + <span i18n-content="fontSettingsSansSerifLabel"></span> + <select id="fontSettingsSansSerifSelector" + pref="webkit.webprefs.sansserif_font_family"></select> + </label> + <label style="display:inline;"> + <span i18n-content="fontSettingsSizeLabel"></span> + <select id="fontSettingsSansSerifSizeSelector" + pref="webkit.webprefs.default_font_size"></select> + </label> + <div id="fontSettingsSansSerifPreview"></div> + </div> + <div> + <label style="display:inline;"> + <span i18n-content="fontSettingsFixedWidthLabel"></span> + <select id="fontSettingsFixedWidthSelector" + pref="webkit.webprefs.fixed_font_family"></select> + </label> + <label style="display:inline;"> + <span i18n-content="fontSettingsSizeLabel"></span> + <select id="fontSettingsFixedWidthSizeSelector" + pref="webkit.webprefs.default_fixed_font_size"></select> + </label> + <div id="fontSettingsFixedWidthPreview"></div> + </div> + </div> + </section> + <section> + <h3 i18n-content="fontSettingsEncodingTitle"></h3> + <div> + <label style="display:inline;"> + <span i18n-content="fontSettingsEncodingLabel"></span> + <select id="fontSettingsEncodingSelector" + pref="intl.charset_default"></select> + </label> + </div> + </section> + <div> + <button id="fontSettingsDismissButton" i18n-content="close" + onclick="OptionsPage.clearOverlays();"></button> + </div> </div> diff --git a/chrome/browser/resources/options/font_settings_overlay.js b/chrome/browser/resources/options/font_settings_overlay.js index 4ffadfa..af445dc 100644 --- a/chrome/browser/resources/options/font_settings_overlay.js +++ b/chrome/browser/resources/options/font_settings_overlay.js @@ -30,10 +30,43 @@ cr.define('options', function() { // Call base class implementation to starts preference initialization. OptionsPage.prototype.initializePage.call(this); - // TODO(csilv): add any initialization here or delete method and/or class. + // Initialize values for selector controls. + $('fontSettingsSerifSelector').initializeValues( + templateData.fontSettingsFontList) + $('fontSettingsSerifSizeSelector').initializeValues( + templateData.fontSettingsFontSizeList) + $('fontSettingsSansSerifSelector').initializeValues( + templateData.fontSettingsFontList) + $('fontSettingsSansSerifSizeSelector').initializeValues( + templateData.fontSettingsFontSizeList) + $('fontSettingsFixedWidthSelector').initializeValues( + templateData.fontSettingsFontList) + $('fontSettingsFixedWidthSizeSelector').initializeValues( + templateData.fontSettingsFontSizeList) + $('fontSettingsEncodingSelector').initializeValues( + templateData.fontSettingsEncodingList) } }; + FontSettingsOverlay.setupFontPreview = function(id, font, size) { + $(id).textContent = font + " " + size; + $(id).style.fontFamily = font; + $(id).style.fontSize = size + "pt"; + } + + // Chrome callbacks + FontSettingsOverlay.setupSerifFontPreview = function(text, size) { + this.setupFontPreview('fontSettingsSerifPreview', text, size); + } + + FontSettingsOverlay.setupSansSerifFontPreview = function(text, size) { + this.setupFontPreview('fontSettingsSansSerifPreview', text, size); + } + + FontSettingsOverlay.setupFixedFontPreview = function(text, size) { + this.setupFontPreview('fontSettingsFixedWidthPreview', text, size); + } + // Export return { FontSettingsOverlay: FontSettingsOverlay diff --git a/chrome/browser/resources/options/pref_ui.js b/chrome/browser/resources/options/pref_ui.js index b815523..49e1e34 100644 --- a/chrome/browser/resources/options/pref_ui.js +++ b/chrome/browser/resources/options/pref_ui.js @@ -191,7 +191,8 @@ cr.define('options', function() { return; } } - self.selectedIndex = -1; + // Item not found, select first item. + self.selectedIndex = 0; }); // Listen to user events. diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index a6d6afe..b6e9d90 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1138,6 +1138,10 @@ 'browser/dom_ui/core_options_handler.h', 'browser/dom_ui/font_settings_handler.cc', 'browser/dom_ui/font_settings_handler.h', + 'browser/dom_ui/font_settings_utils.h', + 'browser/dom_ui/font_settings_utils_gtk.cc', + 'browser/dom_ui/font_settings_utils_mac.mm', + 'browser/dom_ui/font_settings_utils_win.cc', 'browser/dom_ui/passwords_exceptions_handler.cc', 'browser/dom_ui/passwords_exceptions_handler.h', 'browser/dom_ui/personal_options_handler.cc', |