]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: filedlg.cpp | |
3 | // Purpose: wxFileDialog | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
e15e548b | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
ba681060 | 13 | #pragma implementation "filedlg.h" |
2bda0e17 KB |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
ba681060 | 20 | #pragma hdrstop |
2bda0e17 KB |
21 | #endif |
22 | ||
23 | #ifndef WX_PRECOMP | |
ba681060 VZ |
24 | #include <stdio.h> |
25 | #include "wx/defs.h" | |
26 | #include "wx/utils.h" | |
27 | #include "wx/msgdlg.h" | |
28 | #include "wx/dialog.h" | |
29 | #include "wx/filedlg.h" | |
30 | #include "wx/intl.h" | |
2662e49e | 31 | #include "wx/log.h" |
2bda0e17 KB |
32 | #endif |
33 | ||
34 | #include <windows.h> | |
35 | ||
5ea105e0 | 36 | #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__) |
ba681060 | 37 | #include <commdlg.h> |
2bda0e17 KB |
38 | #endif |
39 | ||
40 | #include "wx/msw/private.h" | |
41 | ||
42 | #include <math.h> | |
43 | #include <stdlib.h> | |
44 | #include <string.h> | |
45 | ||
2bda0e17 | 46 | #if !USE_SHARED_LIBRARY |
ba681060 | 47 | IMPLEMENT_CLASS(wxFileDialog, wxDialog) |
2bda0e17 KB |
48 | #endif |
49 | ||
837e5743 OK |
50 | wxString wxFileSelector(const wxChar *title, |
51 | const wxChar *defaultDir, | |
52 | const wxChar *defaultFileName, | |
53 | const wxChar *defaultExtension, | |
54 | const wxChar *filter, | |
ba681060 VZ |
55 | int flags, |
56 | wxWindow *parent, | |
57 | int x, int y) | |
2bda0e17 | 58 | { |
1f2f0331 VZ |
59 | // In the original implementation, defaultExtension is passed to the |
60 | // lpstrDefExt member of OPENFILENAME. This extension, if non-NULL, is | |
61 | // appended to the filename if the user fails to type an extension. The new | |
62 | // implementation (taken from wxFileSelectorEx) appends the extension | |
63 | // automatically, by looking at the filter specification. In fact this | |
64 | // should be better than the native Microsoft implementation because | |
65 | // Windows only allows *one* default extension, whereas here we do the | |
66 | // right thing depending on the filter the user has chosen. | |
67 | ||
68 | // If there's a default extension specified but no filter, we create a | |
69 | // suitable filter. | |
70 | ||
71 | wxString filter2; | |
e15e548b | 72 | if ( defaultExtension && !filter ) |
837e5743 | 73 | filter2 = wxString(_T("*.")) + defaultExtension; |
e15e548b VZ |
74 | else if ( filter ) |
75 | filter2 = filter; | |
76 | ||
77 | wxString defaultDirString; | |
78 | if (defaultDir) | |
79 | defaultDirString = defaultDir; | |
e15e548b VZ |
80 | |
81 | wxString defaultFilenameString; | |
82 | if (defaultFileName) | |
83 | defaultFilenameString = defaultFileName; | |
1f2f0331 VZ |
84 | |
85 | wxFileDialog fileDialog(parent, title, defaultDirString, | |
86 | defaultFilenameString, filter2, | |
87 | flags, wxPoint(x, y)); | |
837e5743 | 88 | if( wxStrlen(defaultExtension) != 0 ) |
1f2f0331 VZ |
89 | { |
90 | int filterFind = 1, | |
91 | filterIndex = 0; | |
92 | ||
93 | for( unsigned int i = 0; i < filter2.Len(); i++ ) | |
94 | { | |
837e5743 | 95 | if( filter2.GetChar(i) == _T('|') ) |
1f2f0331 VZ |
96 | { |
97 | // save the start index of the new filter | |
98 | unsigned int is = i++; | |
99 | filterIndex++; | |
100 | ||
101 | // find the end of the filter | |
102 | for( ; i < filter2.Len(); i++ ) | |
103 | { | |
837e5743 | 104 | if(filter2[i] == _T('|')) |
1f2f0331 VZ |
105 | break; |
106 | } | |
107 | ||
108 | if( i-is-1 > 0 && is+1 < filter2.Len() ) | |
109 | { | |
574c0bbf JS |
110 | if( filter2.Mid(is+1,i-is-1).Contains(defaultExtension) ) |
111 | // if( filter2.Mid(is+1,i-is-1) == defaultExtension ) | |
1f2f0331 VZ |
112 | { |
113 | filterFind = filterIndex; | |
114 | break; | |
115 | } | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | fileDialog.SetFilterIndex(filterFind); | |
121 | } | |
122 | ||
e15e548b | 123 | if ( fileDialog.ShowModal() == wxID_OK ) |
1f2f0331 | 124 | { |
837e5743 | 125 | wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath()); |
e15e548b | 126 | return wxBuffer; |
1f2f0331 | 127 | } |
e15e548b | 128 | else |
ba681060 | 129 | return wxGetEmptyString(); |
2bda0e17 KB |
130 | } |
131 | ||
132 | # if __BORLANDC__ | |
1f2f0331 | 133 | # include <dir.h> // for MAXPATH etc. ( Borland 3.1 ) |
2bda0e17 KB |
134 | # endif |
135 | ||
136 | # ifndef MAXPATH | |
1f2f0331 | 137 | # define MAXPATH 400 |
2bda0e17 KB |
138 | # endif |
139 | ||
140 | # ifndef MAXDRIVE | |
141 | # define MAXDRIVE 3 | |
142 | # endif | |
143 | ||
1f2f0331 | 144 | # ifndef MAXFILE |
2bda0e17 KB |
145 | # define MAXFILE 9 |
146 | # endif | |
147 | ||
148 | # ifndef MAXEXT | |
149 | # define MAXEXT 5 | |
150 | # endif | |
151 | ||
152 | ||
837e5743 OK |
153 | wxString wxFileSelectorEx(const wxChar *title, |
154 | const wxChar *defaultDir, | |
155 | const wxChar *defaultFileName, | |
e15e548b | 156 | int* defaultFilterIndex, |
837e5743 | 157 | const wxChar *filter, |
e15e548b VZ |
158 | int flags, |
159 | wxWindow* parent, | |
160 | int x, | |
161 | int y) | |
2bda0e17 KB |
162 | |
163 | { | |
837e5743 OK |
164 | wxFileDialog fileDialog(parent, title ? title : _T(""), defaultDir ? defaultDir : _T(""), |
165 | defaultFileName ? defaultFileName : _T(""), filter ? filter : _T(""), flags, wxPoint(x, y)); | |
2bda0e17 | 166 | |
e15e548b VZ |
167 | if ( fileDialog.ShowModal() == wxID_OK ) |
168 | { | |
169 | *defaultFilterIndex = fileDialog.GetFilterIndex(); | |
837e5743 | 170 | wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath()); |
e15e548b VZ |
171 | return wxBuffer; |
172 | } | |
173 | else | |
ba681060 | 174 | return wxGetEmptyString(); |
2bda0e17 KB |
175 | } |
176 | ||
177 | wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message, | |
178 | const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard, | |
179 | long style, const wxPoint& pos) | |
180 | { | |
181 | m_message = message; | |
182 | m_dialogStyle = style; | |
183 | m_parent = parent; | |
837e5743 | 184 | m_path = _T(""); |
e15e548b VZ |
185 | m_fileName = defaultFileName; |
186 | m_dir = defaultDir; | |
187 | m_wildCard = wildCard; | |
188 | m_filterIndex = 1; | |
2bda0e17 KB |
189 | } |
190 | ||
191 | int wxFileDialog::ShowModal(void) | |
192 | { | |
1f2f0331 VZ |
193 | HWND hWnd = 0; |
194 | if (m_parent) hWnd = (HWND) m_parent->GetHWND(); | |
2bda0e17 | 195 | |
837e5743 OK |
196 | static wxChar fileNameBuffer [ MAXPATH ]; // the file-name |
197 | wxChar titleBuffer [ MAXFILE+1+MAXEXT ]; // the file-name, without path | |
2bda0e17 | 198 | |
837e5743 OK |
199 | *fileNameBuffer = _T('\0'); |
200 | *titleBuffer = _T('\0'); | |
2bda0e17 | 201 | |
2bda0e17 | 202 | long msw_flags = 0; |
e15e548b | 203 | if ( (m_dialogStyle & wxHIDE_READONLY) || (m_dialogStyle & wxSAVE) ) |
1f2f0331 | 204 | msw_flags |= OFN_HIDEREADONLY; |
e15e548b | 205 | if ( m_dialogStyle & wxFILE_MUST_EXIST ) |
1f2f0331 | 206 | msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; |
2bda0e17 | 207 | |
e15e548b VZ |
208 | OPENFILENAME of; |
209 | memset(&of, 0, sizeof(OPENFILENAME)); | |
2bda0e17 KB |
210 | |
211 | of.lpstrCustomFilter = NULL; // system should not save custom filter | |
e15e548b | 212 | of.nMaxCustFilter = 0L; |
2bda0e17 | 213 | |
e15e548b VZ |
214 | of.nFileOffset = 0; // 0-based pointer to filname in lpstFile |
215 | of.nFileExtension = 0; // 0-based pointer to extension in lpstrFile | |
216 | of.lpstrDefExt = NULL; // no default extension | |
2bda0e17 | 217 | |
e15e548b VZ |
218 | of.lStructSize = sizeof(OPENFILENAME); |
219 | of.hwndOwner = hWnd; | |
837e5743 | 220 | of.lpstrTitle = WXSTRINGCAST m_message; |
2bda0e17 KB |
221 | |
222 | ||
e15e548b VZ |
223 | of.lpstrFileTitle = titleBuffer; |
224 | of.nMaxFileTitle = MAXFILE + 1 + MAXEXT; // Windows 3.0 and 3.1 | |
2bda0e17 | 225 | |
0bc9b25e JS |
226 | // Convert forward slashes to backslashes (file selector doesn't like |
227 | // forward slashes) | |
228 | size_t i = 0; | |
229 | size_t len = m_dir.Length(); | |
230 | for (i = 0; i < len; i++) | |
837e5743 OK |
231 | if (m_dir[i] == _T('/')) |
232 | m_dir[i] = _T('\\'); | |
0bc9b25e | 233 | |
837e5743 | 234 | of.lpstrInitialDir = m_dir.c_str(); |
2bda0e17 | 235 | |
e15e548b | 236 | of.Flags = msw_flags; |
2bda0e17 KB |
237 | |
238 | ||
2bda0e17 KB |
239 | //=== Like Alejandro Sierra's wildcard modification >>=================== |
240 | /* | |
1f2f0331 VZ |
241 | In wxFileSelector you can put, instead of a single wild_card, |
242 | pairs of strings separated by '|'. | |
243 | The first string is a description, and the | |
244 | second is the wild card. You can put any number of pairs. | |
2bda0e17 | 245 | |
1f2f0331 | 246 | eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2" |
2bda0e17 | 247 | |
1f2f0331 VZ |
248 | If you put a single wild card, it works as before the modification. |
249 | */ | |
2bda0e17 KB |
250 | //======================================================================= |
251 | ||
4dba84be | 252 | wxString theFilter; |
837e5743 OK |
253 | if ( wxStrlen(m_wildCard) == 0 ) |
254 | theFilter = wxString(_T("*.*")); | |
4dba84be JS |
255 | else |
256 | theFilter = m_wildCard ; | |
1f2f0331 | 257 | wxString filterBuffer; |
2bda0e17 | 258 | |
837e5743 | 259 | if ( !wxStrchr( theFilter, _T('|') ) ) { // only one filter ==> default text |
1f2f0331 VZ |
260 | filterBuffer.Printf(_("Files (%s)|%s"), |
261 | theFilter.c_str(), theFilter.c_str()); | |
e15e548b | 262 | } |
1f2f0331 VZ |
263 | else { // more then one filter |
264 | filterBuffer = theFilter; | |
2bda0e17 | 265 | |
574c0bbf JS |
266 | } |
267 | ||
837e5743 | 268 | filterBuffer += _T("|"); |
574c0bbf | 269 | // Replace | with \0 |
0bc9b25e | 270 | for (i = 0; i < filterBuffer.Len(); i++ ) { |
837e5743 OK |
271 | if ( filterBuffer.GetChar(i) == _T('|') ) { |
272 | filterBuffer[i] = _T('\0'); | |
e15e548b VZ |
273 | } |
274 | } | |
2bda0e17 | 275 | |
837e5743 | 276 | of.lpstrFilter = (LPTSTR)(const wxChar *)filterBuffer; |
e15e548b | 277 | of.nFilterIndex = m_filterIndex; |
2bda0e17 KB |
278 | |
279 | //=== Setting defaultFileName >>========================================= | |
280 | ||
837e5743 OK |
281 | wxStrncpy( fileNameBuffer, (const wxChar *)m_fileName, MAXPATH-1 ); |
282 | fileNameBuffer[ MAXPATH-1 ] = _T('\0'); | |
2bda0e17 | 283 | |
e15e548b VZ |
284 | of.lpstrFile = fileNameBuffer; // holds returned filename |
285 | of.nMaxFile = MAXPATH; | |
2bda0e17 KB |
286 | |
287 | //== Execute FileDialog >>================================================= | |
288 | ||
1f2f0331 VZ |
289 | bool success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0) |
290 | : (GetOpenFileName(&of) != 0); | |
2bda0e17 KB |
291 | |
292 | if ( success ) | |
293 | { | |
837e5743 | 294 | const wxChar* extension = NULL; |
1f2f0331 | 295 | |
2bda0e17 KB |
296 | //=== Adding the correct extension >>================================= |
297 | ||
298 | m_filterIndex = (int)of.nFilterIndex; | |
299 | ||
837e5743 | 300 | if ( of.nFileExtension && fileNameBuffer[ of.nFileExtension-1] != _T('.') ) |
2bda0e17 | 301 | { // user has typed an filename |
1f2f0331 | 302 | // without an extension: |
2bda0e17 | 303 | |
e15e548b | 304 | int maxFilter = (int)(of.nFilterIndex*2L-1L); |
1f2f0331 | 305 | extension = filterBuffer; |
2bda0e17 | 306 | |
e15e548b | 307 | for( int i = 0; i < maxFilter; i++ ) { // get extension |
837e5743 | 308 | extension = extension + wxStrlen( extension ) +1; |
e15e548b | 309 | } |
2bda0e17 | 310 | |
837e5743 | 311 | extension = wxStrrchr( extension, _T('.') ); |
1f2f0331 | 312 | if ( extension // != "blabla" |
837e5743 OK |
313 | && !wxStrrchr( extension, _T('*') ) // != "blabla.*" |
314 | && !wxStrrchr( extension, _T('?') ) // != "blabla.?" | |
1f2f0331 | 315 | && extension[1] // != "blabla." |
837e5743 | 316 | && extension[1] != _T(' ') ) // != "blabla. " |
e15e548b | 317 | { |
1f2f0331 VZ |
318 | // now concat extension to the fileName: |
319 | m_fileName = wxString(fileNameBuffer) + extension; | |
2bda0e17 | 320 | |
837e5743 OK |
321 | int len = wxStrlen( fileNameBuffer ); |
322 | wxStrncpy( fileNameBuffer + len, extension, MAXPATH - len ); | |
323 | fileNameBuffer[ MAXPATH -1 ] = _T('\0'); | |
2bda0e17 | 324 | } |
e15e548b | 325 | } |
2bda0e17 KB |
326 | |
327 | m_path = fileNameBuffer; | |
e15e548b | 328 | m_fileName = wxFileNameFromPath(fileNameBuffer); |
2bda0e17 KB |
329 | |
330 | ||
e15e548b | 331 | //=== Simulating the wxOVERWRITE_PROMPT >>============================ |
2bda0e17 | 332 | |
1f2f0331 VZ |
333 | if ( (m_dialogStyle & wxOVERWRITE_PROMPT) && |
334 | ::wxFileExists( fileNameBuffer ) ) | |
2bda0e17 | 335 | { |
1f2f0331 VZ |
336 | wxString messageText; |
337 | messageText.Printf(_("Replace file '%s'?"), fileNameBuffer); | |
2bda0e17 | 338 | |
1f2f0331 | 339 | if ( wxMessageBox(messageText, m_message, wxYES_NO ) != wxYES ) |
2bda0e17 KB |
340 | { |
341 | success = FALSE; | |
e15e548b | 342 | } |
e15e548b | 343 | } |
2bda0e17 | 344 | |
7cc98b3e VZ |
345 | } |
346 | else | |
347 | { | |
348 | // common dialog failed - why? | |
349 | #ifdef __WXDEBUG__ | |
350 | DWORD dwErr = CommDlgExtendedError(); | |
351 | if ( dwErr != 0 ) | |
352 | { | |
353 | // this msg is only for developers | |
354 | wxLogError(_T("Common dialog failed with error code %0lx."), | |
355 | dwErr); | |
356 | } | |
357 | //else: it was just cancelled | |
358 | #endif | |
359 | } | |
2bda0e17 | 360 | |
7cc98b3e | 361 | return success ? wxID_OK : wxID_CANCEL; |
2bda0e17 KB |
362 | |
363 | } | |
364 | ||
ba681060 VZ |
365 | // Generic file load/save dialog (for internal use only) |
366 | static | |
367 | wxString wxDefaultFileSelector(bool load, | |
837e5743 OK |
368 | const wxChar *what, |
369 | const wxChar *extension, | |
370 | const wxChar *default_name, | |
ba681060 | 371 | wxWindow *parent) |
2bda0e17 | 372 | { |
1f2f0331 | 373 | wxString prompt; |
837e5743 OK |
374 | wxString str; |
375 | if (load) str = _("Load %s file"); | |
376 | else str = _("Save %s file"); | |
1f2f0331 VZ |
377 | prompt.Printf(str, what); |
378 | ||
837e5743 OK |
379 | const wxChar *ext = extension; |
380 | if (*ext == _T('.')) | |
1f2f0331 | 381 | ext++; |
2bda0e17 | 382 | |
1f2f0331 | 383 | wxString wild; |
837e5743 | 384 | wild.Printf(_T("*.%s"), ext); |
2bda0e17 KB |
385 | |
386 | return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent); | |
387 | } | |
388 | ||
389 | // Generic file load dialog | |
837e5743 OK |
390 | WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what, |
391 | const wxChar *extension, | |
392 | const wxChar *default_name, | |
ba681060 | 393 | wxWindow *parent) |
2bda0e17 | 394 | { |
ba681060 | 395 | return wxDefaultFileSelector(TRUE, what, extension, default_name, parent); |
2bda0e17 KB |
396 | } |
397 | ||
2bda0e17 | 398 | // Generic file save dialog |
837e5743 OK |
399 | WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what, |
400 | const wxChar *extension, | |
401 | const wxChar *default_name, | |
ba681060 | 402 | wxWindow *parent) |
2bda0e17 | 403 | { |
ba681060 | 404 | return wxDefaultFileSelector(FALSE, what, extension, default_name, parent); |
2bda0e17 KB |
405 | } |
406 |