]> git.saurik.com Git - wxWidgets.git/blame - src/os2/filedlg.cpp
[#1577388] - fix for scaling in wxImage.
[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
3660be5d 134#if WXWIN_COMPATIBILITY_2_4
ff3e84ff 135 if (m_windowStyle & wxHIDE_READONLY)
3660be5d
WS
136 lFlags |= FDS_SAVEAS_DIALOG;
137#endif
138
e031f1df 139 if (m_windowStyle & wxFD_SAVE)
4efd4259 140 lFlags |= FDS_SAVEAS_DIALOG;
e031f1df 141 if (m_windowStyle & wxFD_MULTIPLE)
4efd4259
DW
142 lFlags |= FDS_OPEN_DIALOG | FDS_MULTIPLESEL;
143
144 vFileDlg.cbSize = sizeof(FILEDLG);
145 vFileDlg.fl = lFlags;
0fba44b4 146 vFileDlg.pszTitle = (PSZ)zTitleBuffer;
4efd4259
DW
147
148 //
149 // Convert forward slashes to backslashes (file selector doesn't like
150 // forward slashes) and also squeeze multiple consecutive slashes into one
151 // as it doesn't like two backslashes in a row neither
152 //
153 sDir.reserve(nLen);
154 for ( i = 0; i < nLen; i++ )
155 {
f74172ab 156 wxChar ch = m_dir[i];
4efd4259
DW
157
158 switch (ch)
159 {
160 case _T('/'):
161 //
162 // Convert to backslash
163 //
164 ch = _T('\\');
165
166 //
167 // Fall through
168 //
169 case _T('\\'):
170 while (i < nLen - 1)
171 {
f74172ab 172 wxChar chNext = m_dir[i + 1];
4efd4259
DW
173
174 if (chNext != _T('\\') && chNext != _T('/'))
175 break;
176
177 //
178 // Ignore the next one, unless it is at the start of a UNC path
179 //
180 if (i > 0)
181 i++;
182 else
183 break;
184 }
185
186 //
187 // Fall through
188 //
189
190 default:
191 //
192 // Normal char
193 sDir += ch;
194 }
195 }
f74172ab 196 if ( wxStrlen(m_wildCard) == 0 )
0fba44b4 197 sTheFilter = wxEmptyString;
4efd4259 198 else
f74172ab 199 sTheFilter = m_wildCard;
4efd4259 200
0fba44b4 201 wxStrtok((wxChar*)sTheFilter.c_str(), wxT("|"), &pzFilterBuffer);
b93f4bb9 202 while(pzFilterBuffer != NULL)
4efd4259 203 {
b93f4bb9
DW
204 if (nCount > 0 && !(nCount % 2))
205 sDir += wxT(";");
206 if (nCount % 2)
4efd4259 207 {
b93f4bb9 208 sDir += pzFilterBuffer;
4efd4259 209 }
0fba44b4 210 wxStrtok(NULL, wxT("|"), &pzFilterBuffer);
b93f4bb9 211 nCount++;
4efd4259 212 }
b93f4bb9 213 if (nCount == 0)
f74172ab 214 sDir += m_fileName;
6670f564 215 if (sDir.empty())
0fba44b4
DW
216 sDir = wxT("*.*");
217 wxStrcpy((wxChar*)vFileDlg.szFullFile, sDir);
25fc812c 218 sFilterBuffer = sDir;
4efd4259 219
b93f4bb9 220 hWnd = ::WinFileDlg( HWND_DESKTOP
f74172ab 221 ,GetHwndOf(m_parent)
4efd4259
DW
222 ,&vFileDlg
223 );
224 if (hWnd && vFileDlg.lReturn == DID_OK)
225 {
f74172ab 226 m_fileNames.Empty();
e031f1df 227 if ((m_windowStyle & wxFD_MULTIPLE ) && vFileDlg.ulFQFCount > 1)
4efd4259 228 {
9923c37d 229 for (int i = 0; i < (int)vFileDlg.ulFQFCount; i++)
4efd4259
DW
230 {
231 if (i == 0)
232 {
0fba44b4
DW
233 m_dir = wxPathOnly(wxString((const wxChar*)*vFileDlg.papszFQFilename[0]));
234 m_path = (const wxChar*)*vFileDlg.papszFQFilename[0];
4efd4259 235 }
0fba44b4 236 m_fileName = wxFileNameFromPath(wxString((const wxChar*)*vFileDlg.papszFQFilename[i]));
f74172ab 237 m_fileNames.Add(m_fileName);
4efd4259 238 }
b93f4bb9 239 ::WinFreeFileDlgList(vFileDlg.papszFQFilename);
4efd4259 240 }
e031f1df 241 else if (!(m_windowStyle & wxFD_SAVE))
4efd4259 242 {
0fba44b4
DW
243 m_path = (wxChar*)vFileDlg.szFullFile;
244 m_fileName = wxFileNameFromPath(wxString((const wxChar*)vFileDlg.szFullFile));
245 m_dir = wxPathOnly((const wxChar*)vFileDlg.szFullFile);
4efd4259
DW
246 }
247 else // save file
248 {
249 const wxChar* pzExtension = NULL;
250
0fba44b4 251 wxStrcpy(zFileNameBuffer, (const wxChar*)vFileDlg.szFullFile);
4efd4259
DW
252
253 int nIdx = wxStrlen(zFileNameBuffer) - 1;
25fc812c 254 wxString sExt;
4efd4259 255
25fc812c 256 wxSplitPath( zFileNameBuffer
f74172ab
VZ
257 ,&m_path
258 ,&m_fileName
25fc812c
DW
259 ,&sExt
260 );
6670f564 261 if (zFileNameBuffer[nIdx] == wxT('.') || sExt.empty())
4efd4259
DW
262 {
263 zFileNameBuffer[nIdx] = wxT('\0');
264
265 //
266 // User has typed a filename without an extension:
267 //
268 // A filename can end in a "." here ("abc."), this means it
269 // does not have an extension. Because later on a "." with
270 // the default extension is appended we remove the "." if
271 // filename ends with one (We don't want files called
272 // "abc..ext")
273 //
274 pzExtension = sFilterBuffer.c_str();
275
9923c37d 276 for( int i = 0; i < (int)sFilterBuffer.length(); i++ )
4efd4259
DW
277 {
278 //
279 // Get extension
280 //
281 pzExtension = wxStrrchr(pzExtension, wxT('.'));
282 if ( pzExtension &&
283 !wxStrrchr(pzExtension, wxT('*')) &&
284 !wxStrrchr(pzExtension, wxT('?')) &&
285 pzExtension[1] &&
286 pzExtension[1] != wxT(' ')
287 ) // != "blabla. "
288 {
289 //
290 // Now concat extension to the fileName:
291 //
f74172ab 292 m_path = wxString(zFileNameBuffer) + pzExtension;
4efd4259
DW
293 }
294 }
295 }
296 else
297 {
0fba44b4 298 m_path = (wxChar*)vFileDlg.szFullFile;
4efd4259 299 }
0fba44b4
DW
300 m_fileName = wxFileNameFromPath((const wxChar*)vFileDlg.szFullFile);
301 m_dir = wxPathOnly((const wxChar*)vFileDlg.szFullFile);
4efd4259
DW
302
303 //
e031f1df 304 // === Simulating the wxFD_OVERWRITE_PROMPT >>============================
4efd4259 305 //
e031f1df
WS
306 if ((m_windowStyle & wxFD_OVERWRITE_PROMPT) &&
307 (m_windowStyle & wxFD_SAVE) &&
f74172ab 308 (wxFileExists(m_path.c_str())))
4efd4259
DW
309 {
310 wxString sMessageText;
311
312 sMessageText.Printf( _("File '%s' already exists.\nDo you want to replace it?")
f74172ab 313 ,m_path.c_str()
4efd4259
DW
314 );
315 if (wxMessageBox( sMessageText
316 ,wxT("Save File As")
317 ,wxYES_NO | wxICON_EXCLAMATION
318 ) != wxYES)
319 {
320 return wxID_CANCEL;
321 }
322 }
323 }
324 return wxID_OK;
325 }
0e320a79 326 return wxID_CANCEL;
4efd4259 327} // end of wxFileDialog::ShowModal
0e320a79 328
6670f564 329#endif // wxUSE_FILEDLG