omnibox_view_views.h revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#ifndef CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
6#define CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
7
8#include <set>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/memory/scoped_ptr.h"
13#include "chrome/browser/ui/omnibox/omnibox_view.h"
14#include "chrome/browser/ui/toolbar/toolbar_model.h"
15#include "ui/base/window_open_disposition.h"
16#include "ui/gfx/range/range.h"
17#include "ui/views/controls/textfield/textfield.h"
18#include "ui/views/controls/textfield/textfield_controller.h"
19
20#if defined(OS_CHROMEOS)
21#include "chromeos/ime/input_method_manager.h"
22#endif
23
24class LocationBarView;
25class OmniboxPopupView;
26class Profile;
27
28namespace gfx {
29class RenderText;
30}
31
32namespace ui {
33class OSExchangeData;
34}  // namespace ui
35
36// Views-implementation of OmniboxView.
37class OmniboxViewViews
38    : public OmniboxView,
39      public views::Textfield,
40#if defined(OS_CHROMEOS)
41      public
42          chromeos::input_method::InputMethodManager::CandidateWindowObserver,
43#endif
44      public views::TextfieldController {
45 public:
46  // The internal view class name.
47  static const char kViewClassName[];
48
49  OmniboxViewViews(OmniboxEditController* controller,
50                   Profile* profile,
51                   CommandUpdater* command_updater,
52                   bool popup_window_mode,
53                   LocationBarView* location_bar,
54                   const gfx::FontList& font_list);
55  virtual ~OmniboxViewViews();
56
57  // Initialize, create the underlying views, etc.
58  void Init();
59
60  // Exposes the RenderText for tests.
61#if defined(UNIT_TEST)
62  gfx::RenderText* GetRenderText() {
63    return views::Textfield::GetRenderText();
64  }
65#endif
66
67  // OmniboxView:
68  virtual void SaveStateToTab(content::WebContents* tab) OVERRIDE;
69  virtual void OnTabChanged(const content::WebContents* web_contents) OVERRIDE;
70  virtual void Update() OVERRIDE;
71  virtual void UpdatePlaceholderText() OVERRIDE;
72  virtual base::string16 GetText() const OVERRIDE;
73  virtual void SetUserText(const base::string16& text,
74                           const base::string16& display_text,
75                           bool update_popup) OVERRIDE;
76  virtual void SetForcedQuery() OVERRIDE;
77  virtual void GetSelectionBounds(
78      base::string16::size_type* start,
79      base::string16::size_type* end) const OVERRIDE;
80  virtual void SelectAll(bool reversed) OVERRIDE;
81  virtual void RevertAll() OVERRIDE;
82  virtual void SetFocus() OVERRIDE;
83  virtual int GetTextWidth() const OVERRIDE;
84  virtual bool IsImeComposing() const OVERRIDE;
85
86  // views::Textfield:
87  virtual gfx::Size GetMinimumSize() const OVERRIDE;
88  virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
89  virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
90
91 private:
92  FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, CloseOmniboxPopupOnTextDrag);
93
94  // Update the field with |text| and set the selection.
95  void SetTextAndSelectedRange(const base::string16& text,
96                               const gfx::Range& range);
97
98  // Returns the selected text.
99  base::string16 GetSelectedText() const;
100
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()
104  // for details).
105  // It is assumed this is invoked after a call to OnBeforePossibleChange() and
106  // that after invoking this OnAfterPossibleChange() is invoked.
107  void OnPaste();
108
109  // Handle keyword hint tab-to-search and tabbing through dropdown results.
110  bool HandleEarlyTabActions(const ui::KeyEvent& event);
111
112  // OmniboxView:
113  virtual void SetWindowTextAndCaretPos(const base::string16& text,
114                                        size_t caret_pos,
115                                        bool update_popup,
116                                        bool notify_text_changed) OVERRIDE;
117  virtual bool IsSelectAll() const OVERRIDE;
118  virtual bool DeleteAtEndPressed() OVERRIDE;
119  virtual void UpdatePopup() OVERRIDE;
120  virtual void ApplyCaretVisibility() OVERRIDE;
121  virtual void OnTemporaryTextMaybeChanged(
122      const base::string16& display_text,
123      bool save_original_selection,
124      bool notify_text_changed) OVERRIDE;
125  virtual bool OnInlineAutocompleteTextMaybeChanged(
126      const base::string16& display_text, size_t user_text_length) OVERRIDE;
127  virtual void OnInlineAutocompleteTextCleared() OVERRIDE;
128  virtual void OnRevertTemporaryText() OVERRIDE;
129  virtual void OnBeforePossibleChange() OVERRIDE;
130  virtual bool OnAfterPossibleChange() OVERRIDE;
131  virtual gfx::NativeView GetNativeView() const OVERRIDE;
132  virtual gfx::NativeView GetRelativeWindowForPopup() const OVERRIDE;
133  virtual void SetGrayTextAutocompletion(const base::string16& input) OVERRIDE;
134  virtual base::string16 GetGrayTextAutocompletion() const OVERRIDE;
135  virtual int GetWidth() const OVERRIDE;
136  virtual bool IsImeShowingPopup() const OVERRIDE;
137  virtual void ShowImeIfNeeded() OVERRIDE;
138  virtual void OnMatchOpened(const AutocompleteMatch& match,
139                             content::WebContents* web_contents) OVERRIDE;
140  virtual int GetOmniboxTextLength() const OVERRIDE;
141  virtual void EmphasizeURLComponents() OVERRIDE;
142
143  // views::Textfield:
144  virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE;
145  virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE;
146  virtual base::string16 GetLabelForCommandId(int command_id) const OVERRIDE;
147  virtual const char* GetClassName() const OVERRIDE;
148  virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
149  virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
150  virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
151  virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
152  virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
153  virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
154  virtual bool SkipDefaultKeyEventProcessing(
155      const ui::KeyEvent& event) OVERRIDE;
156  virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
157  virtual void OnFocus() OVERRIDE;
158  virtual void OnBlur() OVERRIDE;
159  virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
160  virtual base::string16 GetSelectionClipboardText() const OVERRIDE;
161
162  // chromeos::input_method::InputMethodManager::CandidateWindowObserver:
163#if defined(OS_CHROMEOS)
164  virtual void CandidateWindowOpened(
165      chromeos::input_method::InputMethodManager* manager) OVERRIDE;
166  virtual void CandidateWindowClosed(
167      chromeos::input_method::InputMethodManager* manager) OVERRIDE;
168#endif
169
170  // views::TextfieldController:
171  virtual void ContentsChanged(views::Textfield* sender,
172                               const base::string16& new_contents) OVERRIDE;
173  virtual bool HandleKeyEvent(views::Textfield* sender,
174                              const ui::KeyEvent& key_event) OVERRIDE;
175  virtual void OnBeforeUserAction(views::Textfield* sender) OVERRIDE;
176  virtual void OnAfterUserAction(views::Textfield* sender) OVERRIDE;
177  virtual void OnAfterCutOrCopy(ui::ClipboardType clipboard_type) OVERRIDE;
178  virtual void OnWriteDragData(ui::OSExchangeData* data) OVERRIDE;
179  virtual void OnGetDragOperationsForTextfield(int* drag_operations) OVERRIDE;
180  virtual void AppendDropFormats(
181      int* formats,
182      std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
183  virtual int OnDrop(const ui::OSExchangeData& data) OVERRIDE;
184  virtual void UpdateContextMenu(ui::SimpleMenuModel* menu_contents) OVERRIDE;
185
186  // When true, the location bar view is read only and also is has a slightly
187  // different presentation (smaller font size). This is used for popups.
188  bool popup_window_mode_;
189
190  scoped_ptr<OmniboxPopupView> popup_view_;
191
192  ToolbarModel::SecurityLevel security_level_;
193
194  // Selection persisted across temporary text changes, like popup suggestions.
195  gfx::Range saved_temporary_selection_;
196
197  // Holds the user's selection across focus changes.  There is only a saved
198  // selection if this range IsValid().
199  gfx::Range saved_selection_for_focus_change_;
200
201  // Tracking state before and after a possible change.
202  base::string16 text_before_change_;
203  gfx::Range sel_before_change_;
204  bool ime_composing_before_change_;
205
206  // Was the delete key pressed with an empty selection at the end of the edit?
207  bool delete_at_end_pressed_;
208  LocationBarView* location_bar_view_;
209
210  // True if the IME candidate window is open. When this is true, we want to
211  // avoid showing the popup. So far, the candidate window is detected only
212  // on Chrome OS.
213  bool ime_candidate_window_open_;
214
215  // Should we select all the text when we see the mouse button get released?
216  // We select in response to a click that focuses the omnibox, but we defer
217  // until release, setting this variable back to false if we saw a drag, to
218  // allow the user to select just a portion of the text.
219  bool select_all_on_mouse_release_;
220
221  // Indicates if we want to select all text in the omnibox when we get a
222  // GESTURE_TAP. We want to select all only when the textfield is not in focus
223  // and gets a tap. So we use this variable to remember focus state before tap.
224  bool select_all_on_gesture_tap_;
225
226  DISALLOW_COPY_AND_ASSIGN(OmniboxViewViews);
227};
228
229#endif  // CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
230