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