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