From 291ec7038ab5cd4ac59b17837fa0f657387d7c44 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Wed, 15 Jul 2015 00:32:36 -0400 Subject: [PATCH] Save window position and theme on exit --- src/AppConfig.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++ src/AppConfig.h | 9 +++++++ src/AppFrame.cpp | 39 ++++++++++++++++++++--------- src/AppFrame.h | 2 +- 4 files changed, 101 insertions(+), 13 deletions(-) diff --git a/src/AppConfig.cpp b/src/AppConfig.cpp index 11b4088..8fb6d92 100644 --- a/src/AppConfig.cpp +++ b/src/AppConfig.cpp @@ -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::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"); diff --git a/src/AppConfig.h b/src/AppConfig.h index f1bff72..9d98957 100644 --- a/src/AppConfig.h +++ b/src/AppConfig.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -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 deviceConfig; + std::atomic_int winX,winY,winW,winH; + std::atomic_int themeId; }; diff --git a/src/AppFrame.cpp b/src/AppFrame.cpp index f2eba43..363e304 100644 --- a/src/AppFrame.cpp +++ b/src/AppFrame.cpp @@ -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)) { diff --git a/src/AppFrame.h b/src/AppFrame.h index ebae67e..ec24119 100644 --- a/src/AppFrame.h +++ b/src/AppFrame.h @@ -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);