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