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