OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/views/controls/button/custom_button.h" | 5 #include "ui/views/controls/button/custom_button.h" |
6 | 6 |
7 #include "ui/base/accessibility/accessible_view_state.h" | 7 #include "ui/base/accessibility/accessible_view_state.h" |
8 #include "ui/base/animation/throb_animation.h" | 8 #include "ui/base/animation/throb_animation.h" |
9 #include "ui/base/keycodes/keyboard_codes.h" | 9 #include "ui/base/keycodes/keyboard_codes.h" |
10 #include "ui/gfx/screen.h" | 10 #include "ui/gfx/screen.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 } | 66 } |
67 | 67 |
68 bool CustomButton::IsMouseHovered() const { | 68 bool CustomButton::IsMouseHovered() const { |
69 // If we haven't yet been placed in an onscreen view hierarchy, we can't be | 69 // If we haven't yet been placed in an onscreen view hierarchy, we can't be |
70 // hovered. | 70 // hovered. |
71 if (!GetWidget()) | 71 if (!GetWidget()) |
72 return false; | 72 return false; |
73 | 73 |
74 gfx::Point cursor_pos(gfx::Screen::GetCursorScreenPoint()); | 74 gfx::Point cursor_pos(gfx::Screen::GetCursorScreenPoint()); |
75 ConvertPointToView(NULL, this, &cursor_pos); | 75 ConvertPointToView(NULL, this, &cursor_pos); |
76 return HitTest(cursor_pos); | 76 return HitTest(gfx::Rect(cursor_pos, gfx::Size(0, 0))); |
77 } | 77 } |
78 | 78 |
79 void CustomButton::SetHotTracked(bool is_hot_tracked) { | 79 void CustomButton::SetHotTracked(bool is_hot_tracked) { |
80 if (state_ != BS_DISABLED) | 80 if (state_ != BS_DISABLED) |
81 SetState(is_hot_tracked ? BS_HOT : BS_NORMAL); | 81 SetState(is_hot_tracked ? BS_HOT : BS_NORMAL); |
82 | 82 |
83 if (is_hot_tracked && GetWidget()) { | 83 if (is_hot_tracked && GetWidget()) { |
84 GetWidget()->NotifyAccessibilityEvent( | 84 GetWidget()->NotifyAccessibilityEvent( |
85 this, ui::AccessibilityTypes::EVENT_FOCUS, true); | 85 this, ui::AccessibilityTypes::EVENT_FOCUS, true); |
86 } | 86 } |
(...skipping 15 matching lines...) Expand all Loading... |
102 else | 102 else |
103 SetState(BS_DISABLED); | 103 SetState(BS_DISABLED); |
104 } | 104 } |
105 | 105 |
106 std::string CustomButton::GetClassName() const { | 106 std::string CustomButton::GetClassName() const { |
107 return kViewClassName; | 107 return kViewClassName; |
108 } | 108 } |
109 | 109 |
110 bool CustomButton::OnMousePressed(const MouseEvent& event) { | 110 bool CustomButton::OnMousePressed(const MouseEvent& event) { |
111 if (state_ != BS_DISABLED) { | 111 if (state_ != BS_DISABLED) { |
112 if (ShouldEnterPushedState(event) && HitTest(event.location())) | 112 if (ShouldEnterPushedState(event) && |
| 113 HitTest(gfx::Rect(event.location(), gfx::Size(0, 0)))) |
113 SetState(BS_PUSHED); | 114 SetState(BS_PUSHED); |
114 if (request_focus_on_press_) | 115 if (request_focus_on_press_) |
115 RequestFocus(); | 116 RequestFocus(); |
116 } | 117 } |
117 return true; | 118 return true; |
118 } | 119 } |
119 | 120 |
120 bool CustomButton::OnMouseDragged(const MouseEvent& event) { | 121 bool CustomButton::OnMouseDragged(const MouseEvent& event) { |
121 if (state_ != BS_DISABLED) { | 122 if (state_ != BS_DISABLED) { |
122 if (HitTest(event.location())) | 123 if (HitTest(gfx::Rect(event.location(), gfx::Size(0, 0)))) |
123 SetState(ShouldEnterPushedState(event) ? BS_PUSHED : BS_HOT); | 124 SetState(ShouldEnterPushedState(event) ? BS_PUSHED : BS_HOT); |
124 else | 125 else |
125 SetState(BS_NORMAL); | 126 SetState(BS_NORMAL); |
126 } | 127 } |
127 return true; | 128 return true; |
128 } | 129 } |
129 | 130 |
130 void CustomButton::OnMouseReleased(const MouseEvent& event) { | 131 void CustomButton::OnMouseReleased(const MouseEvent& event) { |
131 if (state_ == BS_DISABLED) | 132 if (state_ == BS_DISABLED) |
132 return; | 133 return; |
133 | 134 |
134 if (!HitTest(event.location())) { | 135 if (!HitTest(gfx::Rect(event.location(), gfx::Size(0, 0)))) { |
135 SetState(BS_NORMAL); | 136 SetState(BS_NORMAL); |
136 return; | 137 return; |
137 } | 138 } |
138 | 139 |
139 SetState(BS_HOT); | 140 SetState(BS_HOT); |
140 if (IsTriggerableEvent(event)) { | 141 if (IsTriggerableEvent(event)) { |
141 NotifyClick(event); | 142 NotifyClick(event); |
142 // NOTE: We may be deleted at this point (by the listener's notification | 143 // NOTE: We may be deleted at this point (by the listener's notification |
143 // handler). | 144 // handler). |
144 } | 145 } |
(...skipping 11 matching lines...) Expand all Loading... |
156 } | 157 } |
157 | 158 |
158 void CustomButton::OnMouseExited(const MouseEvent& event) { | 159 void CustomButton::OnMouseExited(const MouseEvent& event) { |
159 // Starting a drag results in a MouseExited, we need to ignore it. | 160 // Starting a drag results in a MouseExited, we need to ignore it. |
160 if (state_ != BS_DISABLED && !InDrag()) | 161 if (state_ != BS_DISABLED && !InDrag()) |
161 SetState(BS_NORMAL); | 162 SetState(BS_NORMAL); |
162 } | 163 } |
163 | 164 |
164 void CustomButton::OnMouseMoved(const MouseEvent& event) { | 165 void CustomButton::OnMouseMoved(const MouseEvent& event) { |
165 if (state_ != BS_DISABLED) | 166 if (state_ != BS_DISABLED) |
166 SetState(HitTest(event.location()) ? BS_HOT : BS_NORMAL); | 167 SetState(HitTest(gfx::Rect(event.location(), gfx::Size(0, 0))) ? |
| 168 BS_HOT : BS_NORMAL); |
167 } | 169 } |
168 | 170 |
169 bool CustomButton::OnKeyPressed(const KeyEvent& event) { | 171 bool CustomButton::OnKeyPressed(const KeyEvent& event) { |
170 if (state_ == BS_DISABLED) | 172 if (state_ == BS_DISABLED) |
171 return false; | 173 return false; |
172 | 174 |
173 // Space sets button state to pushed. Enter clicks the button. This matches | 175 // Space sets button state to pushed. Enter clicks the button. This matches |
174 // the Windows native behavior of buttons, where Space clicks the button on | 176 // the Windows native behavior of buttons, where Space clicks the button on |
175 // KeyRelease and Enter clicks the button on KeyPressed. | 177 // KeyRelease and Enter clicks the button on KeyPressed. |
176 if (event.key_code() == ui::VKEY_SPACE) { | 178 if (event.key_code() == ui::VKEY_SPACE) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 if (!is_add && state_ != BS_DISABLED) | 304 if (!is_add && state_ != BS_DISABLED) |
303 SetState(BS_NORMAL); | 305 SetState(BS_NORMAL); |
304 } | 306 } |
305 | 307 |
306 void CustomButton::OnBlur() { | 308 void CustomButton::OnBlur() { |
307 if (IsHotTracked()) | 309 if (IsHotTracked()) |
308 SetState(BS_NORMAL); | 310 SetState(BS_NORMAL); |
309 } | 311 } |
310 | 312 |
311 } // namespace views | 313 } // namespace views |
OLD | NEW |