]> git.saurik.com Git - wxWidgets.git/blame - src/msw/filedlg.cpp
bitmap and image updates
[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"
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
50wxString 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 )
223d09f6 73 filter2 = wxString(wxT("*.")) + 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 {
223d09f6 95 if( filter2.GetChar(i) == wxT('|') )
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 {
223d09f6 104 if(filter2[i] == wxT('|'))
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
153wxString 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{
223d09f6
KB
164 wxFileDialog fileDialog(parent, title ? title : wxT(""), defaultDir ? defaultDir : wxT(""),
165 defaultFileName ? defaultFileName : wxT(""), filter ? filter : wxT(""), 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
177wxFileDialog::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;
223d09f6 184 m_path = wxT("");
e15e548b
VZ
185 m_fileName = defaultFileName;
186 m_dir = defaultDir;
187 m_wildCard = wildCard;
6b8b0e7c 188 m_filterIndex = 0;
2bda0e17
KB
189}
190
191int 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
223d09f6
KB
199 *fileNameBuffer = wxT('\0');
200 *titleBuffer = wxT('\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++)
223d09f6
KB
231 if (m_dir[i] == wxT('/'))
232 m_dir[i] = wxT('\\');
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 253 if ( wxStrlen(m_wildCard) == 0 )
223d09f6 254 theFilter = wxString(wxT("*.*"));
4dba84be
JS
255 else
256 theFilter = m_wildCard ;
1f2f0331 257 wxString filterBuffer;
2bda0e17 258
223d09f6 259 if ( !wxStrchr( theFilter, wxT('|') ) ) { // 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
223d09f6 268 filterBuffer += wxT("|");
574c0bbf 269 // Replace | with \0
0bc9b25e 270 for (i = 0; i < filterBuffer.Len(); i++ ) {
223d09f6
KB
271 if ( filterBuffer.GetChar(i) == wxT('|') ) {
272 filterBuffer[i] = wxT('\0');
e15e548b
VZ
273 }
274 }
2bda0e17 275
837e5743 276 of.lpstrFilter = (LPTSTR)(const wxChar *)filterBuffer;
6b8b0e7c 277 of.nFilterIndex = m_filterIndex + 1; // m_filterIndex is zero-based, but nFilterIndex is 1-based
2bda0e17
KB
278
279 //=== Setting defaultFileName >>=========================================
280
837e5743 281 wxStrncpy( fileNameBuffer, (const wxChar *)m_fileName, MAXPATH-1 );
223d09f6 282 fileNameBuffer[ MAXPATH-1 ] = wxT('\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
6b8b0e7c 298 m_filterIndex = wxMax((int)of.nFilterIndex - 1, 0);
2bda0e17 299
223d09f6 300 if ( of.nFileExtension && fileNameBuffer[ of.nFileExtension-1] != wxT('.') )
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
223d09f6 311 extension = wxStrrchr( extension, wxT('.') );
1f2f0331 312 if ( extension // != "blabla"
223d09f6
KB
313 && !wxStrrchr( extension, wxT('*') ) // != "blabla.*"
314 && !wxStrrchr( extension, wxT('?') ) // != "blabla.?"
1f2f0331 315 && extension[1] // != "blabla."
223d09f6 316 && extension[1] != wxT(' ') ) // != "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 );
223d09f6 323 fileNameBuffer[ MAXPATH -1 ] = wxT('\0');
2bda0e17 324 }
e15e548b 325 }
2bda0e17
KB
326
327 m_path = fileNameBuffer;
e15e548b 328 m_fileName = wxFileNameFromPath(fileNameBuffer);
c5be98a6 329 m_dir = wxPathOnly(fileNameBuffer);
2bda0e17
KB
330
331
e15e548b 332 //=== Simulating the wxOVERWRITE_PROMPT >>============================
2bda0e17 333
1f2f0331
VZ
334 if ( (m_dialogStyle & wxOVERWRITE_PROMPT) &&
335 ::wxFileExists( fileNameBuffer ) )
2bda0e17 336 {
1f2f0331
VZ
337 wxString messageText;
338 messageText.Printf(_("Replace file '%s'?"), fileNameBuffer);
2bda0e17 339
1f2f0331 340 if ( wxMessageBox(messageText, m_message, wxYES_NO ) != wxYES )
2bda0e17
KB
341 {
342 success = FALSE;
e15e548b 343 }
e15e548b 344 }
2bda0e17 345
7cc98b3e
VZ
346 }
347 else
348 {
349 // common dialog failed - why?
350#ifdef __WXDEBUG__
351 DWORD dwErr = CommDlgExtendedError();
352 if ( dwErr != 0 )
353 {
354 // this msg is only for developers
223d09f6 355 wxLogError(wxT("Common dialog failed with error code %0lx."),
7cc98b3e
VZ
356 dwErr);
357 }
358 //else: it was just cancelled
359#endif
360 }
2bda0e17 361
7cc98b3e 362 return success ? wxID_OK : wxID_CANCEL;
2bda0e17
KB
363
364}
365
ba681060
VZ
366// Generic file load/save dialog (for internal use only)
367static
368wxString wxDefaultFileSelector(bool load,
837e5743
OK
369 const wxChar *what,
370 const wxChar *extension,
371 const wxChar *default_name,
ba681060 372 wxWindow *parent)
2bda0e17 373{
1f2f0331 374 wxString prompt;
837e5743
OK
375 wxString str;
376 if (load) str = _("Load %s file");
377 else str = _("Save %s file");
1f2f0331
VZ
378 prompt.Printf(str, what);
379
837e5743 380 const wxChar *ext = extension;
223d09f6 381 if (*ext == wxT('.'))
1f2f0331 382 ext++;
2bda0e17 383
1f2f0331 384 wxString wild;
223d09f6 385 wild.Printf(wxT("*.%s"), ext);
2bda0e17
KB
386
387 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
388}
389
390// Generic file load dialog
837e5743
OK
391WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
392 const wxChar *extension,
393 const wxChar *default_name,
ba681060 394 wxWindow *parent)
2bda0e17 395{
ba681060 396 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
2bda0e17
KB
397}
398
2bda0e17 399// Generic file save dialog
837e5743
OK
400WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
401 const wxChar *extension,
402 const wxChar *default_name,
ba681060 403 wxWindow *parent)
2bda0e17 404{
ba681060 405 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
2bda0e17
KB
406}
407