Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / message_center / views / padded_button.cc
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.
4
5 #include "ui/message_center/views/padded_button.h"
6
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"
13
14 namespace message_center {
15
16 PaddedButton::PaddedButton(views::ButtonListener* listener)
17     : views::ImageButton(listener) {
18   SetFocusable(true);
19   set_request_focus_on_press(false);
20   SetFocusPainter(views::Painter::CreateSolidFocusPainter(
21       kFocusBorderColor,
22       gfx::Insets(1, 2, 2, 2)));
23 }
24
25 PaddedButton::~PaddedButton() {
26 }
27
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));
33 }
34
35 void PaddedButton::SetNormalImage(int resource_id) {
36   SetImage(views::CustomButton::STATE_NORMAL,
37            ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
38                resource_id));
39 }
40
41 void PaddedButton::SetHoveredImage(int resource_id) {
42   SetImage(views::CustomButton::STATE_HOVERED,
43            ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
44                resource_id));
45 }
46
47 void PaddedButton::SetPressedImage(int resource_id) {
48   SetImage(views::CustomButton::STATE_PRESSED,
49            ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
50                resource_id));
51 }
52
53 gfx::Size PaddedButton::GetPreferredSize() {
54   return gfx::Size(message_center::kControlButtonSize,
55                    message_center::kControlButtonSize);
56 }
57
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());
69   }
70   views::Painter::PaintFocusPainter(this, canvas, focus_painter());
71 }
72
73 void PaddedButton::OnFocus() {
74   views::ImageButton::OnFocus();
75   ScrollRectToVisible(GetLocalBounds());
76 }
77
78 gfx::Point PaddedButton::ComputePaddedImagePaintPosition(
79     const gfx::ImageSkia& image) {
80   gfx::Vector2d offset;
81   gfx::Rect bounds = GetContentsBounds();
82   bounds.Inset(padding_);
83
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.
88
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.
93
94   return bounds.origin() + offset;
95 }
96
97 }  // namespace message_center