diff options
author | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-28 01:28:31 +0000 |
---|---|---|
committer | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-28 01:28:31 +0000 |
commit | 1eaabcb4a8bf38a1ed8b260efe019d91f33dbeae (patch) | |
tree | b6578a76ab52cf67a7ecf59fffc32ecd67e80031 /chrome/browser | |
parent | daa82dc7442d26d9a50e342c0d9dcee43f287df8 (diff) | |
download | chromium_src-1eaabcb4a8bf38a1ed8b260efe019d91f33dbeae.zip chromium_src-1eaabcb4a8bf38a1ed8b260efe019d91f33dbeae.tar.gz chromium_src-1eaabcb4a8bf38a1ed8b260efe019d91f33dbeae.tar.bz2 |
Gtk: Implement font selection options.
BUG=11507
Review URL: http://codereview.chromium.org/159458
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21786 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/gtk/options/fonts_languages_window_gtk.cc | 12 | ||||
-rw-r--r-- | chrome/browser/gtk/options/fonts_page_gtk.cc | 157 | ||||
-rw-r--r-- | chrome/browser/gtk/options/fonts_page_gtk.h | 70 |
3 files changed, 235 insertions, 4 deletions
diff --git a/chrome/browser/gtk/options/fonts_languages_window_gtk.cc b/chrome/browser/gtk/options/fonts_languages_window_gtk.cc index a5d3a49..255a495 100644 --- a/chrome/browser/gtk/options/fonts_languages_window_gtk.cc +++ b/chrome/browser/gtk/options/fonts_languages_window_gtk.cc @@ -9,6 +9,7 @@ #include "app/l10n_util.h" #include "base/message_loop.h" #include "chrome/browser/profile.h" +#include "chrome/browser/gtk/options/fonts_page_gtk.h" #include "chrome/common/gtk_util.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" @@ -39,6 +40,9 @@ class FontsLanguagesWindowGtk { // The Profile associated with these options. Profile* profile_; + // The fonts page. + FontsPageGtk fonts_page_; + DISALLOW_COPY_AND_ASSIGN(FontsLanguagesWindowGtk); }; @@ -51,7 +55,8 @@ FontsLanguagesWindowGtk::FontsLanguagesWindowGtk(Profile* profile) // Always show preferences for the original profile. Most state when off // the record comes from the original profile, but we explicitly use // the original profile to avoid potential problems. - : profile_(profile->GetOriginalProfile()) { + : profile_(profile->GetOriginalProfile()), + fonts_page_(profile_) { dialog_ = gtk_dialog_new_with_buttons( l10n_util::GetStringFUTF8(IDS_FONT_LANGUAGE_SETTING_WINDOWS_TITLE, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)).c_str(), @@ -67,11 +72,12 @@ FontsLanguagesWindowGtk::FontsLanguagesWindowGtk(Profile* profile) gtk_util::kContentAreaSpacing); notebook_ = gtk_notebook_new(); + gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), notebook_); // Fonts and Encoding tab. gtk_notebook_append_page( GTK_NOTEBOOK(notebook_), - gtk_label_new("TODO content"), + fonts_page_.get_page_widget(), gtk_label_new( l10n_util::GetStringUTF8( IDS_FONT_LANGUAGE_SETTING_FONT_TAB_TITLE).c_str())); @@ -84,8 +90,6 @@ FontsLanguagesWindowGtk::FontsLanguagesWindowGtk(Profile* profile) l10n_util::GetStringUTF8( IDS_FONT_LANGUAGE_SETTING_LANGUAGES_TAB_TITLE).c_str())); - gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), notebook_); - // Show the notebook. gtk_widget_show_all(dialog_); diff --git a/chrome/browser/gtk/options/fonts_page_gtk.cc b/chrome/browser/gtk/options/fonts_page_gtk.cc new file mode 100644 index 0000000..b85c305 --- /dev/null +++ b/chrome/browser/gtk/options/fonts_page_gtk.cc @@ -0,0 +1,157 @@ +// Copyright (c) 2009 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/gtk/options/fonts_page_gtk.h" + +#include "app/l10n_util.h" +#include "chrome/browser/gtk/options/options_layout_gtk.h" +#include "chrome/common/gtk_util.h" +#include "chrome/common/pref_names.h" +#include "grit/generated_resources.h" + +namespace { + +// Make a Gtk font name string from a font family name and pixel size. +std::string MakeFontName(std::wstring family_name, int pixel_size) { + std::string fontname; + // TODO(mattm): We can pass in the size in pixels (px), and the font button + // actually honors it, but when you open the selector it interprets it as + // points. See crbug.com/17857 + SStringPrintf(&fontname, "%s %dpx", WideToUTF8(family_name).c_str(), + pixel_size); + return fontname; +} + +} // namespace + +FontsPageGtk::FontsPageGtk(Profile* profile) : OptionsPageBase(profile) { + Init(); +} + +FontsPageGtk::~FontsPageGtk() { +} + +void FontsPageGtk::Init() { + OptionsLayoutBuilderGtk options_builder; + + serif_font_button_ = gtk_font_button_new(); + gtk_font_button_set_use_font(GTK_FONT_BUTTON(serif_font_button_), TRUE); + gtk_font_button_set_use_size(GTK_FONT_BUTTON(serif_font_button_), TRUE); + g_signal_connect(serif_font_button_, "font-set", G_CALLBACK(OnSerifFontSet), + this); + + sans_font_button_ = gtk_font_button_new(); + gtk_font_button_set_use_font(GTK_FONT_BUTTON(sans_font_button_), TRUE); + gtk_font_button_set_use_size(GTK_FONT_BUTTON(sans_font_button_), TRUE); + g_signal_connect(sans_font_button_, "font-set", G_CALLBACK(OnSansFontSet), + this); + + fixed_font_button_ = gtk_font_button_new(); + gtk_font_button_set_use_font(GTK_FONT_BUTTON(fixed_font_button_), TRUE); + gtk_font_button_set_use_size(GTK_FONT_BUTTON(fixed_font_button_), TRUE); + g_signal_connect(fixed_font_button_, "font-set", G_CALLBACK(OnFixedFontSet), + this); + + GtkWidget* font_controls = gtk_util::CreateLabeledControlsGroup(NULL, + l10n_util::GetStringUTF8( + IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SERIF_LABEL).c_str(), + serif_font_button_, + l10n_util::GetStringUTF8( + IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_SANS_SERIF_LABEL).c_str(), + sans_font_button_, + l10n_util::GetStringUTF8( + IDS_FONT_LANGUAGE_SETTING_FONT_SELECTOR_FIXED_WIDTH_LABEL).c_str(), + fixed_font_button_, + NULL); + + options_builder.AddOptionGroup(l10n_util::GetStringUTF8( + IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_FONT_TITLE), + font_controls, false); + + // TODO(mattm): default encoding + options_builder.AddOptionGroup(l10n_util::GetStringUTF8( + IDS_FONT_LANGUAGE_SETTING_FONT_SUB_DIALOG_ENCODING_TITLE), + gtk_label_new("todo"), false); + + page_ = options_builder.get_page_widget(); + + serif_name_.Init(prefs::kWebKitSerifFontFamily, profile()->GetPrefs(), this); + sans_serif_name_.Init(prefs::kWebKitSansSerifFontFamily, + profile()->GetPrefs(), this); + variable_width_size_.Init(prefs::kWebKitDefaultFontSize, + profile()->GetPrefs(), this); + + fixed_width_name_.Init(prefs::kWebKitFixedFontFamily, profile()->GetPrefs(), + this); + fixed_width_size_.Init(prefs::kWebKitDefaultFixedFontSize, + profile()->GetPrefs(), this); + + default_encoding_.Init(prefs::kDefaultCharset, profile()->GetPrefs(), this); + + NotifyPrefChanged(NULL); +} + +void FontsPageGtk::NotifyPrefChanged(const std::wstring* pref_name) { + if (!pref_name || *pref_name == prefs::kWebKitSerifFontFamily || + *pref_name == prefs::kWebKitDefaultFontSize) { + gtk_font_button_set_font_name(GTK_FONT_BUTTON(serif_font_button_), + MakeFontName(serif_name_.GetValue(), + variable_width_size_.GetValue()).c_str()); + } + if (!pref_name || *pref_name == prefs::kWebKitSansSerifFontFamily || + *pref_name == prefs::kWebKitDefaultFontSize) { + gtk_font_button_set_font_name(GTK_FONT_BUTTON(sans_font_button_), + MakeFontName(sans_serif_name_.GetValue(), + variable_width_size_.GetValue()).c_str()); + } + if (!pref_name || *pref_name == prefs::kWebKitFixedFontFamily || + *pref_name == prefs::kWebKitDefaultFixedFontSize) { + gtk_font_button_set_font_name(GTK_FONT_BUTTON(fixed_font_button_), + MakeFontName(fixed_width_name_.GetValue(), + fixed_width_size_.GetValue()).c_str()); + } + if (!pref_name || *pref_name == prefs::kDefaultCharset) { + // TODO + } +} + +void FontsPageGtk::SetFontsFromButton(StringPrefMember* name_pref, + IntegerPrefMember* size_pref, + GtkFontButton* font_button) { + PangoFontDescription* desc = pango_font_description_from_string( + gtk_font_button_get_font_name(font_button)); + int size = pango_font_description_get_size(desc); + name_pref->SetValue(UTF8ToWide(pango_font_description_get_family(desc))); + size_pref->SetValue(size / PANGO_SCALE); + pango_font_description_free(desc); + // Reset the button font in px, since the chooser will have set it in points. + // Also, both sans and serif share the same size so we need to update them + // both. + NotifyPrefChanged(NULL); +} + + +// static +void FontsPageGtk::OnSerifFontSet(GtkFontButton* font_button, + FontsPageGtk* fonts_page) { + fonts_page->SetFontsFromButton(&fonts_page->serif_name_, + &fonts_page->variable_width_size_, + font_button); +} + +// static +void FontsPageGtk::OnSansFontSet(GtkFontButton* font_button, + FontsPageGtk* fonts_page) { + fonts_page->SetFontsFromButton(&fonts_page->sans_serif_name_, + &fonts_page->variable_width_size_, + font_button); +} + +// static +void FontsPageGtk::OnFixedFontSet(GtkFontButton* font_button, + FontsPageGtk* fonts_page) { + fonts_page->SetFontsFromButton(&fonts_page->fixed_width_name_, + &fonts_page->fixed_width_size_, + font_button); +} diff --git a/chrome/browser/gtk/options/fonts_page_gtk.h b/chrome/browser/gtk/options/fonts_page_gtk.h new file mode 100644 index 0000000..7733094 --- /dev/null +++ b/chrome/browser/gtk/options/fonts_page_gtk.h @@ -0,0 +1,70 @@ +// Copyright (c) 2009 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. + +// The fonts page of the fonts & languages options dialog, which contains font +// family and size settings, as well as the default encoding option. + +#ifndef CHROME_BROWSER_GTK_OPTIONS_FONTS_PAGE_GTK_H_ +#define CHROME_BROWSER_GTK_OPTIONS_FONTS_PAGE_GTK_H_ + +#include <gtk/gtk.h> + +#include "chrome/browser/options_page_base.h" +#include "chrome/common/pref_member.h" + +class FontsPageGtk : public OptionsPageBase { + public: + explicit FontsPageGtk(Profile* profile); + virtual ~FontsPageGtk(); + + GtkWidget* get_page_widget() const { + return page_; + } + + private: + void Init(); + + // Overridden from OptionsPageBase. + virtual void NotifyPrefChanged(const std::wstring* pref_name); + + // Retrieve the font selection from the button and save it to the prefs. Also + // ensure the button(s) are displayed in the proper size, as the + // GtkFontSelector returns the value in points not pixels. + void SetFontsFromButton(StringPrefMember* name_pref, + IntegerPrefMember* size_pref, + GtkFontButton* font_button); + + // Callbacks + static void OnSerifFontSet(GtkFontButton* font_button, + FontsPageGtk* fonts_page); + static void OnSansFontSet(GtkFontButton* font_button, + FontsPageGtk* fonts_page); + static void OnFixedFontSet(GtkFontButton* font_button, + FontsPageGtk* fonts_page); + + // The font chooser widgets + GtkWidget* serif_font_button_; + GtkWidget* sans_font_button_; + GtkWidget* fixed_font_button_; + + // The widget containing the options for this page. + GtkWidget* page_; + + // Font name preferences. + StringPrefMember serif_name_; + StringPrefMember sans_serif_name_; + StringPrefMember fixed_width_name_; + + // Font size preferences, in pixels. + IntegerPrefMember variable_width_size_; + IntegerPrefMember fixed_width_size_; + + // Default encoding preference. + StringPrefMember default_encoding_; + + DISALLOW_COPY_AND_ASSIGN(FontsPageGtk); +}; + + +#endif // #ifndef CHROME_BROWSER_GTK_OPTIONS_FONTS_PAGE_GTK_H_ |