1. wxLoad/SaveFileSelector return "wxString" instead of "char *"
[wxWidgets.git] / src / msw / filedlg.cpp
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 {
111 filterFind = filterIndex;
112 break;
113 }
114 }
115 }
116 }
117
118 fileDialog.SetFilterIndex(filterFind);
119 }
120
121 if ( fileDialog.ShowModal() == wxID_OK )
122 {
123 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
124 return wxBuffer;
125 }
126 else
127 return wxGetEmptyString();
128 }
129
130 # if __BORLANDC__
131 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
132 # endif
133
134 # ifndef MAXPATH
135 # define MAXPATH 400
136 # endif
137
138 # ifndef MAXDRIVE
139 # define MAXDRIVE 3
140 # endif
141
142 # ifndef MAXFILE
143 # define MAXFILE 9
144 # endif
145
146 # ifndef MAXEXT
147 # define MAXEXT 5
148 # endif
149
150
151 wxString wxFileSelectorEx(const char *title,
152 const char *defaultDir,
153 const char *defaultFileName,
154 int* defaultFilterIndex,
155 const char *filter,
156 int flags,
157 wxWindow* parent,
158 int x,
159 int y)
160
161 {
162 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
163 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
164
165 if ( fileDialog.ShowModal() == wxID_OK )
166 {
167 *defaultFilterIndex = fileDialog.GetFilterIndex();
168 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
169 return wxBuffer;
170 }
171 else
172 return wxGetEmptyString();
173 }
174
175 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
176 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
177 long style, const wxPoint& pos)
178 {
179 m_message = message;
180 m_dialogStyle = style;
181 m_parent = parent;
182 m_path = "";
183 m_fileName = defaultFileName;
184 m_dir = defaultDir;
185 m_wildCard = wildCard;
186 m_filterIndex = 1;
187 }
188
189 int wxFileDialog::ShowModal(void)
190 {
191 HWND hWnd = 0;
192 if (m_parent) hWnd = (HWND) m_parent->GetHWND();
193
194 static char fileNameBuffer [ MAXPATH ]; // the file-name
195 char titleBuffer [ MAXFILE+1+MAXEXT ]; // the file-name, without path
196
197 *fileNameBuffer = '\0';
198 *titleBuffer = '\0';
199
200 long msw_flags = 0;
201 if ( (m_dialogStyle & wxHIDE_READONLY) || (m_dialogStyle & wxSAVE) )
202 msw_flags |= OFN_HIDEREADONLY;
203 if ( m_dialogStyle & wxFILE_MUST_EXIST )
204 msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
205
206 OPENFILENAME of;
207 memset(&of, 0, sizeof(OPENFILENAME));
208
209 of.lpstrCustomFilter = NULL; // system should not save custom filter
210 of.nMaxCustFilter = 0L;
211
212 of.nFileOffset = 0; // 0-based pointer to filname in lpstFile
213 of.nFileExtension = 0; // 0-based pointer to extension in lpstrFile
214 of.lpstrDefExt = NULL; // no default extension
215
216 of.lStructSize = sizeof(OPENFILENAME);
217 of.hwndOwner = hWnd;
218 of.lpstrTitle = (char *)(const char *)m_message;
219
220
221 of.lpstrFileTitle = titleBuffer;
222 of.nMaxFileTitle = MAXFILE + 1 + MAXEXT; // Windows 3.0 and 3.1
223
224 of.lpstrInitialDir = (const char *) m_dir;
225
226 of.Flags = msw_flags;
227
228
229
230 //=== Like Alejandro Sierra's wildcard modification >>===================
231 /*
232 In wxFileSelector you can put, instead of a single wild_card,
233 pairs of strings separated by '|'.
234 The first string is a description, and the
235 second is the wild card. You can put any number of pairs.
236
237 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
238
239 If you put a single wild card, it works as before the modification.
240 */
241 //=======================================================================
242
243 wxString theFilter = ( Strlen(m_wildCard) == 0 ) ? wxString("*.*") : m_wildCard;
244 wxString filterBuffer;
245
246 if ( !strchr( theFilter, '|' ) ) { // only one filter ==> default text
247 filterBuffer.Printf(_("Files (%s)|%s"),
248 theFilter.c_str(), theFilter.c_str());
249 }
250 else { // more then one filter
251 filterBuffer = theFilter;
252
253 for ( unsigned int i = 0; i < filterBuffer.Len(); i++ ) {
254 if ( filterBuffer.GetChar(i) == '|' ) {
255 filterBuffer[i] = '\0';
256 }
257 }
258 }
259
260 of.lpstrFilter = (LPSTR)(const char *)filterBuffer;
261 of.nFilterIndex = m_filterIndex;
262
263 //=== Setting defaultFileName >>=========================================
264
265 strncpy( fileNameBuffer, (const char *)m_fileName, MAXPATH-1 );
266 fileNameBuffer[ MAXPATH-1 ] = '\0';
267
268 of.lpstrFile = fileNameBuffer; // holds returned filename
269 of.nMaxFile = MAXPATH;
270
271 //== Execute FileDialog >>=================================================
272
273 bool success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
274 : (GetOpenFileName(&of) != 0);
275
276 if ( success )
277 {
278 const char* extension = NULL;
279
280 //=== Adding the correct extension >>=================================
281
282 m_filterIndex = (int)of.nFilterIndex;
283
284 if ( of.nFileExtension && fileNameBuffer[ of.nFileExtension-1] != '.' )
285 { // user has typed an filename
286 // without an extension:
287
288 int maxFilter = (int)(of.nFilterIndex*2L-1L);
289 extension = filterBuffer;
290
291 for( int i = 0; i < maxFilter; i++ ) { // get extension
292 extension = extension + strlen( extension ) +1;
293 }
294
295 extension = strrchr( extension, '.' );
296 if ( extension // != "blabla"
297 && !strrchr( extension, '*' ) // != "blabla.*"
298 && !strrchr( extension, '?' ) // != "blabla.?"
299 && extension[1] // != "blabla."
300 && extension[1] != ' ' ) // != "blabla. "
301 {
302 // now concat extension to the fileName:
303 m_fileName = wxString(fileNameBuffer) + extension;
304
305 int len = strlen( fileNameBuffer );
306 strncpy( fileNameBuffer + len, extension, MAXPATH - len );
307 fileNameBuffer[ MAXPATH -1 ] = '\0';
308 }
309 }
310
311 m_path = fileNameBuffer;
312 m_fileName = wxFileNameFromPath(fileNameBuffer);
313
314
315 //=== Simulating the wxOVERWRITE_PROMPT >>============================
316
317 if ( (m_dialogStyle & wxOVERWRITE_PROMPT) &&
318 ::wxFileExists( fileNameBuffer ) )
319 {
320 wxString messageText;
321 messageText.Printf(_("Replace file '%s'?"), fileNameBuffer);
322
323 if ( wxMessageBox(messageText, m_message, wxYES_NO ) != wxYES )
324 {
325 success = FALSE;
326 }
327 }
328
329 } // END: if ( success )
330
331 return (success ? wxID_OK : wxID_CANCEL) ;
332
333 }
334
335 // Generic file load/save dialog (for internal use only)
336 static
337 wxString wxDefaultFileSelector(bool load,
338 const char *what,
339 const char *extension,
340 const char *default_name,
341 wxWindow *parent)
342 {
343 wxString prompt;
344 wxString str = load ? _("Load %s file") : _("Save %s file");
345 prompt.Printf(str, what);
346
347 const char *ext = extension;
348 if (*ext == '.')
349 ext++;
350
351 wxString wild;
352 wild.Printf("*.%s", ext);
353
354 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
355 }
356
357 // Generic file load dialog
358 WXDLLEXPORT wxString wxLoadFileSelector(const char *what,
359 const char *extension,
360 const char *default_name,
361 wxWindow *parent)
362 {
363 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
364 }
365
366 // Generic file save dialog
367 WXDLLEXPORT wxString wxSaveFileSelector(const char *what,
368 const char *extension,
369 const char *default_name,
370 wxWindow *parent)
371 {
372 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
373 }
374