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. 4 5 #include "ui/views/controls/menu/menu_host_root_view.h" 6 7 #include "ui/views/controls/menu/menu_controller.h" 8 #include "ui/views/controls/menu/menu_item_view.h" 9 #include "ui/views/controls/menu/submenu_view.h" 10 11 namespace views { 12 13 MenuHostRootView::MenuHostRootView(Widget* widget, 14 SubmenuView* submenu) 15 : internal::RootView(widget), 16 submenu_(submenu), 17 forward_drag_to_menu_controller_(true) { 18 } 19 20 bool MenuHostRootView::OnMousePressed(const ui::MouseEvent& event) { 21 forward_drag_to_menu_controller_ = 22 !GetLocalBounds().Contains(event.location()) || 23 !RootView::OnMousePressed(event) || 24 DoesEventTargetEmptyMenuItem(event); 25 26 if (forward_drag_to_menu_controller_ && GetMenuController()) 27 GetMenuController()->OnMousePressed(submenu_, event); 28 return true; 29 } 30 31 bool MenuHostRootView::OnMouseDragged(const ui::MouseEvent& event) { 32 if (forward_drag_to_menu_controller_ && GetMenuController()) { 33 GetMenuController()->OnMouseDragged(submenu_, event); 34 return true; 35 } 36 return RootView::OnMouseDragged(event); 37 } 38 39 void MenuHostRootView::OnMouseReleased(const ui::MouseEvent& event) { 40 RootView::OnMouseReleased(event); 41 if (forward_drag_to_menu_controller_ && GetMenuController()) { 42 forward_drag_to_menu_controller_ = false; 43 GetMenuController()->OnMouseReleased(submenu_, event); 44 } 45 } 46 47 void MenuHostRootView::OnMouseMoved(const ui::MouseEvent& event) { 48 RootView::OnMouseMoved(event); 49 if (GetMenuController()) 50 GetMenuController()->OnMouseMoved(submenu_, event); 51 } 52 53 bool MenuHostRootView::OnMouseWheel(const ui::MouseWheelEvent& event) { 54 #if defined(USE_AURA) 55 // Aura uses MenuController to forward events like other mouse events. 56 return GetMenuController() && 57 GetMenuController()->OnMouseWheel(submenu_, event); 58 #else 59 // Windows uses focus_util_win::RerouteMouseWheel to forward events to 60 // the right menu. 61 // RootView::OnMouseWheel forwards to the focused view. We don't have a 62 // focused view, so we need to override this then forward to the menu. 63 return submenu_->OnMouseWheel(event); 64 #endif 65 } 66 67 void MenuHostRootView::DispatchGestureEvent(ui::GestureEvent* event) { 68 RootView::DispatchGestureEvent(event); 69 if (event->handled()) 70 return; 71 // ChromeOS uses MenuController to forward events like other 72 // mouse events. 73 if (!GetMenuController()) 74 return; 75 GetMenuController()->OnGestureEvent(submenu_, event); 76 } 77 78 MenuController* MenuHostRootView::GetMenuController() { 79 return submenu_ ? submenu_->GetMenuItem()->GetMenuController() : NULL; 80 } 81 82 bool MenuHostRootView::DoesEventTargetEmptyMenuItem( 83 const ui::MouseEvent& event) { 84 View* view = GetEventHandlerForPoint(event.location()); 85 return view && view->id() == MenuItemView::kEmptyMenuItemViewID; 86 } 87 88 } // namespace views 89