• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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"
12 
13 namespace message_center {
14 
PaddedButton(views::ButtonListener * listener)15 PaddedButton::PaddedButton(views::ButtonListener* listener)
16     : views::ImageButton(listener) {
17   SetFocusable(true);
18   set_request_focus_on_press(false);
19   SetFocusPainter(views::Painter::CreateSolidFocusPainter(
20       kFocusBorderColor,
21       gfx::Insets(1, 2, 2, 2)));
22 }
23 
~PaddedButton()24 PaddedButton::~PaddedButton() {
25 }
26 
SetPadding(int horizontal_padding,int vertical_padding)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));
32 }
33 
SetNormalImage(int resource_id)34 void PaddedButton::SetNormalImage(int resource_id) {
35   SetImage(views::CustomButton::STATE_NORMAL,
36            ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
37                resource_id));
38 }
39 
SetHoveredImage(int resource_id)40 void PaddedButton::SetHoveredImage(int resource_id) {
41   SetImage(views::CustomButton::STATE_HOVERED,
42            ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
43                resource_id));
44 }
45 
SetPressedImage(int resource_id)46 void PaddedButton::SetPressedImage(int resource_id) {
47   SetImage(views::CustomButton::STATE_PRESSED,
48            ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
49                resource_id));
50 }
51 
GetPreferredSize() const52 gfx::Size PaddedButton::GetPreferredSize() const {
53   return gfx::Size(message_center::kControlButtonSize,
54                    message_center::kControlButtonSize);
55 }
56 
OnPaint(gfx::Canvas * canvas)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());
68   }
69   views::Painter::PaintFocusPainter(this, canvas, focus_painter());
70 }
71 
OnFocus()72 void PaddedButton::OnFocus() {
73   views::ImageButton::OnFocus();
74   ScrollRectToVisible(GetLocalBounds());
75 }
76 
ComputePaddedImagePaintPosition(const gfx::ImageSkia & image)77 gfx::Point PaddedButton::ComputePaddedImagePaintPosition(
78     const gfx::ImageSkia& image) {
79   gfx::Vector2d offset;
80   gfx::Rect bounds = GetContentsBounds();
81   bounds.Inset(padding_);
82 
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.
87 
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.
92 
93   return bounds.origin() + offset;
94 }
95 
96 }  // namespace message_center
97