]> git.saurik.com Git - wxWidgets.git/blob - src/common/fldlgcmn.cpp
Removed #include <typeinfo> (since C++ RTTI is no longer used)
[wxWidgets.git] / src / common / fldlgcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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)
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
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 #if wxUSE_FILEDLG
20
21 #include "wx/filedlg.h"
22 #include "wx/dirdlg.h"
23
24 #ifndef WX_PRECOMP
25 #include "wx/string.h"
26 #include "wx/intl.h"
27 #include "wx/window.h"
28 #endif // WX_PRECOMP
29
30 //----------------------------------------------------------------------------
31 // wxFileDialogBase
32 //----------------------------------------------------------------------------
33
34 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase, wxDialog)
35
36 void wxFileDialogBase::Init()
37 {
38 m_filterIndex = 0;
39 m_windowStyle = 0;
40 m_extraControl = NULL;
41 m_extraControlCreator = NULL;
42 }
43
44 bool wxFileDialogBase::Create(wxWindow *parent,
45 const wxString& message,
46 const wxString& defaultDir,
47 const wxString& defaultFile,
48 const wxString& wildCard,
49 long style,
50 const wxPoint& WXUNUSED(pos),
51 const wxSize& WXUNUSED(sz),
52 const wxString& WXUNUSED(name))
53 {
54 m_message = message;
55 m_dir = defaultDir;
56 m_fileName = defaultFile;
57 m_wildCard = wildCard;
58
59 m_parent = parent;
60 m_windowStyle = style;
61 m_filterIndex = 0;
62
63 if (!HasFdFlag(wxFD_OPEN) && !HasFdFlag(wxFD_SAVE))
64 m_windowStyle |= wxFD_OPEN; // wxFD_OPEN is the default
65
66 // check that the styles are not contradictory
67 wxASSERT_MSG( !(HasFdFlag(wxFD_SAVE) && HasFdFlag(wxFD_OPEN)),
68 _T("can't specify both wxFD_SAVE and wxFD_OPEN at once") );
69
70 wxASSERT_MSG( !HasFdFlag(wxFD_SAVE) ||
71 (!HasFdFlag(wxFD_MULTIPLE) && !HasFdFlag(wxFD_FILE_MUST_EXIST)),
72 _T("wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST can't be used with wxFD_SAVE" ) );
73
74 wxASSERT_MSG( !HasFdFlag(wxFD_OPEN) || !HasFdFlag(wxFD_OVERWRITE_PROMPT),
75 _T("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
76
77 if ( wildCard.empty() || wildCard == wxFileSelectorDefaultWildcardStr )
78 {
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"),
97 wildCard.c_str() + nDot,
98 wildCard.c_str(),
99 wildCard.c_str()
100 );
101 }
102 }
103
104 return true;
105 }
106
107 #if WXWIN_COMPATIBILITY_2_6
108 long wxFileDialogBase::GetStyle() const
109 {
110 return GetWindowStyle();
111 }
112
113 void wxFileDialogBase::SetStyle(long style)
114 {
115 SetWindowStyle(style);
116 }
117 #endif // WXWIN_COMPATIBILITY_2_6
118
119
120 wxString 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
127 int idx_dot = fileName.Find(wxT('.'), true);
128 if ((idx_dot != wxNOT_FOUND) && (idx_dot < (int)fileName.length() - 1))
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
135 int idx_ext_dot = ext.Find(wxT('.'), true);
136 if ((idx_ext_dot == wxNOT_FOUND) || (idx_ext_dot == (int)ext.length() - 1))
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) ||
144 (ext.Strip(wxString::both).empty()))
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
154 bool wxFileDialogBase::SetExtraControlCreator(ExtraControlCreatorFunction c)
155 {
156 wxCHECK_MSG( !m_extraControlCreator, false,
157 "wxFileDialog::SetExtraControl() called second time" );
158
159 m_extraControlCreator = c;
160 return SupportsExtraControl();
161 }
162
163 bool wxFileDialogBase::CreateExtraControl()
164 {
165 if (!m_extraControlCreator || m_extraControl)
166 return false;
167 m_extraControl = (*m_extraControlCreator)(this);
168 return true;
169 }
170
171 //----------------------------------------------------------------------------
172 // wxFileDialog convenience functions
173 //----------------------------------------------------------------------------
174
175 wxString wxFileSelector(const wxString& title,
176 const wxString& defaultDir,
177 const wxString& defaultFileName,
178 const wxString& defaultExtension,
179 const wxString& filter,
180 int flags,
181 wxWindow *parent,
182 int x, int y)
183 {
184 // The defaultExtension, if non-empty, is
185 // appended to the filename if the user fails to type an extension. The new
186 // implementation (taken from wxFileSelectorEx) appends the extension
187 // automatically, by looking at the filter specification. In fact this
188 // should be better than the native Microsoft implementation because
189 // Windows only allows *one* default extension, whereas here we do the
190 // right thing depending on the filter the user has chosen.
191
192 // If there's a default extension specified but no filter, we create a
193 // suitable filter.
194
195 wxString filter2;
196 if ( !defaultExtension.empty() && filter.empty() )
197 filter2 = wxString(wxT("*.")) + defaultExtension;
198 else if ( !filter.empty() )
199 filter2 = filter;
200
201 wxFileDialog fileDialog(parent, title, defaultDir,
202 defaultFileName, filter2,
203 flags, wxPoint(x, y));
204
205 // if filter is of form "All files (*)|*|..." set correct filter index
206 if ( !defaultExtension.empty() && filter2.find(wxT('|')) != wxString::npos )
207 {
208 int filterIndex = 0;
209
210 wxArrayString descriptions, filters;
211 // don't care about errors, handled already by wxFileDialog
212 (void)wxParseCommonDialogsFilter(filter2, descriptions, filters);
213 for (size_t n=0; n<filters.GetCount(); n++)
214 {
215 if (filters[n].Contains(defaultExtension))
216 {
217 filterIndex = n;
218 break;
219 }
220 }
221
222 if (filterIndex > 0)
223 fileDialog.SetFilterIndex(filterIndex);
224 }
225
226 wxString filename;
227 if ( fileDialog.ShowModal() == wxID_OK )
228 {
229 filename = fileDialog.GetPath();
230 }
231
232 return filename;
233 }
234
235 //----------------------------------------------------------------------------
236 // wxFileSelectorEx
237 //----------------------------------------------------------------------------
238
239 wxString wxFileSelectorEx(const wxString& title,
240 const wxString& defaultDir,
241 const wxString& defaultFileName,
242 int* defaultFilterIndex,
243 const wxString& filter,
244 int flags,
245 wxWindow* parent,
246 int x,
247 int y)
248
249 {
250 wxFileDialog fileDialog(parent,
251 title,
252 defaultDir,
253 defaultFileName,
254 filter,
255 flags, wxPoint(x, y));
256
257 wxString filename;
258 if ( fileDialog.ShowModal() == wxID_OK )
259 {
260 if ( defaultFilterIndex )
261 *defaultFilterIndex = fileDialog.GetFilterIndex();
262
263 filename = fileDialog.GetPath();
264 }
265
266 return filename;
267 }
268
269 //----------------------------------------------------------------------------
270 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
271 //----------------------------------------------------------------------------
272
273 static wxString wxDefaultFileSelector(bool load,
274 const wxString& what,
275 const wxString& extension,
276 const wxString& default_name,
277 wxWindow *parent)
278 {
279 wxString prompt;
280 wxString str;
281 if (load)
282 str = _("Load %s file");
283 else
284 str = _("Save %s file");
285 prompt.Printf(str, what);
286
287 wxString wild;
288 wxString ext;
289 if ( !extension.empty() )
290 {
291 if ( extension[0u] == _T('.') )
292 ext = extension.substr(1);
293 else
294 ext = extension;
295
296 wild.Printf(wxT("*.%s"), ext);
297 }
298 else // no extension specified
299 {
300 wild = wxFileSelectorDefaultWildcardStr;
301 }
302
303 return wxFileSelector(prompt, wxEmptyString, default_name, ext, wild,
304 load ? wxFD_OPEN : wxFD_SAVE, parent);
305 }
306
307 //----------------------------------------------------------------------------
308 // wxLoadFileSelector
309 //----------------------------------------------------------------------------
310
311 WXDLLEXPORT wxString wxLoadFileSelector(const wxString& what,
312 const wxString& extension,
313 const wxString& default_name,
314 wxWindow *parent)
315 {
316 return wxDefaultFileSelector(true, what, extension, default_name, parent);
317 }
318
319 //----------------------------------------------------------------------------
320 // wxSaveFileSelector
321 //----------------------------------------------------------------------------
322
323 WXDLLEXPORT wxString wxSaveFileSelector(const wxString& what,
324 const wxString& extension,
325 const wxString& default_name,
326 wxWindow *parent)
327 {
328 return wxDefaultFileSelector(false, what, extension, default_name, parent);
329 }
330
331
332 //----------------------------------------------------------------------------
333 // wxDirDialogBase
334 //----------------------------------------------------------------------------
335
336 #if WXWIN_COMPATIBILITY_2_6
337 long wxDirDialogBase::GetStyle() const
338 {
339 return GetWindowStyle();
340 }
341
342 void wxDirDialogBase::SetStyle(long style)
343 {
344 SetWindowStyle(style);
345 }
346 #endif // WXWIN_COMPATIBILITY_2_6
347
348
349 #endif // wxUSE_FILEDLG