moved recent files code to separate class...
This commit is contained in:
parent
1879f075a7
commit
347914dcf8
@ -228,6 +228,10 @@
|
|||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
|
RelativePath=".\recentfiles.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
|
<File
|
||||||
RelativePath=".\resource.h"
|
RelativePath=".\resource.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
|||||||
111
editor/recentfiles.h
Normal file
111
editor/recentfiles.h
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "stdafx.h"
|
||||||
|
|
||||||
|
static inline bool setRegString(HKEY key, const std::string &name, const std::string &value)
|
||||||
|
{
|
||||||
|
return ERROR_SUCCESS == RegSetValueEx(key, name.c_str(), 0, REG_SZ, (BYTE *)value.c_str(), (DWORD)value.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool getRegString(HKEY key, const std::string &name, std::string &out)
|
||||||
|
{
|
||||||
|
DWORD size = 0;
|
||||||
|
DWORD type = 0;
|
||||||
|
if (ERROR_SUCCESS != RegQueryValueEx(key, name.c_str(), 0, &type, (LPBYTE)NULL, &size)) return false;
|
||||||
|
if (REG_SZ != type) return false;
|
||||||
|
|
||||||
|
out.resize(size);
|
||||||
|
DWORD ret = RegQueryValueEx(key, name.c_str(), 0, &type, (LPBYTE)&out[0], &size);
|
||||||
|
while (out.size() > 0 && out[out.size() - 1] == '\0') out.resize(out.size() - 1);
|
||||||
|
|
||||||
|
assert(ret == ERROR_SUCCESS);
|
||||||
|
assert(REG_SZ == type);
|
||||||
|
assert(size == out.size() + 1);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
class RecentFiles
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
RecentFiles(HMENU menu) : mruFileMenu(menu) { }
|
||||||
|
|
||||||
|
void load(HKEY key)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < 5; ++i)
|
||||||
|
{
|
||||||
|
std::string fileName;
|
||||||
|
if (getRegString(key, getEntryName(i), fileName))
|
||||||
|
{
|
||||||
|
mruList.push_back(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mruList.size() > 0) update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void save(HKEY key)
|
||||||
|
{
|
||||||
|
std::list<std::string>::const_iterator it;
|
||||||
|
size_t i;
|
||||||
|
for (i = 0, it = mruList.begin(); it != mruList.end(); ++it, ++i)
|
||||||
|
{
|
||||||
|
assert(i <= 5);
|
||||||
|
setRegString(key, getEntryName(i), *it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void insert(const std::string &fileName)
|
||||||
|
{
|
||||||
|
mruList.remove(fileName); // remove, if present
|
||||||
|
mruList.push_front(fileName); // add to front
|
||||||
|
while (mruList.size() > 5) mruList.pop_back(); // remove old entries
|
||||||
|
}
|
||||||
|
|
||||||
|
void update()
|
||||||
|
{
|
||||||
|
while (0 != RemoveMenu(mruFileMenu, 0, MF_BYPOSITION));
|
||||||
|
|
||||||
|
std::list<std::string>::const_iterator it;
|
||||||
|
size_t i;
|
||||||
|
for (i = 0, it = mruList.begin(); it != mruList.end(); ++it, ++i)
|
||||||
|
{
|
||||||
|
assert(i <= 5);
|
||||||
|
std::string menuEntry = std::string("&");
|
||||||
|
menuEntry += char('1' + i);
|
||||||
|
menuEntry += " ";
|
||||||
|
menuEntry += *it;
|
||||||
|
AppendMenu(mruFileMenu, MF_STRING, ID_RECENTFILES_FILE1 + i, menuEntry.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t getEntryCount() const
|
||||||
|
{
|
||||||
|
return mruList.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getEntry(size_t index, std::string &out) const
|
||||||
|
{
|
||||||
|
std::list<std::string>::const_iterator it;
|
||||||
|
size_t i;
|
||||||
|
for (i = 0, it = mruList.begin(); it != mruList.end(); ++it, ++i)
|
||||||
|
{
|
||||||
|
if (i == index)
|
||||||
|
{
|
||||||
|
out = *it;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static std::string getEntryName(size_t i)
|
||||||
|
{
|
||||||
|
std::string temp = std::string("RecentFile");
|
||||||
|
temp += char('0' + i);
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::list<std::string> mruList;
|
||||||
|
HMENU mruFileMenu;
|
||||||
|
};
|
||||||
@ -12,6 +12,8 @@
|
|||||||
#include <commdlg.h>
|
#include <commdlg.h>
|
||||||
|
|
||||||
#include "trackview.h"
|
#include "trackview.h"
|
||||||
|
#include "recentfiles.h"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
const TCHAR *mainWindowClassName = _T("MainWindow");
|
const TCHAR *mainWindowClassName = _T("MainWindow");
|
||||||
const TCHAR *mainWindowTitle = _T("GNU Rocket System");
|
const TCHAR *mainWindowTitle = _T("GNU Rocket System");
|
||||||
@ -23,6 +25,7 @@ HWND trackViewWin = NULL;
|
|||||||
HWND statusBarWin = NULL;
|
HWND statusBarWin = NULL;
|
||||||
SyncDocument document;
|
SyncDocument document;
|
||||||
HKEY regConfigKey = NULL;
|
HKEY regConfigKey = NULL;
|
||||||
|
RecentFiles mruFileList(NULL);
|
||||||
|
|
||||||
#define WM_SETROWS (WM_USER+1)
|
#define WM_SETROWS (WM_USER+1)
|
||||||
#define WM_BIASSELECTION (WM_USER+2)
|
#define WM_BIASSELECTION (WM_USER+2)
|
||||||
@ -131,68 +134,6 @@ void setWindowFileName(std::string fileName)
|
|||||||
SetWindowText(hwnd, windowTitle.c_str());
|
SetWindowText(hwnd, windowTitle.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool setRegString(HKEY key, const std::string &name, const std::string &value)
|
|
||||||
{
|
|
||||||
return ERROR_SUCCESS == RegSetValueEx(key, name.c_str(), 0, REG_SZ, (BYTE *)value.c_str(), (DWORD)value.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool getRegString(HKEY key, const std::string &name, std::string &out)
|
|
||||||
{
|
|
||||||
DWORD size = 0;
|
|
||||||
DWORD type = 0;
|
|
||||||
if (ERROR_SUCCESS != RegQueryValueEx(key, name.c_str(), 0, &type, (LPBYTE)NULL, &size)) return false;
|
|
||||||
if (REG_SZ != type) return false;
|
|
||||||
|
|
||||||
out.resize(size);
|
|
||||||
DWORD ret = RegQueryValueEx(key, name.c_str(), 0, &type, (LPBYTE)&out[0], &size);
|
|
||||||
while (out.size() > 0 && out[out.size() - 1] == '\0') out.resize(out.size() - 1);
|
|
||||||
|
|
||||||
assert(ret == ERROR_SUCCESS);
|
|
||||||
assert(REG_SZ == type);
|
|
||||||
assert(size == out.size() + 1);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::list<std::string> mruList;
|
|
||||||
|
|
||||||
std::string getMruEntryName(size_t i)
|
|
||||||
{
|
|
||||||
std::string temp = std::string("RecentFile");
|
|
||||||
temp += char('0' + i);
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
void initMruList()
|
|
||||||
{
|
|
||||||
for (size_t i = 0; i < 5; ++i)
|
|
||||||
{
|
|
||||||
std::string fileName;
|
|
||||||
if (getRegString(regConfigKey, getMruEntryName(i), fileName))
|
|
||||||
{
|
|
||||||
mruList.push_back(fileName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void saveMruList()
|
|
||||||
{
|
|
||||||
std::list<std::string>::const_iterator it;
|
|
||||||
size_t i;
|
|
||||||
for (i = 0, it = mruList.begin(); it != mruList.end(); ++it, ++i)
|
|
||||||
{
|
|
||||||
assert(i <= 5);
|
|
||||||
setRegString(regConfigKey, getMruEntryName(i), *it);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void mruListInsert(const std::string fileName)
|
|
||||||
{
|
|
||||||
mruList.remove(fileName); // remove, if present
|
|
||||||
mruList.push_front(fileName); // add to front
|
|
||||||
while (mruList.size() > 5) mruList.pop_back(); // remove old entries
|
|
||||||
}
|
|
||||||
|
|
||||||
HMENU findSubMenuContaining(HMENU menu, UINT id)
|
HMENU findSubMenuContaining(HMENU menu, UINT id)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < GetMenuItemCount(menu); ++i)
|
for (int i = 0; i < GetMenuItemCount(menu); ++i)
|
||||||
@ -211,26 +152,6 @@ HMENU findSubMenuContaining(HMENU menu, UINT id)
|
|||||||
return (HMENU)0;
|
return (HMENU)0;
|
||||||
}
|
}
|
||||||
|
|
||||||
HMENU mruFileMenu;
|
|
||||||
|
|
||||||
void mruListUpdate()
|
|
||||||
{
|
|
||||||
while (0 != RemoveMenu(mruFileMenu, 0, MF_BYPOSITION));
|
|
||||||
|
|
||||||
std::list<std::string>::const_iterator it;
|
|
||||||
size_t i;
|
|
||||||
for (i = 0, it = mruList.begin(); it != mruList.end(); ++it, ++i)
|
|
||||||
{
|
|
||||||
assert(i <= 5);
|
|
||||||
std::string menuEntry = std::string("&");
|
|
||||||
menuEntry += char('1' + i);
|
|
||||||
menuEntry += " ";
|
|
||||||
menuEntry += *it;
|
|
||||||
AppendMenu(mruFileMenu, MF_STRING, ID_RECENTFILES_FILE1 + i, menuEntry.c_str());
|
|
||||||
}
|
|
||||||
DrawMenuBar(hwnd);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string fileName;
|
std::string fileName;
|
||||||
|
|
||||||
void fileNew()
|
void fileNew()
|
||||||
@ -256,8 +177,9 @@ void loadDocument(const std::string &_fileName)
|
|||||||
setWindowFileName(_fileName.c_str());
|
setWindowFileName(_fileName.c_str());
|
||||||
fileName = _fileName;
|
fileName = _fileName;
|
||||||
|
|
||||||
mruListInsert(_fileName);
|
mruFileList.insert(_fileName);
|
||||||
mruListUpdate();
|
mruFileList.update();
|
||||||
|
DrawMenuBar(hwnd);
|
||||||
|
|
||||||
document.clearUndoStack();
|
document.clearUndoStack();
|
||||||
document.clearRedoStack();
|
document.clearRedoStack();
|
||||||
@ -307,8 +229,9 @@ void fileSaveAs()
|
|||||||
setWindowFileName(temp);
|
setWindowFileName(temp);
|
||||||
fileName = temp;
|
fileName = temp;
|
||||||
|
|
||||||
mruListInsert(temp);
|
mruFileList.insert(temp);
|
||||||
mruListUpdate();
|
mruFileList.update();
|
||||||
|
DrawMenuBar(hwnd);
|
||||||
}
|
}
|
||||||
else MessageBox(hwnd, _T("Failed to save file"), mainWindowTitle, MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
|
else MessageBox(hwnd, _T("Failed to save file"), mainWindowTitle, MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
|
||||||
}
|
}
|
||||||
@ -373,9 +296,8 @@ static LRESULT CALLBACK mainWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARA
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Recent Files menu */
|
/* Recent Files menu */
|
||||||
mruFileMenu = findSubMenuContaining(GetMenu(hwnd), ID_RECENTFILES_NORECENTFILES);
|
mruFileList = RecentFiles(findSubMenuContaining(GetMenu(hwnd), ID_RECENTFILES_NORECENTFILES));
|
||||||
initMruList();
|
mruFileList.load(regConfigKey);
|
||||||
if (mruList.size() > 0) mruListUpdate();
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -384,7 +306,7 @@ static LRESULT CALLBACK mainWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARA
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case WM_DESTROY:
|
case WM_DESTROY:
|
||||||
saveMruList();
|
mruFileList.save(regConfigKey);
|
||||||
RegCloseKey(regConfigKey);
|
RegCloseKey(regConfigKey);
|
||||||
regConfigKey = NULL;
|
regConfigKey = NULL;
|
||||||
PostQuitMessage(0);
|
PostQuitMessage(0);
|
||||||
@ -447,15 +369,10 @@ static LRESULT CALLBACK mainWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARA
|
|||||||
case ID_RECENTFILES_FILE5:
|
case ID_RECENTFILES_FILE5:
|
||||||
{
|
{
|
||||||
int index = LOWORD(wParam) - ID_RECENTFILES_FILE1;
|
int index = LOWORD(wParam) - ID_RECENTFILES_FILE1;
|
||||||
std::list<std::string>::const_iterator it;
|
std::string fileName;
|
||||||
int i;
|
if (mruFileList.getEntry(index, fileName))
|
||||||
for (i = 0, it = mruList.begin(); it != mruList.end(); ++it, ++i)
|
|
||||||
{
|
{
|
||||||
if (i == index)
|
loadDocument(fileName);
|
||||||
{
|
|
||||||
loadDocument(*it);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user