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_views.h" | 5 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 #include "ui/base/ime/text_input_type.h" | 37 #include "ui/base/ime/text_input_type.h" |
38 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
39 #include "ui/base/models/simple_menu_model.h" | 39 #include "ui/base/models/simple_menu_model.h" |
40 #include "ui/base/resource/resource_bundle.h" | 40 #include "ui/base/resource/resource_bundle.h" |
41 #include "ui/events/event.h" | 41 #include "ui/events/event.h" |
42 #include "ui/gfx/canvas.h" | 42 #include "ui/gfx/canvas.h" |
43 #include "ui/gfx/font_list.h" | 43 #include "ui/gfx/font_list.h" |
44 #include "ui/gfx/selection_model.h" | 44 #include "ui/gfx/selection_model.h" |
45 #include "ui/views/border.h" | 45 #include "ui/views/border.h" |
46 #include "ui/views/button_drag_utils.h" | 46 #include "ui/views/button_drag_utils.h" |
47 #include "ui/views/controls/textfield/native_textfield_views.h" | |
48 #include "ui/views/controls/textfield/textfield.h" | 47 #include "ui/views/controls/textfield/textfield.h" |
49 #include "ui/views/ime/input_method.h" | 48 #include "ui/views/ime/input_method.h" |
50 #include "ui/views/layout/fill_layout.h" | 49 #include "ui/views/layout/fill_layout.h" |
51 #include "ui/views/views_delegate.h" | 50 #include "ui/views/views_delegate.h" |
52 #include "ui/views/widget/widget.h" | 51 #include "ui/views/widget/widget.h" |
53 #include "url/gurl.h" | 52 #include "url/gurl.h" |
54 | 53 |
55 #if defined(OS_WIN) | 54 #if defined(OS_WIN) |
56 #include "chrome/browser/browser_process.h" | 55 #include "chrome/browser/browser_process.h" |
57 #endif | 56 #endif |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 : OmniboxView(profile, controller, command_updater), | 135 : OmniboxView(profile, controller, command_updater), |
137 popup_window_mode_(popup_window_mode), | 136 popup_window_mode_(popup_window_mode), |
138 security_level_(ToolbarModel::NONE), | 137 security_level_(ToolbarModel::NONE), |
139 saved_selection_for_focus_change_(gfx::Range::InvalidRange()), | 138 saved_selection_for_focus_change_(gfx::Range::InvalidRange()), |
140 ime_composing_before_change_(false), | 139 ime_composing_before_change_(false), |
141 delete_at_end_pressed_(false), | 140 delete_at_end_pressed_(false), |
142 location_bar_view_(location_bar), | 141 location_bar_view_(location_bar), |
143 ime_candidate_window_open_(false), | 142 ime_candidate_window_open_(false), |
144 select_all_on_mouse_release_(false), | 143 select_all_on_mouse_release_(false), |
145 select_all_on_gesture_tap_(false) { | 144 select_all_on_gesture_tap_(false) { |
146 RemoveBorder(); | 145 set_border(NULL); |
147 set_id(VIEW_ID_OMNIBOX); | 146 set_id(VIEW_ID_OMNIBOX); |
148 SetFontList(font_list); | 147 SetFontList(font_list); |
149 } | 148 } |
150 | 149 |
151 OmniboxViewViews::~OmniboxViewViews() { | 150 OmniboxViewViews::~OmniboxViewViews() { |
152 #if defined(OS_CHROMEOS) | 151 #if defined(OS_CHROMEOS) |
153 chromeos::input_method::InputMethodManager::Get()-> | 152 chromeos::input_method::InputMethodManager::Get()-> |
154 RemoveCandidateWindowObserver(this); | 153 RemoveCandidateWindowObserver(this); |
155 #endif | 154 #endif |
156 | 155 |
(...skipping 12 matching lines...) Expand all Loading... |
169 ToolbarModel::NONE, LocationBarView::BACKGROUND)); | 168 ToolbarModel::NONE, LocationBarView::BACKGROUND)); |
170 | 169 |
171 if (popup_window_mode_) | 170 if (popup_window_mode_) |
172 SetReadOnly(true); | 171 SetReadOnly(true); |
173 | 172 |
174 if (chrome::ShouldDisplayOriginChip()) | 173 if (chrome::ShouldDisplayOriginChip()) |
175 set_placeholder_text(l10n_util::GetStringUTF16(IDS_OMNIBOX_EMPTY_HINT)); | 174 set_placeholder_text(l10n_util::GetStringUTF16(IDS_OMNIBOX_EMPTY_HINT)); |
176 | 175 |
177 // Initialize the popup view using the same font. | 176 // Initialize the popup view using the same font. |
178 popup_view_.reset(OmniboxPopupContentsView::Create( | 177 popup_view_.reset(OmniboxPopupContentsView::Create( |
179 font_list(), this, model(), location_bar_view_)); | 178 GetFontList(), this, model(), location_bar_view_)); |
180 | 179 |
181 #if defined(OS_CHROMEOS) | 180 #if defined(OS_CHROMEOS) |
182 chromeos::input_method::InputMethodManager::Get()-> | 181 chromeos::input_method::InputMethodManager::Get()-> |
183 AddCandidateWindowObserver(this); | 182 AddCandidateWindowObserver(this); |
184 #endif | 183 #endif |
185 } | 184 } |
186 | 185 |
187 //////////////////////////////////////////////////////////////////////////////// | 186 //////////////////////////////////////////////////////////////////////////////// |
188 // OmniboxViewViews, views::Textfield implementation: | 187 // OmniboxViewViews, views::Textfield implementation: |
189 | 188 |
190 const char* OmniboxViewViews::GetClassName() const { | 189 const char* OmniboxViewViews::GetClassName() const { |
191 return kViewClassName; | 190 return kViewClassName; |
192 } | 191 } |
193 | 192 |
194 void OmniboxViewViews::OnGestureEvent(ui::GestureEvent* event) { | 193 void OmniboxViewViews::OnGestureEvent(ui::GestureEvent* event) { |
195 views::Textfield::OnGestureEvent(event); | |
196 if (!HasFocus() && event->type() == ui::ET_GESTURE_TAP_DOWN) { | 194 if (!HasFocus() && event->type() == ui::ET_GESTURE_TAP_DOWN) { |
197 select_all_on_gesture_tap_ = true; | 195 select_all_on_gesture_tap_ = true; |
198 | 196 |
199 // If we're trying to select all on tap, invalidate any saved selection lest | 197 // If we're trying to select all on tap, invalidate any saved selection lest |
200 // restoring it fights with the "select all" action. | 198 // restoring it fights with the "select all" action. |
201 saved_selection_for_focus_change_ = gfx::Range::InvalidRange(); | 199 saved_selection_for_focus_change_ = gfx::Range::InvalidRange(); |
202 return; | |
203 } | 200 } |
| 201 |
204 if (select_all_on_gesture_tap_ && event->type() == ui::ET_GESTURE_TAP) | 202 if (select_all_on_gesture_tap_ && event->type() == ui::ET_GESTURE_TAP) |
205 SelectAll(false); | 203 SelectAll(false); |
206 | 204 |
207 if (event->type() == ui::ET_GESTURE_TAP || | 205 if (event->type() == ui::ET_GESTURE_TAP || |
208 event->type() == ui::ET_GESTURE_TAP_CANCEL || | 206 event->type() == ui::ET_GESTURE_TAP_CANCEL || |
209 event->type() == ui::ET_GESTURE_TWO_FINGER_TAP || | 207 event->type() == ui::ET_GESTURE_TWO_FINGER_TAP || |
210 event->type() == ui::ET_GESTURE_SCROLL_BEGIN || | 208 event->type() == ui::ET_GESTURE_SCROLL_BEGIN || |
211 event->type() == ui::ET_GESTURE_PINCH_BEGIN || | 209 event->type() == ui::ET_GESTURE_PINCH_BEGIN || |
212 event->type() == ui::ET_GESTURE_LONG_PRESS || | 210 event->type() == ui::ET_GESTURE_LONG_PRESS || |
213 event->type() == ui::ET_GESTURE_LONG_TAP) { | 211 event->type() == ui::ET_GESTURE_LONG_TAP) { |
214 select_all_on_gesture_tap_ = false; | 212 select_all_on_gesture_tap_ = false; |
215 } | 213 } |
| 214 |
| 215 views::Textfield::OnGestureEvent(event); |
216 } | 216 } |
217 | 217 |
218 void OmniboxViewViews::GetAccessibleState(ui::AccessibleViewState* state) { | 218 void OmniboxViewViews::GetAccessibleState(ui::AccessibleViewState* state) { |
219 location_bar_view_->GetAccessibleState(state); | 219 location_bar_view_->GetAccessibleState(state); |
220 state->role = ui::AccessibilityTypes::ROLE_TEXT; | 220 state->role = ui::AccessibilityTypes::ROLE_TEXT; |
221 } | 221 } |
222 | 222 |
223 bool OmniboxViewViews::OnMousePressed(const ui::MouseEvent& event) { | 223 bool OmniboxViewViews::OnMousePressed(const ui::MouseEvent& event) { |
224 select_all_on_mouse_release_ = | 224 select_all_on_mouse_release_ = |
225 (event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton()) && | 225 (event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton()) && |
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
667 | 667 |
668 base::string16 OmniboxViewViews::GetGrayTextAutocompletion() const { | 668 base::string16 OmniboxViewViews::GetGrayTextAutocompletion() const { |
669 #if defined(OS_WIN) || defined(USE_AURA) | 669 #if defined(OS_WIN) || defined(USE_AURA) |
670 return location_bar_view_->GetGrayTextAutocompletion(); | 670 return location_bar_view_->GetGrayTextAutocompletion(); |
671 #else | 671 #else |
672 return base::string16(); | 672 return base::string16(); |
673 #endif | 673 #endif |
674 } | 674 } |
675 | 675 |
676 int OmniboxViewViews::GetTextWidth() const { | 676 int OmniboxViewViews::GetTextWidth() const { |
677 return textfield_view_->GetWidthNeededForText(); | 677 // Returns the width necessary to display the current text, including any |
| 678 // necessary space for the cursor or border/margin. |
| 679 return GetRenderText()->GetContentWidth() + GetInsets().width(); |
678 } | 680 } |
679 | 681 |
680 int OmniboxViewViews::GetWidth() const { | 682 int OmniboxViewViews::GetWidth() const { |
681 return location_bar_view_->width(); | 683 return location_bar_view_->width(); |
682 } | 684 } |
683 | 685 |
684 bool OmniboxViewViews::IsImeComposing() const { | 686 bool OmniboxViewViews::IsImeComposing() const { |
685 return IsIMEComposing(); | 687 return IsIMEComposing(); |
686 } | 688 } |
687 | 689 |
688 bool OmniboxViewViews::IsImeShowingPopup() const { | 690 bool OmniboxViewViews::IsImeShowingPopup() const { |
689 #if defined(OS_CHROMEOS) | 691 #if defined(OS_CHROMEOS) |
690 return ime_candidate_window_open_; | 692 return ime_candidate_window_open_; |
691 #else | 693 #else |
692 const views::InputMethod* input_method = this->GetInputMethod(); | 694 const views::InputMethod* input_method = this->GetInputMethod(); |
693 return input_method && input_method->IsCandidatePopupOpen(); | 695 return input_method && input_method->IsCandidatePopupOpen(); |
694 #endif | 696 #endif |
695 } | 697 } |
696 | 698 |
| 699 bool OmniboxViewViews::IsCommandIdEnabled(int command_id) const { |
| 700 if (command_id == IDS_APP_PASTE) |
| 701 return !read_only() && !GetClipboardText().empty(); |
| 702 if (command_id == IDS_PASTE_AND_GO) |
| 703 return !read_only() && model()->CanPasteAndGo(GetClipboardText()); |
| 704 if (command_id == IDS_SHOW_URL) |
| 705 return controller()->GetToolbarModel()->WouldReplaceURL(); |
| 706 return Textfield::IsCommandIdEnabled(command_id) || |
| 707 command_updater()->IsCommandEnabled(command_id); |
| 708 } |
| 709 |
| 710 bool OmniboxViewViews::IsItemForCommandIdDynamic(int command_id) const { |
| 711 return command_id == IDS_PASTE_AND_GO; |
| 712 } |
| 713 |
| 714 base::string16 OmniboxViewViews::GetLabelForCommandId(int command_id) const { |
| 715 DCHECK_EQ(IDS_PASTE_AND_GO, command_id); |
| 716 return l10n_util::GetStringUTF16( |
| 717 model()->IsPasteAndSearch(GetClipboardText()) ? |
| 718 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO); |
| 719 } |
| 720 |
| 721 void OmniboxViewViews::ExecuteCommand(int command_id, int event_flags) { |
| 722 switch (command_id) { |
| 723 // These commands don't invoke the popup via OnBefore/AfterPossibleChange(). |
| 724 case IDS_PASTE_AND_GO: |
| 725 model()->PasteAndGo(GetClipboardText()); |
| 726 break; |
| 727 case IDS_SHOW_URL: |
| 728 ShowURL(); |
| 729 break; |
| 730 case IDC_EDIT_SEARCH_ENGINES: |
| 731 command_updater()->ExecuteCommand(command_id); |
| 732 break; |
| 733 |
| 734 default: |
| 735 OnBeforePossibleChange(); |
| 736 if (command_id == IDS_APP_PASTE) |
| 737 OnPaste(); |
| 738 else if (Textfield::IsCommandIdEnabled(command_id)) |
| 739 Textfield::ExecuteCommand(command_id, event_flags); |
| 740 else |
| 741 command_updater()->ExecuteCommand(command_id); |
| 742 OnAfterPossibleChange(); |
| 743 break; |
| 744 } |
| 745 } |
| 746 |
697 //////////////////////////////////////////////////////////////////////////////// | 747 //////////////////////////////////////////////////////////////////////////////// |
698 // OmniboxViewViews, views::TextfieldController implementation: | 748 // OmniboxViewViews, views::TextfieldController implementation: |
699 | 749 |
700 void OmniboxViewViews::ContentsChanged(views::Textfield* sender, | 750 void OmniboxViewViews::ContentsChanged(views::Textfield* sender, |
701 const base::string16& new_contents) { | 751 const base::string16& new_contents) { |
702 } | 752 } |
703 | 753 |
704 bool OmniboxViewViews::HandleKeyEvent(views::Textfield* textfield, | 754 bool OmniboxViewViews::HandleKeyEvent(views::Textfield* textfield, |
705 const ui::KeyEvent& event) { | 755 const ui::KeyEvent& event) { |
706 delete_at_end_pressed_ = false; | 756 delete_at_end_pressed_ = false; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
844 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL); | 894 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL); |
845 } | 895 } |
846 | 896 |
847 // Minor note: We use IDC_ for command id here while the underlying textfield | 897 // Minor note: We use IDC_ for command id here while the underlying textfield |
848 // is using IDS_ for all its command ids. This is because views cannot depend | 898 // is using IDS_ for all its command ids. This is because views cannot depend |
849 // on IDC_ for now. | 899 // on IDC_ for now. |
850 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, | 900 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, |
851 IDS_EDIT_SEARCH_ENGINES); | 901 IDS_EDIT_SEARCH_ENGINES); |
852 } | 902 } |
853 | 903 |
854 bool OmniboxViewViews::IsCommandIdEnabled(int command_id) const { | |
855 if (command_id == IDS_APP_PASTE) | |
856 return !read_only() && !GetClipboardText().empty(); | |
857 if (command_id == IDS_PASTE_AND_GO) | |
858 return !read_only() && model()->CanPasteAndGo(GetClipboardText()); | |
859 if (command_id == IDS_SHOW_URL) | |
860 return controller()->GetToolbarModel()->WouldReplaceURL(); | |
861 return command_updater()->IsCommandEnabled(command_id); | |
862 } | |
863 | |
864 bool OmniboxViewViews::IsItemForCommandIdDynamic(int command_id) const { | |
865 return command_id == IDS_PASTE_AND_GO; | |
866 } | |
867 | |
868 base::string16 OmniboxViewViews::GetLabelForCommandId(int command_id) const { | |
869 DCHECK_EQ(IDS_PASTE_AND_GO, command_id); | |
870 return l10n_util::GetStringUTF16( | |
871 model()->IsPasteAndSearch(GetClipboardText()) ? | |
872 IDS_PASTE_AND_SEARCH : IDS_PASTE_AND_GO); | |
873 } | |
874 | |
875 bool OmniboxViewViews::HandlesCommand(int command_id) const { | |
876 // See description in OnPaste() for details on why we need to handle paste. | |
877 return command_id == IDS_APP_PASTE; | |
878 } | |
879 | |
880 void OmniboxViewViews::ExecuteCommand(int command_id, int event_flags) { | |
881 switch (command_id) { | |
882 // These commands don't invoke the popup via OnBefore/AfterPossibleChange(). | |
883 case IDS_PASTE_AND_GO: | |
884 model()->PasteAndGo(GetClipboardText()); | |
885 break; | |
886 case IDS_SHOW_URL: | |
887 ShowURL(); | |
888 break; | |
889 case IDC_EDIT_SEARCH_ENGINES: | |
890 command_updater()->ExecuteCommand(command_id); | |
891 break; | |
892 | |
893 default: | |
894 OnBeforePossibleChange(); | |
895 if (command_id == IDS_APP_PASTE) | |
896 OnPaste(); | |
897 else | |
898 command_updater()->ExecuteCommand(command_id); | |
899 OnAfterPossibleChange(); | |
900 break; | |
901 } | |
902 } | |
903 | |
904 #if defined(OS_CHROMEOS) | 904 #if defined(OS_CHROMEOS) |
905 void OmniboxViewViews::CandidateWindowOpened( | 905 void OmniboxViewViews::CandidateWindowOpened( |
906 chromeos::input_method::InputMethodManager* manager) { | 906 chromeos::input_method::InputMethodManager* manager) { |
907 ime_candidate_window_open_ = true; | 907 ime_candidate_window_open_ = true; |
908 } | 908 } |
909 | 909 |
910 void OmniboxViewViews::CandidateWindowClosed( | 910 void OmniboxViewViews::CandidateWindowClosed( |
911 chromeos::input_method::InputMethodManager* manager) { | 911 chromeos::input_method::InputMethodManager* manager) { |
912 ime_candidate_window_open_ = false; | 912 ime_candidate_window_open_ = false; |
913 } | 913 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
975 const base::string16 text(GetClipboardText()); | 975 const base::string16 text(GetClipboardText()); |
976 if (!text.empty()) { | 976 if (!text.empty()) { |
977 // Record this paste, so we can do different behavior. | 977 // Record this paste, so we can do different behavior. |
978 model()->OnPaste(); | 978 model()->OnPaste(); |
979 // Force a Paste operation to trigger the text_changed code in | 979 // Force a Paste operation to trigger the text_changed code in |
980 // OnAfterPossibleChange(), even if identical contents are pasted. | 980 // OnAfterPossibleChange(), even if identical contents are pasted. |
981 text_before_change_.clear(); | 981 text_before_change_.clear(); |
982 InsertOrReplaceText(text); | 982 InsertOrReplaceText(text); |
983 } | 983 } |
984 } | 984 } |
OLD | NEW |