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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
6 #define CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chrome/browser/ui/omnibox/omnibox_view.h"
15 #include "chrome/browser/ui/toolbar/toolbar_model.h"
16 #include "ui/base/window_open_disposition.h"
17 #include "ui/gfx/range/range.h"
18 #include "ui/views/controls/textfield/textfield.h"
19 #include "ui/views/controls/textfield/textfield_controller.h"
21 #if defined(OS_CHROMEOS)
22 #include "chromeos/ime/input_method_manager.h"
25 class LocationBarView;
26 class OmniboxPopupView;
37 // Views-implementation of OmniboxView.
38 class OmniboxViewViews
40 public views::Textfield,
41 #if defined(OS_CHROMEOS)
43 chromeos::input_method::InputMethodManager::CandidateWindowObserver,
45 public views::TextfieldController {
47 // The internal view class name.
48 static const char kViewClassName[];
50 OmniboxViewViews(OmniboxEditController* controller,
52 CommandUpdater* command_updater,
53 bool popup_window_mode,
54 LocationBarView* location_bar,
55 const gfx::FontList& font_list);
56 ~OmniboxViewViews() override;
58 // Initialize, create the underlying views, etc.
61 // Exposes the RenderText for tests.
62 #if defined(UNIT_TEST)
63 gfx::RenderText* GetRenderText() {
64 return views::Textfield::GetRenderText();
69 void SaveStateToTab(content::WebContents* tab) override;
70 void OnTabChanged(const content::WebContents* web_contents) override;
71 void Update() override;
72 void UpdatePlaceholderText() override;
73 base::string16 GetText() const override;
74 void SetUserText(const base::string16& text,
75 const base::string16& display_text,
76 bool update_popup) override;
77 void SetForcedQuery() override;
78 void GetSelectionBounds(base::string16::size_type* start,
79 base::string16::size_type* end) const override;
80 void SelectAll(bool reversed) override;
81 void RevertAll() override;
82 void SetFocus() override;
83 int GetTextWidth() const override;
84 bool IsImeComposing() const override;
87 gfx::Size GetMinimumSize() const override;
88 void OnNativeThemeChanged(const ui::NativeTheme* theme) override;
89 void ExecuteCommand(int command_id, int event_flags) override;
92 FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, CloseOmniboxPopupOnTextDrag);
94 // Update the field with |text| and set the selection.
95 void SetTextAndSelectedRange(const base::string16& text,
96 const gfx::Range& range);
98 // Returns the selected text.
99 base::string16 GetSelectedText() const;
101 // Paste text from the clipboard into the omnibox.
102 // Textfields implementation of Paste() pastes the contents of the clipboard
103 // as is. We want to strip whitespace and other things (see GetClipboardText()
105 // It is assumed this is invoked after a call to OnBeforePossibleChange() and
106 // that after invoking this OnAfterPossibleChange() is invoked.
109 // Handle keyword hint tab-to-search and tabbing through dropdown results.
110 bool HandleEarlyTabActions(const ui::KeyEvent& event);
112 // Handles a request to change the value of this text field from software
113 // using an accessibility API (typically automation software, screen readers
114 // don't normally use this). Sets the value and clears the selection.
115 void AccessibilitySetValue(const base::string16& new_value);
118 void SetWindowTextAndCaretPos(const base::string16& text,
121 bool notify_text_changed) override;
122 bool IsSelectAll() const override;
123 bool DeleteAtEndPressed() override;
124 void UpdatePopup() override;
125 void ApplyCaretVisibility() override;
126 void OnTemporaryTextMaybeChanged(const base::string16& display_text,
127 bool save_original_selection,
128 bool notify_text_changed) override;
129 bool OnInlineAutocompleteTextMaybeChanged(const base::string16& display_text,
130 size_t user_text_length) override;
131 void OnInlineAutocompleteTextCleared() override;
132 void OnRevertTemporaryText() override;
133 void OnBeforePossibleChange() override;
134 bool OnAfterPossibleChange() override;
135 gfx::NativeView GetNativeView() const override;
136 gfx::NativeView GetRelativeWindowForPopup() const override;
137 void SetGrayTextAutocompletion(const base::string16& input) override;
138 base::string16 GetGrayTextAutocompletion() const override;
139 int GetWidth() const override;
140 bool IsImeShowingPopup() const override;
141 void ShowImeIfNeeded() override;
142 void OnMatchOpened(const AutocompleteMatch& match,
143 content::WebContents* web_contents) override;
144 int GetOmniboxTextLength() const override;
145 void EmphasizeURLComponents() override;
148 bool OnKeyReleased(const ui::KeyEvent& event) override;
149 bool IsItemForCommandIdDynamic(int command_id) const override;
150 base::string16 GetLabelForCommandId(int command_id) const override;
151 const char* GetClassName() const override;
152 bool OnMousePressed(const ui::MouseEvent& event) override;
153 bool OnMouseDragged(const ui::MouseEvent& event) override;
154 void OnMouseReleased(const ui::MouseEvent& event) override;
155 bool OnKeyPressed(const ui::KeyEvent& event) override;
156 void OnGestureEvent(ui::GestureEvent* event) override;
157 void AboutToRequestFocusFromTabTraversal(bool reverse) override;
158 bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) override;
159 void GetAccessibleState(ui::AXViewState* state) override;
160 void OnFocus() override;
161 void OnBlur() override;
162 bool IsCommandIdEnabled(int command_id) const override;
163 base::string16 GetSelectionClipboardText() const override;
165 // chromeos::input_method::InputMethodManager::CandidateWindowObserver:
166 #if defined(OS_CHROMEOS)
167 virtual void CandidateWindowOpened(
168 chromeos::input_method::InputMethodManager* manager) override;
169 virtual void CandidateWindowClosed(
170 chromeos::input_method::InputMethodManager* manager) override;
173 // views::TextfieldController:
174 void ContentsChanged(views::Textfield* sender,
175 const base::string16& new_contents) override;
176 bool HandleKeyEvent(views::Textfield* sender,
177 const ui::KeyEvent& key_event) override;
178 void OnBeforeUserAction(views::Textfield* sender) override;
179 void OnAfterUserAction(views::Textfield* sender) override;
180 void OnAfterCutOrCopy(ui::ClipboardType clipboard_type) override;
181 void OnWriteDragData(ui::OSExchangeData* data) override;
182 void OnGetDragOperationsForTextfield(int* drag_operations) override;
183 void AppendDropFormats(
185 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) override;
186 int OnDrop(const ui::OSExchangeData& data) override;
187 void UpdateContextMenu(ui::SimpleMenuModel* menu_contents) override;
189 // When true, the location bar view is read only and also is has a slightly
190 // different presentation (smaller font size). This is used for popups.
191 bool popup_window_mode_;
193 scoped_ptr<OmniboxPopupView> popup_view_;
195 ToolbarModel::SecurityLevel security_level_;
197 // Selection persisted across temporary text changes, like popup suggestions.
198 gfx::Range saved_temporary_selection_;
200 // Holds the user's selection across focus changes. There is only a saved
201 // selection if this range IsValid().
202 gfx::Range saved_selection_for_focus_change_;
204 // Tracking state before and after a possible change.
205 base::string16 text_before_change_;
206 gfx::Range sel_before_change_;
207 bool ime_composing_before_change_;
209 // Was the delete key pressed with an empty selection at the end of the edit?
210 bool delete_at_end_pressed_;
211 LocationBarView* location_bar_view_;
213 // True if the IME candidate window is open. When this is true, we want to
214 // avoid showing the popup. So far, the candidate window is detected only
216 bool ime_candidate_window_open_;
218 // Should we select all the text when we see the mouse button get released?
219 // We select in response to a click that focuses the omnibox, but we defer
220 // until release, setting this variable back to false if we saw a drag, to
221 // allow the user to select just a portion of the text.
222 bool select_all_on_mouse_release_;
224 // Indicates if we want to select all text in the omnibox when we get a
225 // GESTURE_TAP. We want to select all only when the textfield is not in focus
226 // and gets a tap. So we use this variable to remember focus state before tap.
227 bool select_all_on_gesture_tap_;
229 // Used to bind callback functions to this object.
230 base::WeakPtrFactory<OmniboxViewViews> weak_ptr_factory_;
232 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViews);
235 #endif // CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_