This source file includes following definitions.
- SortLabelsByDecreasingWidth
- close_button_
- CreateLabel
- CreateLink
- CreateMenuButton
- CreateLabelButton
- AssignWidths
- Layout
- ViewHierarchyChanged
- PaintChildren
- ButtonPressed
- ContentMinimumWidth
- StartX
- EndX
- OffsetY
- container_delegate
- RunMenuAt
- AssignWidthsSorted
- PlatformSpecificShow
- PlatformSpecificHide
- PlatformSpecificOnHeightsRecalculated
- GetAccessibleState
- GetPreferredSize
- OnWillChangeFocus
#include "chrome/browser/ui/views/infobars/infobar_view.h"
#if defined(OS_WIN)
#include <shellapi.h>
#endif
#include <algorithm>
#include "base/memory/scoped_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/infobars/infobar_delegate.h"
#include "chrome/browser/ui/views/infobars/infobar_background.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "grit/ui_resources.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/label_button_border.h"
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/non_client_view.h"
#if defined(OS_WIN)
#include "base/win/win_util.h"
#include "base/win/windows_version.h"
#include "ui/gfx/icon_util.h"
#include "ui/gfx/win/hwnd_util.h"
#endif
namespace {
bool SortLabelsByDecreasingWidth(views::Label* label_1, views::Label* label_2) {
return label_1->GetPreferredSize().width() >
label_2->GetPreferredSize().width();
}
}
const int InfoBar::kSeparatorLineHeight =
views::NonClientFrameView::kClientEdgeThickness;
const int InfoBar::kDefaultArrowTargetHeight = 9;
const int InfoBar::kMaximumArrowTargetHeight = 24;
const int InfoBar::kDefaultArrowTargetHalfWidth = kDefaultArrowTargetHeight;
const int InfoBar::kMaximumArrowTargetHalfWidth = 14;
const int InfoBar::kDefaultBarTargetHeight = 36;
const int InfoBarView::kButtonButtonSpacing = 10;
const int InfoBarView::kEndOfLabelSpacing = 16;
const int InfoBarView::kHorizontalPadding = 6;
const int InfoBarView::kCloseButtonSpacing = kEndOfLabelSpacing;
InfoBarView::InfoBarView(scoped_ptr<InfoBarDelegate> delegate)
: InfoBar(delegate.Pass()),
views::ExternalFocusTracker(this, NULL),
icon_(NULL),
close_button_(NULL) {
set_owned_by_client();
set_background(new InfoBarBackground(InfoBar::delegate()->GetInfoBarType()));
}
InfoBarView::~InfoBarView() {
DCHECK(!menu_runner_.get());
}
views::Label* InfoBarView::CreateLabel(const base::string16& text) const {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
views::Label* label = new views::Label(
text, rb.GetFontList(ui::ResourceBundle::MediumFont));
label->SizeToPreferredSize();
label->SetBackgroundColor(background()->get_color());
label->SetEnabledColor(SK_ColorBLACK);
label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
return label;
}
views::Link* InfoBarView::CreateLink(const base::string16& text,
views::LinkListener* listener) const {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
views::Link* link = new views::Link(text);
link->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont));
link->SizeToPreferredSize();
link->SetHorizontalAlignment(gfx::ALIGN_LEFT);
link->set_listener(listener);
link->SetBackgroundColor(background()->get_color());
return link;
}
views::MenuButton* InfoBarView::CreateMenuButton(
const base::string16& text,
views::MenuButtonListener* menu_button_listener) {
scoped_ptr<views::TextButtonDefaultBorder> menu_button_border(
new views::TextButtonDefaultBorder());
const int kNormalImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_NORMAL);
menu_button_border->set_normal_painter(
views::Painter::CreateImageGridPainter(kNormalImageSet));
const int kHotImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_HOVER);
menu_button_border->set_hot_painter(
views::Painter::CreateImageGridPainter(kHotImageSet));
const int kPushedImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_PRESSED);
menu_button_border->set_pushed_painter(
views::Painter::CreateImageGridPainter(kPushedImageSet));
views::MenuButton* menu_button = new views::MenuButton(
NULL, text, menu_button_listener, true);
menu_button->SetBorder(menu_button_border.PassAs<views::Border>());
menu_button->set_animate_on_state_change(false);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
menu_button->set_menu_marker(
rb.GetImageNamed(IDR_INFOBARBUTTON_MENU_DROPARROW).ToImageSkia());
menu_button->SetEnabledColor(SK_ColorBLACK);
menu_button->SetHoverColor(SK_ColorBLACK);
menu_button->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont));
menu_button->SizeToPreferredSize();
menu_button->SetFocusable(true);
return menu_button;
}
views::LabelButton* InfoBarView::CreateLabelButton(
views::ButtonListener* listener,
const base::string16& text,
bool needs_elevation) {
scoped_ptr<views::LabelButtonBorder> label_button_border(
new views::LabelButtonBorder(views::Button::STYLE_TEXTBUTTON));
const int kNormalImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_NORMAL);
label_button_border->SetPainter(
false, views::Button::STATE_NORMAL,
views::Painter::CreateImageGridPainter(kNormalImageSet));
const int kHoveredImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_HOVER);
label_button_border->SetPainter(
false, views::Button::STATE_HOVERED,
views::Painter::CreateImageGridPainter(kHoveredImageSet));
const int kPressedImageSet[] = IMAGE_GRID(IDR_INFOBARBUTTON_PRESSED);
label_button_border->SetPainter(
false, views::Button::STATE_PRESSED,
views::Painter::CreateImageGridPainter(kPressedImageSet));
views::LabelButton* label_button = new views::LabelButton(listener, text);
label_button->SetBorder(label_button_border.PassAs<views::Border>());
label_button->set_animate_on_state_change(false);
label_button->SetTextColor(views::Button::STATE_NORMAL, SK_ColorBLACK);
label_button->SetTextColor(views::Button::STATE_HOVERED, SK_ColorBLACK);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
label_button->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont));
#if defined(OS_WIN)
if (needs_elevation &&
(base::win::GetVersion() >= base::win::VERSION_VISTA) &&
base::win::UserAccountControlIsEnabled()) {
SHSTOCKICONINFO icon_info = { sizeof(SHSTOCKICONINFO) };
typedef HRESULT (STDAPICALLTYPE *GetStockIconInfo)(SHSTOCKICONID, UINT,
SHSTOCKICONINFO*);
GetStockIconInfo func = reinterpret_cast<GetStockIconInfo>(
GetProcAddress(GetModuleHandle(L"shell32.dll"), "SHGetStockIconInfo"));
if (SUCCEEDED((*func)(SIID_SHIELD, SHGSI_ICON | SHGSI_SMALLICON,
&icon_info))) {
scoped_ptr<SkBitmap> icon(IconUtil::CreateSkBitmapFromHICON(
icon_info.hIcon, gfx::Size(GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON))));
if (icon.get()) {
label_button->SetImage(views::Button::STATE_NORMAL,
gfx::ImageSkia::CreateFrom1xBitmap(*icon));
}
DestroyIcon(icon_info.hIcon);
}
}
#endif
label_button->SizeToPreferredSize();
label_button->SetFocusable(true);
return label_button;
}
void InfoBarView::AssignWidths(Labels* labels, int available_width) {
std::sort(labels->begin(), labels->end(), SortLabelsByDecreasingWidth);
AssignWidthsSorted(labels, available_width);
}
void InfoBarView::Layout() {
stroke_path_.rewind();
fill_path_.rewind();
const InfoBarContainer::Delegate* delegate = container_delegate();
if (delegate) {
static_cast<InfoBarBackground*>(background())->set_separator_color(
delegate->GetInfoBarSeparatorColor());
int arrow_x;
SkScalar arrow_fill_height =
SkIntToScalar(std::max(arrow_height() - kSeparatorLineHeight, 0));
SkScalar arrow_fill_half_width = SkIntToScalar(arrow_half_width());
SkScalar separator_height = SkIntToScalar(kSeparatorLineHeight);
if (delegate->DrawInfoBarArrows(&arrow_x) && arrow_fill_height) {
stroke_path_.moveTo(
SkIntToScalar(arrow_x) + SK_ScalarHalf - arrow_fill_half_width,
SkIntToScalar(arrow_height()) - (separator_height * SK_ScalarHalf));
stroke_path_.rLineTo(arrow_fill_half_width, -arrow_fill_height);
stroke_path_.rLineTo(arrow_fill_half_width, arrow_fill_height);
fill_path_ = stroke_path_;
fill_path_.offset(0, -separator_height * SK_ScalarHalf);
const SkScalar epsilon = 0.01f;
fill_path_.rLineTo(-epsilon, 0);
fill_path_.rLineTo(0, separator_height);
fill_path_.rLineTo(epsilon - (arrow_fill_half_width * 2), 0);
fill_path_.close();
}
}
if (bar_height()) {
fill_path_.addRect(0.0, SkIntToScalar(arrow_height()),
SkIntToScalar(width()), SkIntToScalar(height() - kSeparatorLineHeight));
}
int start_x = kHorizontalPadding;
if (icon_ != NULL) {
icon_->SetPosition(gfx::Point(start_x, OffsetY(icon_)));
start_x = icon_->bounds().right() + kHorizontalPadding;
}
int content_minimum_width = ContentMinimumWidth();
close_button_->SetPosition(gfx::Point(
std::max(start_x + content_minimum_width +
((content_minimum_width > 0) ? kCloseButtonSpacing : 0),
width() - kHorizontalPadding - close_button_->width()),
OffsetY(close_button_)));
}
void InfoBarView::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
View::ViewHierarchyChanged(details);
if (details.is_add && (details.child == this) && (close_button_ == NULL)) {
gfx::Image image = delegate()->GetIcon();
if (!image.IsEmpty()) {
icon_ = new views::ImageView;
icon_->SetImage(image.ToImageSkia());
icon_->SizeToPreferredSize();
AddChildView(icon_);
}
close_button_ = new views::ImageButton(this);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
close_button_->SetImage(views::CustomButton::STATE_NORMAL,
rb.GetImageNamed(IDR_CLOSE_1).ToImageSkia());
close_button_->SetImage(views::CustomButton::STATE_HOVERED,
rb.GetImageNamed(IDR_CLOSE_1_H).ToImageSkia());
close_button_->SetImage(views::CustomButton::STATE_PRESSED,
rb.GetImageNamed(IDR_CLOSE_1_P).ToImageSkia());
close_button_->SizeToPreferredSize();
close_button_->SetAccessibleName(
l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE));
close_button_->SetFocusable(true);
AddChildView(close_button_);
} else if ((close_button_ != NULL) && (details.parent == this) &&
(details.child != close_button_) && (close_button_->parent() == this) &&
(child_at(child_count() - 1) != close_button_)) {
RemoveChildView(close_button_);
AddChildView(close_button_);
}
const int kMinimumVerticalPadding = 6;
int height = kDefaultBarTargetHeight;
for (int i = 0; i < child_count(); ++i) {
const int child_height = child_at(i)->height();
height = std::max(height, child_height + kMinimumVerticalPadding);
}
SetBarTargetHeight(height);
}
void InfoBarView::PaintChildren(gfx::Canvas* canvas) {
canvas->Save();
DCHECK_EQ(total_height(), height())
<< "Infobar piecewise heights do not match overall height";
canvas->ClipRect(gfx::Rect(0, arrow_height(), width(), bar_height()));
views::View::PaintChildren(canvas);
canvas->Restore();
}
void InfoBarView::ButtonPressed(views::Button* sender,
const ui::Event& event) {
if (!owner())
return;
if (sender == close_button_) {
delegate()->InfoBarDismissed();
RemoveSelf();
}
}
int InfoBarView::ContentMinimumWidth() {
return 0;
}
int InfoBarView::StartX() const {
return std::min(EndX(),
((icon_ != NULL) ? icon_->bounds().right() : 0) + kHorizontalPadding);
}
int InfoBarView::EndX() const {
return close_button_->x() - kCloseButtonSpacing;
}
int InfoBarView::OffsetY(views::View* view) const {
return arrow_height() +
std::max((bar_target_height() - view->height()) / 2, 0) -
(bar_target_height() - bar_height());
}
const InfoBarContainer::Delegate* InfoBarView::container_delegate() const {
const InfoBarContainer* infobar_container = container();
return infobar_container ? infobar_container->delegate() : NULL;
}
void InfoBarView::RunMenuAt(ui::MenuModel* menu_model,
views::MenuButton* button,
views::MenuItemView::AnchorPosition anchor) {
DCHECK(owner());
gfx::Point screen_point;
views::View::ConvertPointToScreen(button, &screen_point);
menu_runner_.reset(new views::MenuRunner(menu_model));
ignore_result(menu_runner_->RunMenuAt(
GetWidget(), button, gfx::Rect(screen_point, button->size()), anchor,
ui::MENU_SOURCE_NONE, views::MenuRunner::HAS_MNEMONICS));
}
void InfoBarView::AssignWidthsSorted(Labels* labels, int available_width) {
if (labels->empty())
return;
gfx::Size back_label_size(labels->back()->GetPreferredSize());
back_label_size.set_width(
std::min(back_label_size.width(),
available_width / static_cast<int>(labels->size())));
labels->back()->SetSize(back_label_size);
labels->pop_back();
AssignWidthsSorted(labels, available_width - back_label_size.width());
}
void InfoBarView::PlatformSpecificShow(bool animate) {
SetFocusManager(GetFocusManager());
NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
}
void InfoBarView::PlatformSpecificHide(bool animate) {
menu_runner_.reset();
SetFocusManager(NULL);
if (!animate)
return;
views::Widget* widget = GetWidget();
if (!widget || widget->IsActive())
FocusLastFocusedExternalView();
}
void InfoBarView::PlatformSpecificOnHeightsRecalculated() {
InvalidateLayout();
}
void InfoBarView::GetAccessibleState(ui::AXViewState* state) {
state->name = l10n_util::GetStringUTF16(
(delegate()->GetInfoBarType() == InfoBarDelegate::WARNING_TYPE) ?
IDS_ACCNAME_INFOBAR_WARNING : IDS_ACCNAME_INFOBAR_PAGE_ACTION);
state->role = ui::AX_ROLE_ALERT;
}
gfx::Size InfoBarView::GetPreferredSize() {
return gfx::Size(
kHorizontalPadding + (icon_ ? (icon_->width() + kHorizontalPadding) : 0) +
ContentMinimumWidth() + kCloseButtonSpacing + close_button_->width() +
kHorizontalPadding,
total_height());
}
void InfoBarView::OnWillChangeFocus(View* focused_before, View* focused_now) {
views::ExternalFocusTracker::OnWillChangeFocus(focused_before, focused_now);
if (focused_before && focused_now && !Contains(focused_before) &&
Contains(focused_now)) {
NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
}
}