| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/dom_ui/options/font_settings_handler.h" | 5 #include "chrome/browser/dom_ui/options/font_settings_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/character_encoding.h" | 14 #include "chrome/browser/character_encoding.h" |
| 15 #include "chrome/browser/dom_ui/options/dom_options_util.h" |
| 15 #include "chrome/browser/dom_ui/options/font_settings_utils.h" | 16 #include "chrome/browser/dom_ui/options/font_settings_utils.h" |
| 16 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/common/notification_details.h" | 19 #include "chrome/common/notification_details.h" |
| 19 #include "chrome/common/notification_type.h" | 20 #include "chrome/common/notification_type.h" |
| 20 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 21 #include "grit/chromium_strings.h" | 22 #include "grit/chromium_strings.h" |
| 22 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 23 | 24 |
| 24 FontSettingsHandler::FontSettingsHandler() { | 25 FontSettingsHandler::FontSettingsHandler() { |
| 25 } | 26 } |
| 26 | 27 |
| 27 FontSettingsHandler::~FontSettingsHandler() { | 28 FontSettingsHandler::~FontSettingsHandler() { |
| 28 } | 29 } |
| 29 | 30 |
| 30 void FontSettingsHandler::GetLocalizedValues( | 31 void FontSettingsHandler::GetLocalizedValues( |
| 31 DictionaryValue* localized_strings) { | 32 DictionaryValue* localized_strings) { |
| 32 DCHECK(localized_strings); | 33 DCHECK(localized_strings); |
| 33 | 34 |
| 34 localized_strings->SetString("fontSettingsTitle", | 35 localized_strings->SetString("fontSettingsTitle", |
| 35 l10n_util::GetStringUTF16(IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE)); | 36 l10n_util::GetStringUTF16(IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE)); |
| 36 localized_strings->SetString("fontSettingsFontTitle", | 37 localized_strings->SetString("fontSettingsStandard", |
| 37 l10n_util::GetStringUTF16( | 38 l10n_util::GetStringUTF16( |
| 38 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_FONT_TITLE)); | 39 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_FONT_TITLE)); |
| 40 localized_strings->SetString("fontSettingsFixedWidth", |
| 41 dom_options_util::StripColon( |
| 42 l10n_util::GetStringUTF16( |
| 43 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_FIXED_WIDTH_LABEL))); |
| 44 localized_strings->SetString("fontSettingsMinimumSize", |
| 45 l10n_util::GetStringUTF16( |
| 46 IDS_FONT_LANGUAGE_SETTING_MINIMUM_FONT_SIZE_TITLE)); |
| 47 localized_strings->SetString("fontSettingsEncoding", |
| 48 l10n_util::GetStringUTF16( |
| 49 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE)); |
| 39 localized_strings->SetString("fontSettingsSerifLabel", | 50 localized_strings->SetString("fontSettingsSerifLabel", |
| 40 l10n_util::GetStringUTF16( | 51 l10n_util::GetStringUTF16( |
| 41 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SERIF_LABEL)); | 52 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SERIF_LABEL)); |
| 42 localized_strings->SetString("fontSettingsSansSerifLabel", | 53 localized_strings->SetString("fontSettingsSansSerifLabel", |
| 43 l10n_util::GetStringUTF16( | 54 l10n_util::GetStringUTF16( |
| 44 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SANS_SERIF_LABEL)); | 55 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SANS_SERIF_LABEL)); |
| 45 localized_strings->SetString("fontSettingsFixedWidthLabel", | |
| 46 l10n_util::GetStringUTF16( | |
| 47 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_FIXED_WIDTH_LABEL)); | |
| 48 localized_strings->SetString("fontSettingsSizeLabel", | 56 localized_strings->SetString("fontSettingsSizeLabel", |
| 49 l10n_util::GetStringUTF16( | 57 l10n_util::GetStringUTF16( |
| 50 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_SELECTOR_LABEL)); | 58 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_SELECTOR_LABEL)); |
| 51 | 59 localized_strings->SetString("fontSettingsSizeTiny", |
| 52 localized_strings->SetString("fontSettingsMinimumSizeTitle", | |
| 53 l10n_util::GetStringUTF16( | 60 l10n_util::GetStringUTF16( |
| 54 IDS_FONT_LANGUAGE_SETTING_MINIMUM_FONT_SIZE_TITLE)); | 61 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_TINY)); |
| 55 | 62 localized_strings->SetString("fontSettingsSizeHuge", |
| 56 localized_strings->SetString("fontSettingsEncodingTitle", | |
| 57 l10n_util::GetStringUTF16( | 63 l10n_util::GetStringUTF16( |
| 58 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE)); | 64 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_HUGE)); |
| 59 localized_strings->SetString("fontSettingsEncodingLabel", | 65 localized_strings->SetString("fontSettingsEncodingLabel", |
| 60 l10n_util::GetStringUTF16( | 66 l10n_util::GetStringUTF16( |
| 61 IDS_FONT_LANGUAGE_SETTING_FONT_DEFAULT_ENCODING_SELECTOR_LABEL)); | 67 IDS_FONT_LANGUAGE_SETTING_FONT_DEFAULT_ENCODING_SELECTOR_LABEL)); |
| 68 localized_strings->SetString("fontSettingsLoremIpsum", |
| 69 l10n_util::GetStringUTF16( |
| 70 IDS_FONT_LANGUAGE_SETTING_LOREM_IPSUM)); |
| 62 | 71 |
| 63 // Fonts | 72 // Fonts |
| 64 ListValue* font_list = FontSettingsUtilities::GetFontsList(); | 73 ListValue* font_list = FontSettingsUtilities::GetFontsList(); |
| 65 if (font_list) localized_strings->Set("fontSettingsFontList", font_list); | 74 if (font_list) localized_strings->Set("fontSettingsFontList", font_list); |
| 66 | 75 |
| 67 // Font sizes | 76 // Font sizes |
| 68 int font_sizes[] = { 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, | 77 int font_sizes[] = { 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, |
| 69 28, 30, 32, 34, 36, 40, 44, 48, 56, 64, 72 }; | 78 28, 30, 32, 34, 36, 40, 44, 48, 56, 64, 72 }; |
| 70 int count = arraysize(font_sizes); | 79 int count = arraysize(font_sizes); |
| 71 ListValue* font_size_list = new ListValue; | 80 ListValue* font_size_list = new ListValue; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 119 |
| 111 ListValue* option = new ListValue(); | 120 ListValue* option = new ListValue(); |
| 112 option->Append(Value::CreateStringValue(encoding)); | 121 option->Append(Value::CreateStringValue(encoding)); |
| 113 option->Append(Value::CreateStringValue(name)); | 122 option->Append(Value::CreateStringValue(name)); |
| 114 encoding_list->Append(option); | 123 encoding_list->Append(option); |
| 115 } | 124 } |
| 116 localized_strings->Set("fontSettingsEncodingList", encoding_list); | 125 localized_strings->Set("fontSettingsEncodingList", encoding_list); |
| 117 } | 126 } |
| 118 | 127 |
| 119 void FontSettingsHandler::Initialize() { | 128 void FontSettingsHandler::Initialize() { |
| 120 SetupSerifFontPreview(); | 129 SetupSerifFontSample(); |
| 121 SetupSansSerifFontPreview(); | 130 SetupMinimumFontSample(); |
| 122 SetupFixedFontPreview(); | 131 SetupFixedFontSample(); |
| 123 } | 132 } |
| 124 | 133 |
| 125 DOMMessageHandler* FontSettingsHandler::Attach(DOMUI* dom_ui) { | 134 DOMMessageHandler* FontSettingsHandler::Attach(DOMUI* dom_ui) { |
| 126 // Call through to superclass. | 135 // Call through to superclass. |
| 127 DOMMessageHandler* handler = OptionsPageUIHandler::Attach(dom_ui); | 136 DOMMessageHandler* handler = OptionsPageUIHandler::Attach(dom_ui); |
| 128 | 137 |
| 129 // Perform validation for saved fonts. | 138 // Perform validation for saved fonts. |
| 130 DCHECK(dom_ui_); | 139 DCHECK(dom_ui_); |
| 131 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 140 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
| 132 FontSettingsUtilities::ValidateSavedFonts(pref_service); | 141 FontSettingsUtilities::ValidateSavedFonts(pref_service); |
| 133 | 142 |
| 134 // Register for preferences that we need to observe manually. | 143 // Register for preferences that we need to observe manually. |
| 135 serif_font_.Init(prefs::kWebKitSerifFontFamily, pref_service, this); | 144 serif_font_.Init(prefs::kWebKitSerifFontFamily, pref_service, this); |
| 136 sans_serif_font_.Init(prefs::kWebKitSansSerifFontFamily, pref_service, this); | |
| 137 fixed_font_.Init(prefs::kWebKitFixedFontFamily, pref_service, this); | 145 fixed_font_.Init(prefs::kWebKitFixedFontFamily, pref_service, this); |
| 138 default_font_size_.Init(prefs::kWebKitDefaultFontSize, pref_service, this); | 146 default_font_size_.Init(prefs::kWebKitDefaultFontSize, pref_service, this); |
| 139 default_fixed_font_size_.Init(prefs::kWebKitDefaultFixedFontSize, | 147 default_fixed_font_size_.Init(prefs::kWebKitDefaultFixedFontSize, |
| 140 pref_service, this); | 148 pref_service, this); |
| 149 minimum_font_size_.Init(prefs::kWebKitMinimumFontSize, pref_service, this); |
| 141 | 150 |
| 142 // Return result from the superclass. | 151 // Return result from the superclass. |
| 143 return handler; | 152 return handler; |
| 144 } | 153 } |
| 145 | 154 |
| 146 void FontSettingsHandler::Observe(NotificationType type, | 155 void FontSettingsHandler::Observe(NotificationType type, |
| 147 const NotificationSource& source, | 156 const NotificationSource& source, |
| 148 const NotificationDetails& details) { | 157 const NotificationDetails& details) { |
| 149 if (type == NotificationType::PREF_CHANGED) { | 158 if (type == NotificationType::PREF_CHANGED) { |
| 150 std::string* pref_name = Details<std::string>(details).ptr(); | 159 std::string* pref_name = Details<std::string>(details).ptr(); |
| 151 if (*pref_name == prefs::kWebKitSerifFontFamily || | 160 if (*pref_name == prefs::kWebKitSerifFontFamily || |
| 152 *pref_name == prefs::kWebKitDefaultFontSize) { | 161 *pref_name == prefs::kWebKitDefaultFontSize) { |
| 153 SetupSerifFontPreview(); | 162 SetupSerifFontSample(); |
| 154 } else if (*pref_name == prefs::kWebKitSansSerifFontFamily || | |
| 155 *pref_name == prefs::kWebKitDefaultFontSize) { | |
| 156 SetupSansSerifFontPreview(); | |
| 157 } else if (*pref_name == prefs::kWebKitFixedFontFamily || | 163 } else if (*pref_name == prefs::kWebKitFixedFontFamily || |
| 158 *pref_name == prefs::kWebKitDefaultFixedFontSize) { | 164 *pref_name == prefs::kWebKitDefaultFixedFontSize) { |
| 159 SetupFixedFontPreview(); | 165 SetupFixedFontSample(); |
| 166 } else if (*pref_name == prefs::kWebKitMinimumFontSize) { |
| 167 SetupMinimumFontSample(); |
| 160 } | 168 } |
| 161 } | 169 } |
| 162 } | 170 } |
| 163 | 171 |
| 164 void FontSettingsHandler::SetupSerifFontPreview() { | 172 void FontSettingsHandler::SetupSerifFontSample() { |
| 165 DCHECK(dom_ui_); | 173 DCHECK(dom_ui_); |
| 166 StringValue font_value(serif_font_.GetValue()); | 174 StringValue font_value(serif_font_.GetValue()); |
| 167 FundamentalValue size_value(default_font_size_.GetValue()); | 175 FundamentalValue size_value(default_font_size_.GetValue()); |
| 168 dom_ui_->CallJavascriptFunction( | 176 dom_ui_->CallJavascriptFunction( |
| 169 L"FontSettings.setupSerifFontPreview", font_value, size_value); | 177 L"FontSettings.setupSerifFontSample", font_value, size_value); |
| 170 } | 178 } |
| 171 | 179 |
| 172 void FontSettingsHandler::SetupSansSerifFontPreview() { | 180 void FontSettingsHandler::SetupFixedFontSample() { |
| 173 DCHECK(dom_ui_); | |
| 174 StringValue font_value(sans_serif_font_.GetValue()); | |
| 175 FundamentalValue size_value(default_font_size_.GetValue()); | |
| 176 dom_ui_->CallJavascriptFunction( | |
| 177 L"FontSettings.setupSansSerifFontPreview", font_value, size_value); | |
| 178 } | |
| 179 | |
| 180 void FontSettingsHandler::SetupFixedFontPreview() { | |
| 181 DCHECK(dom_ui_); | 181 DCHECK(dom_ui_); |
| 182 StringValue font_value(fixed_font_.GetValue()); | 182 StringValue font_value(fixed_font_.GetValue()); |
| 183 FundamentalValue size_value(default_fixed_font_size_.GetValue()); | 183 FundamentalValue size_value(default_fixed_font_size_.GetValue()); |
| 184 dom_ui_->CallJavascriptFunction( | 184 dom_ui_->CallJavascriptFunction( |
| 185 L"FontSettings.setupFixedFontPreview", font_value, size_value); | 185 L"FontSettings.setupFixedFontSample", font_value, size_value); |
| 186 } | 186 } |
| 187 |
| 188 void FontSettingsHandler::SetupMinimumFontSample() { |
| 189 DCHECK(dom_ui_); |
| 190 FundamentalValue size_value(minimum_font_size_.GetValue()); |
| 191 dom_ui_->CallJavascriptFunction( |
| 192 L"FontSettings.setupMinimumFontSample", size_value); |
| 193 } |
| OLD | NEW |