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