1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_POPUP_CONTENTS_VIEW_H_
6#define CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_POPUP_CONTENTS_VIEW_H_
7
8#include "base/memory/weak_ptr.h"
9#include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
10#include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
11#include "ui/base/window_open_disposition.h"
12#include "ui/gfx/animation/animation_delegate.h"
13#include "ui/gfx/animation/slide_animation.h"
14#include "ui/gfx/font_list.h"
15#include "ui/views/view.h"
16#include "ui/views/view_targeter_delegate.h"
17
18struct AutocompleteMatch;
19class LocationBarView;
20class OmniboxEditModel;
21class OmniboxResultView;
22class OmniboxView;
23class Profile;
24
25// A view representing the contents of the autocomplete popup.
26class OmniboxPopupContentsView : public views::View,
27                                 public OmniboxPopupView,
28                                 public views::ViewTargeterDelegate,
29                                 public gfx::AnimationDelegate {
30 public:
31  // Factory method for creating the AutocompletePopupView.
32  static OmniboxPopupView* Create(const gfx::FontList& font_list,
33                                  OmniboxView* omnibox_view,
34                                  OmniboxEditModel* edit_model,
35                                  LocationBarView* location_bar_view);
36
37  // Returns the bounds the popup should be shown at. This is the display bounds
38  // and includes offsets for the dropshadow which this view's border renders.
39  gfx::Rect GetPopupBounds() const;
40
41  virtual void LayoutChildren();
42
43  // Overridden from OmniboxPopupView:
44  virtual bool IsOpen() const OVERRIDE;
45  virtual void InvalidateLine(size_t line) OVERRIDE;
46  virtual void UpdatePopupAppearance() OVERRIDE;
47  virtual gfx::Rect GetTargetBounds() OVERRIDE;
48  virtual void PaintUpdatesNow() OVERRIDE;
49  virtual void OnDragCanceled() OVERRIDE;
50
51  // Overridden from gfx::AnimationDelegate:
52  virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
53
54  // Overridden from views::View:
55  virtual void Layout() OVERRIDE;
56  virtual views::View* GetTooltipHandlerForPoint(
57      const gfx::Point& point) OVERRIDE;
58  virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
59  virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
60  virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
61  virtual void OnMouseCaptureLost() OVERRIDE;
62  virtual void OnMouseMoved(const ui::MouseEvent& event) OVERRIDE;
63  virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
64  virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
65
66  // Overridden from ui::EventHandler:
67  virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
68
69  bool IsSelectedIndex(size_t index) const;
70  bool IsHoveredIndex(size_t index) const;
71  gfx::Image GetIconIfExtensionMatch(size_t index) const;
72  bool IsStarredMatch(const AutocompleteMatch& match) const;
73
74  int max_match_contents_width() const {
75    return max_match_contents_width_;
76  }
77
78 protected:
79  OmniboxPopupContentsView(const gfx::FontList& font_list,
80                           OmniboxView* omnibox_view,
81                           OmniboxEditModel* edit_model,
82                           LocationBarView* location_bar_view);
83  virtual ~OmniboxPopupContentsView();
84
85  LocationBarView* location_bar_view() { return location_bar_view_; }
86
87  virtual void PaintResultViews(gfx::Canvas* canvas);
88
89  // Calculates the height needed to show all the results in the model.
90  virtual int CalculatePopupHeight();
91  virtual OmniboxResultView* CreateResultView(int model_index,
92                                              const gfx::FontList& font_list);
93
94  // Overridden from views::View:
95  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
96  // This method should not be triggered directly as we paint our children
97  // in an un-conventional way inside OnPaint. We use a separate canvas to
98  // paint the children. Hence we override this method to a no-op so that
99  // the view hierarchy does not "accidentally" trigger this.
100  virtual void PaintChildren(gfx::Canvas* canvas,
101                             const views::CullSet& cull_set) OVERRIDE;
102
103  scoped_ptr<OmniboxPopupModel> model_;
104
105 private:
106  class AutocompletePopupWidget;
107
108  // views::ViewTargeterDelegate:
109  virtual views::View* TargetForRect(views::View* root,
110                                     const gfx::Rect& rect) OVERRIDE;
111
112  // Call immediately after construction.
113  void Init();
114
115  // Returns true if the model has a match at the specified index.
116  bool HasMatchAt(size_t index) const;
117
118  // Returns the match at the specified index within the popup model.
119  const AutocompleteMatch& GetMatchAtIndex(size_t index) const;
120
121  // Fill a path for the contents' roundrect. |bounding_rect| is the rect that
122  // bounds the path.
123  void MakeContentsPath(gfx::Path* path, const gfx::Rect& bounding_rect);
124
125  // Find the index of the match under the given |point|, specified in window
126  // coordinates. Returns OmniboxPopupModel::kNoMatch if there isn't a match at
127  // the specified point.
128  size_t GetIndexForPoint(const gfx::Point& point);
129
130  // Processes a located event (e.g. mouse/gesture) and sets the selection/hover
131  // state of a line in the list.
132  void UpdateLineEvent(const ui::LocatedEvent& event,
133                       bool should_set_selected_line);
134
135  // Opens an entry from the list depending on the event and the selected
136  // disposition.
137  void OpenSelectedLine(const ui::LocatedEvent& event,
138                        WindowOpenDisposition disposition);
139
140  OmniboxResultView* result_view_at(size_t i);
141
142  // The popup that contains this view.  We create this, but it deletes itself
143  // when its window is destroyed.  This is a WeakPtr because it's possible for
144  // the OS to destroy the window and thus delete this object before we're
145  // deleted, or without our knowledge.
146  base::WeakPtr<AutocompletePopupWidget> popup_;
147
148  // The edit view that invokes us.
149  OmniboxView* omnibox_view_;
150
151  LocationBarView* location_bar_view_;
152
153  // The font list used for result rows, based on the omnibox font list.
154  gfx::FontList font_list_;
155
156  // If the user cancels a dragging action (i.e. by pressing ESC), we don't have
157  // a convenient way to release mouse capture. Instead we use this flag to
158  // simply ignore all remaining drag events, and the eventual mouse release
159  // event. Since OnDragCanceled() can be called when we're not dragging, this
160  // flag is reset to false on a mouse pressed event, to make sure we don't
161  // erroneously ignore the next drag.
162  bool ignore_mouse_drag_;
163
164  // The popup sizes vertically using an animation when the popup is getting
165  // shorter (not larger, that makes it look "slow").
166  gfx::SlideAnimation size_animation_;
167  gfx::Rect start_bounds_;
168  gfx::Rect target_bounds_;
169
170  int left_margin_;
171  int right_margin_;
172
173  const gfx::ImageSkia* bottom_shadow_;  // Ptr owned by resource bundle.
174
175  // Amount of extra padding to add to the popup on the top and bottom.
176  int outside_vertical_padding_;
177
178  // When the dropdown is not wide enough while displaying postfix suggestions,
179  // we use the width of widest match contents to shift the suggestions so that
180  // the widest suggestion just reaches the end edge.
181  int max_match_contents_width_;
182
183  DISALLOW_COPY_AND_ASSIGN(OmniboxPopupContentsView);
184};
185
186#endif  // CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_POPUP_CONTENTS_VIEW_H_
187