]> git.saurik.com Git - wxWidgets.git/blame - src/os2/filedlg.cpp
simplify code so it always returns the same object
[wxWidgets.git] / src / os2 / filedlg.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
e031f1df 2// Name: src/os2/filedlg.cpp
0e320a79 3// Purpose: wxFileDialog
f0a56ab0 4// Author: David Webster
0e320a79 5// Modified by:
f0a56ab0 6// Created: 10/05/99
0e320a79 7// RCS-ID: $Id$
f0a56ab0 8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
fb46a9a6
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
6670f564
WS
19#if wxUSE_FILEDLG
20
949c9f74
WS
21#include "wx/filedlg.h"
22
fb46a9a6 23#ifndef WX_PRECOMP
fb46a9a6
DW
24 #include "wx/utils.h"
25 #include "wx/msgdlg.h"
e39b5fa4 26 #include "wx/filename.h"
fb46a9a6
DW
27 #include "wx/intl.h"
28 #include "wx/log.h"
4efd4259 29 #include "wx/app.h"
463c4d71 30 #include "wx/math.h"
fb46a9a6
DW
31#endif
32
33#define INCL_PM
34#include <os2.h>
35
36#include "wx/os2/private.h"
37
fb46a9a6
DW
38#include <stdlib.h>
39#include <string.h>
0e320a79 40
4efd4259 41#include "wx/tokenzr.h"
643e9cf9 42#include "wx/testing.h"
4efd4259
DW
43
44#define wxMAXPATH 1024
45#define wxMAXFILE 1024
46#define wxMAXEXT 5
47
48#ifndef MAXPATH
49# define MAXPATH 400
50#endif
51
52#ifndef MAXDRIVE
53# define MAXDRIVE 3
54#endif
55
56#ifndef MAXFILE
57# define MAXFILE 9
58#endif
59
60#ifndef MAXEXT
61# define MAXEXT 5
62#endif
6670f564 63
f74172ab 64IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
0e320a79 65
4efd4259
DW
66// ----------------------------------------------------------------------------
67// CLASS wxFileDialog
68// ----------------------------------------------------------------------------
69
70wxFileDialog::wxFileDialog (
71 wxWindow* pParent
72, const wxString& rsMessage
73, const wxString& rsDefaultDir
74, const wxString& rsDefaultFileName
75, const wxString& rsWildCard
76, long lStyle
ff3e84ff
VZ
77, const wxPoint& rPos,
78 const wxSize& sz,
79 const wxString& name
4efd4259 80)
ff3e84ff 81 :wxFileDialogBase(pParent, rsMessage, rsDefaultDir, rsDefaultFileName, rsWildCard, lStyle, rPos, sz, name)
f74172ab 82
0e320a79 83{
556151f5 84 // NB: all style checks are done by wxFileDialogBase::Create
f74172ab
VZ
85
86 m_filterIndex = 1;
4efd4259
DW
87} // end of wxFileDialog::wxFileDialog
88
89void wxFileDialog::GetPaths (
90 wxArrayString& rasPaths
91) const
92{
f74172ab
VZ
93 wxString sDir(m_dir);
94 size_t nCount = m_fileNames.GetCount();
4efd4259
DW
95
96 rasPaths.Empty();
9a83f860
VZ
97 if (m_dir.Last() != wxT('\\'))
98 sDir += wxT('\\');
4efd4259
DW
99
100 for ( size_t n = 0; n < nCount; n++ )
101 {
f74172ab 102 rasPaths.Add(sDir + m_fileNames[n]);
4efd4259
DW
103 }
104} // end of wxFileDialog::GetPaths
0e320a79
DW
105
106int wxFileDialog::ShowModal()
107{
643e9cf9
VS
108 WX_TESTING_SHOW_MODAL_HOOK();
109
4efd4259
DW
110 wxString sTheFilter;
111 wxString sFilterBuffer;
b93f4bb9 112 wxChar* pzFilterBuffer;
4efd4259
DW
113 static wxChar zFileNameBuffer[wxMAXPATH]; // the file-name
114 HWND hWnd = 0;
115 wxChar zTitleBuffer[wxMAXFILE + 1 + wxMAXEXT]; // the file-name, without path
b93f4bb9
DW
116 wxString sDir;
117 size_t i;
f74172ab 118 size_t nLen = m_dir.length();
b93f4bb9 119 int nCount = 0;
4efd4259
DW
120 FILEDLG vFileDlg;
121 ULONG lFlags = 0L;
122
123 memset(&vFileDlg, '\0', sizeof(FILEDLG));
f74172ab
VZ
124 if (m_parent)
125 hWnd = (HWND) m_parent->GetHWND();
4efd4259
DW
126 if (!hWnd && wxTheApp->GetTopWindow())
127 hWnd = (HWND) wxTheApp->GetTopWindow()->GetHWND();
128
129
130 *zFileNameBuffer = wxT('\0');
131 *zTitleBuffer = wxT('\0');
132
e031f1df 133 if (m_windowStyle & wxFD_SAVE)
b93f4bb9
DW
134 lFlags = FDS_SAVEAS_DIALOG;
135 else
136 lFlags = FDS_OPEN_DIALOG;
137
e031f1df 138 if (m_windowStyle & wxFD_SAVE)
4efd4259 139 lFlags |= FDS_SAVEAS_DIALOG;
e031f1df 140 if (m_windowStyle & wxFD_MULTIPLE)
4efd4259
DW
141 lFlags |= FDS_OPEN_DIALOG | FDS_MULTIPLESEL;
142
143 vFileDlg.cbSize = sizeof(FILEDLG);
144 vFileDlg.fl = lFlags;
0fba44b4 145 vFileDlg.pszTitle = (PSZ)zTitleBuffer;
4efd4259
DW
146
147 //
148 // Convert forward slashes to backslashes (file selector doesn't like
149 // forward slashes) and also squeeze multiple consecutive slashes into one
150 // as it doesn't like two backslashes in a row neither
151 //
152 sDir.reserve(nLen);
153 for ( i = 0; i < nLen; i++ )
154 {
f74172ab 155 wxChar ch = m_dir[i];
4efd4259
DW
156
157 switch (ch)
158 {
9a83f860 159 case wxT('/'):
4efd4259
DW
160 //
161 // Convert to backslash
162 //
9a83f860 163 ch = wxT('\\');
4efd4259
DW
164
165 //
166 // Fall through
167 //
9a83f860 168 case wxT('\\'):
4efd4259
DW
169 while (i < nLen - 1)
170 {
f74172ab 171 wxChar chNext = m_dir[i + 1];
4efd4259 172
9a83f860 173 if (chNext != wxT('\\') && chNext != wxT('/'))
4efd4259
DW
174 break;
175
176 //
177 // Ignore the next one, unless it is at the start of a UNC path
178 //
179 if (i > 0)
180 i++;
181 else
182 break;
183 }
184
185 //
186 // Fall through
187 //
188
189 default:
190 //
191 // Normal char
192 sDir += ch;
193 }
194 }
f74172ab 195 if ( wxStrlen(m_wildCard) == 0 )
0fba44b4 196 sTheFilter = wxEmptyString;
4efd4259 197 else
f74172ab 198 sTheFilter = m_wildCard;
4efd4259 199
6d8eb95b 200 wxStrtok(sTheFilter.wchar_str(), wxT("|"), &pzFilterBuffer);
b93f4bb9 201 while(pzFilterBuffer != NULL)
4efd4259 202 {
b93f4bb9
DW
203 if (nCount > 0 && !(nCount % 2))
204 sDir += wxT(";");
205 if (nCount % 2)
4efd4259 206 {
b93f4bb9 207 sDir += pzFilterBuffer;
4efd4259 208 }
0fba44b4 209 wxStrtok(NULL, wxT("|"), &pzFilterBuffer);
b93f4bb9 210 nCount++;
4efd4259 211 }
b93f4bb9 212 if (nCount == 0)
f74172ab 213 sDir += m_fileName;
6670f564 214 if (sDir.empty())
0fba44b4
DW
215 sDir = wxT("*.*");
216 wxStrcpy((wxChar*)vFileDlg.szFullFile, sDir);
25fc812c 217 sFilterBuffer = sDir;
4efd4259 218
b93f4bb9 219 hWnd = ::WinFileDlg( HWND_DESKTOP
f74172ab 220 ,GetHwndOf(m_parent)
4efd4259
DW
221 ,&vFileDlg
222 );
223 if (hWnd && vFileDlg.lReturn == DID_OK)
224 {
f74172ab 225 m_fileNames.Empty();
e031f1df 226 if ((m_windowStyle & wxFD_MULTIPLE ) && vFileDlg.ulFQFCount > 1)
4efd4259 227 {
9923c37d 228 for (int i = 0; i < (int)vFileDlg.ulFQFCount; i++)
4efd4259
DW
229 {
230 if (i == 0)
231 {
0fba44b4
DW
232 m_dir = wxPathOnly(wxString((const wxChar*)*vFileDlg.papszFQFilename[0]));
233 m_path = (const wxChar*)*vFileDlg.papszFQFilename[0];
4efd4259 234 }
0fba44b4 235 m_fileName = wxFileNameFromPath(wxString((const wxChar*)*vFileDlg.papszFQFilename[i]));
f74172ab 236 m_fileNames.Add(m_fileName);
4efd4259 237 }
b93f4bb9 238 ::WinFreeFileDlgList(vFileDlg.papszFQFilename);
4efd4259 239 }
e031f1df 240 else if (!(m_windowStyle & wxFD_SAVE))
4efd4259 241 {
0fba44b4
DW
242 m_path = (wxChar*)vFileDlg.szFullFile;
243 m_fileName = wxFileNameFromPath(wxString((const wxChar*)vFileDlg.szFullFile));
244 m_dir = wxPathOnly((const wxChar*)vFileDlg.szFullFile);
4efd4259
DW
245 }
246 else // save file
247 {
248 const wxChar* pzExtension = NULL;
249
0fba44b4 250 wxStrcpy(zFileNameBuffer, (const wxChar*)vFileDlg.szFullFile);
4efd4259
DW
251
252 int nIdx = wxStrlen(zFileNameBuffer) - 1;
25fc812c 253 wxString sExt;
4efd4259 254
bd365871
FM
255 wxFileName::SplitPath( zFileNameBuffer
256 ,&m_path
257 ,&m_fileName
258 ,&sExt
259 );
6670f564 260 if (zFileNameBuffer[nIdx] == wxT('.') || sExt.empty())
4efd4259
DW
261 {
262 zFileNameBuffer[nIdx] = wxT('\0');
263
264 //
265 // User has typed a filename without an extension:
266 //
267 // A filename can end in a "." here ("abc."), this means it
268 // does not have an extension. Because later on a "." with
269 // the default extension is appended we remove the "." if
270 // filename ends with one (We don't want files called
271 // "abc..ext")
272 //
273 pzExtension = sFilterBuffer.c_str();
274
9923c37d 275 for( int i = 0; i < (int)sFilterBuffer.length(); i++ )
4efd4259
DW
276 {
277 //
278 // Get extension
279 //
280 pzExtension = wxStrrchr(pzExtension, wxT('.'));
281 if ( pzExtension &&
282 !wxStrrchr(pzExtension, wxT('*')) &&
283 !wxStrrchr(pzExtension, wxT('?')) &&
284 pzExtension[1] &&
285 pzExtension[1] != wxT(' ')
286 ) // != "blabla. "
287 {
288 //
289 // Now concat extension to the fileName:
290 //
f74172ab 291 m_path = wxString(zFileNameBuffer) + pzExtension;
4efd4259
DW
292 }
293 }
294 }
295 else
296 {
0fba44b4 297 m_path = (wxChar*)vFileDlg.szFullFile;
4efd4259 298 }
0fba44b4
DW
299 m_fileName = wxFileNameFromPath((const wxChar*)vFileDlg.szFullFile);
300 m_dir = wxPathOnly((const wxChar*)vFileDlg.szFullFile);
4efd4259
DW
301
302 //
e031f1df 303 // === Simulating the wxFD_OVERWRITE_PROMPT >>============================
4efd4259 304 //
e031f1df
WS
305 if ((m_windowStyle & wxFD_OVERWRITE_PROMPT) &&
306 (m_windowStyle & wxFD_SAVE) &&
f74172ab 307 (wxFileExists(m_path.c_str())))
4efd4259
DW
308 {
309 wxString sMessageText;
310
311 sMessageText.Printf( _("File '%s' already exists.\nDo you want to replace it?")
f74172ab 312 ,m_path.c_str()
4efd4259
DW
313 );
314 if (wxMessageBox( sMessageText
315 ,wxT("Save File As")
316 ,wxYES_NO | wxICON_EXCLAMATION
317 ) != wxYES)
318 {
319 return wxID_CANCEL;
320 }
321 }
322 }
323 return wxID_OK;
324 }
0e320a79 325 return wxID_CANCEL;
4efd4259 326} // end of wxFileDialog::ShowModal
0e320a79 327
6670f564 328#endif // wxUSE_FILEDLG