infobar_view.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 "chrome/browser/infobars/infobar.h" 11#include "chrome/browser/infobars/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_item_view.h" 15#include "ui/views/focus/external_focus_tracker.h" 16 17namespace ui { 18class MenuModel; 19} 20 21namespace views { 22class ImageButton; 23class ImageView; 24class Label; 25class LabelButton; 26class Link; 27class LinkListener; 28class MenuButton; 29class MenuButtonListener; 30class MenuRunner; 31} // namespace views 32 33class InfoBarView : public InfoBar, 34 public views::View, 35 public views::ButtonListener, 36 public views::ExternalFocusTracker { 37 public: 38 explicit InfoBarView(scoped_ptr<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 bool needs_elevation); 70 71 // Given |labels| and the total |available_width| to display them in, sets 72 // each label's size so that the longest label shrinks until it reaches the 73 // length of the next-longest label, then both shrink until reaching the 74 // length of the next-longest, and so forth. 75 static void AssignWidths(Labels* labels, int available_width); 76 77 // views::View: 78 virtual void Layout() OVERRIDE; 79 virtual void ViewHierarchyChanged( 80 const ViewHierarchyChangedDetails& details) OVERRIDE; 81 82 // views::ButtonListener: 83 // NOTE: This must not be called if we're unowned. (Subclasses should ignore 84 // calls to ButtonPressed() in this case.) 85 virtual void ButtonPressed(views::Button* sender, 86 const ui::Event& event) OVERRIDE; 87 88 // Returns the minimum width the content (that is, everything between the icon 89 // and the close button) can be shrunk to. This is used to prevent the close 90 // button from overlapping views that cannot be shrunk any further. 91 virtual int ContentMinimumWidth(); 92 93 // These return x coordinates delimiting the usable area for subclasses to lay 94 // out their controls. 95 int StartX() const; 96 int EndX() const; 97 98 // Given a |view|, returns the centered y position within us, taking into 99 // account animation so the control "slides in" (or out) as we animate open 100 // and closed. 101 int OffsetY(views::View* view) const; 102 103 // Convenience getter. 104 const InfoBarContainer::Delegate* container_delegate() const; 105 106 // Shows a menu at the specified position. 107 // NOTE: This must not be called if we're unowned. (Subclasses should ignore 108 // calls to RunMenu() in this case.) 109 void RunMenuAt(ui::MenuModel* menu_model, 110 views::MenuButton* button, 111 views::MenuItemView::AnchorPosition anchor); 112 113 private: 114 static const int kHorizontalPadding; 115 static const int kCloseButtonSpacing; 116 117 // Does the actual work for AssignWidths(). Assumes |labels| is sorted by 118 // decreasing preferred width. 119 static void AssignWidthsSorted(Labels* labels, int available_width); 120 121 // InfoBar: 122 virtual void PlatformSpecificShow(bool animate) OVERRIDE; 123 virtual void PlatformSpecificHide(bool animate) OVERRIDE; 124 virtual void PlatformSpecificOnHeightsRecalculated() OVERRIDE; 125 126 // views::View: 127 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 128 virtual gfx::Size GetPreferredSize() OVERRIDE; 129 virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE; 130 131 // views::ExternalFocusTracker: 132 virtual void OnWillChangeFocus(View* focused_before, 133 View* focused_now) OVERRIDE; 134 135 // The optional icon at the left edge of the InfoBar. 136 views::ImageView* icon_; 137 138 // The close button at the right edge of the InfoBar. 139 views::ImageButton* close_button_; 140 141 // The paths for the InfoBarBackground to draw, sized according to the heights 142 // above. 143 SkPath fill_path_; 144 SkPath stroke_path_; 145 146 // Used to run the menu. 147 scoped_ptr<views::MenuRunner> menu_runner_; 148 149 DISALLOW_COPY_AND_ASSIGN(InfoBarView); 150}; 151 152#endif // CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_VIEW_H_ 153