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