[email protected] | 3cb0f8d9 | 2012-02-29 05:43:34 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b76ac71 | 2011-05-03 22:17:11 | [diff] [blame] | 5 | // This file defines the interface class OmniboxView. Each toolkit will |
| 6 | // implement the edit view differently, so that code is inherently platform |
[email protected] | fbdc423 | 2012-06-24 15:28:37 | [diff] [blame] | 7 | // specific. However, the OmniboxEditModel needs to do some communication with |
| 8 | // the view. Since the model is shared between platforms, we need to define an |
| 9 | // interface that all view implementations will share. |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 10 | |
[email protected] | b76ac71 | 2011-05-03 22:17:11 | [diff] [blame] | 11 | #ifndef CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_ |
| 12 | #define CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_ |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 13 | |
| 14 | #include <string> |
| 15 | |
[email protected] | 553aabb | 2010-11-11 00:37:15 | [diff] [blame] | 16 | #include "base/string16.h" |
[email protected] | 067095a | 2011-05-24 23:43:44 | [diff] [blame] | 17 | #include "base/string_util.h" |
| 18 | #include "base/utf_string_conversions.h" |
[email protected] | 7e41c2b | 2011-05-06 10:31:16 | [diff] [blame] | 19 | #include "chrome/browser/autocomplete/autocomplete_match.h" |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 20 | #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" |
| 21 | #include "chrome/browser/ui/toolbar/toolbar_model.h" |
[email protected] | a1d2916 | 2011-10-14 17:14:03 | [diff] [blame] | 22 | #include "content/public/common/url_constants.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 23 | #include "ui/gfx/native_widget_types.h" |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 24 | #include "webkit/glue/window_open_disposition.h" |
| 25 | |
[email protected] | ce47a3c | 2010-03-04 03:30:55 | [diff] [blame] | 26 | class CommandUpdater; |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 27 | class GURL; |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 28 | class OmniboxEditController; |
| 29 | class Profile; |
| 30 | class ToolbarModel; |
[email protected] | 83a2610a | 2012-01-05 01:00:27 | [diff] [blame] | 31 | |
| 32 | namespace content { |
| 33 | class WebContents; |
| 34 | } |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 35 | |
[email protected] | caa62f47 | 2011-01-07 17:46:59 | [diff] [blame] | 36 | #if defined(TOOLKIT_VIEWS) |
[email protected] | 33b6226 | 2011-11-09 00:58:47 | [diff] [blame] | 37 | // TODO(beng): Move all views-related code to a views-specific sub-interface. |
[email protected] | 2809425 | 2012-06-07 19:51:15 | [diff] [blame] | 38 | namespace gfx { |
| 39 | class Font; |
| 40 | } |
| 41 | |
[email protected] | caa62f47 | 2011-01-07 17:46:59 | [diff] [blame] | 42 | namespace views { |
[email protected] | 557205c | 2011-02-17 18:44:01 | [diff] [blame] | 43 | class DropTargetEvent; |
[email protected] | caa62f47 | 2011-01-07 17:46:59 | [diff] [blame] | 44 | class View; |
[email protected] | 2809425 | 2012-06-07 19:51:15 | [diff] [blame] | 45 | } |
[email protected] | caa62f47 | 2011-01-07 17:46:59 | [diff] [blame] | 46 | #endif |
| 47 | |
[email protected] | b76ac71 | 2011-05-03 22:17:11 | [diff] [blame] | 48 | class OmniboxView { |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 49 | public: |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 50 | virtual ~OmniboxView(); |
| 51 | |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 52 | // Used by the automation system for getting at the model from the view. |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 53 | OmniboxEditModel* model() { return model_.get(); } |
| 54 | const OmniboxEditModel* model() const { return model_.get(); } |
| 55 | |
| 56 | CommandUpdater* command_updater() { return command_updater_; } |
| 57 | const CommandUpdater* command_updater() const { return command_updater_; } |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 58 | |
| 59 | // For use when switching tabs, this saves the current state onto the tab so |
| 60 | // that it can be restored during a later call to Update(). |
[email protected] | 83a2610a | 2012-01-05 01:00:27 | [diff] [blame] | 61 | virtual void SaveStateToTab(content::WebContents* tab) = 0; |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 62 | |
| 63 | // Called when any LocationBarView state changes. If |
[email protected] | 83a2610a | 2012-01-05 01:00:27 | [diff] [blame] | 64 | // |tab_for_state_restoring| is non-NULL, it points to a WebContents whose |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 65 | // state we should restore. |
[email protected] | 83a2610a | 2012-01-05 01:00:27 | [diff] [blame] | 66 | virtual void Update(const content::WebContents* tab_for_state_restoring) = 0; |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 67 | |
[email protected] | 7e41c2b | 2011-05-06 10:31:16 | [diff] [blame] | 68 | // Asks the browser to load the specified match's |destination_url|, which |
| 69 | // is assumed to be one of the popup entries, using the supplied disposition |
| 70 | // and transition type. |alternate_nav_url|, if non-empty, contains the |
| 71 | // alternate navigation URL for for this match. See comments on |
| 72 | // AutocompleteResult::GetAlternateNavURL(). |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 73 | // |
| 74 | // |selected_line| is passed to SendOpenNotification(); see comments there. |
| 75 | // |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 76 | // This may close the popup. |
[email protected] | 7e41c2b | 2011-05-06 10:31:16 | [diff] [blame] | 77 | virtual void OpenMatch(const AutocompleteMatch& match, |
| 78 | WindowOpenDisposition disposition, |
| 79 | const GURL& alternate_nav_url, |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 80 | size_t selected_line); |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 81 | |
| 82 | // Returns the current text of the edit control, which could be the |
| 83 | // "temporary" text set by the popup, the "permanent" text set by the |
| 84 | // browser, or just whatever the user has currently typed. |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 85 | virtual string16 GetText() const = 0; |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 86 | |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 87 | // |true| if the user is in the process of editing the field, or if |
| 88 | // the field is empty. |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 89 | bool IsEditingOrEmpty() const; |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 90 | |
| 91 | // Returns the resource ID of the icon to show for the current text. |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 92 | int GetIcon() const; |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 93 | |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 94 | // The user text is the text the user has manually keyed in. When present, |
| 95 | // this is shown in preference to the permanent text; hitting escape will |
| 96 | // revert to the permanent text. |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 97 | void SetUserText(const string16& text); |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 98 | virtual void SetUserText(const string16& text, |
| 99 | const string16& display_text, |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 100 | bool update_popup); |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 101 | |
| 102 | // Sets the window text and the caret position. |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 103 | virtual void SetWindowTextAndCaretPos(const string16& text, |
[email protected] | 3cb0f8d9 | 2012-02-29 05:43:34 | [diff] [blame] | 104 | size_t caret_pos, |
| 105 | bool update_popup, |
| 106 | bool notify_text_changed) = 0; |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 107 | |
[email protected] | 731980c | 2009-06-05 19:41:46 | [diff] [blame] | 108 | // Sets the edit to forced query mode. Practically speaking, this means that |
| 109 | // if the edit is not in forced query mode, its text is set to "?" with the |
| 110 | // cursor at the end, and if the edit is in forced query mode (its first |
[email protected] | 3c11b5b0 | 2010-09-11 05:20:42 | [diff] [blame] | 111 | // non-whitespace character is '?'), the text after the '?' is selected. |
[email protected] | 731980c | 2009-06-05 19:41:46 | [diff] [blame] | 112 | // |
| 113 | // In the future we should display the search engine UI for the default engine |
| 114 | // rather than '?'. |
| 115 | virtual void SetForcedQuery() = 0; |
| 116 | |
[email protected] | 5fdafa3 | 2009-08-28 00:31:28 | [diff] [blame] | 117 | // Returns true if all text is selected or there is no text at all. |
[email protected] | fb8e3a3 | 2012-05-10 21:03:52 | [diff] [blame] | 118 | virtual bool IsSelectAll() const = 0; |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 119 | |
[email protected] | b93b79ec | 2010-11-19 20:21:22 | [diff] [blame] | 120 | // Returns true if the user deleted the suggested text. |
| 121 | virtual bool DeleteAtEndPressed() = 0; |
| 122 | |
[email protected] | 3c11b5b0 | 2010-09-11 05:20:42 | [diff] [blame] | 123 | // Fills |start| and |end| with the indexes of the current selection's bounds. |
| 124 | // It is not guaranteed that |*start < *end|, as the selection can be |
| 125 | // directed. If there is no selection, |start| and |end| will both be equal |
| 126 | // to the current cursor position. |
[email protected] | 4003e72 | 2011-10-31 05:04:03 | [diff] [blame] | 127 | virtual void GetSelectionBounds(size_t* start, size_t* end) const = 0; |
[email protected] | 3c11b5b0 | 2010-09-11 05:20:42 | [diff] [blame] | 128 | |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 129 | // Selects all the text in the edit. Use this in place of SetSelAll() to |
| 130 | // avoid selecting the "phantom newline" at the end of the edit. |
| 131 | virtual void SelectAll(bool reversed) = 0; |
| 132 | |
| 133 | // Reverts the edit and popup back to their unedited state (permanent text |
| 134 | // showing, popup closed, no user input in progress). |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 135 | virtual void RevertAll(); |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 136 | |
| 137 | // Updates the autocomplete popup and other state after the text has been |
| 138 | // changed by the user. |
| 139 | virtual void UpdatePopup() = 0; |
| 140 | |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 141 | // Closes the autocomplete popup, if it's open. The name |ClosePopup| |
| 142 | // conflicts with the OSX class override as that has a base class that also |
| 143 | // defines a method with that name. |
| 144 | virtual void CloseOmniboxPopup(); |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 145 | |
[email protected] | 2be2c013 | 2009-09-14 20:20:35 | [diff] [blame] | 146 | // Sets the focus to the autocomplete view. |
| 147 | virtual void SetFocus() = 0; |
| 148 | |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 149 | // Called when the temporary text in the model may have changed. |
| 150 | // |display_text| is the new text to show; |save_original_selection| is true |
| 151 | // when there wasn't previously a temporary text and thus we need to save off |
| 152 | // the user's existing selection. |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 153 | virtual void OnTemporaryTextMaybeChanged(const string16& display_text, |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 154 | bool save_original_selection) = 0; |
| 155 | |
| 156 | // Called when the inline autocomplete text in the model may have changed. |
| 157 | // |display_text| is the new text to show; |user_text_length| is the length of |
| 158 | // the user input portion of that (so, up to but not including the inline |
| 159 | // autocompletion). Returns whether the display text actually changed. |
| 160 | virtual bool OnInlineAutocompleteTextMaybeChanged( |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 161 | const string16& display_text, size_t user_text_length) = 0; |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 162 | |
| 163 | // Called when the temporary text has been reverted by the user. This will |
| 164 | // reset the user's original selection. |
| 165 | virtual void OnRevertTemporaryText() = 0; |
| 166 | |
| 167 | // Every piece of code that can change the edit should call these functions |
| 168 | // before and after the change. These functions determine if anything |
| 169 | // meaningful changed, and do any necessary updating and notification. |
| 170 | virtual void OnBeforePossibleChange() = 0; |
| 171 | // OnAfterPossibleChange() returns true if there was a change that caused it |
| 172 | // to call UpdatePopup(). |
| 173 | virtual bool OnAfterPossibleChange() = 0; |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 174 | |
| 175 | // Returns the gfx::NativeView of the edit view. |
| 176 | virtual gfx::NativeView GetNativeView() const = 0; |
[email protected] | 135fd3b6 | 2009-12-16 01:07:08 | [diff] [blame] | 177 | |
[email protected] | b2544aa9 | 2012-06-21 04:20:09 | [diff] [blame] | 178 | // Gets the relative window for the pop up window of OmniboxPopupView. The pop |
| 179 | // up window will be shown under the relative window. When an IME is attached |
| 180 | // to the rich edit control, the IME window is the relative window. Otherwise, |
| 181 | // the top-most window is the relative window. |
[email protected] | 3510d44 | 2011-10-07 22:13:46 | [diff] [blame] | 182 | virtual gfx::NativeView GetRelativeWindowForPopup() const = 0; |
| 183 | |
[email protected] | 33b8b8e | 2011-03-15 14:51:55 | [diff] [blame] | 184 | // Shows the instant suggestion text. If |animate_to_complete| is true the |
| 185 | // view should start an animation that when done commits the text. |
| 186 | virtual void SetInstantSuggestion(const string16& input, |
| 187 | bool animate_to_complete) = 0; |
[email protected] | eec44d94 | 2011-01-11 08:27:24 | [diff] [blame] | 188 | |
[email protected] | 911696b | 2011-01-28 02:36:49 | [diff] [blame] | 189 | // Returns the current instant suggestion text. |
| 190 | virtual string16 GetInstantSuggestion() const = 0; |
| 191 | |
[email protected] | eec44d94 | 2011-01-11 08:27:24 | [diff] [blame] | 192 | // Returns the width in pixels needed to display the current text. The |
| 193 | // returned value includes margins. |
| 194 | virtual int TextWidth() const = 0; |
| 195 | |
[email protected] | 28ea1c9 | 2011-01-13 00:30:18 | [diff] [blame] | 196 | // Returns true if the user is composing something in an IME. |
| 197 | virtual bool IsImeComposing() const = 0; |
| 198 | |
[email protected] | caa62f47 | 2011-01-07 17:46:59 | [diff] [blame] | 199 | #if defined(TOOLKIT_VIEWS) |
[email protected] | 33b6226 | 2011-11-09 00:58:47 | [diff] [blame] | 200 | virtual int GetMaxEditWidth(int entry_width) const = 0; |
| 201 | |
[email protected] | caa62f47 | 2011-01-07 17:46:59 | [diff] [blame] | 202 | // Adds the autocomplete edit view to view hierarchy and |
| 203 | // returns the views::View of the edit view. |
| 204 | virtual views::View* AddToView(views::View* parent) = 0; |
[email protected] | 557205c | 2011-02-17 18:44:01 | [diff] [blame] | 205 | |
| 206 | // Performs the drop of a drag and drop operation on the view. |
| 207 | virtual int OnPerformDrop(const views::DropTargetEvent& event) = 0; |
[email protected] | 2809425 | 2012-06-07 19:51:15 | [diff] [blame] | 208 | |
| 209 | // Returns the font. |
| 210 | virtual gfx::Font GetFont() = 0; |
| 211 | |
| 212 | // Returns the width in pixels needed to display the text from one character |
| 213 | // before the caret to the end of the string. |
| 214 | virtual int WidthOfTextAfterCursor() = 0; |
[email protected] | caa62f47 | 2011-01-07 17:46:59 | [diff] [blame] | 215 | #endif |
| 216 | |
[email protected] | 067095a | 2011-05-24 23:43:44 | [diff] [blame] | 217 | // Returns a string with any leading javascript schemas stripped from the |
| 218 | // input text. |
| 219 | static string16 StripJavascriptSchemas(const string16& text); |
| 220 | |
[email protected] | b1ce3069 | 2012-05-15 18:26:35 | [diff] [blame] | 221 | // Returns the current clipboard contents as a string that can be pasted in. |
| 222 | // In addition to just getting CF_UNICODETEXT out, this can also extract URLs |
| 223 | // from bookmarks on the clipboard. |
| 224 | static string16 GetClipboardText(); |
| 225 | |
[email protected] | c51eed7 | 2012-08-07 22:01:55 | [diff] [blame^] | 226 | protected: |
| 227 | OmniboxView(Profile* profile, |
| 228 | OmniboxEditController* controller, |
| 229 | ToolbarModel* toolbar_model, |
| 230 | CommandUpdater* command_updater); |
| 231 | |
| 232 | // Internally invoked whenever the text changes in some way. |
| 233 | virtual void TextChanged(); |
| 234 | |
| 235 | // Return the number of characters in the current buffer. The name |
| 236 | // |GetTextLength| can't be used as the Windows override of this class |
| 237 | // inherits from a class that defines a method with that name. |
| 238 | virtual int GetOmniboxTextLength() const = 0; |
| 239 | |
| 240 | // Try to parse the current text as a URL and colorize the components. |
| 241 | virtual void EmphasizeURLComponents() = 0; |
| 242 | |
| 243 | OmniboxEditController* controller() { return controller_; } |
| 244 | ToolbarModel* toolbar_model() { return toolbar_model_; } |
| 245 | |
| 246 | private: |
| 247 | // |model_| can be NULL in tests. |
| 248 | scoped_ptr<OmniboxEditModel> model_; |
| 249 | OmniboxEditController* controller_; |
| 250 | ToolbarModel* toolbar_model_; |
| 251 | |
| 252 | // The object that handles additional command functionality exposed on the |
| 253 | // edit, such as invoking the keyword editor. |
| 254 | CommandUpdater* command_updater_; |
[email protected] | 93e5033 | 2009-03-02 18:58:26 | [diff] [blame] | 255 | }; |
| 256 | |
[email protected] | b76ac71 | 2011-05-03 22:17:11 | [diff] [blame] | 257 | #endif // CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_ |