15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/menu/menu_controller.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/i18n/case_conversion.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/i18n/rtl.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
10eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/dragdrop/drag_utils.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/dragdrop/os_exchange_data.h"
130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "ui/events/event.h"
14d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event_utils.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/canvas.h"
16424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/gfx/native_widget_types.h"
170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "ui/gfx/point.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/screen.h"
19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/gfx/vector2d.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/native_theme/native_theme.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/button/menu_button.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/menu/menu_config.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/menu/menu_controller_delegate.h"
24424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/views/controls/menu/menu_host_root_view.h"
255c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "ui/views/controls/menu/menu_item_view.h"
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "ui/views/controls/menu/menu_message_loop.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/menu/menu_scroll_view_container.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/menu/submenu_view.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/drag_utils.h"
30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/views/focus/view_storage.h"
31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/views/mouse_constants.h"
320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "ui/views/view.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/view_constants.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/views_delegate.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/widget/root_view.h"
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/widget/tooltip_manager.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/widget/widget.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_WIN)
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/base/win/internal_constants.h"
416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "ui/gfx/win/dpi.h"
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/views/win/hwnd_util.h"
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::Time;
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::TimeDelta;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using ui::OSExchangeData;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Period of the scroll timer (in milliseconds).
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const int kScrollTimerMS = 30;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Amount of time from when the drop exits the menu and the menu is hidden.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const int kCloseOnExitTime = 1200;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If a context menu is invoked by touch, we shift the menu by this offset so
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// that the finger does not obscure the menu.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const int kCenteredContextMenuYOffset = -15;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace views {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// When showing context menu on mouse down, the user might accidentally select
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// the menu item on the subsequent mouse up. To prevent this, we add the
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// following delay before the user is able to select an item.
66424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)static int menu_selection_hold_time_ms = kMinimumMsPressedToActivate;
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The spacing offset for the bubble tip.
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int kBubbleTipSizeLeftRight = 12;
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int kBubbleTipSizeTopBottom = 11;
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// The maximum distance (in DIPS) that the mouse can be moved before it should
73424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// trigger a mouse menu item activation (regardless of how long the menu has
74424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// been showing).
75424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)const float kMaximumLengthMovedToActivate = 4.0f;
76424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the mnemonic of |menu| matches key.
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool MatchesMnemonic(MenuItemView* menu, base::char16 key) {
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return key != 0 && menu->GetMnemonic() == key;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if |menu| doesn't have a mnemonic and first character of the its
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// title is |key|.
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool TitleMatchesMnemonic(MenuItemView* menu, base::char16 key) {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (menu->GetMnemonic())
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::string16 lower_title = base::i18n::ToLower(menu->title());
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return !lower_title.empty() && lower_title[0] == key;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the first descendant of |view| that is hot tracked.
95a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)static CustomButton* GetFirstHotTrackedView(View* view) {
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!view)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
98a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  CustomButton* button = CustomButton::AsCustomButton(view);
99a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (button) {
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (button->IsHotTracked())
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return button;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (int i = 0; i < view->child_count(); ++i) {
105a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    CustomButton* hot_view = GetFirstHotTrackedView(view->child_at(i));
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (hot_view)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return hot_view;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Recurses through the child views of |view| returning the first view starting
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// at |start| that is focusable. A value of -1 for |start| indicates to start at
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the first view (if |forward| is false, iterating starts at the last view). If
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |forward| is true the children are considered first to last, otherwise last
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to first.
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static View* GetFirstFocusableView(View* view, int start, bool forward) {
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (forward) {
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (int i = start == -1 ? 0 : start; i < view->child_count(); ++i) {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      View* deepest = GetFirstFocusableView(view->child_at(i), -1, forward);
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (deepest)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return deepest;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (int i = start == -1 ? view->child_count() - 1 : start; i >= 0; --i) {
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      View* deepest = GetFirstFocusableView(view->child_at(i), -1, forward);
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (deepest)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return deepest;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return view->IsFocusable() ? view : NULL;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the first child of |start| that is focusable.
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static View* GetInitialFocusableView(View* start, bool forward) {
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetFirstFocusableView(start, -1, forward);
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns the next view after |start_at| that is focusable. Returns NULL if
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// there are no focusable children of |ancestor| after |start_at|.
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static View* GetNextFocusableView(View* ancestor,
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  View* start_at,
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  bool forward) {
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ancestor->Contains(start_at));
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  View* parent = start_at;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  do {
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    View* new_parent = parent->parent();
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int index = new_parent->GetIndexOf(parent);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    index += forward ? 1 : -1;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (forward || index != -1) {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      View* next = GetFirstFocusableView(new_parent, index, forward);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (next)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return next;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    parent = new_parent;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } while (parent != ancestor);
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MenuScrollTask --------------------------------------------------------------
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MenuScrollTask is used when the SubmenuView does not all fit on screen and
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the mouse is over the scroll up/down buttons. MenuScrollTask schedules
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// itself with a RepeatingTimer. When Run is invoked MenuScrollTask scrolls
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// appropriately.
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MenuController::MenuScrollTask {
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuScrollTask() : submenu_(NULL), is_scrolling_up_(false), start_y_(0) {
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pixels_per_second_ = MenuItemView::pref_menu_height() * 20;
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Update(const MenuController::MenuPart& part) {
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!part.is_scroll()) {
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      StopScrolling();
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(part.submenu);
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SubmenuView* new_menu = part.submenu;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool new_is_up = (part.type == MenuController::MenuPart::SCROLL_UP);
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (new_menu == submenu_ && is_scrolling_up_ == new_is_up)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    start_scroll_time_ = base::Time::Now();
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    start_y_ = part.submenu->GetVisibleBounds().y();
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    submenu_ = new_menu;
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    is_scrolling_up_ = new_is_up;
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!scrolling_timer_.IsRunning()) {
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scrolling_timer_.Start(FROM_HERE,
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             TimeDelta::FromMilliseconds(kScrollTimerMS),
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             this, &MenuScrollTask::Run);
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StopScrolling() {
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (scrolling_timer_.IsRunning()) {
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scrolling_timer_.Stop();
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      submenu_ = NULL;
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The menu being scrolled. Returns null if not scrolling.
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SubmenuView* submenu() const { return submenu_; }
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Run() {
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(submenu_);
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    gfx::Rect vis_rect = submenu_->GetVisibleBounds();
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int delta_y = static_cast<int>(
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        (base::Time::Now() - start_scroll_time_).InMilliseconds() *
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        pixels_per_second_ / 1000);
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    vis_rect.set_y(is_scrolling_up_ ?
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        std::max(0, start_y_ - delta_y) :
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        std::min(submenu_->height() - vis_rect.height(), start_y_ + delta_y));
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    submenu_->ScrollRectToVisible(vis_rect);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SubmenuView being scrolled.
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SubmenuView* submenu_;
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Direction scrolling.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_scrolling_up_;
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Timer to periodically scroll.
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::RepeatingTimer<MenuScrollTask> scrolling_timer_;
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Time we started scrolling at.
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time start_scroll_time_;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // How many pixels to scroll per second.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int pixels_per_second_;
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Y-coordinate of submenu_view_ when scrolling started.
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int start_y_;
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MenuScrollTask);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MenuController:SelectByCharDetails ----------------------------------------
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct MenuController::SelectByCharDetails {
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SelectByCharDetails()
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : first_match(-1),
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        has_multiple(false),
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        index_of_item(-1),
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        next_match(-1) {
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Index of the first menu with the specified mnemonic.
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int first_match;
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If true there are multiple menu items with the same mnemonic.
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool has_multiple;
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Index of the selected item; may remain -1.
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int index_of_item;
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If there are multiple matches this is the index of the item after the
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // currently selected item whose mnemonic matches. This may remain -1 even
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // though there are matches.
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int next_match;
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MenuController:State ------------------------------------------------------
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MenuController::State::State()
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : item(NULL),
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      submenu_open(false),
2705c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      anchor(MENU_ANCHOR_TOPLEFT),
2715c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      context_menu(false) {
2725c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MenuController::State::~State() {}
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MenuController ------------------------------------------------------------
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MenuController* MenuController::active_instance_ = NULL;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MenuController* MenuController::GetActiveInstance() {
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return active_instance_;
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MenuItemView* MenuController::Run(Widget* parent,
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  MenuButton* button,
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  MenuItemView* root,
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const gfx::Rect& bounds,
2905c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                                  MenuAnchorPosition position,
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  bool context_menu,
2925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                  bool is_nested_drag,
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  int* result_event_flags) {
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  exit_type_ = EXIT_NONE;
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  possible_drag_ = false;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  drag_in_progress_ = false;
297116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  did_initiate_drag_ = false;
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  closing_event_time_ = base::TimeDelta();
299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu_start_time_ = base::TimeTicks::Now();
300424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  menu_start_mouse_press_loc_ = gfx::Point();
301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If we are shown on mouse press, we will eat the subsequent mouse down and
303c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the parent widget will not be able to reset its state (it might have mouse
304c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // capture from the mouse down). So we clear its state here.
305424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (parent) {
306424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    View* root_view = parent->GetRootView();
307424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (root_view) {
308424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      root_view->SetMouseHandler(NULL);
309424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      const ui::Event* event =
310424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          static_cast<internal::RootView*>(root_view)->current_event();
311424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      if (event && event->type() == ui::ET_MOUSE_PRESSED) {
312424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        gfx::Point screen_loc(
313424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)            static_cast<const ui::MouseEvent*>(event)->location());
314424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        View::ConvertPointToScreen(
315424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)            static_cast<View*>(event->target()), &screen_loc);
316424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        menu_start_mouse_press_loc_ = screen_loc;
317424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      }
318424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    }
319424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool nested_menu = showing_;
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (showing_) {
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Only support nesting of blocking_run menus, nesting of
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // blocking/non-blocking shouldn't be needed.
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(blocking_run_);
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We're already showing, push the current state.
3281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    menu_stack_.push_back(
3291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        std::make_pair(state_, make_linked_ptr(pressed_lock_.release())));
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The context menu should be owned by the same parent.
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK_EQ(owner_, parent);
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    showing_ = true;
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reset current state.
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pending_state_ = State();
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  state_ = State();
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UpdateInitialLocation(bounds, position, context_menu);
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (owner_)
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    owner_->RemoveObserver(this);
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  owner_ = parent;
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (owner_)
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    owner_->AddObserver(this);
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set the selection, which opens the initial menu.
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelection(root, SELECTION_OPEN_SUBMENU | SELECTION_UPDATE_IMMEDIATELY);
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!blocking_run_) {
3525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (!is_nested_drag) {
3535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      // Start the timer to hide the menu. This is needed as we get no
3545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      // notification when the drag has finished.
3555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      StartCancelAllTimer();
3565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    }
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (button)
3611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    pressed_lock_.reset(new MenuButton::PressedLock(button));
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make sure Chrome doesn't attempt to shut down while the menu is showing.
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (ViewsDelegate::views_delegate)
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ViewsDelegate::views_delegate->AddRef();
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We need to turn on nestable tasks as in some situations (pressing alt-f for
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // one) the menus are run from a task. If we don't do this and are invoked
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from a task none of the tasks we schedule are processed and the menu
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // appears totally broken.
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  message_loop_depth_++;
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK_LE(message_loop_depth_, 2);
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  RunMessageLoop(nested_menu);
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  message_loop_depth_--;
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (ViewsDelegate::views_delegate)
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ViewsDelegate::views_delegate->ReleaseRef();
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Close any open menus.
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelection(NULL, SELECTION_UPDATE_IMMEDIATELY | SELECTION_EXIT);
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(OS_WIN)
383a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // On Windows, if we select the menu item by touch and if the window at the
384a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // location is another window on the same thread, that window gets a
385a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // WM_MOUSEACTIVATE message and ends up activating itself, which is not
386a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // correct. We workaround this by setting a property on the window at the
387a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // current cursor location. We check for this property in our
388a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // WM_MOUSEACTIVATE handler and don't activate the window if the property is
389a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // set.
390a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (item_selected_by_touch_) {
391a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    item_selected_by_touch_ = false;
392a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    POINT cursor_pos;
393a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ::GetCursorPos(&cursor_pos);
394a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)     HWND window = ::WindowFromPoint(cursor_pos);
395a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)     if (::GetWindowThreadProcessId(window, NULL) ==
396a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                    ::GetCurrentThreadId()) {
397a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)       ::SetProp(window, ui::kIgnoreTouchMouseActivateForWindow,
398a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                 reinterpret_cast<HANDLE>(true));
399a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)     }
400a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
401a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif
402a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
4031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  linked_ptr<MenuButton::PressedLock> nested_pressed_lock;
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (nested_menu) {
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(!menu_stack_.empty());
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We're running from within a menu, restore the previous state.
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The menus are already showing, so we don't have to show them.
4081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    state_ = menu_stack_.back().first;
4091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    pending_state_ = menu_stack_.back().first;
4101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    nested_pressed_lock = menu_stack_.back().second;
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    menu_stack_.pop_back();
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    showing_ = false;
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    did_capture_ = false;
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuItemView* result = result_;
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // In case we're nested, reset result_.
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  result_ = NULL;
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (result_event_flags)
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    *result_event_flags = accept_event_flags_;
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (exit_type_ == EXIT_OUTERMOST) {
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetExitType(EXIT_NONE);
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (nested_menu && result) {
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // We're nested and about to return a value. The caller might enter
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // another blocking loop. We need to make sure all menus are hidden
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // before that happens otherwise the menus will stay on screen.
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CloseAllNestedMenus();
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SetSelection(NULL, SELECTION_UPDATE_IMMEDIATELY | SELECTION_EXIT);
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Set exit_all_, which makes sure all nested loops exit immediately.
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (exit_type_ != EXIT_DESTROYED)
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SetExitType(EXIT_ALL);
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Reset our pressed lock to the previous state's, if there was one.
4411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // The lock handles the case if the button was destroyed.
4421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  pressed_lock_.reset(nested_pressed_lock.release());
4431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::Cancel(ExitType type) {
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the menu has already been destroyed, no further cancellation is
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // needed.  We especially don't want to set the |exit_type_| to a lesser
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // value.
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (exit_type_ == EXIT_DESTROYED || exit_type_ == type)
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!showing_) {
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This occurs if we're in the process of notifying the delegate for a drop
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // and the delegate cancels us.
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuItemView* selected = state_.item;
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetExitType(type);
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SendMouseCaptureLostToActiveView();
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Hide windows immediately.
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelection(NULL, SELECTION_UPDATE_IMMEDIATELY | SELECTION_EXIT);
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!blocking_run_) {
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // If we didn't block the caller we need to notify the menu, which
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // triggers deleting us.
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(selected);
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    showing_ = false;
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delegate_->DropMenuClosed(
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        internal::MenuControllerDelegate::NOTIFY_DELEGATE,
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        selected->GetRootMenuItem());
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // WARNING: the call to MenuClosed deletes us.
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::OnMousePressed(SubmenuView* source,
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    const ui::MouseEvent& event) {
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelectionOnPointerDown(source, event);
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::OnMouseDragged(SubmenuView* source,
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    const ui::MouseEvent& event) {
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuPart part = GetMenuPart(source, event.location());
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UpdateScrolling(part);
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!blocking_run_)
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (possible_drag_) {
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (View::ExceededDragThreshold(event.location() - press_pt_))
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      StartDrag(source, press_pt_);
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuItemView* mouse_menu = NULL;
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (part.type == MenuPart::MENU_ITEM) {
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!part.menu)
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      part.menu = source->GetMenuItem();
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    else
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      mouse_menu = part.menu;
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetSelection(part.menu ? part.menu : state_.item, SELECTION_OPEN_SUBMENU);
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (part.type == MenuPart::NONE) {
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ShowSiblingMenu(source, event.location());
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UpdateActiveMouseView(source, event, mouse_menu);
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::OnMouseReleased(SubmenuView* source,
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     const ui::MouseEvent& event) {
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!blocking_run_)
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(state_.item);
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  possible_drag_ = false;
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(blocking_run_);
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuPart part = GetMenuPart(source, event.location());
5212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (event.IsRightMouseButton() && part.type == MenuPart::MENU_ITEM) {
5222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MenuItemView* menu = part.menu;
5232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // |menu| is NULL means this event is from an empty menu or a separator.
5242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If it is from an empty menu, use parent context menu instead of that.
5252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (menu == NULL &&
5262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        part.submenu->child_count() == 1 &&
527424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        part.submenu->child_at(0)->id() == MenuItemView::kEmptyMenuItemViewID) {
5282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      menu = part.parent;
529424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    }
5302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (menu != NULL && ShowContextMenu(menu, source, event,
5327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                        ui::MENU_SOURCE_MOUSE))
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We can use Ctrl+click or the middle mouse button to recursively open urls
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for selected folder menu items. If it's only a left click, show the
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // contents of the folder.
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!part.is_scroll() && part.menu &&
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !(part.menu->HasSubmenu() &&
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        (event.flags() & ui::EF_LEFT_MOUSE_BUTTON))) {
542424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (GetActiveMouseView()) {
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SendMouseReleaseToActiveView(source, event);
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
546424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // If a mouse release was received quickly after showing.
547424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    base::TimeDelta time_shown = base::TimeTicks::Now() - menu_start_time_;
548424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (time_shown.InMilliseconds() < menu_selection_hold_time_ms) {
549424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      // And it wasn't far from the mouse press location.
550424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      gfx::Point screen_loc(event.location());
551424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      View::ConvertPointToScreen(source->GetScrollViewContainer(), &screen_loc);
552424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      gfx::Vector2d moved = screen_loc - menu_start_mouse_press_loc_;
553424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      if (moved.Length() < kMaximumLengthMovedToActivate) {
554424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // Ignore the mouse release as it was likely this menu was shown under
555424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // the mouse and the action was just a normal click.
556424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        return;
557424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      }
558424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    }
5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (part.menu->GetDelegate()->ShouldExecuteCommandWithoutClosingMenu(
5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            part.menu->GetCommand(), event)) {
5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      part.menu->GetDelegate()->ExecuteCommand(part.menu->GetCommand(),
5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                               event.flags());
5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return;
5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!part.menu->NonIconChildViewsCount() &&
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        part.menu->GetDelegate()->IsTriggerableEvent(part.menu, event)) {
567424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      base::TimeDelta shown_time = base::TimeTicks::Now() - menu_start_time_;
568c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      if (!state_.context_menu || !View::ShouldShowContextMenuOnMousePress() ||
569424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          shown_time.InMilliseconds() > menu_selection_hold_time_ms) {
570c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        Accept(part.menu, event.flags());
571424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      }
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (part.type == MenuPart::MENU_ITEM) {
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // User either clicked on empty space, or a menu that has children.
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetSelection(part.menu ? part.menu : state_.item,
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 SELECTION_OPEN_SUBMENU | SELECTION_UPDATE_IMMEDIATELY);
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SendMouseCaptureLostToActiveView();
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::OnMouseMoved(SubmenuView* source,
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const ui::MouseEvent& event) {
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HandleMouseLocation(source, event.location());
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::OnMouseEntered(SubmenuView* source,
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    const ui::MouseEvent& event) {
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // MouseEntered is always followed by a mouse moved, so don't need to
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // do anything here.
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MenuController::OnMouseWheel(SubmenuView* source,
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const ui::MouseWheelEvent& event) {
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuPart part = GetMenuPart(source, event.location());
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return part.submenu && part.submenu->OnMouseWheel(event);
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MenuController::OnGestureEvent(SubmenuView* source,
6002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                    ui::GestureEvent* event) {
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuPart part = GetMenuPart(source, event->location());
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetSelectionOnPointerDown(source, *event);
6042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    event->StopPropagation();
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (event->type() == ui::ET_GESTURE_LONG_PRESS) {
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (part.type == MenuPart::MENU_ITEM && part.menu) {
6077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (ShowContextMenu(part.menu, source, *event, ui::MENU_SOURCE_TOUCH))
6082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        event->StopPropagation();
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (event->type() == ui::ET_GESTURE_TAP) {
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!part.is_scroll() && part.menu &&
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        !(part.menu->HasSubmenu())) {
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (part.menu->GetDelegate()->IsTriggerableEvent(
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          part.menu, *event)) {
6152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        Accept(part.menu, event->flags());
616a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        item_selected_by_touch_ = true;
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
6182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      event->StopPropagation();
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else if (part.type == MenuPart::MENU_ITEM) {
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // User either tapped on empty space, or a menu that has children.
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SetSelection(part.menu ? part.menu : state_.item,
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   SELECTION_OPEN_SUBMENU | SELECTION_UPDATE_IMMEDIATELY);
6232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      event->StopPropagation();
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else if (event->type() == ui::ET_GESTURE_TAP_CANCEL &&
6262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)             part.menu &&
6272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)             part.type == MenuPart::MENU_ITEM) {
6282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Move the selection to the parent menu so that the selection in the
6292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // current menu is unset. Make sure the submenu remains open by sending the
6302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // appropriate SetSelectionTypes flags.
6312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SetSelection(part.menu->GetParentMenuItem(),
6322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SELECTION_OPEN_SUBMENU | SELECTION_UPDATE_IMMEDIATELY);
6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    event->StopPropagation();
6342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (event->stopped_propagation())
6372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return;
6382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!part.submenu)
6402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  part.submenu->OnGestureEvent(event);
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MenuController::GetDropFormats(
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SubmenuView* source,
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int* formats,
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::set<OSExchangeData::CustomFormat>* custom_formats) {
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return source->GetMenuItem()->GetDelegate()->GetDropFormats(
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      source->GetMenuItem(), formats, custom_formats);
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MenuController::AreDropTypesRequired(SubmenuView* source) {
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return source->GetMenuItem()->GetDelegate()->AreDropTypesRequired(
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      source->GetMenuItem());
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MenuController::CanDrop(SubmenuView* source, const OSExchangeData& data) {
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return source->GetMenuItem()->GetDelegate()->CanDrop(source->GetMenuItem(),
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                       data);
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::OnDragEntered(SubmenuView* source,
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const ui::DropTargetEvent& event) {
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  valid_drop_coordinates_ = false;
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int MenuController::OnDragUpdated(SubmenuView* source,
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const ui::DropTargetEvent& event) {
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StopCancelAllTimer();
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Point screen_loc(event.location());
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  View::ConvertPointToScreen(source, &screen_loc);
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (valid_drop_coordinates_ && screen_loc == drop_pt_)
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return last_drop_operation_;
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  drop_pt_ = screen_loc;
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  valid_drop_coordinates_ = true;
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuItemView* menu_item = GetMenuItemAt(source, event.x(), event.y());
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool over_empty_menu = false;
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!menu_item) {
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // See if we're over an empty menu.
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    menu_item = GetEmptyMenuItemAt(source, event.x(), event.y());
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (menu_item)
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      over_empty_menu = true;
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuDelegate::DropPosition drop_position = MenuDelegate::DROP_NONE;
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int drop_operation = ui::DragDropTypes::DRAG_NONE;
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (menu_item) {
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    gfx::Point menu_item_loc(event.location());
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    View::ConvertPointToTarget(source, menu_item, &menu_item_loc);
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MenuItemView* query_menu_item;
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!over_empty_menu) {
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int menu_item_height = menu_item->height();
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (menu_item->HasSubmenu() &&
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          (menu_item_loc.y() > kDropBetweenPixels &&
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           menu_item_loc.y() < (menu_item_height - kDropBetweenPixels))) {
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        drop_position = MenuDelegate::DROP_ON;
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        drop_position = (menu_item_loc.y() < menu_item_height / 2) ?
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            MenuDelegate::DROP_BEFORE : MenuDelegate::DROP_AFTER;
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      query_menu_item = menu_item;
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      query_menu_item = menu_item->GetParentMenuItem();
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      drop_position = MenuDelegate::DROP_ON;
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    drop_operation = menu_item->GetDelegate()->GetDropOperation(
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        query_menu_item, event, &drop_position);
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // If the menu has a submenu, schedule the submenu to open.
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetSelection(menu_item, menu_item->HasSubmenu() ? SELECTION_OPEN_SUBMENU :
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 SELECTION_DEFAULT);
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (drop_position == MenuDelegate::DROP_NONE ||
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        drop_operation == ui::DragDropTypes::DRAG_NONE)
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      menu_item = NULL;
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetSelection(source->GetMenuItem(), SELECTION_OPEN_SUBMENU);
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetDropMenuItem(menu_item, drop_position);
7215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  last_drop_operation_ = drop_operation;
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return drop_operation;
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::OnDragExited(SubmenuView* source) {
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartCancelAllTimer();
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (drop_target_) {
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    StopShowTimer();
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetDropMenuItem(NULL, MenuDelegate::DROP_NONE);
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int MenuController::OnPerformDrop(SubmenuView* source,
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const ui::DropTargetEvent& event) {
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(drop_target_);
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: the delegate may delete us after invoking OnPerformDrop, as such
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we don't call cancel here.
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuItemView* item = state_.item;
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(item);
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuItemView* drop_target = drop_target_;
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuDelegate::DropPosition drop_position = drop_position_;
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Close all menus, including any nested menus.
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelection(NULL, SELECTION_UPDATE_IMMEDIATELY | SELECTION_EXIT);
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CloseAllNestedMenus();
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set state such that we exit.
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  showing_ = false;
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetExitType(EXIT_ALL);
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If over an empty menu item, drop occurs on the parent.
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (drop_target->id() == MenuItemView::kEmptyMenuItemViewID)
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    drop_target = drop_target->GetParentMenuItem();
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!IsBlockingRun()) {
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delegate_->DropMenuClosed(
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        internal::MenuControllerDelegate::DONT_NOTIFY_DELEGATE,
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        item->GetRootMenuItem());
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // WARNING: the call to MenuClosed deletes us.
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return drop_target->GetDelegate()->OnPerformDrop(
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      drop_target, drop_position, event);
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::OnDragEnteredScrollButton(SubmenuView* source,
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                               bool is_up) {
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuPart part;
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  part.type = is_up ? MenuPart::SCROLL_UP : MenuPart::SCROLL_DOWN;
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  part.submenu = source;
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UpdateScrolling(part);
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Do this to force the selection to hide.
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetDropMenuItem(source->GetMenuItemAt(0), MenuDelegate::DROP_NONE);
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StopCancelAllTimer();
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::OnDragExitedScrollButton(SubmenuView* source) {
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartCancelAllTimer();
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetDropMenuItem(NULL, MenuDelegate::DROP_NONE);
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StopScrolling();
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
789116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid MenuController::OnDragWillStart() {
790116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(!drag_in_progress_);
791116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  drag_in_progress_ = true;
792116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
793116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
794116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid MenuController::OnDragComplete(bool should_close) {
795116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(drag_in_progress_);
796116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  drag_in_progress_ = false;
797116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (showing_ && should_close && GetActiveInstance() == this) {
798116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    CloseAllNestedMenus();
799116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    Cancel(EXIT_ALL);
800116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
801116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
802116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::UpdateSubmenuSelection(SubmenuView* submenu) {
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (submenu->IsShowing()) {
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    gfx::Point point = GetScreen()->GetCursorScreenPoint();
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const SubmenuView* root_submenu =
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        submenu->GetMenuItem()->GetRootMenuItem()->GetSubmenu();
808424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    View::ConvertPointFromScreen(
8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        root_submenu->GetWidget()->GetRootView(), &point);
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HandleMouseLocation(submenu, point);
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MenuController::OnWidgetDestroying(Widget* widget) {
8152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(owner_, widget);
8162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  owner_->RemoveObserver(this);
8172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  owner_ = NULL;
818cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_loop_->ClearOwner();
8192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool MenuController::IsCancelAllTimerRunningForTest() {
8225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return cancel_all_timer_.IsRunning();
8235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
8245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
825c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
826424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void MenuController::TurnOffMenuSelectionHoldForTest() {
827424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  menu_selection_hold_time_ms = -1;
828c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
829c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::SetSelection(MenuItemView* menu_item,
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  int selection_types) {
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t paths_differ_at = 0;
8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<MenuItemView*> current_path;
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<MenuItemView*> new_path;
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BuildPathsAndCalculateDiff(pending_state_.item, menu_item, &current_path,
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             &new_path, &paths_differ_at);
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t current_size = current_path.size();
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t new_size = new_path.size();
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool pending_item_changed = pending_state_.item != menu_item;
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pending_item_changed && pending_state_.item) {
843a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    CustomButton* button = GetFirstHotTrackedView(pending_state_.item);
844a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (button)
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      button->SetHotTracked(false);
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notify the old path it isn't selected.
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuDelegate* current_delegate =
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      current_path.empty() ? NULL : current_path.front()->GetDelegate();
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = paths_differ_at; i < current_size; ++i) {
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (current_delegate &&
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        current_path[i]->GetType() == MenuItemView::SUBMENU) {
8545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      current_delegate->WillHideMenu(current_path[i]);
8555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    current_path[i]->SetSelected(false);
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notify the new path it is selected.
860eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = paths_differ_at; i < new_size; ++i) {
861ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    new_path[i]->ScrollRectToVisible(new_path[i]->GetLocalBounds());
8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_path[i]->SetSelected(true);
863eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
8645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (menu_item && menu_item->GetDelegate())
8665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    menu_item->GetDelegate()->SelectionChanged(menu_item);
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(menu_item || (selection_types & SELECTION_EXIT) != 0);
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pending_state_.item = menu_item;
8715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pending_state_.submenu_open = (selection_types & SELECTION_OPEN_SUBMENU) != 0;
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Stop timers.
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StopCancelAllTimer();
8755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Resets show timer only when pending menu item is changed.
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (pending_item_changed)
8775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    StopShowTimer();
8785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (selection_types & SELECTION_UPDATE_IMMEDIATELY)
8805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CommitPendingSelection();
8815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  else if (pending_item_changed)
8825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    StartShowTimer();
8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notify an accessibility focus event on all menu items except for the root.
8855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (menu_item &&
8865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (MenuDepth(menu_item) != 1 ||
8875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       menu_item->GetType() != MenuItemView::SUBMENU)) {
888c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    menu_item->NotifyAccessibilityEvent(
889a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        ui::AX_EVENT_FOCUS, true);
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::SetSelectionOnPointerDown(SubmenuView* source,
8945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                               const ui::LocatedEvent& event) {
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!blocking_run_)
8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
898424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(!GetActiveMouseView());
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuPart part = GetMenuPart(source, event.location());
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (part.is_scroll())
9025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;  // Ignore presses on scroll buttons.
9035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // When this menu is opened through a touch event, a simulated right-click
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is sent before the menu appears.  Ignore it.
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if ((event.flags() & ui::EF_RIGHT_MOUSE_BUTTON) &&
9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (event.flags() & ui::EF_FROM_TOUCH))
9085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (part.type == MenuPart::NONE ||
9115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (part.type == MenuPart::MENU_ITEM && part.menu &&
9125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       part.menu->GetRootMenuItem() != state_.item->GetRootMenuItem())) {
9132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Remember the time when we repost the event. The owner can then use this
9142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // to figure out if this menu was finished with the same click which is
9152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // sent to it thereafter. Note that the time stamp front he event cannot be
9162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // used since the reposting will set a new timestamp when the event gets
9172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // processed. As such it is better to take the current time which will be
9182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // closer to the time when it arrives again in the menu handler.
9192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    closing_event_time_ = ui::EventTimeForNow();
9202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Mouse wasn't pressed over any menu, or the active menu, cancel.
9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
923c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_WIN)
9245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We're going to close and we own the mouse capture. We need to repost the
925424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // mouse down, otherwise the window the user clicked on won't get the event.
9260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    RepostEvent(source, event);
9275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
9285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // And close.
9305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ExitType exit_type = EXIT_ALL;
9315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!menu_stack_.empty()) {
9325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // We're running nested menus. Only exit all if the mouse wasn't over one
9335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // of the menus from the last run.
9345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      gfx::Point screen_loc(event.location());
9355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      View::ConvertPointToScreen(source->GetScrollViewContainer(), &screen_loc);
9365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MenuPart last_part = GetMenuPartByScreenCoordinateUsingMenu(
9371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          menu_stack_.back().first.item, screen_loc);
9385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (last_part.type != MenuPart::NONE)
9395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        exit_type = EXIT_OUTERMOST;
9405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
9415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Cancel(exit_type);
9422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
94323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#if defined(OS_CHROMEOS)
9442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // We're going to exit the menu and want to repost the event so that is
9452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // is handled normally after the context menu has exited. We call
9462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // RepostEvent after Cancel so that mouse capture has been released so
9472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // that finding the event target is unaffected by the current capture.
9482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RepostEvent(source, event);
9492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
95023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    // Do not repost events for Linux Aura because this behavior is more
95123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    // consistent with the behavior of other Linux apps.
9525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
9535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On a press we immediately commit the selection, that way a submenu
9565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pops up immediately rather than after a delay.
9575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int selection_types = SELECTION_UPDATE_IMMEDIATELY;
9585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!part.menu) {
9595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    part.menu = part.parent;
9605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    selection_types |= SELECTION_OPEN_SUBMENU;
9615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
9625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (part.menu->GetDelegate()->CanDrag(part.menu)) {
9635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      possible_drag_ = true;
9645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      press_pt_ = event.location();
9655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
9665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (part.menu->HasSubmenu())
9675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      selection_types |= SELECTION_OPEN_SUBMENU;
9685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelection(part.menu, selection_types);
9705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MenuController::StartDrag(SubmenuView* source,
9735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const gfx::Point& location) {
9745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MenuItemView* item = state_.item;
9755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(item);
9765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Points are in the coordinates of the submenu, need to map to that of
977