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