OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 |
11 #include <richedit.h> | 11 #include <richedit.h> |
12 #include <textserv.h> | 12 #include <textserv.h> |
13 | 13 |
14 #include "base/auto_reset.h" | 14 #include "base/auto_reset.h" |
15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
16 #include "base/i18n/rtl.h" | 16 #include "base/i18n/rtl.h" |
17 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
20 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
21 #include "base/win/iat_patch_function.h" | 21 #include "base/win/iat_patch_function.h" |
22 #include "chrome/app/chrome_command_ids.h" | 22 #include "chrome/app/chrome_command_ids.h" |
23 #include "chrome/browser/autocomplete/autocomplete_accessibility.h" | |
24 #include "chrome/browser/autocomplete/autocomplete_match.h" | 23 #include "chrome/browser/autocomplete/autocomplete_match.h" |
25 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" | 24 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" |
26 #include "chrome/browser/autocomplete/keyword_provider.h" | 25 #include "chrome/browser/autocomplete/keyword_provider.h" |
27 #include "chrome/browser/browser_process.h" | 26 #include "chrome/browser/browser_process.h" |
28 #include "chrome/browser/command_updater.h" | 27 #include "chrome/browser/command_updater.h" |
29 #include "chrome/browser/net/url_fixer_upper.h" | 28 #include "chrome/browser/net/url_fixer_upper.h" |
30 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
31 #include "chrome/browser/ui/browser.h" | 30 #include "chrome/browser/ui/browser.h" |
32 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 31 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
33 #include "chrome/common/chrome_notification_types.h" | 32 #include "chrome/common/chrome_notification_types.h" |
34 #include "content/browser/tab_contents/tab_contents.h" | 33 #include "content/browser/tab_contents/tab_contents.h" |
35 #include "content/browser/user_metrics.h" | 34 #include "content/browser/user_metrics.h" |
36 #include "googleurl/src/url_util.h" | 35 #include "googleurl/src/url_util.h" |
37 #include "grit/generated_resources.h" | 36 #include "grit/generated_resources.h" |
38 #include "net/base/escape.h" | 37 #include "net/base/escape.h" |
39 #include "skia/ext/skia_utils_win.h" | 38 #include "skia/ext/skia_utils_win.h" |
| 39 #include "ui/base/accessibility/accessible_view_state.h" |
40 #include "ui/base/clipboard/clipboard.h" | 40 #include "ui/base/clipboard/clipboard.h" |
41 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 41 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
42 #include "ui/base/dragdrop/drag_drop_types.h" | 42 #include "ui/base/dragdrop/drag_drop_types.h" |
43 #include "ui/base/dragdrop/drag_source.h" | 43 #include "ui/base/dragdrop/drag_source.h" |
44 #include "ui/base/dragdrop/drop_target.h" | 44 #include "ui/base/dragdrop/drop_target.h" |
45 #include "ui/base/dragdrop/os_exchange_data.h" | 45 #include "ui/base/dragdrop/os_exchange_data.h" |
46 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" | 46 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" |
47 #include "ui/base/events.h" | 47 #include "ui/base/events.h" |
48 #include "ui/base/keycodes/keyboard_codes.h" | 48 #include "ui/base/keycodes/keyboard_codes.h" |
49 #include "ui/base/l10n/l10n_util.h" | 49 #include "ui/base/l10n/l10n_util.h" |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 if (text_object_model_) | 299 if (text_object_model_) |
300 text_object_model_->Undo(tomSuspend, NULL); | 300 text_object_model_->Undo(tomSuspend, NULL); |
301 } | 301 } |
302 | 302 |
303 OmniboxViewWin::ScopedSuspendUndo::~ScopedSuspendUndo() { | 303 OmniboxViewWin::ScopedSuspendUndo::~ScopedSuspendUndo() { |
304 // Resume Undo processing. | 304 // Resume Undo processing. |
305 if (text_object_model_) | 305 if (text_object_model_) |
306 text_object_model_->Undo(tomResume, NULL); | 306 text_object_model_->Undo(tomResume, NULL); |
307 } | 307 } |
308 | 308 |
| 309 // A subclass of NativeViewHost that provides accessibility info for the |
| 310 // underlying Omnibox view. |
| 311 class OmniboxViewWrapper : public views::NativeViewHost { |
| 312 public: |
| 313 explicit OmniboxViewWrapper(OmniboxViewWin* omnibox_view_win) |
| 314 : omnibox_view_win_(omnibox_view_win) {} |
| 315 |
| 316 gfx::NativeViewAccessible GetNativeViewAccessible() { |
| 317 // This forces it to use NativeViewAccessibilityWin rather than |
| 318 // any accessibility provided natively by the HWND. |
| 319 return View::GetNativeViewAccessible(); |
| 320 } |
| 321 |
| 322 // views::View |
| 323 virtual void GetAccessibleState(ui::AccessibleViewState* state) { |
| 324 views::NativeViewHost::GetAccessibleState(state); |
| 325 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_LOCATION); |
| 326 state->role = ui::AccessibilityTypes::ROLE_TEXT; |
| 327 state->value = omnibox_view_win_->GetText(); |
| 328 state->state = ui::AccessibilityTypes::STATE_EDITABLE; |
| 329 size_t sel_start; |
| 330 size_t sel_end; |
| 331 omnibox_view_win_->GetSelectionBounds(&sel_start, &sel_end); |
| 332 state->selection_start = sel_start; |
| 333 state->selection_end = sel_end; |
| 334 } |
| 335 |
| 336 private: |
| 337 OmniboxViewWin* omnibox_view_win_; |
| 338 |
| 339 DISALLOW_COPY_AND_ASSIGN(OmniboxViewWrapper); |
| 340 }; |
| 341 |
309 /////////////////////////////////////////////////////////////////////////////// | 342 /////////////////////////////////////////////////////////////////////////////// |
310 // OmniboxViewWin | 343 // OmniboxViewWin |
311 | 344 |
312 namespace { | 345 namespace { |
313 | 346 |
314 // These are used to hook the CRichEditCtrl's calls to BeginPaint() and | 347 // These are used to hook the CRichEditCtrl's calls to BeginPaint() and |
315 // EndPaint() and provide a memory DC instead. See OnPaint(). | 348 // EndPaint() and provide a memory DC instead. See OnPaint(). |
316 HWND edit_hwnd = NULL; | 349 HWND edit_hwnd = NULL; |
317 PAINTSTRUCT paint_struct; | 350 PAINTSTRUCT paint_struct; |
318 | 351 |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 CHARRANGE selection; | 697 CHARRANGE selection; |
665 GetSel(selection); | 698 GetSel(selection); |
666 return IsSelectAllForRange(selection); | 699 return IsSelectAllForRange(selection); |
667 } | 700 } |
668 | 701 |
669 bool OmniboxViewWin::DeleteAtEndPressed() { | 702 bool OmniboxViewWin::DeleteAtEndPressed() { |
670 return delete_at_end_pressed_; | 703 return delete_at_end_pressed_; |
671 } | 704 } |
672 | 705 |
673 void OmniboxViewWin::GetSelectionBounds(string16::size_type* start, | 706 void OmniboxViewWin::GetSelectionBounds(string16::size_type* start, |
674 string16::size_type* end) { | 707 string16::size_type* end) const { |
675 CHARRANGE selection; | 708 CHARRANGE selection; |
676 GetSel(selection); | 709 GetSel(selection); |
677 *start = static_cast<size_t>(selection.cpMin); | 710 *start = static_cast<size_t>(selection.cpMin); |
678 *end = static_cast<size_t>(selection.cpMax); | 711 *end = static_cast<size_t>(selection.cpMax); |
679 } | 712 } |
680 | 713 |
681 void OmniboxViewWin::SelectAll(bool reversed) { | 714 void OmniboxViewWin::SelectAll(bool reversed) { |
682 if (reversed) | 715 if (reversed) |
683 SetSelection(GetTextLength(), 0); | 716 SetSelection(GetTextLength(), 0); |
684 else | 717 else |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
726 } | 759 } |
727 | 760 |
728 void OmniboxViewWin::ClosePopup() { | 761 void OmniboxViewWin::ClosePopup() { |
729 model_->StopAutocomplete(); | 762 model_->StopAutocomplete(); |
730 } | 763 } |
731 | 764 |
732 void OmniboxViewWin::SetFocus() { | 765 void OmniboxViewWin::SetFocus() { |
733 ::SetFocus(m_hWnd); | 766 ::SetFocus(m_hWnd); |
734 } | 767 } |
735 | 768 |
736 IAccessible* OmniboxViewWin::GetIAccessible() { | |
737 if (!autocomplete_accessibility_) { | |
738 CComObject<AutocompleteAccessibility>* accessibility = NULL; | |
739 if (!SUCCEEDED(CComObject<AutocompleteAccessibility>::CreateInstance( | |
740 &accessibility)) || !accessibility) | |
741 return NULL; | |
742 | |
743 // Wrap the created object in a smart pointer so it won't leak. | |
744 base::win::ScopedComPtr<IAccessible> accessibility_comptr(accessibility); | |
745 if (!SUCCEEDED(accessibility->Initialize(this))) | |
746 return NULL; | |
747 | |
748 // Copy to the class smart pointer, and notify that an instance of | |
749 // IAccessible was allocated for m_hWnd. | |
750 autocomplete_accessibility_ = accessibility_comptr; | |
751 NotifyWinEvent(EVENT_OBJECT_CREATE, m_hWnd, OBJID_CLIENT, CHILDID_SELF); | |
752 } | |
753 // Detach to leave ref counting to the caller. | |
754 return autocomplete_accessibility_.Detach(); | |
755 } | |
756 | |
757 void OmniboxViewWin::SetDropHighlightPosition(int position) { | 769 void OmniboxViewWin::SetDropHighlightPosition(int position) { |
758 if (drop_highlight_position_ != position) { | 770 if (drop_highlight_position_ != position) { |
759 RepaintDropHighlight(drop_highlight_position_); | 771 RepaintDropHighlight(drop_highlight_position_); |
760 drop_highlight_position_ = position; | 772 drop_highlight_position_ = position; |
761 RepaintDropHighlight(drop_highlight_position_); | 773 RepaintDropHighlight(drop_highlight_position_); |
762 } | 774 } |
763 } | 775 } |
764 | 776 |
765 void OmniboxViewWin::MoveSelectedText(int new_position) { | 777 void OmniboxViewWin::MoveSelectedText(int new_position) { |
766 const string16 selected_text(GetSelectedText()); | 778 const string16 selected_text(GetSelectedText()); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
887 | 899 |
888 if (selection_differs) | 900 if (selection_differs) |
889 controller_->OnSelectionBoundsChanged(); | 901 controller_->OnSelectionBoundsChanged(); |
890 | 902 |
891 if (something_changed && text_differs) | 903 if (something_changed && text_differs) |
892 TextChanged(); | 904 TextChanged(); |
893 | 905 |
894 if (text_differs) { | 906 if (text_differs) { |
895 // Note that a TEXT_CHANGED event implies that the cursor/selection | 907 // Note that a TEXT_CHANGED event implies that the cursor/selection |
896 // probably changed too, so we don't need to send both. | 908 // probably changed too, so we don't need to send both. |
897 parent_view_->GetWidget()->NotifyAccessibilityEvent( | 909 native_view_host_->GetWidget()->NotifyAccessibilityEvent( |
898 parent_view_, ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true); | 910 native_view_host_, ui::AccessibilityTypes::EVENT_TEXT_CHANGED, true); |
899 } else if (selection_differs) { | 911 } else if (selection_differs) { |
900 // Notify assistive technology that the cursor or selection changed. | 912 // Notify assistive technology that the cursor or selection changed. |
901 parent_view_->GetWidget()->NotifyAccessibilityEvent( | 913 native_view_host_->GetWidget()->NotifyAccessibilityEvent( |
902 parent_view_, ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, true); | 914 native_view_host_, |
| 915 ui::AccessibilityTypes::EVENT_SELECTION_CHANGED, |
| 916 true); |
903 } else if (delete_at_end_pressed_) { | 917 } else if (delete_at_end_pressed_) { |
904 model_->OnChanged(); | 918 model_->OnChanged(); |
905 } | 919 } |
906 | 920 |
907 return something_changed; | 921 return something_changed; |
908 } | 922 } |
909 | 923 |
910 gfx::NativeView OmniboxViewWin::GetNativeView() const { | 924 gfx::NativeView OmniboxViewWin::GetNativeView() const { |
911 return m_hWnd; | 925 return m_hWnd; |
912 } | 926 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
949 bool ime_composing = false; | 963 bool ime_composing = false; |
950 HIMC context = ImmGetContext(m_hWnd); | 964 HIMC context = ImmGetContext(m_hWnd); |
951 if (context) { | 965 if (context) { |
952 ime_composing = !!ImmGetCompositionString(context, GCS_COMPSTR, NULL, 0); | 966 ime_composing = !!ImmGetCompositionString(context, GCS_COMPSTR, NULL, 0); |
953 ImmReleaseContext(m_hWnd, context); | 967 ImmReleaseContext(m_hWnd, context); |
954 } | 968 } |
955 return ime_composing; | 969 return ime_composing; |
956 } | 970 } |
957 | 971 |
958 views::View* OmniboxViewWin::AddToView(views::View* parent) { | 972 views::View* OmniboxViewWin::AddToView(views::View* parent) { |
959 views::NativeViewHost* host = new views::NativeViewHost; | 973 native_view_host_ = new OmniboxViewWrapper(this); |
960 parent->AddChildView(host); | 974 parent->AddChildView(native_view_host_); |
961 host->set_focus_view(parent); | 975 native_view_host_->set_focus_view(parent); |
962 host->Attach(GetNativeView()); | 976 native_view_host_->Attach(GetNativeView()); |
963 return host; | 977 return native_view_host_; |
964 } | 978 } |
965 | 979 |
966 int OmniboxViewWin::OnPerformDrop(const views::DropTargetEvent& event) { | 980 int OmniboxViewWin::OnPerformDrop(const views::DropTargetEvent& event) { |
967 return OnPerformDropImpl(event, false); | 981 return OnPerformDropImpl(event, false); |
968 } | 982 } |
969 | 983 |
970 int OmniboxViewWin::OnPerformDropImpl(const views::DropTargetEvent& event, | 984 int OmniboxViewWin::OnPerformDropImpl(const views::DropTargetEvent& event, |
971 bool in_drag) { | 985 bool in_drag) { |
972 const ui::OSExchangeData& data = event.data(); | 986 const ui::OSExchangeData& data = event.data(); |
973 | 987 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1332 } | 1346 } |
1333 | 1347 |
1334 void OmniboxViewWin::OnCut() { | 1348 void OmniboxViewWin::OnCut() { |
1335 OnCopy(); | 1349 OnCopy(); |
1336 | 1350 |
1337 // This replace selection will have no effect (even on the undo stack) if the | 1351 // This replace selection will have no effect (even on the undo stack) if the |
1338 // current selection is empty. | 1352 // current selection is empty. |
1339 ReplaceSel(L"", true); | 1353 ReplaceSel(L"", true); |
1340 } | 1354 } |
1341 | 1355 |
1342 LRESULT OmniboxViewWin::OnGetObject(UINT uMsg, | 1356 LRESULT OmniboxViewWin::OnGetObject(UINT message, |
1343 WPARAM wparam, | 1357 WPARAM wparam, |
1344 LPARAM lparam) { | 1358 LPARAM lparam) { |
1345 // Accessibility readers will send an OBJID_CLIENT message. | 1359 // This is a request for the native accessibility object. |
1346 if (lparam == OBJID_CLIENT) { | 1360 if (lparam == OBJID_CLIENT) { |
1347 // Re-attach for internal re-usage of accessibility pointer. | 1361 return LresultFromObject(IID_IAccessible, wparam, |
1348 autocomplete_accessibility_.Attach(GetIAccessible()); | 1362 native_view_host_->GetNativeViewAccessible()); |
1349 | |
1350 if (autocomplete_accessibility_) { | |
1351 return LresultFromObject(IID_IAccessible, wparam, | |
1352 autocomplete_accessibility_); | |
1353 } | |
1354 } | 1363 } |
1355 return 0; | 1364 return 0; |
1356 } | 1365 } |
1357 | 1366 |
1358 LRESULT OmniboxViewWin::OnImeComposition(UINT message, | 1367 LRESULT OmniboxViewWin::OnImeComposition(UINT message, |
1359 WPARAM wparam, | 1368 WPARAM wparam, |
1360 LPARAM lparam) { | 1369 LPARAM lparam) { |
1361 if (ignore_ime_messages_) { | 1370 if (ignore_ime_messages_) { |
1362 // This message was sent while we're in the middle of meddling with the | 1371 // This message was sent while we're in the middle of meddling with the |
1363 // underlying edit control. If we handle it below, OnAfterPossibleChange() | 1372 // underlying edit control. If we handle it below, OnAfterPossibleChange() |
(...skipping 1279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2643 // PosFromChar(i) might return 0 when i is greater than 1. | 2652 // PosFromChar(i) might return 0 when i is greater than 1. |
2644 return font_.GetStringWidth(text) + GetHorizontalMargin(); | 2653 return font_.GetStringWidth(text) + GetHorizontalMargin(); |
2645 } | 2654 } |
2646 | 2655 |
2647 bool OmniboxViewWin::IsCaretAtEnd() const { | 2656 bool OmniboxViewWin::IsCaretAtEnd() const { |
2648 long length = GetTextLength(); | 2657 long length = GetTextLength(); |
2649 CHARRANGE sel; | 2658 CHARRANGE sel; |
2650 GetSelection(sel); | 2659 GetSelection(sel); |
2651 return sel.cpMin == sel.cpMax && sel.cpMin == length; | 2660 return sel.cpMin == sel.cpMax && sel.cpMin == length; |
2652 } | 2661 } |
OLD | NEW |