Move the wxwidgets stuff to one directory
[lsnes.git] / platform / wxwidgets / filenamebox.cpp
blob6141e5b1d45a9f76badf52b0276088a36fca579f
1 #include "filenamebox.hpp"
2 #include "common.hpp"
3 #include "zip.hpp"
4 #include <wx/wx.h>
5 #include <wx/event.h>
6 #include <wx/control.h>
7 #include <wx/combobox.h>
9 filenamebox::filenamebox(wxSizer* sizer, wxWindow* parent, const std::string& initial_label, int flags,
10 wxEvtHandler* dispatch_to, wxObjectEventFunction on_fn_change)
12 wxSizer* inner = sizer;
13 if(flags & FNBF_OI)
14 inner = new wxFlexGridSizer(1, 3, 0, 0);
15 given_flags = flags;
16 last_label = initial_label;
17 label = new wxStaticText(parent, wxID_ANY, towxstring(last_label));
18 filename = new wxTextCtrl(parent, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(500, -1));
19 file_select = new wxButton(parent, wxID_ANY, wxT("..."));
20 inner->Add(label, 0, wxGROW);
21 inner->Add(filename, 1, wxGROW);
22 inner->Add(file_select, 0, wxGROW);
23 if((flags & FNBF_NN) == 0)
24 filename->Connect(wxEVT_COMMAND_TEXT_UPDATED, on_fn_change, NULL, dispatch_to);
25 file_select->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(filenamebox::on_file_select), NULL,
26 this);
27 if(flags & FNBF_SD)
28 disable();
29 if(flags & FNBF_OI) {
30 inner->SetSizeHints(parent);
31 sizer->Add(inner, 0, wxGROW);
33 pwindow = parent;
34 enabled = ((flags & FNBF_SD) == 0);
37 filenamebox::~filenamebox()
39 //Wxwidgets destroys the subwidgets.
43 void filenamebox::on_file_select(wxCommandEvent& e)
45 std::string fname;
46 wxFileDialog* d = new wxFileDialog(pwindow, towxstring("Choose " + last_label), wxT("."));
47 if(d->ShowModal() == wxID_CANCEL) {
48 d->Destroy();
49 return;
51 fname = tostdstring(d->GetPath());
52 d->Destroy();
53 if(given_flags & FNBF_PZ) {
54 //Did we pick a .zip file?
55 try {
56 zip_reader zr(fname);
57 std::vector<wxString> files;
58 for(auto i : zr)
59 files.push_back(towxstring(i));
60 wxSingleChoiceDialog* d2 = new wxSingleChoiceDialog(pwindow, wxT("Select file within .zip"),
61 wxT("Select member"), files.size(), &files[0]);
62 if(d2->ShowModal() == wxID_CANCEL) {
63 d2->Destroy();
64 return;
66 fname = fname + "/" + tostdstring(d2->GetStringSelection());
67 d2->Destroy();
68 } catch(...) {
69 //Ignore error.
72 filename->SetValue(towxstring(fname));
75 std::string filenamebox::get_file()
77 if(!enabled)
78 return "";
79 else
80 return tostdstring(filename->GetValue());
83 bool filenamebox::is_enabled()
85 return enabled;
88 void filenamebox::enable(const std::string& new_label)
90 change_label(new_label);
91 enable();
94 void filenamebox::change_label(const std::string& new_label)
96 last_label = new_label;
97 if(enabled || (given_flags & FNBF_PL))
98 label->SetLabel(towxstring(last_label));
101 void filenamebox::disable()
103 label->Disable();
104 filename->Disable();
105 file_select->Disable();
106 if((given_flags & FNBF_PL) == 0)
107 label->SetLabel(wxT(""));
108 enabled = false;
111 void filenamebox::enable()
113 if((given_flags & FNBF_PL) == 0)
114 label->SetLabel(towxstring(last_label));
115 label->Enable();
116 filename->Enable();
117 file_select->Enable();
118 enabled = true;
121 bool filenamebox::is_nonblank()
123 if(!enabled)
124 return false;
125 return (filename->GetValue().Length() != 0);
128 bool filenamebox::is_nonblank_or_disabled()
130 if(!enabled)
131 return true;
132 return (filename->GetValue().Length() != 0);
135 void filenamebox::clear()
137 filename->SetValue(wxT(""));