Home | History | Annotate | Download | only in autocomplete
      1 // Copyright (c) 2011 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_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_CONTENTS_VIEW_H_
      6 #define CHROME_BROWSER_UI_VIEWS_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_CONTENTS_VIEW_H_
      7 #pragma once
      8 
      9 #include "chrome/browser/autocomplete/autocomplete.h"
     10 #include "chrome/browser/autocomplete/autocomplete_popup_model.h"
     11 #include "chrome/browser/autocomplete/autocomplete_popup_view.h"
     12 #include "chrome/browser/ui/views/autocomplete/autocomplete_result_view_model.h"
     13 #include "ui/base/animation/animation_delegate.h"
     14 #include "ui/base/animation/slide_animation.h"
     15 #include "ui/gfx/font.h"
     16 #include "views/view.h"
     17 #include "webkit/glue/window_open_disposition.h"
     18 
     19 #if defined(OS_WIN)
     20 #include "chrome/browser/ui/views/autocomplete/autocomplete_popup_win.h"
     21 #else
     22 #include "chrome/browser/ui/views/autocomplete/autocomplete_popup_gtk.h"
     23 #endif
     24 
     25 class AutocompleteEditModel;
     26 class AutocompleteEditViewWin;
     27 struct AutocompleteMatch;
     28 class AutocompleteResultView;
     29 class BubbleBorder;
     30 class Profile;
     31 
     32 namespace gfx {
     33 class CanvasSkia;
     34 class Insets;
     35 }
     36 
     37 // A view representing the contents of the autocomplete popup.
     38 class AutocompletePopupContentsView : public views::View,
     39                                       public AutocompleteResultViewModel,
     40                                       public AutocompletePopupView,
     41                                       public ui::AnimationDelegate {
     42  public:
     43   AutocompletePopupContentsView(const gfx::Font& font,
     44                                 AutocompleteEditView* edit_view,
     45                                 AutocompleteEditModel* edit_model,
     46                                 Profile* profile,
     47                                 const views::View* location_bar);
     48   virtual ~AutocompletePopupContentsView();
     49 
     50   // Returns the bounds the popup should be shown at. This is the display bounds
     51   // and includes offsets for the dropshadow which this view's border renders.
     52   gfx::Rect GetPopupBounds() const;
     53 
     54   virtual void LayoutChildren();
     55 
     56   // Overridden from AutocompletePopupView:
     57   virtual bool IsOpen() const OVERRIDE;
     58   virtual void InvalidateLine(size_t line) OVERRIDE;
     59   virtual void UpdatePopupAppearance() OVERRIDE;
     60   virtual gfx::Rect GetTargetBounds() OVERRIDE;
     61   virtual void PaintUpdatesNow() OVERRIDE;
     62   virtual void OnDragCanceled() OVERRIDE;
     63 
     64   // Overridden from AutocompleteResultViewModel:
     65   virtual bool IsSelectedIndex(size_t index) const OVERRIDE;
     66   virtual bool IsHoveredIndex(size_t index) const OVERRIDE;
     67   virtual const SkBitmap* GetIconIfExtensionMatch(size_t index) const OVERRIDE;
     68 
     69   // Overridden from ui::AnimationDelegate:
     70   virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE;
     71 
     72   // Overridden from views::View:
     73   virtual void Layout() OVERRIDE;
     74   virtual views::View* GetEventHandlerForPoint(
     75       const gfx::Point& point) OVERRIDE;
     76   virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE;
     77   virtual bool OnMouseDragged(const views::MouseEvent& event) OVERRIDE;
     78   virtual void OnMouseReleased(const views::MouseEvent& event) OVERRIDE;
     79   virtual void OnMouseCaptureLost() OVERRIDE;
     80   virtual void OnMouseMoved(const views::MouseEvent& event) OVERRIDE;
     81   virtual void OnMouseEntered(const views::MouseEvent& event) OVERRIDE;
     82   virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE;
     83 
     84  protected:
     85   virtual void PaintResultViews(gfx::CanvasSkia* canvas);
     86 
     87   // Calculates the height needed to show all the results in the model.
     88   virtual int CalculatePopupHeight();
     89   virtual AutocompleteResultView* CreateResultView(
     90       AutocompleteResultViewModel* model,
     91       int model_index,
     92       const gfx::Font& font,
     93       const gfx::Font& bold_font);
     94 
     95   // Overridden from views::View:
     96   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
     97   // This method should not be triggered directly as we paint our children
     98   // in an un-conventional way inside OnPaint. We use a separate canvas to
     99   // paint the children. Hence we override this method to a no-op so that
    100   // the view hierarchy does not "accidentally" trigger this.
    101   virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE;
    102 
    103   scoped_ptr<AutocompletePopupModel> model_;
    104 
    105   // The "Opt-in to Instant" promo view, if there is one.
    106   views::View* opt_in_view_;
    107 
    108  private:
    109 #if defined(OS_WIN)
    110   typedef AutocompletePopupWin AutocompletePopupClass;
    111 #else
    112   typedef AutocompletePopupGtk AutocompletePopupClass;
    113 #endif
    114   class InstantOptInView;
    115 
    116   // Returns true if the model has a match at the specified index.
    117   bool HasMatchAt(size_t index) const;
    118 
    119   // Returns the match at the specified index within the popup model.
    120   const AutocompleteMatch& GetMatchAtIndex(size_t index) const;
    121 
    122   // Fill a path for the contents' roundrect. |bounding_rect| is the rect that
    123   // bounds the path.
    124   void MakeContentsPath(gfx::Path* path, const gfx::Rect& bounding_rect);
    125 
    126   // Updates the window's blur region for the current size.
    127   void UpdateBlurRegion();
    128 
    129   // Makes the contents of the canvas slightly transparent.
    130   void MakeCanvasTransparent(gfx::Canvas* canvas);
    131 
    132   // Called when the line at the specified index should be opened with the
    133   // provided disposition.
    134   void OpenIndex(size_t index, WindowOpenDisposition disposition);
    135 
    136   // Find the index of the match under the given |point|, specified in window
    137   // coordinates. Returns AutocompletePopupModel::kNoMatch if there isn't a
    138   // match at the specified point.
    139   size_t GetIndexForPoint(const gfx::Point& point);
    140 
    141   // Returns the target bounds given the specified content height.
    142   gfx::Rect CalculateTargetBounds(int h);
    143 
    144   // Invoked if the user clicks on one of the opt-in buttons. Removes the opt-in
    145   // view.
    146   void UserPressedOptIn(bool opt_in);
    147 
    148   // The popup that contains this view.  We create this, but it deletes itself
    149   // when its window is destroyed.  This is a WeakPtr because it's possible for
    150   // the OS to destroy the window and thus delete this object before we're
    151   // deleted, or without our knowledge.
    152   base::WeakPtr<AutocompletePopupClass> popup_;
    153 
    154   // The edit view that invokes us.
    155   AutocompleteEditView* edit_view_;
    156 
    157   // An object that the popup positions itself against.
    158   const views::View* location_bar_;
    159 
    160   // Our border, which can compute our desired bounds.
    161   const BubbleBorder* bubble_border_;
    162 
    163   // The font that we should use for result rows. This is based on the font used
    164   // by the edit that created us.
    165   gfx::Font result_font_;
    166 
    167   // The font used for portions that match the input.
    168   gfx::Font result_bold_font_;
    169 
    170   // If the user cancels a dragging action (i.e. by pressing ESC), we don't have
    171   // a convenient way to release mouse capture. Instead we use this flag to
    172   // simply ignore all remaining drag events, and the eventual mouse release
    173   // event. Since OnDragCanceled() can be called when we're not dragging, this
    174   // flag is reset to false on a mouse pressed event, to make sure we don't
    175   // erroneously ignore the next drag.
    176   bool ignore_mouse_drag_;
    177 
    178   // The popup sizes vertically using an animation when the popup is getting
    179   // shorter (not larger, that makes it look "slow").
    180   ui::SlideAnimation size_animation_;
    181   gfx::Rect start_bounds_;
    182   gfx::Rect target_bounds_;
    183 
    184   DISALLOW_COPY_AND_ASSIGN(AutocompletePopupContentsView);
    185 };
    186 
    187 #endif  // CHROME_BROWSER_UI_VIEWS_AUTOCOMPLETE_AUTOCOMPLETE_POPUP_CONTENTS_VIEW_H_
    188