[NEW_FEATURE] Add "Restore last closed file" (Ctrl+Shift+T) feature.

This commit is contained in:
Don Ho 2015-04-07 02:10:03 +02:00
parent 3ed1e767b1
commit ce9810ecb5
6 changed files with 53 additions and 14 deletions

View File

@ -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)
{

View File

@ -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);
}
}

View File

@ -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},

View File

@ -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;

View File

@ -43,12 +43,12 @@ typedef std::deque<RecentItem> 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;

View File

@ -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