]> git.saurik.com Git - wxWidgets.git/blame - src/os2/filedlg.cpp
implemented wxSS::GetMetric on wxGTK2 (patch 950618)
[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
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
3660be5d
WS
129#if WXWIN_COMPATIBILITY_2_4
130 if (m_dialogStyle & wxHIDE_READONLY)
131 lFlags |= FDS_SAVEAS_DIALOG;
132#endif
133
134 if (m_dialogStyle & wxSAVE)
4efd4259 135 lFlags |= FDS_SAVEAS_DIALOG;
f74172ab 136 if (m_dialogStyle & wxMULTIPLE )
4efd4259
DW
137 lFlags |= FDS_OPEN_DIALOG | FDS_MULTIPLESEL;
138
139 vFileDlg.cbSize = sizeof(FILEDLG);
140 vFileDlg.fl = lFlags;
141 vFileDlg.pszTitle = zTitleBuffer;
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 )
4efd4259
DW
192 sTheFilter = "";
193 else
f74172ab 194 sTheFilter = m_wildCard;
4efd4259 195
b93f4bb9
DW
196 pzFilterBuffer = strtok((char*)sTheFilter.c_str(), "|");
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 }
b93f4bb9
DW
205 pzFilterBuffer = strtok(NULL, "|");
206 nCount++;
4efd4259 207 }
b93f4bb9 208 if (nCount == 0)
f74172ab 209 sDir += m_fileName;
b93f4bb9
DW
210 if (sDir.IsEmpty())
211 sDir = "*.*";
4efd4259 212 wxStrcpy(vFileDlg.szFullFile, sDir.c_str());
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
VZ
221 m_fileNames.Empty();
222 if ((m_dialogStyle & wxMULTIPLE ) && vFileDlg.ulFQFCount > 1)
4efd4259 223 {
9923c37d 224 for (int i = 0; i < (int)vFileDlg.ulFQFCount; i++)
4efd4259
DW
225 {
226 if (i == 0)
227 {
f74172ab
VZ
228 m_dir = wxPathOnly(wxString((const char*)*vFileDlg.papszFQFilename[0]));
229 m_path = (const char*)*vFileDlg.papszFQFilename[0];
4efd4259 230 }
f74172ab
VZ
231 m_fileName = wxFileNameFromPath(wxString((const char*)*vFileDlg.papszFQFilename[i]));
232 m_fileNames.Add(m_fileName);
4efd4259 233 }
b93f4bb9 234 ::WinFreeFileDlgList(vFileDlg.papszFQFilename);
4efd4259 235 }
f74172ab 236 else if (!(m_dialogStyle & wxSAVE))
4efd4259 237 {
f74172ab
VZ
238 m_path = vFileDlg.szFullFile;
239 m_fileName = wxFileNameFromPath(vFileDlg.szFullFile);
240 m_dir = wxPathOnly(vFileDlg.szFullFile);
4efd4259
DW
241 }
242 else // save file
243 {
244 const wxChar* pzExtension = NULL;
245
246 wxStrcpy(zFileNameBuffer, vFileDlg.szFullFile);
247
248 int nIdx = wxStrlen(zFileNameBuffer) - 1;
25fc812c 249 wxString sExt;
4efd4259 250
25fc812c 251 wxSplitPath( zFileNameBuffer
f74172ab
VZ
252 ,&m_path
253 ,&m_fileName
25fc812c
DW
254 ,&sExt
255 );
256 if (zFileNameBuffer[nIdx] == wxT('.') || sExt.IsEmpty())
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 {
f74172ab 293 m_path = vFileDlg.szFullFile;
4efd4259 294 }
f74172ab
VZ
295 m_fileName = wxFileNameFromPath(vFileDlg.szFullFile);
296 m_dir = wxPathOnly(vFileDlg.szFullFile);
4efd4259
DW
297
298 //
299 // === Simulating the wxOVERWRITE_PROMPT >>============================
300 //
f74172ab
VZ
301 if ((m_dialogStyle & wxOVERWRITE_PROMPT) &&
302 (m_dialogStyle & wxSAVE) &&
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