1 // Copyright (c) 2011 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_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_VIEWS_H_ 6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_VIEWS_H_ 7 #pragma once 8 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "chrome/browser/autocomplete/autocomplete_edit_view.h" 14 #include "chrome/browser/ui/toolbar/toolbar_model.h" 15 #include "content/common/notification_observer.h" 16 #include "content/common/page_transition_types.h" 17 #include "ui/base/range/range.h" 18 #include "views/controls/textfield/textfield_controller.h" 19 #include "views/view.h" 20 #include "webkit/glue/window_open_disposition.h" 21 22 class AutocompleteEditController; 23 class AutocompleteEditModel; 24 class AutocompletePopupView; 25 class Profile; 26 class TabContents; 27 28 // Views-implementation of AutocompleteEditView. This is based on 29 // gtk implementation. The following features are not yet supported. 30 // 31 // IME support. 32 // LTR support. 33 // Selection behavior. 34 // Cut,copy and paste behavior. 35 // Drag and drop behavior. 36 // URL styles (strikestrough insecure scheme, emphasize host). 37 // Custom context menu for omnibox. 38 // Instant. 39 class AutocompleteEditViewViews : public views::View, 40 public AutocompleteEditView, 41 public NotificationObserver, 42 public views::TextfieldController { 43 public: 44 AutocompleteEditViewViews(AutocompleteEditController* controller, 45 ToolbarModel* toolbar_model, 46 Profile* profile, 47 CommandUpdater* command_updater, 48 bool popup_window_mode, 49 const views::View* location_bar); 50 virtual ~AutocompleteEditViewViews(); 51 52 // Initialize, create the underlying views, etc; 53 void Init(); 54 55 // Sets the colors of the text view according to the theme. 56 void SetBaseColor(); 57 58 // Called after key even is handled either by HandleKeyEvent or by Textfield. 59 bool HandleAfterKeyEvent(const views::KeyEvent& event, bool handled); 60 61 // Called when KeyRelease event is generated on textfield. 62 bool HandleKeyReleaseEvent(const views::KeyEvent& event); 63 64 // Called when Focus is set/unset on textfield. 65 void HandleFocusIn(); 66 void HandleFocusOut(); 67 68 // Implements views::View 69 virtual void Layout() OVERRIDE; 70 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 71 72 // Implement the AutocompleteEditView interface. 73 virtual AutocompleteEditModel* model() OVERRIDE; 74 virtual const AutocompleteEditModel* model() const OVERRIDE; 75 76 virtual void SaveStateToTab(TabContents* tab) OVERRIDE; 77 78 virtual void Update(const TabContents* tab_for_state_restoring) OVERRIDE; 79 80 virtual void OpenURL(const GURL& url, 81 WindowOpenDisposition disposition, 82 PageTransition::Type transition, 83 const GURL& alternate_nav_url, 84 size_t selected_line, 85 const string16& keyword) OVERRIDE; 86 87 virtual string16 GetText() const OVERRIDE; 88 89 virtual bool IsEditingOrEmpty() const OVERRIDE; 90 virtual int GetIcon() const OVERRIDE; 91 virtual void SetUserText(const string16& text) OVERRIDE; 92 virtual void SetUserText(const string16& text, 93 const string16& display_text, 94 bool update_popup) OVERRIDE; 95 virtual void SetWindowTextAndCaretPos(const string16& text, 96 size_t caret_pos) OVERRIDE; 97 virtual void SetForcedQuery() OVERRIDE; 98 virtual bool IsSelectAll() OVERRIDE; 99 virtual bool DeleteAtEndPressed() OVERRIDE; 100 virtual void GetSelectionBounds(string16::size_type* start, 101 string16::size_type* end) OVERRIDE; 102 virtual void SelectAll(bool reversed) OVERRIDE; 103 virtual void RevertAll() OVERRIDE; 104 virtual void UpdatePopup() OVERRIDE; 105 virtual void ClosePopup() OVERRIDE; 106 virtual void SetFocus() OVERRIDE; 107 virtual void OnTemporaryTextMaybeChanged( 108 const string16& display_text, 109 bool save_original_selection) OVERRIDE; 110 virtual bool OnInlineAutocompleteTextMaybeChanged( 111 const string16& display_text, size_t user_text_length) OVERRIDE; 112 virtual void OnRevertTemporaryText() OVERRIDE; 113 virtual void OnBeforePossibleChange() OVERRIDE; 114 virtual bool OnAfterPossibleChange() OVERRIDE; 115 virtual gfx::NativeView GetNativeView() const OVERRIDE; 116 virtual CommandUpdater* GetCommandUpdater() OVERRIDE; 117 virtual void SetInstantSuggestion(const string16& input, 118 bool animate_to_complete) OVERRIDE; 119 virtual string16 GetInstantSuggestion() const OVERRIDE; 120 virtual int TextWidth() const OVERRIDE; 121 virtual bool IsImeComposing() const OVERRIDE; 122 virtual views::View* AddToView(views::View* parent) OVERRIDE; 123 virtual int OnPerformDrop(const views::DropTargetEvent& event) OVERRIDE; 124 125 // NotificationObserver: 126 virtual void Observe(NotificationType type, 127 const NotificationSource& source, 128 const NotificationDetails& details) OVERRIDE; 129 130 // views::TextfieldController: 131 virtual void ContentsChanged(views::Textfield* sender, 132 const string16& new_contents) OVERRIDE; 133 virtual bool HandleKeyEvent(views::Textfield* sender, 134 const views::KeyEvent& key_event) OVERRIDE; 135 virtual void OnBeforeUserAction(views::Textfield* sender) OVERRIDE; 136 virtual void OnAfterUserAction(views::Textfield* sender) OVERRIDE; 137 138 private: 139 // Return the number of characers in the current buffer. 140 size_t GetTextLength() const; 141 142 // Try to parse the current text as a URL and colorize the components. 143 void EmphasizeURLComponents(); 144 145 // Internally invoked whenever the text changes in some way. 146 void TextChanged(); 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 // Selects the text given by |caret| and |end|. 156 void SelectRange(size_t caret, size_t end); 157 158 AutocompletePopupView* CreatePopupView(Profile* profile, 159 const View* location_bar); 160 161 views::Textfield* textfield_; 162 163 scoped_ptr<AutocompleteEditModel> model_; 164 scoped_ptr<AutocompletePopupView> popup_view_; 165 AutocompleteEditController* controller_; 166 ToolbarModel* toolbar_model_; 167 168 // The object that handles additional command functionality exposed on the 169 // edit, such as invoking the keyword editor. 170 CommandUpdater* command_updater_; 171 172 // When true, the location bar view is read only and also is has a slightly 173 // different presentation (smaller font size). This is used for popups. 174 bool popup_window_mode_; 175 176 ToolbarModel::SecurityLevel security_level_; 177 178 // Selection at the point where the user started using the 179 // arrows to move around in the popup. 180 ui::Range saved_temporary_selection_; 181 182 // Tracking state before and after a possible change. 183 string16 text_before_change_; 184 ui::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 190 DISALLOW_COPY_AND_ASSIGN(AutocompleteEditViewViews); 191 }; 192 193 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_VIEWS_H_ 194