From 34186d2e85bd6467e6a2db09fc767919cd7b0980 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Fri, 12 May 2023 04:01:20 +0200 Subject: [PATCH] Fix menu strings cut off regression for some localication ref: https://github.com/notepad-plus-plus/notepad-plus-plus/issues/13556#issuecomment-1518197329 Fix #13556, close #13645 --- PowerEditor/src/Notepad_plus.cpp | 40 +++++++++---------- PowerEditor/src/NppCommands.cpp | 11 +---- PowerEditor/src/Parameters.cpp | 16 ++++---- .../src/WinControls/shortcut/shortcut.cpp | 18 ++++----- .../src/WinControls/shortcut/shortcut.h | 7 ++-- 5 files changed, 40 insertions(+), 52 deletions(-) diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index bf41ed503..6019a6429 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -504,14 +504,13 @@ LRESULT Notepad_plus::init(HWND hwnd) ExternalLangContainer & externalLangContainer = nppParam.getELCFromIndex(i); int numLangs = ::GetMenuItemCount(hLangMenu); - const int bufferSize = 100; - TCHAR buffer[bufferSize] = { '\0' }; + TCHAR buffer[menuItemStrLenMax]{}; const TCHAR* lexerNameW = wmc.char2wchar(externalLangContainer._name.c_str(), CP_ACP); int x = 0; for (; (x == 0 || lstrcmp(lexerNameW, buffer) > 0) && x < numLangs; ++x) { - ::GetMenuString(hLangMenu, x, buffer, bufferSize, MF_BYPOSITION); + ::GetMenuString(hLangMenu, x, buffer, menuItemStrLenMax, MF_BYPOSITION); } ::InsertMenu(hLangMenu, x - 1, MF_BYPOSITION, IDM_LANG_EXTERNAL + i, lexerNameW); @@ -587,12 +586,12 @@ LRESULT Notepad_plus::init(HWND hwnd) // Update Scintilla context menu strings (translated) vector & tmp = nppParam.getContextMenuItems(); size_t len = tmp.size(); - TCHAR menuName[64]; + TCHAR menuName[menuItemStrLenMax]; for (size_t i = 0; i < len; ++i) { if (tmp[i]._itemName.empty()) { - ::GetMenuString(_mainMenuHandle, tmp[i]._cmdID, menuName, 64, MF_BYCOMMAND); + ::GetMenuString(_mainMenuHandle, tmp[i]._cmdID, menuName, menuItemStrLenMax, MF_BYCOMMAND); tmp[i]._itemName = purgeMenuItemString(menuName); } } @@ -605,7 +604,7 @@ LRESULT Notepad_plus::init(HWND hwnd) { if (tmp2[i]._itemName.empty()) { - ::GetMenuString(_mainMenuHandle, tmp2[i]._cmdID, menuName, 64, MF_BYCOMMAND); + ::GetMenuString(_mainMenuHandle, tmp2[i]._cmdID, menuName, menuItemStrLenMax, MF_BYCOMMAND); tmp2[i]._itemName = purgeMenuItemString(menuName); } } @@ -2613,11 +2612,11 @@ void Notepad_plus::checkLangsMenu(int id) const if (curBuf->isUserDefineLangExt()) { const TCHAR *userLangName = curBuf->getUserDefineLangName(); - TCHAR menuLangName[langNameLenMax]; + TCHAR menuLangName[menuItemStrLenMax]; for (int i = IDM_LANG_USER + 1 ; i <= IDM_LANG_USER_LIMIT ; ++i) { - if (::GetMenuString(_mainMenuHandle, i, menuLangName, langNameLenMax, MF_BYCOMMAND)) + if (::GetMenuString(_mainMenuHandle, i, menuLangName, menuItemStrLenMax, MF_BYCOMMAND)) { if (!lstrcmp(userLangName, menuLangName)) { @@ -2928,9 +2927,8 @@ void Notepad_plus::setUniModeText() } cmdID += IDM_FORMAT_ENCODE; - const int itemSize = 64; - TCHAR uniModeText[itemSize] = {}; - ::GetMenuString(_mainMenuHandle, cmdID, uniModeText, itemSize, MF_BYCOMMAND); + TCHAR uniModeText[menuItemStrLenMax]{}; + ::GetMenuString(_mainMenuHandle, cmdID, uniModeText, menuItemStrLenMax, MF_BYCOMMAND); uniModeTextString = uniModeText; // Remove the shortcut text from the menu text. const size_t tabPos = uniModeTextString.find_last_of('\t'); @@ -6732,11 +6730,10 @@ void Notepad_plus::setWorkingDir(const TCHAR *dir) int Notepad_plus::getLangFromMenuName(const TCHAR * langName) { int id = 0; - const int menuSize = 64; - TCHAR menuLangName[menuSize]; + TCHAR menuLangName[menuItemStrLenMax]; for ( int i = IDM_LANG_C; i <= IDM_LANG_USER; ++i ) - if ( ::GetMenuString( _mainMenuHandle, i, menuLangName, menuSize, MF_BYCOMMAND ) ) + if ( ::GetMenuString( _mainMenuHandle, i, menuLangName, menuItemStrLenMax, MF_BYCOMMAND ) ) if ( !lstrcmp( langName, menuLangName ) ) { id = i; @@ -6746,7 +6743,7 @@ int Notepad_plus::getLangFromMenuName(const TCHAR * langName) if ( id == 0 ) { for ( int i = IDM_LANG_USER + 1; i <= IDM_LANG_USER_LIMIT; ++i ) - if ( ::GetMenuString( _mainMenuHandle, i, menuLangName, menuSize, MF_BYCOMMAND ) ) + if ( ::GetMenuString( _mainMenuHandle, i, menuLangName, menuItemStrLenMax, MF_BYCOMMAND ) ) if ( !lstrcmp( langName, menuLangName ) ) { id = i; @@ -6762,13 +6759,12 @@ generic_string Notepad_plus::getLangFromMenu(const Buffer * buf) int id; generic_string userLangName; - const int nbChar = 32; - TCHAR menuLangName[nbChar]; + TCHAR menuLangName[menuItemStrLenMax]{}; id = (NppParameters::getInstance()).langTypeToCommandID( buf->getLangType() ); if ( ( id != IDM_LANG_USER ) || !( buf->isUserDefineLangExt() ) ) { - ::GetMenuString(_mainMenuHandle, id, menuLangName, nbChar-1, MF_BYCOMMAND); + ::GetMenuString(_mainMenuHandle, id, menuLangName, menuItemStrLenMax, MF_BYCOMMAND); userLangName = menuLangName; } else @@ -6822,12 +6818,12 @@ bool Notepad_plus::reloadLang() // Update scintilla context menu strings vector & tmp = nppParam.getContextMenuItems(); size_t len = tmp.size(); - TCHAR menuName[64]; + TCHAR menuName[menuItemStrLenMax]; for (size_t i = 0 ; i < len ; ++i) { if (tmp[i]._itemName == TEXT("")) { - ::GetMenuString(_mainMenuHandle, tmp[i]._cmdID, menuName, 64, MF_BYCOMMAND); + ::GetMenuString(_mainMenuHandle, tmp[i]._cmdID, menuName, menuItemStrLenMax, MF_BYCOMMAND); tmp[i]._itemName = purgeMenuItemString(menuName); } } @@ -8557,8 +8553,8 @@ void Notepad_plus::updateCommandShortcuts() if (menuName.length() == 0) { - TCHAR szMenuName[64]; - if (::GetMenuString(_mainMenuHandle, csc.getID(), szMenuName, _countof(szMenuName), MF_BYCOMMAND)) + TCHAR szMenuName[menuItemStrLenMax]; + if (::GetMenuString(_mainMenuHandle, csc.getID(), szMenuName, menuItemStrLenMax, MF_BYCOMMAND)) menuName = purgeMenuItemString(szMenuName, true); else menuName = csc.getShortcutName(); diff --git a/PowerEditor/src/NppCommands.cpp b/PowerEditor/src/NppCommands.cpp index f7bf5cca8..e3f25d6be 100644 --- a/PowerEditor/src/NppCommands.cpp +++ b/PowerEditor/src/NppCommands.cpp @@ -3936,8 +3936,8 @@ void Notepad_plus::command(int id) } else if ((id > IDM_LANG_USER) && (id < IDM_LANG_USER_LIMIT)) { - TCHAR langName[langNameLenMax]; - ::GetMenuString(_mainMenuHandle, id, langName, langNameLenMax, MF_BYCOMMAND); + TCHAR langName[menuItemStrLenMax]; + ::GetMenuString(_mainMenuHandle, id, langName, menuItemStrLenMax, MF_BYCOMMAND); _pEditView->getCurrentBuffer()->setLangType(L_USER, langName); if (_pDocMap) { @@ -3976,13 +3976,6 @@ void Notepad_plus::command(int id) { _pluginsManager.relayNppMessages(WM_COMMAND, id, 0); } -/*UNLOAD - else if ((id >= ID_PLUGINS_REMOVING) && (id < ID_PLUGINS_REMOVING_END)) - { - int i = id - ID_PLUGINS_REMOVING; - _pluginsManager.unloadPlugin(i, _pPublicInterface->getHSelf()); - } -*/ else if ((id >= IDM_WINDOW_MRU_FIRST) && (id <= IDM_WINDOW_MRU_LIMIT)) { activateDoc(id - IDM_WINDOW_MRU_FIRST); diff --git a/PowerEditor/src/Parameters.cpp b/PowerEditor/src/Parameters.cpp index cd07354db..65f66a46b 100644 --- a/PowerEditor/src/Parameters.cpp +++ b/PowerEditor/src/Parameters.cpp @@ -2031,8 +2031,8 @@ int NppParameters::getCmdIdFromMenuEntryItemName(HMENU mainMenuHadle, const gene int nbMenuEntry = ::GetMenuItemCount(mainMenuHadle); for (int i = 0; i < nbMenuEntry; ++i) { - TCHAR menuEntryString[64]; - ::GetMenuString(mainMenuHadle, i, menuEntryString, 64, MF_BYPOSITION); + TCHAR menuEntryString[menuItemStrLenMax]; + ::GetMenuString(mainMenuHadle, i, menuEntryString, menuItemStrLenMax, MF_BYPOSITION); if (wcsicmp(menuEntryName.c_str(), purgeMenuItemString(menuEntryString).c_str()) == 0) { vector< pair > parentMenuPos; @@ -2056,8 +2056,8 @@ int NppParameters::getCmdIdFromMenuEntryItemName(HMENU mainMenuHadle, const gene else { // Check current menu position. - TCHAR cmdStr[256]; - ::GetMenuString(currMenu, currMenuPos, cmdStr, 256, MF_BYPOSITION); + TCHAR cmdStr[menuItemStrLenMax]; + ::GetMenuString(currMenu, currMenuPos, cmdStr, menuItemStrLenMax, MF_BYPOSITION); if (wcsicmp(menuItemName.c_str(), purgeMenuItemString(cmdStr).c_str()) == 0) { return ::GetMenuItemID(currMenu, currMenuPos); @@ -2091,16 +2091,16 @@ int NppParameters::getPluginCmdIdFromMenuEntryItemName(HMENU pluginsMenu, const int nbPlugins = ::GetMenuItemCount(pluginsMenu); for (int i = 0; i < nbPlugins; ++i) { - TCHAR menuItemString[256]; - ::GetMenuString(pluginsMenu, i, menuItemString, 256, MF_BYPOSITION); + TCHAR menuItemString[menuItemStrLenMax]; + ::GetMenuString(pluginsMenu, i, menuItemString, menuItemStrLenMax, MF_BYPOSITION); if (wcsicmp(pluginName.c_str(), purgeMenuItemString(menuItemString).c_str()) == 0) { HMENU pluginMenu = ::GetSubMenu(pluginsMenu, i); int nbPluginCmd = ::GetMenuItemCount(pluginMenu); for (int j = 0; j < nbPluginCmd; ++j) { - TCHAR pluginCmdStr[256]; - ::GetMenuString(pluginMenu, j, pluginCmdStr, 256, MF_BYPOSITION); + TCHAR pluginCmdStr[menuItemStrLenMax]; + ::GetMenuString(pluginMenu, j, pluginCmdStr, menuItemStrLenMax, MF_BYPOSITION); if (wcsicmp(pluginCmdName.c_str(), purgeMenuItemString(pluginCmdStr).c_str()) == 0) { return ::GetMenuItemID(pluginMenu, j); diff --git a/PowerEditor/src/WinControls/shortcut/shortcut.cpp b/PowerEditor/src/WinControls/shortcut/shortcut.cpp index 11abff9ad..97892c74d 100644 --- a/PowerEditor/src/WinControls/shortcut/shortcut.cpp +++ b/PowerEditor/src/WinControls/shortcut/shortcut.cpp @@ -157,10 +157,10 @@ string Shortcut::toString() const void Shortcut::setName(const char* menuName, const char* shortcutName) { - lstrcpynA(_menuName, menuName, nameLenMax); + lstrcpynA(_menuName, menuName, menuItemStrLenMax); char const * name = shortcutName ? shortcutName : menuName; size_t i = 0, j = 0; - while (name[j] != 0 && i < (nameLenMax - 1)) + while (name[j] != 0 && i < (menuItemStrLenMax - 1)) { if (name[j] != '&') { @@ -318,10 +318,9 @@ void getNameStrFromCmd(DWORD cmd, wstring & str) else { HWND hNotepad_plus = ::FindWindow(Notepad_plus_Window::getClassName(), NULL); - const int commandSize = 64; - TCHAR cmdName[commandSize]; + TCHAR cmdName[menuItemStrLenMax]; HMENU m = reinterpret_cast(::SendMessage(hNotepad_plus, NPPM_INTERNAL_GETMENU, 0, 0)); - int nbChar = ::GetMenuString(m, cmd, cmdName, commandSize, MF_BYCOMMAND); + int nbChar = ::GetMenuString(m, cmd, cmdName, menuItemStrLenMax, MF_BYCOMMAND); if (!nbChar) return; bool fin = false; @@ -485,8 +484,8 @@ intptr_t CALLBACK Shortcut::run_dlgProc(UINT Message, WPARAM wParam, LPARAM lPar if (_canModifyName) { - TCHAR editName[nameLenMax]{}; - ::SendDlgItemMessage(_hSelf, IDC_NAME_EDIT, WM_GETTEXT, nameLenMax, reinterpret_cast(editName)); + TCHAR editName[menuItemStrLenMax]{}; + ::SendDlgItemMessage(_hSelf, IDC_NAME_EDIT, WM_GETTEXT, menuItemStrLenMax, reinterpret_cast(editName)); setName(wstring2string(editName, CP_UTF8).c_str()); } ::EndDialog(_hSelf, 0); @@ -958,9 +957,8 @@ void ScintillaAccelerator::updateKeys() void ScintillaAccelerator::updateMenuItemByID(const ScintillaKeyMap& skm, int id) { - const int commandSize = 64; - TCHAR cmdName[commandSize]; - ::GetMenuString(_hAccelMenu, id, cmdName, commandSize, MF_BYCOMMAND); + TCHAR cmdName[menuItemStrLenMax]; + ::GetMenuString(_hAccelMenu, id, cmdName, menuItemStrLenMax, MF_BYCOMMAND); int i = 0; while (cmdName[i] != 0) { diff --git a/PowerEditor/src/WinControls/shortcut/shortcut.h b/PowerEditor/src/WinControls/shortcut/shortcut.h index a4b25ae46..bb10eeff3 100644 --- a/PowerEditor/src/WinControls/shortcut/shortcut.h +++ b/PowerEditor/src/WinControls/shortcut/shortcut.h @@ -23,7 +23,8 @@ #include "Common.h" #include "menuCmdID.h" -const size_t nameLenMax = 64; +constexpr int menuItemStrLenMax = 64 + 64; // Add 64 "units" more for being compatible to the current localization file. See: + // https://github.com/notepad-plus-plus/notepad-plus-plus/issues/13556#issuecomment-1518197329 class NppParameters; @@ -176,8 +177,8 @@ protected : KeyCombo _keyCombo; virtual intptr_t CALLBACK run_dlgProc(UINT Message, WPARAM wParam, LPARAM lParam); bool _canModifyName = false; - char _name[nameLenMax] = {'\0'}; //normal name is plain text (for display purposes) - char _menuName[nameLenMax] = { '\0' }; //menu name has ampersands for quick keys + char _name[menuItemStrLenMax] {}; //normal name is plain text (for display purposes) + char _menuName[menuItemStrLenMax] {}; //menu name has ampersands for quick keys void updateConflictState(const bool endSession = false) const; };