omnibox_view_views.h revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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/range/range.h"
15#include "ui/base/window_open_disposition.h"
16#include "ui/views/controls/textfield/textfield.h"
17#include "ui/views/controls/textfield/textfield_controller.h"
18
19#if defined(OS_CHROMEOS)
20#include "chromeos/ime/input_method_manager.h"
21#endif
22
23class LocationBarView;
24class OmniboxPopupView;
25class Profile;
26
27namespace ui {
28class OSExchangeData;
29}  // namespace ui
30
31// Views-implementation of OmniboxView, based on the gtk implementation.
32class OmniboxViewViews
33    : public views::Textfield,
34      public OmniboxView,
35#if defined(OS_CHROMEOS)
36      public
37          chromeos::input_method::InputMethodManager::CandidateWindowObserver,
38#endif
39      public views::TextfieldController {
40 public:
41  // The internal view class name.
42  static const char kViewClassName[];
43
44  OmniboxViewViews(OmniboxEditController* controller,
45                   ToolbarModel* toolbar_model,
46                   Profile* profile,
47                   CommandUpdater* command_updater,
48                   bool popup_window_mode,
49                   LocationBarView* location_bar,
50                   const gfx::FontList& font_list,
51                   int font_y_offset);
52  virtual ~OmniboxViewViews();
53
54  // Initialize, create the underlying views, etc;
55  void Init();
56
57  // Sets the colors of the text view according to the theme.
58  void SetBaseColor();
59
60  // views::Textfield:
61  virtual const char* GetClassName() const OVERRIDE;
62  virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
63  virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
64  virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
65  virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
66  virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
67  virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
68  virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE;
69  virtual bool SkipDefaultKeyEventProcessing(
70      const ui::KeyEvent& event) OVERRIDE;
71  virtual void OnFocus() OVERRIDE;
72  virtual void OnBlur() OVERRIDE;
73
74  // OmniboxView:
75  virtual void SaveStateToTab(content::WebContents* tab) OVERRIDE;
76  virtual void Update(
77      const content::WebContents* tab_for_state_restoring) OVERRIDE;
78  virtual string16 GetText() const OVERRIDE;
79  virtual void SetWindowTextAndCaretPos(const string16& text,
80                                        size_t caret_pos,
81                                        bool update_popup,
82                                        bool notify_text_changed) OVERRIDE;
83  virtual void SetForcedQuery() OVERRIDE;
84  virtual bool IsSelectAll() const OVERRIDE;
85  virtual bool DeleteAtEndPressed() OVERRIDE;
86  virtual void GetSelectionBounds(string16::size_type* start,
87                                  string16::size_type* end) const OVERRIDE;
88  virtual void SelectAll(bool reversed) OVERRIDE;
89  virtual void UpdatePopup() OVERRIDE;
90  virtual void SetFocus() OVERRIDE;
91  virtual void ApplyCaretVisibility() OVERRIDE;
92  virtual void OnTemporaryTextMaybeChanged(
93      const string16& display_text,
94      bool save_original_selection,
95      bool notify_text_changed) OVERRIDE;
96  virtual bool OnInlineAutocompleteTextMaybeChanged(
97      const string16& display_text, size_t user_text_length) OVERRIDE;
98  virtual void OnRevertTemporaryText() OVERRIDE;
99  virtual void OnBeforePossibleChange() OVERRIDE;
100  virtual bool OnAfterPossibleChange() OVERRIDE;
101  virtual gfx::NativeView GetNativeView() const OVERRIDE;
102  virtual gfx::NativeView GetRelativeWindowForPopup() const OVERRIDE;
103  virtual void SetGrayTextAutocompletion(const string16& input) OVERRIDE;
104  virtual string16 GetGrayTextAutocompletion() const OVERRIDE;
105  virtual int TextWidth() const OVERRIDE;
106  virtual bool IsImeComposing() const OVERRIDE;
107  virtual bool IsImeShowingPopup() const OVERRIDE;
108  virtual int GetMaxEditWidth(int entry_width) const OVERRIDE;
109  virtual views::View* AddToView(views::View* parent) OVERRIDE;
110  virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE;
111
112  // views::TextfieldController:
113  virtual void ContentsChanged(views::Textfield* sender,
114                               const string16& new_contents) OVERRIDE;
115  virtual bool HandleKeyEvent(views::Textfield* sender,
116                              const ui::KeyEvent& key_event) OVERRIDE;
117  virtual void OnBeforeUserAction(views::Textfield* sender) OVERRIDE;
118  virtual void OnAfterUserAction(views::Textfield* sender) OVERRIDE;
119  virtual void OnAfterCutOrCopy() OVERRIDE;
120  virtual void OnWriteDragData(ui::OSExchangeData* data) OVERRIDE;
121  virtual void OnGetDragOperationsForTextfield(int* drag_operations) OVERRIDE;
122  virtual void AppendDropFormats(
123      int* formats,
124      std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
125  virtual int OnDrop(const ui::OSExchangeData& data) OVERRIDE;
126  virtual void UpdateContextMenu(ui::SimpleMenuModel* menu_contents) OVERRIDE;
127  virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
128  virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE;
129  virtual string16 GetLabelForCommandId(int command_id) const OVERRIDE;
130  virtual bool HandlesCommand(int command_id) const OVERRIDE;
131  virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
132
133#if defined(OS_CHROMEOS)
134  // chromeos::input_method::InputMethodManager::CandidateWindowObserver:
135  virtual void CandidateWindowOpened(
136      chromeos::input_method::InputMethodManager* manager) OVERRIDE;
137  virtual void CandidateWindowClosed(
138      chromeos::input_method::InputMethodManager* manager) OVERRIDE;
139#endif
140
141 private:
142  // Return the number of characers in the current buffer.
143  virtual int GetOmniboxTextLength() const OVERRIDE;
144
145  // Try to parse the current text as a URL and colorize the components.
146  virtual void EmphasizeURLComponents() OVERRIDE;
147
148  // Update the field with |text| and set the selection.
149  void SetTextAndSelectedRange(const string16& text,
150                               const ui::Range& range);
151
152  // Returns the selected text.
153  string16 GetSelectedText() const;
154
155  // Copy the URL instead of the text in the textfield into clipboard.
156  void CopyURL();
157
158  // Paste text from the clipboard into the omnibox.
159  // Textfields implementation of Paste() pastes the contents of the clipboard
160  // as is. We want to strip whitespace and other things (see GetClipboardText()
161  // for details).
162  // It is assumed this is invoked after a call to OnBeforePossibleChange() and
163  // that after invoking this OnAfterPossibleChange() is invoked.
164  void OnPaste();
165
166  // When true, the location bar view is read only and also is has a slightly
167  // different presentation (smaller font size). This is used for popups.
168  bool popup_window_mode_;
169
170  scoped_ptr<OmniboxPopupView> popup_view_;
171
172  ToolbarModel::SecurityLevel security_level_;
173
174  // Selection persisted across temporary text changes, like popup suggestions.
175  ui::Range saved_temporary_selection_;
176
177  // Tracking state before and after a possible change.
178  string16 text_before_change_;
179  ui::Range sel_before_change_;
180  bool ime_composing_before_change_;
181
182  // Was the delete key pressed with an empty selection at the end of the edit?
183  bool delete_at_end_pressed_;
184  LocationBarView* location_bar_view_;
185
186  // True if the IME candidate window is open. When this is true, we want to
187  // avoid showing the popup. So far, the candidate window is detected only
188  // on Chrome OS.
189  bool ime_candidate_window_open_;
190
191  // Should we select all the text when we see the mouse button get released?
192  // We select in response to a click that focuses the omnibox, but we defer
193  // until release, setting this variable back to false if we saw a drag, to
194  // allow the user to select just a portion of the text.
195  bool select_all_on_mouse_release_;
196
197  // Indicates if we want to select all text in the omnibox when we get a
198  // GESTURE_TAP. We want to select all only when the textfield is not in focus
199  // and gets a tap. So we use this variable to remember focus state before tap.
200  bool select_all_on_gesture_tap_;
201
202  DISALLOW_COPY_AND_ASSIGN(OmniboxViewViews);
203};
204
205#endif  // CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
206