forked from nwjs/nw.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu_views.cc
More file actions
271 lines (231 loc) · 8.57 KB
/
menu_views.cc
File metadata and controls
271 lines (231 loc) · 8.57 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright (c) 2012 Intel Corp
// Copyright (c) 2012 The Chromium Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
// pies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in al
// l copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "content/nw/src/api/menu/menu.h"
#include "base/values.h"
#include "base/strings/utf_string_conversions.h"
#include "content/nw/src/api/dispatcher_host.h"
#include "content/nw/src/api/menuitem/menuitem.h"
#include "content/nw/src/browser/native_window_aura.h"
#include "content/nw/src/nw_shell.h"
#include "content/public/browser/web_contents.h"
#include "skia/ext/image_operations.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/widget/widget.h"
#include "ui/views/focus/focus_manager.h"
#include "vector"
#if defined(OS_WIN)
#include "ui/gfx/gdi_util.h"
#include "ui/gfx/icon_util.h"
#include "ui/views/controls/menu/menu_2.h"
#endif
namespace {
#if defined(OS_WIN)
HBITMAP GetNativeBitmapFromSkBitmap(const SkBitmap& bitmap) {
int width = bitmap.width();
int height = bitmap.height();
BITMAPV4HEADER native_bitmap_header;
gfx::CreateBitmapV4Header(width, height, &native_bitmap_header);
HDC dc = ::GetDC(NULL);
void* bits;
HBITMAP native_bitmap = ::CreateDIBSection(dc,
reinterpret_cast<BITMAPINFO*>(&native_bitmap_header),
DIB_RGB_COLORS,
&bits,
NULL,
0);
DCHECK(native_bitmap);
::ReleaseDC(NULL, dc);
bitmap.copyPixelsTo(bits, width * height * 4, width * 4);
return native_bitmap;
}
#endif
} // namespace
namespace ui {
NwMenuModel::NwMenuModel(Delegate* delegate) : SimpleMenuModel(delegate) {
}
bool NwMenuModel::HasIcons() const {
// Always return false, see the comment about |NwMenuModel|.
return false;
}
} // namespace ui
namespace nwapi {
#if defined(OS_WIN)
// The width of the icon for the menuitem
static const int kIconWidth = 16;
// The height of the icon for the menuitem
static const int kIconHeight = 16;
#endif
void Menu::Create(const base::DictionaryValue& option) {
is_menu_modified_ = true;
menu_delegate_.reset(new MenuDelegate(dispatcher_host()));
menu_model_.reset(new ui::NwMenuModel(menu_delegate_.get()));
#if defined(OS_WIN)
menu_.reset(new views::NativeMenuWin(menu_model_.get(), NULL));
#endif
focus_manager_ = NULL;
window_ = NULL;
std::string type;
#if defined(OS_WIN)
if (option.GetString("type", &type) && type == "menubar")
menu_->set_is_popup_menu(false);
#endif
menu_items_.empty();
}
void Menu::Destroy() {
#if defined(OS_WIN)
for (size_t index = 0; index < icon_bitmaps_.size(); ++index) {
::DeleteObject(icon_bitmaps_[index]);
}
#endif
}
void Menu::Append(MenuItem* menu_item) {
if (menu_item->submenu_)
menu_model_->AddSubMenu(menu_item->id(), menu_item->label_,
menu_item->submenu_->menu_model_.get());
else if (menu_item->type_ == "normal")
menu_model_->AddItem(menu_item->id(), menu_item->label_);
else if (menu_item->type_ == "checkbox")
menu_model_->AddCheckItem(menu_item->id(), menu_item->label_);
else if (menu_item->type_ == "separator")
menu_model_->AddSeparator(ui::NORMAL_SEPARATOR);
is_menu_modified_ = true;
menu_items_.push_back(menu_item);
menu_item->menu_ = this;
}
void Menu::Insert(MenuItem* menu_item, int pos) {
if (menu_item->submenu_)
menu_model_->InsertSubMenuAt(pos, menu_item->id(), menu_item->label_,
menu_item->submenu_->menu_model_.get());
else if (menu_item->type_ == "normal")
menu_model_->InsertItemAt(pos, menu_item->id(), menu_item->label_);
else if (menu_item->type_ == "checkbox")
menu_model_->InsertCheckItemAt(pos, menu_item->id(), menu_item->label_);
else if (menu_item->type_ == "separator")
menu_model_->InsertSeparatorAt(pos, ui::NORMAL_SEPARATOR);
is_menu_modified_ = true;
menu_item->menu_ = this;
}
void Menu::Remove(MenuItem* menu_item, int pos) {
menu_model_->RemoveItemAt(pos);
is_menu_modified_ = true;
menu_item->menu_ = NULL;
}
void Menu::Popup(int x, int y, content::Shell* shell) {
// Rebuild();
// Map point from document to screen.
gfx::Point screen_point(x, y);
// Convert from content coordinates to window coordinates.
// This code copied from chrome_web_contents_view_delegate_views.cc
aura::Window* web_contents_window =
shell->web_contents()->GetNativeView();
aura::Window* root_window = web_contents_window->GetRootWindow();
aura::client::ScreenPositionClient* screen_position_client =
aura::client::GetScreenPositionClient(root_window);
if (screen_position_client) {
screen_position_client->ConvertPointToScreen(web_contents_window,
&screen_point);
}
views::MenuRunner runner(menu_model_.get(), views::MenuRunner::CONTEXT_MENU);
if (views::MenuRunner::MENU_DELETED ==
runner.RunMenuAt(static_cast<nw::NativeWindowAura*>(shell->window())->window(),
NULL,
gfx::Rect(screen_point, gfx::Size()),
views::MENU_ANCHOR_TOPRIGHT,
ui::MENU_SOURCE_NONE))
return;
// menu_->RunMenuAt(screen_point, views::Menu2::ALIGN_TOPLEFT);
}
#if defined(OS_WIN)
void Menu::Rebuild(const HMENU *parent_menu) {
if (is_menu_modified_) {
// Refresh menu before show.
menu_->Rebuild(NULL);
menu_->UpdateStates();
for (size_t index = 0; index < icon_bitmaps_.size(); ++index) {
::DeleteObject(icon_bitmaps_[index]);
}
icon_bitmaps_.clear();
HMENU native_menu = parent_menu == NULL ?
menu_->GetNativeMenu() : *parent_menu;
for (int model_index = 0;
model_index < menu_model_->GetItemCount();
++model_index) {
int command_id = menu_model_->GetCommandIdAt(model_index);
if (menu_model_->GetTypeAt(model_index) == ui::MenuModel::TYPE_COMMAND ||
menu_model_->GetTypeAt(model_index) == ui::MenuModel::TYPE_SUBMENU) {
gfx::Image icon;
menu_model_->GetIconAt(model_index, &icon);
if (!icon.IsEmpty()) {
SkBitmap resized_bitmap =
skia::ImageOperations::Resize(*icon.ToSkBitmap(),
skia::ImageOperations::RESIZE_GOOD,
kIconWidth,
kIconHeight);
HBITMAP icon_bitmap = GetNativeBitmapFromSkBitmap(resized_bitmap);
::SetMenuItemBitmaps(native_menu, command_id, MF_BYCOMMAND,
icon_bitmap, icon_bitmap);
icon_bitmaps_.push_back(icon_bitmap);
}
}
MenuItem* item = dispatcher_host()->GetApiObject<MenuItem>(command_id);
if (item != NULL && item->submenu_) {
item->submenu_->Rebuild(&native_menu);
}
}
is_menu_modified_ = false;
}
}
#endif
void Menu::UpdateKeys(views::FocusManager *focus_manager){
if (focus_manager == NULL){
return ;
} else {
focus_manager_ = focus_manager;
std::vector<MenuItem*>::iterator it = menu_items_.begin();
while(it!=menu_items_.end()){
(*it)->UpdateKeys(focus_manager);
++it;
}
}
}
void Menu::UpdateStates() {
#if defined(OS_WIN)
if (window_)
window_->menu_->menu_->UpdateStates();
#endif
}
#if defined(OS_WIN)
void Menu::SetWindow(nw::NativeWindowAura* win) {
window_ = win;
for (int model_index = 0;
model_index < menu_model_->GetItemCount();
++model_index) {
int command_id = menu_model_->GetCommandIdAt(model_index);
MenuItem* item = dispatcher_host()->GetApiObject<MenuItem>(command_id);
if (item != NULL && item->submenu_) {
item->submenu_->SetWindow(win);
}
}
}
#endif
} // namespace nwapi