Rework jukebox
[lsnes.git] / src / platform / wxwidgets / editor-settings.cpp
blobdd5d3aac0536022eae80a109ab08942818d7baf6
1 #include "core/command.hpp"
2 #include "core/dispatch.hpp"
3 #include "core/mainloop.hpp"
4 #include "core/misc.hpp"
5 #include "core/moviefile.hpp"
6 #include "core/settings.hpp"
7 #include "core/window.hpp"
9 #include "platform/wxwidgets/platform.hpp"
11 #include <fstream>
12 #include <stdexcept>
15 #include <vector>
16 #include <string>
18 #include <wx/wx.h>
19 #include <wx/event.h>
20 #include <wx/control.h>
21 #include <wx/combobox.h>
23 class wxeditor_settings_setting : public wxEvtHandler
25 public:
26 wxeditor_settings_setting(wxSizer* sizer, wxWindow* window, const std::string& name);
27 void on_clear_click(wxCommandEvent& e);
28 void on_edit_click(wxCommandEvent& e);
29 void change_setting(const std::string& setting, const std::string& value);
30 void clear_setting(const std::string& setting);
31 private:
32 std::string a_name;
33 wxWindow* parent;
34 wxStaticText* label;
35 wxButton* clear;
36 wxButton* edit;
39 class wxeditor_settings;
41 class wxeditor_settings_listener : public information_dispatch
43 public:
44 wxeditor_settings_listener(wxeditor_settings* _editor);
45 ~wxeditor_settings_listener() throw();
46 void on_setting_change(const std::string& setting, const std::string& value);
47 void on_setting_clear(const std::string& setting);
48 private:
49 wxeditor_settings* editor;
52 class wxeditor_settings : public wxDialog
54 public:
55 wxeditor_settings(wxWindow* parent);
56 ~wxeditor_settings();
57 bool ShouldPreventAppExit() const;
58 void on_close(wxCommandEvent& e);
59 void change_setting(const std::string& setting, const std::string& value);
60 void clear_setting(const std::string& setting);
61 private:
62 wxeditor_settings_listener listener;
63 std::vector<wxeditor_settings_setting*> esettings;
64 wxScrolledWindow* scrollwin;
65 wxButton* close;
68 wxeditor_settings_setting::wxeditor_settings_setting(wxSizer* sizer, wxWindow* window, const std::string& name)
70 a_name = name;
71 parent = window;
72 std::string pvalue = "<unknown>";
73 runemufn([&pvalue, a_name]() {
74 try {
75 if(!setting::is_set(a_name))
76 pvalue = "<unset>";
77 else
78 pvalue = setting::get(a_name);
79 } catch(...) {
81 });
82 sizer->Add(new wxStaticText(window, wxID_ANY, towxstring(name)), 0, wxGROW);
83 sizer->Add(label = new wxStaticText(window, wxID_ANY, towxstring(pvalue)), 0, wxGROW);
84 sizer->Add(edit = new wxButton(window, wxID_ANY, wxT("Edit")), 0, wxGROW);
85 sizer->Add(clear = new wxButton(window, wxID_ANY, wxT("Clear")), 0, wxGROW);
86 edit->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
87 wxCommandEventHandler(wxeditor_settings_setting::on_edit_click), NULL, this);
88 clear->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
89 wxCommandEventHandler(wxeditor_settings_setting::on_clear_click), NULL, this);
92 void wxeditor_settings_setting::on_clear_click(wxCommandEvent& e)
94 try {
95 bool fault = false;;
96 std::string faulttext;
97 runemufn([a_name, &fault, &faulttext]() {
98 try {
99 setting::blank(a_name);
100 } catch(std::exception& e) {
101 fault = true;
102 faulttext = e.what();
105 if(fault) {
106 wxMessageDialog* d = new wxMessageDialog(parent,
107 towxstring(std::string("Can't clear setting: ") + faulttext), wxT("Error"), wxOK |
108 wxICON_EXCLAMATION);
109 d->ShowModal();
110 d->Destroy();
112 }catch(std::exception& e) {
113 wxMessageDialog* d = new wxMessageDialog(parent, towxstring(std::string("Can't clear setting: ") +
114 e.what()), wxT("Error"), wxOK | wxICON_EXCLAMATION);
115 d->ShowModal();
116 d->Destroy();
120 void wxeditor_settings_setting::on_edit_click(wxCommandEvent& e)
122 try {
123 std::string newsetting;
124 std::string oldvalue = setting::get(a_name);
125 wxTextEntryDialog* d = new wxTextEntryDialog(parent, towxstring("Enter new value for " + a_name),
126 wxT("Enter new value for setting"), towxstring(oldvalue));
127 if(d->ShowModal() == wxID_CANCEL) {
128 d->Destroy();
129 return;
131 newsetting = tostdstring(d->GetValue());
132 bool fault = false;;
133 std::string faulttext;
134 runemufn([a_name, newsetting, &fault, &faulttext]() {
135 try {
136 setting::set(a_name, newsetting);
137 } catch(std::exception& e) {
138 fault = true;
139 faulttext = e.what();
142 if(fault) {
143 wxMessageDialog* d = new wxMessageDialog(parent,
144 towxstring(std::string("Can't set setting: ") + faulttext), wxT("Error"), wxOK |
145 wxICON_EXCLAMATION);
146 d->ShowModal();
147 d->Destroy();
149 } catch(std::exception& e) {
150 wxMessageDialog* d = new wxMessageDialog(parent, towxstring(std::string("Can't set setting: ") +
151 e.what()), wxT("Error"), wxOK | wxICON_EXCLAMATION);
152 d->ShowModal();
153 d->Destroy();
157 void wxeditor_settings_setting::change_setting(const std::string& _setting, const std::string& value)
159 runuifun([_setting, label, a_name, value]() {
160 if(_setting != a_name)
161 return;
162 label->SetLabel(towxstring(value));
166 void wxeditor_settings_setting::clear_setting(const std::string& _setting)
168 runuifun([_setting, label, a_name]() {
169 if(_setting != a_name)
170 return;
171 label->SetLabel(wxT("<unset>"));
175 wxeditor_settings::wxeditor_settings(wxWindow* parent)
176 : wxDialog(parent, wxID_ANY, wxT("lsnes: Edit settings"), wxDefaultPosition, wxSize(-1, -1)), listener(this)
178 std::set<std::string> settings_set;
179 runemufn([&settings_set]() { settings_set = setting::get_settings_set(); });
181 scrollwin = new wxScrolledWindow(this, wxID_ANY, wxDefaultPosition, wxSize(-1, -1), wxVSCROLL);
182 scrollwin->SetMinSize(wxSize(-1, 500));
184 Centre();
185 wxFlexGridSizer* top_s = new wxFlexGridSizer(2, 1, 0, 0);
186 SetSizer(top_s);
188 wxFlexGridSizer* t_s = new wxFlexGridSizer(settings_set.size(), 4, 0, 0);
189 for(auto i : settings_set)
190 esettings.push_back(new wxeditor_settings_setting(t_s, scrollwin, i));
191 scrollwin->SetSizer(t_s);
192 top_s->Add(scrollwin);
193 scrollwin->SetScrollRate(0, 20);
195 wxBoxSizer* pbutton_s = new wxBoxSizer(wxHORIZONTAL);
196 pbutton_s->AddStretchSpacer();
197 pbutton_s->Add(close = new wxButton(this, wxID_CANCEL, wxT("Close")), 0, wxGROW);
198 close->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
199 wxCommandEventHandler(wxeditor_settings::on_close), NULL, this);
200 top_s->Add(pbutton_s, 0, wxGROW);
202 t_s->SetSizeHints(this);
203 top_s->SetSizeHints(this);
204 t_s->Layout();
205 top_s->Layout();
206 Fit();
209 wxeditor_settings::~wxeditor_settings()
211 for(auto i : esettings)
212 delete i;
215 bool wxeditor_settings::ShouldPreventAppExit() const
217 return false;
220 void wxeditor_settings::on_close(wxCommandEvent& e)
222 EndModal(wxID_OK);
225 void wxeditor_settings::change_setting(const std::string& _setting, const std::string& value)
227 for(auto i : esettings)
228 i->change_setting(_setting, value);
231 void wxeditor_settings::clear_setting(const std::string& _setting)
233 for(auto i : esettings)
234 i->clear_setting(_setting);
237 wxeditor_settings_listener::wxeditor_settings_listener(wxeditor_settings* _editor)
238 : information_dispatch("wxeditor_settings-listener")
240 editor = _editor;
243 wxeditor_settings_listener::~wxeditor_settings_listener() throw()
247 void wxeditor_settings_listener::on_setting_change(const std::string& _setting, const std::string& value)
249 editor->change_setting(_setting, value);
252 void wxeditor_settings_listener::on_setting_clear(const std::string& _setting)
254 editor->clear_setting(_setting);
257 void wxeditor_settings_display(wxWindow* parent)
259 modal_pause_holder hld;
260 wxDialog* editor;
261 try {
262 editor = new wxeditor_settings(parent);
263 editor->ShowModal();
264 } catch(...) {
266 editor->Destroy();