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