1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/views/controls/menu/menu_runner.h"
6
7#include "ui/views/controls/menu/menu_runner_handler.h"
8#include "ui/views/controls/menu/menu_runner_impl.h"
9
10namespace views {
11
12MenuRunner::MenuRunner(ui::MenuModel* menu_model, int32 run_types)
13    : run_types_(run_types),
14      impl_(internal::MenuRunnerImplInterface::Create(menu_model, run_types)) {
15}
16
17MenuRunner::MenuRunner(MenuItemView* menu_view, int32 run_types)
18    : run_types_(run_types), impl_(new internal::MenuRunnerImpl(menu_view)) {
19}
20
21MenuRunner::~MenuRunner() {
22  impl_->Release();
23}
24
25MenuRunner::RunResult MenuRunner::RunMenuAt(Widget* parent,
26                                            MenuButton* button,
27                                            const gfx::Rect& bounds,
28                                            MenuAnchorPosition anchor,
29                                            ui::MenuSourceType source_type) {
30  if (runner_handler_.get()) {
31    return runner_handler_->RunMenuAt(
32        parent, button, bounds, anchor, source_type, run_types_);
33  }
34
35  // The parent of the nested menu will have created a DisplayChangeListener, so
36  // we avoid creating a DisplayChangeListener if nested. Drop menus are
37  // transient, so we don't cancel in that case.
38  if ((run_types_ & (IS_NESTED | FOR_DROP)) == 0 && parent) {
39    display_change_listener_.reset(
40        internal::DisplayChangeListener::Create(parent, this));
41  }
42
43  if (run_types_ & CONTEXT_MENU) {
44    switch (source_type) {
45      case ui::MENU_SOURCE_NONE:
46      case ui::MENU_SOURCE_KEYBOARD:
47      case ui::MENU_SOURCE_MOUSE:
48        anchor = MENU_ANCHOR_TOPLEFT;
49        break;
50      case ui::MENU_SOURCE_TOUCH:
51      case ui::MENU_SOURCE_TOUCH_EDIT_MENU:
52        anchor = MENU_ANCHOR_BOTTOMCENTER;
53        break;
54      default:
55        break;
56    }
57  }
58
59  return impl_->RunMenuAt(parent, button, bounds, anchor, run_types_);
60}
61
62bool MenuRunner::IsRunning() const {
63  return impl_->IsRunning();
64}
65
66void MenuRunner::Cancel() {
67  impl_->Cancel();
68}
69
70base::TimeDelta MenuRunner::closing_event_time() const {
71  return impl_->GetClosingEventTime();
72}
73
74void MenuRunner::SetRunnerHandler(
75    scoped_ptr<MenuRunnerHandler> runner_handler) {
76  runner_handler_ = runner_handler.Pass();
77}
78
79}  // namespace views
80