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