-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathFilePickerCtrl.cpp
58 lines (53 loc) · 2.75 KB
/
FilePickerCtrl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <wx/app.h>
#include <wx/filepicker.h>
#include <wx/frame.h>
#include <wx/panel.h>
#include <wx/stattext.h>
#include <wx/sysopt.h>
// Workaround : with wxWidgets version <= 3.2 wxFilePickerCtrl::SetFilterIndex doesn't work on macOS
#if !defined(__APPLE__)
using FilePickerCtrl = wxFilePickerCtrl;
#else
class FilePickerCtrl : public wxFilePickerCtrl {
public:
FilePickerCtrl(wxWindow* parent, wxWindowID id, const wxString& path = wxEmptyString, const wxString& message = wxFileSelectorPromptStr, const wxString& wildcard = wxFileSelectorDefaultWildcardStr, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxFLP_DEFAULT_STYLE, const wxValidator& validator = wxDefaultValidator, const wxString& name = wxFilePickerCtrlNameStr) : wxFilePickerCtrl {parent, id, path, message, wildcard, pos, size, style, validator, name} {
auto pickerCtrl = GetPickerCtrl();
if (size == wxDefaultSize) SetSize(150, GetSize().GetHeight());
auto wx_dialog_style = 0;
if ((style & wxFLP_OPEN) == wxFLP_OPEN) wx_dialog_style |= wxFD_OPEN;
if ((style & wxFLP_SAVE) == wxFLP_SAVE) wx_dialog_style |= wxFD_SAVE;
if ((style & wxFLP_OVERWRITE_PROMPT) == wxFLP_OVERWRITE_PROMPT) wx_dialog_style |= wxFD_OVERWRITE_PROMPT;
if ((style & wxFLP_FILE_MUST_EXIST) == wxFLP_FILE_MUST_EXIST) wx_dialog_style |= wxFD_FILE_MUST_EXIST;
if ((style & wxFLP_CHANGE_DIR) == wxFLP_CHANGE_DIR) wx_dialog_style |= wxFD_CHANGE_DIR;
pickerCtrl->Bind(wxEVT_BUTTON, [=](wxCommandEvent& event) {
auto openFileDialog = wxFileDialog {parent, message, path, wxEmptyString, wildcard, wx_dialog_style};
openFileDialog.SetFilterIndex(0);
if (openFileDialog.ShowModal() == wxID_OK) {
SetPath(openFileDialog.GetPath());
wxPostEvent(this, wxFileDirPickerEvent(wxEVT_FILEPICKER_CHANGED, this, id, GetPath()));
}
});
}
};
#endif
namespace FilePickerCtrlExample {
class Frame : public wxFrame {
public:
Frame() : wxFrame {nullptr, wxID_ANY, "FilePickerCtrl example"} {
picker->Bind(wxEVT_FILEPICKER_CHANGED, [&](wxFileDirPickerEvent& event) {
label->SetLabel(wxString::Format("File = %s", picker->GetPath()));
});
}
private:
wxPanel* panel = new wxPanel {this};
wxStaticText* label = new wxStaticText {panel, wxID_ANY, "File = ", wxPoint(10, 50)};
FilePickerCtrl* picker = new FilePickerCtrl {panel, wxID_ANY, wxEmptyString, wxEmptyString, "Text Files (*.txt)|*.txt|All Files (*.*)|*.*", {10, 10}, wxDefaultSize, wxFLP_DEFAULT_STYLE|wxFLP_SMALL};
};
class Application : public wxApp {
bool OnInit() override {
wxSystemOptions::SetOption("osx.openfiledialog.always-show-types", 1);
return (new Frame)->Show();
}
};
}
wxIMPLEMENT_APP(FilePickerCtrlExample::Application);