Get rid of win32-crap.[ch]pp
[lsnes.git] / platform / wxwidgets / src / keyentry.cpp
blobe476005b05e9cc564b301310ea9389890d0bca71
1 #include "keyentry.hpp"
2 #include "common.hpp"
3 #include "keymapper.hpp"
5 #define S_DONT_CARE "Ignore"
6 #define S_RELEASED "Released"
7 #define S_PRESSED "Pressed"
9 wx_key_entry::wx_key_entry(wxWindow* parent)
10 : wxDialog(parent, wxID_ANY, wxT("Specify key"), wxDefaultPosition, wxSize(-1, -1))
12 std::vector<wxString> keych;
14 std::set<std::string> mods = modifier::get_set();
15 std::set<std::string> keys = keygroup::get_keys();
16 Centre();
17 wxFlexGridSizer* top_s = new wxFlexGridSizer(2, 1, 0, 0);
18 SetSizer(top_s);
20 wxFlexGridSizer* t_s = new wxFlexGridSizer(mods.size() + 1, 3, 0, 0);
21 for(auto i : mods) {
22 t_s->Add(new wxStaticText(this, wxID_ANY, towxstring(i)), 0, wxGROW);
23 keyentry_mod_data m;
24 t_s->Add(m.pressed = new wxCheckBox(this, wxID_ANY, wxT("Pressed")), 0, wxGROW);
25 t_s->Add(m.unmasked = new wxCheckBox(this, wxID_ANY, wxT("Unmasked")), 0, wxGROW);
26 m.pressed->Disable();
27 modifiers[i] = m;
28 m.pressed->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED,
29 wxCommandEventHandler(wx_key_entry::on_change_setting), NULL, this);
30 m.unmasked->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED,
31 wxCommandEventHandler(wx_key_entry::on_change_setting), NULL, this);
33 for(auto i : keys)
34 keych.push_back(towxstring(i));
35 mainkey = new labeledcombobox(t_s, this, "Key", &keych[0], keych.size(), 0, true, this,
36 (wxObjectEventFunction)(&wx_key_entry::on_change_setting));
37 t_s->Add(new wxStaticText(this, wxID_ANY, wxT("")), 0, wxGROW);
38 top_s->Add(t_s);
40 wxBoxSizer* pbutton_s = new wxBoxSizer(wxHORIZONTAL);
41 pbutton_s->AddStretchSpacer();
42 pbutton_s->Add(ok = new wxButton(this, wxID_OK, wxT("OK")), 0, wxGROW);
43 pbutton_s->Add(cancel = new wxButton(this, wxID_CANCEL, wxT("Cancel")), 0, wxGROW);
44 ok->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
45 wxCommandEventHandler(wx_key_entry::on_ok), NULL, this);
46 cancel->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
47 wxCommandEventHandler(wx_key_entry::on_cancel), NULL, this);
48 top_s->Add(pbutton_s, 0, wxGROW);
50 t_s->SetSizeHints(this);
51 top_s->SetSizeHints(this);
52 Fit();
55 #define TMPFLAG_UNMASKED 65
56 #define TMPFLAG_UNMASKED_LINK_CHILD 2
57 #define TMPFLAG_UNMASKED_LINK_PARENT 68
58 #define TMPFLAG_PRESSED 8
59 #define TMPFLAG_PRESSED_LINK_CHILD 16
60 #define TMPFLAG_PRESSED_LINK_PARENT 32
62 void wx_key_entry::on_change_setting(wxCommandEvent& e)
64 for(auto& i : modifiers)
65 i.second.tmpflags = 0;
66 for(auto& i : modifiers) {
67 modifier* m = NULL;
68 try {
69 m = &modifier::lookup(i.first);
70 } catch(...) {
71 i.second.pressed->Disable();
72 i.second.unmasked->Disable();
73 continue;
75 std::string j = m->linked_name();
76 if(i.second.unmasked->GetValue())
77 i.second.tmpflags |= TMPFLAG_UNMASKED;
78 if(j != "") {
79 if(modifiers[j].unmasked->GetValue())
80 i.second.tmpflags |= TMPFLAG_UNMASKED_LINK_PARENT;
81 if(i.second.unmasked->GetValue())
82 modifiers[j].tmpflags |= TMPFLAG_UNMASKED_LINK_CHILD;
84 if(i.second.pressed->GetValue())
85 i.second.tmpflags |= TMPFLAG_PRESSED;
86 if(j != "") {
87 if(modifiers[j].pressed->GetValue())
88 i.second.tmpflags |= TMPFLAG_PRESSED_LINK_PARENT;
89 if(i.second.pressed->GetValue())
90 modifiers[j].tmpflags |= TMPFLAG_PRESSED_LINK_CHILD;
93 for(auto& i : modifiers) {
94 //Unmasked is to be enabled if neither unmasked link flag is set.
95 if(i.second.tmpflags & ((TMPFLAG_UNMASKED_LINK_CHILD | TMPFLAG_UNMASKED_LINK_PARENT) & ~64)) {
96 i.second.unmasked->SetValue(false);
97 i.second.unmasked->Disable();
98 } else
99 i.second.unmasked->Enable();
100 //Pressed is to be enabled if:
101 //- This modifier is unmasked or parent is unmasked.
102 //- Parent nor child is not pressed.
103 if(((i.second.tmpflags & (TMPFLAG_UNMASKED | TMPFLAG_UNMASKED_LINK_PARENT | TMPFLAG_PRESSED_LINK_CHILD |
104 TMPFLAG_PRESSED_LINK_PARENT)) & 112) == 64)
105 i.second.pressed->Enable();
106 else {
107 i.second.pressed->SetValue(false);
108 i.second.pressed->Disable();
113 void wx_key_entry::on_ok(wxCommandEvent& e)
115 EndModal(wxID_OK);
118 void wx_key_entry::on_cancel(wxCommandEvent& e)
120 EndModal(wxID_CANCEL);
123 std::string wx_key_entry::getkey()
125 std::string x;
126 bool f;
127 f = true;
128 for(auto i : modifiers) {
129 if(i.second.pressed->GetValue()) {
130 if(!f)
131 x = x + ",";
132 f = false;
133 x = x + i.first;
136 x = x + "/";
137 f = true;
138 for(auto i : modifiers) {
139 if(i.second.unmasked->GetValue()) {
140 if(!f)
141 x = x + ",";
142 f = false;
143 x = x + i.first;
146 x = x + "|" + mainkey->get_choice();
147 return x;