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