| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/views/options/fonts_page_view.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 #include <shlobj.h> | |
| 9 #include <vsstyle.h> | |
| 10 #include <vssym32.h> | |
| 11 | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/file_util.h" | |
| 15 #include "base/string_util.h" | |
| 16 #include "base/utf_string_conversions.h" | |
| 17 #include "chrome/browser/default_encoding_combo_model.h" | |
| 18 #include "chrome/browser/prefs/pref_service.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | |
| 20 #include "chrome/common/pref_names.h" | |
| 21 #include "grit/generated_resources.h" | |
| 22 #include "grit/theme_resources.h" | |
| 23 #include "grit/locale_settings.h" | |
| 24 #include "third_party/skia/include/core/SkBitmap.h" | |
| 25 #include "ui/base/l10n/l10n_util.h" | |
| 26 #include "ui/base/resource/resource_bundle.h" | |
| 27 #include "ui/gfx/canvas_skia.h" | |
| 28 #include "ui/gfx/font.h" | |
| 29 #include "ui/gfx/native_theme_win.h" | |
| 30 #include "views/controls/button/native_button.h" | |
| 31 #include "views/layout/grid_layout.h" | |
| 32 #include "views/layout/layout_constants.h" | |
| 33 #include "views/widget/widget.h" | |
| 34 | |
| 35 //////////////////////////////////////////////////////////////////////////////// | |
| 36 // FontDisplayView | |
| 37 | |
| 38 class FontDisplayView : public views::View { | |
| 39 public: | |
| 40 FontDisplayView(); | |
| 41 virtual ~FontDisplayView(); | |
| 42 | |
| 43 // This method takes in font size in pixel units, instead of the normal point | |
| 44 // unit because users expect the font size number to represent pixels and not | |
| 45 // points. | |
| 46 void SetFontType(const std::wstring& font_name, | |
| 47 int font_size); | |
| 48 | |
| 49 std::wstring font_name() { return font_name_; } | |
| 50 int font_size() { return font_size_; } | |
| 51 | |
| 52 // views::View overrides: | |
| 53 virtual void OnPaint(gfx::Canvas* canvas); | |
| 54 virtual void Layout(); | |
| 55 virtual gfx::Size GetPreferredSize(); | |
| 56 | |
| 57 private: | |
| 58 views::Label* font_text_label_; | |
| 59 std::wstring font_name_; | |
| 60 int font_size_; | |
| 61 std::wstring font_text_label_string_; | |
| 62 | |
| 63 static const int kFontDisplayMaxWidthChars = 50; | |
| 64 static const int kFontDisplayMaxHeightChars = 1; | |
| 65 static const int kFontDisplayLabelPadding = 5; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(FontDisplayView); | |
| 68 }; | |
| 69 | |
| 70 FontDisplayView::FontDisplayView() | |
| 71 : font_text_label_(new views::Label) { | |
| 72 AddChildView(font_text_label_); | |
| 73 } | |
| 74 | |
| 75 FontDisplayView::~FontDisplayView() { | |
| 76 } | |
| 77 | |
| 78 void FontDisplayView::SetFontType(const std::wstring& font_name, | |
| 79 int font_size) { | |
| 80 if (font_name.empty()) | |
| 81 return; | |
| 82 | |
| 83 font_name_ = font_name; | |
| 84 font_size_ = font_size; | |
| 85 std::wstring displayed_text = font_name_; | |
| 86 | |
| 87 // Append the font type and size. | |
| 88 displayed_text += L", "; | |
| 89 displayed_text += UTF8ToWide(::StringPrintf("%d", font_size_)); | |
| 90 HDC hdc = GetDC(NULL); | |
| 91 int font_size_point = MulDiv(font_size, 72, GetDeviceCaps(hdc, LOGPIXELSY)); | |
| 92 gfx::Font font = gfx::Font(font_name, font_size_point); | |
| 93 font_text_label_->SetFont(font); | |
| 94 font_text_label_->SetText(displayed_text); | |
| 95 } | |
| 96 | |
| 97 void FontDisplayView::OnPaint(gfx::Canvas* canvas) { | |
| 98 HDC dc = canvas->BeginPlatformPaint(); | |
| 99 RECT rect = { 0, 0, width(), height() }; | |
| 100 gfx::NativeTheme::instance()->PaintTextField( | |
| 101 dc, EP_BACKGROUND, EBS_NORMAL, 0, &rect, ::GetSysColor(COLOR_3DFACE), | |
| 102 true, true); | |
| 103 canvas->EndPlatformPaint(); | |
| 104 } | |
| 105 | |
| 106 void FontDisplayView::Layout() { | |
| 107 font_text_label_->SetBounds(0, 0, width(), height()); | |
| 108 } | |
| 109 | |
| 110 gfx::Size FontDisplayView::GetPreferredSize() { | |
| 111 gfx::Size size = font_text_label_->GetPreferredSize(); | |
| 112 size.set_width(size.width() + 2 * kFontDisplayLabelPadding); | |
| 113 size.set_height(size.height() + 2 * kFontDisplayLabelPadding); | |
| 114 return size; | |
| 115 } | |
| 116 | |
| 117 void EmbellishTitle(views::Label* title_label) { | |
| 118 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 119 gfx::Font title_font = | |
| 120 rb.GetFont(ResourceBundle::BaseFont).DeriveFont(0, gfx::Font::BOLD); | |
| 121 title_label->SetFont(title_font); | |
| 122 SkColor title_color = | |
| 123 gfx::NativeTheme::instance()->GetThemeColorWithDefault( | |
| 124 gfx::NativeTheme::BUTTON, BP_GROUPBOX, GBS_NORMAL, TMT_TEXTCOLOR, | |
| 125 COLOR_WINDOWTEXT); | |
| 126 title_label->SetColor(title_color); | |
| 127 } | |
| 128 | |
| 129 FontsPageView::FontsPageView(Profile* profile) | |
| 130 : ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 131 select_font_dialog_(SelectFontDialog::Create(this))), | |
| 132 fonts_group_title_(NULL), | |
| 133 encoding_group_title_(NULL), | |
| 134 fixed_width_font_change_page_button_(NULL), | |
| 135 serif_font_change_page_button_(NULL), | |
| 136 sans_serif_font_change_page_button_(NULL), | |
| 137 fixed_width_font_label_(NULL), | |
| 138 serif_font_label_(NULL), | |
| 139 sans_serif_font_label_(NULL), | |
| 140 default_encoding_combobox_(NULL), | |
| 141 serif_button_pressed_(false), | |
| 142 sans_serif_button_pressed_(false), | |
| 143 fixed_width_button_pressed_(false), | |
| 144 encoding_dropdown_clicked_(false), | |
| 145 font_type_being_changed_(NONE), | |
| 146 OptionsPageView(profile), | |
| 147 font_changed_(false), | |
| 148 default_encoding_changed_(false), | |
| 149 serif_font_size_pixel_(0), | |
| 150 sans_serif_font_size_pixel_(0), | |
| 151 fixed_width_font_size_pixel_(0) { | |
| 152 serif_name_.Init(prefs::kWebKitSerifFontFamily, profile->GetPrefs(), NULL); | |
| 153 serif_size_.Init(prefs::kWebKitDefaultFontSize, profile->GetPrefs(), NULL); | |
| 154 | |
| 155 sans_serif_name_.Init(prefs::kWebKitSansSerifFontFamily, profile->GetPrefs(), | |
| 156 NULL); | |
| 157 sans_serif_size_.Init(prefs::kWebKitDefaultFontSize, profile->GetPrefs(), | |
| 158 NULL); | |
| 159 | |
| 160 fixed_width_name_.Init(prefs::kWebKitFixedFontFamily, profile->GetPrefs(), | |
| 161 NULL); | |
| 162 fixed_width_size_.Init(prefs::kWebKitDefaultFixedFontSize, | |
| 163 profile->GetPrefs(), NULL); | |
| 164 | |
| 165 default_encoding_.Init(prefs::kDefaultCharset, profile->GetPrefs(), NULL); | |
| 166 } | |
| 167 | |
| 168 FontsPageView::~FontsPageView() { | |
| 169 } | |
| 170 | |
| 171 void FontsPageView::ButtonPressed( | |
| 172 views::Button* sender, const views::Event& event) { | |
| 173 HWND owning_hwnd = GetAncestor(GetWidget()->GetNativeView(), GA_ROOT); | |
| 174 std::wstring font_name; | |
| 175 int font_size = 0; | |
| 176 if (sender == serif_font_change_page_button_) { | |
| 177 font_type_being_changed_ = SERIF; | |
| 178 font_name = serif_font_display_view_->font_name(); | |
| 179 font_size = serif_font_size_pixel_; | |
| 180 } else if (sender == sans_serif_font_change_page_button_) { | |
| 181 font_type_being_changed_ = SANS_SERIF; | |
| 182 font_name = sans_serif_font_display_view_->font_name(); | |
| 183 font_size = sans_serif_font_size_pixel_; | |
| 184 } else if (sender == fixed_width_font_change_page_button_) { | |
| 185 font_type_being_changed_ = FIXED_WIDTH; | |
| 186 font_name = fixed_width_font_display_view_->font_name(); | |
| 187 font_size = fixed_width_font_size_pixel_; | |
| 188 } else { | |
| 189 NOTREACHED(); | |
| 190 return; | |
| 191 } | |
| 192 | |
| 193 select_font_dialog_->SelectFont(owning_hwnd, NULL, font_name, font_size); | |
| 194 } | |
| 195 | |
| 196 void FontsPageView::ItemChanged(views::Combobox* combo_box, | |
| 197 int prev_index, int new_index) { | |
| 198 if (combo_box == default_encoding_combobox_) { | |
| 199 if (prev_index != new_index) { // Default-Encoding has been changed. | |
| 200 encoding_dropdown_clicked_ = true; | |
| 201 default_encoding_selected_ = default_encoding_combobox_model_-> | |
| 202 GetEncodingCharsetByIndex(new_index); | |
| 203 default_encoding_changed_ = true; | |
| 204 } | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 void FontsPageView::FontSelected(const gfx::Font& font, void* params) { | |
| 209 if (font.GetFontName().empty()) | |
| 210 return; | |
| 211 int font_size = font.GetFontSize(); | |
| 212 // Currently we do not have separate font sizes for Serif and Sans Serif. | |
| 213 // Therefore, when Serif font size is changed, Sans-Serif font size changes, | |
| 214 // and vice versa. | |
| 215 if (font_type_being_changed_ == SERIF) { | |
| 216 sans_serif_font_size_pixel_ = serif_font_size_pixel_ = font_size; | |
| 217 serif_font_display_view_->SetFontType( | |
| 218 font.GetFontName(), | |
| 219 serif_font_size_pixel_); | |
| 220 sans_serif_font_display_view_->SetFontType( | |
| 221 sans_serif_font_display_view_->font_name(), | |
| 222 sans_serif_font_size_pixel_); | |
| 223 } else if (font_type_being_changed_ == SANS_SERIF) { | |
| 224 sans_serif_font_size_pixel_ = serif_font_size_pixel_ = font_size; | |
| 225 sans_serif_font_display_view_->SetFontType( | |
| 226 font.GetFontName(), | |
| 227 sans_serif_font_size_pixel_); | |
| 228 serif_font_display_view_->SetFontType( | |
| 229 serif_font_display_view_->font_name(), | |
| 230 sans_serif_font_size_pixel_); | |
| 231 } else if (font_type_being_changed_ == FIXED_WIDTH) { | |
| 232 fixed_width_font_size_pixel_ = font_size; | |
| 233 fixed_width_font_display_view_->SetFontType(font.GetFontName(), font_size); | |
| 234 } | |
| 235 font_changed_ = true; | |
| 236 } | |
| 237 | |
| 238 void FontsPageView::SaveChanges() { | |
| 239 // Set Fonts. | |
| 240 if (font_changed_) { | |
| 241 serif_name_.SetValue(WideToUTF8(serif_font_display_view_->font_name())); | |
| 242 serif_size_.SetValue(serif_font_size_pixel_); | |
| 243 sans_serif_name_.SetValue( | |
| 244 WideToUTF8(sans_serif_font_display_view_->font_name())); | |
| 245 sans_serif_size_.SetValue(sans_serif_font_size_pixel_); | |
| 246 fixed_width_name_.SetValue(WideToUTF8( | |
| 247 fixed_width_font_display_view_->font_name())); | |
| 248 fixed_width_size_.SetValue(fixed_width_font_size_pixel_); | |
| 249 } | |
| 250 // Set Encoding. | |
| 251 if (default_encoding_changed_) | |
| 252 default_encoding_.SetValue(default_encoding_selected_); | |
| 253 } | |
| 254 | |
| 255 void FontsPageView::InitControlLayout() { | |
| 256 using views::GridLayout; | |
| 257 using views::ColumnSet; | |
| 258 | |
| 259 GridLayout* layout = GridLayout::CreatePanel(this); | |
| 260 SetLayoutManager(layout); | |
| 261 | |
| 262 const int single_column_view_set_id = 0; | |
| 263 ColumnSet* column_set = layout->AddColumnSet(single_column_view_set_id); | |
| 264 | |
| 265 // Fonts group. | |
| 266 column_set->AddColumn(GridLayout::FILL, GridLayout::LEADING, 1, | |
| 267 GridLayout::USE_PREF, 0, 0); | |
| 268 fonts_group_title_ = new views::Label( | |
| 269 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 270 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_FONT_TITLE))); | |
| 271 EmbellishTitle(fonts_group_title_); | |
| 272 fonts_group_title_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 273 layout->StartRow(0, single_column_view_set_id); | |
| 274 layout->AddView(fonts_group_title_); | |
| 275 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 276 layout->StartRow(0, single_column_view_set_id); | |
| 277 InitFontLayout(); | |
| 278 layout->AddView(fonts_contents_); | |
| 279 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); | |
| 280 | |
| 281 // Encoding group. | |
| 282 encoding_group_title_ = new views::Label( | |
| 283 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 284 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE))); | |
| 285 EmbellishTitle(encoding_group_title_); | |
| 286 encoding_group_title_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 287 layout->StartRow(0, single_column_view_set_id); | |
| 288 layout->AddView(encoding_group_title_); | |
| 289 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 290 layout->StartRow(0, single_column_view_set_id); | |
| 291 InitEncodingLayout(); | |
| 292 layout->AddView(encoding_contents_); | |
| 293 } | |
| 294 | |
| 295 void FontsPageView::NotifyPrefChanged(const std::string* pref_name) { | |
| 296 if (!pref_name || *pref_name == prefs::kWebKitFixedFontFamily) { | |
| 297 fixed_width_font_size_pixel_ = fixed_width_size_.GetValue(); | |
| 298 fixed_width_font_display_view_->SetFontType( | |
| 299 UTF8ToWide(fixed_width_name_.GetValue()), | |
| 300 fixed_width_font_size_pixel_); | |
| 301 } | |
| 302 if (!pref_name || *pref_name == prefs::kWebKitSerifFontFamily) { | |
| 303 serif_font_size_pixel_ = serif_size_.GetValue(); | |
| 304 serif_font_display_view_->SetFontType( | |
| 305 UTF8ToWide(serif_name_.GetValue()), | |
| 306 serif_font_size_pixel_); | |
| 307 } | |
| 308 if (!pref_name || *pref_name == prefs::kWebKitSansSerifFontFamily) { | |
| 309 sans_serif_font_size_pixel_ = sans_serif_size_.GetValue(); | |
| 310 sans_serif_font_display_view_->SetFontType( | |
| 311 UTF8ToWide(sans_serif_name_.GetValue()), | |
| 312 sans_serif_font_size_pixel_); | |
| 313 } | |
| 314 } | |
| 315 | |
| 316 void FontsPageView::InitFontLayout() { | |
| 317 // Fixed width. | |
| 318 fixed_width_font_display_view_ = new FontDisplayView; | |
| 319 fixed_width_font_change_page_button_ = new views::NativeButton( | |
| 320 this, | |
| 321 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 322 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_BUTTON_LABEL))); | |
| 323 | |
| 324 fixed_width_font_label_ = new views::Label( | |
| 325 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 326 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_FIXED_WIDTH_LABEL))); | |
| 327 fixed_width_font_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 328 | |
| 329 // Serif font. | |
| 330 serif_font_display_view_ = new FontDisplayView; | |
| 331 serif_font_change_page_button_ = new views::NativeButton( | |
| 332 this, | |
| 333 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 334 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_BUTTON_LABEL))); | |
| 335 | |
| 336 serif_font_label_ = new views::Label( | |
| 337 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 338 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SERIF_LABEL))); | |
| 339 serif_font_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 340 | |
| 341 // Sans Serif font. | |
| 342 sans_serif_font_display_view_ = new FontDisplayView; | |
| 343 sans_serif_font_change_page_button_ = new views::NativeButton( | |
| 344 this, | |
| 345 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 346 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_BUTTON_LABEL))); | |
| 347 | |
| 348 sans_serif_font_label_ = new views::Label( | |
| 349 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 350 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SANS_SERIF_LABEL))); | |
| 351 sans_serif_font_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
| 352 | |
| 353 // Now add the views. | |
| 354 using views::GridLayout; | |
| 355 using views::ColumnSet; | |
| 356 | |
| 357 fonts_contents_ = new views::View; | |
| 358 GridLayout* layout = new GridLayout(fonts_contents_); | |
| 359 fonts_contents_->SetLayoutManager(layout); | |
| 360 | |
| 361 const int triple_column_view_set_id = 0; | |
| 362 ColumnSet* column_set = layout->AddColumnSet(triple_column_view_set_id); | |
| 363 | |
| 364 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, | |
| 365 GridLayout::USE_PREF, 0, 0); | |
| 366 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | |
| 367 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, | |
| 368 GridLayout::USE_PREF, 0, 0); | |
| 369 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | |
| 370 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, | |
| 371 GridLayout::USE_PREF, 0, 0); | |
| 372 | |
| 373 // Serif font controls. | |
| 374 layout->StartRow(0, triple_column_view_set_id); | |
| 375 layout->AddView(serif_font_label_); | |
| 376 layout->AddView(serif_font_display_view_, 1, 1, | |
| 377 GridLayout::FILL, GridLayout::CENTER); | |
| 378 layout->AddView(serif_font_change_page_button_); | |
| 379 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 380 | |
| 381 // Sans serif font controls. | |
| 382 layout->StartRow(0, triple_column_view_set_id); | |
| 383 layout->AddView(sans_serif_font_label_); | |
| 384 layout->AddView(sans_serif_font_display_view_, 1, 1, | |
| 385 GridLayout::FILL, GridLayout::CENTER); | |
| 386 layout->AddView(sans_serif_font_change_page_button_); | |
| 387 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 388 | |
| 389 // Fixed-width font controls. | |
| 390 layout->StartRow(0, triple_column_view_set_id); | |
| 391 layout->AddView(fixed_width_font_label_); | |
| 392 layout->AddView(fixed_width_font_display_view_, 1, 1, | |
| 393 GridLayout::FILL, GridLayout::CENTER); | |
| 394 layout->AddView(fixed_width_font_change_page_button_); | |
| 395 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 396 } | |
| 397 | |
| 398 void FontsPageView::InitEncodingLayout() { | |
| 399 default_encoding_combobox_label_ = new views::Label( | |
| 400 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 401 IDS_FONT_LANGUAGE_SETTING_FONT_DEFAULT_ENCODING_SELECTOR_LABEL))); | |
| 402 default_encoding_combobox_model_.reset(new DefaultEncodingComboboxModel); | |
| 403 default_encoding_combobox_ = new views::Combobox( | |
| 404 default_encoding_combobox_model_.get()); | |
| 405 int selected_encoding_index = default_encoding_combobox_model_-> | |
| 406 GetSelectedEncodingIndex(profile()); | |
| 407 default_encoding_combobox_->SetSelectedItem(selected_encoding_index); | |
| 408 default_encoding_selected_ = default_encoding_combobox_model_-> | |
| 409 GetEncodingCharsetByIndex(selected_encoding_index); | |
| 410 default_encoding_combobox_->set_listener(this); | |
| 411 | |
| 412 // Now add the views. | |
| 413 using views::GridLayout; | |
| 414 using views::ColumnSet; | |
| 415 | |
| 416 encoding_contents_ = new views::View; | |
| 417 GridLayout* layout = new GridLayout(encoding_contents_); | |
| 418 encoding_contents_->SetLayoutManager(layout); | |
| 419 | |
| 420 // Double column. | |
| 421 const int double_column_view_set_id = 2; | |
| 422 ColumnSet* column_set = layout->AddColumnSet(double_column_view_set_id); | |
| 423 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 0, | |
| 424 GridLayout::USE_PREF, 0, 0); | |
| 425 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | |
| 426 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, | |
| 427 GridLayout::USE_PREF, 0, 0); | |
| 428 | |
| 429 // Add Encoding Combobox. | |
| 430 layout->StartRow(0, double_column_view_set_id); | |
| 431 layout->AddView(default_encoding_combobox_label_); | |
| 432 layout->AddView(default_encoding_combobox_, 1, 1, GridLayout::FILL, | |
| 433 GridLayout::CENTER); | |
| 434 } | |
| OLD | NEW |