summaryrefslogtreecommitdiffstats
path: root/views/controls/menu/native_menu_views.cc
blob: 10d3ccf1c25eab437a72be7c2ef5d2fc497e2a8e (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) 2011 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 "views/controls/menu/native_menu_views.h"

#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "ui/base/models/menu_model.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/skia_util.h"
#include "views/controls/menu/menu_2.h"
#include "views/controls/menu/menu_item_view.h"
#include "views/controls/menu/menu_listener.h"
#include "views/controls/menu/menu_runner.h"
#include "views/controls/menu/submenu_view.h"

namespace views {

NativeMenuViews::NativeMenuViews(Menu2* menu)
    : model_(menu->model()),
      ALLOW_THIS_IN_INITIALIZER_LIST(root_(new MenuItemView(this))),
      menu_runner_(new MenuRunner(root_)) {
}

NativeMenuViews::~NativeMenuViews() {
}

// MenuWrapper implementation:
void NativeMenuViews::RunMenuAt(const gfx::Point& point, int alignment) {
  // TODO: this should really return the value from MenuRunner.
  UpdateStates();
  if (menu_runner_->RunMenuAt(NULL, NULL, gfx::Rect(point, gfx::Size()),
          alignment == Menu2::ALIGN_TOPLEFT ? MenuItemView::TOPLEFT :
          MenuItemView::TOPRIGHT, MenuRunner::HAS_MNEMONICS) ==
      MenuRunner::MENU_DELETED)
    return;
}

void NativeMenuViews::CancelMenu() {
  NOTIMPLEMENTED();
}

void NativeMenuViews::Rebuild() {
  if (SubmenuView* submenu = root_->GetSubmenu())
    submenu->RemoveAllChildViews(true);
  AddMenuItemsFromModel(root_, model_);
}

void NativeMenuViews::UpdateStates() {
  SubmenuView* submenu = root_->CreateSubmenu();
  UpdateMenuFromModel(submenu, model_);
}

gfx::NativeMenu NativeMenuViews::GetNativeMenu() const {
  NOTIMPLEMENTED();
  return NULL;
}

MenuWrapper::MenuAction NativeMenuViews::GetMenuAction() const {
  NOTIMPLEMENTED();
  return MENU_ACTION_NONE;
}

void NativeMenuViews::AddMenuListener(MenuListener* listener) {
  NOTIMPLEMENTED();
}

void NativeMenuViews::RemoveMenuListener(MenuListener* listener) {
  NOTIMPLEMENTED();
}

void NativeMenuViews::SetMinimumWidth(int width) {
  NOTIMPLEMENTED();
}

// MenuDelegate implementation

bool NativeMenuViews::IsItemChecked(int cmd) const {
  int index;
  ui::MenuModel* model = model_;
  if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index))
    return false;
  return model->IsItemCheckedAt(index);
}

bool NativeMenuViews::IsCommandEnabled(int cmd) const {
  int index;
  ui::MenuModel* model = model_;
  if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index))
    return false;
  return model->IsEnabledAt(index);
}

void NativeMenuViews::ExecuteCommand(int cmd) {
  int index;
  ui::MenuModel* model = model_;
  if (!ui::MenuModel::GetModelAndIndexForCommandId(cmd, &model, &index))
    return;
  model->ActivatedAt(index);
}

bool NativeMenuViews::GetAccelerator(int id, views::Accelerator* accelerator) {
  int index;
  ui::MenuModel* model = model_;
  if (!ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index))
    return false;

  ui::Accelerator menu_accelerator;
  if (!model->GetAcceleratorAt(index, &menu_accelerator))
    return false;

  *accelerator = views::Accelerator(menu_accelerator.key_code(),
                                    menu_accelerator.modifiers());
  return true;
}

// private
void NativeMenuViews::AddMenuItemsFromModel(MenuItemView* parent,
                                            ui::MenuModel* model) {
  for (int i = 0; i < model->GetItemCount(); ++i) {
    int index = i + model->GetFirstItemIndex(NULL);
    MenuItemView* child = parent->AppendMenuItemFromModel(model, index,
        model->GetCommandIdAt(index));

    if (child && child->GetType() == MenuItemView::SUBMENU) {
      AddMenuItemsFromModel(child, model->GetSubmenuModelAt(index));
    }
  }
}

void NativeMenuViews::UpdateMenuFromModel(SubmenuView* menu,
                                          ui::MenuModel* model) {
  for (int i = 0, sep = 0; i < model->GetItemCount(); ++i) {
    int index = i + model->GetFirstItemIndex(NULL);
    if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SEPARATOR) {
      ++sep;
      continue;
    }

    // The submenu excludes the separators when counting the menu-items
    // in it. So exclude the number of separators to get the correct index.
    MenuItemView* mitem = menu->GetMenuItemAt(index - sep);
    mitem->SetVisible(model->IsVisibleAt(index));
    mitem->SetEnabled(model->IsEnabledAt(index));
    if (model->IsItemDynamicAt(index)) {
      mitem->SetTitle(model->GetLabelAt(index));
    }

    SkBitmap icon;
    if (model->GetIconAt(index, &icon)) {
      // TODO(atwilson): Support removing the icon dynamically
      // (http://crbug.com/66508).
      mitem->SetIcon(icon);
    }

    if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SUBMENU) {
      DCHECK(mitem->HasSubmenu());
      UpdateMenuFromModel(mitem->GetSubmenu(), model->GetSubmenuModelAt(index));
    }
  }
}

// MenuWrapper, public:

// static
MenuWrapper* MenuWrapper::CreateWrapper(Menu2* menu) {
  return new NativeMenuViews(menu);
}

}  // namespace views