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