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