1 //------------------------------------------------------------------------------
2 // emFileDialog.h
3 //
4 // Copyright (C) 2015 Oliver Hamann.
5 //
6 // Homepage: http://eaglemode.sourceforge.net/
7 //
8 // This program is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU General Public License version 3 as published by the
10 // Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License version 3
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //------------------------------------------------------------------------------
20 
21 #ifndef emFileDialog_h
22 #define emFileDialog_h
23 
24 #ifndef emDialog_h
25 #include <emCore/emDialog.h>
26 #endif
27 
28 #ifndef emFileSelectionBox_h
29 #include <emCore/emFileSelectionBox.h>
30 #endif
31 
32 
33 //==============================================================================
34 //================================ emFileDialog ================================
35 //==============================================================================
36 
37 class emFileDialog : public emDialog {
38 
39 public:
40 
41 	// Class for a dialog which allows the user to choose a file (or
42 	// optionally multiple files). This is an emDialog with an
43 	// emFileSelectionBox as the content panel, and with OK and Cancel
44 	// buttons. Most of the public methods of emFileSelectionBox are copied
45 	// here with forwarding implementations. Additional features are for the
46 	// dialog aspects. Especially see ModeType:
47 
48 	enum ModeType {
49 		// Data type for the general mode of the file dialog.
50 
51 		M_SELECT,
52 			// The dialog is for selecting a file without popping up
53 			// any child dialogs like "file not existing" or "are
54 			// you sure to overwrite". The title is set to "Files",
55 			// and the button are "OK" and "Cancel"
56 
57 		M_OPEN,
58 			// The dialog is for opening (reading) a file. If the
59 			// user tries to open a file that does not exist, an
60 			// error message is shown in a child dialog. The title
61 			// is set to "Open", and the buttons are "Open" and
62 			// "Cancel"
63 
64 		M_SAVE
65 			// The dialog is for saving (writing) a file. If the
66 			// user tries to save a file which already exists, a
67 			// child dialog is shown which asks the user if he
68 			// really wants to overwrite that file. The title is set
69 			// to "Save As", and the buttons are "Save" and "Cancel"
70 	};
71 
72 	emFileDialog(
73 		emContext & parentContext,
74 		ModeType mode=M_SELECT,
75 		ViewFlags viewFlags=VF_POPUP_ZOOM|VF_ROOT_SAME_TALLNESS,
76 		WindowFlags windowFlags=WF_MODAL,
77 		const emString & wmResName="emDialog"
78 	);
79 		// Constructor.
80 		// Arguments:
81 		//   parentContext - Parent context(/window/dialog) for this
82 		//                   dialog.
83 		//   mode          - Mode of the file dialog.
84 		//   viewFlags     - View features.
85 		//   windowFlags   - Window features.
86 		//   wmResName     - A resource name for the dialog. This is
87 		//                   reported to the window manager.
88 
89 	virtual ~emFileDialog();
90 		// Destructor.
91 
92 	ModeType GetMode() const;
93 		// Get the file dialog mode.
94 
95 	void SetMode(ModeType mode);
96 		// Set the file dialog mode. This also sets the dialog title and
97 		// the button texts accordingly.
98 
99 	bool IsDirectoryResultAllowed() const;
100 		// Ask whether it is allowed to open/save a directory.
101 
102 	void SetDirectoryResultAllowed(bool dirAllowed=true);
103 		// Set whether it is allowed to open/save a directory. If false
104 		// (the default), pressing "OK" while a directory is selected
105 		// has the effect of entering that directory instead of
106 		// finishing the dialog.
107 
108 	bool IsMultiSelectionEnabled() const;
109 		// Ask whether the user is allowed to select multiple entries.
110 
111 	void SetMultiSelectionEnabled(bool enabled=true);
112 		// Set whether the user is allowed to select multiple entries.
113 		// The default is false.
114 
115 	const emString & GetParentDirectory() const;
116 		// Get the absolute path of the parent directory.
117 
118 	void SetParentDirectory(const emString & parentDirectory);
119 		// Set the parent directory.
120 
121 	emString GetSelectedName() const;
122 		// Get the name of the selected entry. If nothing is selected,
123 		// an empty string is returned. If multiple entries are
124 		// selected, the first one is returned.
125 
126 	const emArray<emString> & GetSelectedNames() const;
127 		// Get an array with the names of all the selected entries.
128 
129 	void SetSelectedName(const emString & selectedName);
130 		// Select a single entry. An empty string means to clear the
131 		// selection.
132 
133 	void SetSelectedNames(const emArray<emString> & selectedNames);
134 		// Select any number of entries.
135 
136 	void ClearSelection();
137 		// Clear the selection.
138 
139 	emString GetSelectedPath() const;
140 		// Get the absolute path of the selected entry. If nothing is
141 		// selected, the absolute path of the parent directory is
142 		// returned. If multiple entries are selected, the absolute path
143 		// of the first selected one is returned.
144 
145 	void SetSelectedPath(const emString & selectedPath);
146 		// Change the parent directory and the selection in one call. If
147 		// the given path is a directory, the parent directory is set to
148 		// that directory, and the selection is cleared. Otherwise the
149 		// parent directory is set to the parent path of the given path,
150 		// and the selection is set to the final name in the given path.
151 
152 	const emSignal & GetSelectionSignal() const;
153 		// Signaled when the selection or the parent directory has
154 		// changed.
155 
156 	const emArray<emString> & GetFilters() const;
157 		// Get the file type filters. See SetFilters for more.
158 
159 	void SetFilters(const emArray<emString> & filters);
160 		// Set the file type filters, from which the user can choose
161 		// one. The default is to have no filters, which means to show
162 		// all entries. If no filter is selected at the time of calling
163 		// SetFilters with a non-empty array, the first filter is
164 		// selected automatically. Each filter is a string of the form:
165 		//   <description>'('<pattern>[<separator><pattern>...]')'
166 		// With:
167 		//   <description> = Any text.
168 		//   <pattern>     = A pattern for the file name. The only supported
169 		//                   meta character is an asterisk ('*'). It
170 		//                   means to match any sequence (including the
171 		//                   empty sequence). Everything else is taken
172 		//                   literal, but case-insensitive.
173 		//   <separator>   = A space char, a semicolon, a comma, or a
174 		//                   vertical bar.
175 		// Examples:
176 		//   "All files (*)"
177 		//   "Targa files (*.tga)"
178 		//   "HTML files (*.htm *.html)"
179 
180 	int GetSelectedFilterIndex() const;
181 		// Get the index of the selected filter, or -1 if none is
182 		// selected.
183 
184 	void SetSelectedFilterIndex(int selectedFilterIndex);
185 		// Set the index of the selected filter. -1 means to select none.
186 
187 	bool AreHiddenFilesShown() const;
188 		// Ask whether hidden files are shown.
189 
190 	void SetHiddenFilesShown(bool hiddenFilesShown=true);
191 		// Set whether hidden files are shown. The default is false.
192 
193 protected:
194 
195 	virtual bool Cycle();
196 	virtual bool CheckFinish(int result);
197 
198 private:
199 
200 	emFileSelectionBox * Fsb;
201 	ModeType Mode;
202 	bool DirAllowed;
203 
204 	emCrossPtr<emDialog> OverwriteDialog;
205 	emString OverwriteAsked;
206 	emString OverwriteConfirmed;
207 };
208 
GetMode()209 inline emFileDialog::ModeType emFileDialog::GetMode() const
210 {
211 	return Mode;
212 }
213 
IsDirectoryResultAllowed()214 inline bool emFileDialog::IsDirectoryResultAllowed() const
215 {
216 	return DirAllowed;
217 }
218 
IsMultiSelectionEnabled()219 inline bool emFileDialog::IsMultiSelectionEnabled() const
220 {
221 	return Fsb->IsMultiSelectionEnabled();
222 }
223 
SetMultiSelectionEnabled(bool enabled)224 inline void emFileDialog::SetMultiSelectionEnabled(bool enabled)
225 {
226 	Fsb->SetMultiSelectionEnabled(enabled);
227 }
228 
GetParentDirectory()229 inline const emString & emFileDialog::GetParentDirectory() const
230 {
231 	return Fsb->GetParentDirectory();
232 }
233 
SetParentDirectory(const emString & parentDirectory)234 inline void emFileDialog::SetParentDirectory(const emString & parentDirectory)
235 {
236 	Fsb->SetParentDirectory(parentDirectory);
237 }
238 
GetSelectedName()239 inline emString emFileDialog::GetSelectedName() const
240 {
241 	return Fsb->GetSelectedName();
242 }
243 
GetSelectedNames()244 inline const emArray<emString> & emFileDialog::GetSelectedNames() const
245 {
246 	return Fsb->GetSelectedNames();
247 }
248 
SetSelectedName(const emString & selectedName)249 inline void emFileDialog::SetSelectedName(const emString & selectedName)
250 {
251 	Fsb->SetSelectedName(selectedName);
252 }
253 
SetSelectedNames(const emArray<emString> & selectedNames)254 inline void emFileDialog::SetSelectedNames(const emArray<emString> & selectedNames)
255 {
256 	Fsb->SetSelectedNames(selectedNames);
257 }
258 
ClearSelection()259 inline void emFileDialog::ClearSelection()
260 {
261 	Fsb->ClearSelection();
262 }
263 
GetSelectedPath()264 inline emString emFileDialog::GetSelectedPath() const
265 {
266 	return Fsb->GetSelectedPath();
267 }
268 
SetSelectedPath(const emString & selectedPath)269 inline void emFileDialog::SetSelectedPath(const emString & selectedPath)
270 {
271 	Fsb->SetSelectedPath(selectedPath);
272 }
273 
GetSelectionSignal()274 inline const emSignal & emFileDialog::GetSelectionSignal() const
275 {
276 	return Fsb->GetSelectionSignal();
277 }
278 
GetFilters()279 inline const emArray<emString> & emFileDialog::GetFilters() const
280 {
281 	return Fsb->GetFilters();
282 }
283 
SetFilters(const emArray<emString> & filters)284 inline void emFileDialog::SetFilters(const emArray<emString> & filters)
285 {
286 	Fsb->SetFilters(filters);
287 }
288 
GetSelectedFilterIndex()289 inline int emFileDialog::GetSelectedFilterIndex() const
290 {
291 	return Fsb->GetSelectedFilterIndex();
292 }
293 
SetSelectedFilterIndex(int selectedFilterIndex)294 inline void emFileDialog::SetSelectedFilterIndex(int selectedFilterIndex)
295 {
296 	Fsb->SetSelectedFilterIndex(selectedFilterIndex);
297 }
298 
AreHiddenFilesShown()299 inline bool emFileDialog::AreHiddenFilesShown() const
300 {
301 	return Fsb->AreHiddenFilesShown();
302 }
303 
SetHiddenFilesShown(bool hiddenFilesShown)304 inline void emFileDialog::SetHiddenFilesShown(bool hiddenFilesShown)
305 {
306 	Fsb->SetHiddenFilesShown(hiddenFilesShown);
307 }
308 
309 
310 #endif
311