]> git.saurik.com Git - wxWidgets.git/blame - src/msw/filedlg.cpp
Some Motif corrections; Dialog Editor compilation under Motif
[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"
2432b92d 27#include "wx/msgdlg.h"
2bda0e17
KB
28#include "wx/dialog.h"
29#include "wx/filedlg.h"
2432b92d 30#include "wx/intl.h"
2bda0e17
KB
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{
e15e548b
VZ
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));
450cab2d
MR
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
e15e548b 112 if ( fileDialog.ShowModal() == wxID_OK )
450cab2d 113 {
e15e548b
VZ
114 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
115 return wxBuffer;
450cab2d 116 }
e15e548b
VZ
117 else
118 return NULL;
2bda0e17
KB
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
e15e548b
VZ
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)
2bda0e17
KB
151
152{
e15e548b 153 wxFileDialog fileDialog(parent, title ? title : "", defaultDir ? defaultDir : "",
2bda0e17
KB
154 defaultFileName ? defaultFileName : "", filter ? filter : "", flags, wxPoint(x, y));
155
e15e548b
VZ
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;
2bda0e17
KB
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;
e15e548b
VZ
173 m_path = "";
174 m_fileName = defaultFileName;
175 m_dir = defaultDir;
176 m_wildCard = wildCard;
177 m_filterIndex = 1;
2bda0e17
KB
178}
179
180int wxFileDialog::ShowModal(void)
181{
182 HWND hWnd = 0;
183 if (m_parent) hWnd = (HWND) m_parent->GetHWND();
184
e15e548b
VZ
185 static char fileNameBuffer [ MAXPATH ]; // the file-name
186 char titleBuffer [ MAXFILE+1+MAXEXT ]; // the file-name, without path
2bda0e17 187
e15e548b 188 *fileNameBuffer = '\0';
2bda0e17
KB
189 *titleBuffer = '\0';
190
e15e548b
VZ
191 char* filterBuffer = NULL;
192 char* extension = NULL;
193 char* theFilter = (char *)(const char *)m_wildCard;
2bda0e17
KB
194
195 long msw_flags = 0;
e15e548b
VZ
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;
2bda0e17 200
e15e548b
VZ
201 OPENFILENAME of;
202 memset(&of, 0, sizeof(OPENFILENAME));
2bda0e17
KB
203
204 of.lpstrCustomFilter = NULL; // system should not save custom filter
e15e548b 205 of.nMaxCustFilter = 0L;
2bda0e17 206
e15e548b
VZ
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
2bda0e17 210
e15e548b
VZ
211 of.lStructSize = sizeof(OPENFILENAME);
212 of.hwndOwner = hWnd;
213 of.lpstrTitle = (char *)(const char *)m_message;
2bda0e17
KB
214
215
e15e548b
VZ
216 of.lpstrFileTitle = titleBuffer;
217 of.nMaxFileTitle = MAXFILE + 1 + MAXEXT; // Windows 3.0 and 3.1
2bda0e17 218
e15e548b 219 of.lpstrInitialDir = (const char *) m_dir;
2bda0e17 220
e15e548b 221 of.Flags = msw_flags;
2bda0e17
KB
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
e15e548b 238 if ( !theFilter || (strcmp(theFilter, "") == 0)) theFilter = "*.*";
2bda0e17 239
e15e548b 240 int filterBufferLen = 0;
2bda0e17 241
e15e548b
VZ
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 ];
2bda0e17 246
e15e548b
VZ
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 ];
2bda0e17 254
e15e548b
VZ
255 if ( filterBuffer ) {
256 strcpy( filterBuffer, theFilter );
257 }
258 }
2bda0e17 259
e15e548b
VZ
260 if ( filterBuffer ) { // Substituting '|' with '\0'
261 for ( int i = 0; i < filterBufferLen; i++ ) {
262 if ( filterBuffer[i] == '|' ) { filterBuffer[i] = '\0'; }
263 }
264 }
2bda0e17 265
e15e548b 266 filterBuffer[filterBufferLen+1] = '\0';
2bda0e17 267
e15e548b
VZ
268 of.lpstrFilter = (LPSTR)filterBuffer;
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
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
e15e548b 289 if ( of.nFileExtension && fileNameBuffer[ of.nFileExtension-1] != '.' )
2bda0e17
KB
290 { // user has typed an filename
291 // without an extension:
292
e15e548b
VZ
293 int maxFilter = (int)(of.nFilterIndex*2L-1L);
294 extension = filterBuffer;
2bda0e17 295
e15e548b
VZ
296 for( int i = 0; i < maxFilter; i++ ) { // get extension
297 extension = extension + strlen( extension ) +1;
298 }
2bda0e17 299
fd3f686c
VZ
300 extension = strrchr( extension, '.' );
301 if ( extension // != "blabla"
e15e548b
VZ
302 && !strrchr( extension, '*' ) // != "blabla.*"
303 && !strrchr( extension, '?' ) // != "blabla.?"
304 && extension[1] // != "blabla."
305 && extension[1] != ' ' ) // != "blabla. "
306 {
2bda0e17 307 // now concat extension to the fileName:
e15e548b 308 m_fileName = wxString(fileNameBuffer) + wxString(extension);
2bda0e17
KB
309
310 int len = strlen( fileNameBuffer );
e15e548b 311 strncpy( fileNameBuffer + len, extension, MAXPATH - len );
2bda0e17
KB
312 fileNameBuffer[ MAXPATH -1 ] = '\0';
313 }
e15e548b 314 }
2bda0e17
KB
315
316 m_path = fileNameBuffer;
e15e548b 317 m_fileName = wxFileNameFromPath(fileNameBuffer);
2bda0e17
KB
318
319
e15e548b 320 //=== Simulating the wxOVERWRITE_PROMPT >>============================
2bda0e17 321
e15e548b 322 if ( (m_dialogStyle & wxOVERWRITE_PROMPT) && ::wxFileExists( fileNameBuffer ) )
2bda0e17 323 {
e15e548b
VZ
324 char questionText[] = "Replace file\n%s?";
325 char* messageText = new char[strlen(questionText)+strlen(fileNameBuffer)-1];
326 sprintf( messageText, questionText, fileNameBuffer );
2bda0e17 327
e15e548b 328 if ( messageText && ( wxMessageBox( (const char *)messageText, m_message, wxYES_NO ) != wxYES ) )
2bda0e17
KB
329 {
330 success = FALSE;
e15e548b 331 }
2bda0e17 332
e15e548b
VZ
333 delete[] messageText;
334 }
2bda0e17
KB
335
336 } // END: if ( success )
337
338
339 delete[] filterBuffer;
340
e15e548b 341 return (success ? wxID_OK : wxID_CANCEL) ;
2bda0e17
KB
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)
3aadbb82 358 str = "Load %s file";
2bda0e17 359 else
3aadbb82
VZ
360 str = "Save %s file";
361 sprintf(prompt, wxGetTranslation(str), what);
2bda0e17
KB
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