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 "ui/views/controls/button/button.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "ui/accessibility/ax_view_state.h"
9
10namespace views {
11
12////////////////////////////////////////////////////////////////////////////////
13// Button, static public:
14
15// static
16Button::ButtonState Button::GetButtonStateFrom(ui::NativeTheme::State state) {
17  switch (state) {
18    case ui::NativeTheme::kDisabled:  return Button::STATE_DISABLED;
19    case ui::NativeTheme::kHovered:   return Button::STATE_HOVERED;
20    case ui::NativeTheme::kNormal:    return Button::STATE_NORMAL;
21    case ui::NativeTheme::kPressed:   return Button::STATE_PRESSED;
22    case ui::NativeTheme::kNumStates: NOTREACHED();
23  }
24  return Button::STATE_NORMAL;
25}
26
27////////////////////////////////////////////////////////////////////////////////
28// Button, public:
29
30Button::~Button() {
31}
32
33void Button::SetTooltipText(const base::string16& tooltip_text) {
34  tooltip_text_ = tooltip_text;
35  if (accessible_name_.empty())
36    accessible_name_ = tooltip_text_;
37  TooltipTextChanged();
38}
39
40void Button::SetAccessibleName(const base::string16& name) {
41  accessible_name_ = name;
42}
43
44////////////////////////////////////////////////////////////////////////////////
45// Button, View overrides:
46
47bool Button::GetTooltipText(const gfx::Point& p,
48                            base::string16* tooltip) const {
49  if (tooltip_text_.empty())
50    return false;
51
52  *tooltip = tooltip_text_;
53  return true;
54}
55
56void Button::GetAccessibleState(ui::AXViewState* state) {
57  state->role = ui::AX_ROLE_BUTTON;
58  state->name = accessible_name_;
59}
60
61////////////////////////////////////////////////////////////////////////////////
62// Button, protected:
63
64Button::Button(ButtonListener* listener)
65    : listener_(listener),
66      tag_(-1) {
67  SetAccessibilityFocusable(true);
68}
69
70void Button::NotifyClick(const ui::Event& event) {
71  // We can be called when there is no listener, in cases like double clicks on
72  // menu buttons etc.
73  if (listener_)
74    listener_->ButtonPressed(this, event);
75}
76
77}  // namespace views
78