neoWidgets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
FileDialog.h
1 #ifndef NEO_FILEDIALOG_H
2 #define NEO_FILEDIALOG_H
3 
4 #include <Shobjidl.h> // IFileDialog
5 
6 namespace neo
7 {
8 
9 enum FD_FLAGS_ENUM
10 {
11  FD_FILEMUSTEXIST = 0x00000001, //<! the return item must exist
12  FD_PATHMUSTEXIST = 0x00000002, //<! the return item must be in an existing path
13  FD_FORCEFILESYSTEM = 0x00000004, //<! only allow file system items
14  FD_CREATEPROMPT = 0x00000008, //<! prompt when choosing a nonexisting file in SaveFileDialog
15  FD_OVERWRITEPROMPT = 0x00000010, //<! prompt before overwriting a file in SaveFileDialog
16  FD_STRICTFILETYPES = 0x00000020, //<! only allow the types set by FileDialog::SetFilter()
17  FD_KEEPWORKINGDIR = 0x00000040, //<! don't change the working directory
18  FD_NOVALIDATE = 0x00000080, //<! don't check the returned item from OpenFileDialog
19  FD_ALLOWMULTISELECT = 0x00000100, //<! for OpenFileDialog
20  FD_NOREADONLYRETURN = 0x00000200, //<! don't return read only item
21 
22  /*
23  FOS_ALLNONSTORAGEITEMS = 0x00000080,
24  FOS_SHAREAWARE = 0x00004000,
25  FOS_NOTESTFILECREATE = 0x00010000,
26  FOS_HIDEPINNEDPLACES = 0x00040000,
27  FOS_NODEREFERENCELINKS = 0x00100000,
28  FOS_DONTADDTORECENT = 0x02000000,
29  FOS_FORCESHOWHIDDEN = 0x10000000,
30  FOS_FORCEPREVIEWPANEON = 0x40000000,
31  FOS_SUPPORTSTREAMABLEITEMS = 0x80000000
32  */
33 };
34 typedef DWORD FD_FLAGS;
35 
40 {
41 public:
42  FileTypeFilter& Add(const tstring& type, const tstring& description);
43  size_t GetCount() const { return _filter.size(); }
44  const tstring& GetType(size_t i) const { return _filter[i].first; }
45  const tstring& GetDescription(size_t i) const { return _filter[i].second; }
46 private:
47  std::vector<std::pair<tstring, tstring>> _filter;
48 };
49 
55 {
56 public:
57  virtual ~ActiveFileDialogIF() {}
58 
59  virtual tstring GetResult() = 0;
60  virtual std::vector<tstring> GetResults() = 0;
61 };
62 
63 class FileDialog;
64 
70 {
71 public:
72  virtual ~FileDialogEvents() {}
73 
77  virtual bool OnFileOk(FileDialog* dialog, ActiveFileDialogIF* dialogImpl) { return true; }
78 };
79 
81 {
82 public:
86  enum MODE
87  {
91  };
92 
94 
95  MODE _mode;
96  FD_FLAGS _addedFlags;
97  FD_FLAGS _removedFlags;
98  FileTypeFilter _fileFilter;
99  UINT _fileFilterIndex;
100  tstring _filename;
101  tstring _folder;
102  tstring _title;
103  tstring _defaultExt;
104 };
105 
110 {
111 public:
116  FileDialog(HWND parent, FileDialogEvents* events = 0);
117  virtual ~FileDialog();
118 
120  void SetIdentifier(REFGUID id);
122  const GUID* GetIdentifier() const { return _identifier; }
124 
125  HWND GetParentHandle() const { return _parent; }
126  FileDialogEvents* GetEvents() const { return _events; }
127 
129  int ShowModal();
130 
132  const tstring& GetResult() const { return resultFiles.front(); }
133 
135  const std::vector<tstring>& GetResults() const { return resultFiles; }
136 
138  void SetMode(FileDialogOptions::MODE mode);
139 
141  void ChangeFlags(FD_FLAGS add, FD_FLAGS remove = 0);
142 
144  void SetFilter(const FileTypeFilter& filter);
145 
147  void SetFilterIndex(UINT index);
148 
150  void SetFilename(const tstring& filename);
151 
153  void SetFolder(const tstring& folder);
154 
156  void SetTitle(const tstring& title);
157 
159  void SetDefaultExtension(const tstring& ext);
160 
162  const FileDialogOptions& GetOptions() const { return _options; }
163 protected:
167  virtual void OnInitIFileDialog(IFileDialog* dialog) {}
168 private:
169  // these are not part of FileDialogOptions because they are pointers
170  HWND _parent;
171  FileDialogEvents* _events;
172  const GUID* _identifier;
173 
174  std::vector<tstring> resultFiles;
175 
176  FileDialogOptions _options;
177 };
178 
180 {
181 public:
182  OpenFileDialog(HWND parent, FileDialogEvents* events = 0)
183  : FileDialog(parent, events)
184  {
186  }
187 };
188 
190 {
191 public:
192  SaveFileDialog(HWND parent, FileDialogEvents* events = 0)
193  : FileDialog(parent, events)
194  {
196  }
197 };
198 
200 {
201 public:
202  PickFolderDialog(HWND parent, FileDialogEvents* events = 0)
203  : FileDialog(parent, events)
204  {
206  }
207 };
208 
209 } // namespace neo
210 
211 #endif
void SetDefaultExtension(const tstring &ext)
Default extension will be appended to the result if no extension is present.
Definition: FileDialog.inl:405
void ChangeFlags(FD_FLAGS add, FD_FLAGS remove=0)
Add or remove flags from the dialog.
Definition: FileDialog.inl:374
FileDialog(HWND parent, FileDialogEvents *events=0)
Definition: FileDialog.inl:335
const FileDialogOptions & GetOptions() const
Get one solid block of options.
Definition: FileDialog.h:162
void SetFolder(const tstring &folder)
Overrides the default starting folder.
Definition: FileDialog.inl:395
Definition: FileDialog.h:54
MODE
Definition: FileDialog.h:86
const std::vector< tstring > & GetResults() const
Retrieves multiple result after ShowModal() has finished. Only for OpenFileDialog().
Definition: FileDialog.h:135
Definition: FileDialog.h:199
Definition: FileDialog.h:189
void SetFilename(const tstring &filename)
Filename inside the textbox of the dialog.
Definition: FileDialog.inl:390
int ShowModal()
The dialog is created here.
Definition: FileDialog.inl:349
const GUID * GetIdentifier() const
The file dialog can remember different starting directories based on the identifier.
Definition: FileDialog.h:122
virtual void OnInitIFileDialog(IFileDialog *dialog)
Definition: FileDialog.h:167
void SetTitle(const tstring &title)
Title text of the dialog.
Definition: FileDialog.inl:400
Definition: FileDialog.h:39
Definition: FileDialog.h:109
void SetMode(FileDialogOptions::MODE mode)
Switch the general operation mode. Will be used by subclasses on construction.
Definition: FileDialog.inl:369
void SetFilterIndex(UINT index)
Index corresponds to the filter from SetFilter()
Definition: FileDialog.inl:385
Definition: FileDialog.h:179
Definition: FileDialog.h:80
const tstring & GetResult() const
Retrieves the result after ShowModal() has finished.
Definition: FileDialog.h:132
virtual bool OnFileOk(FileDialog *dialog, ActiveFileDialogIF *dialogImpl)
Definition: FileDialog.h:77
void SetFilter(const FileTypeFilter &filter)
Sets a filter for the allowed file types in the dialog.
Definition: FileDialog.inl:380
select a directory
Definition: FileDialog.h:90
Definition: FileDialog.h:69
select a file to save
Definition: FileDialog.h:89
select a file to open
Definition: FileDialog.h:88
void SetIdentifier(REFGUID id)
The file dialog can remember different starting directories based on the identifier.
Definition: FileDialog.inl:344