2016-01-06 20:20:54 +01:00
|
|
|
// Copyright (c) 2012 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 "libcef/browser/native/native_menu_win.h"
|
|
|
|
|
|
|
|
#include "libcef/browser/native/menu_2.h"
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/logging.h"
|
2016-11-23 21:54:29 +01:00
|
|
|
#include "base/memory/ptr_util.h"
|
2016-01-06 20:20:54 +01:00
|
|
|
#include "base/stl_util.h"
|
|
|
|
#include "base/strings/string_util.h"
|
2019-06-05 16:15:45 +02:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2016-10-21 21:52:29 +02:00
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2016-01-06 20:20:54 +01:00
|
|
|
#include "base/win/wrapped_window_proc.h"
|
2017-03-03 23:37:23 +01:00
|
|
|
#include "skia/ext/platform_canvas.h"
|
2017-01-23 18:36:54 +01:00
|
|
|
#include "skia/ext/skia_utils_win.h"
|
2016-01-06 20:20:54 +01:00
|
|
|
#include "ui/base/accelerators/accelerator.h"
|
|
|
|
#include "ui/base/l10n/l10n_util.h"
|
|
|
|
#include "ui/base/l10n/l10n_util_win.h"
|
2020-06-09 19:48:00 +02:00
|
|
|
#include "ui/base/models/image_model.h"
|
2016-01-06 20:20:54 +01:00
|
|
|
#include "ui/base/models/menu_model.h"
|
|
|
|
#include "ui/events/keycodes/keyboard_codes.h"
|
|
|
|
#include "ui/gfx/canvas.h"
|
|
|
|
#include "ui/gfx/font_list.h"
|
|
|
|
#include "ui/gfx/geometry/rect.h"
|
|
|
|
#include "ui/gfx/image/image.h"
|
|
|
|
#include "ui/gfx/image/image_skia.h"
|
|
|
|
#include "ui/gfx/text_utils.h"
|
|
|
|
#include "ui/gfx/win/hwnd_util.h"
|
|
|
|
#include "ui/native_theme/native_theme.h"
|
2020-06-09 19:48:00 +02:00
|
|
|
#include "ui/native_theme/themed_vector_icon.h"
|
2016-01-06 20:20:54 +01:00
|
|
|
#include "ui/views/controls/menu/menu_config.h"
|
|
|
|
#include "ui/views/controls/menu/menu_insertion_delegate_win.h"
|
|
|
|
|
|
|
|
using ui::NativeTheme;
|
|
|
|
|
|
|
|
namespace views {
|
|
|
|
|
|
|
|
// The width of an icon, including the pixels between the icon and
|
|
|
|
// the item label.
|
|
|
|
static const int kIconWidth = 23;
|
|
|
|
// Margins between the top of the item and the label.
|
|
|
|
static const int kItemTopMargin = 3;
|
|
|
|
// Margins between the bottom of the item and the label.
|
|
|
|
static const int kItemBottomMargin = 4;
|
|
|
|
// Margins between the left of the item and the icon.
|
|
|
|
static const int kItemLeftMargin = 4;
|
|
|
|
// The width for displaying the sub-menu arrow.
|
|
|
|
static const int kArrowWidth = 10;
|
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
// Horizontal spacing between the end of an item (i.e. an icon or a checkbox)
|
|
|
|
// and the start of its corresponding text.
|
|
|
|
constexpr int kItemLabelSpacing = 10;
|
|
|
|
|
2017-01-23 18:36:54 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Draws the top layer of the canvas into the specified HDC. Only works
|
|
|
|
// with a SkCanvas with a BitmapPlatformDevice. Will create a temporary
|
|
|
|
// HDC to back the canvas if one doesn't already exist, tearing it down
|
|
|
|
// before returning. If |src_rect| is null, copies the entire canvas.
|
|
|
|
// Deleted from skia/ext/platform_canvas.h in https://crbug.com/675977#c13
|
2017-05-17 11:29:28 +02:00
|
|
|
void DrawToNativeContext(SkCanvas* canvas,
|
|
|
|
HDC destination_hdc,
|
|
|
|
int x,
|
|
|
|
int y,
|
2017-01-23 18:36:54 +01:00
|
|
|
const RECT* src_rect) {
|
|
|
|
RECT temp_rect;
|
|
|
|
if (!src_rect) {
|
|
|
|
temp_rect.left = 0;
|
|
|
|
temp_rect.right = canvas->imageInfo().width();
|
|
|
|
temp_rect.top = 0;
|
|
|
|
temp_rect.bottom = canvas->imageInfo().height();
|
|
|
|
src_rect = &temp_rect;
|
|
|
|
}
|
|
|
|
skia::CopyHDC(skia::GetNativeDrawingContext(canvas), destination_hdc, x, y,
|
|
|
|
canvas->imageInfo().isOpaque(), *src_rect,
|
|
|
|
canvas->getTotalMatrix());
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:15:45 +02:00
|
|
|
HFONT CreateNativeFont(const gfx::Font& font) {
|
|
|
|
// Extracts |fonts| properties.
|
|
|
|
const DWORD italic = (font.GetStyle() & gfx::Font::ITALIC) ? TRUE : FALSE;
|
|
|
|
const DWORD underline =
|
|
|
|
(font.GetStyle() & gfx::Font::UNDERLINE) ? TRUE : FALSE;
|
|
|
|
// The font mapper matches its absolute value against the character height of
|
|
|
|
// the available fonts.
|
|
|
|
const int height = -font.GetFontSize();
|
|
|
|
|
|
|
|
// Select the primary font which forces a mapping to a physical font.
|
|
|
|
return ::CreateFont(height, 0, 0, 0, static_cast<int>(font.GetWeight()),
|
|
|
|
italic, underline, FALSE, DEFAULT_CHARSET,
|
|
|
|
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
|
|
|
|
DEFAULT_PITCH | FF_DONTCARE,
|
2021-04-21 00:52:34 +02:00
|
|
|
base::UTF8ToWide(font.GetFontName()).c_str());
|
2019-06-05 16:15:45 +02:00
|
|
|
}
|
|
|
|
|
2017-01-23 18:36:54 +01:00
|
|
|
} // namespace
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
struct CefNativeMenuWin::ItemData {
|
2016-01-06 20:20:54 +01:00
|
|
|
// The Windows API requires that whoever creates the menus must own the
|
|
|
|
// strings used for labels, and keep them around for the lifetime of the
|
|
|
|
// created menu. So be it.
|
2021-04-21 00:52:34 +02:00
|
|
|
std::wstring label;
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
// Someone needs to own submenus, it may as well be us.
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<Menu2> submenu;
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
// We need a pointer back to the containing menu in various circumstances.
|
2016-07-14 03:35:07 +02:00
|
|
|
CefNativeMenuWin* native_menu_win;
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
// The index of the item within the menu's model.
|
|
|
|
int model_index;
|
|
|
|
};
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
// Returns the CefNativeMenuWin for a particular HMENU.
|
|
|
|
static CefNativeMenuWin* GetCefNativeMenuWinFromHMENU(HMENU hmenu) {
|
2016-01-06 20:20:54 +01:00
|
|
|
MENUINFO mi = {0};
|
|
|
|
mi.cbSize = sizeof(mi);
|
|
|
|
mi.fMask = MIM_MENUDATA | MIM_STYLE;
|
|
|
|
GetMenuInfo(hmenu, &mi);
|
2016-07-14 03:35:07 +02:00
|
|
|
return reinterpret_cast<CefNativeMenuWin*>(mi.dwMenuData);
|
2016-01-06 20:20:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A window that receives messages from Windows relevant to the native menu
|
2016-07-14 03:35:07 +02:00
|
|
|
// structure we have constructed in CefNativeMenuWin.
|
|
|
|
class CefNativeMenuWin::MenuHostWindow {
|
2016-01-06 20:20:54 +01:00
|
|
|
public:
|
2017-12-07 22:44:24 +01:00
|
|
|
MenuHostWindow() {
|
2016-01-06 20:20:54 +01:00
|
|
|
RegisterClass();
|
|
|
|
hwnd_ = CreateWindowEx(l10n_util::GetExtendedStyles(), kWindowClassName,
|
|
|
|
L"", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
|
2020-07-08 19:23:29 +02:00
|
|
|
gfx::CheckWindowCreated(hwnd_, ::GetLastError());
|
2016-01-06 20:20:54 +01:00
|
|
|
gfx::SetWindowUserData(hwnd_, this);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
~MenuHostWindow() { DestroyWindow(hwnd_); }
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
HWND hwnd() const { return hwnd_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const wchar_t* kWindowClassName;
|
|
|
|
|
|
|
|
void RegisterClass() {
|
|
|
|
static bool registered = false;
|
|
|
|
if (registered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
WNDCLASSEX window_class;
|
|
|
|
base::win::InitializeWindowClass(
|
2017-05-17 11:29:28 +02:00
|
|
|
kWindowClassName, &base::win::WrappedWindowProc<MenuHostWindowProc>,
|
|
|
|
CS_DBLCLKS, 0, 0, NULL, reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1),
|
|
|
|
NULL, NULL, NULL, &window_class);
|
2016-01-06 20:20:54 +01:00
|
|
|
ATOM clazz = RegisterClassEx(&window_class);
|
|
|
|
CHECK(clazz);
|
|
|
|
registered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts the WPARAM value passed to WM_MENUSELECT into an index
|
|
|
|
// corresponding to the menu item that was selected.
|
|
|
|
int GetMenuItemIndexFromWPARAM(HMENU menu, WPARAM w_param) const {
|
|
|
|
int count = GetMenuItemCount(menu);
|
|
|
|
// For normal command menu items, Windows passes a command id as the LOWORD
|
|
|
|
// of WPARAM for WM_MENUSELECT. We need to walk forward through the menu
|
|
|
|
// items to find an item with a matching ID. Ugh!
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
MENUITEMINFO mii = {0};
|
|
|
|
mii.cbSize = sizeof(mii);
|
|
|
|
mii.fMask = MIIM_ID;
|
|
|
|
GetMenuItemInfo(menu, i, MF_BYPOSITION, &mii);
|
|
|
|
if (mii.wID == w_param)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
// If we didn't find a matching command ID, this means a submenu has been
|
|
|
|
// selected instead, and rather than passing a command ID in
|
|
|
|
// LOWORD(w_param), Windows has actually passed us a position, so we just
|
|
|
|
// return it.
|
|
|
|
return w_param;
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
CefNativeMenuWin::ItemData* GetItemData(ULONG_PTR item_data) {
|
|
|
|
return reinterpret_cast<CefNativeMenuWin::ItemData*>(item_data);
|
2016-01-06 20:20:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the user selects a specific item.
|
|
|
|
void OnMenuCommand(int position, HMENU menu) {
|
2016-07-14 03:35:07 +02:00
|
|
|
CefNativeMenuWin* menu_win = GetCefNativeMenuWinFromHMENU(menu);
|
2016-01-06 20:20:54 +01:00
|
|
|
ui::MenuModel* model = menu_win->model_;
|
2016-07-14 03:35:07 +02:00
|
|
|
CefNativeMenuWin* root_menu = menu_win;
|
2016-01-06 20:20:54 +01:00
|
|
|
while (root_menu->parent_)
|
|
|
|
root_menu = root_menu->parent_;
|
|
|
|
|
|
|
|
// Only notify the model if it didn't already send out notification.
|
|
|
|
// See comment in MenuMessageHook for details.
|
|
|
|
if (root_menu->menu_action_ == MenuWrapper::MENU_ACTION_NONE)
|
|
|
|
model->ActivatedAt(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by Windows to measure the size of an owner-drawn menu item.
|
|
|
|
void OnMeasureItem(WPARAM w_param, MEASUREITEMSTRUCT* measure_item_struct) {
|
2016-07-14 03:35:07 +02:00
|
|
|
CefNativeMenuWin::ItemData* data =
|
|
|
|
GetItemData(measure_item_struct->itemData);
|
2016-01-06 20:20:54 +01:00
|
|
|
if (data) {
|
|
|
|
gfx::FontList font_list;
|
|
|
|
measure_item_struct->itemWidth =
|
2021-04-21 00:52:34 +02:00
|
|
|
gfx::GetStringWidth(base::WideToUTF16(data->label), font_list) +
|
|
|
|
kIconWidth + kItemLeftMargin + kItemLabelSpacing -
|
2016-01-06 20:20:54 +01:00
|
|
|
GetSystemMetrics(SM_CXMENUCHECK);
|
|
|
|
if (data->submenu.get())
|
|
|
|
measure_item_struct->itemWidth += kArrowWidth;
|
|
|
|
// If the label contains an accelerator, make room for tab.
|
2021-04-21 00:52:34 +02:00
|
|
|
if (data->label.find(L'\t') != std::wstring::npos)
|
|
|
|
measure_item_struct->itemWidth += gfx::GetStringWidth(u" ", font_list);
|
2016-01-06 20:20:54 +01:00
|
|
|
measure_item_struct->itemHeight =
|
|
|
|
font_list.GetHeight() + kItemBottomMargin + kItemTopMargin;
|
|
|
|
} else {
|
|
|
|
// Measure separator size.
|
|
|
|
measure_item_struct->itemHeight = GetSystemMetrics(SM_CYMENU) / 2;
|
|
|
|
measure_item_struct->itemWidth = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by Windows to paint an owner-drawn menu item.
|
|
|
|
void OnDrawItem(UINT w_param, DRAWITEMSTRUCT* draw_item_struct) {
|
|
|
|
HDC dc = draw_item_struct->hDC;
|
|
|
|
COLORREF prev_bg_color, prev_text_color;
|
|
|
|
|
|
|
|
// Set background color and text color
|
|
|
|
if (draw_item_struct->itemState & ODS_SELECTED) {
|
|
|
|
prev_bg_color = SetBkColor(dc, GetSysColor(COLOR_HIGHLIGHT));
|
|
|
|
prev_text_color = SetTextColor(dc, GetSysColor(COLOR_HIGHLIGHTTEXT));
|
|
|
|
} else {
|
|
|
|
prev_bg_color = SetBkColor(dc, GetSysColor(COLOR_MENU));
|
|
|
|
if (draw_item_struct->itemState & ODS_DISABLED)
|
|
|
|
prev_text_color = SetTextColor(dc, GetSysColor(COLOR_GRAYTEXT));
|
|
|
|
else
|
|
|
|
prev_text_color = SetTextColor(dc, GetSysColor(COLOR_MENUTEXT));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (draw_item_struct->itemData) {
|
2016-07-14 03:35:07 +02:00
|
|
|
CefNativeMenuWin::ItemData* data =
|
|
|
|
GetItemData(draw_item_struct->itemData);
|
2016-01-06 20:20:54 +01:00
|
|
|
// Draw the background.
|
|
|
|
HBRUSH hbr = CreateSolidBrush(GetBkColor(dc));
|
|
|
|
FillRect(dc, &draw_item_struct->rcItem, hbr);
|
|
|
|
DeleteObject(hbr);
|
|
|
|
|
|
|
|
// Draw the label.
|
|
|
|
RECT rect = draw_item_struct->rcItem;
|
|
|
|
rect.top += kItemTopMargin;
|
|
|
|
// Should we add kIconWidth only when icon.width() != 0 ?
|
|
|
|
rect.left += kItemLeftMargin + kIconWidth;
|
2017-07-27 01:19:27 +02:00
|
|
|
rect.right -= kItemLabelSpacing;
|
2016-01-06 20:20:54 +01:00
|
|
|
UINT format = DT_TOP | DT_SINGLELINE;
|
|
|
|
// Check whether the mnemonics should be underlined.
|
|
|
|
BOOL underline_mnemonics;
|
|
|
|
SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &underline_mnemonics, 0);
|
|
|
|
if (!underline_mnemonics)
|
|
|
|
format |= DT_HIDEPREFIX;
|
|
|
|
gfx::FontList font_list;
|
2019-06-05 16:15:45 +02:00
|
|
|
HFONT new_font = CreateNativeFont(font_list.GetPrimaryFont());
|
|
|
|
HGDIOBJ old_font = SelectObject(dc, new_font);
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
// If an accelerator is specified (with a tab delimiting the rest of the
|
|
|
|
// label from the accelerator), we have to justify the fist part on the
|
|
|
|
// left and the accelerator on the right.
|
|
|
|
// TODO(jungshik): This will break in RTL UI. Currently, he/ar use the
|
|
|
|
// window system UI font and will not hit here.
|
2021-04-21 00:52:34 +02:00
|
|
|
std::wstring label = data->label;
|
|
|
|
std::wstring accel;
|
|
|
|
std::wstring::size_type tab_pos = label.find(L'\t');
|
|
|
|
if (tab_pos != std::wstring::npos) {
|
2016-01-06 20:20:54 +01:00
|
|
|
accel = label.substr(tab_pos);
|
|
|
|
label = label.substr(0, tab_pos);
|
|
|
|
}
|
|
|
|
DrawTextEx(dc, const_cast<wchar_t*>(label.data()),
|
|
|
|
static_cast<int>(label.size()), &rect, format | DT_LEFT, NULL);
|
2019-06-05 16:15:45 +02:00
|
|
|
if (!accel.empty()) {
|
2016-01-06 20:20:54 +01:00
|
|
|
DrawTextEx(dc, const_cast<wchar_t*>(accel.data()),
|
2017-05-17 11:29:28 +02:00
|
|
|
static_cast<int>(accel.size()), &rect, format | DT_RIGHT,
|
|
|
|
NULL);
|
2019-06-05 16:15:45 +02:00
|
|
|
}
|
2016-01-06 20:20:54 +01:00
|
|
|
SelectObject(dc, old_font);
|
2019-06-05 16:15:45 +02:00
|
|
|
DeleteObject(new_font);
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
ui::MenuModel::ItemType type =
|
|
|
|
data->native_menu_win->model_->GetTypeAt(data->model_index);
|
|
|
|
|
|
|
|
// Draw the icon after the label, otherwise it would be covered
|
|
|
|
// by the label.
|
2020-06-09 19:48:00 +02:00
|
|
|
ui::ImageModel icon =
|
|
|
|
data->native_menu_win->model_->GetIconAt(data->model_index);
|
|
|
|
if (icon.IsImage() || icon.IsVectorIcon()) {
|
|
|
|
ui::NativeTheme* native_theme =
|
|
|
|
ui::NativeTheme::GetInstanceForNativeUi();
|
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
// We currently don't support items with both icons and checkboxes.
|
2020-06-09 19:48:00 +02:00
|
|
|
const gfx::ImageSkia skia_icon =
|
|
|
|
icon.IsImage() ? icon.GetImage().AsImageSkia()
|
|
|
|
: ui::ThemedVectorIcon(icon.GetVectorIcon())
|
|
|
|
.GetImageSkia(native_theme, 16);
|
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
DCHECK(type != ui::MenuModel::TYPE_CHECK);
|
2017-04-20 21:28:17 +02:00
|
|
|
std::unique_ptr<SkCanvas> canvas = skia::CreatePlatformCanvas(
|
|
|
|
skia_icon.width(), skia_icon.height(), false);
|
2021-03-04 23:36:57 +01:00
|
|
|
canvas->drawImage(skia_icon.bitmap()->asImage(), 0, 0);
|
2017-01-23 18:36:54 +01:00
|
|
|
DrawToNativeContext(
|
2017-05-17 11:29:28 +02:00
|
|
|
canvas.get(), dc, draw_item_struct->rcItem.left + kItemLeftMargin,
|
|
|
|
draw_item_struct->rcItem.top +
|
|
|
|
(draw_item_struct->rcItem.bottom -
|
|
|
|
draw_item_struct->rcItem.top - skia_icon.height()) /
|
|
|
|
2,
|
|
|
|
NULL);
|
2016-01-06 20:20:54 +01:00
|
|
|
} else if (type == ui::MenuModel::TYPE_CHECK &&
|
|
|
|
data->native_menu_win->model_->IsItemCheckedAt(
|
|
|
|
data->model_index)) {
|
|
|
|
// Manually render a checkbox.
|
|
|
|
const MenuConfig& config = MenuConfig::instance();
|
|
|
|
NativeTheme::State state;
|
|
|
|
if (draw_item_struct->itemState & ODS_DISABLED) {
|
|
|
|
state = NativeTheme::kDisabled;
|
|
|
|
} else {
|
2017-05-17 11:29:28 +02:00
|
|
|
state = draw_item_struct->itemState & ODS_SELECTED
|
|
|
|
? NativeTheme::kHovered
|
|
|
|
: NativeTheme::kNormal;
|
2016-01-06 20:20:54 +01:00
|
|
|
}
|
2017-04-20 21:28:17 +02:00
|
|
|
|
|
|
|
std::unique_ptr<SkCanvas> canvas = skia::CreatePlatformCanvas(
|
|
|
|
config.check_width, config.check_height, false);
|
|
|
|
cc::SkiaPaintCanvas paint_canvas(canvas.get());
|
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
NativeTheme::ExtraParams extra;
|
|
|
|
extra.menu_check.is_radio = false;
|
|
|
|
gfx::Rect bounds(0, 0, config.check_width, config.check_height);
|
|
|
|
|
|
|
|
// Draw the background and the check.
|
2017-01-23 18:36:54 +01:00
|
|
|
ui::NativeTheme* native_theme =
|
|
|
|
ui::NativeTheme::GetInstanceForNativeUi();
|
2017-05-17 11:29:28 +02:00
|
|
|
native_theme->Paint(&paint_canvas, NativeTheme::kMenuCheckBackground,
|
|
|
|
state, bounds, extra);
|
|
|
|
native_theme->Paint(&paint_canvas, NativeTheme::kMenuCheck, state,
|
|
|
|
bounds, extra);
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
// Draw checkbox to menu.
|
2017-05-17 11:29:28 +02:00
|
|
|
DrawToNativeContext(
|
|
|
|
canvas.get(), dc, draw_item_struct->rcItem.left + kItemLeftMargin,
|
|
|
|
draw_item_struct->rcItem.top +
|
|
|
|
(draw_item_struct->rcItem.bottom -
|
|
|
|
draw_item_struct->rcItem.top - config.check_height) /
|
|
|
|
2,
|
|
|
|
NULL);
|
2016-01-06 20:20:54 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Draw the separator
|
|
|
|
draw_item_struct->rcItem.top +=
|
|
|
|
(draw_item_struct->rcItem.bottom - draw_item_struct->rcItem.top) / 3;
|
|
|
|
DrawEdge(dc, &draw_item_struct->rcItem, EDGE_ETCHED, BF_TOP);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetBkColor(dc, prev_bg_color);
|
|
|
|
SetTextColor(dc, prev_text_color);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessWindowMessage(HWND window,
|
|
|
|
UINT message,
|
|
|
|
WPARAM w_param,
|
|
|
|
LPARAM l_param,
|
|
|
|
LRESULT* l_result) {
|
|
|
|
switch (message) {
|
|
|
|
case WM_MENUCOMMAND:
|
|
|
|
OnMenuCommand(w_param, reinterpret_cast<HMENU>(l_param));
|
|
|
|
*l_result = 0;
|
|
|
|
return true;
|
|
|
|
case WM_MENUSELECT:
|
|
|
|
*l_result = 0;
|
|
|
|
return true;
|
|
|
|
case WM_MEASUREITEM:
|
|
|
|
OnMeasureItem(w_param, reinterpret_cast<MEASUREITEMSTRUCT*>(l_param));
|
|
|
|
*l_result = 0;
|
|
|
|
return true;
|
|
|
|
case WM_DRAWITEM:
|
|
|
|
OnDrawItem(w_param, reinterpret_cast<DRAWITEMSTRUCT*>(l_param));
|
|
|
|
*l_result = 0;
|
|
|
|
return true;
|
2017-05-17 11:29:28 +02:00
|
|
|
// TODO(beng): bring over owner draw from old menu system.
|
2016-01-06 20:20:54 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LRESULT CALLBACK MenuHostWindowProc(HWND window,
|
|
|
|
UINT message,
|
|
|
|
WPARAM w_param,
|
|
|
|
LPARAM l_param) {
|
|
|
|
MenuHostWindow* host =
|
|
|
|
reinterpret_cast<MenuHostWindow*>(gfx::GetWindowUserData(window));
|
|
|
|
// host is null during initial construction.
|
|
|
|
LRESULT l_result = 0;
|
|
|
|
if (!host || !host->ProcessWindowMessage(window, message, w_param, l_param,
|
|
|
|
&l_result)) {
|
|
|
|
return DefWindowProc(window, message, w_param, l_param);
|
|
|
|
}
|
|
|
|
return l_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
HWND hwnd_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(MenuHostWindow);
|
|
|
|
};
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
struct CefNativeMenuWin::HighlightedMenuItemInfo {
|
2016-01-06 20:20:54 +01:00
|
|
|
HighlightedMenuItemInfo()
|
2020-01-15 14:36:24 +01:00
|
|
|
: has_parent(false), has_submenu(false), menu(nullptr), position(-1) {}
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
bool has_parent;
|
|
|
|
bool has_submenu;
|
|
|
|
|
|
|
|
// The menu and position. These are only set for non-disabled menu items.
|
2016-07-14 03:35:07 +02:00
|
|
|
CefNativeMenuWin* menu;
|
2016-01-06 20:20:54 +01:00
|
|
|
int position;
|
|
|
|
};
|
|
|
|
|
|
|
|
// static
|
2016-07-14 03:35:07 +02:00
|
|
|
const wchar_t* CefNativeMenuWin::MenuHostWindow::kWindowClassName =
|
2016-01-06 20:20:54 +01:00
|
|
|
L"ViewsMenuHostWindow";
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2016-07-14 03:35:07 +02:00
|
|
|
// CefNativeMenuWin, public:
|
2016-01-06 20:20:54 +01:00
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
CefNativeMenuWin::CefNativeMenuWin(ui::MenuModel* model, HWND system_menu_for)
|
2016-01-06 20:20:54 +01:00
|
|
|
: model_(model),
|
2020-01-15 14:36:24 +01:00
|
|
|
menu_(nullptr),
|
2016-01-06 20:20:54 +01:00
|
|
|
owner_draw_(l10n_util::NeedOverrideDefaultUIFont(NULL, NULL) &&
|
|
|
|
!system_menu_for),
|
|
|
|
system_menu_for_(system_menu_for),
|
|
|
|
first_item_index_(0),
|
|
|
|
menu_action_(MENU_ACTION_NONE),
|
2020-01-15 14:36:24 +01:00
|
|
|
menu_to_select_(nullptr),
|
2016-01-06 20:20:54 +01:00
|
|
|
position_to_select_(-1),
|
2020-01-15 14:36:24 +01:00
|
|
|
parent_(nullptr),
|
|
|
|
destroyed_flag_(nullptr),
|
2017-05-17 11:29:28 +02:00
|
|
|
menu_to_select_factory_(this) {}
|
2016-01-06 20:20:54 +01:00
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
CefNativeMenuWin::~CefNativeMenuWin() {
|
2016-01-06 20:20:54 +01:00
|
|
|
if (destroyed_flag_)
|
|
|
|
*destroyed_flag_ = true;
|
|
|
|
DestroyMenu(menu_);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2016-07-14 03:35:07 +02:00
|
|
|
// CefNativeMenuWin, MenuWrapper implementation:
|
2016-01-06 20:20:54 +01:00
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefNativeMenuWin::RunMenuAt(const gfx::Point& point, int alignment) {
|
2016-01-06 20:20:54 +01:00
|
|
|
CreateHostWindow();
|
|
|
|
UpdateStates();
|
|
|
|
UINT flags = TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RECURSE;
|
|
|
|
flags |= GetAlignmentFlags(alignment);
|
|
|
|
menu_action_ = MENU_ACTION_NONE;
|
|
|
|
|
|
|
|
// Set a hook function so we can listen for keyboard events while the
|
|
|
|
// menu is open, and store a pointer to this object in a static
|
|
|
|
// variable so the hook has access to it (ugly, but it's the
|
|
|
|
// only way).
|
|
|
|
open_native_menu_win_ = this;
|
|
|
|
HHOOK hhook = SetWindowsHookEx(WH_MSGFILTER, MenuMessageHook,
|
|
|
|
GetModuleHandle(NULL), ::GetCurrentThreadId());
|
|
|
|
|
|
|
|
// Command dispatch is done through WM_MENUCOMMAND, handled by the host
|
|
|
|
// window.
|
2020-01-15 14:36:24 +01:00
|
|
|
menu_to_select_ = nullptr;
|
2016-01-06 20:20:54 +01:00
|
|
|
position_to_select_ = -1;
|
|
|
|
menu_to_select_factory_.InvalidateWeakPtrs();
|
|
|
|
bool destroyed = false;
|
|
|
|
destroyed_flag_ = &destroyed;
|
|
|
|
model_->MenuWillShow();
|
|
|
|
TrackPopupMenu(menu_, flags, point.x(), point.y(), 0, host_window_->hwnd(),
|
|
|
|
NULL);
|
|
|
|
UnhookWindowsHookEx(hhook);
|
2020-01-15 14:36:24 +01:00
|
|
|
open_native_menu_win_ = nullptr;
|
2016-01-06 20:20:54 +01:00
|
|
|
if (destroyed)
|
|
|
|
return;
|
2020-01-15 14:36:24 +01:00
|
|
|
destroyed_flag_ = nullptr;
|
2016-01-06 20:20:54 +01:00
|
|
|
if (menu_to_select_) {
|
|
|
|
// Folks aren't too happy if we notify immediately. In particular, notifying
|
|
|
|
// the delegate can cause destruction leaving the stack in a weird
|
|
|
|
// state. Instead post a task, then notify. This mirrors what WM_MENUCOMMAND
|
|
|
|
// does.
|
|
|
|
menu_to_select_factory_.InvalidateWeakPtrs();
|
2016-10-21 21:52:29 +02:00
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostTask(
|
2017-05-17 11:29:28 +02:00
|
|
|
FROM_HERE, base::Bind(&CefNativeMenuWin::DelayedSelect,
|
|
|
|
menu_to_select_factory_.GetWeakPtr()));
|
2016-01-06 20:20:54 +01:00
|
|
|
menu_action_ = MENU_ACTION_SELECTED;
|
|
|
|
}
|
2016-10-17 20:14:44 +02:00
|
|
|
// Send MenuWillClose after we schedule the select, otherwise MenuWillClose is
|
|
|
|
// processed after the select (MenuWillClose posts a delayed task too).
|
|
|
|
model_->MenuWillClose();
|
2016-01-06 20:20:54 +01:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefNativeMenuWin::CancelMenu() {
|
2016-01-06 20:20:54 +01:00
|
|
|
EndMenu();
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefNativeMenuWin::Rebuild(MenuInsertionDelegateWin* delegate) {
|
2016-01-06 20:20:54 +01:00
|
|
|
ResetNativeMenu();
|
|
|
|
items_.clear();
|
|
|
|
|
|
|
|
owner_draw_ = model_->HasIcons() || owner_draw_;
|
|
|
|
first_item_index_ = delegate ? delegate->GetInsertionIndex(menu_) : 0;
|
|
|
|
for (int menu_index = first_item_index_;
|
2017-05-17 11:29:28 +02:00
|
|
|
menu_index < first_item_index_ + model_->GetItemCount(); ++menu_index) {
|
2016-01-06 20:20:54 +01:00
|
|
|
int model_index = menu_index - first_item_index_;
|
|
|
|
if (model_->GetTypeAt(model_index) == ui::MenuModel::TYPE_SEPARATOR)
|
|
|
|
AddSeparatorItemAt(menu_index, model_index);
|
|
|
|
else
|
|
|
|
AddMenuItemAt(menu_index, model_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefNativeMenuWin::UpdateStates() {
|
2016-01-06 20:20:54 +01:00
|
|
|
// A depth-first walk of the menu items, updating states.
|
|
|
|
int model_index = 0;
|
2016-11-23 21:54:29 +01:00
|
|
|
ItemDataList::const_iterator it;
|
2016-01-06 20:20:54 +01:00
|
|
|
for (it = items_.begin(); it != items_.end(); ++it, ++model_index) {
|
|
|
|
int menu_index = model_index + first_item_index_;
|
|
|
|
SetMenuItemState(menu_index, model_->IsEnabledAt(model_index),
|
|
|
|
model_->IsItemCheckedAt(model_index), false);
|
|
|
|
if (model_->IsItemDynamicAt(model_index)) {
|
|
|
|
// TODO(atwilson): Update the icon as well (http://crbug.com/66508).
|
|
|
|
SetMenuItemLabel(menu_index, model_index,
|
2021-04-21 00:52:34 +02:00
|
|
|
base::UTF16ToWide(model_->GetLabelAt(model_index)));
|
2016-01-06 20:20:54 +01:00
|
|
|
}
|
|
|
|
Menu2* submenu = (*it)->submenu.get();
|
|
|
|
if (submenu)
|
|
|
|
submenu->UpdateStates();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
HMENU CefNativeMenuWin::GetNativeMenu() const {
|
2016-01-06 20:20:54 +01:00
|
|
|
return menu_;
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
CefNativeMenuWin::MenuAction CefNativeMenuWin::GetMenuAction() const {
|
2016-01-06 20:20:54 +01:00
|
|
|
return menu_action_;
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefNativeMenuWin::SetMinimumWidth(int width) {
|
2016-01-06 20:20:54 +01:00
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2016-07-14 03:35:07 +02:00
|
|
|
// CefNativeMenuWin, private:
|
2016-01-06 20:20:54 +01:00
|
|
|
|
|
|
|
// static
|
2020-01-15 14:36:24 +01:00
|
|
|
CefNativeMenuWin* CefNativeMenuWin::open_native_menu_win_ = nullptr;
|
2016-01-06 20:20:54 +01:00
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefNativeMenuWin::DelayedSelect() {
|
2016-01-06 20:20:54 +01:00
|
|
|
if (menu_to_select_)
|
|
|
|
menu_to_select_->model_->ActivatedAt(position_to_select_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2016-07-14 03:35:07 +02:00
|
|
|
bool CefNativeMenuWin::GetHighlightedMenuItemInfo(
|
2016-01-06 20:20:54 +01:00
|
|
|
HMENU menu,
|
|
|
|
HighlightedMenuItemInfo* info) {
|
|
|
|
for (int i = 0; i < ::GetMenuItemCount(menu); i++) {
|
|
|
|
UINT state = ::GetMenuState(menu, i, MF_BYPOSITION);
|
|
|
|
if (state & MF_HILITE) {
|
|
|
|
if (state & MF_POPUP) {
|
|
|
|
HMENU submenu = GetSubMenu(menu, i);
|
|
|
|
if (GetHighlightedMenuItemInfo(submenu, info))
|
|
|
|
info->has_parent = true;
|
|
|
|
else
|
|
|
|
info->has_submenu = true;
|
|
|
|
} else if (!(state & MF_SEPARATOR) && !(state & MF_DISABLED)) {
|
2016-07-14 03:35:07 +02:00
|
|
|
info->menu = GetCefNativeMenuWinFromHMENU(menu);
|
2016-01-06 20:20:54 +01:00
|
|
|
info->position = i;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2017-05-17 11:29:28 +02:00
|
|
|
LRESULT CALLBACK CefNativeMenuWin::MenuMessageHook(int n_code,
|
|
|
|
WPARAM w_param,
|
|
|
|
LPARAM l_param) {
|
2016-01-06 20:20:54 +01:00
|
|
|
LRESULT result = CallNextHookEx(NULL, n_code, w_param, l_param);
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
CefNativeMenuWin* this_ptr = open_native_menu_win_;
|
2016-01-06 20:20:54 +01:00
|
|
|
if (!this_ptr)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
MSG* msg = reinterpret_cast<MSG*>(l_param);
|
|
|
|
if (msg->message == WM_LBUTTONUP || msg->message == WM_RBUTTONUP) {
|
|
|
|
HighlightedMenuItemInfo info;
|
|
|
|
if (GetHighlightedMenuItemInfo(this_ptr->menu_, &info) && info.menu) {
|
|
|
|
// It appears that when running a menu by way of TrackPopupMenu(Ex) win32
|
|
|
|
// gets confused if the underlying window paints itself. As its very easy
|
|
|
|
// for the underlying window to repaint itself (especially since some menu
|
|
|
|
// items trigger painting of the tabstrip on mouse over) we have this
|
|
|
|
// workaround. When the mouse is released on a menu item we remember the
|
|
|
|
// menu item and end the menu. When the nested message loop returns we
|
|
|
|
// schedule a task to notify the model. It's still possible to get a
|
|
|
|
// WM_MENUCOMMAND, so we have to be careful that we don't notify the model
|
|
|
|
// twice.
|
|
|
|
this_ptr->menu_to_select_ = info.menu;
|
|
|
|
this_ptr->position_to_select_ = info.position;
|
|
|
|
EndMenu();
|
|
|
|
}
|
|
|
|
} else if (msg->message == WM_KEYDOWN) {
|
|
|
|
HighlightedMenuItemInfo info;
|
|
|
|
if (GetHighlightedMenuItemInfo(this_ptr->menu_, &info)) {
|
|
|
|
if (msg->wParam == VK_LEFT && !info.has_parent) {
|
|
|
|
this_ptr->menu_action_ = MENU_ACTION_PREVIOUS;
|
|
|
|
::EndMenu();
|
|
|
|
} else if (msg->wParam == VK_RIGHT && !info.has_parent &&
|
|
|
|
!info.has_submenu) {
|
|
|
|
this_ptr->menu_action_ = MENU_ACTION_NEXT;
|
|
|
|
::EndMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
bool CefNativeMenuWin::IsSeparatorItemAt(int menu_index) const {
|
2016-01-06 20:20:54 +01:00
|
|
|
MENUITEMINFO mii = {0};
|
|
|
|
mii.cbSize = sizeof(mii);
|
|
|
|
mii.fMask = MIIM_FTYPE;
|
|
|
|
GetMenuItemInfo(menu_, menu_index, MF_BYPOSITION, &mii);
|
|
|
|
return !!(mii.fType & MF_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefNativeMenuWin::AddMenuItemAt(int menu_index, int model_index) {
|
2016-01-06 20:20:54 +01:00
|
|
|
MENUITEMINFO mii = {0};
|
|
|
|
mii.cbSize = sizeof(mii);
|
|
|
|
mii.fMask = MIIM_FTYPE | MIIM_ID | MIIM_DATA;
|
|
|
|
if (!owner_draw_)
|
|
|
|
mii.fType = MFT_STRING;
|
|
|
|
else
|
|
|
|
mii.fType = MFT_OWNERDRAW;
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
std::unique_ptr<ItemData> item_data = std::make_unique<ItemData>();
|
2021-04-21 00:52:34 +02:00
|
|
|
item_data->label = std::wstring();
|
2016-01-06 20:20:54 +01:00
|
|
|
ui::MenuModel::ItemType type = model_->GetTypeAt(model_index);
|
|
|
|
if (type == ui::MenuModel::TYPE_SUBMENU) {
|
|
|
|
item_data->submenu.reset(new Menu2(model_->GetSubmenuModelAt(model_index)));
|
|
|
|
mii.fMask |= MIIM_SUBMENU;
|
|
|
|
mii.hSubMenu = item_data->submenu->GetNativeMenu();
|
2016-07-14 03:35:07 +02:00
|
|
|
GetCefNativeMenuWinFromHMENU(mii.hSubMenu)->parent_ = this;
|
2016-01-06 20:20:54 +01:00
|
|
|
} else {
|
|
|
|
if (type == ui::MenuModel::TYPE_RADIO)
|
|
|
|
mii.fType |= MFT_RADIOCHECK;
|
|
|
|
mii.wID = model_->GetCommandIdAt(model_index);
|
|
|
|
}
|
|
|
|
item_data->native_menu_win = this;
|
|
|
|
item_data->model_index = model_index;
|
2016-11-23 21:54:29 +01:00
|
|
|
mii.dwItemData = reinterpret_cast<ULONG_PTR>(item_data.get());
|
|
|
|
items_.insert(items_.begin() + model_index, std::move(item_data));
|
2021-04-21 00:52:34 +02:00
|
|
|
UpdateMenuItemInfoForString(
|
|
|
|
&mii, model_index, base::UTF16ToWide(model_->GetLabelAt(model_index)));
|
2016-01-06 20:20:54 +01:00
|
|
|
InsertMenuItem(menu_, menu_index, TRUE, &mii);
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefNativeMenuWin::AddSeparatorItemAt(int menu_index, int model_index) {
|
2016-01-06 20:20:54 +01:00
|
|
|
MENUITEMINFO mii = {0};
|
|
|
|
mii.cbSize = sizeof(mii);
|
|
|
|
mii.fMask = MIIM_FTYPE;
|
|
|
|
mii.fType = MFT_SEPARATOR;
|
|
|
|
// Insert a dummy entry into our label list so we can index directly into it
|
|
|
|
// using item indices if need be.
|
2018-03-20 21:15:08 +01:00
|
|
|
items_.insert(items_.begin() + model_index, std::make_unique<ItemData>());
|
2016-01-06 20:20:54 +01:00
|
|
|
InsertMenuItem(menu_, menu_index, TRUE, &mii);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefNativeMenuWin::SetMenuItemState(int menu_index,
|
|
|
|
bool enabled,
|
|
|
|
bool checked,
|
|
|
|
bool is_default) {
|
2016-01-06 20:20:54 +01:00
|
|
|
if (IsSeparatorItemAt(menu_index))
|
|
|
|
return;
|
|
|
|
|
|
|
|
UINT state = enabled ? MFS_ENABLED : MFS_DISABLED;
|
|
|
|
if (checked)
|
|
|
|
state |= MFS_CHECKED;
|
|
|
|
if (is_default)
|
|
|
|
state |= MFS_DEFAULT;
|
|
|
|
|
|
|
|
MENUITEMINFO mii = {0};
|
|
|
|
mii.cbSize = sizeof(mii);
|
|
|
|
mii.fMask = MIIM_STATE;
|
|
|
|
mii.fState = state;
|
|
|
|
SetMenuItemInfo(menu_, menu_index, MF_BYPOSITION, &mii);
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefNativeMenuWin::SetMenuItemLabel(int menu_index,
|
2017-05-17 11:29:28 +02:00
|
|
|
int model_index,
|
2021-04-21 00:52:34 +02:00
|
|
|
const std::wstring& label) {
|
2016-01-06 20:20:54 +01:00
|
|
|
if (IsSeparatorItemAt(menu_index))
|
|
|
|
return;
|
|
|
|
|
|
|
|
MENUITEMINFO mii = {0};
|
|
|
|
mii.cbSize = sizeof(mii);
|
|
|
|
UpdateMenuItemInfoForString(&mii, model_index, label);
|
|
|
|
SetMenuItemInfo(menu_, menu_index, MF_BYPOSITION, &mii);
|
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void CefNativeMenuWin::UpdateMenuItemInfoForString(MENUITEMINFO* mii,
|
|
|
|
int model_index,
|
|
|
|
const std::wstring& label) {
|
|
|
|
std::wstring formatted = label;
|
2016-01-06 20:20:54 +01:00
|
|
|
ui::MenuModel::ItemType type = model_->GetTypeAt(model_index);
|
|
|
|
// Strip out any tabs, otherwise they get interpreted as accelerators and can
|
|
|
|
// lead to weird behavior.
|
|
|
|
base::ReplaceSubstringsAfterOffset(&formatted, 0, L"\t", L" ");
|
|
|
|
if (type != ui::MenuModel::TYPE_SUBMENU) {
|
|
|
|
// Add accelerator details to the label if provided.
|
|
|
|
ui::Accelerator accelerator(ui::VKEY_UNKNOWN, ui::EF_NONE);
|
|
|
|
if (model_->GetAcceleratorAt(model_index, &accelerator)) {
|
|
|
|
formatted += L"\t";
|
2021-04-21 00:52:34 +02:00
|
|
|
formatted += base::UTF16ToWide(accelerator.GetShortcutText());
|
2016-01-06 20:20:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the owned string, since Windows will want us to keep this new
|
|
|
|
// version around.
|
|
|
|
items_[model_index]->label = formatted;
|
|
|
|
|
|
|
|
// Give Windows a pointer to the label string.
|
|
|
|
mii->fMask |= MIIM_STRING;
|
2017-05-17 11:29:28 +02:00
|
|
|
mii->dwTypeData = const_cast<wchar_t*>(items_[model_index]->label.c_str());
|
2016-01-06 20:20:54 +01:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
UINT CefNativeMenuWin::GetAlignmentFlags(int alignment) const {
|
2016-01-06 20:20:54 +01:00
|
|
|
UINT alignment_flags = TPM_TOPALIGN;
|
|
|
|
if (alignment == Menu2::ALIGN_TOPLEFT)
|
|
|
|
alignment_flags |= TPM_LEFTALIGN;
|
|
|
|
else if (alignment == Menu2::ALIGN_TOPRIGHT)
|
|
|
|
alignment_flags |= TPM_RIGHTALIGN;
|
|
|
|
return alignment_flags;
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefNativeMenuWin::ResetNativeMenu() {
|
2016-01-06 20:20:54 +01:00
|
|
|
if (IsWindow(system_menu_for_)) {
|
|
|
|
if (menu_)
|
|
|
|
GetSystemMenu(system_menu_for_, TRUE);
|
|
|
|
menu_ = GetSystemMenu(system_menu_for_, FALSE);
|
|
|
|
} else {
|
|
|
|
if (menu_)
|
|
|
|
DestroyMenu(menu_);
|
|
|
|
menu_ = CreatePopupMenu();
|
|
|
|
// Rather than relying on the return value of TrackPopupMenuEx, which is
|
|
|
|
// always a command identifier, instead we tell the menu to notify us via
|
|
|
|
// our host window and the WM_MENUCOMMAND message.
|
|
|
|
MENUINFO mi = {0};
|
|
|
|
mi.cbSize = sizeof(mi);
|
|
|
|
mi.fMask = MIM_STYLE | MIM_MENUDATA;
|
|
|
|
mi.dwStyle = MNS_NOTIFYBYPOS;
|
|
|
|
mi.dwMenuData = reinterpret_cast<ULONG_PTR>(this);
|
|
|
|
SetMenuInfo(menu_, &mi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefNativeMenuWin::CreateHostWindow() {
|
2016-01-06 20:20:54 +01:00
|
|
|
// This only gets called from RunMenuAt, and as such there is only ever one
|
2016-07-14 03:35:07 +02:00
|
|
|
// host window per menu hierarchy, no matter how many CefNativeMenuWin objects
|
2016-01-06 20:20:54 +01:00
|
|
|
// exist wrapping submenus.
|
|
|
|
if (!host_window_.get())
|
2017-12-07 22:44:24 +01:00
|
|
|
host_window_.reset(new MenuHostWindow());
|
2016-01-06 20:20:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// MenuWrapper, public:
|
|
|
|
|
|
|
|
// static
|
|
|
|
MenuWrapper* MenuWrapper::CreateWrapper(ui::MenuModel* model) {
|
2016-07-14 03:35:07 +02:00
|
|
|
return new CefNativeMenuWin(model, NULL);
|
2016-01-06 20:20:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace views
|