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 "chrome/browser/ui/gtk/tabs/tab_strip_menu_controller.h"
6
7#include "chrome/browser/ui/gtk/accelerators_gtk.h"
8#include "chrome/browser/ui/gtk/menu_gtk.h"
9#include "chrome/browser/ui/gtk/tabs/tab_gtk.h"
10#include "chrome/browser/ui/tabs/tab_strip_model.h"
11
12TabStripMenuController::TabStripMenuController(TabGtk* tab,
13                                               TabStripModel* model,
14                                               int index)
15    : tab_(tab),
16      model_(this, model, index) {
17  menu_.reset(new MenuGtk(this, &model_));
18}
19
20TabStripMenuController::~TabStripMenuController() {}
21
22void TabStripMenuController::RunMenu(const gfx::Point& point,
23                                     guint32 event_time) {
24  menu_->PopupAsContext(point, event_time);
25}
26
27void TabStripMenuController::Cancel() {
28  tab_ = NULL;
29  menu_->Cancel();
30}
31
32bool TabStripMenuController::IsCommandIdChecked(int command_id) const {
33  return false;
34}
35
36bool TabStripMenuController::IsCommandIdEnabled(int command_id) const {
37  return tab_ && tab_->delegate()->IsCommandEnabledForTab(
38      static_cast<TabStripModel::ContextMenuCommand>(command_id),
39      tab_);
40}
41
42bool TabStripMenuController::GetAcceleratorForCommandId(
43    int command_id,
44    ui::Accelerator* out_accelerator) {
45  int browser_command;
46  if (!TabStripModel::ContextMenuCommandToBrowserCommand(command_id,
47                                                         &browser_command))
48    return false;
49  const ui::Accelerator* accelerator =
50      AcceleratorsGtk::GetInstance()->GetPrimaryAcceleratorForCommand(
51          browser_command);
52  if (!accelerator)
53    return false;
54  *out_accelerator = *accelerator;
55  return true;
56}
57
58void TabStripMenuController::ExecuteCommand(int command_id, int event_flags) {
59  // Checking if the tab still exists since it is possible that the tab
60  // corresponding to this context menu has been closed.
61  if (!tab_)
62    return;
63  tab_->delegate()->ExecuteCommandForTab(
64      static_cast<TabStripModel::ContextMenuCommand>(command_id), tab_);
65}
66
67GtkWidget* TabStripMenuController::GetImageForCommandId(int command_id) const {
68  int browser_cmd_id;
69  if (!TabStripModel::ContextMenuCommandToBrowserCommand(command_id,
70                                                         &browser_cmd_id))
71    return NULL;
72  return MenuGtk::Delegate::GetDefaultImageForCommandId(browser_cmd_id);
73}
74