]> git.saurik.com Git - wxWidgets.git/blame - src/msw/filedlg.cpp
Cured last focus bug (I hope); some wxMotif mods
[wxWidgets.git] / src / msw / filedlg.cpp
CommitLineData
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"
2bda0e17
KB
31#endif
32
33#include <windows.h>
34
ce3ed50d 35#if !defined(__WIN32__) || defined(__SALFORDC__)
ba681060 36 #include <commdlg.h>
2bda0e17
KB
37#endif
38
39#include "wx/msw/private.h"
40
41#include <math.h>
42#include <stdlib.h>
43#include <string.h>
44
2bda0e17 45#if !USE_SHARED_LIBRARY
ba681060 46 IMPLEMENT_CLASS(wxFileDialog, wxDialog)
2bda0e17
KB
47#endif
48
ba681060
VZ
49wxString 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)
2bda0e17 57{
1f2f0331
VZ
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;
e15e548b 71 if ( defaultExtension && !filter )
1f2f0331 72 filter2 = wxString("*.") + defaultExtension;
e15e548b
VZ
73 else if ( filter )
74 filter2 = filter;
75
76 wxString defaultDirString;
77 if (defaultDir)
78 defaultDirString = defaultDir;
e15e548b
VZ
79
80 wxString defaultFilenameString;
81 if (defaultFileName)
82 defaultFilenameString = defaultFileName;
1f2f0331
VZ
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 {
574c0bbf
JS
109 if( filter2.Mid(is+1,i-is-1).Contains(defaultExtension) )
110// if( filter2.Mid(is+1,i-is-1) == defaultExtension )
1f2f0331
VZ
111 {
112 filterFind = filterIndex;
113 break;
114 }
115 }
116 }
117 }
118
119 fileDialog.SetFilterIndex(filterFind);
120 }
121
e15e548b 122 if ( fileDialog.ShowModal() == wxID_OK )
1f2f0331 123 {
e15e548b
VZ
124 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
125 return wxBuffer;
1f2f0331 126 }
e15e548b 127 else
ba681060 128 return wxGetEmptyString();
2bda0e17
KB
129}
130
131# if __BORLANDC__
1f2f0331 132# include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
2bda0e17
KB
133# endif
134
135# ifndef MAXPATH
1f2f0331 136# define MAXPATH 400
2bda0e17
KB
137# endif
138
139# ifndef MAXDRIVE
140# define MAXDRIVE 3
141# endif
142
1f2f0331 143# ifndef MAXFILE
2bda0e17
KB
144# define MAXFILE 9
145# endif
146
147# ifndef MAXEXT
148# define MAXEXT 5
149# endif
150
151
ba681060 152wxString wxFileSelectorEx(const char *title,
e15e548b
VZ
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)
2bda0e17
KB
161
162{
e15e548b 163 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
2bda0e17
KB
164 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
165
e15e548b
VZ
166 if ( fileDialog.ShowModal() == wxID_OK )
167 {
168 *defaultFilterIndex = fileDialog.GetFilterIndex();
169 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
170 return wxBuffer;
171 }
172 else
ba681060 173 return wxGetEmptyString();
2bda0e17
KB
174}
175
176wxFileDialog::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;
e15e548b
VZ
183 m_path = "";
184 m_fileName = defaultFileName;
185 m_dir = defaultDir;
186 m_wildCard = wildCard;
187 m_filterIndex = 1;
2bda0e17
KB
188}
189
190int wxFileDialog::ShowModal(void)
191{
1f2f0331
VZ
192 HWND hWnd = 0;
193 if (m_parent) hWnd = (HWND) m_parent->GetHWND();
2bda0e17 194
e15e548b
VZ
195 static char fileNameBuffer [ MAXPATH ]; // the file-name
196 char titleBuffer [ MAXFILE+1+MAXEXT ]; // the file-name, without path
2bda0e17 197
e15e548b 198 *fileNameBuffer = '\0';
2bda0e17
KB
199 *titleBuffer = '\0';
200
2bda0e17 201 long msw_flags = 0;
e15e548b 202 if ( (m_dialogStyle & wxHIDE_READONLY) || (m_dialogStyle & wxSAVE) )
1f2f0331 203 msw_flags |= OFN_HIDEREADONLY;
e15e548b 204 if ( m_dialogStyle & wxFILE_MUST_EXIST )
1f2f0331 205 msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
2bda0e17 206
e15e548b
VZ
207 OPENFILENAME of;
208 memset(&of, 0, sizeof(OPENFILENAME));
2bda0e17
KB
209
210 of.lpstrCustomFilter = NULL; // system should not save custom filter
e15e548b 211 of.nMaxCustFilter = 0L;
2bda0e17 212
e15e548b
VZ
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
2bda0e17 216
e15e548b
VZ
217 of.lStructSize = sizeof(OPENFILENAME);
218 of.hwndOwner = hWnd;
219 of.lpstrTitle = (char *)(const char *)m_message;
2bda0e17
KB
220
221
e15e548b
VZ
222 of.lpstrFileTitle = titleBuffer;
223 of.nMaxFileTitle = MAXFILE + 1 + MAXEXT; // Windows 3.0 and 3.1
2bda0e17 224
e15e548b 225 of.lpstrInitialDir = (const char *) m_dir;
2bda0e17 226
e15e548b 227 of.Flags = msw_flags;
2bda0e17
KB
228
229
230
231 //=== Like Alejandro Sierra's wildcard modification >>===================
232 /*
1f2f0331
VZ
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.
2bda0e17 237
1f2f0331 238 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
2bda0e17 239
1f2f0331
VZ
240 If you put a single wild card, it works as before the modification.
241 */
2bda0e17
KB
242 //=======================================================================
243
4dba84be
JS
244 wxString theFilter;
245 if ( Strlen(m_wildCard) == 0 )
246 theFilter = wxString("*.*");
247 else
248 theFilter = m_wildCard ;
1f2f0331 249 wxString filterBuffer;
2bda0e17 250
1f2f0331
VZ
251 if ( !strchr( theFilter, '|' ) ) { // only one filter ==> default text
252 filterBuffer.Printf(_("Files (%s)|%s"),
253 theFilter.c_str(), theFilter.c_str());
e15e548b 254 }
1f2f0331
VZ
255 else { // more then one filter
256 filterBuffer = theFilter;
2bda0e17 257
574c0bbf
JS
258 }
259
260 filterBuffer += "|";
261 // Replace | with \0
262 for ( unsigned int i = 0; i < filterBuffer.Len(); i++ ) {
263 if ( filterBuffer.GetChar(i) == '|' ) {
264 filterBuffer[i] = '\0';
e15e548b
VZ
265 }
266 }
2bda0e17 267
1f2f0331 268 of.lpstrFilter = (LPSTR)(const char *)filterBuffer;
e15e548b 269 of.nFilterIndex = m_filterIndex;
2bda0e17
KB
270
271 //=== Setting defaultFileName >>=========================================
272
273 strncpy( fileNameBuffer, (const char *)m_fileName, MAXPATH-1 );
274 fileNameBuffer[ MAXPATH-1 ] = '\0';
275
e15e548b
VZ
276 of.lpstrFile = fileNameBuffer; // holds returned filename
277 of.nMaxFile = MAXPATH;
2bda0e17
KB
278
279 //== Execute FileDialog >>=================================================
280
1f2f0331
VZ
281 bool success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
282 : (GetOpenFileName(&of) != 0);
2bda0e17
KB
283
284 if ( success )
285 {
1f2f0331
VZ
286 const char* extension = NULL;
287
2bda0e17
KB
288 //=== Adding the correct extension >>=================================
289
290 m_filterIndex = (int)of.nFilterIndex;
291
e15e548b 292 if ( of.nFileExtension && fileNameBuffer[ of.nFileExtension-1] != '.' )
2bda0e17 293 { // user has typed an filename
1f2f0331 294 // without an extension:
2bda0e17 295
e15e548b 296 int maxFilter = (int)(of.nFilterIndex*2L-1L);
1f2f0331 297 extension = filterBuffer;
2bda0e17 298
e15e548b
VZ
299 for( int i = 0; i < maxFilter; i++ ) { // get extension
300 extension = extension + strlen( extension ) +1;
301 }
2bda0e17 302
fd3f686c 303 extension = strrchr( extension, '.' );
1f2f0331
VZ
304 if ( extension // != "blabla"
305 && !strrchr( extension, '*' ) // != "blabla.*"
306 && !strrchr( extension, '?' ) // != "blabla.?"
307 && extension[1] // != "blabla."
308 && extension[1] != ' ' ) // != "blabla. "
e15e548b 309 {
1f2f0331
VZ
310 // now concat extension to the fileName:
311 m_fileName = wxString(fileNameBuffer) + extension;
2bda0e17
KB
312
313 int len = strlen( fileNameBuffer );
e15e548b 314 strncpy( fileNameBuffer + len, extension, MAXPATH - len );
2bda0e17
KB
315 fileNameBuffer[ MAXPATH -1 ] = '\0';
316 }
e15e548b 317 }
2bda0e17
KB
318
319 m_path = fileNameBuffer;
e15e548b 320 m_fileName = wxFileNameFromPath(fileNameBuffer);
2bda0e17
KB
321
322
e15e548b 323 //=== Simulating the wxOVERWRITE_PROMPT >>============================
2bda0e17 324
1f2f0331
VZ
325 if ( (m_dialogStyle & wxOVERWRITE_PROMPT) &&
326 ::wxFileExists( fileNameBuffer ) )
2bda0e17 327 {
1f2f0331
VZ
328 wxString messageText;
329 messageText.Printf(_("Replace file '%s'?"), fileNameBuffer);
2bda0e17 330
1f2f0331 331 if ( wxMessageBox(messageText, m_message, wxYES_NO ) != wxYES )
2bda0e17
KB
332 {
333 success = FALSE;
e15e548b 334 }
e15e548b 335 }
2bda0e17
KB
336
337 } // END: if ( success )
338
e15e548b 339 return (success ? wxID_OK : wxID_CANCEL) ;
2bda0e17
KB
340
341}
342
ba681060
VZ
343// Generic file load/save dialog (for internal use only)
344static
345wxString wxDefaultFileSelector(bool load,
346 const char *what,
347 const char *extension,
348 const char *default_name,
349 wxWindow *parent)
2bda0e17 350{
1f2f0331 351 wxString prompt;
ba681060 352 wxString str = load ? _("Load %s file") : _("Save %s file");
1f2f0331
VZ
353 prompt.Printf(str, what);
354
355 const char *ext = extension;
356 if (*ext == '.')
357 ext++;
2bda0e17 358
1f2f0331
VZ
359 wxString wild;
360 wild.Printf("*.%s", ext);
2bda0e17
KB
361
362 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
363}
364
365// Generic file load dialog
ba681060
VZ
366WXDLLEXPORT wxString wxLoadFileSelector(const char *what,
367 const char *extension,
368 const char *default_name,
369 wxWindow *parent)
2bda0e17 370{
ba681060 371 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
2bda0e17
KB
372}
373
2bda0e17 374// Generic file save dialog
ba681060
VZ
375WXDLLEXPORT wxString wxSaveFileSelector(const char *what,
376 const char *extension,
377 const char *default_name,
378 wxWindow *parent)
2bda0e17 379{
ba681060 380 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
2bda0e17
KB
381}
382