FileDialog.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * FileDialog.h
  3. * ------------
  4. * Purpose: File and folder selection dialogs implementation.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include <string>
  12. #include <vector>
  13. OPENMPT_NAMESPACE_BEGIN
  14. // Generic open / save file dialog. Cannot be instanced by the user, use OpenFileDialog / SaveFileDialog instead.
  15. class FileDialog
  16. {
  17. public:
  18. using PathList = std::vector<mpt::PathString>;
  19. protected:
  20. mpt::RawPathString m_defaultExtension;
  21. mpt::RawPathString m_defaultFilename;
  22. mpt::RawPathString m_extFilter;
  23. mpt::RawPathString m_lastPreviewFile;
  24. mpt::RawPathString m_workingDirectory;
  25. mpt::RawPathString m_extension;
  26. PathList m_filenames;
  27. PathList m_places;
  28. int *m_filterIndex = nullptr;
  29. bool m_load;
  30. bool m_multiSelect = false;
  31. bool m_preview = false;
  32. protected:
  33. FileDialog(bool load) : m_load(load) { }
  34. public:
  35. // Default extension to use if none is specified.
  36. FileDialog &DefaultExtension(const mpt::PathString &ext) { m_defaultExtension = ext.AsNative(); return *this; }
  37. FileDialog &DefaultExtension(const AnyStringLocale &ext) { m_defaultExtension = mpt::ToWin(ext); return *this; }
  38. // Default suggested filename.
  39. FileDialog &DefaultFilename(const mpt::PathString &name) { m_defaultFilename = name.AsNative(); return *this; }
  40. FileDialog &DefaultFilename(const AnyStringLocale &name) { m_defaultFilename = mpt::ToWin(name); return *this; }
  41. // List of possible extensions. Format: "description|extensions|...|description|extensions||"
  42. FileDialog &ExtensionFilter(const mpt::PathString &filter) { m_extFilter = filter.AsNative(); return *this; }
  43. FileDialog &ExtensionFilter(const AnyStringLocale &filter) { m_extFilter = mpt::ToWin(filter); return *this; }
  44. // Default directory of the dialog.
  45. FileDialog &WorkingDirectory(const mpt::PathString &dir) { m_workingDirectory = dir.AsNative(); return *this; }
  46. // Pointer to a variable holding the index of the last extension filter to use. Holds the selected filter after the dialog has been closed.
  47. FileDialog &FilterIndex(int *index) { m_filterIndex = index; return *this; }
  48. // Enable preview of instrument files (if globally enabled).
  49. FileDialog &EnableAudioPreview() { m_preview = true; return *this; }
  50. // Add a directory to the application-specific quick-access directories in the file dialog
  51. FileDialog &AddPlace(mpt::PathString path) { m_places.push_back(std::move(path)); return *this; }
  52. // Show the file selection dialog.
  53. bool Show(CWnd *parent = nullptr);
  54. // Get some selected file. Mostly useful when only one selected file is possible anyway.
  55. mpt::PathString GetFirstFile() const
  56. {
  57. if(!m_filenames.empty())
  58. return m_filenames.front();
  59. else
  60. return {};
  61. }
  62. // Gets a reference to all selected filenames.
  63. const PathList &GetFilenames() const { return m_filenames; }
  64. // Gets directory in which the selected files are placed.
  65. mpt::PathString GetWorkingDirectory() const { return mpt::PathString::FromNative(m_workingDirectory); }
  66. // Gets the extension of the first selected file, without dot.
  67. mpt::PathString GetExtension() const { return mpt::PathString::FromNative(m_extension); }
  68. };
  69. // Dialog for opening files
  70. class OpenFileDialog : public FileDialog
  71. {
  72. public:
  73. OpenFileDialog() : FileDialog(true) { }
  74. // Enable selection of multiple files
  75. OpenFileDialog &AllowMultiSelect() { m_multiSelect = true; return *this; }
  76. };
  77. // Dialog for saving files
  78. class SaveFileDialog : public FileDialog
  79. {
  80. public:
  81. SaveFileDialog() : FileDialog(false) { }
  82. };
  83. // Folder browser.
  84. class BrowseForFolder
  85. {
  86. protected:
  87. mpt::PathString m_workingDirectory;
  88. CString m_caption;
  89. public:
  90. BrowseForFolder(const mpt::PathString &dir, const CString &caption) : m_workingDirectory(dir), m_caption(caption) { }
  91. // Show the folder selection dialog.
  92. bool Show(CWnd *parent = nullptr);
  93. // Gets selected directory.
  94. mpt::PathString GetDirectory() const { return m_workingDirectory; }
  95. protected:
  96. static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData);
  97. };
  98. OPENMPT_NAMESPACE_END