1// Copyright (c) 2011 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 "chrome/browser/ui/views/tab_contents/render_view_context_menu_views.h"
6
7#include "base/logging.h"
8#include "chrome/app/chrome_command_ids.h"
9#include "content/browser/renderer_host/render_widget_host_view.h"
10#include "content/browser/tab_contents/tab_contents.h"
11#include "grit/generated_resources.h"
12#include "ui/base/keycodes/keyboard_codes.h"
13#include "views/accelerator.h"
14#include "views/controls/menu/menu_2.h"
15
16////////////////////////////////////////////////////////////////////////////////
17// RenderViewContextMenuViews, public:
18
19RenderViewContextMenuViews::RenderViewContextMenuViews(
20    TabContents* tab_contents,
21    const ContextMenuParams& params)
22    : RenderViewContextMenu(tab_contents, params) {
23}
24
25RenderViewContextMenuViews::~RenderViewContextMenuViews() {
26}
27
28void RenderViewContextMenuViews::RunMenuAt(int x, int y) {
29  menu_->RunContextMenuAt(gfx::Point(x, y));
30}
31
32#if defined(OS_WIN)
33void RenderViewContextMenuViews::SetExternal() {
34  external_ = true;
35}
36#endif
37
38void RenderViewContextMenuViews::UpdateMenuItemStates() {
39  menu_->UpdateStates();
40}
41
42////////////////////////////////////////////////////////////////////////////////
43// RenderViewContextMenuViews, protected:
44
45void RenderViewContextMenuViews::PlatformInit() {
46  menu_.reset(new views::Menu2(&menu_model_));
47
48#if defined(OS_WIN)
49  if (external_) {
50    // The external tab container needs to be notified by command
51    // and not by index. So we are turning off the MNS_NOTIFYBYPOS
52    // style.
53    HMENU menu = GetMenuHandle();
54    DCHECK(menu != NULL);
55
56    MENUINFO mi = {0};
57    mi.cbSize = sizeof(mi);
58    mi.fMask = MIM_STYLE | MIM_MENUDATA;
59    mi.dwMenuData = reinterpret_cast<ULONG_PTR>(this);
60    SetMenuInfo(menu, &mi);
61  }
62#endif
63}
64
65bool RenderViewContextMenuViews::GetAcceleratorForCommandId(
66    int command_id,
67    ui::Accelerator* accel) {
68  // There are no formally defined accelerators we can query so we assume
69  // that Ctrl+C, Ctrl+V, Ctrl+X, Ctrl-A, etc do what they normally do.
70  switch (command_id) {
71    case IDC_CONTENT_CONTEXT_UNDO:
72      *accel = views::Accelerator(ui::VKEY_Z, false, true, false);
73      return true;
74
75    case IDC_CONTENT_CONTEXT_REDO:
76      // TODO(jcampan): should it be Ctrl-Y?
77      *accel = views::Accelerator(ui::VKEY_Z, true, true, false);
78      return true;
79
80    case IDC_CONTENT_CONTEXT_CUT:
81      *accel = views::Accelerator(ui::VKEY_X, false, true, false);
82      return true;
83
84    case IDC_CONTENT_CONTEXT_COPY:
85      *accel = views::Accelerator(ui::VKEY_C, false, true, false);
86      return true;
87
88    case IDC_CONTENT_CONTEXT_PASTE:
89      *accel = views::Accelerator(ui::VKEY_V, false, true, false);
90      return true;
91
92    case IDC_CONTENT_CONTEXT_SELECTALL:
93      *accel = views::Accelerator(ui::VKEY_A, false, true, false);
94      return true;
95
96    default:
97      return false;
98  }
99}
100