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