-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathOpenFileDialogShowWindowModal.cpp
More file actions
52 lines (46 loc) · 2.26 KB
/
OpenFileDialogShowWindowModal.cpp
File metadata and controls
52 lines (46 loc) · 2.26 KB
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
#include <wx/wx.h>
#include <wx/filedlg.h>
#include <wx/sysopt.h>
#include <wx/windowptr.h>
// Workaround : with wxWidgets version <= 3.2.0 wxFileDialog ShowWindowModal method doesn't exists on other platform that macOS
#if defined(__APPLE__)
using FileDialog = wxFileDialog;
#else
class FileDialog : public wxFileDialog {
public:
FileDialog(wxWindow* parent, const wxString& message = wxFileSelectorPromptStr, const wxString& defaultDir = wxEmptyString, const wxString& defaultFile = wxEmptyString, const wxString& wildCard = wxFileSelectorDefaultWildcardStr, long style = wxFD_DEFAULT_STYLE, const wxPoint& pos = wxDefaultPosition, const wxSize& sz = wxDefaultSize, const wxString& name = wxFileDialogNameStr) : wxFileDialog(parent, message, defaultDir, defaultFile, wildCard, style, pos, sz, name) {}
void ShowWindowModal() {
SetReturnCode(ShowModal());
wxWindowModalDialogEvent event(wxEVT_WINDOW_MODAL_DIALOG_CLOSED, this->GetId());
event.SetEventObject(this);
wxPostEvent(this, event);
}
};
#endif
namespace OpenFileDialogShowWindowModalExample {
class Frame : public wxFrame {
public:
Frame() : wxFrame(nullptr, wxID_ANY, "OpenFileDialog (ShowWindowModal) example") {
button->Bind(wxEVT_BUTTON, [&](wxCommandEvent& event) {
wxWindowPtr<FileDialog> openFileDialog(new FileDialog(this, wxEmptyString, wxEmptyString, wxEmptyString, "Text Files (*.txt)|*.txt|All Files (*.*)|*.*", wxFD_OPEN|wxFD_FILE_MUST_EXIST));
openFileDialog->SetFilterIndex(0);
openFileDialog->ShowWindowModal();
openFileDialog->Bind(wxEVT_WINDOW_MODAL_DIALOG_CLOSED, [this,openFileDialog](wxWindowModalDialogEvent& event) {
if (event.GetReturnCode() == wxID_OK)
label->SetLabelText(wxString::Format("File = %s", openFileDialog->GetPath()));
});
});
}
private:
wxPanel* panel = new wxPanel {this};
wxButton* button = new wxButton(panel, wxID_ANY, "Open...", {10, 10});
wxStaticText* label = new wxStaticText(panel, wxID_ANY, "", {10, 40});
};
class Application : public wxApp {
bool OnInit() override {
wxSystemOptions::SetOption("osx.openfiledialog.always-show-types", 1);
return (new Frame)->Show();
}
};
}
wxIMPLEMENT_APP(OpenFileDialogShowWindowModalExample::Application);