s/wxSplitPath/wxFileName::SplitPath
[wxWidgets.git] / src / os2 / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/05/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
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 #if wxUSE_FILEDLG
20
21 #include "wx/filedlg.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/utils.h"
25 #include "wx/msgdlg.h"
26 #include "wx/intl.h"
27 #include "wx/log.h"
28 #include "wx/app.h"
29 #include "wx/math.h"
30 #endif
31
32 #define INCL_PM
33 #include <os2.h>
34
35 #include "wx/os2/private.h"
36
37 #include <stdlib.h>
38 #include <string.h>
39
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
61
62 IMPLEMENT_CLASS(wxFileDialog, wxFileDialogBase)
63
64 // ----------------------------------------------------------------------------
65 // CLASS wxFileDialog
66 // ----------------------------------------------------------------------------
67
68 wxFileDialog::wxFileDialog (
69 wxWindow* pParent
70 , const wxString& rsMessage
71 , const wxString& rsDefaultDir
72 , const wxString& rsDefaultFileName
73 , const wxString& rsWildCard
74 , long lStyle
75 , const wxPoint& rPos,
76 const wxSize& sz,
77 const wxString& name
78 )
79 :wxFileDialogBase(pParent, rsMessage, rsDefaultDir, rsDefaultFileName, rsWildCard, lStyle, rPos, sz, name)
80
81 {
82 // NB: all style checks are done by wxFileDialogBase::Create
83
84 m_filterIndex = 1;
85 } // end of wxFileDialog::wxFileDialog
86
87 void wxFileDialog::GetPaths (
88 wxArrayString& rasPaths
89 ) const
90 {
91 wxString sDir(m_dir);
92 size_t nCount = m_fileNames.GetCount();
93
94 rasPaths.Empty();
95 if (m_dir.Last() != _T('\\'))
96 sDir += _T('\\');
97
98 for ( size_t n = 0; n < nCount; n++ )
99 {
100 rasPaths.Add(sDir + m_fileNames[n]);
101 }
102 } // end of wxFileDialog::GetPaths
103
104 int wxFileDialog::ShowModal()
105 {
106 wxString sTheFilter;
107 wxString sFilterBuffer;
108 wxChar* pzFilterBuffer;
109 static wxChar zFileNameBuffer[wxMAXPATH]; // the file-name
110 HWND hWnd = 0;
111 wxChar zTitleBuffer[wxMAXFILE + 1 + wxMAXEXT]; // the file-name, without path
112 wxString sDir;
113 size_t i;
114 size_t nLen = m_dir.length();
115 int nCount = 0;
116 FILEDLG vFileDlg;
117 ULONG lFlags = 0L;
118
119 memset(&vFileDlg, '\0', sizeof(FILEDLG));
120 if (m_parent)
121 hWnd = (HWND) m_parent->GetHWND();
122 if (!hWnd && wxTheApp->GetTopWindow())
123 hWnd = (HWND) wxTheApp->GetTopWindow()->GetHWND();
124
125
126 *zFileNameBuffer = wxT('\0');
127 *zTitleBuffer = wxT('\0');
128
129 if (m_windowStyle & wxFD_SAVE)
130 lFlags = FDS_SAVEAS_DIALOG;
131 else
132 lFlags = FDS_OPEN_DIALOG;
133
134 if (m_windowStyle & wxFD_SAVE)
135 lFlags |= FDS_SAVEAS_DIALOG;
136 if (m_windowStyle & wxFD_MULTIPLE)
137 lFlags |= FDS_OPEN_DIALOG | FDS_MULTIPLESEL;
138
139 vFileDlg.cbSize = sizeof(FILEDLG);
140 vFileDlg.fl = lFlags;
141 vFileDlg.pszTitle = (PSZ)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 {
151 wxChar ch = m_dir[i];
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 {
167 wxChar chNext = m_dir[i + 1];
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 }
191 if ( wxStrlen(m_wildCard) == 0 )
192 sTheFilter = wxEmptyString;
193 else
194 sTheFilter = m_wildCard;
195
196 wxStrtok(sTheFilter.wchar_str(), wxT("|"), &pzFilterBuffer);
197 while(pzFilterBuffer != NULL)
198 {
199 if (nCount > 0 && !(nCount % 2))
200 sDir += wxT(";");
201 if (nCount % 2)
202 {
203 sDir += pzFilterBuffer;
204 }
205 wxStrtok(NULL, wxT("|"), &pzFilterBuffer);
206 nCount++;
207 }
208 if (nCount == 0)
209 sDir += m_fileName;
210 if (sDir.empty())
211 sDir = wxT("*.*");
212 wxStrcpy((wxChar*)vFileDlg.szFullFile, sDir);
213 sFilterBuffer = sDir;
214
215 hWnd = ::WinFileDlg( HWND_DESKTOP
216 ,GetHwndOf(m_parent)
217 ,&vFileDlg
218 );
219 if (hWnd && vFileDlg.lReturn == DID_OK)
220 {
221 m_fileNames.Empty();
222 if ((m_windowStyle & wxFD_MULTIPLE ) && vFileDlg.ulFQFCount > 1)
223 {
224 for (int i = 0; i < (int)vFileDlg.ulFQFCount; i++)
225 {
226 if (i == 0)
227 {
228 m_dir = wxPathOnly(wxString((const wxChar*)*vFileDlg.papszFQFilename[0]));
229 m_path = (const wxChar*)*vFileDlg.papszFQFilename[0];
230 }
231 m_fileName = wxFileNameFromPath(wxString((const wxChar*)*vFileDlg.papszFQFilename[i]));
232 m_fileNames.Add(m_fileName);
233 }
234 ::WinFreeFileDlgList(vFileDlg.papszFQFilename);
235 }
236 else if (!(m_windowStyle & wxFD_SAVE))
237 {
238 m_path = (wxChar*)vFileDlg.szFullFile;
239 m_fileName = wxFileNameFromPath(wxString((const wxChar*)vFileDlg.szFullFile));
240 m_dir = wxPathOnly((const wxChar*)vFileDlg.szFullFile);
241 }
242 else // save file
243 {
244 const wxChar* pzExtension = NULL;
245
246 wxStrcpy(zFileNameBuffer, (const wxChar*)vFileDlg.szFullFile);
247
248 int nIdx = wxStrlen(zFileNameBuffer) - 1;
249 wxString sExt;
250
251 wxFileName::SplitPath( zFileNameBuffer
252 ,&m_path
253 ,&m_fileName
254 ,&sExt
255 );
256 if (zFileNameBuffer[nIdx] == wxT('.') || sExt.empty())
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
271 for( int i = 0; i < (int)sFilterBuffer.length(); i++ )
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 //
287 m_path = wxString(zFileNameBuffer) + pzExtension;
288 }
289 }
290 }
291 else
292 {
293 m_path = (wxChar*)vFileDlg.szFullFile;
294 }
295 m_fileName = wxFileNameFromPath((const wxChar*)vFileDlg.szFullFile);
296 m_dir = wxPathOnly((const wxChar*)vFileDlg.szFullFile);
297
298 //
299 // === Simulating the wxFD_OVERWRITE_PROMPT >>============================
300 //
301 if ((m_windowStyle & wxFD_OVERWRITE_PROMPT) &&
302 (m_windowStyle & wxFD_SAVE) &&
303 (wxFileExists(m_path.c_str())))
304 {
305 wxString sMessageText;
306
307 sMessageText.Printf( _("File '%s' already exists.\nDo you want to replace it?")
308 ,m_path.c_str()
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 }
321 return wxID_CANCEL;
322 } // end of wxFileDialog::ShowModal
323
324 #endif // wxUSE_FILEDLG