1 #include "platform/wxwidgets/settings-common.hpp"
2 #include "core/instance.hpp"
3 #include "core/instance-map.hpp"
4 #include "core/keymapper.hpp"
8 std::map
<std::string
, settings_tab_factory
*>& _factories()
10 static std::map
<std::string
, settings_tab_factory
*> x
;
14 class wxeditor_esettings2
;
15 instance_map
<wxeditor_esettings2
> sdialogs
;
19 settings_tab_factory::settings_tab_factory(const std::string
& tabname
,
20 std::function
<settings_tab
*(wxWindow
* parent
, emulator_instance
& inst
)> create_fn
)
21 : _tabname(tabname
), _create_fn(create_fn
)
23 _factories()[_tabname
] = this;
26 settings_tab_factory::~settings_tab_factory()
28 _factories().erase(_tabname
);
31 std::list
<settings_tab_factory
*> settings_tab_factory::factories()
33 std::list
<settings_tab_factory
*> f
;
34 for(auto i
: _factories())
35 f
.push_back(i
.second
);
39 settings_menu::settings_menu(wxWindow
* win
, emulator_instance
& _inst
, int id
)
45 Append(id
, towxstring("All as tabs..."));
46 win
->Connect(id
++, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(settings_menu::on_selected
), NULL
,
49 for(auto i
: settings_tab_factory::factories()) {
51 Append(id
, towxstring(i
->get_name() + "..."));
52 win
->Connect(id
++, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(settings_menu::on_selected
),
57 void settings_menu::on_selected(wxCommandEvent
& e
)
63 display_settings_dialog(parent
, inst
, items
[id
]);
68 volatile bool keygrab_active
= false;
70 std::function
<void(std::string key
)> keygrab_callback
;
72 class keygrabber
: public keyboard::event_listener
77 keygrab_active
= false;
82 void on_key_event(keyboard::modifier_set
& mods
, keyboard::key
& key
, keyboard::event
& event
)
86 uint32_t dev
= event
.get_change_mask();
87 auto subkeys
= key
.get_subkeys();
88 for(unsigned i
= 0; i
< 16 && i
< subkeys
.size(); i
++) {
89 std::string pname
= key
.get_name() + subkeys
[i
];
90 if(((dev
>> (2 * i
)) & 3) == 3)
92 if(((dev
>> (2 * i
)) & 3) == 2) {
94 keygrab_active
= false;
95 std::string tmp
= pkey
;
96 runuifun([this, tmp
]() { this->keygrab_callback(tmp
); });
102 volatile bool keygrab_active
;
104 std::function
<void(std::string key
)> keygrab_callback
;
107 class wxeditor_esettings2
: public wxDialog
110 wxeditor_esettings2(emulator_instance
& _inst
, wxWindow
* parent
, settings_tab_factory
* singletab
);
111 ~wxeditor_esettings2();
112 bool ShouldPreventAppExit() const;
113 void on_close(wxCommandEvent
& e
);
116 emulator_instance
& inst
;
118 wxButton
* closebutton
;
119 std::list
<settings_tab
*> tabs
;
120 std::string
get_title(settings_tab_factory
* singletab
);
125 std::string
wxeditor_esettings2::get_title(settings_tab_factory
* singletab
)
128 return "lsnes: Configuration";
130 return "lsnes: Configuration: " + singletab
->get_name();
133 wxeditor_esettings2::wxeditor_esettings2(emulator_instance
& _inst
, wxWindow
* parent
,
134 settings_tab_factory
* singletab
)
135 : wxDialog(parent
, wxID_ANY
, towxstring(get_title(singletab
)), wxDefaultPosition
, wxSize(-1, -1),
136 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
| wxRESIZE_BORDER
), inst(_inst
)
139 //Grab keys to prevent the joystick driver from running who knows what commands.
140 inst
.keyboard
->set_exclusive(&keygrab
);
143 wxSizer
* top_s
= new wxBoxSizer(wxVERTICAL
);
147 //If this throws, let it throw through.
148 settings_tab
* t
= singletab
->create(this, inst
);
149 top_s
->Add(t
, 1, wxGROW
);
150 t
->set_notify([this]() { this->on_notify(); });
154 tabset
= new wxNotebook(this, -1, wxDefaultPosition
, wxDefaultSize
, wxNB_TOP
);
155 for(auto i
: settings_tab_factory::factories()) {
158 t
= i
->create(tabset
, inst
);
162 tabset
->AddPage(t
, towxstring(i
->get_name()));
163 t
->set_notify([this]() { this->on_notify(); });
167 top_s
->Add(tabset
, 1, wxGROW
);
169 throw std::runtime_error("Nothing to configure here, move along");
172 wxBoxSizer
* pbutton_s
= new wxBoxSizer(wxHORIZONTAL
);
173 pbutton_s
->AddStretchSpacer();
174 pbutton_s
->Add(closebutton
= new wxButton(this, wxID_ANY
, wxT("Close")), 0, wxGROW
);
175 closebutton
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
176 wxCommandEventHandler(wxeditor_esettings2::on_close
), NULL
, this);
177 top_s
->Add(pbutton_s
, 0, wxGROW
);
179 top_s
->SetSizeHints(this);
183 wxeditor_esettings2::~wxeditor_esettings2()
188 inst
.keyboard
->set_exclusive(NULL
);
189 sdialogs
.remove(inst
);
192 bool wxeditor_esettings2::ShouldPreventAppExit() const
197 void wxeditor_esettings2::on_close(wxCommandEvent
& e
)
205 void wxeditor_esettings2::on_notify()
213 void display_settings_dialog(wxWindow
* parent
, emulator_instance
& inst
, settings_tab_factory
* singletab
)
216 modal_pause_holder hld
;
220 editor
= sdialogs
.create(inst
, parent
, singletab
);
221 } catch(std::exception
& e
) {
222 std::string title
= "Configure";
224 title
= "Configure " + singletab
->get_name();
225 show_message_ok(parent
, title
, e
.what(), wxICON_EXCLAMATION
);
233 do_save_configuration();
236 void settings_activate_keygrab(emulator_instance
& inst
, std::function
<void(std::string key
)> callback
)
238 auto s
= sdialogs
.lookup(inst
);
240 s
->keygrab
.keygrab_callback
= callback
;
241 s
->keygrab
.keygrab_active
= true;
244 void settings_deactivate_keygrab(emulator_instance
& inst
)
246 auto s
= sdialogs
.lookup(inst
);
248 s
->keygrab
.keygrab_active
= false;