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