Utilities
Utilities header only library
WxWidgetsDialog.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_WX_WIDGETS_DIALOG_H
7 #define UTILS_WX_WIDGETS_DIALOG_H
8 
9 #include <Utils/DialogInterface.h>
10 
11 #include <wx/app.h>
12 #include <wx/msgdlg.h>
13 #include <wx/wxprec.h>
14 #include <wx/wfstream.h>
15 #include <wx/dirdlg.h>
16 #include <wx/filedlg.h>
17 
18 /*
19  To make the "stay on top" dialog feature work properly, the "top window" must be set
20  Example:
21  wxFrame dummy_window;
22  dummy_window.SetHWND(hiro::GetWindowHandle());
23  dummy_window.Show();
24  wxTheApp->SetTopWindow(&dummy_window);
25 */
26 
27 class WxWidgetsDialog : public IDialogInterface
28 {
29 public:
30  virtual std::optional<std::string> GetPathFromDirectorySelectionDialog(const std::wstring &title, const std::string &default_path = "") const override
31  {
32  wxDirDialog selectDirDialog(wxTheApp->GetTopWindow(), title, default_path, wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
33  if (selectDirDialog.ShowModal() == wxID_CANCEL)
34  {
35  return std::nullopt;
36  }
37 
38  return selectDirDialog.GetPath().ToStdString();
39  };
40  virtual std::vector<std::string> GetPathsFromFileSelectionDialog(const std::wstring &title, const std::string &wildcard, bool enable_multiselect, const std::string &default_path = "") const override
41  {
42  long dialogFlags = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
43  if (enable_multiselect)
44  {
45  dialogFlags |= wxFD_MULTIPLE;
46  }
47 
48  std::vector<std::string> returnPaths;
49 
50  wxFileDialog openFileDialog(wxTheApp->GetTopWindow(), title, default_path, "", wildcard, dialogFlags);
51  if (openFileDialog.ShowModal() == wxID_CANCEL)
52  {
53  return returnPaths;
54  }
55 
56  wxArrayString paths;
57  openFileDialog.GetPaths(paths);
58  for (const auto &path : paths)
59  {
60  returnPaths.push_back(path.ToStdString());
61  }
62 
63  return returnPaths;
64  };
65  virtual std::optional<std::string> GetPathFromFileSaveDialog(const std::wstring &title, const std::string &wildcard, bool overwrite_prompt_enabled, const std::string &default_path = "") const override
66  {
67  long flags = wxFD_SAVE;
68  if (overwrite_prompt_enabled)
69  {
70  flags |= wxFD_OVERWRITE_PROMPT;
71  }
72  wxFileDialog saveFileDialog(wxTheApp->GetTopWindow(), title, default_path, "", wildcard, flags);
73  if (saveFileDialog.ShowModal() == wxID_CANCEL)
74  {
75  return std::nullopt;
76  }
77 
78  return saveFileDialog.GetPath().ToStdString();
79  };
80 
81  virtual void ShowErrorMessage(const std::wstring &message, const std::wstring &title = L"Message") const override
82  {
83  wxMessageBox(message, title, wxICON_ERROR, wxTheApp->GetTopWindow());
84  }
85 
86  virtual void ShowWarningMessage(const std::wstring &message, const std::wstring &title = L"Message") const override
87  {
88  wxMessageBox(message, title, wxICON_WARNING, wxTheApp->GetTopWindow());
89  };
90  virtual void ShowInfoMessage(const std::wstring &message, const std::wstring &title = L"Message") const override
91  {
92  wxMessageBox(message, title, wxICON_INFORMATION, wxTheApp->GetTopWindow());
93  };
94  virtual bool ShowConfirmationDialog(const std::wstring &message, const std::wstring &title = L"Confirmation") const override
95  {
96  return wxMessageBox(message, title, wxYES_NO | wxICON_EXCLAMATION, wxTheApp->GetTopWindow()) == wxYES;
97  };
98 };
99 
100 #endif /* !UTILS_WX_WIDGETS_DIALOG_H */