OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "grit/app_strings.h" | 10 #include "grit/app_strings.h" |
11 #include "ui/base/accessibility/accessible_view_state.h" | 11 #include "ui/base/accessibility/accessible_view_state.h" |
12 #include "ui/base/l10n/l10n_util.h" | 12 #include "ui/base/l10n/l10n_util.h" |
13 #include "views/controls/menu/view_menu_delegate.h" | 13 #include "ui/base/models/menu_model.h" |
| 14 #include "views/controls/menu/menu_item_view.h" |
| 15 #include "views/controls/menu/menu_model_adapter.h" |
14 #include "views/widget/widget.h" | 16 #include "views/widget/widget.h" |
| 17 #include "views/window/window.h" |
15 | 18 |
16 namespace views { | 19 namespace views { |
17 | 20 |
18 // How long to wait before showing the menu | 21 // How long to wait before showing the menu |
19 static const int kMenuTimerDelay = 500; | 22 static const int kMenuTimerDelay = 500; |
20 | 23 |
21 //////////////////////////////////////////////////////////////////////////////// | 24 //////////////////////////////////////////////////////////////////////////////// |
22 // | 25 // |
23 // ButtonDropDown - constructors, destructors, initialization, cleanup | 26 // ButtonDropDown - constructors, destructors, initialization, cleanup |
24 // | 27 // |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 } | 114 } |
112 | 115 |
113 bool ButtonDropDown::ShouldEnterPushedState(const MouseEvent& event) { | 116 bool ButtonDropDown::ShouldEnterPushedState(const MouseEvent& event) { |
114 // Enter PUSHED state on press with Left or Right mouse button. Remain | 117 // Enter PUSHED state on press with Left or Right mouse button. Remain |
115 // in this state while the context menu is open. | 118 // in this state while the context menu is open. |
116 return ((ui::EF_LEFT_BUTTON_DOWN | | 119 return ((ui::EF_LEFT_BUTTON_DOWN | |
117 ui::EF_RIGHT_BUTTON_DOWN) & event.flags()) != 0; | 120 ui::EF_RIGHT_BUTTON_DOWN) & event.flags()) != 0; |
118 } | 121 } |
119 | 122 |
120 void ButtonDropDown::ShowDropDownMenu(gfx::NativeView window) { | 123 void ButtonDropDown::ShowDropDownMenu(gfx::NativeView window) { |
121 if (model_) { | 124 gfx::Rect lb = GetLocalBounds(); |
122 gfx::Rect lb = GetLocalBounds(); | |
123 | 125 |
124 // Both the menu position and the menu anchor type change if the UI layout | 126 // Both the menu position and the menu anchor type change if the UI layout |
125 // is right-to-left. | 127 // is right-to-left. |
126 gfx::Point menu_position(lb.origin()); | 128 gfx::Point menu_position(lb.origin()); |
127 menu_position.Offset(0, lb.height() - 1); | 129 menu_position.Offset(0, lb.height() - 1); |
128 if (base::i18n::IsRTL()) | 130 if (base::i18n::IsRTL()) |
129 menu_position.Offset(lb.width() - 1, 0); | 131 menu_position.Offset(lb.width() - 1, 0); |
130 | 132 |
131 View::ConvertPointToScreen(this, &menu_position); | 133 View::ConvertPointToScreen(this, &menu_position); |
132 | 134 |
133 #if defined(OS_WIN) | 135 #if defined(OS_WIN) |
134 int left_bound = GetSystemMetrics(SM_XVIRTUALSCREEN); | 136 int left_bound = GetSystemMetrics(SM_XVIRTUALSCREEN); |
135 #else | 137 #else |
136 int left_bound = 0; | 138 int left_bound = 0; |
137 NOTIMPLEMENTED(); | 139 NOTIMPLEMENTED(); |
138 #endif | 140 #endif |
139 if (menu_position.x() < left_bound) | 141 if (menu_position.x() < left_bound) |
140 menu_position.set_x(left_bound); | 142 menu_position.set_x(left_bound); |
141 | 143 |
142 // Make the button look depressed while the menu is open. | 144 // Make the button look depressed while the menu is open. |
143 SetState(BS_PUSHED); | 145 SetState(BS_PUSHED); |
144 menu_.reset(new Menu2(model_)); | |
145 menu_->RunMenuAt(menu_position, Menu2::ALIGN_TOPLEFT); | |
146 | 146 |
147 // Need to explicitly clear mouse handler so that events get sent | 147 // Create and run menu. Display an empty menu if model is NULL. |
148 // properly after the menu finishes running. If we don't do this, then | 148 if (model_) { |
149 // the first click to other parts of the UI is eaten. | 149 MenuModelAdapter menu_delegate(model_); |
150 SetMouseHandler(NULL); | 150 MenuItemView menu(&menu_delegate); |
| 151 menu_delegate.BuildMenu(&menu); |
151 | 152 |
152 // Set the state back to normal after the drop down menu is closed. | 153 menu.RunMenuAt(GetWindow()->GetNativeWindow(), NULL, |
153 if (state_ != BS_DISABLED) | 154 gfx::Rect(menu_position, gfx::Size(0, 0)), |
154 SetState(BS_NORMAL); | 155 views::MenuItemView::TOPLEFT, |
| 156 true); |
| 157 } else { |
| 158 MenuDelegate menu_delegate; |
| 159 MenuItemView menu(&menu_delegate); |
| 160 menu.RunMenuAt(GetWindow()->GetNativeWindow(), NULL, |
| 161 gfx::Rect(menu_position, gfx::Size(0, 0)), |
| 162 views::MenuItemView::TOPLEFT, |
| 163 true); |
155 } | 164 } |
| 165 |
| 166 // Need to explicitly clear mouse handler so that events get sent |
| 167 // properly after the menu finishes running. If we don't do this, then |
| 168 // the first click to other parts of the UI is eaten. |
| 169 SetMouseHandler(NULL); |
| 170 |
| 171 // Set the state back to normal after the drop down menu is closed. |
| 172 if (state_ != BS_DISABLED) |
| 173 SetState(BS_NORMAL); |
156 } | 174 } |
157 | 175 |
158 //////////////////////////////////////////////////////////////////////////////// | 176 //////////////////////////////////////////////////////////////////////////////// |
159 // | 177 // |
160 // ButtonDropDown - Accessibility | 178 // ButtonDropDown - Accessibility |
161 // | 179 // |
162 //////////////////////////////////////////////////////////////////////////////// | 180 //////////////////////////////////////////////////////////////////////////////// |
163 | 181 |
164 } // namespace views | 182 } // namespace views |
OLD | NEW |