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