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