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