blob: 2d040125a62c673e5bc63ec3d8c49836550adb05 [file] [log] [blame]
[email protected]3cb0f8d92012-02-29 05:43:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]93e50332009-03-02 18:58:262// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b76ac712011-05-03 22:17:115// This file defines the interface class OmniboxView. Each toolkit will
6// implement the edit view differently, so that code is inherently platform
[email protected]fbdc4232012-06-24 15:28:377// 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]93e50332009-03-02 18:58:2610
[email protected]b76ac712011-05-03 22:17:1111#ifndef CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_
12#define CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_
[email protected]93e50332009-03-02 18:58:2613
14#include <string>
15
[email protected]553aabb2010-11-11 00:37:1516#include "base/string16.h"
[email protected]067095a2011-05-24 23:43:4417#include "base/string_util.h"
18#include "base/utf_string_conversions.h"
[email protected]7e41c2b2011-05-06 10:31:1619#include "chrome/browser/autocomplete/autocomplete_match.h"
[email protected]c51eed72012-08-07 22:01:5520#include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
21#include "chrome/browser/ui/toolbar/toolbar_model.h"
[email protected]a1d29162011-10-14 17:14:0322#include "content/public/common/url_constants.h"
[email protected]08397d52011-02-05 01:53:3823#include "ui/gfx/native_widget_types.h"
[email protected]93e50332009-03-02 18:58:2624#include "webkit/glue/window_open_disposition.h"
25
[email protected]ce47a3c2010-03-04 03:30:5526class CommandUpdater;
[email protected]93e50332009-03-02 18:58:2627class GURL;
[email protected]c51eed72012-08-07 22:01:5528class OmniboxEditController;
29class Profile;
30class ToolbarModel;
[email protected]83a2610a2012-01-05 01:00:2731
32namespace content {
33class WebContents;
34}
[email protected]93e50332009-03-02 18:58:2635
[email protected]caa62f472011-01-07 17:46:5936#if defined(TOOLKIT_VIEWS)
[email protected]33b62262011-11-09 00:58:4737// TODO(beng): Move all views-related code to a views-specific sub-interface.
[email protected]28094252012-06-07 19:51:1538namespace gfx {
39class Font;
40}
41
[email protected]caa62f472011-01-07 17:46:5942namespace views {
[email protected]557205c2011-02-17 18:44:0143class DropTargetEvent;
[email protected]caa62f472011-01-07 17:46:5944class View;
[email protected]28094252012-06-07 19:51:1545}
[email protected]caa62f472011-01-07 17:46:5946#endif
47
[email protected]b76ac712011-05-03 22:17:1148class OmniboxView {
[email protected]93e50332009-03-02 18:58:2649 public:
[email protected]c51eed72012-08-07 22:01:5550 virtual ~OmniboxView();
51
[email protected]93e50332009-03-02 18:58:2652 // Used by the automation system for getting at the model from the view.
[email protected]c51eed72012-08-07 22:01:5553 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]93e50332009-03-02 18:58:2658
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]83a2610a2012-01-05 01:00:2761 virtual void SaveStateToTab(content::WebContents* tab) = 0;
[email protected]93e50332009-03-02 18:58:2662
63 // Called when any LocationBarView state changes. If
[email protected]83a2610a2012-01-05 01:00:2764 // |tab_for_state_restoring| is non-NULL, it points to a WebContents whose
[email protected]93e50332009-03-02 18:58:2665 // state we should restore.
[email protected]83a2610a2012-01-05 01:00:2766 virtual void Update(const content::WebContents* tab_for_state_restoring) = 0;
[email protected]93e50332009-03-02 18:58:2667
[email protected]7e41c2b2011-05-06 10:31:1668 // 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]93e50332009-03-02 18:58:2673 //
74 // |selected_line| is passed to SendOpenNotification(); see comments there.
75 //
[email protected]93e50332009-03-02 18:58:2676 // This may close the popup.
[email protected]7e41c2b2011-05-06 10:31:1677 virtual void OpenMatch(const AutocompleteMatch& match,
78 WindowOpenDisposition disposition,
79 const GURL& alternate_nav_url,
[email protected]c51eed72012-08-07 22:01:5580 size_t selected_line);
[email protected]93e50332009-03-02 18:58:2681
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]a2fedb1e2011-01-25 15:23:3685 virtual string16 GetText() const = 0;
[email protected]93e50332009-03-02 18:58:2686
[email protected]69c579e2010-04-23 20:01:0087 // |true| if the user is in the process of editing the field, or if
88 // the field is empty.
[email protected]c51eed72012-08-07 22:01:5589 bool IsEditingOrEmpty() const;
[email protected]69c579e2010-04-23 20:01:0090
91 // Returns the resource ID of the icon to show for the current text.
[email protected]c51eed72012-08-07 22:01:5592 int GetIcon() const;
[email protected]69c579e2010-04-23 20:01:0093
[email protected]93e50332009-03-02 18:58:2694 // 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]c51eed72012-08-07 22:01:5597 void SetUserText(const string16& text);
[email protected]a2fedb1e2011-01-25 15:23:3698 virtual void SetUserText(const string16& text,
99 const string16& display_text,
[email protected]c51eed72012-08-07 22:01:55100 bool update_popup);
[email protected]93e50332009-03-02 18:58:26101
102 // Sets the window text and the caret position.
[email protected]a2fedb1e2011-01-25 15:23:36103 virtual void SetWindowTextAndCaretPos(const string16& text,
[email protected]3cb0f8d92012-02-29 05:43:34104 size_t caret_pos,
105 bool update_popup,
106 bool notify_text_changed) = 0;
[email protected]93e50332009-03-02 18:58:26107
[email protected]731980c2009-06-05 19:41:46108 // 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]3c11b5b02010-09-11 05:20:42111 // non-whitespace character is '?'), the text after the '?' is selected.
[email protected]731980c2009-06-05 19:41:46112 //
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]5fdafa32009-08-28 00:31:28117 // Returns true if all text is selected or there is no text at all.
[email protected]fb8e3a32012-05-10 21:03:52118 virtual bool IsSelectAll() const = 0;
[email protected]93e50332009-03-02 18:58:26119
[email protected]b93b79ec2010-11-19 20:21:22120 // Returns true if the user deleted the suggested text.
121 virtual bool DeleteAtEndPressed() = 0;
122
[email protected]3c11b5b02010-09-11 05:20:42123 // 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]4003e722011-10-31 05:04:03127 virtual void GetSelectionBounds(size_t* start, size_t* end) const = 0;
[email protected]3c11b5b02010-09-11 05:20:42128
[email protected]93e50332009-03-02 18:58:26129 // 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]c51eed72012-08-07 22:01:55135 virtual void RevertAll();
[email protected]93e50332009-03-02 18:58:26136
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]c51eed72012-08-07 22:01:55141 // 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]93e50332009-03-02 18:58:26145
[email protected]2be2c0132009-09-14 20:20:35146 // Sets the focus to the autocomplete view.
147 virtual void SetFocus() = 0;
148
[email protected]93e50332009-03-02 18:58:26149 // 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]a2fedb1e2011-01-25 15:23:36153 virtual void OnTemporaryTextMaybeChanged(const string16& display_text,
[email protected]93e50332009-03-02 18:58:26154 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]a2fedb1e2011-01-25 15:23:36161 const string16& display_text, size_t user_text_length) = 0;
[email protected]93e50332009-03-02 18:58:26162
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]fb5153c52009-07-31 19:40:33174
175 // Returns the gfx::NativeView of the edit view.
176 virtual gfx::NativeView GetNativeView() const = 0;
[email protected]135fd3b62009-12-16 01:07:08177
[email protected]b2544aa92012-06-21 04:20:09178 // 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]3510d442011-10-07 22:13:46182 virtual gfx::NativeView GetRelativeWindowForPopup() const = 0;
183
[email protected]33b8b8e2011-03-15 14:51:55184 // 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]eec44d942011-01-11 08:27:24188
[email protected]911696b2011-01-28 02:36:49189 // Returns the current instant suggestion text.
190 virtual string16 GetInstantSuggestion() const = 0;
191
[email protected]eec44d942011-01-11 08:27:24192 // 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]28ea1c92011-01-13 00:30:18196 // Returns true if the user is composing something in an IME.
197 virtual bool IsImeComposing() const = 0;
198
[email protected]caa62f472011-01-07 17:46:59199#if defined(TOOLKIT_VIEWS)
[email protected]33b62262011-11-09 00:58:47200 virtual int GetMaxEditWidth(int entry_width) const = 0;
201
[email protected]caa62f472011-01-07 17:46:59202 // 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]557205c2011-02-17 18:44:01205
206 // Performs the drop of a drag and drop operation on the view.
207 virtual int OnPerformDrop(const views::DropTargetEvent& event) = 0;
[email protected]28094252012-06-07 19:51:15208
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]caa62f472011-01-07 17:46:59215#endif
216
[email protected]067095a2011-05-24 23:43:44217 // Returns a string with any leading javascript schemas stripped from the
218 // input text.
219 static string16 StripJavascriptSchemas(const string16& text);
220
[email protected]b1ce30692012-05-15 18:26:35221 // 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]c51eed72012-08-07 22:01:55226 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]93e50332009-03-02 18:58:26255};
256
[email protected]b76ac712011-05-03 22:17:11257#endif // CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_