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