Index: chrome/browser/ui/views/omnibox/omnibox_view_win.cc |
diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_win.cc b/chrome/browser/ui/views/omnibox/omnibox_view_win.cc |
index 01cee6dfa4831f24ae015261e42ea5b4448db177..66715e857861cf295fffc734d75f213d2c8abcb8 100644 |
--- a/chrome/browser/ui/views/omnibox/omnibox_view_win.cc |
+++ b/chrome/browser/ui/views/omnibox/omnibox_view_win.cc |
@@ -454,6 +454,8 @@ OmniboxViewWin::OmniboxViewWin(AutocompleteEditController* controller, |
ignore_ime_messages_(false), |
delete_at_end_pressed_(false), |
font_(parent_view->font()), |
+ font_x_height_(-1), |
+ font_y_adjustment_(-1), |
possible_drag_(false), |
in_drag_(false), |
initiated_drag_(false), |
@@ -491,37 +493,6 @@ OmniboxViewWin::OmniboxViewWin(AutocompleteEditController* controller, |
SendMessage(m_hWnd, EM_SETWORDBREAKPROC, 0, |
reinterpret_cast<LPARAM>(&WordBreakProc)); |
- // Get the metrics for the font. |
- HDC hdc = ::GetDC(NULL); |
- HGDIOBJ old_font = SelectObject(hdc, font_.GetNativeFont()); |
- TEXTMETRIC tm = {0}; |
- GetTextMetrics(hdc, &tm); |
- const float kXHeightRatio = 0.7f; // The ratio of a font's x-height to its |
- // cap height. Sadly, Windows doesn't |
- // provide a true value for a font's |
- // x-height in its text metrics, so we |
- // approximate. |
- font_x_height_ = static_cast<int>((static_cast<float>(font_.GetBaseline() - |
- tm.tmInternalLeading) * kXHeightRatio) + 0.5); |
- // The distance from the top of the field to the desired baseline of the |
- // rendered text. |
- const int kTextBaseline = popup_window_mode_ ? 15 : 18; |
- font_y_adjustment_ = kTextBaseline - font_.GetBaseline(); |
- |
- // Get the number of twips per pixel, which we need below to offset our text |
- // by the desired number of pixels. |
- const long kTwipsPerPixel = kTwipsPerInch / GetDeviceCaps(hdc, LOGPIXELSY); |
- // It's unsafe to delete a DC with a non-stock object selected, so restore the |
- // original font. |
- SelectObject(hdc, old_font); |
- ::ReleaseDC(NULL, hdc); |
- |
- // Set the default character style -- adjust to our desired baseline. |
- CHARFORMAT cf = {0}; |
- cf.dwMask = CFM_OFFSET; |
- cf.yOffset = -font_y_adjustment_ * kTwipsPerPixel; |
- SetDefaultCharFormat(cf); |
- |
SetBackgroundColor(background_color_); |
// By default RichEdit has a drop target. Revoke it so that we can install our |
@@ -1770,6 +1741,10 @@ void OmniboxViewWin::OnPaint(HDC bogus_hdc) { |
// edit to paint into a memory DC, which we also paint onto, then blit the |
// whole thing to the screen. |
+ // Font layout members should be set before we ever paint. |
+ DCHECK_NE(font_x_height_, -1); |
+ DCHECK_NE(font_y_adjustment_, -1); |
+ |
// Don't paint if not necessary. |
CRect paint_clip_rect; |
if (!GetUpdateRect(&paint_clip_rect, true)) |
@@ -1912,6 +1887,52 @@ void OmniboxViewWin::OnSysChar(TCHAR ch, |
void OmniboxViewWin::OnWindowPosChanging(WINDOWPOS* window_pos) { |
if (force_hidden_) |
window_pos->flags &= ~SWP_SHOWWINDOW; |
+ |
+ // The first time this method is called is the earliest we know the |
+ // dimensions of the view, which we must wait for so that we can |
+ // calculate our font layout members, font_x_height_ and |
+ // font_y_adjustment_. |
Peter Kasting
2012/05/16 21:34:34
Instead of doing this, pass in the expected height
|
+ if (font_x_height_ == -1) { |
+ DCHECK_EQ(font_y_adjustment_, -1); |
+ |
+ // Get the metrics for the font. |
+ HDC hdc = ::GetDC(NULL); |
+ HGDIOBJ old_font = SelectObject(hdc, font_.GetNativeFont()); |
+ TEXTMETRIC tm = {0}; |
+ GetTextMetrics(hdc, &tm); |
+ int cap_height = font_.GetBaseline() - tm.tmInternalLeading; |
+ const float kXHeightRatio = 0.7f; // The ratio of a font's x-height to its |
+ // cap height. Sadly, Windows doesn't |
+ // provide a true value for a font's |
+ // x-height in its text metrics, so we |
+ // approximate. |
+ font_x_height_ = static_cast<int>( |
+ (static_cast<float>(cap_height) * kXHeightRatio) + 0.5); |
+ |
+ // We set font_y_adjustment_ so that the ascender of the font gets |
+ // centered on the available height of the view. |
+ DCHECK_GT(window_pos->cy, cap_height); |
+ font_y_adjustment_ = |
+ (window_pos->cy - cap_height) / 2 + cap_height - font_.GetBaseline(); |
+ |
+ // Get the number of twips per pixel, which we need below to offset our text |
+ // by the desired number of pixels. |
+ const long kTwipsPerPixel = kTwipsPerInch / GetDeviceCaps(hdc, LOGPIXELSY); |
+ // It's unsafe to delete a DC with a non-stock object selected, so |
+ // restore the original font. |
+ SelectObject(hdc, old_font); |
+ ::ReleaseDC(NULL, hdc); |
+ |
+ // Set the default character style -- adjust to our desired baseline. |
+ CHARFORMAT cf = {0}; |
+ cf.dwMask = CFM_OFFSET; |
+ cf.yOffset = -font_y_adjustment_ * kTwipsPerPixel; |
+ SetDefaultCharFormat(cf); |
+ |
+ DCHECK_NE(font_x_height_, -1); |
+ DCHECK_NE(font_y_adjustment_, -1); |
+ } |
+ |
SetMsgHandled(true); |
} |