omnibox_view_views.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 SlideAnimation;
30}
31
32namespace ui {
33class OSExchangeData;
34}  // namespace ui
35
36// Views-implementation of OmniboxView, based on the gtk implementation.
37class OmniboxViewViews
38    : public OmniboxView,
39      public views::Textfield,
40      public gfx::AnimationDelegate,
41#if defined(OS_CHROMEOS)
42      public
43          chromeos::input_method::InputMethodManager::CandidateWindowObserver,
44#endif
45      public views::TextfieldController {
46 public:
47  // The internal view class name.
48  static const char kViewClassName[];
49
50  OmniboxViewViews(OmniboxEditController* controller,
51                   Profile* profile,
52                   CommandUpdater* command_updater,
53                   bool popup_window_mode,
54                   LocationBarView* location_bar,
55                   const gfx::FontList& font_list);
56  virtual ~OmniboxViewViews();
57
58  // Initialize, create the underlying views, etc;
59  void Init();
60
61  // Starts an animation that fades in the entire OmniboxView.
62  void FadeIn();
63
64  // OmniboxView:
65  virtual void SaveStateToTab(content::WebContents* tab) OVERRIDE;
66  virtual void OnTabChanged(const content::WebContents* web_contents) OVERRIDE;
67  virtual void Update() OVERRIDE;
68  virtual base::string16 GetText() const OVERRIDE;
69  virtual void SetUserText(const base::string16& text,
70                           const base::string16& display_text,
71                           bool update_popup) OVERRIDE;
72  virtual void SetForcedQuery() OVERRIDE;
73  virtual void GetSelectionBounds(
74      base::string16::size_type* start,
75      base::string16::size_type* end) const OVERRIDE;
76  virtual void SelectAll(bool reversed) OVERRIDE;
77  virtual void RevertAll() OVERRIDE;
78  virtual void SetFocus() OVERRIDE;
79  virtual int GetTextWidth() const OVERRIDE;
80  virtual bool IsImeComposing() const OVERRIDE;
81  virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
82
83 private:
84  // Return the number of characers in the current buffer.
85  virtual int GetOmniboxTextLength() const OVERRIDE;
86
87  // Try to parse the current text as a URL and colorize the components.
88  virtual void EmphasizeURLComponents() OVERRIDE;
89
90  // Update the field with |text| and set the selection.
91  void SetTextAndSelectedRange(const base::string16& text,
92                               const gfx::Range& range);
93
94  // Returns the selected text.
95  base::string16 GetSelectedText() const;
96
97  // Paste text from the clipboard into the omnibox.
98  // Textfields implementation of Paste() pastes the contents of the clipboard
99  // as is. We want to strip whitespace and other things (see GetClipboardText()
100  // for details).
101  // It is assumed this is invoked after a call to OnBeforePossibleChange() and
102  // that after invoking this OnAfterPossibleChange() is invoked.
103  void OnPaste();
104
105  // Handle keyword hint tab-to-search and tabbing through dropdown results.
106  bool HandleEarlyTabActions(const ui::KeyEvent& event);
107
108  // OmniboxView:
109  virtual void SetWindowTextAndCaretPos(const base::string16& text,
110                                        size_t caret_pos,
111                                        bool update_popup,
112                                        bool notify_text_changed) OVERRIDE;
113  virtual bool IsSelectAll() const OVERRIDE;
114  virtual bool DeleteAtEndPressed() OVERRIDE;
115  virtual void UpdatePopup() OVERRIDE;
116  virtual void ApplyCaretVisibility() OVERRIDE;
117  virtual void OnTemporaryTextMaybeChanged(
118      const base::string16& display_text,
119      bool save_original_selection,
120      bool notify_text_changed) OVERRIDE;
121  virtual bool OnInlineAutocompleteTextMaybeChanged(
122      const base::string16& display_text, size_t user_text_length) OVERRIDE;
123  virtual void OnInlineAutocompleteTextCleared() OVERRIDE;
124  virtual void OnRevertTemporaryText() OVERRIDE;
125  virtual void OnBeforePossibleChange() OVERRIDE;
126  virtual bool OnAfterPossibleChange() OVERRIDE;
127  virtual gfx::NativeView GetNativeView() const OVERRIDE;
128  virtual gfx::NativeView GetRelativeWindowForPopup() const OVERRIDE;
129  virtual void SetGrayTextAutocompletion(const base::string16& input) OVERRIDE;
130  virtual base::string16 GetGrayTextAutocompletion() const OVERRIDE;
131  virtual int GetWidth() const OVERRIDE;
132  virtual bool IsImeShowingPopup() const OVERRIDE;
133  virtual void ShowImeIfNeeded() OVERRIDE;
134  virtual void OnMatchOpened(const AutocompleteMatch& match,
135                             Profile* profile,
136                             content::WebContents* web_contents) const OVERRIDE;
137  virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
138  virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE;
139  virtual base::string16 GetLabelForCommandId(int command_id) const OVERRIDE;
140
141  // views::Textfield:
142  virtual const char* GetClassName() const OVERRIDE;
143  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
144  virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
145  virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
146  virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
147  virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
148  virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE;
149  virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
150  virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
151  virtual bool SkipDefaultKeyEventProcessing(
152      const ui::KeyEvent& event) OVERRIDE;
153  virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
154  virtual void OnFocus() OVERRIDE;
155  virtual void OnBlur() OVERRIDE;
156  virtual base::string16 GetSelectionClipboardText() const OVERRIDE;
157
158  // gfx::AnimationDelegate:
159  virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
160  virtual void AnimationEnded(const gfx::Animation* animation) 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  scoped_ptr<gfx::SlideAnimation> fade_in_animation_;
227
228  DISALLOW_COPY_AND_ASSIGN(OmniboxViewViews);
229};
230
231#endif  // CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
232