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