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
3 // found in the LICENSE file.
5 #include "ui/views/controls/button/menu_button.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/accessibility/ax_view_state.h"
9 #include "ui/base/dragdrop/drag_drop_types.h"
10 #include "ui/base/l10n/l10n_util.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/base/ui_base_switches_util.h"
13 #include "ui/events/event.h"
14 #include "ui/events/event_constants.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/gfx/screen.h"
18 #include "ui/gfx/text_constants.h"
19 #include "ui/resources/grit/ui_resources.h"
20 #include "ui/strings/grit/ui_strings.h"
21 #include "ui/views/controls/button/button.h"
22 #include "ui/views/controls/button/menu_button_listener.h"
23 #include "ui/views/mouse_constants.h"
24 #include "ui/views/widget/root_view.h"
25 #include "ui/views/widget/widget.h"
27 using base::TimeTicks;
28 using base::TimeDelta;
32 // Default menu offset.
33 static const int kDefaultMenuOffsetX = -2;
34 static const int kDefaultMenuOffsetY = -4;
37 const char MenuButton::kViewClassName[] = "MenuButton";
38 const int MenuButton::kMenuMarkerPaddingLeft = 3;
39 const int MenuButton::kMenuMarkerPaddingRight = -1;
41 ////////////////////////////////////////////////////////////////////////////////
43 // MenuButton::PressedLock
45 ////////////////////////////////////////////////////////////////////////////////
47 MenuButton::PressedLock::PressedLock(MenuButton* menu_button)
48 : menu_button_(menu_button->weak_factory_.GetWeakPtr()) {
49 menu_button_->IncrementPressedLocked();
52 MenuButton::PressedLock::~PressedLock() {
53 if (menu_button_.get())
54 menu_button_->DecrementPressedLocked();
57 ////////////////////////////////////////////////////////////////////////////////
59 // MenuButton - constructors, destructors, initialization
61 ////////////////////////////////////////////////////////////////////////////////
63 MenuButton::MenuButton(ButtonListener* listener,
64 const base::string16& text,
65 MenuButtonListener* menu_button_listener,
66 bool show_menu_marker)
67 : LabelButton(listener, text),
68 menu_offset_(kDefaultMenuOffsetX, kDefaultMenuOffsetY),
69 listener_(menu_button_listener),
70 show_menu_marker_(show_menu_marker),
71 menu_marker_(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
72 IDR_MENU_DROPARROW).ToImageSkia()),
73 destroyed_flag_(NULL),
74 pressed_lock_count_(0),
76 SetHorizontalAlignment(gfx::ALIGN_LEFT);
79 MenuButton::~MenuButton() {
81 *destroyed_flag_ = true;
84 ////////////////////////////////////////////////////////////////////////////////
86 // MenuButton - Public APIs
88 ////////////////////////////////////////////////////////////////////////////////
90 bool MenuButton::Activate() {
91 SetState(STATE_PRESSED);
93 gfx::Rect lb = GetLocalBounds();
95 // The position of the menu depends on whether or not the locale is
97 gfx::Point menu_position(lb.right(), lb.bottom());
98 if (base::i18n::IsRTL())
99 menu_position.set_x(lb.x());
101 View::ConvertPointToScreen(this, &menu_position);
102 if (base::i18n::IsRTL())
103 menu_position.Offset(-menu_offset_.x(), menu_offset_.y());
105 menu_position.Offset(menu_offset_.x(), menu_offset_.y());
107 int max_x_coordinate = GetMaximumScreenXCoordinate();
108 if (max_x_coordinate && max_x_coordinate <= menu_position.x())
109 menu_position.set_x(max_x_coordinate - 1);
111 // We're about to show the menu from a mouse press. By showing from the
112 // mouse press event we block RootView in mouse dispatching. This also
113 // appears to cause RootView to get a mouse pressed BEFORE the mouse
114 // release is seen, which means RootView sends us another mouse press no
115 // matter where the user pressed. To force RootView to recalculate the
116 // mouse target during the mouse press we explicitly set the mouse handler
118 static_cast<internal::RootView*>(GetWidget()->GetRootView())->
119 SetMouseHandler(NULL);
121 bool destroyed = false;
122 destroyed_flag_ = &destroyed;
124 // We don't set our state here. It's handled in the MenuController code or
125 // by our click listener.
127 listener_->OnMenuButtonClicked(this, menu_position);
130 // The menu was deleted while showing. Don't attempt any processing.
134 destroyed_flag_ = NULL;
136 menu_closed_time_ = TimeTicks::Now();
138 // We must return false here so that the RootView does not get stuck
139 // sending all mouse pressed events to us instead of the appropriate
146 void MenuButton::OnPaint(gfx::Canvas* canvas) {
147 LabelButton::OnPaint(canvas);
149 if (show_menu_marker_)
150 PaintMenuMarker(canvas);
153 ////////////////////////////////////////////////////////////////////////////////
155 // MenuButton - Events
157 ////////////////////////////////////////////////////////////////////////////////
159 gfx::Size MenuButton::GetPreferredSize() const {
160 gfx::Size prefsize = LabelButton::GetPreferredSize();
161 if (show_menu_marker_) {
162 prefsize.Enlarge(menu_marker_->width() + kMenuMarkerPaddingLeft +
163 kMenuMarkerPaddingRight,
169 const char* MenuButton::GetClassName() const {
170 return kViewClassName;
173 bool MenuButton::OnMousePressed(const ui::MouseEvent& event) {
175 if (state() != STATE_DISABLED && ShouldEnterPushedState(event) &&
176 HitTestPoint(event.location())) {
177 TimeDelta delta = TimeTicks::Now() - menu_closed_time_;
178 if (delta.InMilliseconds() > kMinimumMsBetweenButtonClicks)
184 void MenuButton::OnMouseReleased(const ui::MouseEvent& event) {
185 if (state() != STATE_DISABLED && ShouldEnterPushedState(event) &&
186 HitTestPoint(event.location()) && !InDrag()) {
189 LabelButton::OnMouseReleased(event);
193 void MenuButton::OnMouseEntered(const ui::MouseEvent& event) {
194 if (pressed_lock_count_ == 0) // Ignore mouse movement if state is locked.
195 LabelButton::OnMouseEntered(event);
198 void MenuButton::OnMouseExited(const ui::MouseEvent& event) {
199 if (pressed_lock_count_ == 0) // Ignore mouse movement if state is locked.
200 LabelButton::OnMouseExited(event);
203 void MenuButton::OnMouseMoved(const ui::MouseEvent& event) {
204 if (pressed_lock_count_ == 0) // Ignore mouse movement if state is locked.
205 LabelButton::OnMouseMoved(event);
208 void MenuButton::OnGestureEvent(ui::GestureEvent* event) {
209 if (state() != STATE_DISABLED) {
210 if (ShouldEnterPushedState(*event) && !Activate()) {
211 // When |Activate()| returns |false|, it means that a menu is shown and
212 // has handled the gesture event. So, there is no need to further process
213 // the gesture event here.
216 if (switches::IsTouchFeedbackEnabled()) {
217 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
219 SetState(Button::STATE_HOVERED);
220 } else if (state() == Button::STATE_HOVERED &&
221 (event->type() == ui::ET_GESTURE_TAP_CANCEL ||
222 event->type() == ui::ET_GESTURE_END)) {
223 SetState(Button::STATE_NORMAL);
227 LabelButton::OnGestureEvent(event);
230 bool MenuButton::OnKeyPressed(const ui::KeyEvent& event) {
231 switch (event.key_code()) {
233 // Alt-space on windows should show the window menu.
234 if (event.IsAltDown())
236 case ui::VKEY_RETURN:
238 case ui::VKEY_DOWN: {
239 // WARNING: we may have been deleted by the time Activate returns.
241 // This is to prevent the keyboard event from being dispatched twice. If
242 // the keyboard event is not handled, we pass it to the default handler
243 // which dispatches the event back to us causing the menu to get displayed
244 // again. Return true to prevent this.
253 bool MenuButton::OnKeyReleased(const ui::KeyEvent& event) {
254 // Override CustomButton's implementation, which presses the button when
255 // you press space and clicks it when you release space. For a MenuButton
256 // we always activate the menu on key press.
260 void MenuButton::GetAccessibleState(ui::AXViewState* state) {
261 CustomButton::GetAccessibleState(state);
262 state->role = ui::AX_ROLE_POP_UP_BUTTON;
263 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS);
264 state->AddStateFlag(ui::AX_STATE_HASPOPUP);
267 void MenuButton::PaintMenuMarker(gfx::Canvas* canvas) {
268 gfx::Insets insets = GetInsets();
270 // Using the Views mirroring infrastructure incorrectly flips icon content.
271 // Instead, manually mirror the position of the down arrow.
272 gfx::Rect arrow_bounds(width() - insets.right() -
273 menu_marker_->width() - kMenuMarkerPaddingRight,
274 height() / 2 - menu_marker_->height() / 2,
275 menu_marker_->width(),
276 menu_marker_->height());
277 arrow_bounds.set_x(GetMirroredXForRect(arrow_bounds));
278 canvas->DrawImageInt(*menu_marker_, arrow_bounds.x(), arrow_bounds.y());
281 gfx::Rect MenuButton::GetChildAreaBounds() {
282 gfx::Size s = size();
284 if (show_menu_marker_) {
285 s.set_width(s.width() - menu_marker_->width() - kMenuMarkerPaddingLeft -
286 kMenuMarkerPaddingRight);
292 bool MenuButton::ShouldEnterPushedState(const ui::Event& event) {
293 if (event.IsMouseEvent()) {
294 const ui::MouseEvent& mouseev = static_cast<const ui::MouseEvent&>(event);
295 // Active on left mouse button only, to prevent a menu from being activated
296 // when a right-click would also activate a context menu.
297 if (!mouseev.IsOnlyLeftMouseButton())
299 // If dragging is supported activate on release, otherwise activate on
301 ui::EventType active_on =
302 GetDragOperations(mouseev.location()) == ui::DragDropTypes::DRAG_NONE
303 ? ui::ET_MOUSE_PRESSED
304 : ui::ET_MOUSE_RELEASED;
305 return event.type() == active_on;
308 return event.type() == ui::ET_GESTURE_TAP;
311 void MenuButton::IncrementPressedLocked() {
312 ++pressed_lock_count_;
313 SetState(STATE_PRESSED);
316 void MenuButton::DecrementPressedLocked() {
317 --pressed_lock_count_;
318 DCHECK_GE(pressed_lock_count_, 0);
320 // If this was the last lock, manually reset state to "normal". We set
321 // "normal" and not "hot" because the likelihood is that the mouse is now
322 // somewhere else (user clicked elsewhere on screen to close the menu or
323 // selected an item) and we will inevitably refresh the hot state in the event
324 // the mouse _is_ over the view.
325 if (pressed_lock_count_ == 0)
326 SetState(STATE_NORMAL);
329 int MenuButton::GetMaximumScreenXCoordinate() {
335 gfx::Rect monitor_bounds = GetWidget()->GetWorkAreaBoundsInScreen();
336 return monitor_bounds.right() - 1;