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