From ce9810ecb557b39aa4cb6b67a315e42d30e94e46 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Tue, 7 Apr 2015 02:10:03 +0200 Subject: [PATCH] [NEW_FEATURE] Add "Restore last closed file" (Ctrl+Shift+T) feature. --- PowerEditor/src/Notepad_plus.cpp | 2 +- PowerEditor/src/NppCommands.cpp | 26 ++++++++++++++++++++++---- PowerEditor/src/Parameters.cpp | 2 ++ PowerEditor/src/lastRecentFileList.cpp | 19 ++++++++++++++----- PowerEditor/src/lastRecentFileList.h | 13 ++++++++++--- PowerEditor/src/menuCmdID.h | 5 ++++- 6 files changed, 53 insertions(+), 14 deletions(-) diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index 72279d9f8..3285ef4be 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -513,7 +513,7 @@ LRESULT Notepad_plus::init(HWND hwnd) int nbLRFile = pNppParam->getNbLRFile(); //int pos = IDM_FILEMENU_LASTONE - IDM_FILE + 1 /* +1 : because of IDM_FILE_PRINTNOW */; - _lastRecentFileList.initMenu(hFileMenu, IDM_FILEMENU_LASTONE + 1, IDM_FILEMENU_EXISTCMDPOSITION, pNppParam->putRecentFileInSubMenu()); + _lastRecentFileList.initMenu(hFileMenu, IDM_FILEMENU_LASTONE + 1, IDM_FILEMENU_EXISTCMDPOSITION, &_accelerator, pNppParam->putRecentFileInSubMenu()); _lastRecentFileList.setLangEncoding(_nativeLangSpeaker.getLangEncoding()); for (int i = 0 ; i < nbLRFile ; ++i) { diff --git a/PowerEditor/src/NppCommands.cpp b/PowerEditor/src/NppCommands.cpp index 1e34d619a..bd75c272f 100644 --- a/PowerEditor/src/NppCommands.cpp +++ b/PowerEditor/src/NppCommands.cpp @@ -2368,7 +2368,8 @@ void Notepad_plus::command(int id) } break; - case IDM_OPEN_ALL_RECENT_FILE : { + case IDM_OPEN_ALL_RECENT_FILE : + { BufferID lastOne = BUFFER_INVALID; int size = _lastRecentFileList.getSize(); for (int i = size - 1; i >= 0; i--) @@ -2377,10 +2378,12 @@ void Notepad_plus::command(int id) if (test != BUFFER_INVALID) lastOne = test; } - if (lastOne != BUFFER_INVALID) { + if (lastOne != BUFFER_INVALID) + { switchToFile(lastOne); } - break; } + break; + } case IDM_CLEAN_RECENT_FILE_LIST : _lastRecentFileList.clear(); @@ -2466,11 +2469,26 @@ void Notepad_plus::command(int id) } break; + case IDM_FILE_RESTORELASTCLOSEDFILE: + { + generic_string lastOpenedFullPath = _lastRecentFileList.getFirstItem(); + if (lastOpenedFullPath != TEXT("")) + { + BufferID lastOpened = doOpen(lastOpenedFullPath.c_str()); + if (lastOpened != BUFFER_INVALID) + { + switchToFile(lastOpened); + } + } + } + break; + default : if (id > IDM_FILEMENU_LASTONE && id < (IDM_FILEMENU_LASTONE + _lastRecentFileList.getMaxNbLRF() + 1)) { BufferID lastOpened = doOpen(_lastRecentFileList.getItem(id).c_str()); - if (lastOpened != BUFFER_INVALID) { + if (lastOpened != BUFFER_INVALID) + { switchToFile(lastOpened); } } diff --git a/PowerEditor/src/Parameters.cpp b/PowerEditor/src/Parameters.cpp index 9682adfdb..ad88aeec9 100644 --- a/PowerEditor/src/Parameters.cpp +++ b/PowerEditor/src/Parameters.cpp @@ -81,6 +81,8 @@ WinMenuKeyDefinition winKeyDefs[] = { {VK_NULL, IDM_FILE_PRINTNOW, false, false, false, NULL}, {VK_F4, IDM_FILE_EXIT, false, true, false, NULL}, + { VK_T, IDM_FILE_RESTORELASTCLOSEDFILE, true, false, true, NULL}, + // {VK_NULL, IDM_EDIT_UNDO, false, false, false, NULL}, // {VK_NULL, IDM_EDIT_REDO, false, false, false, NULL}, // {VK_NULL, IDM_EDIT_CUT, false, false, false, NULL}, diff --git a/PowerEditor/src/lastRecentFileList.cpp b/PowerEditor/src/lastRecentFileList.cpp index 240549da4..a2f24a86d 100644 --- a/PowerEditor/src/lastRecentFileList.cpp +++ b/PowerEditor/src/lastRecentFileList.cpp @@ -31,7 +31,7 @@ #include "menuCmdID.h" #include "localization.h" -void LastRecentFileList::initMenu(HMENU hMenu, int idBase, int posBase, bool doSubMenu) +void LastRecentFileList::initMenu(HMENU hMenu, int idBase, int posBase, Accelerator *pAccelerator, bool doSubMenu) { if (doSubMenu) { @@ -46,6 +46,7 @@ void LastRecentFileList::initMenu(HMENU hMenu, int idBase, int posBase, bool doS _idBase = idBase; _posBase = posBase; + _pAccelerator = pAccelerator; _nativeLangEncoding = NPP_CP_WIN_1252; for (int i = 0 ; i < sizeof(_idFreeArray) ; ++i) @@ -56,7 +57,9 @@ void LastRecentFileList::initMenu(HMENU hMenu, int idBase, int posBase, bool doS void LastRecentFileList::switchMode() { //Remove all menu items + ::RemoveMenu(_hMenu, IDM_FILE_RESTORELASTCLOSEDFILE, MF_BYCOMMAND); ::RemoveMenu(_hMenu, IDM_OPEN_ALL_RECENT_FILE, MF_BYCOMMAND); + ::RemoveMenu(_hMenu, IDM_FILE_RESTORELASTCLOSEDFILE, MF_BYCOMMAND); ::RemoveMenu(_hMenu, IDM_CLEAN_RECENT_FILE_LIST, MF_BYCOMMAND); for(int i = 0; i < _size; ++i) @@ -100,11 +103,14 @@ void LastRecentFileList::updateMenu() NativeLangSpeaker *pNativeLangSpeaker = pNppParam->getNativeLangSpeaker(); generic_string recentFileList = pNativeLangSpeaker->getSpecialMenuEntryName("RecentFiles"); + generic_string openRecentClosedFile = pNativeLangSpeaker->getNativeLangMenuString(IDM_FILE_RESTORELASTCLOSEDFILE); generic_string openAllFiles = pNativeLangSpeaker->getNativeLangMenuString(IDM_OPEN_ALL_RECENT_FILE); generic_string cleanFileList = pNativeLangSpeaker->getNativeLangMenuString(IDM_CLEAN_RECENT_FILE_LIST); if (recentFileList == TEXT("")) recentFileList = TEXT("&Recent Files"); + if (openRecentClosedFile == TEXT("")) + openRecentClosedFile = TEXT("Restore Recent Closed File"); if (openAllFiles == TEXT("")) openAllFiles = TEXT("Open All Recent Files"); if (cleanFileList == TEXT("")) @@ -113,9 +119,10 @@ void LastRecentFileList::updateMenu() if (!isSubMenuMode()) ::InsertMenu(_hMenu, _posBase + 0, MF_BYPOSITION, UINT(-1), 0); - ::InsertMenu(_hMenu, _posBase + 1, MF_BYPOSITION, IDM_OPEN_ALL_RECENT_FILE, openAllFiles.c_str()); - ::InsertMenu(_hMenu, _posBase + 2, MF_BYPOSITION, IDM_CLEAN_RECENT_FILE_LIST, cleanFileList.c_str()); - ::InsertMenu(_hMenu, _posBase + 3, MF_BYPOSITION, UINT(-1), 0); + ::InsertMenu(_hMenu, _posBase + 1, MF_BYPOSITION, IDM_FILE_RESTORELASTCLOSEDFILE, openRecentClosedFile.c_str()); + ::InsertMenu(_hMenu, _posBase + 2, MF_BYPOSITION, IDM_OPEN_ALL_RECENT_FILE, openAllFiles.c_str()); + ::InsertMenu(_hMenu, _posBase + 3, MF_BYPOSITION, IDM_CLEAN_RECENT_FILE_LIST, cleanFileList.c_str()); + ::InsertMenu(_hMenu, _posBase + 4, MF_BYPOSITION, UINT(-1), 0); _hasSeparators = true; if (isSubMenuMode()) @@ -123,12 +130,14 @@ void LastRecentFileList::updateMenu() ::InsertMenu(_hParentMenu, _posBase + 0, MF_BYPOSITION | MF_POPUP, UINT(_hMenu), (LPCTSTR)recentFileList.c_str()); ::InsertMenu(_hParentMenu, _posBase + 1, MF_BYPOSITION, UINT(-1), 0); } + _pAccelerator->updateFullMenu(); } else if (_hasSeparators && _size == 0) //remove separators { - ::RemoveMenu(_hMenu, _posBase + 3, MF_BYPOSITION); + ::RemoveMenu(_hMenu, _posBase + 4, MF_BYPOSITION); ::RemoveMenu(_hMenu, IDM_CLEAN_RECENT_FILE_LIST, MF_BYCOMMAND); ::RemoveMenu(_hMenu, IDM_OPEN_ALL_RECENT_FILE, MF_BYCOMMAND); + ::RemoveMenu(_hMenu, IDM_FILE_RESTORELASTCLOSEDFILE, MF_BYCOMMAND); ::RemoveMenu(_hMenu, _posBase + 0, MF_BYPOSITION); _hasSeparators = false; diff --git a/PowerEditor/src/lastRecentFileList.h b/PowerEditor/src/lastRecentFileList.h index 6b6fb9d2c..f2271d37c 100644 --- a/PowerEditor/src/lastRecentFileList.h +++ b/PowerEditor/src/lastRecentFileList.h @@ -43,12 +43,12 @@ typedef std::deque recentList; class LastRecentFileList { -public : +public: LastRecentFileList() : _hasSeparators(false), _size(0), _locked(false) { _userMax = (NppParameters::getInstance())->getNbMaxRecentFile(); }; - void initMenu(HMENU hMenu, int idBase, int posBase, bool doSubMenu = false); + void initMenu(HMENU hMenu, int idBase, int posBase, Accelerator *accelerator, bool doSubMenu = false); void switchMode(); void updateMenu(); @@ -69,10 +69,16 @@ public : int getUserMaxNbLRF() const { return _userMax; }; - + generic_string & getItem(int id); //use menu id generic_string & getIndex(int index); //use menu id + generic_string getFirstItem() const { + if (_lrfl.size() == 0) + return TEXT(""); + return _lrfl.front()._name; + }; + void setUserMaxNbLRF(int size); void saveLRFL(); @@ -91,6 +97,7 @@ public : private: recentList _lrfl; + Accelerator *_pAccelerator; int _userMax; int _size; int _nativeLangEncoding; diff --git a/PowerEditor/src/menuCmdID.h b/PowerEditor/src/menuCmdID.h index 6196e27f3..9886b6d22 100644 --- a/PowerEditor/src/menuCmdID.h +++ b/PowerEditor/src/menuCmdID.h @@ -54,11 +54,14 @@ #define IDM_FILE_CLOSEALL_TORIGHT (IDM_FILE + 18) #define IDM_FILE_OPEN_FOLDER (IDM_FILE + 19) #define IDM_FILE_OPEN_CMD (IDM_FILE + 20) + // IMPORTANT: If list above is modified, you have to change the following values: // To be updated if new menu item(s) is (are) added in menu "File" #define IDM_FILEMENU_LASTONE IDM_FILE_OPEN_CMD - +// Byond of the last one + #define IDM_FILE_RESTORELASTCLOSEDFILE (IDM_FILE + 30) + // 0 based position of command "Exit" including the bars in the file menu // and without counting "Recent files history" items