diff options
Diffstat (limited to 'chrome/browser/views/infobars/infobars.cc')
-rw-r--r-- | chrome/browser/views/infobars/infobars.cc | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/chrome/browser/views/infobars/infobars.cc b/chrome/browser/views/infobars/infobars.cc index 52fcf55..475b7ea 100644 --- a/chrome/browser/views/infobars/infobars.cc +++ b/chrome/browser/views/infobars/infobars.cc @@ -5,6 +5,7 @@ #include "chrome/browser/views/infobars/infobars.h" #include "chrome/app/theme/theme_resources.h" +#include "chrome/browser/views/event_utils.h" #include "chrome/browser/views/infobars/infobar_container.h" #include "chrome/common/l10n_util.h" #include "chrome/common/resource_bundle.h" @@ -24,6 +25,7 @@ static const int kVerticalPadding = 3; static const int kHorizontalPadding = 3; static const int kIconLabelSpacing = 5; static const int kButtonSpacing = 5; +static const int kWordSpacing = 2; static const SkColor kBackgroundColorTop = SkColorSetRGB(255, 242, 183); static const SkColor kBackgroundColorBottom = SkColorSetRGB(250, 230, 145); @@ -262,6 +264,106 @@ AlertInfoBarDelegate* AlertInfoBar::GetDelegate() { return delegate()->AsAlertInfoBarDelegate(); } +// LinkInfoBar, public: -------------------------------------------------------- + +LinkInfoBar::LinkInfoBar(LinkInfoBarDelegate* delegate) + : icon_(new views::ImageView), + label_1_(new views::Label), + label_2_(new views::Label), + link_(new views::Link), + InfoBar(delegate) { + // Set up the icon. + if (delegate->GetIcon()) + icon_->SetImage(delegate->GetIcon()); + AddChildView(icon_); + + // Set up the labels. + size_t offset; + std::wstring message_text = delegate->GetMessageTextWithOffset(&offset); + if (offset != std::wstring::npos) { + label_1_->SetText(message_text.substr(0, offset)); + label_2_->SetText(message_text.substr(offset)); + } else { + label_1_->SetText(message_text); + } + ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + label_1_->SetFont(rb.GetFont(ResourceBundle::MediumFont)); + label_2_->SetFont(rb.GetFont(ResourceBundle::MediumFont)); + label_1_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); + label_2_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); + AddChildView(label_1_); + AddChildView(label_2_); + + // Set up the link. + link_->SetText(delegate->GetLinkText()); + link_->SetFont(rb.GetFont(ResourceBundle::MediumFont)); + link_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); + link_->SetController(this); + AddChildView(link_); +} + +LinkInfoBar::~LinkInfoBar() { +} + +// LinkInfoBar, views::LinkController implementation: -------------------------- + +void LinkInfoBar::LinkActivated(views::Link* source, int event_flags) { + DCHECK(source == link_); + if (GetDelegate()->LinkClicked( + event_utils::DispositionFromEventFlags(event_flags))) { + RemoveInfoBar(); + } +} + +// LinkInfoBar, views::View overrides: ----------------------------------------- + +void LinkInfoBar::Layout() { + // Layout the close button. + InfoBar::Layout(); + + // Layout the icon. + gfx::Size icon_ps = icon_->GetPreferredSize(); + icon_->SetBounds(kHorizontalPadding, OffsetY(this, icon_ps), icon_ps.width(), + icon_ps.height()); + + int label_1_x = icon_->bounds().right() + kIconLabelSpacing; + + // Figure out the amount of space available to the rest of the content now + // that the close button and the icon have been positioned. + int available_width = GetAvailableWidth() - label_1_x; + + // Layout the left label. + gfx::Size label_1_ps = label_1_->GetPreferredSize(); + label_1_->SetBounds(label_1_x, OffsetY(this, label_1_ps), label_1_ps.width(), + label_1_ps.height()); + + // Layout the link. + gfx::Size link_ps = link_->GetPreferredSize(); + bool has_second_label = !label_2_->GetText().empty(); + if (has_second_label) { + // Embed the link in the text string between the two labels. + link_->SetBounds(label_1_->bounds().right() + kWordSpacing, + OffsetY(this, link_ps), link_ps.width(), link_ps.height()); + } else { + // Right-align the link toward the edge of the InfoBar. + link_->SetBounds(label_1_x + available_width - link_ps.width(), + OffsetY(this, link_ps), link_ps.width(), link_ps.height()); + } + + // Layout the right label (we do this regardless of whether or not it has + // text). + gfx::Size label_2_ps = label_2_->GetPreferredSize(); + label_2_->SetBounds(link_->bounds().right() + kWordSpacing, + OffsetY(this, label_2_ps), label_2_ps.width(), + label_2_ps.height()); +} + +// LinkInfoBar, private: ------------------------------------------------------- + +LinkInfoBarDelegate* LinkInfoBar::GetDelegate() { + return delegate()->AsLinkInfoBarDelegate(); +} + // ConfirmInfoBar, public: ----------------------------------------------------- ConfirmInfoBar::ConfirmInfoBar(ConfirmInfoBarDelegate* delegate) @@ -361,6 +463,12 @@ InfoBar* AlertInfoBarDelegate::CreateInfoBar() { return new AlertInfoBar(this); } +// LinkInfoBarDelegate, InfoBarDelegate overrides: ----------------------------- + +InfoBar* LinkInfoBarDelegate::CreateInfoBar() { + return new LinkInfoBar(this); +} + // ConfirmInfoBarDelegate, InfoBarDelegate overrides: -------------------------- InfoBar* ConfirmInfoBarDelegate::CreateInfoBar() { |