omnibox_view_views.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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 int font_y_offset); 51 virtual ~OmniboxViewViews(); 52 53 // Initialize, create the underlying views, etc; 54 void Init(); 55 56 // Sets the colors of the text view according to the theme. 57 void SetBaseColor(); 58 59 // views::Textfield: 60 virtual const char* GetClassName() const OVERRIDE; 61 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 62 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 63 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 64 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE; 65 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 66 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; 67 virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE; 68 virtual bool SkipDefaultKeyEventProcessing( 69 const ui::KeyEvent& event) OVERRIDE; 70 virtual void OnFocus() OVERRIDE; 71 virtual void OnBlur() OVERRIDE; 72 73 // OmniboxView: 74 virtual void SaveStateToTab(content::WebContents* tab) OVERRIDE; 75 virtual void OnTabChanged(const content::WebContents* web_contents) OVERRIDE; 76 virtual void Update() OVERRIDE; 77 virtual string16 GetText() const OVERRIDE; 78 virtual void SetWindowTextAndCaretPos(const string16& text, 79 size_t caret_pos, 80 bool update_popup, 81 bool notify_text_changed) OVERRIDE; 82 virtual void SetForcedQuery() OVERRIDE; 83 virtual bool IsSelectAll() const OVERRIDE; 84 virtual bool DeleteAtEndPressed() OVERRIDE; 85 virtual void GetSelectionBounds(string16::size_type* start, 86 string16::size_type* end) const OVERRIDE; 87 virtual void SelectAll(bool reversed) OVERRIDE; 88 virtual void UpdatePopup() OVERRIDE; 89 virtual void SetFocus() OVERRIDE; 90 virtual void ApplyCaretVisibility() OVERRIDE; 91 virtual void OnTemporaryTextMaybeChanged( 92 const string16& display_text, 93 bool save_original_selection, 94 bool notify_text_changed) OVERRIDE; 95 virtual bool OnInlineAutocompleteTextMaybeChanged( 96 const string16& display_text, size_t user_text_length) OVERRIDE; 97 virtual void OnRevertTemporaryText() OVERRIDE; 98 virtual void OnBeforePossibleChange() OVERRIDE; 99 virtual bool OnAfterPossibleChange() OVERRIDE; 100 virtual gfx::NativeView GetNativeView() const OVERRIDE; 101 virtual gfx::NativeView GetRelativeWindowForPopup() const OVERRIDE; 102 virtual void SetGrayTextAutocompletion(const string16& input) OVERRIDE; 103 virtual string16 GetGrayTextAutocompletion() const OVERRIDE; 104 virtual int TextWidth() const OVERRIDE; 105 virtual bool IsImeComposing() const OVERRIDE; 106 virtual bool IsImeShowingPopup() const OVERRIDE; 107 virtual int GetMaxEditWidth(int entry_width) const OVERRIDE; 108 virtual views::View* AddToView(views::View* parent) OVERRIDE; 109 virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE; 110 111 // views::TextfieldController: 112 virtual void ContentsChanged(views::Textfield* sender, 113 const string16& new_contents) OVERRIDE; 114 virtual bool HandleKeyEvent(views::Textfield* sender, 115 const ui::KeyEvent& key_event) OVERRIDE; 116 virtual void OnBeforeUserAction(views::Textfield* sender) OVERRIDE; 117 virtual void OnAfterUserAction(views::Textfield* sender) OVERRIDE; 118 virtual void OnAfterCutOrCopy() OVERRIDE; 119 virtual void OnWriteDragData(ui::OSExchangeData* data) OVERRIDE; 120 virtual void OnGetDragOperationsForTextfield(int* drag_operations) OVERRIDE; 121 virtual void AppendDropFormats( 122 int* formats, 123 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE; 124 virtual int OnDrop(const ui::OSExchangeData& data) OVERRIDE; 125 virtual void UpdateContextMenu(ui::SimpleMenuModel* menu_contents) OVERRIDE; 126 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; 127 virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE; 128 virtual string16 GetLabelForCommandId(int command_id) const OVERRIDE; 129 virtual bool HandlesCommand(int command_id) const OVERRIDE; 130 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; 131 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; 138#endif 139 140 private: 141 // Return the number of characers in the current buffer. 142 virtual int GetOmniboxTextLength() const OVERRIDE; 143 144 // Try to parse the current text as a URL and colorize the components. 145 virtual void EmphasizeURLComponents() OVERRIDE; 146 147 // Update the field with |text| and set the selection. 148 void SetTextAndSelectedRange(const string16& text, 149 const gfx::Range& range); 150 151 // Returns the selected text. 152 string16 GetSelectedText() const; 153 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() 157 // for details). 158 // It is assumed this is invoked after a call to OnBeforePossibleChange() and 159 // that after invoking this OnAfterPossibleChange() is invoked. 160 void OnPaste(); 161 162 // When true, the location bar view is read only and also is has a slightly 163 // different presentation (smaller font size). This is used for popups. 164 bool popup_window_mode_; 165 166 scoped_ptr<OmniboxPopupView> popup_view_; 167 168 ToolbarModel::SecurityLevel security_level_; 169 170 // Selection persisted across temporary text changes, like popup suggestions. 171 gfx::Range saved_temporary_selection_; 172 173 // Tracking state before and after a possible change. 174 string16 text_before_change_; 175 gfx::Range sel_before_change_; 176 bool ime_composing_before_change_; 177 178 // Was the delete key pressed with an empty selection at the end of the edit? 179 bool delete_at_end_pressed_; 180 LocationBarView* location_bar_view_; 181 182 // True if the IME candidate window is open. When this is true, we want to 183 // avoid showing the popup. So far, the candidate window is detected only 184 // on Chrome OS. 185 bool ime_candidate_window_open_; 186 187 // Should we select all the text when we see the mouse button get released? 188 // We select in response to a click that focuses the omnibox, but we defer 189 // until release, setting this variable back to false if we saw a drag, to 190 // allow the user to select just a portion of the text. 191 bool select_all_on_mouse_release_; 192 193 // Indicates if we want to select all text in the omnibox when we get a 194 // GESTURE_TAP. We want to select all only when the textfield is not in focus 195 // and gets a tap. So we use this variable to remember focus state before tap. 196 bool select_all_on_gesture_tap_; 197 198 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViews); 199}; 200 201#endif // CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_ 202