1// Copyright (c) 2011 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 "chrome/browser/chromeos/login/shutdown_button.h"
6
7#include "base/utf_string_conversions.h"
8#include "chrome/browser/chromeos/cros/cros_library.h"
9#include "chrome/browser/chromeos/cros/power_library.h"
10#include "chrome/browser/chromeos/login/rounded_rect_painter.h"
11#include "chrome/browser/chromeos/view_ids.h"
12#include "grit/generated_resources.h"
13#include "grit/theme_resources.h"
14#include "ui/base/l10n/l10n_util.h"
15#include "ui/base/resource/resource_bundle.h"
16#include "ui/gfx/gtk_util.h"
17#include "views/background.h"
18
19namespace {
20
21// Style parameters for Shutdown button.
22
23// Bottom/Right padding to locale the shutdown button.
24const int kBottomPadding = 12;
25const int kRightPadding = 12;
26
27// Normal/Hover colors.
28const SkColor kShutdownButtonColor = 0xFF303845;
29const SkColor kShutdownHoverColor = 0xFF353E4E;
30
31// Padding inside button.
32const int kVerticalPadding = 13;
33const int kIconTextPadding = 10;
34const int kHorizontalPadding = 13;
35
36// Rounded corner radious.
37const int kCornerRadius = 4;
38
39class HoverBackground : public views::Background {
40 public:
41  HoverBackground(views::Background* normal, views::Background* hover)
42      : normal_(normal), hover_(hover) {
43  }
44
45  // views::Background implementation.
46  virtual void Paint(gfx::Canvas* canvas, views::View* view) const {
47    views::TextButton* button = static_cast<views::TextButton*>(view);
48    if (button->state() == views::CustomButton::BS_HOT) {
49      hover_->Paint(canvas, view);
50    } else {
51      normal_->Paint(canvas, view);
52    }
53  }
54
55 private:
56  views::Background* normal_;
57  views::Background* hover_;
58
59  DISALLOW_COPY_AND_ASSIGN(HoverBackground);
60};
61
62}  // namespace
63
64namespace chromeos {
65
66ShutdownButton::ShutdownButton()
67    : ALLOW_THIS_IN_INITIALIZER_LIST(TextButton(this, std::wstring())) {
68}
69
70void ShutdownButton::Init() {
71  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
72  SetIcon(*rb.GetBitmapNamed(IDR_SHUTDOWN_ICON));
73  set_icon_text_spacing(kIconTextPadding);
74  SetFocusable(true);
75  SetID(VIEW_ID_SCREEN_LOCKER_SHUTDOWN);
76  // Set label colors.
77  SetEnabledColor(SK_ColorWHITE);
78  SetDisabledColor(SK_ColorWHITE);
79  SetHighlightColor(SK_ColorWHITE);
80  SetHoverColor(SK_ColorWHITE);
81  // Disable throbbing and make border always visible.
82  SetAnimationDuration(0);
83  SetNormalHasBorder(true);
84  // Setup round shapes.
85  set_background(
86      new HoverBackground(
87          CreateRoundedBackground(
88              kCornerRadius, 0, kShutdownButtonColor, 0),
89          CreateRoundedBackground(
90              kCornerRadius, 0, kShutdownHoverColor, 0)));
91  set_border(
92      views::Border::CreateEmptyBorder(kVerticalPadding,
93                                       kHorizontalPadding,
94                                       kVerticalPadding,
95                                       kHorizontalPadding));
96  OnLocaleChanged();  // set text
97}
98
99void ShutdownButton::LayoutIn(views::View* parent) {
100  // No RTL for now. RTL will be handled in new WebUI based Login/Locker.
101  gfx::Size button_size = GetPreferredSize();
102  SetBounds(
103      parent->width() - button_size.width()- kRightPadding,
104      parent->height() - button_size.height() - kBottomPadding,
105      button_size.width(),
106      button_size.height());
107}
108
109void ShutdownButton::OnLocaleChanged() {
110  SetText(UTF8ToWide(l10n_util::GetStringUTF8(IDS_SHUTDOWN_BUTTON)));
111  if (parent()) {
112    parent()->Layout();
113    parent()->SchedulePaint();
114  }
115}
116
117gfx::NativeCursor ShutdownButton::GetCursorForPoint(
118    ui::EventType event_type,
119    const gfx::Point& p) {
120  if (!IsEnabled()) {
121    return NULL;
122  }
123  return gfx::GetCursor(GDK_HAND2);
124}
125
126void ShutdownButton::ButtonPressed(views::Button* sender,
127                                   const views::Event& event) {
128  DCHECK(CrosLibrary::Get()->EnsureLoaded());
129  CrosLibrary::Get()->GetPowerLibrary()->RequestShutdown();
130}
131
132}  // namespace chromeos
133