moved recent files code to source-file

This commit is contained in:
Erik Faye-Lund 2008-10-14 23:23:14 +00:00
parent 3c42ac535a
commit 3fa3ed5c4d
4 changed files with 105 additions and 85 deletions

View File

@ -191,6 +191,10 @@
> >
</File> </File>
<File <File
RelativePath=".\recentfiles.cpp"
>
</File>
<File
RelativePath=".\syncdocument.cpp" RelativePath=".\syncdocument.cpp"
> >
</File> </File>
@ -225,6 +229,10 @@
> >
</File> </File>
<File <File
RelativePath=".\recentfiles.h"
>
</File>
<File
RelativePath=".\resource.h" RelativePath=".\resource.h"
> >
</File> </File>

89
editor/recentfiles.cpp Normal file
View File

@ -0,0 +1,89 @@
#include "recentfiles.h"
#include "resource.h"
static 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 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;
}
void RecentFiles::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 RecentFiles::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 RecentFiles::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 RecentFiles::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());
}
}
bool RecentFiles::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;
}

View File

@ -1,102 +1,22 @@
#pragma once #pragma once
#include "stdafx.h" #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 class RecentFiles
{ {
public: public:
RecentFiles(HMENU menu) : mruFileMenu(menu) { } RecentFiles(HMENU menu) : mruFileMenu(menu) { }
void load(HKEY key) void load(HKEY key);
{ void save(HKEY key);
for (size_t i = 0; i < 5; ++i) void insert(const std::string &fileName);
{ void update();
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 size_t getEntryCount() const
{ {
return mruList.size(); return mruList.size();
} }
bool getEntry(size_t index, std::string &out) const 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: private:
static std::string getEntryName(size_t i) static std::string getEntryName(size_t i)

View File

@ -7,6 +7,9 @@
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h> #include <windows.h>
#include <string>
#include <list>
#include <stdio.h> #include <stdio.h>
#include <tchar.h> #include <tchar.h>
#include <assert.h> #include <assert.h>