Skip to content

Commit

Permalink
Check if GetMenu returns a valid handle and change return type to int…
Browse files Browse the repository at this point in the history
… for GetMenuItemCount (#306)

* Check it GetMenu returns a valid handle and change return type to int for GetMenuItemCount

* Refactor loop variable type from idx_t to int in MenuManager.cpp

The change was made to match the type of the for loop variable to the value returned by GetMenuItemCount.
  • Loading branch information
Ekopalypse authored Aug 23, 2023
1 parent 33dcca7 commit 75c639d
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions PythonScript/src/MenuManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,9 +837,9 @@ idx_t MenuManager::findPluginCommand(const TCHAR *pluginName, const TCHAR *menuO
{
HMENU hPluginMenu = (HMENU)::SendMessage(m_hNotepad, NPPM_GETMENUHANDLE, 0, 0);

size_t iMenuItems = (size_t)GetMenuItemCount(hPluginMenu);
int iMenuItems = GetMenuItemCount(hPluginMenu);
TCHAR strBuffer[500]{};
for ( idx_t i = 0; i < iMenuItems; ++i )
for ( int i = 0; i < iMenuItems; ++i )
{
MENUITEMINFO mii{};
mii.cbSize = sizeof(MENUITEMINFO);
Expand Down Expand Up @@ -913,6 +913,9 @@ idx_t MenuManager::findMenuCommand(const TCHAR *menuName, const TCHAR *menuOptio
}

HMENU hMenuBar = ::GetMenu(m_hNotepad);
if (hMenuBar == NULL) {
return 0;
}
idx_t retVal = findMenuCommand(hMenuBar, _T(""), menuName, menuOption);

if (retVal != 0)
Expand All @@ -936,12 +939,12 @@ tstring MenuManager::formatMenuName(const TCHAR *name)

idx_t MenuManager::findMenuCommand(HMENU hParentMenu, const TCHAR *parentMenuName, const TCHAR *menuName, const TCHAR *menuOption)
{
size_t iMenuItems = (size_t)GetMenuItemCount(hParentMenu);
int iMenuItems = GetMenuItemCount(hParentMenu);
idx_t retVal = 0;

TCHAR strBuffer[500]{};

for ( idx_t i = 0; i < iMenuItems; ++i )
for ( int i = 0; i < iMenuItems; ++i )
{
MENUITEMINFO mii{};
mii.cbSize = sizeof(MENUITEMINFO);
Expand All @@ -956,8 +959,8 @@ idx_t MenuManager::findMenuCommand(HMENU hParentMenu, const TCHAR *parentMenuNam
tstring thisMenuName = formatMenuName(strBuffer);
if (NULL == menuName || 0 == _tcsicmp(menuName, thisMenuName.c_str()))
{
size_t subMenuItems = (size_t)GetMenuItemCount(mii.hSubMenu);
for (idx_t subMenuPos = 0; subMenuPos < subMenuItems; ++subMenuPos)
int subMenuItems = GetMenuItemCount(mii.hSubMenu);
for (int subMenuPos = 0; subMenuPos < subMenuItems; ++subMenuPos)
{
TCHAR *context = NULL;
::GetMenuString(mii.hSubMenu, static_cast<UINT>(subMenuPos), strBuffer, 500, MF_BYPOSITION);
Expand All @@ -968,7 +971,7 @@ idx_t MenuManager::findMenuCommand(HMENU hParentMenu, const TCHAR *parentMenuNam

if (0 == _tcsicmp(menuOption, nameStr.c_str()))
{
return ::GetMenuItemID(mii.hSubMenu, (int)subMenuPos);
return ::GetMenuItemID(mii.hSubMenu, subMenuPos);
}
}
}
Expand Down Expand Up @@ -1142,4 +1145,4 @@ void MenuManager::checkShowConsole(bool checked)
{
::SendMessage(m_hNotepad, NPPM_SETMENUITEMCHECK, (WPARAM)m_funcItems[1]._cmdID, (LPARAM)checked);
s_menuItemConsoleChecked = checked;
}
}

0 comments on commit 75c639d

Please sign in to comment.