Home | History | Annotate | Download | only in views
      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     if (!overlay_image_.isNull())
     70       canvas->DrawImageInt(overlay_image_, position.x(), position.y());
     71   }
     72   views::Painter::PaintFocusPainter(this, canvas, focus_painter());
     73 }
     74 
     75 void PaddedButton::OnFocus() {
     76   views::ImageButton::OnFocus();
     77   ScrollRectToVisible(GetLocalBounds());
     78 }
     79 
     80 gfx::Point PaddedButton::ComputePaddedImagePaintPosition(
     81     const gfx::ImageSkia& image) {
     82   gfx::Vector2d offset;
     83   gfx::Rect bounds = GetContentsBounds();
     84   bounds.Inset(padding_);
     85 
     86   if (padding_.left() == 0 && padding_.right() == 0)
     87     offset.set_x((bounds.width() - image.width()) / 2);  // Center align.
     88   else if (padding_.right() > 0)
     89     offset.set_x(bounds.width() - image.width());  // Right align.
     90 
     91   if (padding_.top() == 0 && padding_.bottom() == 0)
     92     offset.set_y((bounds.height() - image.height()) / 2);  // Middle align.
     93   else if (padding_.bottom() > 0)
     94     offset.set_y(bounds.height() - image.height());  // Bottom align.
     95 
     96   return bounds.origin() + offset;
     97 }
     98 
     99 }  // namespace message_center
    100