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" |
47 #include "ui/views/controls/textfield/textfield.h" | 48 #include "ui/views/controls/textfield/textfield.h" |
48 #include "ui/views/ime/input_method.h" | 49 #include "ui/views/ime/input_method.h" |
49 #include "ui/views/layout/fill_layout.h" | 50 #include "ui/views/layout/fill_layout.h" |
50 #include "ui/views/views_delegate.h" | 51 #include "ui/views/views_delegate.h" |
51 #include "ui/views/widget/widget.h" | 52 #include "ui/views/widget/widget.h" |
52 #include "url/gurl.h" | 53 #include "url/gurl.h" |
53 | 54 |
54 #if defined(OS_WIN) | 55 #if defined(OS_WIN) |
55 #include "chrome/browser/browser_process.h" | 56 #include "chrome/browser/browser_process.h" |
56 #endif | 57 #endif |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 : OmniboxView(profile, controller, command_updater), | 136 : OmniboxView(profile, controller, command_updater), |
136 popup_window_mode_(popup_window_mode), | 137 popup_window_mode_(popup_window_mode), |
137 security_level_(ToolbarModel::NONE), | 138 security_level_(ToolbarModel::NONE), |
138 saved_selection_for_focus_change_(gfx::Range::InvalidRange()), | 139 saved_selection_for_focus_change_(gfx::Range::InvalidRange()), |
139 ime_composing_before_change_(false), | 140 ime_composing_before_change_(false), |
140 delete_at_end_pressed_(false), | 141 delete_at_end_pressed_(false), |
141 location_bar_view_(location_bar), | 142 location_bar_view_(location_bar), |
142 ime_candidate_window_open_(false), | 143 ime_candidate_window_open_(false), |
143 select_all_on_mouse_release_(false), | 144 select_all_on_mouse_release_(false), |
144 select_all_on_gesture_tap_(false) { | 145 select_all_on_gesture_tap_(false) { |
145 set_border(NULL); | 146 RemoveBorder(); |
146 set_id(VIEW_ID_OMNIBOX); | 147 set_id(VIEW_ID_OMNIBOX); |
147 SetFontList(font_list); | 148 SetFontList(font_list); |
148 } | 149 } |
149 | 150 |
150 OmniboxViewViews::~OmniboxViewViews() { | 151 OmniboxViewViews::~OmniboxViewViews() { |
151 #if defined(OS_CHROMEOS) | 152 #if defined(OS_CHROMEOS) |
152 chromeos::input_method::InputMethodManager::Get()-> | 153 chromeos::input_method::InputMethodManager::Get()-> |
153 RemoveCandidateWindowObserver(this); | 154 RemoveCandidateWindowObserver(this); |
154 #endif | 155 #endif |
155 | 156 |
(...skipping 12 matching lines...) Expand all Loading... |
168 ToolbarModel::NONE, LocationBarView::BACKGROUND)); | 169 ToolbarModel::NONE, LocationBarView::BACKGROUND)); |
169 | 170 |
170 if (popup_window_mode_) | 171 if (popup_window_mode_) |
171 SetReadOnly(true); | 172 SetReadOnly(true); |
172 | 173 |
173 if (chrome::ShouldDisplayOriginChip()) | 174 if (chrome::ShouldDisplayOriginChip()) |
174 set_placeholder_text(l10n_util::GetStringUTF16(IDS_OMNIBOX_EMPTY_HINT)); | 175 set_placeholder_text(l10n_util::GetStringUTF16(IDS_OMNIBOX_EMPTY_HINT)); |
175 | 176 |
176 // Initialize the popup view using the same font. | 177 // Initialize the popup view using the same font. |
177 popup_view_.reset(OmniboxPopupContentsView::Create( | 178 popup_view_.reset(OmniboxPopupContentsView::Create( |
178 GetFontList(), this, model(), location_bar_view_)); | 179 font_list(), this, model(), location_bar_view_)); |
179 | 180 |
180 #if defined(OS_CHROMEOS) | 181 #if defined(OS_CHROMEOS) |
181 chromeos::input_method::InputMethodManager::Get()-> | 182 chromeos::input_method::InputMethodManager::Get()-> |
182 AddCandidateWindowObserver(this); | 183 AddCandidateWindowObserver(this); |
183 #endif | 184 #endif |
184 } | 185 } |
185 | 186 |
186 //////////////////////////////////////////////////////////////////////////////// | 187 //////////////////////////////////////////////////////////////////////////////// |
187 // OmniboxViewViews, views::Textfield implementation: | 188 // OmniboxViewViews, views::Textfield implementation: |
188 | 189 |
189 const char* OmniboxViewViews::GetClassName() const { | 190 const char* OmniboxViewViews::GetClassName() const { |
190 return kViewClassName; | 191 return kViewClassName; |
191 } | 192 } |
192 | 193 |
193 void OmniboxViewViews::OnGestureEvent(ui::GestureEvent* event) { | 194 void OmniboxViewViews::OnGestureEvent(ui::GestureEvent* event) { |
| 195 views::Textfield::OnGestureEvent(event); |
194 if (!HasFocus() && event->type() == ui::ET_GESTURE_TAP_DOWN) { | 196 if (!HasFocus() && event->type() == ui::ET_GESTURE_TAP_DOWN) { |
195 select_all_on_gesture_tap_ = true; | 197 select_all_on_gesture_tap_ = true; |
196 | 198 |
197 // If we're trying to select all on tap, invalidate any saved selection lest | 199 // If we're trying to select all on tap, invalidate any saved selection lest |
198 // restoring it fights with the "select all" action. | 200 // restoring it fights with the "select all" action. |
199 saved_selection_for_focus_change_ = gfx::Range::InvalidRange(); | 201 saved_selection_for_focus_change_ = gfx::Range::InvalidRange(); |
| 202 return; |
200 } | 203 } |
201 | |
202 if (select_all_on_gesture_tap_ && event->type() == ui::ET_GESTURE_TAP) | 204 if (select_all_on_gesture_tap_ && event->type() == ui::ET_GESTURE_TAP) |
203 SelectAll(false); | 205 SelectAll(false); |
204 | 206 |
205 if (event->type() == ui::ET_GESTURE_TAP || | 207 if (event->type() == ui::ET_GESTURE_TAP || |
206 event->type() == ui::ET_GESTURE_TAP_CANCEL || | 208 event->type() == ui::ET_GESTURE_TAP_CANCEL || |
207 event->type() == ui::ET_GESTURE_TWO_FINGER_TAP || | 209 event->type() == ui::ET_GESTURE_TWO_FINGER_TAP || |
208 event->type() == ui::ET_GESTURE_SCROLL_BEGIN || | 210 event->type() == ui::ET_GESTURE_SCROLL_BEGIN || |
209 event->type() == ui::ET_GESTURE_PINCH_BEGIN || | 211 event->type() == ui::ET_GESTURE_PINCH_BEGIN || |
210 event->type() == ui::ET_GESTURE_LONG_PRESS || | 212 event->type() == ui::ET_GESTURE_LONG_PRESS || |
211 event->type() == ui::ET_GESTURE_LONG_TAP) { | 213 event->type() == ui::ET_GESTURE_LONG_TAP) { |
212 select_all_on_gesture_tap_ = false; | 214 select_all_on_gesture_tap_ = false; |
213 } | 215 } |
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 // Returns the width necessary to display the current text, including any | 677 return textfield_view_->GetWidthNeededForText(); |
678 // necessary space for the cursor or border/margin. | |
679 return GetRenderText()->GetContentWidth() + GetInsets().width(); | |
680 } | 678 } |
681 | 679 |
682 int OmniboxViewViews::GetWidth() const { | 680 int OmniboxViewViews::GetWidth() const { |
683 return location_bar_view_->width(); | 681 return location_bar_view_->width(); |
684 } | 682 } |
685 | 683 |
686 bool OmniboxViewViews::IsImeComposing() const { | 684 bool OmniboxViewViews::IsImeComposing() const { |
687 return IsIMEComposing(); | 685 return IsIMEComposing(); |
688 } | 686 } |
689 | 687 |
690 bool OmniboxViewViews::IsImeShowingPopup() const { | 688 bool OmniboxViewViews::IsImeShowingPopup() const { |
691 #if defined(OS_CHROMEOS) | 689 #if defined(OS_CHROMEOS) |
692 return ime_candidate_window_open_; | 690 return ime_candidate_window_open_; |
693 #else | 691 #else |
694 const views::InputMethod* input_method = this->GetInputMethod(); | 692 const views::InputMethod* input_method = this->GetInputMethod(); |
695 return input_method && input_method->IsCandidatePopupOpen(); | 693 return input_method && input_method->IsCandidatePopupOpen(); |
696 #endif | 694 #endif |
697 } | 695 } |
698 | 696 |
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 | |
747 //////////////////////////////////////////////////////////////////////////////// | 697 //////////////////////////////////////////////////////////////////////////////// |
748 // OmniboxViewViews, views::TextfieldController implementation: | 698 // OmniboxViewViews, views::TextfieldController implementation: |
749 | 699 |
750 void OmniboxViewViews::ContentsChanged(views::Textfield* sender, | 700 void OmniboxViewViews::ContentsChanged(views::Textfield* sender, |
751 const base::string16& new_contents) { | 701 const base::string16& new_contents) { |
752 } | 702 } |
753 | 703 |
754 bool OmniboxViewViews::HandleKeyEvent(views::Textfield* textfield, | 704 bool OmniboxViewViews::HandleKeyEvent(views::Textfield* textfield, |
755 const ui::KeyEvent& event) { | 705 const ui::KeyEvent& event) { |
756 delete_at_end_pressed_ = false; | 706 delete_at_end_pressed_ = false; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
894 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL); | 844 select_all_position + 1, IDS_SHOW_URL, IDS_SHOW_URL); |
895 } | 845 } |
896 | 846 |
897 // Minor note: We use IDC_ for command id here while the underlying textfield | 847 // Minor note: We use IDC_ for command id here while the underlying textfield |
898 // is using IDS_ for all its command ids. This is because views cannot depend | 848 // is using IDS_ for all its command ids. This is because views cannot depend |
899 // on IDC_ for now. | 849 // on IDC_ for now. |
900 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, | 850 menu_contents->AddItemWithStringId(IDC_EDIT_SEARCH_ENGINES, |
901 IDS_EDIT_SEARCH_ENGINES); | 851 IDS_EDIT_SEARCH_ENGINES); |
902 } | 852 } |
903 | 853 |
| 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 |