]> git.saurik.com Git - wxWidgets.git/blob - src/os2/filedlg.cpp
f4fc9da6f11269bc69734045a1ca7ed63b9fb494
[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 #include "wx/filedlg.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/utils.h"
25 #include "wx/msgdlg.h"
26 #include "wx/filename.h"
27 #include "wx/intl.h"
28 #include "wx/log.h"
29 #include "wx/app.h"
30 #include "wx/math.h"
31 #endif
32
33 #define INCL_PM
34 #include <os2.h>
35
36 #include "wx/os2/private.h"
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "wx/tokenzr.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 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_dir.length();
116 int nCount = 0;
117 FILEDLG vFileDlg;
118 ULONG lFlags = 0L;
119
120 memset(&vFileDlg, '\0', sizeof(FILEDLG));
121 if (m_parent)
122 hWnd = (HWND) m_parent->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_windowStyle & wxFD_SAVE)
131 lFlags = FDS_SAVEAS_DIALOG;
132 else
133 lFlags = FDS_OPEN_DIALOG;
134
135 if (m_windowStyle & wxFD_SAVE)
136 lFlags |= FDS_SAVEAS_DIALOG;
137 if (m_windowStyle & wxFD_MULTIPLE)
138 lFlags |= FDS_OPEN_DIALOG | FDS_MULTIPLESEL;
139
140 vFileDlg.cbSize = sizeof(FILEDLG);
141 vFileDlg.fl = lFlags;
142 vFileDlg.pszTitle = (PSZ)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_dir[i];
153
154 switch (ch)
155 {
156 case wxT('/'):
157 //
158 // Convert to backslash
159 //
160 ch = wxT('\\');
161
162 //
163 // Fall through
164 //
165 case wxT('\\'):
166 while (i < nLen - 1)
167 {
168 wxChar chNext = m_dir[i + 1];
169
170 if (chNext != wxT('\\') && chNext != wxT('/'))
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_wildCard) == 0 )
193 sTheFilter = wxEmptyString;
194 else
195 sTheFilter = m_wildCard;
196
197 wxStrtok(sTheFilter.wchar_str(), wxT("|"), &pzFilterBuffer);
198 while(pzFilterBuffer != NULL)
199 {
200 if (nCount > 0 && !(nCount % 2))
201 sDir += wxT(";");
202 if (nCount % 2)
203 {
204 sDir += pzFilterBuffer;
205 }
206 wxStrtok(NULL, wxT("|"), &pzFilterBuffer);
207 nCount++;
208 }
209 if (nCount == 0)
210 sDir += m_fileName;
211 if (sDir.empty())
212 sDir = wxT("*.*");
213 wxStrcpy((wxChar*)vFileDlg.szFullFile, sDir);
214 sFilterBuffer = sDir;
215
216 hWnd = ::WinFileDlg( HWND_DESKTOP
217 ,GetHwndOf(m_parent)
218 ,&vFileDlg
219 );
220 if (hWnd && vFileDlg.lReturn == DID_OK)
221 {
222 m_fileNames.Empty();
223 if ((m_windowStyle & wxFD_MULTIPLE ) && vFileDlg.ulFQFCount > 1)
224 {
225 for (int i = 0; i < (int)vFileDlg.ulFQFCount; i++)
226 {
227 if (i == 0)
228 {
229 m_dir = wxPathOnly(wxString((const wxChar*)*vFileDlg.papszFQFilename[0]));
230 m_path = (const wxChar*)*vFileDlg.papszFQFilename[0];
231 }
232 m_fileName = wxFileNameFromPath(wxString((const wxChar*)*vFileDlg.papszFQFilename[i]));
233 m_fileNames.Add(m_fileName);
234 }
235 ::WinFreeFileDlgList(vFileDlg.papszFQFilename);
236 }
237 else if (!(m_windowStyle & wxFD_SAVE))
238 {
239 m_path = (wxChar*)vFileDlg.szFullFile;
240 m_fileName = wxFileNameFromPath(wxString((const wxChar*)vFileDlg.szFullFile));
241 m_dir = wxPathOnly((const wxChar*)vFileDlg.szFullFile);
242 }
243 else // save file
244 {
245 const wxChar* pzExtension = NULL;
246
247 wxStrcpy(zFileNameBuffer, (const wxChar*)vFileDlg.szFullFile);
248
249 int nIdx = wxStrlen(zFileNameBuffer) - 1;
250 wxString sExt;
251
252 wxFileName::SplitPath( zFileNameBuffer
253 ,&m_path
254 ,&m_fileName
255 ,&sExt
256 );
257 if (zFileNameBuffer[nIdx] == wxT('.') || sExt.empty())
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 < (int)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_path = wxString(zFileNameBuffer) + pzExtension;
289 }
290 }
291 }
292 else
293 {
294 m_path = (wxChar*)vFileDlg.szFullFile;
295 }
296 m_fileName = wxFileNameFromPath((const wxChar*)vFileDlg.szFullFile);
297 m_dir = wxPathOnly((const wxChar*)vFileDlg.szFullFile);
298
299 //
300 // === Simulating the wxFD_OVERWRITE_PROMPT >>============================
301 //
302 if ((m_windowStyle & wxFD_OVERWRITE_PROMPT) &&
303 (m_windowStyle & wxFD_SAVE) &&
304 (wxFileExists(m_path.c_str())))
305 {
306 wxString sMessageText;
307
308 sMessageText.Printf( _("File '%s' already exists.\nDo you want to replace it?")
309 ,m_path.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
325 #endif // wxUSE_FILEDLG