wx/math.h integration
[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 #include "wx/math.h"
27 #endif
28
29 #define INCL_PM
30 #include <os2.h>
31
32 #include "wx/os2/private.h"
33
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 WXWIN_COMPATIBILITY_2_4
130 if (m_dialogStyle & wxHIDE_READONLY)
131 lFlags |= FDS_SAVEAS_DIALOG;
132 #endif
133
134 if (m_dialogStyle & wxSAVE)
135 lFlags |= FDS_SAVEAS_DIALOG;
136 if (m_dialogStyle & wxMULTIPLE )
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 {
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 = "";
193 else
194 sTheFilter = m_wildCard;
195
196 pzFilterBuffer = strtok((char*)sTheFilter.c_str(), "|");
197 while(pzFilterBuffer != NULL)
198 {
199 if (nCount > 0 && !(nCount % 2))
200 sDir += wxT(";");
201 if (nCount % 2)
202 {
203 sDir += pzFilterBuffer;
204 }
205 pzFilterBuffer = strtok(NULL, "|");
206 nCount++;
207 }
208 if (nCount == 0)
209 sDir += m_fileName;
210 if (sDir.IsEmpty())
211 sDir = "*.*";
212 wxStrcpy(vFileDlg.szFullFile, sDir.c_str());
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_dialogStyle & wxMULTIPLE ) && 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 char*)*vFileDlg.papszFQFilename[0]));
229 m_path = (const char*)*vFileDlg.papszFQFilename[0];
230 }
231 m_fileName = wxFileNameFromPath(wxString((const char*)*vFileDlg.papszFQFilename[i]));
232 m_fileNames.Add(m_fileName);
233 }
234 ::WinFreeFileDlgList(vFileDlg.papszFQFilename);
235 }
236 else if (!(m_dialogStyle & wxSAVE))
237 {
238 m_path = vFileDlg.szFullFile;
239 m_fileName = wxFileNameFromPath(vFileDlg.szFullFile);
240 m_dir = wxPathOnly(vFileDlg.szFullFile);
241 }
242 else // save file
243 {
244 const wxChar* pzExtension = NULL;
245
246 wxStrcpy(zFileNameBuffer, vFileDlg.szFullFile);
247
248 int nIdx = wxStrlen(zFileNameBuffer) - 1;
249 wxString sExt;
250
251 wxSplitPath( zFileNameBuffer
252 ,&m_path
253 ,&m_fileName
254 ,&sExt
255 );
256 if (zFileNameBuffer[nIdx] == wxT('.') || sExt.IsEmpty())
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 = vFileDlg.szFullFile;
294 }
295 m_fileName = wxFileNameFromPath(vFileDlg.szFullFile);
296 m_dir = wxPathOnly(vFileDlg.szFullFile);
297
298 //
299 // === Simulating the wxOVERWRITE_PROMPT >>============================
300 //
301 if ((m_dialogStyle & wxOVERWRITE_PROMPT) &&
302 (m_dialogStyle & wxSAVE) &&
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