Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: chrome/browser/ui/webui/options2/font_settings_handler.cc

Issue 10837331: Options: s/options2/options/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: wut Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/ui/webui/options2/font_settings_handler.h"
6
7 #include <string>
8
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/i18n/rtl.h"
13 #include "base/string_number_conversions.h"
14 #include "base/string_util.h"
15 #include "base/values.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/character_encoding.h"
18 #include "chrome/browser/prefs/pref_service.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/webui/options2/font_settings_utils.h"
21 #include "chrome/common/chrome_notification_types.h"
22 #include "chrome/common/pref_names.h"
23 #include "content/public/browser/font_list_async.h"
24 #include "content/public/browser/notification_details.h"
25 #include "content/public/browser/web_ui.h"
26 #include "grit/chromium_strings.h"
27 #include "grit/generated_resources.h"
28 #include "ui/base/l10n/l10n_util.h"
29
30 #if defined(OS_WIN)
31 #include "ui/gfx/font.h"
32 #include "ui/gfx/platform_font_win.h"
33 #endif
34
35 namespace {
36
37 // Returns the localized name of a font so that settings can find it within the
38 // list of system fonts. On Windows, the list of system fonts has names only
39 // for the system locale, but the pref value may be in the English name.
40 std::string MaybeGetLocalizedFontName(const std::string& font_name) {
41 #if defined(OS_WIN)
42 gfx::Font font(font_name, 12); // dummy font size
43 return static_cast<gfx::PlatformFontWin*>(font.platform_font())->
44 GetLocalizedFontName();
45 #else
46 return font_name;
47 #endif
48 }
49
50 } // namespace
51
52
53 namespace options {
54
55 FontSettingsHandler::FontSettingsHandler() {
56 }
57
58 FontSettingsHandler::~FontSettingsHandler() {
59 }
60
61 void FontSettingsHandler::GetLocalizedValues(
62 DictionaryValue* localized_strings) {
63 DCHECK(localized_strings);
64
65 static OptionsStringResource resources[] = {
66 { "fontSettingsStandard",
67 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_STANDARD_LABEL },
68 { "fontSettingsSerif",
69 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SERIF_LABEL },
70 { "fontSettingsSansSerif",
71 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SANS_SERIF_LABEL },
72 { "fontSettingsFixedWidth",
73 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_FIXED_WIDTH_LABEL },
74 { "fontSettingsMinimumSize",
75 IDS_FONT_LANGUAGE_SETTING_MINIMUM_FONT_SIZE_TITLE },
76 { "fontSettingsEncoding",
77 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE },
78 { "fontSettingsSizeTiny",
79 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_TINY },
80 { "fontSettingsSizeHuge",
81 IDS_FONT_LANGUAGE_SETTING_FONT_SIZE_HUGE },
82 { "fontSettingsLoremIpsum",
83 IDS_FONT_LANGUAGE_SETTING_LOREM_IPSUM },
84 };
85
86 RegisterStrings(localized_strings, resources, arraysize(resources));
87 RegisterTitle(localized_strings, "fontSettingsPage",
88 IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE);
89 localized_strings->SetString("fontSettingsPlaceholder",
90 l10n_util::GetStringUTF16(
91 IDS_FONT_LANGUAGE_SETTING_PLACEHOLDER));
92 }
93
94 void FontSettingsHandler::InitializePage() {
95 DCHECK(web_ui());
96 SetUpStandardFontSample();
97 SetUpSerifFontSample();
98 SetUpSansSerifFontSample();
99 SetUpFixedFontSample();
100 SetUpMinimumFontSample();
101 }
102
103 void FontSettingsHandler::RegisterMessages() {
104 // Perform validation for saved fonts.
105 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
106 FontSettingsUtilities::ValidateSavedFonts(pref_service);
107
108 // Register for preferences that we need to observe manually.
109 standard_font_.Init(prefs::kWebKitStandardFontFamily, pref_service, this);
110 serif_font_.Init(prefs::kWebKitSerifFontFamily, pref_service, this);
111 sans_serif_font_.Init(prefs::kWebKitSansSerifFontFamily, pref_service, this);
112 fixed_font_.Init(prefs::kWebKitFixedFontFamily, pref_service, this);
113 font_encoding_.Init(prefs::kDefaultCharset, pref_service, this);
114 default_font_size_.Init(prefs::kWebKitDefaultFontSize, pref_service, this);
115 default_fixed_font_size_.Init(prefs::kWebKitDefaultFixedFontSize,
116 pref_service, this);
117 minimum_font_size_.Init(prefs::kWebKitMinimumFontSize, pref_service, this);
118
119 web_ui()->RegisterMessageCallback("fetchFontsData",
120 base::Bind(&FontSettingsHandler::HandleFetchFontsData,
121 base::Unretained(this)));
122 }
123
124 void FontSettingsHandler::HandleFetchFontsData(const ListValue* args) {
125 content::GetFontListAsync(
126 base::Bind(&FontSettingsHandler::FontsListHasLoaded,
127 base::Unretained(this)));
128 }
129
130 void FontSettingsHandler::FontsListHasLoaded(
131 scoped_ptr<base::ListValue> list) {
132 // Selects the directionality for the fonts in the given list.
133 for (size_t i = 0; i < list->GetSize(); i++) {
134 ListValue* font;
135 bool has_font = list->GetList(i, &font);
136 DCHECK(has_font);
137 string16 value;
138 bool has_value = font->GetString(1, &value);
139 DCHECK(has_value);
140 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(value);
141 font->Append(Value::CreateStringValue(has_rtl_chars ? "rtl" : "ltr"));
142 }
143
144 ListValue encoding_list;
145 const std::vector<CharacterEncoding::EncodingInfo>* encodings;
146 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
147 encodings = CharacterEncoding::GetCurrentDisplayEncodings(
148 g_browser_process->GetApplicationLocale(),
149 pref_service->GetString(prefs::kStaticEncodings),
150 pref_service->GetString(prefs::kRecentlySelectedEncoding));
151 DCHECK(encodings);
152 DCHECK(!encodings->empty());
153
154 std::vector<CharacterEncoding::EncodingInfo>::const_iterator it;
155 for (it = encodings->begin(); it != encodings->end(); ++it) {
156 ListValue* option = new ListValue();
157 if (it->encoding_id) {
158 int cmd_id = it->encoding_id;
159 std::string encoding =
160 CharacterEncoding::GetCanonicalEncodingNameByCommandId(cmd_id);
161 string16 name = it->encoding_display_name;
162 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(name);
163 option->Append(Value::CreateStringValue(encoding));
164 option->Append(Value::CreateStringValue(name));
165 option->Append(Value::CreateStringValue(has_rtl_chars ? "rtl" : "ltr"));
166 } else {
167 // Add empty name/value to indicate a separator item.
168 option->Append(Value::CreateStringValue(""));
169 option->Append(Value::CreateStringValue(""));
170 }
171 encoding_list.Append(option);
172 }
173
174 ListValue selected_values;
175 selected_values.Append(Value::CreateStringValue(MaybeGetLocalizedFontName(
176 standard_font_.GetValue())));
177 selected_values.Append(Value::CreateStringValue(MaybeGetLocalizedFontName(
178 serif_font_.GetValue())));
179 selected_values.Append(Value::CreateStringValue(MaybeGetLocalizedFontName(
180 sans_serif_font_.GetValue())));
181 selected_values.Append(Value::CreateStringValue(MaybeGetLocalizedFontName(
182 fixed_font_.GetValue())));
183 selected_values.Append(Value::CreateStringValue(font_encoding_.GetValue()));
184
185 web_ui()->CallJavascriptFunction("FontSettings.setFontsData",
186 *list.get(), encoding_list,
187 selected_values);
188 }
189
190 void FontSettingsHandler::Observe(int type,
191 const content::NotificationSource& source,
192 const content::NotificationDetails& details) {
193 if (type == chrome::NOTIFICATION_PREF_CHANGED) {
194 std::string* pref_name = content::Details<std::string>(details).ptr();
195 if (*pref_name == prefs::kWebKitStandardFontFamily) {
196 SetUpStandardFontSample();
197 } else if (*pref_name == prefs::kWebKitSerifFontFamily) {
198 SetUpSerifFontSample();
199 } else if (*pref_name == prefs::kWebKitSansSerifFontFamily) {
200 SetUpSansSerifFontSample();
201 } else if (*pref_name == prefs::kWebKitFixedFontFamily ||
202 *pref_name == prefs::kWebKitDefaultFixedFontSize) {
203 SetUpFixedFontSample();
204 } else if (*pref_name == prefs::kWebKitDefaultFontSize) {
205 SetUpStandardFontSample();
206 SetUpSerifFontSample();
207 SetUpSansSerifFontSample();
208 } else if (*pref_name == prefs::kWebKitMinimumFontSize) {
209 SetUpMinimumFontSample();
210 }
211 }
212 }
213
214 void FontSettingsHandler::SetUpStandardFontSample() {
215 base::StringValue font_value(standard_font_.GetValue());
216 base::FundamentalValue size_value(default_font_size_.GetValue());
217 web_ui()->CallJavascriptFunction(
218 "FontSettings.setUpStandardFontSample", font_value, size_value);
219 }
220
221 void FontSettingsHandler::SetUpSerifFontSample() {
222 base::StringValue font_value(serif_font_.GetValue());
223 base::FundamentalValue size_value(default_font_size_.GetValue());
224 web_ui()->CallJavascriptFunction(
225 "FontSettings.setUpSerifFontSample", font_value, size_value);
226 }
227
228 void FontSettingsHandler::SetUpSansSerifFontSample() {
229 base::StringValue font_value(sans_serif_font_.GetValue());
230 base::FundamentalValue size_value(default_font_size_.GetValue());
231 web_ui()->CallJavascriptFunction(
232 "FontSettings.setUpSansSerifFontSample", font_value, size_value);
233 }
234
235 void FontSettingsHandler::SetUpFixedFontSample() {
236 base::StringValue font_value(fixed_font_.GetValue());
237 base::FundamentalValue size_value(default_fixed_font_size_.GetValue());
238 web_ui()->CallJavascriptFunction(
239 "FontSettings.setUpFixedFontSample", font_value, size_value);
240 }
241
242 void FontSettingsHandler::SetUpMinimumFontSample() {
243 base::FundamentalValue size_value(minimum_font_size_.GetValue());
244 web_ui()->CallJavascriptFunction("FontSettings.setUpMinimumFontSample",
245 size_value);
246 }
247
248 } // namespace options
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/options2/font_settings_handler.h ('k') | chrome/browser/ui/webui/options2/font_settings_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698