autocomplete_edit_view.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2010 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// This file defines the interface class AutocompleteEditView.  Each toolkit
6// will implement the edit view differently, so that code is inherently
7// platform specific.  However, the AutocompleteEditModel needs to do some
8// communication with the view.  Since the model is shared between platforms,
9// we need to define an interface that all view implementations will share.
10
11#ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_H_
12#define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_H_
13#pragma once
14
15#include <string>
16
17#include "base/string16.h"
18#include "chrome/common/page_transition_types.h"
19#include "ui/gfx/native_widget_types.h"
20#include "webkit/glue/window_open_disposition.h"
21
22class AutocompleteEditModel;
23class CommandUpdater;
24class GURL;
25class TabContents;
26
27#if defined(TOOLKIT_VIEWS)
28namespace views {
29class View;
30}  // namespace views
31#endif
32
33class AutocompleteEditView {
34 public:
35  // Used by the automation system for getting at the model from the view.
36  virtual AutocompleteEditModel* model() = 0;
37  virtual const AutocompleteEditModel* model() const = 0;
38
39  // For use when switching tabs, this saves the current state onto the tab so
40  // that it can be restored during a later call to Update().
41  virtual void SaveStateToTab(TabContents* tab) = 0;
42
43  // Called when any LocationBarView state changes. If
44  // |tab_for_state_restoring| is non-NULL, it points to a TabContents whose
45  // state we should restore.
46  virtual void Update(const TabContents* tab_for_state_restoring) = 0;
47
48  // Asks the browser to load the specified URL, which is assumed to be one of
49  // the popup entries, using the supplied disposition and transition type.
50  // |alternate_nav_url|, if non-empty, contains the alternate navigation URL
51  // for |url|.  See comments on AutocompleteResult::GetAlternateNavURL().
52  //
53  // |selected_line| is passed to SendOpenNotification(); see comments there.
54  //
55  // If the URL was expanded from a keyword, |keyword| is that keyword.
56  //
57  // This may close the popup.
58  virtual void OpenURL(const GURL& url,
59                       WindowOpenDisposition disposition,
60                       PageTransition::Type transition,
61                       const GURL& alternate_nav_url,
62                       size_t selected_line,
63                       const string16& keyword) = 0;
64
65  // Returns the current text of the edit control, which could be the
66  // "temporary" text set by the popup, the "permanent" text set by the
67  // browser, or just whatever the user has currently typed.
68  virtual string16 GetText() const = 0;
69
70  // |true| if the user is in the process of editing the field, or if
71  // the field is empty.
72  virtual bool IsEditingOrEmpty() const = 0;
73
74  // Returns the resource ID of the icon to show for the current text.
75  virtual int GetIcon() const = 0;
76
77  // The user text is the text the user has manually keyed in.  When present,
78  // this is shown in preference to the permanent text; hitting escape will
79  // revert to the permanent text.
80  virtual void SetUserText(const string16& text) = 0;
81  virtual void SetUserText(const string16& text,
82                           const string16& display_text,
83                           bool update_popup) = 0;
84
85  // Sets the window text and the caret position.
86  virtual void SetWindowTextAndCaretPos(const string16& text,
87                                        size_t caret_pos) = 0;
88
89  // Sets the edit to forced query mode.  Practically speaking, this means that
90  // if the edit is not in forced query mode, its text is set to "?" with the
91  // cursor at the end, and if the edit is in forced query mode (its first
92  // non-whitespace character is '?'), the text after the '?' is selected.
93  //
94  // In the future we should display the search engine UI for the default engine
95  // rather than '?'.
96  virtual void SetForcedQuery() = 0;
97
98  // Returns true if all text is selected or there is no text at all.
99  virtual bool IsSelectAll() = 0;
100
101  // Returns true if the user deleted the suggested text.
102  virtual bool DeleteAtEndPressed() = 0;
103
104  // Fills |start| and |end| with the indexes of the current selection's bounds.
105  // It is not guaranteed that |*start < *end|, as the selection can be
106  // directed.  If there is no selection, |start| and |end| will both be equal
107  // to the current cursor position.
108  virtual void GetSelectionBounds(string16::size_type* start,
109                                  string16::size_type* end) = 0;
110
111  // Selects all the text in the edit.  Use this in place of SetSelAll() to
112  // avoid selecting the "phantom newline" at the end of the edit.
113  virtual void SelectAll(bool reversed) = 0;
114
115  // Reverts the edit and popup back to their unedited state (permanent text
116  // showing, popup closed, no user input in progress).
117  virtual void RevertAll() = 0;
118
119  // Updates the autocomplete popup and other state after the text has been
120  // changed by the user.
121  virtual void UpdatePopup() = 0;
122
123  // Closes the autocomplete popup, if it's open.
124  virtual void ClosePopup() = 0;
125
126  // Sets the focus to the autocomplete view.
127  virtual void SetFocus() = 0;
128
129  // Called when the temporary text in the model may have changed.
130  // |display_text| is the new text to show; |save_original_selection| is true
131  // when there wasn't previously a temporary text and thus we need to save off
132  // the user's existing selection.
133  virtual void OnTemporaryTextMaybeChanged(const string16& display_text,
134                                           bool save_original_selection) = 0;
135
136  // Called when the inline autocomplete text in the model may have changed.
137  // |display_text| is the new text to show; |user_text_length| is the length of
138  // the user input portion of that (so, up to but not including the inline
139  // autocompletion).  Returns whether the display text actually changed.
140  virtual bool OnInlineAutocompleteTextMaybeChanged(
141      const string16& display_text, size_t user_text_length) = 0;
142
143  // Called when the temporary text has been reverted by the user.  This will
144  // reset the user's original selection.
145  virtual void OnRevertTemporaryText() = 0;
146
147  // Every piece of code that can change the edit should call these functions
148  // before and after the change.  These functions determine if anything
149  // meaningful changed, and do any necessary updating and notification.
150  virtual void OnBeforePossibleChange() = 0;
151  // OnAfterPossibleChange() returns true if there was a change that caused it
152  // to call UpdatePopup().
153  virtual bool OnAfterPossibleChange() = 0;
154
155  // Returns the gfx::NativeView of the edit view.
156  virtual gfx::NativeView GetNativeView() const = 0;
157
158  // Returns the command updater for this view.
159  virtual CommandUpdater* GetCommandUpdater() = 0;
160
161  // Shows the instant suggestion text.
162  virtual void SetInstantSuggestion(const string16& input) = 0;
163
164  // Returns the current instant suggestion text.
165  virtual string16 GetInstantSuggestion() const = 0;
166
167  // Returns the width in pixels needed to display the current text. The
168  // returned value includes margins.
169  virtual int TextWidth() const = 0;
170
171  // Returns true if the user is composing something in an IME.
172  virtual bool IsImeComposing() const = 0;
173
174#if defined(TOOLKIT_VIEWS)
175  // Adds the autocomplete edit view to view hierarchy and
176  // returns the views::View of the edit view.
177  virtual views::View* AddToView(views::View* parent) = 0;
178#endif
179
180  virtual ~AutocompleteEditView() {}
181};
182
183#endif  // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_H_
184