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