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
11namespace views {
12
13MenuHostRootView::MenuHostRootView(Widget* widget,
14                                   SubmenuView* submenu)
15    : internal::RootView(widget),
16      submenu_(submenu),
17      forward_drag_to_menu_controller_(true) {
18}
19
20bool 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
31bool 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
39void 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
47void MenuHostRootView::OnMouseMoved(const ui::MouseEvent& event) {
48  RootView::OnMouseMoved(event);
49  if (GetMenuController())
50    GetMenuController()->OnMouseMoved(submenu_, event);
51}
52
53bool MenuHostRootView::OnMouseWheel(const ui::MouseWheelEvent& event) {
54  return GetMenuController() &&
55      GetMenuController()->OnMouseWheel(submenu_, event);
56}
57
58ui::EventDispatchDetails MenuHostRootView::OnEventFromSource(ui::Event* event) {
59  ui::EventDispatchDetails result = RootView::OnEventFromSource(event);
60
61  if (event->IsGestureEvent()) {
62    ui::GestureEvent* gesture_event = event->AsGestureEvent();
63    if (gesture_event->handled())
64      return result;
65    // ChromeOS uses MenuController to forward events like other
66    // mouse events.
67    if (!GetMenuController())
68      return result;
69    GetMenuController()->OnGestureEvent(submenu_, gesture_event);
70  }
71
72  return result;
73}
74
75MenuController* MenuHostRootView::GetMenuController() {
76  return submenu_ ? submenu_->GetMenuItem()->GetMenuController() : NULL;
77}
78
79bool MenuHostRootView::DoesEventTargetEmptyMenuItem(
80    const ui::MouseEvent& event) {
81  View* view = GetEventHandlerForPoint(event.location());
82  return view && view->id() == MenuItemView::kEmptyMenuItemViewID;
83}
84
85}  // namespace views
86