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/padded_button.h"
7 #include "ui/base/resource/resource_bundle.h"
8 #include "ui/gfx/canvas.h"
9 #include "ui/message_center/message_center_style.h"
10 #include "ui/views/controls/button/image_button.h"
11 #include "ui/views/painter.h"
13 namespace message_center
{
15 PaddedButton::PaddedButton(views::ButtonListener
* listener
)
16 : views::ImageButton(listener
) {
18 set_request_focus_on_press(false);
19 SetFocusPainter(views::Painter::CreateSolidFocusPainter(
21 gfx::Insets(1, 2, 2, 2)));
24 PaddedButton::~PaddedButton() {
27 void PaddedButton::SetPadding(int horizontal_padding
, int vertical_padding
) {
28 padding_
.Set(std::max(vertical_padding
, 0),
29 std::max(horizontal_padding
, 0),
30 std::max(-vertical_padding
, 0),
31 std::max(-horizontal_padding
, 0));
34 void PaddedButton::SetNormalImage(int resource_id
) {
35 SetImage(views::CustomButton::STATE_NORMAL
,
36 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
40 void PaddedButton::SetHoveredImage(int resource_id
) {
41 SetImage(views::CustomButton::STATE_HOVERED
,
42 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
46 void PaddedButton::SetPressedImage(int resource_id
) {
47 SetImage(views::CustomButton::STATE_PRESSED
,
48 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
52 gfx::Size
PaddedButton::GetPreferredSize() const {
53 return gfx::Size(message_center::kControlButtonSize
,
54 message_center::kControlButtonSize
);
57 void PaddedButton::OnPaint(gfx::Canvas
* canvas
) {
58 // This is the same implementation as ImageButton::OnPaint except
59 // that it calls ComputePaddedImagePaintPosition() instead of
60 // ComputeImagePaintPosition(), in effect overriding that private method.
61 View::OnPaint(canvas
);
62 gfx::ImageSkia image
= GetImageToPaint();
63 if (!image
.isNull()) {
64 gfx::Point position
= ComputePaddedImagePaintPosition(image
);
65 if (!background_image_
.isNull())
66 canvas
->DrawImageInt(background_image_
, position
.x(), position
.y());
67 canvas
->DrawImageInt(image
, position
.x(), position
.y());
69 views::Painter::PaintFocusPainter(this, canvas
, focus_painter());
72 void PaddedButton::OnFocus() {
73 views::ImageButton::OnFocus();
74 ScrollRectToVisible(GetLocalBounds());
77 gfx::Point
PaddedButton::ComputePaddedImagePaintPosition(
78 const gfx::ImageSkia
& image
) {
80 gfx::Rect bounds
= GetContentsBounds();
81 bounds
.Inset(padding_
);
83 if (padding_
.left() == 0 && padding_
.right() == 0)
84 offset
.set_x((bounds
.width() - image
.width()) / 2); // Center align.
85 else if (padding_
.right() > 0)
86 offset
.set_x(bounds
.width() - image
.width()); // Right align.
88 if (padding_
.top() == 0 && padding_
.bottom() == 0)
89 offset
.set_y((bounds
.height() - image
.height()) / 2); // Middle align.
90 else if (padding_
.bottom() > 0)
91 offset
.set_y(bounds
.height() - image
.height()); // Bottom align.
93 return bounds
.origin() + offset
;
96 } // namespace message_center