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