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