From ae42dc5909ef2da1ec7ce8cc0722f64f3eec7bd8 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Sat, 7 Apr 2012 20:02:51 +0300 Subject: [PATCH] Wxwidgets: Move settings to main configuration dialog --- include/platform/wxwidgets/platform.hpp | 1 - src/platform/wxwidgets/editor-settings.cpp | 267 ----------------------------- src/platform/wxwidgets/mainwindow.cpp | 5 - src/platform/wxwidgets/settings.cpp | 147 +++++++++++++++- 4 files changed, 145 insertions(+), 275 deletions(-) delete mode 100644 src/platform/wxwidgets/editor-settings.cpp diff --git a/include/platform/wxwidgets/platform.hpp b/include/platform/wxwidgets/platform.hpp index cd99bc10..b76eb137 100644 --- a/include/platform/wxwidgets/platform.hpp +++ b/include/platform/wxwidgets/platform.hpp @@ -34,7 +34,6 @@ void _runuifun_async(void (*fn)(void*), void* arg); //Editor dialogs. void wxeditor_authors_display(wxWindow* parent); -void wxeditor_settings_display(wxWindow* parent); void wxeditor_hotkeys_display(wxWindow* parent); std::string wxeditor_keyselect(wxWindow* parent, bool clearable); void wxsetingsdialog_display(wxWindow* parent); diff --git a/src/platform/wxwidgets/editor-settings.cpp b/src/platform/wxwidgets/editor-settings.cpp deleted file mode 100644 index dd5d3aac..00000000 --- a/src/platform/wxwidgets/editor-settings.cpp +++ /dev/null @@ -1,267 +0,0 @@ -#include "core/command.hpp" -#include "core/dispatch.hpp" -#include "core/mainloop.hpp" -#include "core/misc.hpp" -#include "core/moviefile.hpp" -#include "core/settings.hpp" -#include "core/window.hpp" - -#include "platform/wxwidgets/platform.hpp" - -#include -#include - - -#include -#include - -#include -#include -#include -#include - -class wxeditor_settings_setting : public wxEvtHandler -{ -public: - wxeditor_settings_setting(wxSizer* sizer, wxWindow* window, const std::string& name); - void on_clear_click(wxCommandEvent& e); - void on_edit_click(wxCommandEvent& e); - void change_setting(const std::string& setting, const std::string& value); - void clear_setting(const std::string& setting); -private: - std::string a_name; - wxWindow* parent; - wxStaticText* label; - wxButton* clear; - wxButton* edit; -}; - -class wxeditor_settings; - -class wxeditor_settings_listener : public information_dispatch -{ -public: - wxeditor_settings_listener(wxeditor_settings* _editor); - ~wxeditor_settings_listener() throw(); - void on_setting_change(const std::string& setting, const std::string& value); - void on_setting_clear(const std::string& setting); -private: - wxeditor_settings* editor; -}; - -class wxeditor_settings : public wxDialog -{ -public: - wxeditor_settings(wxWindow* parent); - ~wxeditor_settings(); - bool ShouldPreventAppExit() const; - void on_close(wxCommandEvent& e); - void change_setting(const std::string& setting, const std::string& value); - void clear_setting(const std::string& setting); -private: - wxeditor_settings_listener listener; - std::vector esettings; - wxScrolledWindow* scrollwin; - wxButton* close; -}; - -wxeditor_settings_setting::wxeditor_settings_setting(wxSizer* sizer, wxWindow* window, const std::string& name) -{ - a_name = name; - parent = window; - std::string pvalue = ""; - runemufn([&pvalue, a_name]() { - try { - if(!setting::is_set(a_name)) - pvalue = ""; - else - pvalue = setting::get(a_name); - } catch(...) { - } - }); - sizer->Add(new wxStaticText(window, wxID_ANY, towxstring(name)), 0, wxGROW); - sizer->Add(label = new wxStaticText(window, wxID_ANY, towxstring(pvalue)), 0, wxGROW); - sizer->Add(edit = new wxButton(window, wxID_ANY, wxT("Edit")), 0, wxGROW); - sizer->Add(clear = new wxButton(window, wxID_ANY, wxT("Clear")), 0, wxGROW); - edit->Connect(wxEVT_COMMAND_BUTTON_CLICKED, - wxCommandEventHandler(wxeditor_settings_setting::on_edit_click), NULL, this); - clear->Connect(wxEVT_COMMAND_BUTTON_CLICKED, - wxCommandEventHandler(wxeditor_settings_setting::on_clear_click), NULL, this); -} - -void wxeditor_settings_setting::on_clear_click(wxCommandEvent& e) -{ - try { - bool fault = false;; - std::string faulttext; - runemufn([a_name, &fault, &faulttext]() { - try { - setting::blank(a_name); - } catch(std::exception& e) { - fault = true; - faulttext = e.what(); - } - }); - if(fault) { - wxMessageDialog* d = new wxMessageDialog(parent, - towxstring(std::string("Can't clear setting: ") + faulttext), wxT("Error"), wxOK | - wxICON_EXCLAMATION); - d->ShowModal(); - d->Destroy(); - } - }catch(std::exception& e) { - wxMessageDialog* d = new wxMessageDialog(parent, towxstring(std::string("Can't clear setting: ") + - e.what()), wxT("Error"), wxOK | wxICON_EXCLAMATION); - d->ShowModal(); - d->Destroy(); - } -} - -void wxeditor_settings_setting::on_edit_click(wxCommandEvent& e) -{ - try { - std::string newsetting; - std::string oldvalue = setting::get(a_name); - wxTextEntryDialog* d = new wxTextEntryDialog(parent, towxstring("Enter new value for " + a_name), - wxT("Enter new value for setting"), towxstring(oldvalue)); - if(d->ShowModal() == wxID_CANCEL) { - d->Destroy(); - return; - } - newsetting = tostdstring(d->GetValue()); - bool fault = false;; - std::string faulttext; - runemufn([a_name, newsetting, &fault, &faulttext]() { - try { - setting::set(a_name, newsetting); - } catch(std::exception& e) { - fault = true; - faulttext = e.what(); - } - }); - if(fault) { - wxMessageDialog* d = new wxMessageDialog(parent, - towxstring(std::string("Can't set setting: ") + faulttext), wxT("Error"), wxOK | - wxICON_EXCLAMATION); - d->ShowModal(); - d->Destroy(); - } - } catch(std::exception& e) { - wxMessageDialog* d = new wxMessageDialog(parent, towxstring(std::string("Can't set setting: ") + - e.what()), wxT("Error"), wxOK | wxICON_EXCLAMATION); - d->ShowModal(); - d->Destroy(); - } -} - -void wxeditor_settings_setting::change_setting(const std::string& _setting, const std::string& value) -{ - runuifun([_setting, label, a_name, value]() { - if(_setting != a_name) - return; - label->SetLabel(towxstring(value)); - }); -} - -void wxeditor_settings_setting::clear_setting(const std::string& _setting) -{ - runuifun([_setting, label, a_name]() { - if(_setting != a_name) - return; - label->SetLabel(wxT("")); - }); -} - -wxeditor_settings::wxeditor_settings(wxWindow* parent) - : wxDialog(parent, wxID_ANY, wxT("lsnes: Edit settings"), wxDefaultPosition, wxSize(-1, -1)), listener(this) -{ - std::set settings_set; - runemufn([&settings_set]() { settings_set = setting::get_settings_set(); }); - - scrollwin = new wxScrolledWindow(this, wxID_ANY, wxDefaultPosition, wxSize(-1, -1), wxVSCROLL); - scrollwin->SetMinSize(wxSize(-1, 500)); - - Centre(); - wxFlexGridSizer* top_s = new wxFlexGridSizer(2, 1, 0, 0); - SetSizer(top_s); - - wxFlexGridSizer* t_s = new wxFlexGridSizer(settings_set.size(), 4, 0, 0); - for(auto i : settings_set) - esettings.push_back(new wxeditor_settings_setting(t_s, scrollwin, i)); - scrollwin->SetSizer(t_s); - top_s->Add(scrollwin); - scrollwin->SetScrollRate(0, 20); - - wxBoxSizer* pbutton_s = new wxBoxSizer(wxHORIZONTAL); - pbutton_s->AddStretchSpacer(); - pbutton_s->Add(close = new wxButton(this, wxID_CANCEL, wxT("Close")), 0, wxGROW); - close->Connect(wxEVT_COMMAND_BUTTON_CLICKED, - wxCommandEventHandler(wxeditor_settings::on_close), NULL, this); - top_s->Add(pbutton_s, 0, wxGROW); - - t_s->SetSizeHints(this); - top_s->SetSizeHints(this); - t_s->Layout(); - top_s->Layout(); - Fit(); -} - -wxeditor_settings::~wxeditor_settings() -{ - for(auto i : esettings) - delete i; -} - -bool wxeditor_settings::ShouldPreventAppExit() const -{ - return false; -} - -void wxeditor_settings::on_close(wxCommandEvent& e) -{ - EndModal(wxID_OK); -} - -void wxeditor_settings::change_setting(const std::string& _setting, const std::string& value) -{ - for(auto i : esettings) - i->change_setting(_setting, value); -} - -void wxeditor_settings::clear_setting(const std::string& _setting) -{ - for(auto i : esettings) - i->clear_setting(_setting); -} - -wxeditor_settings_listener::wxeditor_settings_listener(wxeditor_settings* _editor) - : information_dispatch("wxeditor_settings-listener") -{ - editor = _editor; -} - -wxeditor_settings_listener::~wxeditor_settings_listener() throw() -{ -} - -void wxeditor_settings_listener::on_setting_change(const std::string& _setting, const std::string& value) -{ - editor->change_setting(_setting, value); -} - -void wxeditor_settings_listener::on_setting_clear(const std::string& _setting) -{ - editor->clear_setting(_setting); -} - -void wxeditor_settings_display(wxWindow* parent) -{ - modal_pause_holder hld; - wxDialog* editor; - try { - editor = new wxeditor_settings(parent); - editor->ShowModal(); - } catch(...) { - } - editor->Destroy(); -} diff --git a/src/platform/wxwidgets/mainwindow.cpp b/src/platform/wxwidgets/mainwindow.cpp index fd14cf57..fadf3c51 100644 --- a/src/platform/wxwidgets/mainwindow.cpp +++ b/src/platform/wxwidgets/mainwindow.cpp @@ -63,7 +63,6 @@ enum wxID_EDIT_AUTHORS, wxID_AUTOHOLD_FIRST, wxID_AUTOHOLD_LAST = wxID_AUTOHOLD_FIRST + 1023, - wxID_EDIT_SETTINGS, wxID_EDIT_KEYBINDINGS, wxID_EDIT_ALIAS, wxID_EDIT_MEMORYWATCH, @@ -767,7 +766,6 @@ wxwin_mainwindow::wxwin_mainwindow() wxID_DUMP_FIRST, wxID_DUMP_LAST))); menu_start(wxT("Settings")); - menu_entry(wxID_EDIT_SETTINGS, wxT("Configure settings...")); menu_entry(wxID_EDIT_KEYBINDINGS, wxT("Configure keybindings...")); menu_entry(wxID_EDIT_ALIAS, wxT("Configure aliases...")); menu_separator(); @@ -914,9 +912,6 @@ void wxwin_mainwindow::handle_menu_click_cancelable(wxCommandEvent& e) case wxID_EDIT_AUTHORS: wxeditor_authors_display(this); return; - case wxID_EDIT_SETTINGS: - wxeditor_settings_display(this); - return; case wxID_EDIT_HOTKEYS: wxeditor_hotkeys_display(this); return; diff --git a/src/platform/wxwidgets/settings.cpp b/src/platform/wxwidgets/settings.cpp index 3978ff7c..978498aa 100644 --- a/src/platform/wxwidgets/settings.cpp +++ b/src/platform/wxwidgets/settings.cpp @@ -1,4 +1,5 @@ #include "platform/wxwidgets/platform.hpp" +#include "core/dispatch.hpp" #include "core/settings.hpp" #include "library/string.hpp" @@ -469,7 +470,12 @@ void wxeditor_esettings_screen::on_configure(wxCommandEvent& e) { if(e.GetId() == wxID_HIGHEST + 1) { std::string v = (stringfmt() << horizontal_scale_factor).str(); - v = pick_text(this, "Set X scaling factor", "Enter new horizontal scale factor:", v); + try { + v = pick_text(this, "Set X scaling factor", "Enter new horizontal scale factor:", v); + } catch(...) { + refresh(); + return; + } double x; try { x = parse_value(v); @@ -484,7 +490,12 @@ void wxeditor_esettings_screen::on_configure(wxCommandEvent& e) horizontal_scale_factor = x; } else if(e.GetId() == wxID_HIGHEST + 2) { std::string v = (stringfmt() << vertical_scale_factor).str(); - v = pick_text(this, "Set Y scaling factor", "Enter new vertical scale factor:", v); + try { + v = pick_text(this, "Set Y scaling factor", "Enter new vertical scale factor:", v); + } catch(...) { + refresh(); + return; + } double x; try { x = parse_value(v); @@ -526,6 +537,137 @@ void wxeditor_esettings_screen::refresh() Fit(); } +class wxeditor_esettings_advanced : public wxPanel, information_dispatch +{ +public: + wxeditor_esettings_advanced(wxWindow* parent); + ~wxeditor_esettings_advanced(); + void on_change(wxCommandEvent& e); + void on_clear(wxCommandEvent& e); + void on_setting_change(const std::string& setting, const std::string& value); + void on_setting_clear(const std::string& setting); + void _refresh(); +private: + void refresh(); + std::set settings; + std::map values; + std::map selections; + std::string selected(); + wxListBox* _settings; +}; + +wxeditor_esettings_advanced::wxeditor_esettings_advanced(wxWindow* parent) + : wxPanel(parent, -1), information_dispatch("wxeditor-settings-listener") +{ + wxButton* tmp; + + wxSizer* top_s = new wxBoxSizer(wxVERTICAL); + SetSizer(top_s); + + top_s->Add(_settings = new wxListBox(this, wxID_ANY), 1, wxGROW); + + wxBoxSizer* pbutton_s = new wxBoxSizer(wxHORIZONTAL); + pbutton_s->AddStretchSpacer(); + pbutton_s->Add(tmp = new wxButton(this, wxID_ANY, wxT("Change")), 0, wxGROW); + tmp->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxeditor_esettings_advanced::on_change), NULL, + this); + pbutton_s->Add(tmp = new wxButton(this, wxID_ANY, wxT("Clear")), 0, wxGROW); + tmp->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxeditor_esettings_advanced::on_clear), NULL, + this); + top_s->Add(pbutton_s, 0, wxGROW); + + refresh(); + top_s->SetSizeHints(this); + Fit(); +} + +wxeditor_esettings_advanced::~wxeditor_esettings_advanced() +{ +} + +void wxeditor_esettings_advanced::on_change(wxCommandEvent& e) +{ + std::string name = selected(); + if(name == "") + return; + std::string value; + std::string err; + runemufn([name, &value]() { value = setting::get(name); }); + try { + value = pick_text(this, "Set value to", "Set " + name + " to value:", value); + } catch(...) { + return; + } + runemufn([name, value, &err]() { + try { setting::set(name, value); } catch(std::exception& e) { err = e.what(); } + }); + if(err != "") + wxMessageBox(towxstring(err), wxT("Error setting value"), wxICON_EXCLAMATION | wxOK); +} + +void wxeditor_esettings_advanced::on_clear(wxCommandEvent& e) +{ + std::string name = selected(); + if(name == "") + return; + bool err = false; + runemufn([name, &err]() { try { setting::blank(name); } catch(...) { err = true; }}); + if(err) + wxMessageBox(wxT("This setting can't be cleared"), wxT("Error"), wxICON_EXCLAMATION | wxOK); +} + +void wxeditor_esettings_advanced::on_setting_change(const std::string& setting, const std::string& value) +{ + wxeditor_esettings_advanced* th = this; + runuifun([&settings, &values, setting, value, th]() { + settings.insert(setting); values[setting] = value; th->_refresh(); + }); +} + +void wxeditor_esettings_advanced::on_setting_clear(const std::string& setting) +{ + wxeditor_esettings_advanced* th = this; + runuifun([&settings, &values, setting, th]() { + settings.insert(setting); values.erase(setting); th->_refresh(); + }); +} + +void wxeditor_esettings_advanced::refresh() +{ + runemufn([&settings, &values]() { + settings = setting::get_settings_set(); + for(auto i : settings) { + if(setting::is_set(i)) + values[i] = setting::get(i); + } + }); + _refresh(); +} + +std::string wxeditor_esettings_advanced::selected() +{ + int x = _settings->GetSelection(); + if(selections.count(x)) + return selections[x]; + else + return ""; +} + +void wxeditor_esettings_advanced::_refresh() +{ + std::vector strings; + int k = 0; + for(auto i : settings) { + if(values.count(i)) + strings.push_back(towxstring(i + " (Value: " + values[i] + ")")); + else + strings.push_back(towxstring(i + " (Not set)")); + selections[k++] = i; + } + _settings->Set(strings.size(), &strings[0]); +} + + class wxeditor_esettings : public wxDialog { public: @@ -550,6 +692,7 @@ wxeditor_esettings::wxeditor_esettings(wxWindow* parent) tabset->AddPage(new wxeditor_esettings_joystick(tabset), wxT("Joysticks")); tabset->AddPage(new wxeditor_esettings_paths(tabset), wxT("Paths")); tabset->AddPage(new wxeditor_esettings_screen(tabset), wxT("Scaling")); + tabset->AddPage(new wxeditor_esettings_advanced(tabset), wxT("Advanced")); top_s->Add(tabset, 1, wxGROW); wxBoxSizer* pbutton_s = new wxBoxSizer(wxHORIZONTAL); -- 2.11.4.GIT