| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/button_dropdown.h" | 5 #include "views/controls/button/button_dropdown.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "grit/app_strings.h" | 10 #include "grit/app_strings.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 // determine if the user dragged the mouse down (which should pop up the | 45 // determine if the user dragged the mouse down (which should pop up the |
| 46 // drag down menu immediately, instead of waiting for the timer) | 46 // drag down menu immediately, instead of waiting for the timer) |
| 47 y_position_on_lbuttondown_ = e.y(); | 47 y_position_on_lbuttondown_ = e.y(); |
| 48 | 48 |
| 49 // Schedule a task that will show the menu. | 49 // Schedule a task that will show the menu. |
| 50 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 50 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 51 show_menu_factory_.NewRunnableMethod(&ButtonDropDown::ShowDropDownMenu, | 51 show_menu_factory_.NewRunnableMethod(&ButtonDropDown::ShowDropDownMenu, |
| 52 GetWidget()->GetNativeView()), | 52 GetWidget()->GetNativeView()), |
| 53 kMenuTimerDelay); | 53 kMenuTimerDelay); |
| 54 } | 54 } |
| 55 | |
| 56 return ImageButton::OnMousePressed(e); | 55 return ImageButton::OnMousePressed(e); |
| 57 } | 56 } |
| 58 | 57 |
| 59 void ButtonDropDown::OnMouseReleased(const MouseEvent& e, bool canceled) { | 58 void ButtonDropDown::OnMouseReleased(const MouseEvent& e, bool canceled) { |
| 60 ImageButton::OnMouseReleased(e, canceled); | 59 if (e.IsLeftMouseButton() || (e.IsRightMouseButton() && |
| 60 !HitTest(e.location()))) { |
| 61 ImageButton::OnMouseReleased(e, canceled); |
| 62 } |
| 61 | 63 |
| 62 if (canceled) | 64 if (canceled) |
| 63 return; | 65 return; |
| 64 | 66 |
| 65 if (e.IsLeftMouseButton()) | 67 if (e.IsLeftMouseButton()) |
| 66 show_menu_factory_.RevokeAll(); | 68 show_menu_factory_.RevokeAll(); |
| 67 | 69 |
| 68 if (IsEnabled() && e.IsRightMouseButton() && HitTest(e.location())) { | 70 if (IsEnabled() && e.IsRightMouseButton() && HitTest(e.location())) { |
| 69 show_menu_factory_.RevokeAll(); | 71 show_menu_factory_.RevokeAll(); |
| 70 // Make the button look depressed while the menu is open. | |
| 71 // NOTE: SetState() schedules a paint, but it won't occur until after the | |
| 72 // context menu message loop has terminated, so we PaintNow() to | |
| 73 // update the appearance synchronously. | |
| 74 SetState(BS_PUSHED); | |
| 75 PaintNow(); | |
| 76 ShowDropDownMenu(GetWidget()->GetNativeView()); | 72 ShowDropDownMenu(GetWidget()->GetNativeView()); |
| 73 // Set the state back to normal after the drop down menu is closed. |
| 74 if (state_ != BS_DISABLED) |
| 75 SetState(BS_NORMAL); |
| 77 } | 76 } |
| 78 } | 77 } |
| 79 | 78 |
| 80 bool ButtonDropDown::OnMouseDragged(const MouseEvent& e) { | 79 bool ButtonDropDown::OnMouseDragged(const MouseEvent& e) { |
| 81 bool result = ImageButton::OnMouseDragged(e); | 80 bool result = ImageButton::OnMouseDragged(e); |
| 82 | 81 |
| 83 if (!show_menu_factory_.empty()) { | 82 if (!show_menu_factory_.empty()) { |
| 84 // If the mouse is dragged to a y position lower than where it was when | 83 // If the mouse is dragged to a y position lower than where it was when |
| 85 // clicked then we should not wait for the menu to appear but show | 84 // clicked then we should not wait for the menu to appear but show |
| 86 // it immediately. | 85 // it immediately. |
| 87 if (e.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { | 86 if (e.y() > y_position_on_lbuttondown_ + GetHorizontalDragThreshold()) { |
| 88 show_menu_factory_.RevokeAll(); | 87 show_menu_factory_.RevokeAll(); |
| 89 ShowDropDownMenu(GetWidget()->GetNativeView()); | 88 ShowDropDownMenu(GetWidget()->GetNativeView()); |
| 90 } | 89 } |
| 91 } | 90 } |
| 92 | 91 |
| 93 return result; | 92 return result; |
| 94 } | 93 } |
| 95 | 94 |
| 95 void ButtonDropDown::OnMouseExited(const MouseEvent& e) { |
| 96 // Starting a drag results in a MouseExited, we need to ignore it. |
| 97 // A right click release triggers an exit event. We want to |
| 98 // remain in a PUSHED state until the drop down menu closes. |
| 99 if (state_ != BS_DISABLED && !InDrag() && state_ != BS_PUSHED) |
| 100 SetState(BS_NORMAL); |
| 101 } |
| 102 |
| 96 //////////////////////////////////////////////////////////////////////////////// | 103 //////////////////////////////////////////////////////////////////////////////// |
| 97 // | 104 // |
| 98 // ButtonDropDown - Menu functions | 105 // ButtonDropDown - Menu functions |
| 99 // | 106 // |
| 100 //////////////////////////////////////////////////////////////////////////////// | 107 //////////////////////////////////////////////////////////////////////////////// |
| 101 | 108 |
| 102 void ButtonDropDown::ShowContextMenu(int x, int y, bool is_mouse_gesture) { | 109 void ButtonDropDown::ShowContextMenu(int x, int y, bool is_mouse_gesture) { |
| 103 show_menu_factory_.RevokeAll(); | 110 show_menu_factory_.RevokeAll(); |
| 104 // Make the button look depressed while the menu is open. | 111 // Make the button look depressed while the menu is open. |
| 105 // NOTE: SetState() schedules a paint, but it won't occur until after the | 112 // NOTE: SetState() schedules a paint, but it won't occur until after the |
| 106 // context menu message loop has terminated, so we PaintNow() to | 113 // context menu message loop has terminated, so we PaintNow() to |
| 107 // update the appearance synchronously. | 114 // update the appearance synchronously. |
| 108 SetState(BS_PUSHED); | 115 SetState(BS_PUSHED); |
| 109 PaintNow(); | 116 PaintNow(); |
| 110 ShowDropDownMenu(GetWidget()->GetNativeView()); | 117 ShowDropDownMenu(GetWidget()->GetNativeView()); |
| 111 SetState(BS_HOT); | 118 SetState(BS_HOT); |
| 112 } | 119 } |
| 113 | 120 |
| 121 bool ButtonDropDown::ShouldEnterPushedState(const MouseEvent& e) { |
| 122 // Enter PUSHED state on press with Left or Right mouse button. Remain |
| 123 // in this state while the context menu is open. |
| 124 return ((MouseEvent::EF_LEFT_BUTTON_DOWN | |
| 125 MouseEvent::EF_RIGHT_BUTTON_DOWN) & e.GetFlags()) != 0; |
| 126 } |
| 127 |
| 114 void ButtonDropDown::ShowDropDownMenu(gfx::NativeView window) { | 128 void ButtonDropDown::ShowDropDownMenu(gfx::NativeView window) { |
| 115 if (model_) { | 129 if (model_) { |
| 116 gfx::Rect lb = GetLocalBounds(true); | 130 gfx::Rect lb = GetLocalBounds(true); |
| 117 | 131 |
| 118 // Both the menu position and the menu anchor type change if the UI layout | 132 // Both the menu position and the menu anchor type change if the UI layout |
| 119 // is right-to-left. | 133 // is right-to-left. |
| 120 gfx::Point menu_position(lb.origin()); | 134 gfx::Point menu_position(lb.origin()); |
| 121 menu_position.Offset(0, lb.height() - 1); | 135 menu_position.Offset(0, lb.height() - 1); |
| 122 if (UILayoutIsRightToLeft()) | 136 if (UILayoutIsRightToLeft()) |
| 123 menu_position.Offset(lb.width() - 1, 0); | 137 menu_position.Offset(lb.width() - 1, 0); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 } | 181 } |
| 168 | 182 |
| 169 bool ButtonDropDown::GetAccessibleState(AccessibilityTypes::State* state) { | 183 bool ButtonDropDown::GetAccessibleState(AccessibilityTypes::State* state) { |
| 170 DCHECK(state); | 184 DCHECK(state); |
| 171 | 185 |
| 172 *state = AccessibilityTypes::STATE_HASPOPUP; | 186 *state = AccessibilityTypes::STATE_HASPOPUP; |
| 173 return true; | 187 return true; |
| 174 } | 188 } |
| 175 | 189 |
| 176 } // namespace views | 190 } // namespace views |
| OLD | NEW |