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