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