Logo Search packages:      
Sourcecode: chromium-browser version File versions  Download package

render_view_context_menu_gtk.cc

// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/views/tab_contents/render_view_context_menu_gtk.h"

#include "app/l10n_util.h"
#include "base/compiler_specific.h"
#include "base/keyboard_codes.h"
#include "chrome/browser/profile.h"
#include "grit/generated_resources.h"
#include "views/accelerator.h"
#include "views/controls/menu/menu_2.h"

////////////////////////////////////////////////////////////////////////////////
// RenderViewContextMenuGtk, public:

RenderViewContextMenuGtk::RenderViewContextMenuGtk(
    TabContents* tab_contents,
    const ContextMenuParams& params)
    : RenderViewContextMenu(tab_contents, params),
      current_radio_group_id_(-1),
      sub_menu_contents_(NULL) {
  menu_contents_.reset(new menus::SimpleMenuModel(this));
}

RenderViewContextMenuGtk::~RenderViewContextMenuGtk() {
}

void RenderViewContextMenuGtk::RunMenuAt(int x, int y) {
  menu_->RunContextMenuAt(gfx::Point(x, y));
}

////////////////////////////////////////////////////////////////////////////////
// RenderViewContextMenuGtk, menus::SimpleMenuModel::Delegate implementation:

bool RenderViewContextMenuGtk::IsCommandIdChecked(int command_id) const {
  return ItemIsChecked(command_id);
}

bool RenderViewContextMenuGtk::IsCommandIdEnabled(int command_id) const {
  return IsItemCommandEnabled(command_id);
}

bool RenderViewContextMenuGtk::GetAcceleratorForCommandId(
    int command_id,
    menus::Accelerator* accel) {
  // There are no formally defined accelerators we can query so we assume
  // that Ctrl+C, Ctrl+V, Ctrl+X, Ctrl-A, etc do what they normally do.
  switch (command_id) {
    case IDS_CONTENT_CONTEXT_UNDO:
      *accel = views::Accelerator(base::VKEY_Z, false, true, false);
      return true;

    case IDS_CONTENT_CONTEXT_REDO:
      // TODO(jcampan): should it be Ctrl-Y?
      *accel = views::Accelerator(base::VKEY_Z, true, true, false);
      return true;

    case IDS_CONTENT_CONTEXT_CUT:
      *accel = views::Accelerator(base::VKEY_X, false, true, false);
      return true;

    case IDS_CONTENT_CONTEXT_COPY:
      *accel = views::Accelerator(base::VKEY_C, false, true, false);
      return true;

    case IDS_CONTENT_CONTEXT_PASTE:
      *accel = views::Accelerator(base::VKEY_V, false, true, false);
      return true;

    case IDS_CONTENT_CONTEXT_SELECTALL:
      *accel = views::Accelerator(base::VKEY_A, false, true, false);
      return true;

    default:
      return false;
  }
}

void RenderViewContextMenuGtk::ExecuteCommand(int command_id) {
  ExecuteItemCommand(command_id);
}

////////////////////////////////////////////////////////////////////////////////
// RenderViewContextMenuGtk, protected:

void RenderViewContextMenuGtk::DoInit() {
  menu_.reset(new views::Menu2(menu_contents_.get()));
}

void RenderViewContextMenuGtk::AppendMenuItem(int id) {
  current_radio_group_id_ = -1;
  GetTargetModel()->AddItemWithStringId(id, id);
}

void RenderViewContextMenuGtk::AppendMenuItem(int id,
                                              const string16& label) {
  current_radio_group_id_ = -1;
  GetTargetModel()->AddItem(id, label);
}

void RenderViewContextMenuGtk::AppendRadioMenuItem(int id,
                                                   const string16& label) {
  if (current_radio_group_id_ < 0)
    current_radio_group_id_ = id;
  GetTargetModel()->AddRadioItem(id, label, current_radio_group_id_);
}

void RenderViewContextMenuGtk::AppendCheckboxMenuItem(int id,
                                                      const string16& label) {
  current_radio_group_id_ = -1;
  GetTargetModel()->AddCheckItem(id, label);
}

void RenderViewContextMenuGtk::AppendSeparator() {
  current_radio_group_id_ = -1;
  GetTargetModel()->AddSeparator();
}

void RenderViewContextMenuGtk::StartSubMenu(int id, const string16& label) {
  if (sub_menu_contents_) {
    NOTREACHED() << "nested submenus not supported yet";
    return;
  }
  current_radio_group_id_ = -1;
  sub_menu_contents_ = new menus::SimpleMenuModel(this);
  menu_contents_->AddSubMenu(label, sub_menu_contents_);
  submenu_models_.push_back(sub_menu_contents_);
}

void RenderViewContextMenuGtk::FinishSubMenu() {
  DCHECK(sub_menu_contents_);
  current_radio_group_id_ = -1;
  sub_menu_contents_ = NULL;
}

////////////////////////////////////////////////////////////////////////////////
// RenderViewContextMenuGtk, private:

menus::SimpleMenuModel* RenderViewContextMenuGtk::GetTargetModel() const {
  return sub_menu_contents_ ? sub_menu_contents_ : menu_contents_.get();
}

Generated by  Doxygen 1.6.0   Back to index