OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
456 | 456 |
457 } // namespace | 457 } // namespace |
458 | 458 |
459 HMODULE OmniboxViewWin::loaded_library_module_ = NULL; | 459 HMODULE OmniboxViewWin::loaded_library_module_ = NULL; |
460 | 460 |
461 OmniboxViewWin::OmniboxViewWin(OmniboxEditController* controller, | 461 OmniboxViewWin::OmniboxViewWin(OmniboxEditController* controller, |
462 ToolbarModel* toolbar_model, | 462 ToolbarModel* toolbar_model, |
463 LocationBarView* location_bar, | 463 LocationBarView* location_bar, |
464 CommandUpdater* command_updater, | 464 CommandUpdater* command_updater, |
465 bool popup_window_mode, | 465 bool popup_window_mode, |
466 const gfx::Font& font, | 466 const gfx::FontList& font_list, |
467 int font_y_offset) | 467 int font_y_offset) |
468 : OmniboxView(location_bar->profile(), controller, toolbar_model, | 468 : OmniboxView(location_bar->profile(), controller, toolbar_model, |
469 command_updater), | 469 command_updater), |
470 popup_view_( | 470 popup_view_(OmniboxPopupContentsView::Create( |
471 OmniboxPopupContentsView::Create(font, this, model(), location_bar)), | 471 font_list, this, model(), location_bar)), |
Peter Kasting
2013/07/26 20:54:27
Nit: Indent 4, not 16
Yuki
2013/07/29 15:27:21
Done.
| |
472 location_bar_(location_bar), | 472 location_bar_(location_bar), |
473 popup_window_mode_(popup_window_mode), | 473 popup_window_mode_(popup_window_mode), |
474 force_hidden_(false), | 474 force_hidden_(false), |
475 tracking_click_(), | 475 tracking_click_(), |
476 tracking_double_click_(false), | 476 tracking_double_click_(false), |
477 double_click_time_(0), | 477 double_click_time_(0), |
478 can_discard_mousemove_(false), | 478 can_discard_mousemove_(false), |
479 ignore_ime_messages_(false), | 479 ignore_ime_messages_(false), |
480 delete_at_end_pressed_(false), | 480 delete_at_end_pressed_(false), |
481 font_(font), | 481 font_list_(font_list), |
482 font_y_adjustment_(font_y_offset), | 482 font_y_adjustment_(font_y_offset), |
483 possible_drag_(false), | 483 possible_drag_(false), |
484 in_drag_(false), | 484 in_drag_(false), |
485 initiated_drag_(false), | 485 initiated_drag_(false), |
486 drop_highlight_position_(-1), | 486 drop_highlight_position_(-1), |
487 ime_candidate_window_open_(false), | 487 ime_candidate_window_open_(false), |
488 background_color_(skia::SkColorToCOLORREF(location_bar->GetColor( | 488 background_color_(skia::SkColorToCOLORREF(location_bar->GetColor( |
489 ToolbarModel::NONE, LocationBarView::BACKGROUND))), | 489 ToolbarModel::NONE, LocationBarView::BACKGROUND))), |
490 security_level_(ToolbarModel::NONE), | 490 security_level_(ToolbarModel::NONE), |
491 text_object_model_(NULL), | 491 text_object_model_(NULL), |
492 tsf_event_router_(base::win::IsTSFAwareRequired() ? | 492 tsf_event_router_(base::win::IsTSFAwareRequired() ? |
493 new ui::TSFEventRouter(this) : NULL) { | 493 new ui::TSFEventRouter(this) : NULL) { |
494 if (!loaded_library_module_) | 494 if (!loaded_library_module_) |
495 loaded_library_module_ = LoadLibrary(kRichEditDLLName); | 495 loaded_library_module_ = LoadLibrary(kRichEditDLLName); |
496 // RichEdit should be available; rare exceptions should use the Views omnibox. | 496 // RichEdit should be available; rare exceptions should use the Views omnibox. |
497 DCHECK(loaded_library_module_); | 497 DCHECK(loaded_library_module_); |
498 | 498 |
499 saved_selection_for_focus_change_.cpMin = -1; | 499 saved_selection_for_focus_change_.cpMin = -1; |
500 | 500 |
501 g_paint_patcher.Pointer()->RefPatch(); | 501 g_paint_patcher.Pointer()->RefPatch(); |
502 | 502 |
503 Create(location_bar->GetWidget()->GetNativeView(), 0, 0, 0, | 503 Create(location_bar->GetWidget()->GetNativeView(), 0, 0, 0, |
504 l10n_util::GetExtendedStyles()); | 504 l10n_util::GetExtendedStyles()); |
505 SetReadOnly(popup_window_mode_); | 505 SetReadOnly(popup_window_mode_); |
506 gfx::NativeFont native_font(font_.GetNativeFont()); | 506 gfx::NativeFont native_font(font_list_.GetPrimaryFont().GetNativeFont()); |
507 SetFont(native_font); | 507 SetFont(native_font); |
508 | 508 |
509 // IMF_DUALFONT (on by default) is supposed to use one font for ASCII text | 509 // IMF_DUALFONT (on by default) is supposed to use one font for ASCII text |
510 // and a different one for Asian text. In some cases, ASCII characters may | 510 // and a different one for Asian text. In some cases, ASCII characters may |
511 // be mis-marked as Asian, e.g. because input from the keyboard may be | 511 // be mis-marked as Asian, e.g. because input from the keyboard may be |
512 // auto-stamped with the keyboard language. As a result adjacent characters | 512 // auto-stamped with the keyboard language. As a result adjacent characters |
513 // can render in different fonts, which looks bizarre. To fix this we | 513 // can render in different fonts, which looks bizarre. To fix this we |
514 // disable dual-font mode, which forces the control to use a single font for | 514 // disable dual-font mode, which forces the control to use a single font for |
515 // everything. | 515 // everything. |
516 // Note that we should not disable the very similar IMF_AUTOFONT flag, which | 516 // Note that we should not disable the very similar IMF_AUTOFONT flag, which |
517 // allows the control to hunt for fonts that can display all the current | 517 // allows the control to hunt for fonts that can display all the current |
518 // characters; doing this results in "missing glyph" boxes when the user | 518 // characters; doing this results in "missing glyph" boxes when the user |
519 // enters characters not available in the currently-chosen font. | 519 // enters characters not available in the currently-chosen font. |
520 const LRESULT lang_options = SendMessage(m_hWnd, EM_GETLANGOPTIONS, 0, 0); | 520 const LRESULT lang_options = SendMessage(m_hWnd, EM_GETLANGOPTIONS, 0, 0); |
521 SendMessage(m_hWnd, EM_SETLANGOPTIONS, 0, lang_options & ~IMF_DUALFONT); | 521 SendMessage(m_hWnd, EM_SETLANGOPTIONS, 0, lang_options & ~IMF_DUALFONT); |
522 | 522 |
523 // NOTE: Do not use SetWordBreakProcEx() here, that is no longer supported as | 523 // NOTE: Do not use SetWordBreakProcEx() here, that is no longer supported as |
524 // of Rich Edit 2.0 onward. | 524 // of Rich Edit 2.0 onward. |
525 SendMessage(m_hWnd, EM_SETWORDBREAKPROC, 0, | 525 SendMessage(m_hWnd, EM_SETWORDBREAKPROC, 0, |
526 reinterpret_cast<LPARAM>(&WordBreakProc)); | 526 reinterpret_cast<LPARAM>(&WordBreakProc)); |
527 | 527 |
528 // Get the metrics for the font. | 528 // Get the metrics for the font. |
529 base::win::ScopedGetDC screen_dc(NULL); | 529 base::win::ScopedGetDC screen_dc(NULL); |
530 base::win::ScopedSelectObject font_in_dc(screen_dc, native_font); | 530 base::win::ScopedSelectObject font_in_dc(screen_dc, native_font); |
531 TEXTMETRIC tm = {0}; | 531 TEXTMETRIC tm = {0}; |
532 GetTextMetrics(screen_dc, &tm); | 532 GetTextMetrics(screen_dc, &tm); |
533 int cap_height = font_.GetBaseline() - tm.tmInternalLeading; | 533 int cap_height = font_list_.GetBaseline() - tm.tmInternalLeading; |
534 // The ratio of a font's x-height to its cap height. Sadly, Windows | 534 // The ratio of a font's x-height to its cap height. Sadly, Windows |
535 // doesn't provide a true value for a font's x-height in its text | 535 // doesn't provide a true value for a font's x-height in its text |
536 // metrics, so we approximate. | 536 // metrics, so we approximate. |
537 const float kXHeightRatio = 0.7f; | 537 const float kXHeightRatio = 0.7f; |
538 font_x_height_ = static_cast<int>( | 538 font_x_height_ = static_cast<int>( |
539 (static_cast<float>(cap_height) * kXHeightRatio) + 0.5); | 539 (static_cast<float>(cap_height) * kXHeightRatio) + 0.5); |
540 | 540 |
541 // Get the number of twips per pixel, which we need below to offset our text | 541 // Get the number of twips per pixel, which we need below to offset our text |
542 // by the desired number of pixels. | 542 // by the desired number of pixels. |
543 const long kTwipsPerPixel = | 543 const long kTwipsPerPixel = |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
806 // window resize will effectively restore caret visibility, regardless of | 806 // window resize will effectively restore caret visibility, regardless of |
807 // whether HideCaret() was called before. While we do catch and handle these | 807 // whether HideCaret() was called before. While we do catch and handle these |
808 // paint events (see OnPaint()), it doesn't seem to be enough to simply call | 808 // paint events (see OnPaint()), it doesn't seem to be enough to simply call |
809 // HideCaret() while handling them because of the unpredictability of this | 809 // HideCaret() while handling them because of the unpredictability of this |
810 // Windows API. According to the documentation, it should be a cumulative call | 810 // Windows API. According to the documentation, it should be a cumulative call |
811 // e.g. 5 hide calls should be balanced by 5 show calls. We have not found | 811 // e.g. 5 hide calls should be balanced by 5 show calls. We have not found |
812 // this to be true, which may be explained by the fact that this API is called | 812 // this to be true, which may be explained by the fact that this API is called |
813 // internally in Windows, as well. | 813 // internally in Windows, as well. |
814 ::DestroyCaret(); | 814 ::DestroyCaret(); |
815 if (model()->is_caret_visible()) { | 815 if (model()->is_caret_visible()) { |
816 ::CreateCaret(m_hWnd, (HBITMAP) NULL, 1, font_.GetHeight()); | 816 ::CreateCaret(m_hWnd, (HBITMAP) NULL, 1, font_list_.GetHeight()); |
817 // According to the Windows API documentation, a newly created caret needs | 817 // According to the Windows API documentation, a newly created caret needs |
818 // ShowCaret to be visible. | 818 // ShowCaret to be visible. |
819 ShowCaret(); | 819 ShowCaret(); |
820 } | 820 } |
821 } | 821 } |
822 | 822 |
823 void OmniboxViewWin::SetDropHighlightPosition(int position) { | 823 void OmniboxViewWin::SetDropHighlightPosition(int position) { |
824 if (drop_highlight_position_ != position) { | 824 if (drop_highlight_position_ != position) { |
825 RepaintDropHighlight(drop_highlight_position_); | 825 RepaintDropHighlight(drop_highlight_position_); |
826 drop_highlight_position_ = position; | 826 drop_highlight_position_ = position; |
(...skipping 1683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2510 | 2510 |
2511 // Calculate the rect, in window coordinates, containing the portion of the | 2511 // Calculate the rect, in window coordinates, containing the portion of the |
2512 // scheme where we'll be drawing the slash. Vertically, we draw across one | 2512 // scheme where we'll be drawing the slash. Vertically, we draw across one |
2513 // x-height of text, plus an additional 3 stroke diameters (the stroke width | 2513 // x-height of text, plus an additional 3 stroke diameters (the stroke width |
2514 // plus a half-stroke width of space between the stroke and the text, both | 2514 // plus a half-stroke width of space between the stroke and the text, both |
2515 // above and below the text). | 2515 // above and below the text). |
2516 const int font_top = client_rect.top + font_y_adjustment_; | 2516 const int font_top = client_rect.top + font_y_adjustment_; |
2517 const SkScalar kStrokeWidthPixels = SkIntToScalar(2); | 2517 const SkScalar kStrokeWidthPixels = SkIntToScalar(2); |
2518 const int kAdditionalSpaceOutsideFont = | 2518 const int kAdditionalSpaceOutsideFont = |
2519 static_cast<int>(ceil(kStrokeWidthPixels * 1.5f)); | 2519 static_cast<int>(ceil(kStrokeWidthPixels * 1.5f)); |
2520 const int font_ascent = font_.GetBaseline(); | 2520 const int font_ascent = font_list_.GetBaseline(); |
2521 const CRect scheme_rect( | 2521 const CRect scheme_rect( |
2522 PosFromChar(insecure_scheme_component_.begin).x, | 2522 PosFromChar(insecure_scheme_component_.begin).x, |
2523 font_top + font_ascent - font_x_height_ - kAdditionalSpaceOutsideFont, | 2523 font_top + font_ascent - font_x_height_ - kAdditionalSpaceOutsideFont, |
2524 PosFromChar(insecure_scheme_component_.end()).x, | 2524 PosFromChar(insecure_scheme_component_.end()).x, |
2525 font_top + font_ascent + kAdditionalSpaceOutsideFont); | 2525 font_top + font_ascent + kAdditionalSpaceOutsideFont); |
2526 | 2526 |
2527 // Clip to the portion we care about and translate to canvas coordinates | 2527 // Clip to the portion we care about and translate to canvas coordinates |
2528 // (see the canvas creation below) for use later. | 2528 // (see the canvas creation below) for use later. |
2529 CRect canvas_clip_rect, canvas_paint_clip_rect; | 2529 CRect canvas_clip_rect, canvas_paint_clip_rect; |
2530 canvas_clip_rect.IntersectRect(scheme_rect, client_rect); | 2530 canvas_clip_rect.IntersectRect(scheme_rect, client_rect); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2598 void OmniboxViewWin::DrawDropHighlight(HDC hdc, | 2598 void OmniboxViewWin::DrawDropHighlight(HDC hdc, |
2599 const CRect& client_rect, | 2599 const CRect& client_rect, |
2600 const CRect& paint_clip_rect) { | 2600 const CRect& paint_clip_rect) { |
2601 DCHECK_NE(-1, drop_highlight_position_); | 2601 DCHECK_NE(-1, drop_highlight_position_); |
2602 | 2602 |
2603 const int highlight_y = client_rect.top + font_y_adjustment_; | 2603 const int highlight_y = client_rect.top + font_y_adjustment_; |
2604 const int highlight_x = PosFromChar(drop_highlight_position_).x - 1; | 2604 const int highlight_x = PosFromChar(drop_highlight_position_).x - 1; |
2605 const CRect highlight_rect(highlight_x, | 2605 const CRect highlight_rect(highlight_x, |
2606 highlight_y, | 2606 highlight_y, |
2607 highlight_x + 1, | 2607 highlight_x + 1, |
2608 highlight_y + font_.GetHeight()); | 2608 highlight_y + font_list_.GetHeight()); |
2609 | 2609 |
2610 // Clip the highlight to the region being painted. | 2610 // Clip the highlight to the region being painted. |
2611 CRect clip_rect; | 2611 CRect clip_rect; |
2612 clip_rect.IntersectRect(highlight_rect, paint_clip_rect); | 2612 clip_rect.IntersectRect(highlight_rect, paint_clip_rect); |
2613 if (clip_rect.IsRectNull()) | 2613 if (clip_rect.IsRectNull()) |
2614 return; | 2614 return; |
2615 | 2615 |
2616 HGDIOBJ last_pen = SelectObject(hdc, CreatePen(PS_SOLID, 1, RGB(0, 0, 0))); | 2616 HGDIOBJ last_pen = SelectObject(hdc, CreatePen(PS_SOLID, 1, RGB(0, 0, 0))); |
2617 MoveToEx(hdc, clip_rect.left, clip_rect.top, NULL); | 2617 MoveToEx(hdc, clip_rect.left, clip_rect.top, NULL); |
2618 LineTo(hdc, clip_rect.left, clip_rect.bottom); | 2618 LineTo(hdc, clip_rect.left, clip_rect.bottom); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2753 // a drag regardless of the y-coordinate. | 2753 // a drag regardless of the y-coordinate. |
2754 possible_drag_ = (point.x >= min_sel_location.x) && | 2754 possible_drag_ = (point.x >= min_sel_location.x) && |
2755 (point.x < max_sel_location.x); | 2755 (point.x < max_sel_location.x); |
2756 } | 2756 } |
2757 } | 2757 } |
2758 | 2758 |
2759 void OmniboxViewWin::RepaintDropHighlight(int position) { | 2759 void OmniboxViewWin::RepaintDropHighlight(int position) { |
2760 if ((position != -1) && (position <= GetTextLength())) { | 2760 if ((position != -1) && (position <= GetTextLength())) { |
2761 const POINT min_loc(PosFromChar(position)); | 2761 const POINT min_loc(PosFromChar(position)); |
2762 const RECT highlight_bounds = {min_loc.x - 1, font_y_adjustment_, | 2762 const RECT highlight_bounds = {min_loc.x - 1, font_y_adjustment_, |
2763 min_loc.x + 2, font_.GetHeight() + font_y_adjustment_}; | 2763 min_loc.x + 2, font_list_.GetHeight() + font_y_adjustment_}; |
2764 InvalidateRect(&highlight_bounds, false); | 2764 InvalidateRect(&highlight_bounds, false); |
2765 } | 2765 } |
2766 } | 2766 } |
2767 | 2767 |
2768 void OmniboxViewWin::BuildContextMenu() { | 2768 void OmniboxViewWin::BuildContextMenu() { |
2769 if (context_menu_contents_.get()) | 2769 if (context_menu_contents_.get()) |
2770 return; | 2770 return; |
2771 | 2771 |
2772 context_menu_contents_.reset(new ui::SimpleMenuModel(this)); | 2772 context_menu_contents_.reset(new ui::SimpleMenuModel(this)); |
2773 // Set up context menu. | 2773 // Set up context menu. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2818 | 2818 |
2819 int OmniboxViewWin::GetHorizontalMargin() const { | 2819 int OmniboxViewWin::GetHorizontalMargin() const { |
2820 RECT rect; | 2820 RECT rect; |
2821 GetRect(&rect); | 2821 GetRect(&rect); |
2822 RECT client_rect; | 2822 RECT client_rect; |
2823 GetClientRect(&client_rect); | 2823 GetClientRect(&client_rect); |
2824 return (rect.left - client_rect.left) + (client_rect.right - rect.right); | 2824 return (rect.left - client_rect.left) + (client_rect.right - rect.right); |
2825 } | 2825 } |
2826 | 2826 |
2827 int OmniboxViewWin::WidthNeededToDisplay(const string16& text) const { | 2827 int OmniboxViewWin::WidthNeededToDisplay(const string16& text) const { |
2828 // Use font_.GetStringWidth() instead of PosFromChar(GetTextLength()) because | 2828 // Use font_list_.GetPrimaryFont().GetStringWidth() instead of |
2829 // PosFromChar() is apparently buggy. In both LTR UI and RTL UI with | 2829 // PosFromChar(GetTextLength()) because PosFromChar() is apparently buggy. |
2830 // left-to-right layout, PosFromChar(i) might return 0 when i is greater than | 2830 // In both LTR UI and RTL UI with left-to-right layout, PosFromChar(i) might |
2831 // 1. | 2831 // return 0 when i is greater than 1. |
2832 return font_.GetStringWidth(text) + GetHorizontalMargin(); | 2832 return font_list_.GetPrimaryFont().GetStringWidth(text) + |
2833 GetHorizontalMargin(); | |
2833 } | 2834 } |
OLD | NEW |