]> git.saurik.com Git - wxWidgets.git/blame - src/common/fldlgcmn.cpp
Include wx/listbox.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / common / fldlgcmn.cpp
CommitLineData
b600ed13
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: common/fldlgcmn.cpp
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
19#ifndef WX_PRECOMP
20 #include "wx/string.h"
21 #include "wx/intl.h"
22 #include "wx/window.h"
23#endif // WX_PRECOMP
24
25#include "wx/filedlg.h"
26
27#if wxUSE_FILEDLG
28
f74172ab
VZ
29//----------------------------------------------------------------------------
30// wxFileDialogBase
31//----------------------------------------------------------------------------
32
33IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase, wxDialog)
34
fe6cf128 35void wxFileDialogBase::Init()
7448de8d 36{
1e966cc4 37 m_filterIndex =
ff3e84ff 38 m_windowStyle = 0;
fe6cf128
VZ
39}
40
41bool wxFileDialogBase::Create(wxWindow *parent,
42 const wxString& message,
43 const wxString& defaultDir,
44 const wxString& defaultFile,
45 const wxString& wildCard,
46 long style,
ff3e84ff
VZ
47 const wxPoint& WXUNUSED(pos),
48 const wxSize& WXUNUSED(sz),
49 const wxString& WXUNUSED(name))
f74172ab 50{
fe6cf128
VZ
51 m_message = message;
52 m_dir = defaultDir;
53 m_fileName = defaultFile;
54 m_wildCard = wildCard;
55
f74172ab 56 m_parent = parent;
ff3e84ff 57 m_windowStyle = style;
f74172ab
VZ
58 m_filterIndex = 0;
59
96aed0cd
VZ
60 // check that the styles are not contradictory
61 wxASSERT_MSG( !(HasFlag(wxFD_SAVE) && HasFlag(wxFD_OPEN)),
62 _T("can't specify both wxFD_SAVE and wxFD_OPEN at once") );
63
64 wxASSERT_MSG( !HasFlag(wxFD_SAVE) ||
65 (!HasFlag(wxFD_MULTIPLE) && !HasFlag(wxFD_FILE_MUST_EXIST)),
66 _T("wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST can't be used with wxFD_SAVE" ) );
67
68 wxASSERT_MSG( !HasFlag(wxFD_OPEN) || !HasFlag(wxFD_OVERWRITE_PROMPT),
69 _T("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
ff3e84ff 70
d59eea26 71 if ( wildCard.empty() || wildCard == wxFileSelectorDefaultWildcardStr )
f74172ab 72 {
186545a4
VZ
73 m_wildCard = wxString::Format(_("All files (%s)|%s"),
74 wxFileSelectorDefaultWildcardStr,
75 wxFileSelectorDefaultWildcardStr);
76 }
77 else // have wild card
78 {
79 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
80 if ( m_wildCard.Find(wxT('|')) == wxNOT_FOUND )
81 {
82 wxString::size_type nDot = m_wildCard.find(_T("*."));
83 if ( nDot != wxString::npos )
84 nDot++;
85 else
86 nDot = 0;
87
88 m_wildCard = wxString::Format
89 (
90 _("%s files (%s)|%s"),
d59eea26
VZ
91 wildCard.c_str() + nDot,
92 wildCard.c_str(),
93 wildCard.c_str()
186545a4
VZ
94 );
95 }
f74172ab 96 }
fe6cf128
VZ
97
98 return true;
f74172ab
VZ
99}
100
9e152a55 101#if WXWIN_COMPATIBILITY_2_4
f74172ab
VZ
102// Parses the filterStr, returning the number of filters.
103// Returns 0 if none or if there's a problem.
104// filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
f74172ab
VZ
105int wxFileDialogBase::ParseWildcard(const wxString& filterStr,
106 wxArrayString& descriptions,
107 wxArrayString& filters)
108{
daf32463 109 return ::wxParseCommonDialogsFilter(filterStr, descriptions, filters);
f74172ab 110}
9e152a55 111#endif // WXWIN_COMPATIBILITY_2_4
f74172ab
VZ
112
113wxString wxFileDialogBase::AppendExtension(const wxString &filePath,
114 const wxString &extensionList)
115{
116 // strip off path, to avoid problems with "path.bar/foo"
117 wxString fileName = filePath.AfterLast(wxFILE_SEP_PATH);
118
119 // if fileName is of form "foo.bar" it's ok, return it
a62848fd 120 int idx_dot = fileName.Find(wxT('.'), true);
f74172ab
VZ
121 if ((idx_dot != wxNOT_FOUND) && (idx_dot < (int)fileName.Len() - 1))
122 return filePath;
123
124 // get the first extension from extensionList, or all of it
125 wxString ext = extensionList.BeforeFirst(wxT(';'));
126
127 // if ext == "foo" or "foo." there's no extension
a62848fd 128 int idx_ext_dot = ext.Find(wxT('.'), true);
f74172ab
VZ
129 if ((idx_ext_dot == wxNOT_FOUND) || (idx_ext_dot == (int)ext.Len() - 1))
130 return filePath;
131 else
132 ext = ext.AfterLast(wxT('.'));
133
134 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
135 if ((ext.Find(wxT('*')) != wxNOT_FOUND) ||
136 (ext.Find(wxT('?')) != wxNOT_FOUND) ||
b494c48b 137 (ext.Strip(wxString::both).empty()))
f74172ab
VZ
138 return filePath;
139
140 // if fileName doesn't have a '.' then add one
141 if (filePath.Last() != wxT('.'))
142 ext = wxT(".") + ext;
143
144 return filePath + ext;
145}
146
147//----------------------------------------------------------------------------
148// wxFileDialog convenience functions
149//----------------------------------------------------------------------------
150
b600ed13
VZ
151wxString wxFileSelector(const wxChar *title,
152 const wxChar *defaultDir,
153 const wxChar *defaultFileName,
154 const wxChar *defaultExtension,
155 const wxChar *filter,
156 int flags,
157 wxWindow *parent,
158 int x, int y)
159{
160 // The defaultExtension, if non-NULL, is
161 // appended to the filename if the user fails to type an extension. The new
162 // implementation (taken from wxFileSelectorEx) appends the extension
163 // automatically, by looking at the filter specification. In fact this
164 // should be better than the native Microsoft implementation because
165 // Windows only allows *one* default extension, whereas here we do the
166 // right thing depending on the filter the user has chosen.
167
168 // If there's a default extension specified but no filter, we create a
169 // suitable filter.
170
171 wxString filter2;
172 if ( defaultExtension && !filter )
173 filter2 = wxString(wxT("*.")) + defaultExtension;
174 else if ( filter )
175 filter2 = filter;
176
177 wxString defaultDirString;
178 if (defaultDir)
179 defaultDirString = defaultDir;
180
181 wxString defaultFilenameString;
182 if (defaultFileName)
183 defaultFilenameString = defaultFileName;
184
185 wxFileDialog fileDialog(parent, title, defaultDirString,
186 defaultFilenameString, filter2,
187 flags, wxPoint(x, y));
b600ed13 188
f74172ab
VZ
189 // if filter is of form "All files (*)|*|..." set correct filter index
190 if((wxStrlen(defaultExtension) != 0) && (filter2.Find(wxT('|')) != wxNOT_FOUND))
7448de8d 191 {
f74172ab 192 int filterIndex = 0;
b600ed13 193
f74172ab
VZ
194 wxArrayString descriptions, filters;
195 // don't care about errors, handled already by wxFileDialog
daf32463 196 (void)wxParseCommonDialogsFilter(filter2, descriptions, filters);
f74172ab 197 for (size_t n=0; n<filters.GetCount(); n++)
7448de8d 198 {
f74172ab 199 if (filters[n].Contains(defaultExtension))
7448de8d 200 {
f74172ab 201 filterIndex = n;
b600ed13 202 break;
7448de8d
WS
203 }
204 }
b600ed13 205
f74172ab
VZ
206 if (filterIndex > 0)
207 fileDialog.SetFilterIndex(filterIndex);
7448de8d 208 }
b600ed13 209
b600ed13
VZ
210 wxString filename;
211 if ( fileDialog.ShowModal() == wxID_OK )
212 {
213 filename = fileDialog.GetPath();
214 }
215
216 return filename;
217}
218
f74172ab
VZ
219//----------------------------------------------------------------------------
220// wxFileSelectorEx
221//----------------------------------------------------------------------------
b600ed13
VZ
222
223wxString wxFileSelectorEx(const wxChar *title,
224 const wxChar *defaultDir,
225 const wxChar *defaultFileName,
226 int* defaultFilterIndex,
227 const wxChar *filter,
228 int flags,
229 wxWindow* parent,
230 int x,
231 int y)
232
233{
234 wxFileDialog fileDialog(parent,
b494c48b
WS
235 title ? title : wxEmptyString,
236 defaultDir ? defaultDir : wxEmptyString,
237 defaultFileName ? defaultFileName : wxEmptyString,
238 filter ? filter : wxEmptyString,
b600ed13
VZ
239 flags, wxPoint(x, y));
240
241 wxString filename;
242 if ( fileDialog.ShowModal() == wxID_OK )
243 {
244 if ( defaultFilterIndex )
245 *defaultFilterIndex = fileDialog.GetFilterIndex();
246
247 filename = fileDialog.GetPath();
248 }
249
250 return filename;
251}
252
f74172ab
VZ
253//----------------------------------------------------------------------------
254// wxDefaultFileSelector - Generic load/save dialog (for internal use only)
255//----------------------------------------------------------------------------
b600ed13 256
b600ed13
VZ
257static wxString wxDefaultFileSelector(bool load,
258 const wxChar *what,
259 const wxChar *extension,
260 const wxChar *default_name,
261 wxWindow *parent)
262{
263 wxString prompt;
264 wxString str;
265 if (load)
266 str = _("Load %s file");
267 else
268 str = _("Save %s file");
269 prompt.Printf(str, what);
270
271 wxString wild;
272 const wxChar *ext = extension;
273 if ( ext )
274 {
275 if ( *ext == wxT('.') )
276 ext++;
277
278 wild.Printf(wxT("*.%s"), ext);
279 }
280 else // no extension specified
281 {
282 wild = wxFileSelectorDefaultWildcardStr;
283 }
284
285 return wxFileSelector(prompt, NULL, default_name, ext, wild,
ff3e84ff 286 load ? wxFD_OPEN : wxFD_SAVE, parent);
b600ed13
VZ
287}
288
f74172ab
VZ
289//----------------------------------------------------------------------------
290// wxLoadFileSelector
291//----------------------------------------------------------------------------
292
b600ed13
VZ
293WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
294 const wxChar *extension,
295 const wxChar *default_name,
296 wxWindow *parent)
297{
a62848fd 298 return wxDefaultFileSelector(true, what, extension, default_name, parent);
b600ed13
VZ
299}
300
f74172ab
VZ
301//----------------------------------------------------------------------------
302// wxSaveFileSelector
303//----------------------------------------------------------------------------
304
b600ed13
VZ
305WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
306 const wxChar *extension,
307 const wxChar *default_name,
308 wxWindow *parent)
309{
a62848fd 310 return wxDefaultFileSelector(false, what, extension, default_name, parent);
b600ed13
VZ
311}
312
b600ed13
VZ
313#endif // wxUSE_FILEDLG
314