| 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 |
| 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/property_bag.h" | |
| 20 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 21 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 22 #include "base/win/iat_patch_function.h" | 21 #include "base/win/iat_patch_function.h" |
| 23 #include "base/win/scoped_hdc.h" | 22 #include "base/win/scoped_hdc.h" |
| 24 #include "base/win/scoped_select_object.h" | 23 #include "base/win/scoped_select_object.h" |
| 25 #include "base/win/windows_version.h" | 24 #include "base/win/windows_version.h" |
| 26 #include "chrome/app/chrome_command_ids.h" | 25 #include "chrome/app/chrome_command_ids.h" |
| 27 #include "chrome/browser/autocomplete/autocomplete_input.h" | 26 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 28 #include "chrome/browser/autocomplete/autocomplete_match.h" | 27 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 29 #include "chrome/browser/autocomplete/keyword_provider.h" | 28 #include "chrome/browser/autocomplete/keyword_provider.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // A helper method for determining a valid drag operation given the allowed | 86 // A helper method for determining a valid drag operation given the allowed |
| 88 // operation. We prefer copy over link. | 87 // operation. We prefer copy over link. |
| 89 int CopyOrLinkDragOperation(int drag_operation) { | 88 int CopyOrLinkDragOperation(int drag_operation) { |
| 90 if (drag_operation & ui::DragDropTypes::DRAG_COPY) | 89 if (drag_operation & ui::DragDropTypes::DRAG_COPY) |
| 91 return ui::DragDropTypes::DRAG_COPY; | 90 return ui::DragDropTypes::DRAG_COPY; |
| 92 if (drag_operation & ui::DragDropTypes::DRAG_LINK) | 91 if (drag_operation & ui::DragDropTypes::DRAG_LINK) |
| 93 return ui::DragDropTypes::DRAG_LINK; | 92 return ui::DragDropTypes::DRAG_LINK; |
| 94 return ui::DragDropTypes::DRAG_NONE; | 93 return ui::DragDropTypes::DRAG_NONE; |
| 95 } | 94 } |
| 96 | 95 |
| 96 const char kAutocompleteEditStateKey[] = "AutocompleteEditState"; |
| 97 |
| 97 // The AutocompleteEditState struct contains enough information about the | 98 // The AutocompleteEditState struct contains enough information about the |
| 98 // OmniboxEditModel and OmniboxViewWin to save/restore a user's | 99 // OmniboxEditModel and OmniboxViewWin to save/restore a user's |
| 99 // typing, caret position, etc. across tab changes. We explicitly don't | 100 // typing, caret position, etc. across tab changes. We explicitly don't |
| 100 // preserve things like whether the popup was open as this might be weird. | 101 // preserve things like whether the popup was open as this might be weird. |
| 101 struct AutocompleteEditState { | 102 struct AutocompleteEditState : public base::SupportsUserData::Data { |
| 102 AutocompleteEditState(const OmniboxEditModel::State& model_state, | 103 AutocompleteEditState(const OmniboxEditModel::State& model_state, |
| 103 const OmniboxViewWin::State& view_state) | 104 const OmniboxViewWin::State& view_state) |
| 104 : model_state(model_state), | 105 : model_state(model_state), |
| 105 view_state(view_state) { | 106 view_state(view_state) { |
| 106 } | 107 } |
| 108 virtual ~AutocompleteEditState() {} |
| 107 | 109 |
| 108 const OmniboxEditModel::State model_state; | 110 const OmniboxEditModel::State model_state; |
| 109 const OmniboxViewWin::State view_state; | 111 const OmniboxViewWin::State view_state; |
| 110 }; | 112 }; |
| 111 | 113 |
| 112 // Returns true if the current point is far enough from the origin that it | 114 // Returns true if the current point is far enough from the origin that it |
| 113 // would be considered a drag. | 115 // would be considered a drag. |
| 114 bool IsDrag(const POINT& origin, const POINT& current) { | 116 bool IsDrag(const POINT& origin, const POINT& current) { |
| 115 return views::View::ExceededDragThreshold(current.x - origin.x, | 117 return views::View::ExceededDragThreshold(current.x - origin.x, |
| 116 current.y - origin.y); | 118 current.y - origin.y); |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 | 371 |
| 370 *lpPaint = paint_struct; | 372 *lpPaint = paint_struct; |
| 371 return paint_struct.hdc; | 373 return paint_struct.hdc; |
| 372 } | 374 } |
| 373 | 375 |
| 374 // Intercepted method for EndPaint(). Must use __stdcall convention. | 376 // Intercepted method for EndPaint(). Must use __stdcall convention. |
| 375 BOOL WINAPI EndPaintIntercept(HWND hWnd, const PAINTSTRUCT* lpPaint) { | 377 BOOL WINAPI EndPaintIntercept(HWND hWnd, const PAINTSTRUCT* lpPaint) { |
| 376 return (edit_hwnd && (hWnd == edit_hwnd)) || ::EndPaint(hWnd, lpPaint); | 378 return (edit_hwnd && (hWnd == edit_hwnd)) || ::EndPaint(hWnd, lpPaint); |
| 377 } | 379 } |
| 378 | 380 |
| 379 // Returns a lazily initialized property bag accessor for saving our state in a | |
| 380 // WebContents. | |
| 381 base::PropertyAccessor<AutocompleteEditState>* GetStateAccessor() { | |
| 382 static base::PropertyAccessor<AutocompleteEditState> state; | |
| 383 return &state; | |
| 384 } | |
| 385 | |
| 386 class PaintPatcher { | 381 class PaintPatcher { |
| 387 public: | 382 public: |
| 388 PaintPatcher(); | 383 PaintPatcher(); |
| 389 ~PaintPatcher(); | 384 ~PaintPatcher(); |
| 390 | 385 |
| 391 void RefPatch(); | 386 void RefPatch(); |
| 392 void DerefPatch(); | 387 void DerefPatch(); |
| 393 | 388 |
| 394 private: | 389 private: |
| 395 size_t refcount_; | 390 size_t refcount_; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 return parent_view_; | 542 return parent_view_; |
| 548 } | 543 } |
| 549 | 544 |
| 550 void OmniboxViewWin::SaveStateToTab(WebContents* tab) { | 545 void OmniboxViewWin::SaveStateToTab(WebContents* tab) { |
| 551 DCHECK(tab); | 546 DCHECK(tab); |
| 552 | 547 |
| 553 const OmniboxEditModel::State model_state(model()->GetStateForTabSwitch()); | 548 const OmniboxEditModel::State model_state(model()->GetStateForTabSwitch()); |
| 554 | 549 |
| 555 CHARRANGE selection; | 550 CHARRANGE selection; |
| 556 GetSelection(selection); | 551 GetSelection(selection); |
| 557 GetStateAccessor()->SetProperty(tab->GetPropertyBag(), | 552 tab->SetUserData( |
| 558 AutocompleteEditState( | 553 kAutocompleteEditStateKey, |
| 554 new AutocompleteEditState( |
| 559 model_state, | 555 model_state, |
| 560 State(selection, saved_selection_for_focus_change_))); | 556 State(selection, saved_selection_for_focus_change_))); |
| 561 } | 557 } |
| 562 | 558 |
| 563 void OmniboxViewWin::Update(const WebContents* tab_for_state_restoring) { | 559 void OmniboxViewWin::Update(const WebContents* tab_for_state_restoring) { |
| 564 const bool visibly_changed_permanent_text = | 560 const bool visibly_changed_permanent_text = |
| 565 model()->UpdatePermanentText(toolbar_model()->GetText()); | 561 model()->UpdatePermanentText(toolbar_model()->GetText()); |
| 566 | 562 |
| 567 const ToolbarModel::SecurityLevel security_level = | 563 const ToolbarModel::SecurityLevel security_level = |
| 568 toolbar_model()->GetSecurityLevel(); | 564 toolbar_model()->GetSecurityLevel(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 579 security_level_ = security_level; | 575 security_level_ = security_level; |
| 580 | 576 |
| 581 // When we're switching to a new tab, restore its state, if any. | 577 // When we're switching to a new tab, restore its state, if any. |
| 582 ScopedFreeze freeze(this, GetTextObjectModel()); | 578 ScopedFreeze freeze(this, GetTextObjectModel()); |
| 583 if (tab_for_state_restoring) { | 579 if (tab_for_state_restoring) { |
| 584 // Make sure we reset our own state first. The new tab may not have any | 580 // Make sure we reset our own state first. The new tab may not have any |
| 585 // saved state, or it may not have had input in progress, in which case we | 581 // saved state, or it may not have had input in progress, in which case we |
| 586 // won't overwrite all our local state. | 582 // won't overwrite all our local state. |
| 587 RevertAll(); | 583 RevertAll(); |
| 588 | 584 |
| 589 const AutocompleteEditState* state = GetStateAccessor()->GetProperty( | 585 const AutocompleteEditState* state = static_cast<AutocompleteEditState*>( |
| 590 tab_for_state_restoring->GetPropertyBag()); | 586 tab_for_state_restoring->GetUserData(&kAutocompleteEditStateKey)); |
| 591 if (state) { | 587 if (state) { |
| 592 model()->RestoreState(state->model_state); | 588 model()->RestoreState(state->model_state); |
| 593 | 589 |
| 594 // Restore user's selection. We do this after restoring the user_text | 590 // Restore user's selection. We do this after restoring the user_text |
| 595 // above so we're selecting in the correct string. | 591 // above so we're selecting in the correct string. |
| 596 SetSelectionRange(state->view_state.selection); | 592 SetSelectionRange(state->view_state.selection); |
| 597 saved_selection_for_focus_change_ = | 593 saved_selection_for_focus_change_ = |
| 598 state->view_state.saved_selection_for_focus_change; | 594 state->view_state.saved_selection_for_focus_change; |
| 599 } | 595 } |
| 600 } else if (visibly_changed_permanent_text) { | 596 } else if (visibly_changed_permanent_text) { |
| (...skipping 2034 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2635 return (rect.left - client_rect.left) + (client_rect.right - rect.right); | 2631 return (rect.left - client_rect.left) + (client_rect.right - rect.right); |
| 2636 } | 2632 } |
| 2637 | 2633 |
| 2638 int OmniboxViewWin::WidthNeededToDisplay(const string16& text) const { | 2634 int OmniboxViewWin::WidthNeededToDisplay(const string16& text) const { |
| 2639 // Use font_.GetStringWidth() instead of | 2635 // Use font_.GetStringWidth() instead of |
| 2640 // PosFromChar(location_entry_->GetTextLength()) because PosFromChar() is | 2636 // PosFromChar(location_entry_->GetTextLength()) because PosFromChar() is |
| 2641 // apparently buggy. In both LTR UI and RTL UI with left-to-right layout, | 2637 // apparently buggy. In both LTR UI and RTL UI with left-to-right layout, |
| 2642 // PosFromChar(i) might return 0 when i is greater than 1. | 2638 // PosFromChar(i) might return 0 when i is greater than 1. |
| 2643 return font_.GetStringWidth(text) + GetHorizontalMargin(); | 2639 return font_.GetStringWidth(text) + GetHorizontalMargin(); |
| 2644 } | 2640 } |
| OLD | NEW |