1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fldlgcmn.cpp
3 // Purpose: wxFileDialog common functions
4 // Author: John Labenski
6 // Created: 14.06.03 (extracted from src/*/filedlg.cpp)
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/filedlg.h"
22 #include "wx/dirdlg.h"
25 #include "wx/string.h"
27 #include "wx/window.h"
30 //----------------------------------------------------------------------------
32 //----------------------------------------------------------------------------
34 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase
, wxDialog
)
36 void wxFileDialogBase::Init()
40 m_extraControl
= NULL
;
41 m_extraControlCreator
= NULL
;
44 bool wxFileDialogBase::Create(wxWindow
*parent
,
45 const wxString
& message
,
46 const wxString
& defaultDir
,
47 const wxString
& defaultFile
,
48 const wxString
& wildCard
,
50 const wxPoint
& WXUNUSED(pos
),
51 const wxSize
& WXUNUSED(sz
),
52 const wxString
& WXUNUSED(name
))
56 m_fileName
= defaultFile
;
57 m_wildCard
= wildCard
;
60 m_windowStyle
= style
;
63 if (!HasFdFlag(wxFD_OPEN
) && !HasFdFlag(wxFD_SAVE
))
64 m_windowStyle
|= wxFD_OPEN
; // wxFD_OPEN is the default
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") );
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" ) );
74 wxASSERT_MSG( !HasFdFlag(wxFD_OPEN
) || !HasFdFlag(wxFD_OVERWRITE_PROMPT
),
75 _T("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
77 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
79 m_wildCard
= wxString::Format(_("All files (%s)|%s"),
80 wxFileSelectorDefaultWildcardStr
,
81 wxFileSelectorDefaultWildcardStr
);
83 else // have wild card
85 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
86 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
88 wxString::size_type nDot
= m_wildCard
.find(_T("*."));
89 if ( nDot
!= wxString::npos
)
94 m_wildCard
= wxString::Format
96 _("%s files (%s)|%s"),
97 wildCard
.c_str() + nDot
,
107 #if WXWIN_COMPATIBILITY_2_6
108 long wxFileDialogBase::GetStyle() const
110 return GetWindowStyle();
113 void wxFileDialogBase::SetStyle(long style
)
115 SetWindowStyle(style
);
117 #endif // WXWIN_COMPATIBILITY_2_6
120 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
121 const wxString
&extensionList
)
123 // strip off path, to avoid problems with "path.bar/foo"
124 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
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))
131 // get the first extension from extensionList, or all of it
132 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
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))
139 ext
= ext
.AfterLast(wxT('.'));
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()))
147 // if fileName doesn't have a '.' then add one
148 if (filePath
.Last() != wxT('.'))
149 ext
= wxT(".") + ext
;
151 return filePath
+ ext
;
154 bool wxFileDialogBase::SetExtraControlCreator(ExtraControlCreatorFunction creator
)
156 wxCHECK_MSG( !m_extraControlCreator
, false,
157 "wxFileDialog::SetExtraControl() called second time" );
159 m_extraControlCreator
= creator
;
160 return SupportsExtraControl();
163 bool wxFileDialogBase::CreateExtraControl()
165 if (!m_extraControlCreator
|| m_extraControl
)
167 m_extraControl
= (*m_extraControlCreator
)(this);
171 wxSize
wxFileDialogBase::GetExtraControlSize()
173 if ( !m_extraControlCreator
)
174 return wxDefaultSize
;
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();
183 //----------------------------------------------------------------------------
184 // wxFileDialog convenience functions
185 //----------------------------------------------------------------------------
187 wxString
wxFileSelector(const wxString
& title
,
188 const wxString
& defaultDir
,
189 const wxString
& defaultFileName
,
190 const wxString
& defaultExtension
,
191 const wxString
& filter
,
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.
204 // If there's a default extension specified but no filter, we create a
208 if ( !defaultExtension
.empty() && filter
.empty() )
209 filter2
= wxString(wxT("*.")) + defaultExtension
;
210 else if ( !filter
.empty() )
213 wxFileDialog
fileDialog(parent
, title
, defaultDir
,
214 defaultFileName
, filter2
,
215 flags
, wxPoint(x
, y
));
217 // if filter is of form "All files (*)|*|..." set correct filter index
218 if ( !defaultExtension
.empty() && filter2
.find(wxT('|')) != wxString::npos
)
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
++)
227 if (filters
[n
].Contains(defaultExtension
))
235 fileDialog
.SetFilterIndex(filterIndex
);
239 if ( fileDialog
.ShowModal() == wxID_OK
)
241 filename
= fileDialog
.GetPath();
247 //----------------------------------------------------------------------------
249 //----------------------------------------------------------------------------
251 wxString
wxFileSelectorEx(const wxString
& title
,
252 const wxString
& defaultDir
,
253 const wxString
& defaultFileName
,
254 int* defaultFilterIndex
,
255 const wxString
& filter
,
262 wxFileDialog
fileDialog(parent
,
267 flags
, wxPoint(x
, y
));
270 if ( fileDialog
.ShowModal() == wxID_OK
)
272 if ( defaultFilterIndex
)
273 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
275 filename
= fileDialog
.GetPath();
281 //----------------------------------------------------------------------------
282 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
283 //----------------------------------------------------------------------------
285 static wxString
wxDefaultFileSelector(bool load
,
286 const wxString
& what
,
287 const wxString
& extension
,
288 const wxString
& default_name
,
294 str
= _("Load %s file");
296 str
= _("Save %s file");
297 prompt
.Printf(str
, what
);
301 if ( !extension
.empty() )
303 if ( extension
[0u] == _T('.') )
304 ext
= extension
.substr(1);
308 wild
.Printf(wxT("*.%s"), ext
);
310 else // no extension specified
312 wild
= wxFileSelectorDefaultWildcardStr
;
315 return wxFileSelector(prompt
, wxEmptyString
, default_name
, ext
, wild
,
316 load
? wxFD_OPEN
: wxFD_SAVE
, parent
);
319 //----------------------------------------------------------------------------
320 // wxLoadFileSelector
321 //----------------------------------------------------------------------------
323 WXDLLEXPORT wxString
wxLoadFileSelector(const wxString
& what
,
324 const wxString
& extension
,
325 const wxString
& default_name
,
328 return wxDefaultFileSelector(true, what
, extension
, default_name
, parent
);
331 //----------------------------------------------------------------------------
332 // wxSaveFileSelector
333 //----------------------------------------------------------------------------
335 WXDLLEXPORT wxString
wxSaveFileSelector(const wxString
& what
,
336 const wxString
& extension
,
337 const wxString
& default_name
,
340 return wxDefaultFileSelector(false, what
, extension
, default_name
, parent
);
344 //----------------------------------------------------------------------------
346 //----------------------------------------------------------------------------
348 #if WXWIN_COMPATIBILITY_2_6
349 long wxDirDialogBase::GetStyle() const
351 return GetWindowStyle();
354 void wxDirDialogBase::SetStyle(long style
)
356 SetWindowStyle(style
);
358 #endif // WXWIN_COMPATIBILITY_2_6
361 #endif // wxUSE_FILEDLG