diff --git a/editor/editor_vs2008.vcproj b/editor/editor_vs2008.vcproj index 8262fe2..7f282e7 100644 --- a/editor/editor_vs2008.vcproj +++ b/editor/editor_vs2008.vcproj @@ -191,6 +191,10 @@ > + + @@ -225,6 +229,10 @@ > + + diff --git a/editor/recentfiles.cpp b/editor/recentfiles.cpp new file mode 100644 index 0000000..801c449 --- /dev/null +++ b/editor/recentfiles.cpp @@ -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::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::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::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; +} diff --git a/editor/recentfiles.h b/editor/recentfiles.h index 84bf305..8997c9b 100644 --- a/editor/recentfiles.h +++ b/editor/recentfiles.h @@ -1,102 +1,22 @@ #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::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::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()); - } - } + void load(HKEY key); + void save(HKEY key); + void insert(const std::string &fileName); + void update(); size_t getEntryCount() const { return mruList.size(); } - bool getEntry(size_t index, std::string &out) const - { - std::list::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; - } + bool getEntry(size_t index, std::string &out) const; private: static std::string getEntryName(size_t i) diff --git a/editor/stdafx.h b/editor/stdafx.h index 43ea78e..5136ef9 100644 --- a/editor/stdafx.h +++ b/editor/stdafx.h @@ -7,6 +7,9 @@ #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #include +#include +#include + #include #include #include