Home | History | Annotate | Download | only in infobars
      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_INFOBARS_INFOBAR_VIEW_H_
      6 #define CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_VIEW_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/compiler_specific.h"
     10 #include "components/infobars/core/infobar.h"
     11 #include "components/infobars/core/infobar_container.h"
     12 #include "third_party/skia/include/core/SkPath.h"
     13 #include "ui/views/controls/button/button.h"
     14 #include "ui/views/controls/menu/menu_types.h"
     15 #include "ui/views/focus/external_focus_tracker.h"
     16 
     17 namespace ui {
     18 class MenuModel;
     19 }
     20 
     21 namespace views {
     22 class ImageButton;
     23 class ImageView;
     24 class Label;
     25 class LabelButton;
     26 class Link;
     27 class LinkListener;
     28 class MenuButton;
     29 class MenuButtonListener;
     30 class MenuRunner;
     31 }  // namespace views
     32 
     33 class InfoBarView : public infobars::InfoBar,
     34                     public views::View,
     35                     public views::ButtonListener,
     36                     public views::ExternalFocusTracker {
     37  public:
     38   explicit InfoBarView(scoped_ptr<infobars::InfoBarDelegate> delegate);
     39 
     40   const SkPath& fill_path() const { return fill_path_; }
     41   const SkPath& stroke_path() const { return stroke_path_; }
     42 
     43  protected:
     44   typedef std::vector<views::Label*> Labels;
     45 
     46   static const int kButtonButtonSpacing;
     47   static const int kEndOfLabelSpacing;
     48 
     49   virtual ~InfoBarView();
     50 
     51   // Creates a label with the appropriate font and color for an infobar.
     52   views::Label* CreateLabel(const base::string16& text) const;
     53 
     54   // Creates a link with the appropriate font and color for an infobar.
     55   // NOTE: Subclasses must ignore link clicks if we're unowned.
     56   views::Link* CreateLink(const base::string16& text,
     57                           views::LinkListener* listener) const;
     58 
     59   // Creates a menu button with an infobar-specific appearance.
     60   // NOTE: Subclasses must ignore button presses if we're unowned.
     61   static views::MenuButton* CreateMenuButton(
     62       const base::string16& text,
     63       views::MenuButtonListener* menu_button_listener);
     64 
     65   // Creates a button with an infobar-specific appearance.
     66   // NOTE: Subclasses must ignore button presses if we're unowned.
     67   static views::LabelButton* CreateLabelButton(views::ButtonListener* listener,
     68                                                const base::string16& text);
     69 
     70   // Given |labels| and the total |available_width| to display them in, sets
     71   // each label's size so that the longest label shrinks until it reaches the
     72   // length of the next-longest label, then both shrink until reaching the
     73   // length of the next-longest, and so forth.
     74   static void AssignWidths(Labels* labels, int available_width);
     75 
     76   // views::View:
     77   virtual void Layout() OVERRIDE;
     78   virtual void ViewHierarchyChanged(
     79       const ViewHierarchyChangedDetails& details) OVERRIDE;
     80 
     81   // views::ButtonListener:
     82   // NOTE: This must not be called if we're unowned.  (Subclasses should ignore
     83   // calls to ButtonPressed() in this case.)
     84   virtual void ButtonPressed(views::Button* sender,
     85                              const ui::Event& event) OVERRIDE;
     86 
     87   // Returns the minimum width the content (that is, everything between the icon
     88   // and the close button) can be shrunk to.  This is used to prevent the close
     89   // button from overlapping views that cannot be shrunk any further.
     90   virtual int ContentMinimumWidth() const;
     91 
     92   // These return x coordinates delimiting the usable area for subclasses to lay
     93   // out their controls.
     94   int StartX() const;
     95   int EndX() const;
     96 
     97   // Given a |view|, returns the centered y position within us, taking into
     98   // account animation so the control "slides in" (or out) as we animate open
     99   // and closed.
    100   int OffsetY(views::View* view) const;
    101 
    102   // Convenience getter.
    103   const infobars::InfoBarContainer::Delegate* container_delegate() const;
    104 
    105   // Shows a menu at the specified position.
    106   // NOTE: This must not be called if we're unowned.  (Subclasses should ignore
    107   // calls to RunMenu() in this case.)
    108   void RunMenuAt(ui::MenuModel* menu_model,
    109                  views::MenuButton* button,
    110                  views::MenuAnchorPosition anchor);
    111 
    112  private:
    113   // Does the actual work for AssignWidths().  Assumes |labels| is sorted by
    114   // decreasing preferred width.
    115   static void AssignWidthsSorted(Labels* labels, int available_width);
    116 
    117   // InfoBar:
    118   virtual void PlatformSpecificShow(bool animate) OVERRIDE;
    119   virtual void PlatformSpecificHide(bool animate) OVERRIDE;
    120   virtual void PlatformSpecificOnHeightsRecalculated() OVERRIDE;
    121 
    122   // views::View:
    123   virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
    124   virtual gfx::Size GetPreferredSize() const OVERRIDE;
    125   virtual void PaintChildren(gfx::Canvas* canvas,
    126                              const views::CullSet& cull_set) OVERRIDE;
    127 
    128   // views::ExternalFocusTracker:
    129   virtual void OnWillChangeFocus(View* focused_before,
    130                                  View* focused_now) OVERRIDE;
    131 
    132   // The optional icon at the left edge of the InfoBar.
    133   views::ImageView* icon_;
    134 
    135   // The close button at the right edge of the InfoBar.
    136   views::ImageButton* close_button_;
    137 
    138   // The paths for the InfoBarBackground to draw, sized according to the heights
    139   // above.
    140   SkPath fill_path_;
    141   SkPath stroke_path_;
    142 
    143   // Used to run the menu.
    144   scoped_ptr<views::MenuRunner> menu_runner_;
    145 
    146   DISALLOW_COPY_AND_ASSIGN(InfoBarView);
    147 };
    148 
    149 #endif  // CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_VIEW_H_
    150