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"
23 #include "wx/filename.h"
26 #include "wx/string.h"
28 #include "wx/window.h"
31 //----------------------------------------------------------------------------
33 //----------------------------------------------------------------------------
35 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase
, wxDialog
)
37 void wxFileDialogBase::Init()
41 m_extraControl
= NULL
;
42 m_extraControlCreator
= NULL
;
45 bool wxFileDialogBase::Create(wxWindow
*parent
,
46 const wxString
& message
,
47 const wxString
& defaultDir
,
48 const wxString
& defaultFile
,
49 const wxString
& wildCard
,
51 const wxPoint
& WXUNUSED(pos
),
52 const wxSize
& WXUNUSED(sz
),
53 const wxString
& WXUNUSED(name
))
57 m_fileName
= defaultFile
;
58 m_wildCard
= wildCard
;
61 m_windowStyle
= style
;
64 if (!HasFdFlag(wxFD_OPEN
) && !HasFdFlag(wxFD_SAVE
))
65 m_windowStyle
|= wxFD_OPEN
; // wxFD_OPEN is the default
67 // check that the styles are not contradictory
68 wxASSERT_MSG( !(HasFdFlag(wxFD_SAVE
) && HasFdFlag(wxFD_OPEN
)),
69 wxT("can't specify both wxFD_SAVE and wxFD_OPEN at once") );
71 wxASSERT_MSG( !HasFdFlag(wxFD_SAVE
) ||
72 (!HasFdFlag(wxFD_MULTIPLE
) && !HasFdFlag(wxFD_FILE_MUST_EXIST
)),
73 wxT("wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST can't be used with wxFD_SAVE" ) );
75 wxASSERT_MSG( !HasFdFlag(wxFD_OPEN
) || !HasFdFlag(wxFD_OVERWRITE_PROMPT
),
76 wxT("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
78 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
80 m_wildCard
= wxString::Format(_("All files (%s)|%s"),
81 wxFileSelectorDefaultWildcardStr
,
82 wxFileSelectorDefaultWildcardStr
);
84 else // have wild card
86 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
87 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
89 wxString::size_type nDot
= m_wildCard
.find(wxT("*."));
90 if ( nDot
!= wxString::npos
)
95 m_wildCard
= wxString::Format
97 _("%s files (%s)|%s"),
98 wildCard
.c_str() + nDot
,
108 #if WXWIN_COMPATIBILITY_2_6
109 long wxFileDialogBase::GetStyle() const
111 return GetWindowStyle();
114 void wxFileDialogBase::SetStyle(long style
)
116 SetWindowStyle(style
);
118 #endif // WXWIN_COMPATIBILITY_2_6
121 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
122 const wxString
&extensionList
)
124 // strip off path, to avoid problems with "path.bar/foo"
125 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
127 // if fileName is of form "foo.bar" it's ok, return it
128 int idx_dot
= fileName
.Find(wxT('.'), true);
129 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.length() - 1))
132 // get the first extension from extensionList, or all of it
133 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
135 // if ext == "foo" or "foo." there's no extension
136 int idx_ext_dot
= ext
.Find(wxT('.'), true);
137 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.length() - 1))
140 ext
= ext
.AfterLast(wxT('.'));
142 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
143 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
144 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
145 (ext
.Strip(wxString::both
).empty()))
148 // if fileName doesn't have a '.' then add one
149 if (filePath
.Last() != wxT('.'))
150 ext
= wxT(".") + ext
;
152 return filePath
+ ext
;
155 bool wxFileDialogBase::SetExtraControlCreator(ExtraControlCreatorFunction creator
)
157 wxCHECK_MSG( !m_extraControlCreator
, false,
158 "wxFileDialog::SetExtraControl() called second time" );
160 m_extraControlCreator
= creator
;
161 return SupportsExtraControl();
164 bool wxFileDialogBase::CreateExtraControl()
166 if (!m_extraControlCreator
|| m_extraControl
)
168 m_extraControl
= (*m_extraControlCreator
)(this);
172 wxSize
wxFileDialogBase::GetExtraControlSize()
174 if ( !m_extraControlCreator
)
175 return wxDefaultSize
;
177 // create the extra control in an empty dialog just to find its size: this
178 // is not terribly efficient but we do need to know the size before
179 // creating the native dialog and this seems to be the only way
180 wxDialog
dlg(NULL
, wxID_ANY
, "");
181 return (*m_extraControlCreator
)(&dlg
)->GetSize();
184 void wxFileDialogBase::SetPath(const wxString
& path
)
187 wxFileName::SplitPath(path
, &m_dir
, &m_fileName
, &ext
);
189 m_fileName
<< _T('.') << ext
;
193 void wxFileDialogBase::SetDirectory(const wxString
& dir
)
196 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
199 void wxFileDialogBase::SetFilename(const wxString
& name
)
202 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
205 //----------------------------------------------------------------------------
206 // wxFileDialog convenience functions
207 //----------------------------------------------------------------------------
209 wxString
wxFileSelector(const wxString
& title
,
210 const wxString
& defaultDir
,
211 const wxString
& defaultFileName
,
212 const wxString
& defaultExtension
,
213 const wxString
& filter
,
218 // The defaultExtension, if non-empty, is
219 // appended to the filename if the user fails to type an extension. The new
220 // implementation (taken from wxFileSelectorEx) appends the extension
221 // automatically, by looking at the filter specification. In fact this
222 // should be better than the native Microsoft implementation because
223 // Windows only allows *one* default extension, whereas here we do the
224 // right thing depending on the filter the user has chosen.
226 // If there's a default extension specified but no filter, we create a
230 if ( !defaultExtension
.empty() && filter
.empty() )
231 filter2
= wxString(wxT("*.")) + defaultExtension
;
232 else if ( !filter
.empty() )
235 wxFileDialog
fileDialog(parent
, title
, defaultDir
,
236 defaultFileName
, filter2
,
237 flags
, wxPoint(x
, y
));
239 // if filter is of form "All files (*)|*|..." set correct filter index
240 if ( !defaultExtension
.empty() && filter2
.find(wxT('|')) != wxString::npos
)
244 wxArrayString descriptions
, filters
;
245 // don't care about errors, handled already by wxFileDialog
246 (void)wxParseCommonDialogsFilter(filter2
, descriptions
, filters
);
247 for (size_t n
=0; n
<filters
.GetCount(); n
++)
249 if (filters
[n
].Contains(defaultExtension
))
257 fileDialog
.SetFilterIndex(filterIndex
);
261 if ( fileDialog
.ShowModal() == wxID_OK
)
263 filename
= fileDialog
.GetPath();
269 //----------------------------------------------------------------------------
271 //----------------------------------------------------------------------------
273 wxString
wxFileSelectorEx(const wxString
& title
,
274 const wxString
& defaultDir
,
275 const wxString
& defaultFileName
,
276 int* defaultFilterIndex
,
277 const wxString
& filter
,
284 wxFileDialog
fileDialog(parent
,
289 flags
, wxPoint(x
, y
));
292 if ( fileDialog
.ShowModal() == wxID_OK
)
294 if ( defaultFilterIndex
)
295 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
297 filename
= fileDialog
.GetPath();
303 //----------------------------------------------------------------------------
304 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
305 //----------------------------------------------------------------------------
307 static wxString
wxDefaultFileSelector(bool load
,
308 const wxString
& what
,
309 const wxString
& extension
,
310 const wxString
& default_name
,
316 str
= _("Load %s file");
318 str
= _("Save %s file");
319 prompt
.Printf(str
, what
);
323 if ( !extension
.empty() )
325 if ( extension
[0u] == wxT('.') )
326 ext
= extension
.substr(1);
330 wild
.Printf(wxT("*.%s"), ext
);
332 else // no extension specified
334 wild
= wxFileSelectorDefaultWildcardStr
;
337 return wxFileSelector(prompt
, wxEmptyString
, default_name
, ext
, wild
,
338 load
? (wxFD_OPEN
| wxFD_FILE_MUST_EXIST
) : wxFD_SAVE
,
342 //----------------------------------------------------------------------------
343 // wxLoadFileSelector
344 //----------------------------------------------------------------------------
346 WXDLLEXPORT wxString
wxLoadFileSelector(const wxString
& what
,
347 const wxString
& extension
,
348 const wxString
& default_name
,
351 return wxDefaultFileSelector(true, what
, extension
, default_name
, parent
);
354 //----------------------------------------------------------------------------
355 // wxSaveFileSelector
356 //----------------------------------------------------------------------------
358 WXDLLEXPORT wxString
wxSaveFileSelector(const wxString
& what
,
359 const wxString
& extension
,
360 const wxString
& default_name
,
363 return wxDefaultFileSelector(false, what
, extension
, default_name
, parent
);
367 //----------------------------------------------------------------------------
369 //----------------------------------------------------------------------------
371 #if WXWIN_COMPATIBILITY_2_6
372 long wxDirDialogBase::GetStyle() const
374 return GetWindowStyle();
377 void wxDirDialogBase::SetStyle(long style
)
379 SetWindowStyle(style
);
381 #endif // WXWIN_COMPATIBILITY_2_6
384 #endif // wxUSE_FILEDLG