1 // Copyright 2013 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.
5 #include "ui/message_center/views/notification_button.h"
7 #include "ui/gfx/canvas.h"
8 #include "ui/message_center/message_center_style.h"
9 #include "ui/message_center/views/constants.h"
10 #include "ui/views/background.h"
11 #include "ui/views/border.h"
12 #include "ui/views/controls/image_view.h"
13 #include "ui/views/controls/label.h"
14 #include "ui/views/layout/box_layout.h"
15 #include "ui/views/painter.h"
17 namespace message_center
{
19 NotificationButton::NotificationButton(views::ButtonListener
* listener
)
20 : views::CustomButton(listener
),
23 focus_painter_(views::Painter::CreateSolidFocusPainter(
24 message_center::kFocusBorderColor
,
25 gfx::Insets(1, 2, 2, 2))) {
27 set_request_focus_on_press(false);
28 set_notify_enter_exit_on_child(true);
30 new views::BoxLayout(views::BoxLayout::kHorizontal
,
31 message_center::kButtonHorizontalPadding
,
32 kButtonVecticalPadding
,
33 message_center::kButtonIconToTitlePadding
));
36 NotificationButton::~NotificationButton() {
39 void NotificationButton::SetIcon(const gfx::ImageSkia
& image
) {
41 delete icon_
; // This removes the icon from this view's children.
45 icon_
= new views::ImageView();
46 icon_
->SetImageSize(gfx::Size(message_center::kNotificationButtonIconSize
,
47 message_center::kNotificationButtonIconSize
));
48 icon_
->SetImage(image
);
49 icon_
->SetHorizontalAlignment(views::ImageView::LEADING
);
50 icon_
->SetVerticalAlignment(views::ImageView::LEADING
);
51 icon_
->SetBorder(views::Border::CreateEmptyBorder(
52 message_center::kButtonIconTopPadding
, 0, 0, 0));
53 AddChildViewAt(icon_
, 0);
57 void NotificationButton::SetTitle(const base::string16
& title
) {
59 delete title_
; // This removes the title from this view's children.
63 title_
= new views::Label(title
);
64 title_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
65 title_
->SetEnabledColor(message_center::kRegularTextColor
);
66 title_
->SetBackgroundColor(kRegularTextBackgroundColor
);
68 views::Border::CreateEmptyBorder(kButtonTitleTopPadding
, 0, 0, 0));
71 SetAccessibleName(title
);
74 gfx::Size
NotificationButton::GetPreferredSize() {
75 return gfx::Size(message_center::kNotificationWidth
,
76 message_center::kButtonHeight
);
79 int NotificationButton::GetHeightForWidth(int width
) {
80 return message_center::kButtonHeight
;
83 void NotificationButton::OnPaint(gfx::Canvas
* canvas
) {
84 CustomButton::OnPaint(canvas
);
85 views::Painter::PaintFocusPainter(this, canvas
, focus_painter_
.get());
88 void NotificationButton::OnFocus() {
89 views::CustomButton::OnFocus();
90 ScrollRectToVisible(GetLocalBounds());
91 // We render differently when focused.
95 void NotificationButton::OnBlur() {
96 views::CustomButton::OnBlur();
97 // We render differently when focused.
101 void NotificationButton::StateChanged() {
102 if (state() == STATE_HOVERED
|| state() == STATE_PRESSED
) {
103 set_background(views::Background::CreateSolidBackground(
104 message_center::kHoveredButtonBackgroundColor
));
106 set_background(NULL
);
110 } // namespace message_center