Add <functional> to files that use std::function
[lsnes.git] / src / platform / wxwidgets / projectmenu.cpp
blob785ade5936dc082a7e4a9a8220ab7bb9c6a6be37
1 #include <functional>
2 #include <wx/wx.h>
3 #include <wx/event.h>
4 #include <wx/control.h>
5 #include <wx/combobox.h>
7 #include "platform/wxwidgets/menu_projects.hpp"
8 #include "platform/wxwidgets/platform.hpp"
9 #include "library/eatarg.hpp"
10 #include "core/instance.hpp"
11 #include "core/project.hpp"
13 projects_menu::projects_menu(wxWindow* win, emulator_instance& _inst, int wxid_low, int wxid_high,
14 const std::string& cfg, std::function<void(const std::string& id)> cb)
15 : inst(_inst), hook(*this), rfiles(cfg, wxid_high - wxid_low - 1) //Reserve wxid_low and wxid_high.
17 CHECK_UI_THREAD;
18 pwin = win;
19 wxid_range_low = wxid_low;
20 wxid_range_high = wxid_high;
21 selected_cb = cb;
22 win->Connect(wxid_low, wxid_high, wxEVT_COMMAND_MENU_SELECTED,
23 wxCommandEventHandler(projects_menu::on_select), NULL, this);
24 rfiles.add_hook(hook);
25 update();
28 projects_menu::~projects_menu()
32 void projects_menu::on_select(wxCommandEvent& e)
34 CHECK_UI_THREAD;
35 int id = e.GetId();
36 if(id < wxid_range_low || id > wxid_range_high)
37 return;
38 if(id == wxid_range_low) {
39 //Other.
40 auto projects = inst.project->enumerate();
41 std::vector<std::string> a;
42 std::vector<wxString> b;
43 for(auto i : projects) {
44 a.push_back(i.first);
45 b.push_back(towxstring(i.second));
47 if(a.empty()) {
48 show_message_ok(pwin, "Load project", "No projects available", wxICON_EXCLAMATION);
49 return;
51 wxSingleChoiceDialog* d2 = new wxSingleChoiceDialog(pwin, wxT("Select project to switch to:"),
52 wxT("Load project"), b.size(), &b[0]);
53 if(d2->ShowModal() == wxID_CANCEL) {
54 d2->Destroy();
55 return;
57 std::string _id = a[d2->GetSelection()];
58 selected_cb(_id);
59 } else if(id == wxid_range_high) {
60 //Refresh.
61 update();
62 } else {
63 //Select.
64 if(entries.count(id))
65 selected_cb(entries[id]._id);
69 void projects_menu::update()
71 CHECK_UI_THREAD;
72 auto ents = rfiles.get();
73 int id = wxid_range_low + 1; //wxid_range_low is reserved for other.
74 for(auto i : items)
75 Delete(i.second);
76 items.clear();
77 bool has_ents = false;
78 for(auto i : ents) {
79 has_ents = true;
80 if(id >= wxid_range_high)
81 break;
82 entries[id] = i;
83 items[id] = Append(id, towxstring(i.display()));
84 id++;
86 if(has_ents)
87 items[-1] = AppendSeparator();
88 items[wxid_range_high] = Append(wxid_range_high, wxT("Refresh"));
89 items[-2] = AppendSeparator();
90 items[wxid_range_low] = Append(wxid_range_low, wxT("Select other..."));