1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file.
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_MODEL_H_
6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_MODEL_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
9ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/scoped_ptr.h"
10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/autocomplete/autocomplete.h"
11dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/browser/autocomplete/autocomplete_edit.h"
12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass AutocompleteEditView;
14dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass AutocompletePopupView;
15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass Profile;
16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SkBitmap;
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
1872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenclass AutocompletePopupModel {
19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutocompletePopupModel(AutocompletePopupView* popup_view,
21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                         AutocompleteEditModel* edit_model,
22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                         Profile* profile);
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ~AutocompletePopupModel();
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Invoked when the profile has changed.
26dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  void set_profile(Profile* profile) { profile_ = profile; }
27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
28dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // TODO(sky): see about removing this.
29dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  Profile* profile() const { return profile_; }
30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns true if the popup is currently open.
32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool IsOpen() const;
33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
343345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  AutocompletePopupView* view() const { return view_; }
353345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns the AutocompleteController used by this popup.
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutocompleteController* autocomplete_controller() const {
38dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    return edit_model_->autocomplete_controller();
39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const AutocompleteResult& result() const {
42dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    return autocomplete_controller()->result();
43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  size_t hovered_line() const {
46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return hovered_line_;
47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Call to change the hovered line.  |line| should be within the range of
50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // valid lines (to enable hover) or kNoMatch (to disable hover).
51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetHoveredLine(size_t line);
52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  size_t selected_line() const {
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return selected_line_;
55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Call to change the selected line.  This will update all state and repaint
58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the necessary parts of the window, as well as updating the edit with the
593345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // new temporary text.  |line| will be clamped to the range of valid lines.
60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // |reset_to_default| is true when the selection is being reset back to the
61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // default match, and thus there is no temporary text (and no
6272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // |manually_selected_match_|). If |force| is true then the selected line will
6372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // be updated forcibly even if the |line| is same as the current selected
6472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // line.
65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // NOTE: This assumes the popup is open, and thus both old and new values for
66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the selected line should not be kNoMatch.
6772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  void SetSelectedLine(size_t line, bool reset_to_default, bool force);
68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called when the user hits escape after arrowing around the popup.  This
70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // will change the selected line back to the default match and redraw.
71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void ResetToDefaultMatch();
72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
73ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Gets the selected keyword or keyword hint for the given match. If the match
74ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // is already keyword, then the keyword will be returned directly. Otherwise,
75ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // it returns GetKeywordForText(match.fill_into_edit, keyword).
76ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool GetKeywordForMatch(const AutocompleteMatch& match,
77ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                          string16* keyword) const;
78ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
79ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Gets the selected keyword or keyword hint for the given text. Returns
80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // true if |keyword| represents a keyword hint, or false if |keyword|
81ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // represents a selected keyword. (|keyword| will always be set [though
82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // possibly to the empty string], and you cannot have both a selected keyword
83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // and a keyword hint simultaneously.)
84ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool GetKeywordForText(const string16& text, string16* keyword) const;
85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Immediately updates and opens the popup if necessary, then moves the
87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // current selection down (|count| > 0) or up (|count| < 0), clamping to the
88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // first or last result if necessary.  If |count| == 0, the selection will be
89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // unchanged, but the popup will still redraw and modify the text in the
90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // AutocompleteEditModel.
91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Move(int count);
92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Called when the user hits shift-delete.  This should determine if the item
94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // can be removed from history, and if so, remove it and update the popup.
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void TryDeletingCurrentItem();
96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
97ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // If |match| is from an extension, returns the extension icon; otherwise
98ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // returns NULL.
99ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  const SkBitmap* GetIconIfExtensionMatch(const AutocompleteMatch& match) const;
100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
101dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // The match the user has manually chosen, if any.
102dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  const AutocompleteResult::Selection& manually_selected_match() const {
103dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    return manually_selected_match_;
104dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  }
105731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick
10672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Invoked from the edit model any time the result set of the controller
10772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // changes.
10872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  void OnResultChanged();
10972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The token value for selected_line_, hover_line_ and functions dealing with
111c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // a "line number" that indicates "no line".
112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static const size_t kNoMatch = -1;
113c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private:
115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutocompletePopupView* view_;
116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
117c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutocompleteEditModel* edit_model_;
118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Profile for current tab.
120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  Profile* profile_;
121c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
122c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The line that's currently hovered.  If we're not drawing a hover rect,
123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // this will be kNoMatch, even if the cursor is over the popup contents.
124c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  size_t hovered_line_;
125c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
126c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The currently selected line.  This is kNoMatch when nothing is selected,
127c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // which should only be true when the popup is closed.
128c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  size_t selected_line_;
129c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
130c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The match the user has manually chosen, if any.
131c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  AutocompleteResult::Selection manually_selected_match_;
132c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
133c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DISALLOW_COPY_AND_ASSIGN(AutocompletePopupModel);
134c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
135c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
136c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_MODEL_H_
137