Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/omnibox/omnibox_view_win.h" | 5 #include "chrome/browser/ui/views/omnibox/omnibox_view_win.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <locale> | 8 #include <locale> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 447 command_updater_(command_updater), | 447 command_updater_(command_updater), |
| 448 popup_window_mode_(popup_window_mode), | 448 popup_window_mode_(popup_window_mode), |
| 449 force_hidden_(false), | 449 force_hidden_(false), |
| 450 tracking_click_(), | 450 tracking_click_(), |
| 451 tracking_double_click_(false), | 451 tracking_double_click_(false), |
| 452 double_click_time_(0), | 452 double_click_time_(0), |
| 453 can_discard_mousemove_(false), | 453 can_discard_mousemove_(false), |
| 454 ignore_ime_messages_(false), | 454 ignore_ime_messages_(false), |
| 455 delete_at_end_pressed_(false), | 455 delete_at_end_pressed_(false), |
| 456 font_(parent_view->font()), | 456 font_(parent_view->font()), |
| 457 font_x_height_(-1), | |
| 458 font_y_adjustment_(-1), | |
| 457 possible_drag_(false), | 459 possible_drag_(false), |
| 458 in_drag_(false), | 460 in_drag_(false), |
| 459 initiated_drag_(false), | 461 initiated_drag_(false), |
| 460 drop_highlight_position_(-1), | 462 drop_highlight_position_(-1), |
| 461 ime_candidate_window_open_(false), | 463 ime_candidate_window_open_(false), |
| 462 background_color_(skia::SkColorToCOLORREF(LocationBarView::GetColor( | 464 background_color_(skia::SkColorToCOLORREF(LocationBarView::GetColor( |
| 463 ToolbarModel::NONE, LocationBarView::BACKGROUND))), | 465 ToolbarModel::NONE, LocationBarView::BACKGROUND))), |
| 464 security_level_(ToolbarModel::NONE), | 466 security_level_(ToolbarModel::NONE), |
| 465 text_object_model_(NULL) { | 467 text_object_model_(NULL) { |
| 466 // Dummy call to a function exported by riched20.dll to ensure it sets up an | 468 // Dummy call to a function exported by riched20.dll to ensure it sets up an |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 484 keyboard_.CreateInstance(CLSID_TextInputPanel, NULL, CLSCTX_INPROC); | 486 keyboard_.CreateInstance(CLSID_TextInputPanel, NULL, CLSCTX_INPROC); |
| 485 if (keyboard_ != NULL) | 487 if (keyboard_ != NULL) |
| 486 keyboard_->put_AttachedEditWindow(m_hWnd); | 488 keyboard_->put_AttachedEditWindow(m_hWnd); |
| 487 } | 489 } |
| 488 | 490 |
| 489 // NOTE: Do not use SetWordBreakProcEx() here, that is no longer supported as | 491 // NOTE: Do not use SetWordBreakProcEx() here, that is no longer supported as |
| 490 // of Rich Edit 2.0 onward. | 492 // of Rich Edit 2.0 onward. |
| 491 SendMessage(m_hWnd, EM_SETWORDBREAKPROC, 0, | 493 SendMessage(m_hWnd, EM_SETWORDBREAKPROC, 0, |
| 492 reinterpret_cast<LPARAM>(&WordBreakProc)); | 494 reinterpret_cast<LPARAM>(&WordBreakProc)); |
| 493 | 495 |
| 494 // Get the metrics for the font. | |
| 495 HDC hdc = ::GetDC(NULL); | |
| 496 HGDIOBJ old_font = SelectObject(hdc, font_.GetNativeFont()); | |
| 497 TEXTMETRIC tm = {0}; | |
| 498 GetTextMetrics(hdc, &tm); | |
| 499 const float kXHeightRatio = 0.7f; // The ratio of a font's x-height to its | |
| 500 // cap height. Sadly, Windows doesn't | |
| 501 // provide a true value for a font's | |
| 502 // x-height in its text metrics, so we | |
| 503 // approximate. | |
| 504 font_x_height_ = static_cast<int>((static_cast<float>(font_.GetBaseline() - | |
| 505 tm.tmInternalLeading) * kXHeightRatio) + 0.5); | |
| 506 // The distance from the top of the field to the desired baseline of the | |
| 507 // rendered text. | |
| 508 const int kTextBaseline = popup_window_mode_ ? 15 : 18; | |
| 509 font_y_adjustment_ = kTextBaseline - font_.GetBaseline(); | |
| 510 | |
| 511 // Get the number of twips per pixel, which we need below to offset our text | |
| 512 // by the desired number of pixels. | |
| 513 const long kTwipsPerPixel = kTwipsPerInch / GetDeviceCaps(hdc, LOGPIXELSY); | |
| 514 // It's unsafe to delete a DC with a non-stock object selected, so restore the | |
| 515 // original font. | |
| 516 SelectObject(hdc, old_font); | |
| 517 ::ReleaseDC(NULL, hdc); | |
| 518 | |
| 519 // Set the default character style -- adjust to our desired baseline. | |
| 520 CHARFORMAT cf = {0}; | |
| 521 cf.dwMask = CFM_OFFSET; | |
| 522 cf.yOffset = -font_y_adjustment_ * kTwipsPerPixel; | |
| 523 SetDefaultCharFormat(cf); | |
| 524 | |
| 525 SetBackgroundColor(background_color_); | 496 SetBackgroundColor(background_color_); |
| 526 | 497 |
| 527 // By default RichEdit has a drop target. Revoke it so that we can install our | 498 // By default RichEdit has a drop target. Revoke it so that we can install our |
| 528 // own. Revoke takes care of deleting the existing one. | 499 // own. Revoke takes care of deleting the existing one. |
| 529 RevokeDragDrop(m_hWnd); | 500 RevokeDragDrop(m_hWnd); |
| 530 | 501 |
| 531 // Register our drop target. RichEdit appears to invoke RevokeDropTarget when | 502 // Register our drop target. RichEdit appears to invoke RevokeDropTarget when |
| 532 // done so that we don't have to explicitly. | 503 // done so that we don't have to explicitly. |
| 533 if (!popup_window_mode_) { | 504 if (!popup_window_mode_) { |
| 534 scoped_refptr<EditDropTarget> drop_target = new EditDropTarget(this); | 505 scoped_refptr<EditDropTarget> drop_target = new EditDropTarget(this); |
| (...skipping 1228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1763 } | 1734 } |
| 1764 } | 1735 } |
| 1765 | 1736 |
| 1766 void OmniboxViewWin::OnPaint(HDC bogus_hdc) { | 1737 void OmniboxViewWin::OnPaint(HDC bogus_hdc) { |
| 1767 // We need to paint over the top of the edit. If we simply let the edit do | 1738 // We need to paint over the top of the edit. If we simply let the edit do |
| 1768 // its default painting, then do ours into the window DC, the screen is | 1739 // its default painting, then do ours into the window DC, the screen is |
| 1769 // updated in between and we can get flicker. To avoid this, we force the | 1740 // updated in between and we can get flicker. To avoid this, we force the |
| 1770 // edit to paint into a memory DC, which we also paint onto, then blit the | 1741 // edit to paint into a memory DC, which we also paint onto, then blit the |
| 1771 // whole thing to the screen. | 1742 // whole thing to the screen. |
| 1772 | 1743 |
| 1744 // Font layout members should be set before we ever paint. | |
| 1745 DCHECK_NE(font_x_height_, -1); | |
| 1746 DCHECK_NE(font_y_adjustment_, -1); | |
| 1747 | |
| 1773 // Don't paint if not necessary. | 1748 // Don't paint if not necessary. |
| 1774 CRect paint_clip_rect; | 1749 CRect paint_clip_rect; |
| 1775 if (!GetUpdateRect(&paint_clip_rect, true)) | 1750 if (!GetUpdateRect(&paint_clip_rect, true)) |
| 1776 return; | 1751 return; |
| 1777 | 1752 |
| 1778 // Begin painting, and create a memory DC for the edit to paint into. | 1753 // Begin painting, and create a memory DC for the edit to paint into. |
| 1779 CPaintDC paint_dc(m_hWnd); | 1754 CPaintDC paint_dc(m_hWnd); |
| 1780 CDC memory_dc(CreateCompatibleDC(paint_dc)); | 1755 CDC memory_dc(CreateCompatibleDC(paint_dc)); |
| 1781 CRect rect; | 1756 CRect rect; |
| 1782 GetClientRect(&rect); | 1757 GetClientRect(&rect); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1905 // * alt-space gets translated by the default WM_SYSCHAR handler to a | 1880 // * alt-space gets translated by the default WM_SYSCHAR handler to a |
| 1906 // WM_SYSCOMMAND to open the application context menu, so we need to allow | 1881 // WM_SYSCOMMAND to open the application context menu, so we need to allow |
| 1907 // it through. | 1882 // it through. |
| 1908 if (ch == VK_SPACE) | 1883 if (ch == VK_SPACE) |
| 1909 SetMsgHandled(false); | 1884 SetMsgHandled(false); |
| 1910 } | 1885 } |
| 1911 | 1886 |
| 1912 void OmniboxViewWin::OnWindowPosChanging(WINDOWPOS* window_pos) { | 1887 void OmniboxViewWin::OnWindowPosChanging(WINDOWPOS* window_pos) { |
| 1913 if (force_hidden_) | 1888 if (force_hidden_) |
| 1914 window_pos->flags &= ~SWP_SHOWWINDOW; | 1889 window_pos->flags &= ~SWP_SHOWWINDOW; |
| 1890 | |
| 1891 // The first time this method is called is the earliest we know the | |
| 1892 // dimensions of the view, which we must wait for so that we can | |
| 1893 // calculate our font layout members, font_x_height_ and | |
| 1894 // font_y_adjustment_. | |
|
Peter Kasting
2012/05/16 21:34:34
Instead of doing this, pass in the expected height
| |
| 1895 if (font_x_height_ == -1) { | |
| 1896 DCHECK_EQ(font_y_adjustment_, -1); | |
| 1897 | |
| 1898 // Get the metrics for the font. | |
| 1899 HDC hdc = ::GetDC(NULL); | |
| 1900 HGDIOBJ old_font = SelectObject(hdc, font_.GetNativeFont()); | |
| 1901 TEXTMETRIC tm = {0}; | |
| 1902 GetTextMetrics(hdc, &tm); | |
| 1903 int cap_height = font_.GetBaseline() - tm.tmInternalLeading; | |
| 1904 const float kXHeightRatio = 0.7f; // The ratio of a font's x-height to its | |
| 1905 // cap height. Sadly, Windows doesn't | |
| 1906 // provide a true value for a font's | |
| 1907 // x-height in its text metrics, so we | |
| 1908 // approximate. | |
| 1909 font_x_height_ = static_cast<int>( | |
| 1910 (static_cast<float>(cap_height) * kXHeightRatio) + 0.5); | |
| 1911 | |
| 1912 // We set font_y_adjustment_ so that the ascender of the font gets | |
| 1913 // centered on the available height of the view. | |
| 1914 DCHECK_GT(window_pos->cy, cap_height); | |
| 1915 font_y_adjustment_ = | |
| 1916 (window_pos->cy - cap_height) / 2 + cap_height - font_.GetBaseline(); | |
| 1917 | |
| 1918 // Get the number of twips per pixel, which we need below to offset our text | |
| 1919 // by the desired number of pixels. | |
| 1920 const long kTwipsPerPixel = kTwipsPerInch / GetDeviceCaps(hdc, LOGPIXELSY); | |
| 1921 // It's unsafe to delete a DC with a non-stock object selected, so | |
| 1922 // restore the original font. | |
| 1923 SelectObject(hdc, old_font); | |
| 1924 ::ReleaseDC(NULL, hdc); | |
| 1925 | |
| 1926 // Set the default character style -- adjust to our desired baseline. | |
| 1927 CHARFORMAT cf = {0}; | |
| 1928 cf.dwMask = CFM_OFFSET; | |
| 1929 cf.yOffset = -font_y_adjustment_ * kTwipsPerPixel; | |
| 1930 SetDefaultCharFormat(cf); | |
| 1931 | |
| 1932 DCHECK_NE(font_x_height_, -1); | |
| 1933 DCHECK_NE(font_y_adjustment_, -1); | |
| 1934 } | |
| 1935 | |
| 1915 SetMsgHandled(true); | 1936 SetMsgHandled(true); |
| 1916 } | 1937 } |
| 1917 | 1938 |
| 1918 BOOL OmniboxViewWin::OnMouseWheel(UINT flags, short delta, CPoint point) { | 1939 BOOL OmniboxViewWin::OnMouseWheel(UINT flags, short delta, CPoint point) { |
| 1919 // Forward the mouse-wheel message to the window under the mouse. | 1940 // Forward the mouse-wheel message to the window under the mouse. |
| 1920 if (!ui::RerouteMouseWheel(m_hWnd, MAKEWPARAM(flags, delta), | 1941 if (!ui::RerouteMouseWheel(m_hWnd, MAKEWPARAM(flags, delta), |
| 1921 MAKELPARAM(point.x, point.y))) | 1942 MAKELPARAM(point.x, point.y))) |
| 1922 SetMsgHandled(false); | 1943 SetMsgHandled(false); |
| 1923 return 0; | 1944 return 0; |
| 1924 } | 1945 } |
| (...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2684 bool popup_window_mode, | 2705 bool popup_window_mode, |
| 2685 LocationBarView* location_bar) { | 2706 LocationBarView* location_bar) { |
| 2686 return new OmniboxViewWin(controller, | 2707 return new OmniboxViewWin(controller, |
| 2687 toolbar_model, | 2708 toolbar_model, |
| 2688 location_bar, | 2709 location_bar, |
| 2689 command_updater, | 2710 command_updater, |
| 2690 popup_window_mode, | 2711 popup_window_mode, |
| 2691 location_bar); | 2712 location_bar); |
| 2692 } | 2713 } |
| 2693 #endif | 2714 #endif |
| OLD | NEW |