]> git.saurik.com Git - wxWidgets.git/blob - src/msw/filedlg.cpp
Rotated text patch from Hans-Joachim Baader (with some corrections)
[wxWidgets.git] / src / msw / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: filedlg.cpp
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "filedlg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/defs.h"
26 #include "wx/utils.h"
27 #include "wx/msgdlg.h"
28 #include "wx/dialog.h"
29 #include "wx/filedlg.h"
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #endif
33
34 #include <windows.h>
35
36 #if !defined(__WIN32__) || defined(__SALFORDC__) || defined(__WXWINE__)
37 #include <commdlg.h>
38 #endif
39
40 #include "wx/msw/private.h"
41
42 #include <math.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #if !USE_SHARED_LIBRARY
47 IMPLEMENT_CLASS(wxFileDialog, wxDialog)
48 #endif
49
50 wxString wxFileSelector(const wxChar *title,
51 const wxChar *defaultDir,
52 const wxChar *defaultFileName,
53 const wxChar *defaultExtension,
54 const wxChar *filter,
55 int flags,
56 wxWindow *parent,
57 int x, int y)
58 {
59 // In the original implementation, defaultExtension is passed to the
60 // lpstrDefExt member of OPENFILENAME. This extension, if non-NULL, is
61 // appended to the filename if the user fails to type an extension. The new
62 // implementation (taken from wxFileSelectorEx) appends the extension
63 // automatically, by looking at the filter specification. In fact this
64 // should be better than the native Microsoft implementation because
65 // Windows only allows *one* default extension, whereas here we do the
66 // right thing depending on the filter the user has chosen.
67
68 // If there's a default extension specified but no filter, we create a
69 // suitable filter.
70
71 wxString filter2;
72 if ( defaultExtension && !filter )
73 filter2 = wxString(wxT("*.")) + defaultExtension;
74 else if ( filter )
75 filter2 = filter;
76
77 wxString defaultDirString;
78 if (defaultDir)
79 defaultDirString = defaultDir;
80
81 wxString defaultFilenameString;
82 if (defaultFileName)
83 defaultFilenameString = defaultFileName;
84
85 wxFileDialog fileDialog(parent, title, defaultDirString,
86 defaultFilenameString, filter2,
87 flags, wxPoint(x, y));
88 if( wxStrlen(defaultExtension) != 0 )
89 {
90 int filterFind = 1,
91 filterIndex = 0;
92
93 for( unsigned int i = 0; i < filter2.Len(); i++ )
94 {
95 if( filter2.GetChar(i) == wxT('|') )
96 {
97 // save the start index of the new filter
98 unsigned int is = i++;
99 filterIndex++;
100
101 // find the end of the filter
102 for( ; i < filter2.Len(); i++ )
103 {
104 if(filter2[i] == wxT('|'))
105 break;
106 }
107
108 if( i-is-1 > 0 && is+1 < filter2.Len() )
109 {
110 if( filter2.Mid(is+1,i-is-1).Contains(defaultExtension) )
111 // if( filter2.Mid(is+1,i-is-1) == defaultExtension )
112 {
113 filterFind = filterIndex;
114 break;
115 }
116 }
117 }
118 }
119
120 fileDialog.SetFilterIndex(filterFind);
121 }
122
123 if ( fileDialog.ShowModal() == wxID_OK )
124 {
125 wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath());
126 return wxBuffer;
127 }
128 else
129 return wxGetEmptyString();
130 }
131
132 # if __BORLANDC__
133 # include <dir.h> // for MAXPATH etc. ( Borland 3.1 )
134 # endif
135
136 # ifndef MAXPATH
137 # define MAXPATH 400
138 # endif
139
140 # ifndef MAXDRIVE
141 # define MAXDRIVE 3
142 # endif
143
144 # ifndef MAXFILE
145 # define MAXFILE 9
146 # endif
147
148 # ifndef MAXEXT
149 # define MAXEXT 5
150 # endif
151
152
153 wxString wxFileSelectorEx(const wxChar *title,
154 const wxChar *defaultDir,
155 const wxChar *defaultFileName,
156 int* defaultFilterIndex,
157 const wxChar *filter,
158 int flags,
159 wxWindow* parent,
160 int x,
161 int y)
162
163 {
164 wxFileDialog fileDialog(parent, title ? title : wxT(""), defaultDir ? defaultDir : wxT(""),
165 defaultFileName ? defaultFileName : wxT(""), filter ? filter : wxT(""), flags, wxPoint(x, y));
166
167 if ( fileDialog.ShowModal() == wxID_OK )
168 {
169 *defaultFilterIndex = fileDialog.GetFilterIndex();
170 wxStrcpy(wxBuffer, (const wxChar *)fileDialog.GetPath());
171 return wxBuffer;
172 }
173 else
174 return wxGetEmptyString();
175 }
176
177 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
178 const wxString& defaultDir, const wxString& defaultFileName, const wxString& wildCard,
179 long style, const wxPoint& pos)
180 {
181 m_message = message;
182 m_dialogStyle = style;
183 m_parent = parent;
184 m_path = wxT("");
185 m_fileName = defaultFileName;
186 m_dir = defaultDir;
187 m_wildCard = wildCard;
188 m_filterIndex = 0;
189 }
190
191 int wxFileDialog::ShowModal(void)
192 {
193 HWND hWnd = 0;
194 if (m_parent) hWnd = (HWND) m_parent->GetHWND();
195
196 static wxChar fileNameBuffer [ MAXPATH ]; // the file-name
197 wxChar titleBuffer [ MAXFILE+1+MAXEXT ]; // the file-name, without path
198
199 *fileNameBuffer = wxT('\0');
200 *titleBuffer = wxT('\0');
201
202 long msw_flags = 0;
203 if ( (m_dialogStyle & wxHIDE_READONLY) || (m_dialogStyle & wxSAVE) )
204 msw_flags |= OFN_HIDEREADONLY;
205 if ( m_dialogStyle & wxFILE_MUST_EXIST )
206 msw_flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
207
208 OPENFILENAME of;
209 memset(&of, 0, sizeof(OPENFILENAME));
210
211 of.lpstrCustomFilter = NULL; // system should not save custom filter
212 of.nMaxCustFilter = 0L;
213
214 of.nFileOffset = 0; // 0-based pointer to filname in lpstFile
215 of.nFileExtension = 0; // 0-based pointer to extension in lpstrFile
216 of.lpstrDefExt = NULL; // no default extension
217
218 of.lStructSize = sizeof(OPENFILENAME);
219 of.hwndOwner = hWnd;
220 of.lpstrTitle = WXSTRINGCAST m_message;
221
222
223 of.lpstrFileTitle = titleBuffer;
224 of.nMaxFileTitle = MAXFILE + 1 + MAXEXT; // Windows 3.0 and 3.1
225
226 // Convert forward slashes to backslashes (file selector doesn't like
227 // forward slashes)
228 size_t i = 0;
229 size_t len = m_dir.Length();
230 for (i = 0; i < len; i++)
231 if (m_dir[i] == wxT('/'))
232 m_dir[i] = wxT('\\');
233
234 of.lpstrInitialDir = m_dir.c_str();
235
236 of.Flags = msw_flags;
237
238
239 //=== Like Alejandro Sierra's wildcard modification >>===================
240 /*
241 In wxFileSelector you can put, instead of a single wild_card,
242 pairs of strings separated by '|'.
243 The first string is a description, and the
244 second is the wild card. You can put any number of pairs.
245
246 eg. "description1 (*.ex1)|*.ex1|description2 (*.ex2)|*.ex2"
247
248 If you put a single wild card, it works as before the modification.
249 */
250 //=======================================================================
251
252 wxString theFilter;
253 if ( wxStrlen(m_wildCard) == 0 )
254 theFilter = wxString(wxT("*.*"));
255 else
256 theFilter = m_wildCard ;
257 wxString filterBuffer;
258
259 if ( !wxStrchr( theFilter, wxT('|') ) ) { // only one filter ==> default text
260 filterBuffer.Printf(_("Files (%s)|%s"),
261 theFilter.c_str(), theFilter.c_str());
262 }
263 else { // more then one filter
264 filterBuffer = theFilter;
265
266 }
267
268 filterBuffer += wxT("|");
269 // Replace | with \0
270 for (i = 0; i < filterBuffer.Len(); i++ ) {
271 if ( filterBuffer.GetChar(i) == wxT('|') ) {
272 filterBuffer[i] = wxT('\0');
273 }
274 }
275
276 of.lpstrFilter = (LPTSTR)(const wxChar *)filterBuffer;
277 of.nFilterIndex = m_filterIndex + 1; // m_filterIndex is zero-based, but nFilterIndex is 1-based
278
279 //=== Setting defaultFileName >>=========================================
280
281 wxStrncpy( fileNameBuffer, (const wxChar *)m_fileName, MAXPATH-1 );
282 fileNameBuffer[ MAXPATH-1 ] = wxT('\0');
283
284 of.lpstrFile = fileNameBuffer; // holds returned filename
285 of.nMaxFile = MAXPATH;
286
287 //== Execute FileDialog >>=================================================
288
289 bool success = (m_dialogStyle & wxSAVE) ? (GetSaveFileName(&of) != 0)
290 : (GetOpenFileName(&of) != 0);
291
292 if ( success )
293 {
294 const wxChar* extension = NULL;
295
296 //=== Adding the correct extension >>=================================
297
298 m_filterIndex = wxMax((int)of.nFilterIndex - 1, 0);
299
300 if ( of.nFileExtension && fileNameBuffer[ of.nFileExtension-1] != wxT('.') )
301 { // user has typed an filename
302 // without an extension:
303
304 int maxFilter = (int)(of.nFilterIndex*2L-1L);
305 extension = filterBuffer;
306
307 for( int i = 0; i < maxFilter; i++ ) { // get extension
308 extension = extension + wxStrlen( extension ) +1;
309 }
310
311 extension = wxStrrchr( extension, wxT('.') );
312 if ( extension // != "blabla"
313 && !wxStrrchr( extension, wxT('*') ) // != "blabla.*"
314 && !wxStrrchr( extension, wxT('?') ) // != "blabla.?"
315 && extension[1] // != "blabla."
316 && extension[1] != wxT(' ') ) // != "blabla. "
317 {
318 // now concat extension to the fileName:
319 m_fileName = wxString(fileNameBuffer) + extension;
320
321 int len = wxStrlen( fileNameBuffer );
322 wxStrncpy( fileNameBuffer + len, extension, MAXPATH - len );
323 fileNameBuffer[ MAXPATH -1 ] = wxT('\0');
324 }
325 }
326
327 m_path = fileNameBuffer;
328 m_fileName = wxFileNameFromPath(fileNameBuffer);
329 m_dir = wxPathOnly(fileNameBuffer);
330
331
332 //=== Simulating the wxOVERWRITE_PROMPT >>============================
333
334 if ( (m_dialogStyle & wxOVERWRITE_PROMPT) &&
335 ::wxFileExists( fileNameBuffer ) )
336 {
337 wxString messageText;
338 messageText.Printf(_("Replace file '%s'?"), fileNameBuffer);
339
340 if ( wxMessageBox(messageText, m_message, wxYES_NO ) != wxYES )
341 {
342 success = FALSE;
343 }
344 }
345
346 }
347 else
348 {
349 // common dialog failed - why?
350 #ifdef __WXDEBUG__
351 DWORD dwErr = CommDlgExtendedError();
352 if ( dwErr != 0 )
353 {
354 // this msg is only for developers
355 wxLogError(wxT("Common dialog failed with error code %0lx."),
356 dwErr);
357 }
358 //else: it was just cancelled
359 #endif
360 }
361
362 return success ? wxID_OK : wxID_CANCEL;
363
364 }
365
366 // Generic file load/save dialog (for internal use only)
367 static
368 wxString wxDefaultFileSelector(bool load,
369 const wxChar *what,
370 const wxChar *extension,
371 const wxChar *default_name,
372 wxWindow *parent)
373 {
374 wxString prompt;
375 wxString str;
376 if (load) str = _("Load %s file");
377 else str = _("Save %s file");
378 prompt.Printf(str, what);
379
380 const wxChar *ext = extension;
381 if (*ext == wxT('.'))
382 ext++;
383
384 wxString wild;
385 wild.Printf(wxT("*.%s"), ext);
386
387 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
388 }
389
390 // Generic file load dialog
391 WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
392 const wxChar *extension,
393 const wxChar *default_name,
394 wxWindow *parent)
395 {
396 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
397 }
398
399 // Generic file save dialog
400 WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
401 const wxChar *extension,
402 const wxChar *default_name,
403 wxWindow *parent)
404 {
405 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
406 }
407