]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/fldlgcmn.cpp
Corrected IMPLEMENT_CLASS/BEGIN_EVENT_TABLE base class
[wxWidgets.git] / src / common / fldlgcmn.cpp
... / ...
CommitLineData
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
34IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase, wxDialog)
35
36void wxFileDialogBase::Init()
37{
38 m_filterIndex = 0;
39 m_windowStyle = 0;
40 m_extraControl = NULL;
41 m_extraControlCreator = NULL;
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,
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
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
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
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
154bool wxFileDialogBase::SetExtraControlCreator(ExtraControlCreatorFunction creator)
155{
156 wxCHECK_MSG( !m_extraControlCreator, false,
157 "wxFileDialog::SetExtraControl() called second time" );
158
159 m_extraControlCreator = creator;
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
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
183//----------------------------------------------------------------------------
184// wxFileDialog convenience functions
185//----------------------------------------------------------------------------
186
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)
195{
196 // The defaultExtension, if non-empty, is
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;
208 if ( !defaultExtension.empty() && filter.empty() )
209 filter2 = wxString(wxT("*.")) + defaultExtension;
210 else if ( !filter.empty() )
211 filter2 = filter;
212
213 wxFileDialog fileDialog(parent, title, defaultDir,
214 defaultFileName, filter2,
215 flags, wxPoint(x, y));
216
217 // if filter is of form "All files (*)|*|..." set correct filter index
218 if ( !defaultExtension.empty() && filter2.find(wxT('|')) != wxString::npos )
219 {
220 int filterIndex = 0;
221
222 wxArrayString descriptions, filters;
223 // don't care about errors, handled already by wxFileDialog
224 (void)wxParseCommonDialogsFilter(filter2, descriptions, filters);
225 for (size_t n=0; n<filters.GetCount(); n++)
226 {
227 if (filters[n].Contains(defaultExtension))
228 {
229 filterIndex = n;
230 break;
231 }
232 }
233
234 if (filterIndex > 0)
235 fileDialog.SetFilterIndex(filterIndex);
236 }
237
238 wxString filename;
239 if ( fileDialog.ShowModal() == wxID_OK )
240 {
241 filename = fileDialog.GetPath();
242 }
243
244 return filename;
245}
246
247//----------------------------------------------------------------------------
248// wxFileSelectorEx
249//----------------------------------------------------------------------------
250
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)
260
261{
262 wxFileDialog fileDialog(parent,
263 title,
264 defaultDir,
265 defaultFileName,
266 filter,
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
281//----------------------------------------------------------------------------
282// wxDefaultFileSelector - Generic load/save dialog (for internal use only)
283//----------------------------------------------------------------------------
284
285static wxString wxDefaultFileSelector(bool load,
286 const wxString& what,
287 const wxString& extension,
288 const wxString& default_name,
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;
300 wxString ext;
301 if ( !extension.empty() )
302 {
303 if ( extension[0u] == _T('.') )
304 ext = extension.substr(1);
305 else
306 ext = extension;
307
308 wild.Printf(wxT("*.%s"), ext);
309 }
310 else // no extension specified
311 {
312 wild = wxFileSelectorDefaultWildcardStr;
313 }
314
315 return wxFileSelector(prompt, wxEmptyString, default_name, ext, wild,
316 load ? wxFD_OPEN : wxFD_SAVE, parent);
317}
318
319//----------------------------------------------------------------------------
320// wxLoadFileSelector
321//----------------------------------------------------------------------------
322
323WXDLLEXPORT wxString wxLoadFileSelector(const wxString& what,
324 const wxString& extension,
325 const wxString& default_name,
326 wxWindow *parent)
327{
328 return wxDefaultFileSelector(true, what, extension, default_name, parent);
329}
330
331//----------------------------------------------------------------------------
332// wxSaveFileSelector
333//----------------------------------------------------------------------------
334
335WXDLLEXPORT wxString wxSaveFileSelector(const wxString& what,
336 const wxString& extension,
337 const wxString& default_name,
338 wxWindow *parent)
339{
340 return wxDefaultFileSelector(false, what, extension, default_name, parent);
341}
342
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
361#endif // wxUSE_FILEDLG