Save window position and theme on exit

This commit is contained in:
Charles J. Cliffe 2015-07-15 00:32:36 -04:00
parent 1d20fd16bb
commit 291ec7038a
4 changed files with 101 additions and 13 deletions

View File

@ -142,10 +142,48 @@ std::string AppConfig::getConfigDir() {
return dataDir;
}
void AppConfig::setWindow(wxPoint winXY, wxSize winWH) {
winX.store(winXY.x);
winY.store(winXY.y);
winW.store(winWH.x);
winH.store(winWH.y);
}
wxRect *AppConfig::getWindow() {
wxRect *r = NULL;
if (winH.load() && winW.load()) {
r = new wxRect(winX.load(),winY.load(),winW.load(),winH.load());
}
return r;
}
void AppConfig::setTheme(int themeId) {
this->themeId.store(themeId);
}
int AppConfig::getTheme() {
return themeId.load();
}
bool AppConfig::save() {
DataTree cfg;
cfg.rootNode()->setName("cubicsdr_config");
if (winW.load() && winH.load()) {
DataNode *window_node = cfg.rootNode()->newChild("window");
*window_node->newChild("x") = winX.load();
*window_node->newChild("y") = winY.load();
*window_node->newChild("w") = winW.load();
*window_node->newChild("h") = winH.load();
*window_node->newChild("theme") = themeId.load();
}
DataNode *devices_node = cfg.rootNode()->newChild("devices");
std::map<std::string, DeviceConfig *>::iterator device_config_i;
@ -154,6 +192,7 @@ bool AppConfig::save() {
device_config_i->second->save(device_node);
}
std::string cfgFileDir = getConfigDir();
wxFileName cfgFile = wxFileName(cfgFileDir, "config.xml");
@ -187,6 +226,31 @@ bool AppConfig::load() {
return false;
}
if (cfg.rootNode()->hasAnother("window")) {
int x,y,w,h;
DataNode *win_node = cfg.rootNode()->getNext("window");
if (win_node->hasAnother("w") && win_node->hasAnother("h") && win_node->hasAnother("x") && win_node->hasAnother("y")) {
win_node->getNext("x")->element()->get(x);
win_node->getNext("y")->element()->get(y);
win_node->getNext("w")->element()->get(w);
win_node->getNext("h")->element()->get(h);
winX.store(x);
winY.store(y);
winW.store(w);
winH.store(h);
}
if (win_node->hasAnother("theme")) {
int theme;
win_node->getNext("theme")->element()->get(theme);
themeId.store(theme);
}
}
if (cfg.rootNode()->hasAnother("devices")) {
DataNode *devices_node = cfg.rootNode()->getNext("devices");

View File

@ -3,6 +3,7 @@
#include <wx/stdpaths.h>
#include <wx/dir.h>
#include <wx/filename.h>
#include <wx/panel.h>
#include <atomic>
#include <mutex>
@ -45,10 +46,18 @@ public:
std::string getConfigDir();
DeviceConfig *getDevice(std::string deviceId);
void setWindow(wxPoint winXY, wxSize winWH);
wxRect *getWindow();
void setTheme(int themeId);
int getTheme();
bool save();
bool load();
bool reset();
private:
std::map<std::string, DeviceConfig *> deviceConfig;
std::atomic_int winX,winY,winW,winH;
std::atomic_int themeId;
};

View File

@ -31,7 +31,7 @@
wxBEGIN_EVENT_TABLE(AppFrame, wxFrame)
//EVT_MENU(wxID_NEW, AppFrame::OnNewWindow)
EVT_MENU(wxID_CLOSE, AppFrame::OnClose)
EVT_CLOSE(AppFrame::OnClose)
EVT_MENU(wxID_ANY, AppFrame::OnMenu)
EVT_COMMAND(wxID_ANY, wxEVT_THREAD, AppFrame::OnThread)
EVT_IDLE(AppFrame::OnIdle)
@ -190,13 +190,15 @@ AppFrame::AppFrame() :
menu = new wxMenu;
menu->AppendRadioItem(wxID_THEME_DEFAULT, "Default")->Check(true);
menu->AppendRadioItem(wxID_THEME_RADAR, "RADAR");
menu->AppendRadioItem(wxID_THEME_BW, "Black & White");
menu->AppendRadioItem(wxID_THEME_SHARP, "Sharp");
menu->AppendRadioItem(wxID_THEME_RAD, "Rad");
menu->AppendRadioItem(wxID_THEME_TOUCH, "Touch");
menu->AppendRadioItem(wxID_THEME_HD, "HD");
int themeId = wxGetApp().getConfig()->getTheme();
menu->AppendRadioItem(wxID_THEME_DEFAULT, "Default")->Check(themeId==COLOR_THEME_DEFAULT);
menu->AppendRadioItem(wxID_THEME_RADAR, "RADAR")->Check(themeId==COLOR_THEME_RADAR);
menu->AppendRadioItem(wxID_THEME_BW, "Black & White")->Check(themeId==COLOR_THEME_BW);
menu->AppendRadioItem(wxID_THEME_SHARP, "Sharp")->Check(themeId==COLOR_THEME_SHARP);
menu->AppendRadioItem(wxID_THEME_RAD, "Rad")->Check(themeId==COLOR_THEME_RAD);
menu->AppendRadioItem(wxID_THEME_TOUCH, "Touch")->Check(themeId==COLOR_THEME_TOUCH);
menu->AppendRadioItem(wxID_THEME_HD, "HD")->Check(themeId==COLOR_THEME_HD);
menuBar->Append(menu, wxT("&Color Scheme"));
@ -302,8 +304,18 @@ AppFrame::AppFrame() :
SetMenuBar(menuBar);
CreateStatusBar();
SetClientSize(1280, 600);
Centre();
wxRect *win = wxGetApp().getConfig()->getWindow();
if (win) {
this->SetPosition(win->GetPosition());
this->SetClientSize(win->GetSize());
} else {
SetClientSize(1280, 600);
Centre();
}
ThemeMgr::mgr.setTheme(wxGetApp().getConfig()->getTheme());
Show();
#ifdef _WIN32
@ -506,8 +518,11 @@ void AppFrame::OnMenu(wxCommandEvent& event) {
}
void AppFrame::OnClose(wxCommandEvent& WXUNUSED(event)) {
Close(false);
void AppFrame::OnClose(wxCloseEvent& event) {
wxGetApp().getConfig()->setWindow(this->GetPosition(), this->GetClientSize());
wxGetApp().getConfig()->setTheme(ThemeMgr::mgr.getTheme());
wxGetApp().getConfig()->save();
event.Skip();
}
void AppFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event)) {

View File

@ -65,7 +65,7 @@ public:
private:
void OnMenu(wxCommandEvent& event);
void OnClose(wxCommandEvent& event);
void OnClose(wxCloseEvent& event);
void OnNewWindow(wxCommandEvent& event);
void OnIdle(wxIdleEvent& event);