Skip to content

Commit

Permalink
Stop using WTL::CMenu* in tip_lang_bar_menu.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
yukawa committed Jan 22, 2024
1 parent 034b610 commit cd8c992
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/win32/tip/tip_lang_bar_menu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <strsafe.h>
// clang-format on
#include <wil/com.h>
#include <wil/resource.h>

#include <cstddef>
#include <string>
Expand Down Expand Up @@ -71,8 +72,6 @@ namespace {
using WTL::CBitmap;
using WTL::CDC;
using WTL::CIcon;
using WTL::CMenu;
using WTL::CMenuItemInfo;

// Represents the cookie for the sink to a TipLangBarButton object.
constexpr int kTipLangBarMenuCookie =
Expand Down Expand Up @@ -274,12 +273,12 @@ STDMETHODIMP TipLangBarButton::OnClick(TfLBIClick click, POINT point,
return S_OK;
}

CMenu menu;
menu.CreatePopupMenu();
wil::unique_hmenu menu(::CreatePopupMenu());
for (size_t i = 0; i < menu_data_size(); ++i) {
TipLangBarMenuData *data = menu_data(i);
const UINT id = static_cast<UINT>(data->item_id_);
CMenuItemInfo info;
MENUITEMINFO info = {};
info.cbSize = CCSIZEOF_STRUCT(MENUITEMINFO, hbmpItem);
if (data->flags_ == TF_LBMENUF_SEPARATOR) {
info.fMask |= MIIM_FTYPE;
info.fType |= MFT_SEPARATOR;
Expand Down Expand Up @@ -317,16 +316,30 @@ STDMETHODIMP TipLangBarButton::OnClick(TfLBIClick click, POINT point,
break;
}
}
menu.InsertMenuItemW(i, TRUE, &info);
::InsertMenuItem(menu.get(), i, TRUE, &info);
}

// Caveats: TPM_NONOTIFY is important because the attached window may
// change the menu state unless this flag is specified. We actually suffered
// from this issue with Internet Explorer 10 on Windows 8. b/10217103.
constexpr DWORD kMenuFlags = TPM_NONOTIFY | TPM_RETURNCMD | TPM_LEFTALIGN |
TPM_TOPALIGN | TPM_LEFTBUTTON;
const BOOL result =
menu.TrackPopupMenu(kMenuFlags, point.x, point.y, ::GetFocus());

// Make sure that the x-cordinate is inside the workarea.
const HMONITOR monitor_handle =
::MonitorFromPoint(point, MONITOR_DEFAULTTONEAREST);
if (monitor_handle != nullptr) {
MONITORINFO monitor_info = {};
monitor_info.cbSize = CCSIZEOF_STRUCT(MONITORINFO, dwFlags);
if (::GetMonitorInfo(monitor_handle, &monitor_info)) {
point.x = std::clamp(point.x,
monitor_info.rcWork.left,
monitor_info.rcWork.right);
}
}

const BOOL result = ::TrackPopupMenu(menu.get(), kMenuFlags, point.x, point.y,
0, ::GetFocus(), nullptr);
if (!result) {
return E_FAIL;
}
Expand Down

0 comments on commit cd8c992

Please sign in to comment.