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