]> git.saurik.com Git - wxWidgets.git/blame - src/common/fldlgcmn.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / src / common / fldlgcmn.cpp
CommitLineData
b600ed13 1/////////////////////////////////////////////////////////////////////////////
949c9f74 2// Name: src/common/fldlgcmn.cpp
b600ed13
VZ
3// Purpose: wxFileDialog common functions
4// Author: John Labenski
5// Modified by:
6// Created: 14.06.03 (extracted from src/*/filedlg.cpp)
b600ed13 7// Copyright: (c) Robert Roebling
65571936 8// Licence: wxWindows licence
b600ed13
VZ
9/////////////////////////////////////////////////////////////////////////////
10
b600ed13
VZ
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
b4715d08
VZ
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
949c9f74
WS
18#if wxUSE_FILEDLG
19
20#include "wx/filedlg.h"
cf6982fa 21#include "wx/dirdlg.h"
7430a4bb 22#include "wx/filename.h"
949c9f74 23
b600ed13
VZ
24#ifndef WX_PRECOMP
25 #include "wx/string.h"
26 #include "wx/intl.h"
27 #include "wx/window.h"
28#endif // WX_PRECOMP
29
f313deaa
PC
30extern WXDLLEXPORT_DATA(const char) wxFileDialogNameStr[] = "filedlg";
31extern WXDLLEXPORT_DATA(const char) wxFileSelectorPromptStr[] = "Select a file";
32extern WXDLLEXPORT_DATA(const char) wxFileSelectorDefaultWildcardStr[] =
33#if defined(__WXMSW__) || defined(__OS2__)
34 "*.*"
35#else // Unix/Mac
36 "*"
37#endif
38 ;
39
f74172ab
VZ
40//----------------------------------------------------------------------------
41// wxFileDialogBase
42//----------------------------------------------------------------------------
43
44IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase, wxDialog)
45
fe6cf128 46void wxFileDialogBase::Init()
7448de8d 47{
8ce68f7f 48 m_filterIndex = 0;
ff3e84ff 49 m_windowStyle = 0;
8ce68f7f
VZ
50 m_extraControl = NULL;
51 m_extraControlCreator = NULL;
fe6cf128
VZ
52}
53
54bool wxFileDialogBase::Create(wxWindow *parent,
55 const wxString& message,
56 const wxString& defaultDir,
57 const wxString& defaultFile,
58 const wxString& wildCard,
59 long style,
ff3e84ff
VZ
60 const wxPoint& WXUNUSED(pos),
61 const wxSize& WXUNUSED(sz),
62 const wxString& WXUNUSED(name))
f74172ab 63{
fe6cf128
VZ
64 m_message = message;
65 m_dir = defaultDir;
66 m_fileName = defaultFile;
67 m_wildCard = wildCard;
68
f74172ab 69 m_parent = parent;
45f4109c 70 m_windowStyle = style;
f74172ab
VZ
71 m_filterIndex = 0;
72
b014db05 73 if (!HasFdFlag(wxFD_OPEN) && !HasFdFlag(wxFD_SAVE))
45f4109c 74 m_windowStyle |= wxFD_OPEN; // wxFD_OPEN is the default
556151f5 75
96aed0cd 76 // check that the styles are not contradictory
b014db05 77 wxASSERT_MSG( !(HasFdFlag(wxFD_SAVE) && HasFdFlag(wxFD_OPEN)),
9a83f860 78 wxT("can't specify both wxFD_SAVE and wxFD_OPEN at once") );
96aed0cd 79
b014db05
RR
80 wxASSERT_MSG( !HasFdFlag(wxFD_SAVE) ||
81 (!HasFdFlag(wxFD_MULTIPLE) && !HasFdFlag(wxFD_FILE_MUST_EXIST)),
9a83f860 82 wxT("wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST can't be used with wxFD_SAVE" ) );
96aed0cd 83
b014db05 84 wxASSERT_MSG( !HasFdFlag(wxFD_OPEN) || !HasFdFlag(wxFD_OVERWRITE_PROMPT),
9a83f860 85 wxT("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
ff3e84ff 86
d59eea26 87 if ( wildCard.empty() || wildCard == wxFileSelectorDefaultWildcardStr )
f74172ab 88 {
186545a4
VZ
89 m_wildCard = wxString::Format(_("All files (%s)|%s"),
90 wxFileSelectorDefaultWildcardStr,
91 wxFileSelectorDefaultWildcardStr);
92 }
93 else // have wild card
94 {
95 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
96 if ( m_wildCard.Find(wxT('|')) == wxNOT_FOUND )
97 {
9a83f860 98 wxString::size_type nDot = m_wildCard.find(wxT("*."));
186545a4
VZ
99 if ( nDot != wxString::npos )
100 nDot++;
101 else
102 nDot = 0;
103
104 m_wildCard = wxString::Format
105 (
106 _("%s files (%s)|%s"),
d59eea26
VZ
107 wildCard.c_str() + nDot,
108 wildCard.c_str(),
109 wildCard.c_str()
186545a4
VZ
110 );
111 }
f74172ab 112 }
fe6cf128
VZ
113
114 return true;
f74172ab
VZ
115}
116
cc197ed4
VZ
117#if WXWIN_COMPATIBILITY_2_6
118long wxFileDialogBase::GetStyle() const
119{
120 return GetWindowStyle();
121}
122
123void wxFileDialogBase::SetStyle(long style)
124{
125 SetWindowStyle(style);
126}
127#endif // WXWIN_COMPATIBILITY_2_6
128
129
f74172ab
VZ
130wxString wxFileDialogBase::AppendExtension(const wxString &filePath,
131 const wxString &extensionList)
132{
133 // strip off path, to avoid problems with "path.bar/foo"
134 wxString fileName = filePath.AfterLast(wxFILE_SEP_PATH);
135
136 // if fileName is of form "foo.bar" it's ok, return it
a62848fd 137 int idx_dot = fileName.Find(wxT('.'), true);
949c9f74 138 if ((idx_dot != wxNOT_FOUND) && (idx_dot < (int)fileName.length() - 1))
f74172ab
VZ
139 return filePath;
140
141 // get the first extension from extensionList, or all of it
142 wxString ext = extensionList.BeforeFirst(wxT(';'));
143
144 // if ext == "foo" or "foo." there's no extension
a62848fd 145 int idx_ext_dot = ext.Find(wxT('.'), true);
949c9f74 146 if ((idx_ext_dot == wxNOT_FOUND) || (idx_ext_dot == (int)ext.length() - 1))
f74172ab
VZ
147 return filePath;
148 else
149 ext = ext.AfterLast(wxT('.'));
150
151 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
152 if ((ext.Find(wxT('*')) != wxNOT_FOUND) ||
153 (ext.Find(wxT('?')) != wxNOT_FOUND) ||
b494c48b 154 (ext.Strip(wxString::both).empty()))
f74172ab
VZ
155 return filePath;
156
157 // if fileName doesn't have a '.' then add one
158 if (filePath.Last() != wxT('.'))
159 ext = wxT(".") + ext;
160
161 return filePath + ext;
162}
163
d6dae1b4 164bool wxFileDialogBase::SetExtraControlCreator(ExtraControlCreatorFunction creator)
8ce68f7f
VZ
165{
166 wxCHECK_MSG( !m_extraControlCreator, false,
167 "wxFileDialog::SetExtraControl() called second time" );
168
d6dae1b4 169 m_extraControlCreator = creator;
8ce68f7f
VZ
170 return SupportsExtraControl();
171}
172
173bool wxFileDialogBase::CreateExtraControl()
174{
175 if (!m_extraControlCreator || m_extraControl)
176 return false;
177 m_extraControl = (*m_extraControlCreator)(this);
178 return true;
179}
180
6fa6d659
VZ
181wxSize wxFileDialogBase::GetExtraControlSize()
182{
183 if ( !m_extraControlCreator )
184 return wxDefaultSize;
185
186 // create the extra control in an empty dialog just to find its size: this
187 // is not terribly efficient but we do need to know the size before
188 // creating the native dialog and this seems to be the only way
189 wxDialog dlg(NULL, wxID_ANY, "");
190 return (*m_extraControlCreator)(&dlg)->GetSize();
191}
192
7430a4bb
VZ
193void wxFileDialogBase::SetPath(const wxString& path)
194{
195 wxString ext;
196 wxFileName::SplitPath(path, &m_dir, &m_fileName, &ext);
197 if ( !ext.empty() )
490e22fd 198 m_fileName << wxT('.') << ext;
7430a4bb
VZ
199 m_path = path;
200}
201
202void wxFileDialogBase::SetDirectory(const wxString& dir)
203{
204 m_dir = dir;
205 m_path = wxFileName(m_dir, m_fileName).GetFullPath();
206}
207
208void wxFileDialogBase::SetFilename(const wxString& name)
209{
210 m_fileName = name;
211 m_path = wxFileName(m_dir, m_fileName).GetFullPath();
212}
213
f74172ab
VZ
214//----------------------------------------------------------------------------
215// wxFileDialog convenience functions
216//----------------------------------------------------------------------------
217
6dc2e823
VS
218wxString wxFileSelector(const wxString& title,
219 const wxString& defaultDir,
220 const wxString& defaultFileName,
221 const wxString& defaultExtension,
222 const wxString& filter,
223 int flags,
224 wxWindow *parent,
225 int x, int y)
b600ed13 226{
f8bcb37d 227 // The defaultExtension, if non-empty, is
b600ed13
VZ
228 // appended to the filename if the user fails to type an extension. The new
229 // implementation (taken from wxFileSelectorEx) appends the extension
230 // automatically, by looking at the filter specification. In fact this
231 // should be better than the native Microsoft implementation because
232 // Windows only allows *one* default extension, whereas here we do the
233 // right thing depending on the filter the user has chosen.
234
235 // If there's a default extension specified but no filter, we create a
236 // suitable filter.
237
238 wxString filter2;
f8bcb37d 239 if ( !defaultExtension.empty() && filter.empty() )
b600ed13 240 filter2 = wxString(wxT("*.")) + defaultExtension;
f8bcb37d 241 else if ( !filter.empty() )
b600ed13
VZ
242 filter2 = filter;
243
f8bcb37d
VS
244 wxFileDialog fileDialog(parent, title, defaultDir,
245 defaultFileName, filter2,
b600ed13 246 flags, wxPoint(x, y));
b600ed13 247
f8bcb37d
VS
248 // if filter is of form "All files (*)|*|..." set correct filter index
249 if ( !defaultExtension.empty() && filter2.find(wxT('|')) != wxString::npos )
7448de8d 250 {
f74172ab 251 int filterIndex = 0;
b600ed13 252
f74172ab
VZ
253 wxArrayString descriptions, filters;
254 // don't care about errors, handled already by wxFileDialog
daf32463 255 (void)wxParseCommonDialogsFilter(filter2, descriptions, filters);
f74172ab 256 for (size_t n=0; n<filters.GetCount(); n++)
7448de8d 257 {
f74172ab 258 if (filters[n].Contains(defaultExtension))
7448de8d 259 {
f74172ab 260 filterIndex = n;
f8bcb37d 261 break;
7448de8d
WS
262 }
263 }
b600ed13 264
f74172ab
VZ
265 if (filterIndex > 0)
266 fileDialog.SetFilterIndex(filterIndex);
7448de8d 267 }
b600ed13 268
b600ed13
VZ
269 wxString filename;
270 if ( fileDialog.ShowModal() == wxID_OK )
271 {
272 filename = fileDialog.GetPath();
273 }
274
275 return filename;
276}
277
f74172ab
VZ
278//----------------------------------------------------------------------------
279// wxFileSelectorEx
280//----------------------------------------------------------------------------
b600ed13 281
6dc2e823
VS
282wxString wxFileSelectorEx(const wxString& title,
283 const wxString& defaultDir,
284 const wxString& defaultFileName,
285 int* defaultFilterIndex,
286 const wxString& filter,
287 int flags,
288 wxWindow* parent,
289 int x,
290 int y)
b600ed13
VZ
291
292{
293 wxFileDialog fileDialog(parent,
f8bcb37d
VS
294 title,
295 defaultDir,
296 defaultFileName,
297 filter,
b600ed13
VZ
298 flags, wxPoint(x, y));
299
300 wxString filename;
301 if ( fileDialog.ShowModal() == wxID_OK )
302 {
303 if ( defaultFilterIndex )
304 *defaultFilterIndex = fileDialog.GetFilterIndex();
305
306 filename = fileDialog.GetPath();
307 }
308
309 return filename;
310}
311
f74172ab
VZ
312//----------------------------------------------------------------------------
313// wxDefaultFileSelector - Generic load/save dialog (for internal use only)
314//----------------------------------------------------------------------------
b600ed13 315
b600ed13 316static wxString wxDefaultFileSelector(bool load,
f8bcb37d
VS
317 const wxString& what,
318 const wxString& extension,
319 const wxString& default_name,
b600ed13
VZ
320 wxWindow *parent)
321{
322 wxString prompt;
323 wxString str;
324 if (load)
325 str = _("Load %s file");
326 else
327 str = _("Save %s file");
328 prompt.Printf(str, what);
329
330 wxString wild;
f8bcb37d
VS
331 wxString ext;
332 if ( !extension.empty() )
b600ed13 333 {
9a83f860 334 if ( extension[0u] == wxT('.') )
f8bcb37d
VS
335 ext = extension.substr(1);
336 else
337 ext = extension;
b600ed13
VZ
338
339 wild.Printf(wxT("*.%s"), ext);
340 }
341 else // no extension specified
342 {
343 wild = wxFileSelectorDefaultWildcardStr;
344 }
345
f8bcb37d 346 return wxFileSelector(prompt, wxEmptyString, default_name, ext, wild,
f2a18fbe
VZ
347 load ? (wxFD_OPEN | wxFD_FILE_MUST_EXIST) : wxFD_SAVE,
348 parent);
b600ed13
VZ
349}
350
f74172ab
VZ
351//----------------------------------------------------------------------------
352// wxLoadFileSelector
353//----------------------------------------------------------------------------
354
6dc2e823
VS
355WXDLLEXPORT wxString wxLoadFileSelector(const wxString& what,
356 const wxString& extension,
357 const wxString& default_name,
358 wxWindow *parent)
b600ed13 359{
a62848fd 360 return wxDefaultFileSelector(true, what, extension, default_name, parent);
b600ed13
VZ
361}
362
f74172ab
VZ
363//----------------------------------------------------------------------------
364// wxSaveFileSelector
365//----------------------------------------------------------------------------
366
6dc2e823
VS
367WXDLLEXPORT wxString wxSaveFileSelector(const wxString& what,
368 const wxString& extension,
369 const wxString& default_name,
370 wxWindow *parent)
b600ed13 371{
a62848fd 372 return wxDefaultFileSelector(false, what, extension, default_name, parent);
b600ed13
VZ
373}
374
cc197ed4
VZ
375
376//----------------------------------------------------------------------------
377// wxDirDialogBase
378//----------------------------------------------------------------------------
379
380#if WXWIN_COMPATIBILITY_2_6
381long wxDirDialogBase::GetStyle() const
382{
383 return GetWindowStyle();
384}
385
386void wxDirDialogBase::SetStyle(long style)
387{
388 SetWindowStyle(style);
389}
390#endif // WXWIN_COMPATIBILITY_2_6
391
392
b600ed13 393#endif // wxUSE_FILEDLG