diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-13 16:43:03 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-13 16:43:03 +0000 |
commit | c6ac841f51c0b884b38e917ac30b1dfde0dc43a7 (patch) | |
tree | 2b490ffa6795f72e7232d658b766785f0de64e38 /views/controls | |
parent | 6b32b95cff99ee72fd7824237ae5070263e5c496 (diff) | |
download | chromium_src-c6ac841f51c0b884b38e917ac30b1dfde0dc43a7.zip chromium_src-c6ac841f51c0b884b38e917ac30b1dfde0dc43a7.tar.gz chromium_src-c6ac841f51c0b884b38e917ac30b1dfde0dc43a7.tar.bz2 |
Rework gfx::Font by moving platform-specific code into inner classes.
gfx::Font is a platform-neutral API shim that exists as a wrapper object to allow for the creation and lifetime of gfx::Font objects to remain consistent with past usage.
gfx::PlatformFont is an interface implemented by the platform-specific inner classes (gfx::PlatformFontWin,Mac,Gtk).
BUG=none
TEST=existing unittests
Review URL: http://codereview.chromium.org/3083022
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56040 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views/controls')
23 files changed, 60 insertions, 51 deletions
diff --git a/views/controls/button/checkbox.cc b/views/controls/button/checkbox.cc index 0899e58..404a0ea 100644 --- a/views/controls/button/checkbox.cc +++ b/views/controls/button/checkbox.cc @@ -103,7 +103,7 @@ void Checkbox::Layout() { label_x, 0, std::max(0, width() - label_x - kLabelFocusPaddingHorizontal), height()); - int first_line_height = label_->font().height(); + int first_line_height = label_->font().GetHeight(); native_wrapper_->GetView()->SetBounds( 0, ((first_line_height - checkmark_prefsize.height()) / 2), checkmark_prefsize.width(), checkmark_prefsize.height()); diff --git a/views/controls/button/native_button.cc b/views/controls/button/native_button.cc index 542030a..756cc20 100644 --- a/views/controls/button/native_button.cc +++ b/views/controls/button/native_button.cc @@ -4,16 +4,19 @@ #include "views/controls/button/native_button.h" -#if defined(OS_LINUX) -#include <gdk/gdkkeysyms.h> -#include "views/screen.h" -#endif - #include "base/i18n/rtl.h" #include "base/keyboard_codes.h" #include "base/logging.h" #include "views/controls/native/native_view_host.h" +#if defined(OS_WIN) +#include "gfx/platform_font_win.h" +#elif defined(OS_LINUX) +#include <gdk/gdkkeysyms.h> +#include "views/screen.h" +#endif + + namespace views { #if defined(OS_WIN) @@ -144,10 +147,14 @@ gfx::Size NativeButton::GetPreferredSize() { #if defined(OS_WIN) // Clamp the size returned to at least the minimum size. if (!ignore_minimum_size_) { - sz.set_width(std::max(sz.width(), - font_.horizontal_dlus_to_pixels(kMinWidthDLUs))); - sz.set_height(std::max(sz.height(), - font_.vertical_dlus_to_pixels(kMinHeightDLUs))); + gfx::PlatformFontWin* platform_font = + static_cast<gfx::PlatformFontWin*>(font_.platform_font()); + sz.set_width(std::max( + sz.width(), + platform_font->horizontal_dlus_to_pixels(kMinWidthDLUs))); + sz.set_height(std::max( + sz.height(), + platform_font->vertical_dlus_to_pixels(kMinHeightDLUs))); } // GTK returns a meaningful preferred size so that we don't need to adjust // the preferred size as we do on windows. diff --git a/views/controls/button/native_button_gtk.cc b/views/controls/button/native_button_gtk.cc index d25f444..6720a50 100644 --- a/views/controls/button/native_button_gtk.cc +++ b/views/controls/button/native_button_gtk.cc @@ -1,6 +1,6 @@ -// Copyright (c) 2010 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. +// Copyright (c) 2010 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 "views/controls/button/native_button_gtk.h" @@ -42,8 +42,7 @@ void NativeButtonGtk::UpdateFont() { if (!native_view()) return; - PangoFontDescription* pfd = - gfx::Font::PangoFontFromGfxFont(native_button_->font()); + PangoFontDescription* pfd = native_button_->font().GetNativeFont(); gtk_widget_modify_font(native_view(), pfd); pango_font_description_free(pfd); preferred_size_ = gfx::Size(); diff --git a/views/controls/button/native_button_win.cc b/views/controls/button/native_button_win.cc index 9e24fc3..eae509c 100644 --- a/views/controls/button/native_button_win.cc +++ b/views/controls/button/native_button_win.cc @@ -49,7 +49,7 @@ void NativeButtonWin::UpdateLabel() { void NativeButtonWin::UpdateFont() { SendMessage(native_view(), WM_SETFONT, - reinterpret_cast<WPARAM>(native_button_->font().hfont()), + reinterpret_cast<WPARAM>(native_button_->font().GetNativeFont()), FALSE); button_size_valid_ = false; } diff --git a/views/controls/button/text_button.cc b/views/controls/button/text_button.cc index 2125a02..bcff70d 100644 --- a/views/controls/button/text_button.cc +++ b/views/controls/button/text_button.cc @@ -390,7 +390,7 @@ void TextButton::UpdateTextSize() { gfx::CanvasSkia::SizeStringInt( text_, font_, &width, &height, gfx::Canvas::NO_ELLIPSIS | PrefixTypeToCanvasType(prefix_type_)); - text_size_.SetSize(width, font_.height()); + text_size_.SetSize(width, font_.GetHeight()); max_text_size_.SetSize(std::max(max_text_size_.width(), text_size_.width()), std::max(max_text_size_.height(), text_size_.height())); diff --git a/views/controls/combobox/native_combobox_win.cc b/views/controls/combobox/native_combobox_win.cc index a936b0c..b1bc88f 100644 --- a/views/controls/combobox/native_combobox_win.cc +++ b/views/controls/combobox/native_combobox_win.cc @@ -182,7 +182,7 @@ void NativeComboboxWin::NativeControlCreated(HWND native_control) { void NativeComboboxWin::UpdateFont() { HFONT font = ResourceBundle::GetSharedInstance(). - GetFont(ResourceBundle::BaseFont).hfont(); + GetFont(ResourceBundle::BaseFont).GetNativeFont(); SendMessage(native_view(), WM_SETFONT, reinterpret_cast<WPARAM>(font), FALSE); } diff --git a/views/controls/label.cc b/views/controls/label.cc index b8d3adc..7953d18 100644 --- a/views/controls/label.cc +++ b/views/controls/label.cc @@ -58,7 +58,7 @@ gfx::Size Label::GetPreferredSize() { } int Label::GetBaseline() { - return GetInsets().top() + font_.baseline(); + return GetInsets().top() + font_.GetBaseline(); } int Label::GetHeightForWidth(int w) { @@ -66,7 +66,7 @@ int Label::GetHeightForWidth(int w) { return View::GetHeightForWidth(w); w = std::max(0, w - GetInsets().width()); - int h = font_.height(); + int h = font_.GetHeight(); gfx::CanvasSkia::SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags()); return h + GetInsets().height(); } @@ -292,7 +292,7 @@ gfx::Size Label::GetTextSize() const { // on Linux. int w = is_multi_line_ ? GetAvailableRect().width() : std::numeric_limits<int>::max(); - int h = font_.height(); + int h = font_.GetHeight(); // For single-line strings, ignore the available width and calculate how // wide the text wants to be. int flags = ComputeMultiLineFlags(); diff --git a/views/controls/label_unittest.cc b/views/controls/label_unittest.cc index e072b80..65ed4c9 100644 --- a/views/controls/label_unittest.cc +++ b/views/controls/label_unittest.cc @@ -31,11 +31,11 @@ TEST(LabelTest, FontPropertyCourier) { TEST(LabelTest, FontPropertyArial) { Label label; std::wstring font_name(L"arial"); - gfx::Font font = gfx::Font::CreateFont(font_name, 30); + gfx::Font font(font_name, 30); label.SetFont(font); gfx::Font font_used = label.font(); - EXPECT_EQ(font_name, font_used.FontName()); - EXPECT_EQ(30, font_used.FontSize()); + EXPECT_EQ(font_name, font_used.GetFontName()); + EXPECT_EQ(30, font_used.GetFontSize()); } TEST(LabelTest, TextProperty) { diff --git a/views/controls/link.cc b/views/controls/link.cc index b258217..97dc01f 100644 --- a/views/controls/link.cc +++ b/views/controls/link.cc @@ -211,15 +211,15 @@ void Link::SetHighlighted(bool f) { void Link::ValidateStyle() { if (enabled_) { - if (!(font().style() & gfx::Font::UNDERLINED)) { + if (!(font().GetStyle() & gfx::Font::UNDERLINED)) { Label::SetFont( - font().DeriveFont(0, font().style() | gfx::Font::UNDERLINED)); + font().DeriveFont(0, font().GetStyle() | gfx::Font::UNDERLINED)); } Label::SetColor(highlighted_ ? highlighted_color_ : normal_color_); } else { - if (font().style() & gfx::Font::UNDERLINED) { + if (font().GetStyle() & gfx::Font::UNDERLINED) { Label::SetFont( - font().DeriveFont(0, font().style() & ~gfx::Font::UNDERLINED)); + font().DeriveFont(0, font().GetStyle() & ~gfx::Font::UNDERLINED)); } Label::SetColor(disabled_color_); } diff --git a/views/controls/listbox/native_listbox_win.cc b/views/controls/listbox/native_listbox_win.cc index 5ec5542..76db003 100644 --- a/views/controls/listbox/native_listbox_win.cc +++ b/views/controls/listbox/native_listbox_win.cc @@ -105,7 +105,7 @@ void NativeListboxWin::CreateNativeControl() { listbox_->GetWidget()->GetNativeView(), NULL, NULL, NULL); HFONT font = ResourceBundle::GetSharedInstance(). - GetFont(ResourceBundle::BaseFont).hfont(); + GetFont(ResourceBundle::BaseFont).GetNativeFont(); SendMessage(hwnd, WM_SETFONT, reinterpret_cast<WPARAM>(font), FALSE); l10n_util::AdjustUIFontForWindow(hwnd); diff --git a/views/controls/menu/menu_config_win.cc b/views/controls/menu/menu_config_win.cc index 5adf9eb..35ab9e2 100644 --- a/views/controls/menu/menu_config_win.cc +++ b/views/controls/menu/menu_config_win.cc @@ -30,7 +30,7 @@ MenuConfig* MenuConfig::Create() { l10n_util::AdjustUIFont(&(metrics.lfMenuFont)); HFONT font = CreateFontIndirect(&metrics.lfMenuFont); DLOG_ASSERT(font); - config->font = gfx::Font::CreateFont(font); + config->font = gfx::Font(font); HDC dc = GetDC(NULL); RECT bounds = { 0, 0, 200, 200 }; diff --git a/views/controls/menu/menu_item_view_gtk.cc b/views/controls/menu/menu_item_view_gtk.cc index 5526966..9f77390 100644 --- a/views/controls/menu/menu_item_view_gtk.cc +++ b/views/controls/menu/menu_item_view_gtk.cc @@ -44,7 +44,7 @@ gfx::Size MenuItemView::GetPreferredSize() { // TODO(sky): this is a workaround until I figure out why font.height() // isn't returning the right thing. We really only want to include // kFavIconSize if we're showing icons. - int content_height = std::max(kFavIconSize, font.height()); + int content_height = std::max(kFavIconSize, font.GetHeight()); return gfx::Size( font.GetStringWidth(title_) + label_start_ + item_right_margin_ + GetChildPreferredWidth(), @@ -155,8 +155,8 @@ void MenuItemView::Paint(gfx::Canvas* canvas, bool for_drag) { int accel_width = parent_menu_item_->GetSubmenu()->max_accelerator_width(); int width = this->width() - item_right_margin_ - label_start_ - accel_width; gfx::Rect text_bounds(label_start_, top_margin + - (available_height - font.height()) / 2, width, - font.height()); + (available_height - font.GetHeight()) / 2, width, + font.GetHeight()); text_bounds.set_x(MirroredLeftPointForRect(text_bounds)); canvas->DrawStringInt(GetTitle(), font, fg_color, text_bounds.x(), text_bounds.y(), text_bounds.width(), diff --git a/views/controls/menu/menu_item_view_win.cc b/views/controls/menu/menu_item_view_win.cc index 1e36a8c..f2ad60e 100644 --- a/views/controls/menu/menu_item_view_win.cc +++ b/views/controls/menu/menu_item_view_win.cc @@ -22,7 +22,7 @@ gfx::Size MenuItemView::GetPreferredSize() { return gfx::Size( font.GetStringWidth(title_) + label_start_ + item_right_margin_ + GetChildPreferredWidth(), - font.height() + GetBottomMargin() + GetTopMargin()); + font.GetHeight() + GetBottomMargin() + GetTopMargin()); } void MenuItemView::Paint(gfx::Canvas* canvas, bool for_drag) { @@ -87,7 +87,7 @@ void MenuItemView::Paint(gfx::Canvas* canvas, bool for_drag) { const gfx::Font& font = MenuConfig::instance().font; int accel_width = parent_menu_item_->GetSubmenu()->max_accelerator_width(); int width = this->width() - item_right_margin_ - label_start_ - accel_width; - gfx::Rect text_bounds(label_start_, top_margin, width, font.height()); + gfx::Rect text_bounds(label_start_, top_margin, width, font.GetHeight()); text_bounds.set_x(MirroredLeftPointForRect(text_bounds)); if (for_drag) { // With different themes, it's difficult to tell what the correct diff --git a/views/controls/menu/menu_win.cc b/views/controls/menu/menu_win.cc index 05ada25..90637a0 100644 --- a/views/controls/menu/menu_win.cc +++ b/views/controls/menu/menu_win.cc @@ -112,7 +112,7 @@ class MenuHostWindow : public gfx::WindowImpl { // If the label contains an accelerator, make room for tab. if (data->label.find(L'\t') != std::wstring::npos) lpmis->itemWidth += font.GetStringWidth(L" "); - lpmis->itemHeight = font.height() + kItemBottomMargin + kItemTopMargin; + lpmis->itemHeight = font.GetHeight() + kItemBottomMargin + kItemTopMargin; } else { // Measure separator size. lpmis->itemHeight = GetSystemMetrics(SM_CYMENU) / 2; @@ -158,8 +158,9 @@ class MenuHostWindow : public gfx::WindowImpl { if (!underline_mnemonics) format |= DT_HIDEPREFIX; gfx::Font font; - HGDIOBJ old_font = static_cast<HFONT>(SelectObject(hDC, font.hfont())); - int fontsize = font.FontSize(); + HGDIOBJ old_font = + static_cast<HFONT>(SelectObject(hDC, font.GetNativeFont())); + int fontsize = font.GetFontSize(); // If an accelerator is specified (with a tab delimiting the rest of the // label from the accelerator), we have to justify the fist part on the diff --git a/views/controls/menu/native_menu_gtk.cc b/views/controls/menu/native_menu_gtk.cc index a89fa63..2ddf336 100644 --- a/views/controls/menu/native_menu_gtk.cc +++ b/views/controls/menu/native_menu_gtk.cc @@ -316,8 +316,9 @@ GtkWidget* NativeMenuGtk::AddMenuItemAt(int index, // The label item is the first child of the menu item. GtkWidget* label_widget = GTK_BIN(menu_item)->child; DCHECK(label_widget && GTK_IS_LABEL(label_widget)); - gtk_widget_modify_font(label_widget, - gfx::Font::PangoFontFromGfxFont(*font)); + PangoFontDescription* pfd = font->GetNativeFont(); + gtk_widget_modify_font(label_widget, pfd); + pango_font_description_free(pfd); } if (type == menus::MenuModel::TYPE_SUBMENU) { diff --git a/views/controls/menu/native_menu_win.cc b/views/controls/menu/native_menu_win.cc index cc528e3..f65cfcd 100644 --- a/views/controls/menu/native_menu_win.cc +++ b/views/controls/menu/native_menu_win.cc @@ -151,7 +151,7 @@ class NativeMenuWin::MenuHostWindow { if (data->label.find(L'\t') != std::wstring::npos) measure_item_struct->itemWidth += font.GetStringWidth(L" "); measure_item_struct->itemHeight = - font.height() + kItemBottomMargin + kItemTopMargin; + font.GetHeight() + kItemBottomMargin + kItemTopMargin; } else { // Measure separator size. measure_item_struct->itemHeight = GetSystemMetrics(SM_CYMENU) / 2; @@ -196,8 +196,9 @@ class NativeMenuWin::MenuHostWindow { if (!underline_mnemonics) format |= DT_HIDEPREFIX; gfx::Font font; - HGDIOBJ old_font = static_cast<HFONT>(SelectObject(dc, font.hfont())); - int fontsize = font.FontSize(); + HGDIOBJ old_font = + static_cast<HFONT>(SelectObject(dc, font.GetNativeFont())); + int fontsize = font.GetFontSize(); // If an accelerator is specified (with a tab delimiting the rest of the // label from the accelerator), we have to justify the fist part on the diff --git a/views/controls/tabbed_pane/native_tabbed_pane_win.cc b/views/controls/tabbed_pane/native_tabbed_pane_win.cc index 23b2c50..8abc73c 100644 --- a/views/controls/tabbed_pane/native_tabbed_pane_win.cc +++ b/views/controls/tabbed_pane/native_tabbed_pane_win.cc @@ -284,7 +284,7 @@ void NativeTabbedPaneWin::CreateNativeControl() { NULL); HFONT font = ResourceBundle::GetSharedInstance(). - GetFont(ResourceBundle::BaseFont).hfont(); + GetFont(ResourceBundle::BaseFont).GetNativeFont(); SendMessage(tab_control, WM_SETFONT, reinterpret_cast<WPARAM>(font), FALSE); // Create the view container which is a child of the TabControl. diff --git a/views/controls/table/table_view.cc b/views/controls/table/table_view.cc index 726283c..510d536 100644 --- a/views/controls/table/table_view.cc +++ b/views/controls/table/table_view.cc @@ -1544,7 +1544,7 @@ gfx::Rect TableView::GetAltTextBounds() { gfx::Font font = GetAltTextFont(); // Pad height by 2 for halo. return gfx::Rect(kXOffset, content_offset(), client_rect.width() - kXOffset, - std::max(kImageSize, font.height() + 2)); + std::max(kImageSize, font.GetHeight() + 2)); } gfx::Font TableView::GetAltTextFont() { diff --git a/views/controls/textfield/gtk_views_entry.cc b/views/controls/textfield/gtk_views_entry.cc index a068009..eaf4459 100644 --- a/views/controls/textfield/gtk_views_entry.cc +++ b/views/controls/textfield/gtk_views_entry.cc @@ -38,7 +38,7 @@ static gint gtk_views_entry_expose_event(GtkWidget *widget, UTF16ToWide(text), font, gfx::GdkColorToSkColor(widget->style->text[GTK_STATE_INSENSITIVE]), insets.left(), insets.top(), - widget->allocation.width - insets.width(), font.height()); + widget->allocation.width - insets.width(), font.GetHeight()); } } diff --git a/views/controls/textfield/gtk_views_textview.cc b/views/controls/textfield/gtk_views_textview.cc index da43d93..b7ec107 100644 --- a/views/controls/textfield/gtk_views_textview.cc +++ b/views/controls/textfield/gtk_views_textview.cc @@ -53,7 +53,7 @@ static gint gtk_views_textview_expose_event(GtkWidget *widget, UTF16ToWide(text), font, gfx::GdkColorToSkColor(widget->style->text[GTK_STATE_INSENSITIVE]), insets.left(), insets.top(), - widget->allocation.width - insets.width(), font.height()); + widget->allocation.width - insets.width(), font.GetHeight()); } } diff --git a/views/controls/textfield/native_textfield_gtk.cc b/views/controls/textfield/native_textfield_gtk.cc index 7fd05ad..9cf4832 100644 --- a/views/controls/textfield/native_textfield_gtk.cc +++ b/views/controls/textfield/native_textfield_gtk.cc @@ -235,8 +235,7 @@ void NativeTextfieldGtk::UpdateReadOnly() { void NativeTextfieldGtk::UpdateFont() { if (!native_view()) return; - PangoFontDescription* pfd = - gfx::Font::PangoFontFromGfxFont(textfield_->font()); + PangoFontDescription* pfd = textfield_->font().GetNativeFont(); gtk_widget_modify_font(native_view(), pfd); pango_font_description_free(pfd); } diff --git a/views/controls/textfield/native_textfield_win.cc b/views/controls/textfield/native_textfield_win.cc index be1a0bae..fc4ad8b 100644 --- a/views/controls/textfield/native_textfield_win.cc +++ b/views/controls/textfield/native_textfield_win.cc @@ -226,7 +226,8 @@ void NativeTextfieldWin::UpdateReadOnly() { void NativeTextfieldWin::UpdateFont() { SendMessage(m_hWnd, WM_SETFONT, - reinterpret_cast<WPARAM>(textfield_->font().hfont()), TRUE); + reinterpret_cast<WPARAM>(textfield_->font().GetNativeFont()), + TRUE); // Setting the font blows away any text color we've set, so reset it. UpdateTextColor(); } diff --git a/views/controls/textfield/textfield.cc b/views/controls/textfield/textfield.cc index c98fcc6..43f0897 100644 --- a/views/controls/textfield/textfield.cc +++ b/views/controls/textfield/textfield.cc @@ -225,7 +225,7 @@ gfx::Size Textfield::GetPreferredSize() { insets = native_wrapper_->CalculateInsets(); return gfx::Size(font_.GetExpectedTextWidth(default_width_in_chars_) + insets.width(), - num_lines_ * font_.height() + insets.height()); + num_lines_ * font_.GetHeight() + insets.height()); } bool Textfield::IsFocusable() const { |