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/notification_button.h"
6
7#include "ui/gfx/canvas.h"
8#include "ui/message_center/message_center_style.h"
9#include "ui/message_center/views/constants.h"
10#include "ui/views/background.h"
11#include "ui/views/border.h"
12#include "ui/views/controls/image_view.h"
13#include "ui/views/controls/label.h"
14#include "ui/views/layout/box_layout.h"
15#include "ui/views/painter.h"
16
17namespace message_center {
18
19NotificationButton::NotificationButton(views::ButtonListener* listener)
20    : views::CustomButton(listener),
21      icon_(NULL),
22      title_(NULL),
23      focus_painter_(views::Painter::CreateSolidFocusPainter(
24          message_center::kFocusBorderColor,
25          gfx::Insets(1, 2, 2, 2))) {
26  SetFocusable(true);
27  set_request_focus_on_press(false);
28  set_notify_enter_exit_on_child(true);
29  SetLayoutManager(
30      new views::BoxLayout(views::BoxLayout::kHorizontal,
31                           message_center::kButtonHorizontalPadding,
32                           kButtonVecticalPadding,
33                           message_center::kButtonIconToTitlePadding));
34}
35
36NotificationButton::~NotificationButton() {
37}
38
39void NotificationButton::SetIcon(const gfx::ImageSkia& image) {
40  if (icon_ != NULL)
41    delete icon_;  // This removes the icon from this view's children.
42  if (image.isNull()) {
43    icon_ = NULL;
44  } else {
45    icon_ = new views::ImageView();
46    icon_->SetImageSize(gfx::Size(message_center::kNotificationButtonIconSize,
47                                  message_center::kNotificationButtonIconSize));
48    icon_->SetImage(image);
49    icon_->SetHorizontalAlignment(views::ImageView::LEADING);
50    icon_->SetVerticalAlignment(views::ImageView::LEADING);
51    icon_->set_border(views::Border::CreateEmptyBorder(
52        message_center::kButtonIconTopPadding, 0, 0, 0));
53    AddChildViewAt(icon_, 0);
54  }
55}
56
57void NotificationButton::SetTitle(const string16& title) {
58  if (title_ != NULL)
59    delete title_;  // This removes the title from this view's children.
60  if (title.empty()) {
61    title_ = NULL;
62  } else {
63    title_ = new views::Label(title);
64    title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
65    title_->SetEnabledColor(message_center::kRegularTextColor);
66    title_->SetBackgroundColor(kRegularTextBackgroundColor);
67    title_->set_border(views::Border::CreateEmptyBorder(
68        kButtonTitleTopPadding, 0, 0, 0));
69    AddChildView(title_);
70  }
71  SetAccessibleName(title);
72}
73
74gfx::Size NotificationButton::GetPreferredSize() {
75  return gfx::Size(message_center::kNotificationWidth,
76                   message_center::kButtonHeight);
77}
78
79int NotificationButton::GetHeightForWidth(int width) {
80  return message_center::kButtonHeight;
81}
82
83void NotificationButton::OnPaint(gfx::Canvas* canvas) {
84  CustomButton::OnPaint(canvas);
85  views::Painter::PaintFocusPainter(this, canvas, focus_painter_.get());
86}
87
88void NotificationButton::OnFocus() {
89  views::CustomButton::OnFocus();
90  ScrollRectToVisible(GetLocalBounds());
91  // We render differently when focused.
92  SchedulePaint();
93 }
94
95void NotificationButton::OnBlur() {
96  views::CustomButton::OnBlur();
97  // We render differently when focused.
98  SchedulePaint();
99}
100
101void NotificationButton::StateChanged() {
102  if (state() == STATE_HOVERED || state() == STATE_PRESSED) {
103    set_background(views::Background::CreateSolidBackground(
104        message_center::kHoveredButtonBackgroundColor));
105  } else {
106    set_background(NULL);
107  }
108}
109
110}  // namespace message_center
111