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