| 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/gtk/options/fonts_page_gtk.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/stringprintf.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "chrome/browser/default_encoding_combo_model.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/ui/gtk/gtk_util.h" | |
| 14 #include "chrome/browser/ui/gtk/options/options_layout_gtk.h" | |
| 15 #include "chrome/common/pref_names.h" | |
| 16 #include "grit/generated_resources.h" | |
| 17 #include "ui/base/l10n/l10n_util.h" | |
| 18 #include "ui/gfx/font.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // Make a Gtk font name string from a font family name and pixel size. | |
| 23 std::string MakeFontName(std::string family_name, int pixel_size) { | |
| 24 // The given font might not be available (the default fonts we use are not | |
| 25 // installed by default on some distros). So figure out which font we are | |
| 26 // actually falling back to and display that. (See crbug.com/31381.) | |
| 27 string16 actual_family_name = gfx::Font( | |
| 28 UTF8ToUTF16(family_name), pixel_size).GetFontName(); | |
| 29 std::string fontname; | |
| 30 // TODO(mattm): We can pass in the size in pixels (px), and the font button | |
| 31 // actually honors it, but when you open the selector it interprets it as | |
| 32 // points. See crbug.com/17857 | |
| 33 base::SStringPrintf(&fontname, "%s, %dpx", | |
| 34 UTF16ToUTF8(actual_family_name).c_str(), pixel_size); | |
| 35 return fontname; | |
| 36 } | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 FontsPageGtk::FontsPageGtk(Profile* profile) : OptionsPageBase(profile) { | |
| 41 Init(); | |
| 42 } | |
| 43 | |
| 44 FontsPageGtk::~FontsPageGtk() { | |
| 45 } | |
| 46 | |
| 47 void FontsPageGtk::Init() { | |
| 48 scoped_ptr<OptionsLayoutBuilderGtk> | |
| 49 options_builder(OptionsLayoutBuilderGtk::Create()); | |
| 50 serif_font_button_ = gtk_font_button_new(); | |
| 51 gtk_font_button_set_use_font(GTK_FONT_BUTTON(serif_font_button_), TRUE); | |
| 52 gtk_font_button_set_use_size(GTK_FONT_BUTTON(serif_font_button_), TRUE); | |
| 53 g_signal_connect(serif_font_button_, "font-set", | |
| 54 G_CALLBACK(OnSerifFontSetThunk), this); | |
| 55 | |
| 56 sans_font_button_ = gtk_font_button_new(); | |
| 57 gtk_font_button_set_use_font(GTK_FONT_BUTTON(sans_font_button_), TRUE); | |
| 58 gtk_font_button_set_use_size(GTK_FONT_BUTTON(sans_font_button_), TRUE); | |
| 59 g_signal_connect(sans_font_button_, "font-set", | |
| 60 G_CALLBACK(OnSansFontSetThunk), this); | |
| 61 | |
| 62 fixed_font_button_ = gtk_font_button_new(); | |
| 63 gtk_font_button_set_use_font(GTK_FONT_BUTTON(fixed_font_button_), TRUE); | |
| 64 gtk_font_button_set_use_size(GTK_FONT_BUTTON(fixed_font_button_), TRUE); | |
| 65 g_signal_connect(fixed_font_button_, "font-set", | |
| 66 G_CALLBACK(OnFixedFontSetThunk), this); | |
| 67 | |
| 68 GtkWidget* font_controls = gtk_util::CreateLabeledControlsGroup(NULL, | |
| 69 l10n_util::GetStringUTF8( | |
| 70 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SERIF_LABEL).c_str(), | |
| 71 serif_font_button_, | |
| 72 l10n_util::GetStringUTF8( | |
| 73 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SANS_SERIF_LABEL).c_str(), | |
| 74 sans_font_button_, | |
| 75 l10n_util::GetStringUTF8( | |
| 76 IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_FIXED_WIDTH_LABEL).c_str(), | |
| 77 fixed_font_button_, | |
| 78 NULL); | |
| 79 | |
| 80 options_builder->AddOptionGroup(l10n_util::GetStringUTF8( | |
| 81 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_FONT_TITLE), | |
| 82 font_controls, false); | |
| 83 | |
| 84 InitDefaultEncodingComboBox(); | |
| 85 std::string encoding_group_description = l10n_util::GetStringUTF8( | |
| 86 IDS_FONT_LANGUAGE_SETTING_FONT_DEFAULT_ENCODING_SELECTOR_LABEL); | |
| 87 GtkWidget* encoding_controls = gtk_util::CreateLabeledControlsGroup(NULL, | |
| 88 encoding_group_description.c_str(), | |
| 89 default_encoding_combobox_, | |
| 90 NULL); | |
| 91 options_builder->AddOptionGroup(l10n_util::GetStringUTF8( | |
| 92 IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE), | |
| 93 encoding_controls, false); | |
| 94 | |
| 95 page_ = options_builder->get_page_widget(); | |
| 96 | |
| 97 serif_name_.Init(prefs::kWebKitSerifFontFamily, profile()->GetPrefs(), this); | |
| 98 sans_serif_name_.Init(prefs::kWebKitSansSerifFontFamily, | |
| 99 profile()->GetPrefs(), this); | |
| 100 variable_width_size_.Init(prefs::kWebKitDefaultFontSize, | |
| 101 profile()->GetPrefs(), this); | |
| 102 | |
| 103 fixed_width_name_.Init(prefs::kWebKitFixedFontFamily, profile()->GetPrefs(), | |
| 104 this); | |
| 105 fixed_width_size_.Init(prefs::kWebKitDefaultFixedFontSize, | |
| 106 profile()->GetPrefs(), this); | |
| 107 | |
| 108 default_encoding_.Init(prefs::kDefaultCharset, profile()->GetPrefs(), this); | |
| 109 | |
| 110 NotifyPrefChanged(NULL); | |
| 111 } | |
| 112 | |
| 113 void FontsPageGtk::InitDefaultEncodingComboBox() { | |
| 114 default_encoding_combobox_ = gtk_combo_box_new_text(); | |
| 115 g_signal_connect(default_encoding_combobox_, "changed", | |
| 116 G_CALLBACK(OnDefaultEncodingChangedThunk), this); | |
| 117 default_encoding_combobox_model_.reset(new DefaultEncodingComboboxModel); | |
| 118 for (int i = 0; i < default_encoding_combobox_model_->GetItemCount(); ++i) { | |
| 119 gtk_combo_box_append_text( | |
| 120 GTK_COMBO_BOX(default_encoding_combobox_), | |
| 121 UTF16ToUTF8(default_encoding_combobox_model_->GetItemAt(i)).c_str()); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 void FontsPageGtk::NotifyPrefChanged(const std::string* pref_name) { | |
| 126 if (!pref_name || *pref_name == prefs::kWebKitSerifFontFamily || | |
| 127 *pref_name == prefs::kWebKitDefaultFontSize) { | |
| 128 gtk_font_button_set_font_name(GTK_FONT_BUTTON(serif_font_button_), | |
| 129 MakeFontName(serif_name_.GetValue(), | |
| 130 variable_width_size_.GetValue()).c_str()); | |
| 131 } | |
| 132 if (!pref_name || *pref_name == prefs::kWebKitSansSerifFontFamily || | |
| 133 *pref_name == prefs::kWebKitDefaultFontSize) { | |
| 134 gtk_font_button_set_font_name(GTK_FONT_BUTTON(sans_font_button_), | |
| 135 MakeFontName(sans_serif_name_.GetValue(), | |
| 136 variable_width_size_.GetValue()).c_str()); | |
| 137 } | |
| 138 if (!pref_name || *pref_name == prefs::kWebKitFixedFontFamily || | |
| 139 *pref_name == prefs::kWebKitDefaultFixedFontSize) { | |
| 140 gtk_font_button_set_font_name(GTK_FONT_BUTTON(fixed_font_button_), | |
| 141 MakeFontName(fixed_width_name_.GetValue(), | |
| 142 fixed_width_size_.GetValue()).c_str()); | |
| 143 } | |
| 144 if (!pref_name || *pref_name == prefs::kDefaultCharset) { | |
| 145 gtk_combo_box_set_active( | |
| 146 GTK_COMBO_BOX(default_encoding_combobox_), | |
| 147 default_encoding_combobox_model_->GetSelectedEncodingIndex(profile())); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void FontsPageGtk::SetFontsFromButton(StringPrefMember* name_pref, | |
| 152 IntegerPrefMember* size_pref, | |
| 153 GtkWidget* font_button) { | |
| 154 PangoFontDescription* desc = pango_font_description_from_string( | |
| 155 gtk_font_button_get_font_name(GTK_FONT_BUTTON(font_button))); | |
| 156 int size = pango_font_description_get_size(desc); | |
| 157 name_pref->SetValue(pango_font_description_get_family(desc)); | |
| 158 size_pref->SetValue(size / PANGO_SCALE); | |
| 159 pango_font_description_free(desc); | |
| 160 // Reset the button font in px, since the chooser will have set it in points. | |
| 161 // Also, both sans and serif share the same size so we need to update them | |
| 162 // both. | |
| 163 NotifyPrefChanged(NULL); | |
| 164 } | |
| 165 | |
| 166 void FontsPageGtk::OnSerifFontSet(GtkWidget* font_button) { | |
| 167 SetFontsFromButton(&serif_name_, | |
| 168 &variable_width_size_, | |
| 169 font_button); | |
| 170 } | |
| 171 | |
| 172 void FontsPageGtk::OnSansFontSet(GtkWidget* font_button) { | |
| 173 SetFontsFromButton(&sans_serif_name_, | |
| 174 &variable_width_size_, | |
| 175 font_button); | |
| 176 } | |
| 177 | |
| 178 void FontsPageGtk::OnFixedFontSet(GtkWidget* font_button) { | |
| 179 SetFontsFromButton(&fixed_width_name_, | |
| 180 &fixed_width_size_, | |
| 181 font_button); | |
| 182 } | |
| 183 | |
| 184 void FontsPageGtk::OnDefaultEncodingChanged(GtkWidget* combo_box) { | |
| 185 int index = gtk_combo_box_get_active(GTK_COMBO_BOX(combo_box)); | |
| 186 default_encoding_.SetValue(default_encoding_combobox_model_-> | |
| 187 GetEncodingCharsetByIndex(index)); | |
| 188 } | |
| OLD | NEW |