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)
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/filedlg.h"
21 #include "wx/dirdlg.h"
22 #include "wx/filename.h"
25 #include "wx/string.h"
27 #include "wx/window.h"
30 extern WXDLLEXPORT_DATA(const char) wxFileDialogNameStr
[] = "filedlg";
31 extern WXDLLEXPORT_DATA(const char) wxFileSelectorPromptStr
[] = "Select a file";
32 extern WXDLLEXPORT_DATA(const char) wxFileSelectorDefaultWildcardStr
[] =
33 #if defined(__WXMSW__) || defined(__OS2__)
40 //----------------------------------------------------------------------------
42 //----------------------------------------------------------------------------
44 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase
, wxDialog
)
46 void wxFileDialogBase::Init()
50 m_extraControl
= NULL
;
51 m_extraControlCreator
= NULL
;
54 bool wxFileDialogBase::Create(wxWindow
*parent
,
55 const wxString
& message
,
56 const wxString
& defaultDir
,
57 const wxString
& defaultFile
,
58 const wxString
& wildCard
,
60 const wxPoint
& WXUNUSED(pos
),
61 const wxSize
& WXUNUSED(sz
),
62 const wxString
& WXUNUSED(name
))
66 m_fileName
= defaultFile
;
67 m_wildCard
= wildCard
;
70 m_windowStyle
= style
;
73 if (!HasFdFlag(wxFD_OPEN
) && !HasFdFlag(wxFD_SAVE
))
74 m_windowStyle
|= wxFD_OPEN
; // wxFD_OPEN is the default
76 // check that the styles are not contradictory
77 wxASSERT_MSG( !(HasFdFlag(wxFD_SAVE
) && HasFdFlag(wxFD_OPEN
)),
78 wxT("can't specify both wxFD_SAVE and wxFD_OPEN at once") );
80 wxASSERT_MSG( !HasFdFlag(wxFD_SAVE
) ||
81 (!HasFdFlag(wxFD_MULTIPLE
) && !HasFdFlag(wxFD_FILE_MUST_EXIST
)),
82 wxT("wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST can't be used with wxFD_SAVE" ) );
84 wxASSERT_MSG( !HasFdFlag(wxFD_OPEN
) || !HasFdFlag(wxFD_OVERWRITE_PROMPT
),
85 wxT("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
87 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
89 m_wildCard
= wxString::Format(_("All files (%s)|%s"),
90 wxFileSelectorDefaultWildcardStr
,
91 wxFileSelectorDefaultWildcardStr
);
93 else // have wild card
95 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
96 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
98 wxString::size_type nDot
= m_wildCard
.find(wxT("*."));
99 if ( nDot
!= wxString::npos
)
104 m_wildCard
= wxString::Format
106 _("%s files (%s)|%s"),
107 wildCard
.c_str() + nDot
,
117 #if WXWIN_COMPATIBILITY_2_6
118 long wxFileDialogBase::GetStyle() const
120 return GetWindowStyle();
123 void wxFileDialogBase::SetStyle(long style
)
125 SetWindowStyle(style
);
127 #endif // WXWIN_COMPATIBILITY_2_6
130 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
131 const wxString
&extensionList
)
133 // strip off path, to avoid problems with "path.bar/foo"
134 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
136 // if fileName is of form "foo.bar" it's ok, return it
137 int idx_dot
= fileName
.Find(wxT('.'), true);
138 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.length() - 1))
141 // get the first extension from extensionList, or all of it
142 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
144 // if ext == "foo" or "foo." there's no extension
145 int idx_ext_dot
= ext
.Find(wxT('.'), true);
146 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.length() - 1))
149 ext
= ext
.AfterLast(wxT('.'));
151 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
152 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
153 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
154 (ext
.Strip(wxString::both
).empty()))
157 // if fileName doesn't have a '.' then add one
158 if (filePath
.Last() != wxT('.'))
159 ext
= wxT(".") + ext
;
161 return filePath
+ ext
;
164 bool wxFileDialogBase::SetExtraControlCreator(ExtraControlCreatorFunction creator
)
166 wxCHECK_MSG( !m_extraControlCreator
, false,
167 "wxFileDialog::SetExtraControl() called second time" );
169 m_extraControlCreator
= creator
;
170 return SupportsExtraControl();
173 bool wxFileDialogBase::CreateExtraControl()
175 if (!m_extraControlCreator
|| m_extraControl
)
177 m_extraControl
= (*m_extraControlCreator
)(this);
181 wxSize
wxFileDialogBase::GetExtraControlSize()
183 if ( !m_extraControlCreator
)
184 return wxDefaultSize
;
186 // create the extra control in an empty dialog just to find its size: this
187 // is not terribly efficient but we do need to know the size before
188 // creating the native dialog and this seems to be the only way
189 wxDialog
dlg(NULL
, wxID_ANY
, "");
190 return (*m_extraControlCreator
)(&dlg
)->GetSize();
193 void wxFileDialogBase::SetPath(const wxString
& path
)
196 wxFileName::SplitPath(path
, &m_dir
, &m_fileName
, &ext
);
198 m_fileName
<< wxT('.') << ext
;
202 void wxFileDialogBase::SetDirectory(const wxString
& dir
)
205 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
208 void wxFileDialogBase::SetFilename(const wxString
& name
)
211 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
214 //----------------------------------------------------------------------------
215 // wxFileDialog convenience functions
216 //----------------------------------------------------------------------------
218 wxString
wxFileSelector(const wxString
& title
,
219 const wxString
& defaultDir
,
220 const wxString
& defaultFileName
,
221 const wxString
& defaultExtension
,
222 const wxString
& filter
,
227 // The defaultExtension, if non-empty, is
228 // appended to the filename if the user fails to type an extension. The new
229 // implementation (taken from wxFileSelectorEx) appends the extension
230 // automatically, by looking at the filter specification. In fact this
231 // should be better than the native Microsoft implementation because
232 // Windows only allows *one* default extension, whereas here we do the
233 // right thing depending on the filter the user has chosen.
235 // If there's a default extension specified but no filter, we create a
239 if ( !defaultExtension
.empty() && filter
.empty() )
240 filter2
= wxString(wxT("*.")) + defaultExtension
;
241 else if ( !filter
.empty() )
244 wxFileDialog
fileDialog(parent
, title
, defaultDir
,
245 defaultFileName
, filter2
,
246 flags
, wxPoint(x
, y
));
248 // if filter is of form "All files (*)|*|..." set correct filter index
249 if ( !defaultExtension
.empty() && filter2
.find(wxT('|')) != wxString::npos
)
253 wxArrayString descriptions
, filters
;
254 // don't care about errors, handled already by wxFileDialog
255 (void)wxParseCommonDialogsFilter(filter2
, descriptions
, filters
);
256 for (size_t n
=0; n
<filters
.GetCount(); n
++)
258 if (filters
[n
].Contains(defaultExtension
))
266 fileDialog
.SetFilterIndex(filterIndex
);
270 if ( fileDialog
.ShowModal() == wxID_OK
)
272 filename
= fileDialog
.GetPath();
278 //----------------------------------------------------------------------------
280 //----------------------------------------------------------------------------
282 wxString
wxFileSelectorEx(const wxString
& title
,
283 const wxString
& defaultDir
,
284 const wxString
& defaultFileName
,
285 int* defaultFilterIndex
,
286 const wxString
& filter
,
293 wxFileDialog
fileDialog(parent
,
298 flags
, wxPoint(x
, y
));
301 if ( fileDialog
.ShowModal() == wxID_OK
)
303 if ( defaultFilterIndex
)
304 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
306 filename
= fileDialog
.GetPath();
312 //----------------------------------------------------------------------------
313 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
314 //----------------------------------------------------------------------------
316 static wxString
wxDefaultFileSelector(bool load
,
317 const wxString
& what
,
318 const wxString
& extension
,
319 const wxString
& default_name
,
325 str
= _("Load %s file");
327 str
= _("Save %s file");
328 prompt
.Printf(str
, what
);
332 if ( !extension
.empty() )
334 if ( extension
[0u] == wxT('.') )
335 ext
= extension
.substr(1);
339 wild
.Printf(wxT("*.%s"), ext
);
341 else // no extension specified
343 wild
= wxFileSelectorDefaultWildcardStr
;
346 return wxFileSelector(prompt
, wxEmptyString
, default_name
, ext
, wild
,
347 load
? (wxFD_OPEN
| wxFD_FILE_MUST_EXIST
) : wxFD_SAVE
,
351 //----------------------------------------------------------------------------
352 // wxLoadFileSelector
353 //----------------------------------------------------------------------------
355 WXDLLEXPORT wxString
wxLoadFileSelector(const wxString
& what
,
356 const wxString
& extension
,
357 const wxString
& default_name
,
360 return wxDefaultFileSelector(true, what
, extension
, default_name
, parent
);
363 //----------------------------------------------------------------------------
364 // wxSaveFileSelector
365 //----------------------------------------------------------------------------
367 WXDLLEXPORT wxString
wxSaveFileSelector(const wxString
& what
,
368 const wxString
& extension
,
369 const wxString
& default_name
,
372 return wxDefaultFileSelector(false, what
, extension
, default_name
, parent
);
376 //----------------------------------------------------------------------------
378 //----------------------------------------------------------------------------
380 #if WXWIN_COMPATIBILITY_2_6
381 long wxDirDialogBase::GetStyle() const
383 return GetWindowStyle();
386 void wxDirDialogBase::SetStyle(long style
)
388 SetWindowStyle(style
);
390 #endif // WXWIN_COMPATIBILITY_2_6
393 #endif // wxUSE_FILEDLG