summaryrefslogtreecommitdiffstats
path: root/chrome/browser/tab_contents/render_view_context_menu_gtk.cc
blob: 0362366c8719139de7ea58271793284166b6e29e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// 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/tab_contents/render_view_context_menu_gtk.h"

#include "base/string_util.h"
#include "webkit/glue/context_menu.h"

RenderViewContextMenuGtk::RenderViewContextMenuGtk(
    TabContents* web_contents,
    const ContextMenuParams& params,
    guint32 triggering_event_time)
    : RenderViewContextMenu(web_contents, params),
      making_submenu_(false),
      triggering_event_time_(triggering_event_time) {
  InitMenu(params.node);
  DoneMakingMenu(&menu_);
  gtk_menu_.reset(new MenuGtk(this, menu_.data(), NULL));
}

RenderViewContextMenuGtk::~RenderViewContextMenuGtk() {
}

void RenderViewContextMenuGtk::Popup() {
  gtk_menu_->PopupAsContext(triggering_event_time_);
}

bool RenderViewContextMenuGtk::IsCommandEnabled(int id) const {
  return IsItemCommandEnabled(id);
}

bool RenderViewContextMenuGtk::IsItemChecked(int id) const {
  return ItemIsChecked(id);
}

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

std::string RenderViewContextMenuGtk::GetLabel(int id) const {
  std::map<int, std::string>::const_iterator label = label_map_.find(id);

  if (label != label_map_.end())
    return label->second;

  return std::string();
}

void RenderViewContextMenuGtk::AppendMenuItem(int id) {
  AppendItem(id, std::wstring(), MENU_NORMAL);
}

void RenderViewContextMenuGtk::AppendMenuItem(int id,
    const std::wstring& label) {
  AppendItem(id, label, MENU_NORMAL);
}

void RenderViewContextMenuGtk::AppendRadioMenuItem(int id,
    const std::wstring& label) {
  AppendItem(id, label, MENU_RADIO);
}

void RenderViewContextMenuGtk::AppendCheckboxMenuItem(int id,
    const std::wstring& label) {
  AppendItem(id, label, MENU_CHECKBOX);
}

void RenderViewContextMenuGtk::AppendSeparator() {
  AppendItem(0, std::wstring(), MENU_SEPARATOR);
}

void RenderViewContextMenuGtk::StartSubMenu(int id, const std::wstring& label) {
  AppendItem(id, label, MENU_NORMAL);
  making_submenu_ = true;
}

void RenderViewContextMenuGtk::FinishSubMenu() {
  DoneMakingMenu(&submenu_);
  menu_[menu_.size() - 1].submenu = submenu_.data();
  making_submenu_ = false;
}

void RenderViewContextMenuGtk::AppendItem(
    int id, const std::wstring& label, MenuItemType type) {
  MenuCreateMaterial menu_create_material = {
    type, id, 0, 0, NULL
  };

  if (label.empty())
    menu_create_material.label_id = id;
  else
    label_map_[id] = WideToUTF8(label);

  std::vector<MenuCreateMaterial>* menu =
      making_submenu_ ? &submenu_ : &menu_;
  menu->push_back(menu_create_material);
}

// static
void RenderViewContextMenuGtk::DoneMakingMenu(
    std::vector<MenuCreateMaterial>* menu) {
  static MenuCreateMaterial end_menu_item = {
    MENU_END, 0, 0, 0, NULL
  };
  menu->push_back(end_menu_item);
}