From 39001d7a0272320124117e49fa7ac333a3a9d96e Mon Sep 17 00:00:00 2001 From: Don Ho Date: Mon, 18 Sep 2023 04:51:04 +0200 Subject: [PATCH] Fix Wrong Categories in Shortcuts Mapper and make localization more accurate. Fix #13285, close #14156 --- PowerEditor/installer/nativeLang/english.xml | 10 ---- PowerEditor/installer/nativeLang/french.xml | 10 ---- .../nativeLang/taiwaneseMandarin.xml | 2 +- PowerEditor/src/Notepad_plus.cpp | 8 +-- .../src/WinControls/Grid/ShortcutMapper.cpp | 4 +- .../src/WinControls/shortcut/shortcut.cpp | 56 ++++++++++++------- .../src/WinControls/shortcut/shortcut.h | 5 +- PowerEditor/src/localization.cpp | 37 ++++++++++++ PowerEditor/src/localization.h | 7 +++ 9 files changed, 89 insertions(+), 50 deletions(-) diff --git a/PowerEditor/installer/nativeLang/english.xml b/PowerEditor/installer/nativeLang/english.xml index e405e615f..ac8c32a76 100644 --- a/PowerEditor/installer/nativeLang/english.xml +++ b/PowerEditor/installer/nativeLang/english.xml @@ -641,17 +641,7 @@ Translation note: - - - - - - - - - - diff --git a/PowerEditor/installer/nativeLang/french.xml b/PowerEditor/installer/nativeLang/french.xml index 1ada21f63..bae93c706 100644 --- a/PowerEditor/installer/nativeLang/french.xml +++ b/PowerEditor/installer/nativeLang/french.xml @@ -641,17 +641,7 @@ Translation note: - - - - - - - - - - diff --git a/PowerEditor/installer/nativeLang/taiwaneseMandarin.xml b/PowerEditor/installer/nativeLang/taiwaneseMandarin.xml index 6a0a8c30f..86a8bc980 100644 --- a/PowerEditor/installer/nativeLang/taiwaneseMandarin.xml +++ b/PowerEditor/installer/nativeLang/taiwaneseMandarin.xml @@ -614,7 +614,7 @@ - + diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index cfd4d50b6..71a3f5006 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -6997,6 +6997,7 @@ bool Notepad_plus::reloadLang() } } + _nativeLangSpeaker.resetShortcutMenuNameMap(); updateCommandShortcuts(); _accelerator.updateFullMenu(); @@ -8711,12 +8712,9 @@ void Notepad_plus::createMonitoringThread(Buffer* pBuf) void Notepad_plus::updateCommandShortcuts() { NppParameters& nppParam = NppParameters::getInstance(); - vector & shortcuts = nppParam.getUserShortcuts(); - size_t len = shortcuts.size(); - for (size_t i = 0; i < len; ++i) + for (CommandShortcut& csc : nppParam.getUserShortcuts()) { - CommandShortcut & csc = shortcuts[i]; unsigned long id = csc.getID(); generic_string localizedMenuName = _nativeLangSpeaker.getNativeLangMenuString(id); generic_string menuName = localizedMenuName; @@ -8742,6 +8740,8 @@ void Notepad_plus::updateCommandShortcuts() } csc.setName(wstring2string(menuName, CP_UTF8).c_str(), wstring2string(shortcutName, CP_UTF8).c_str()); + + csc.setCategoryFromMenu(_mainMenuHandle); } } diff --git a/PowerEditor/src/WinControls/Grid/ShortcutMapper.cpp b/PowerEditor/src/WinControls/Grid/ShortcutMapper.cpp index 97f6130ea..da7f0a95d 100644 --- a/PowerEditor/src/WinControls/Grid/ShortcutMapper.cpp +++ b/PowerEditor/src/WinControls/Grid/ShortcutMapper.cpp @@ -300,9 +300,7 @@ void ShortcutMapper::fillOutBabyGrid() if (cshortcuts[i].isEnabled()) //avoid empty strings for better performance _babygrid.setText(cs_index, 2, string2wstring(cshortcuts[i].toString(), CP_UTF8).c_str()); - const TCHAR* category = cshortcuts[i].getCategory(); - generic_string categoryStr = nativeLangSpeaker->getShortcutMapperLangStr((std::string(wstring2string(category, CP_UTF8)) + "Category").c_str(), category); - _babygrid.setText(cs_index, 3, categoryStr.c_str()); + _babygrid.setText(cs_index, 3, cshortcuts[i].getCategory()); if (isMarker) isMarker = _babygrid.setMarker(false); diff --git a/PowerEditor/src/WinControls/shortcut/shortcut.cpp b/PowerEditor/src/WinControls/shortcut/shortcut.cpp index c9b39ba65..47e275cce 100644 --- a/PowerEditor/src/WinControls/shortcut/shortcut.cpp +++ b/PowerEditor/src/WinControls/shortcut/shortcut.cpp @@ -1224,34 +1224,48 @@ intptr_t CALLBACK ScintillaKeyMap::run_dlgProc(UINT Message, WPARAM wParam, LPAR return FALSE; } -CommandShortcut::CommandShortcut(const Shortcut& sc, long id) : Shortcut(sc), _id(id) +void CommandShortcut::setCategoryFromMenu(HMENU hMenu) { - _shortcutName = string2wstring(sc.getName(), CP_UTF8); + NativeLangSpeaker* pNativeSpeaker = NppParameters::getInstance().getNativeLangSpeaker(); + if ( _id >= IDM_WINDOW_SORT_FN_ASC and _id <= IDM_WINDOW_SORT_FS_DSC) - _category = TEXT("Window"); - else if ( _id < IDM_EDIT) - _category = TEXT("File"); - else if ( _id < IDM_SEARCH) - _category = TEXT("Edit"); - else if (((_id >= IDM_EDIT_AUTOCOMPLETE) && (_id <= IDM_EDIT_AUTOCOMPLETE_PATH)) || - ((_id >= IDM_EDIT_FUNCCALLTIP_PREVIOUS) && (_id <= IDM_EDIT_FUNCCALLTIP_NEXT))) - _category = TEXT("Edit"); - else if ( _id < IDM_VIEW) - _category = TEXT("Search"); - else if ( _id < IDM_FORMAT) - _category = TEXT("View"); + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "Window", L"Window"); else if ( _id >= IDM_VIEW_GOTO_ANOTHER_VIEW and _id <= IDM_VIEW_LOAD_IN_NEW_INSTANCE) - _category = TEXT("View"); + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "view", L"View"); + else if (_id == IDM_EDIT_LTR || _id == IDM_EDIT_RTL) + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "view", L"View"); + else if (_id == IDC_PREV_DOC || _id == IDC_NEXT_DOC) + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "view", L"View"); + else if (_id == IDM_FORMAT_TODOS || _id == IDM_FORMAT_TOUNIX || _id == IDM_FORMAT_TOMAC) + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "edit", L"Edit"); + else if (_id == IDM_EDIT_AUTOCOMPLETE || _id == IDM_EDIT_AUTOCOMPLETE_CURRENTFILE || _id == IDM_EDIT_FUNCCALLTIP || + _id == IDM_EDIT_AUTOCOMPLETE_PATH || _id == IDM_EDIT_FUNCCALLTIP_PREVIOUS || _id == IDM_EDIT_FUNCCALLTIP_NEXT) + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "edit", L"Edit"); + else if (_id == IDM_LANGSTYLE_CONFIG_DLG) + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "settings", L"Settings"); + else if (_id == IDM_MACRO_STARTRECORDINGMACRO ||_id == IDM_MACRO_STOPRECORDINGMACRO || _id == IDM_MACRO_RUNMULTIMACRODLG || + _id == IDM_MACRO_PLAYBACKRECORDEDMACRO ||_id == IDM_MACRO_SAVECURRENTMACRO || _id == IDC_EDIT_TOGGLEMACRORECORDING) + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "macro", L"Macro"); + + + else if ( _id < IDM_EDIT) + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "file", L"File"); + else if ( _id < IDM_SEARCH) + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "edit",L"Edit"); + else if ( _id < IDM_VIEW) + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "search", L"Search"); + else if ( _id < IDM_FORMAT) + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "view", L"View"); else if ( _id < IDM_LANG) - _category = TEXT("Format"); + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "encoding", L"Encoding"); else if ( _id < IDM_ABOUT) - _category = TEXT("Lang"); + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "language", L"Language"); else if ( _id < IDM_SETTING) - _category = TEXT("About"); + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "about", L"About"); else if ( _id < IDM_TOOL) - _category = TEXT("Setting"); + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "settings", L"Settings"); else if ( _id < IDM_EXECUTE) - _category = TEXT("Tool"); + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "tools", L"Tools"); else - _category = TEXT("Execute"); + pNativeSpeaker->getMainMenuEntryName(_category, hMenu, "run", L"Run"); } diff --git a/PowerEditor/src/WinControls/shortcut/shortcut.h b/PowerEditor/src/WinControls/shortcut/shortcut.h index bb10eeff3..a7157e4e2 100644 --- a/PowerEditor/src/WinControls/shortcut/shortcut.h +++ b/PowerEditor/src/WinControls/shortcut/shortcut.h @@ -184,7 +184,10 @@ protected : class CommandShortcut : public Shortcut { public: - CommandShortcut(const Shortcut& sc, long id); + CommandShortcut(const Shortcut& sc, long id) : Shortcut(sc), _id(id){ + _shortcutName = string2wstring(getName(), CP_UTF8); + }; + void setCategoryFromMenu(HMENU hMenu); unsigned long getID() const {return _id;}; void setID(unsigned long id) { _id = id;}; const TCHAR * getCategory() const { return _category.c_str(); }; diff --git a/PowerEditor/src/localization.cpp b/PowerEditor/src/localization.cpp index 5c5a08ec6..408ecc456 100644 --- a/PowerEditor/src/localization.cpp +++ b/PowerEditor/src/localization.cpp @@ -284,6 +284,43 @@ MenuPosition & getMenuPosition(const char *id) return menuPos[nbSubMenuPos-1]; } +// Get string from map. +// If string not found, get string from menu, then put it into map for the next use. +void NativeLangSpeaker::getMainMenuEntryName(std::wstring& dest, HMENU hMenu, const char* menuId, const wchar_t* defaultDest) +{ + const auto iter = _shortcutMenuEntryNameMap.find(menuId); + if (iter == _shortcutMenuEntryNameMap.end()) + { + MenuPosition& menuPos = getMenuPosition(menuId); + if (menuPos._x != -1 && menuPos._y == -1 && menuPos._z == -1) + { + wchar_t str[MAX_PATH]; + GetMenuString(hMenu, menuPos._x, str, MAX_PATH, MF_BYPOSITION); + dest = str; + dest.erase(std::remove(dest.begin(), dest.end(), '&'), dest.end()); + _shortcutMenuEntryNameMap[menuId] = dest; + + } + else + { + if (strcmp(menuId, "about") == 0) + { + dest = getShortcutMapperLangStr("AboutCategory", defaultDest); + } + else + { + _shortcutMenuEntryNameMap[menuId] = defaultDest; + dest = defaultDest; + } + } + } + else + { + dest = iter->second; + } +} + + void NativeLangSpeaker::changeMenuLang(HMENU menuHandle) { if (nullptr == _nativeLangA) diff --git a/PowerEditor/src/localization.h b/PowerEditor/src/localization.h index 3be14cd35..62a70fded 100644 --- a/PowerEditor/src/localization.h +++ b/PowerEditor/src/localization.h @@ -17,6 +17,7 @@ #pragma once +#include #include "Common.h" #include "tinyxmlA.h" @@ -83,6 +84,11 @@ public: generic_string getAttrNameStr(const TCHAR *defaultStr, const char *nodeL1Name, const char *nodeL2Name, const char *nodeL3Name = "name") const; generic_string getAttrNameByIdStr(const TCHAR *defaultStr, TiXmlNodeA *targetNode, const char *nodeL1Value, const char *nodeL1Name = "id", const char *nodeL2Name = "name") const; generic_string getLocalizedStrFromID(const char *strID, const generic_string& defaultString) const; + void getMainMenuEntryName(std::wstring& dest, HMENU hMenu, const char* menuIdStr, const wchar_t* defaultDest); + + void resetShortcutMenuNameMap() { + _shortcutMenuEntryNameMap.clear(); + }; int messageBox(const char *msgBoxTagName, HWND hWnd, const TCHAR *message, const TCHAR *title, int msgBoxType, int intInfo = 0, const TCHAR *strInfo = NULL); private: @@ -90,6 +96,7 @@ private: int _nativeLangEncoding; bool _isRTL; const char *_fileName; + std::map _shortcutMenuEntryNameMap; };