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