OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/autocomplete/autocomplete_edit_view_win.h" | 5 #include "chrome/browser/autocomplete/autocomplete_edit_view_win.h" |
6 | 6 |
7 #include <locale> | 7 #include <locale> |
8 | 8 |
9 #include "app/clipboard/clipboard.h" | 9 #include "app/clipboard/clipboard.h" |
10 #include "app/clipboard/scoped_clipboard_writer.h" | 10 #include "app/clipboard/scoped_clipboard_writer.h" |
(...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 model_->SetUserText(text); | 609 model_->SetUserText(text); |
610 saved_selection_for_focus_change_.cpMin = -1; | 610 saved_selection_for_focus_change_.cpMin = -1; |
611 SetWindowTextAndCaretPos(display_text, display_text.length()); | 611 SetWindowTextAndCaretPos(display_text, display_text.length()); |
612 if (update_popup) | 612 if (update_popup) |
613 UpdatePopup(); | 613 UpdatePopup(); |
614 TextChanged(); | 614 TextChanged(); |
615 } | 615 } |
616 | 616 |
617 void AutocompleteEditViewWin::SetWindowTextAndCaretPos(const std::wstring& text, | 617 void AutocompleteEditViewWin::SetWindowTextAndCaretPos(const std::wstring& text, |
618 size_t caret_pos) { | 618 size_t caret_pos) { |
619 HIMC imm_context = ImmGetContext(m_hWnd); | |
620 if (imm_context) { | |
621 // In Windows Vista, SetWindowText() automatically cancels any ongoing | |
622 // IME composition, and updates the text of the underlying edit control. | |
623 // In Windows XP, however, SetWindowText() gets applied to the IME | |
624 // composition string if it exists, and doesn't update the underlying edit | |
625 // control. To avoid this, we force the IME to cancel any outstanding | |
626 // compositions here. This is harmless in Vista and in cases where the IME | |
627 // isn't composing. | |
628 | |
629 // NOTE: We MUST ignore messages like WM_IME_COMPOSITION that may be sent as | |
630 // a result of doing this. Until the SetWindowText() call below, the | |
631 // underlying edit (on XP) has out-of-date text in it; for problems this can | |
632 // cause, see OnImeComposition(). | |
633 ignore_ime_messages_ = true; | |
634 ImmNotifyIME(imm_context, NI_COMPOSITIONSTR, CPS_CANCEL, 0); | |
635 ImmReleaseContext(m_hWnd, imm_context); | |
636 ignore_ime_messages_ = false; | |
637 } | |
638 | |
639 SetWindowText(text.c_str()); | 619 SetWindowText(text.c_str()); |
640 PlaceCaretAt(caret_pos); | 620 PlaceCaretAt(caret_pos); |
641 } | 621 } |
642 | 622 |
643 void AutocompleteEditViewWin::SetForcedQuery() { | 623 void AutocompleteEditViewWin::SetForcedQuery() { |
644 const std::wstring current_text(GetText()); | 624 const std::wstring current_text(GetText()); |
645 if (current_text.empty() || (current_text[0] != '?')) | 625 if (current_text.empty() || (current_text[0] != '?')) |
646 SetUserText(L"?"); | 626 SetUserText(L"?"); |
647 else | 627 else |
648 SetSelection(current_text.length(), 1); | 628 SetSelection(current_text.length(), 1); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 size_t user_text_length) { | 775 size_t user_text_length) { |
796 // Update the text and selection. Because this can be called repeatedly while | 776 // Update the text and selection. Because this can be called repeatedly while |
797 // typing, we've careful not to freeze the edit unless we really need to. | 777 // typing, we've careful not to freeze the edit unless we really need to. |
798 // Also, unlike in the temporary text case above, here we don't want to update | 778 // Also, unlike in the temporary text case above, here we don't want to update |
799 // the caret/selection unless we have to, since this might make the user's | 779 // the caret/selection unless we have to, since this might make the user's |
800 // caret position change without warning during typing. | 780 // caret position change without warning during typing. |
801 if (display_text == GetText()) | 781 if (display_text == GetText()) |
802 return false; | 782 return false; |
803 | 783 |
804 ScopedFreeze freeze(this, GetTextObjectModel()); | 784 ScopedFreeze freeze(this, GetTextObjectModel()); |
805 // NOTE: We don't need the IME composition hack in SetWindowTextAndCaretPos() | |
806 // here, because UpdatePopup() disables inline autocomplete when a | |
807 // composition is in progress, thus preventing us from reaching this code. | |
808 SetWindowText(display_text.c_str()); | 785 SetWindowText(display_text.c_str()); |
809 // Set a reversed selection to keep the caret in the same position, which | 786 // Set a reversed selection to keep the caret in the same position, which |
810 // avoids scrolling the user's text. | 787 // avoids scrolling the user's text. |
811 SetSelection(static_cast<LONG>(display_text.length()), | 788 SetSelection(static_cast<LONG>(display_text.length()), |
812 static_cast<LONG>(user_text_length)); | 789 static_cast<LONG>(user_text_length)); |
813 TextChanged(); | 790 TextChanged(); |
814 return true; | 791 return true; |
815 } | 792 } |
816 | 793 |
817 void AutocompleteEditViewWin::OnRevertTemporaryText() { | 794 void AutocompleteEditViewWin::OnRevertTemporaryText() { |
(...skipping 892 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1710 | 1687 |
1711 // Restore saved selection if available. | 1688 // Restore saved selection if available. |
1712 if (saved_selection_for_focus_change_.cpMin != -1) { | 1689 if (saved_selection_for_focus_change_.cpMin != -1) { |
1713 SetSelectionRange(saved_selection_for_focus_change_); | 1690 SetSelectionRange(saved_selection_for_focus_change_); |
1714 saved_selection_for_focus_change_.cpMin = -1; | 1691 saved_selection_for_focus_change_.cpMin = -1; |
1715 } | 1692 } |
1716 | 1693 |
1717 SetMsgHandled(false); | 1694 SetMsgHandled(false); |
1718 } | 1695 } |
1719 | 1696 |
| 1697 LRESULT AutocompleteEditViewWin::OnSetText(const wchar_t* text) { |
| 1698 // Ignore all IME messages while we process this WM_SETTEXT message. |
| 1699 // When SetWindowText() is called while an IME is composing text, the IME |
| 1700 // calls SendMessage() to send a WM_IME_COMPOSITION message. When we receive |
| 1701 // this WM_IME_COMPOSITION message, we update the omnibox and may call |
| 1702 // SetWindowText() again. To stop this recursive message-handler call, we |
| 1703 // stop updating the omnibox while we process a WM_SETTEXT message. |
| 1704 // We wouldn't need to do this update anyway, because either we're in the |
| 1705 // middle of updating the omnibox already or the caller of SetWindowText() |
| 1706 // is going to update the omnibox next. |
| 1707 AutoReset auto_reset_ignore_ime_messages(&ignore_ime_messages_, true); |
| 1708 return DefWindowProc(WM_SETTEXT, 0, reinterpret_cast<LPARAM>(text)); |
| 1709 } |
| 1710 |
1720 void AutocompleteEditViewWin::OnSysChar(TCHAR ch, | 1711 void AutocompleteEditViewWin::OnSysChar(TCHAR ch, |
1721 UINT repeat_count, | 1712 UINT repeat_count, |
1722 UINT flags) { | 1713 UINT flags) { |
1723 // Nearly all alt-<xxx> combos result in beeping rather than doing something | 1714 // Nearly all alt-<xxx> combos result in beeping rather than doing something |
1724 // useful, so we discard most. Exceptions: | 1715 // useful, so we discard most. Exceptions: |
1725 // * ctrl-alt-<xxx>, which is sometimes important, generates WM_CHAR instead | 1716 // * ctrl-alt-<xxx>, which is sometimes important, generates WM_CHAR instead |
1726 // of WM_SYSCHAR, so it doesn't need to be handled here. | 1717 // of WM_SYSCHAR, so it doesn't need to be handled here. |
1727 // * alt-space gets translated by the default WM_SYSCHAR handler to a | 1718 // * alt-space gets translated by the default WM_SYSCHAR handler to a |
1728 // WM_SYSCOMMAND to open the application context menu, so we need to allow | 1719 // WM_SYSCOMMAND to open the application context menu, so we need to allow |
1729 // it through. | 1720 // it through. |
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2431 context_menu_contents_->AddItemWithStringId(IDS_PASTE_AND_GO, | 2422 context_menu_contents_->AddItemWithStringId(IDS_PASTE_AND_GO, |
2432 IDS_PASTE_AND_GO); | 2423 IDS_PASTE_AND_GO); |
2433 context_menu_contents_->AddSeparator(); | 2424 context_menu_contents_->AddSeparator(); |
2434 context_menu_contents_->AddItemWithStringId(IDS_SELECT_ALL, IDS_SELECT_ALL); | 2425 context_menu_contents_->AddItemWithStringId(IDS_SELECT_ALL, IDS_SELECT_ALL); |
2435 context_menu_contents_->AddSeparator(); | 2426 context_menu_contents_->AddSeparator(); |
2436 context_menu_contents_->AddItemWithStringId(IDS_EDIT_SEARCH_ENGINES, | 2427 context_menu_contents_->AddItemWithStringId(IDS_EDIT_SEARCH_ENGINES, |
2437 IDS_EDIT_SEARCH_ENGINES); | 2428 IDS_EDIT_SEARCH_ENGINES); |
2438 } | 2429 } |
2439 context_menu_.reset(new views::Menu2(context_menu_contents_.get())); | 2430 context_menu_.reset(new views::Menu2(context_menu_contents_.get())); |
2440 } | 2431 } |
OLD | NEW |