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