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