| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "views/controls/button/custom_button.h" | 5 #include "views/controls/button/custom_button.h" |
| 6 | 6 |
| 7 #include "app/throb_animation.h" | 7 #include "app/throb_animation.h" |
| 8 #include "base/keyboard_codes.h" | 8 #include "base/keyboard_codes.h" |
| 9 #include "views/screen.h" |
| 9 | 10 |
| 10 namespace views { | 11 namespace views { |
| 11 | 12 |
| 12 // How long the hover animation takes if uninterrupted. | 13 // How long the hover animation takes if uninterrupted. |
| 13 static const int kHoverFadeDurationMs = 150; | 14 static const int kHoverFadeDurationMs = 150; |
| 14 | 15 |
| 15 //////////////////////////////////////////////////////////////////////////////// | 16 //////////////////////////////////////////////////////////////////////////////// |
| 16 // CustomButton, public: | 17 // CustomButton, public: |
| 17 | 18 |
| 18 CustomButton::~CustomButton() { | 19 CustomButton::~CustomButton() { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 case BS_NORMAL: | 65 case BS_NORMAL: |
| 65 case BS_COUNT: | 66 case BS_COUNT: |
| 66 // No additional accessibility state set for this button state. | 67 // No additional accessibility state set for this button state. |
| 67 break; | 68 break; |
| 68 } | 69 } |
| 69 | 70 |
| 70 return true; | 71 return true; |
| 71 } | 72 } |
| 72 | 73 |
| 73 void CustomButton::SetEnabled(bool enabled) { | 74 void CustomButton::SetEnabled(bool enabled) { |
| 74 if (enabled ? (state_ == BS_DISABLED) : (state_ != BS_DISABLED)) | 75 if (enabled ? (state_ != BS_DISABLED) : (state_ == BS_DISABLED)) |
| 75 SetState(enabled ? BS_NORMAL : BS_DISABLED); | 76 return; |
| 77 |
| 78 if (enabled) |
| 79 SetState(IsMouseHovered() ? BS_HOT : BS_NORMAL); |
| 80 else |
| 81 SetState(BS_DISABLED); |
| 76 } | 82 } |
| 77 | 83 |
| 78 bool CustomButton::IsEnabled() const { | 84 bool CustomButton::IsEnabled() const { |
| 79 return state_ != BS_DISABLED; | 85 return state_ != BS_DISABLED; |
| 80 } | 86 } |
| 81 | 87 |
| 82 bool CustomButton::IsFocusable() const { | 88 bool CustomButton::IsFocusable() const { |
| 83 return (state_ != BS_DISABLED) && View::IsFocusable(); | 89 return (state_ != BS_DISABLED) && View::IsFocusable(); |
| 84 } | 90 } |
| 85 | 91 |
| 92 bool CustomButton::IsMouseHovered() const { |
| 93 // If we haven't yet been placed in an onscreen view hierarchy, we can't be |
| 94 // hovered. |
| 95 if (!GetWidget()) |
| 96 return false; |
| 97 |
| 98 gfx::Point cursor_pos(Screen::GetCursorScreenPoint()); |
| 99 ConvertPointToView(NULL, this, &cursor_pos); |
| 100 return HitTest(cursor_pos); |
| 101 } |
| 102 |
| 86 //////////////////////////////////////////////////////////////////////////////// | 103 //////////////////////////////////////////////////////////////////////////////// |
| 87 // CustomButton, protected: | 104 // CustomButton, protected: |
| 88 | 105 |
| 89 CustomButton::CustomButton(ButtonListener* listener) | 106 CustomButton::CustomButton(ButtonListener* listener) |
| 90 : Button(listener), | 107 : Button(listener), |
| 91 state_(BS_NORMAL), | 108 state_(BS_NORMAL), |
| 92 animate_on_state_change_(true), | 109 animate_on_state_change_(true), |
| 93 triggerable_event_flags_(MouseEvent::EF_LEFT_BUTTON_DOWN), | 110 triggerable_event_flags_(MouseEvent::EF_LEFT_BUTTON_DOWN), |
| 94 request_focus_on_press_(true) { | 111 request_focus_on_press_(true) { |
| 95 hover_animation_.reset(new ThrobAnimation(this)); | 112 hover_animation_.reset(new ThrobAnimation(this)); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 | 253 |
| 237 void CustomButton::AnimationProgressed(const Animation* animation) { | 254 void CustomButton::AnimationProgressed(const Animation* animation) { |
| 238 SchedulePaint(); | 255 SchedulePaint(); |
| 239 } | 256 } |
| 240 | 257 |
| 241 bool CustomButton::ShouldEnterPushedState(const MouseEvent& e) { | 258 bool CustomButton::ShouldEnterPushedState(const MouseEvent& e) { |
| 242 return IsTriggerableEvent(e); | 259 return IsTriggerableEvent(e); |
| 243 } | 260 } |
| 244 | 261 |
| 245 } // namespace views | 262 } // namespace views |
| OLD | NEW |