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(OS_LINUX) 55 // ChromeOS uses MenuController to forward events like other 56 // mouse events. 57 return GetMenuController() && 58 GetMenuController()->OnMouseWheel(submenu_, event); 59 #else 60 // Windows uses focus_util_win::RerouteMouseWheel to forward events to 61 // the right menu. 62 // RootView::OnMouseWheel forwards to the focused view. We don't have a 63 // focused view, so we need to override this then forward to the menu. 64 return submenu_->OnMouseWheel(event); 65 #endif 66 } 67 68 void MenuHostRootView::DispatchGestureEvent(ui::GestureEvent* event) { 69 RootView::DispatchGestureEvent(event); 70 if (event->handled()) 71 return; 72 // ChromeOS uses MenuController to forward events like other 73 // mouse events. 74 if (!GetMenuController()) 75 return; 76 GetMenuController()->OnGestureEvent(submenu_, event); 77 } 78 79 MenuController* MenuHostRootView::GetMenuController() { 80 return submenu_ ? submenu_->GetMenuItem()->GetMenuController() : NULL; 81 } 82 83 bool MenuHostRootView::DoesEventTargetEmptyMenuItem( 84 const ui::MouseEvent& event) { 85 View* view = GetEventHandlerForPoint(event.location()); 86 return view && view->id() == MenuItemView::kEmptyMenuItemViewID; 87 } 88 89 } // namespace views 90