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 extern WXDLLEXPORT_DATA(const char) wxFileDialogNameStr
[] = "filedlg";
32 extern WXDLLEXPORT_DATA(const char) wxFileSelectorPromptStr
[] = "Select a file";
33 extern WXDLLEXPORT_DATA(const char) wxFileSelectorDefaultWildcardStr
[] =
34 #if defined(__WXMSW__) || defined(__OS2__)
41 //----------------------------------------------------------------------------
43 //----------------------------------------------------------------------------
45 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase
, wxDialog
)
47 void wxFileDialogBase::Init()
51 m_extraControl
= NULL
;
52 m_extraControlCreator
= NULL
;
55 bool wxFileDialogBase::Create(wxWindow
*parent
,
56 const wxString
& message
,
57 const wxString
& defaultDir
,
58 const wxString
& defaultFile
,
59 const wxString
& wildCard
,
61 const wxPoint
& WXUNUSED(pos
),
62 const wxSize
& WXUNUSED(sz
),
63 const wxString
& WXUNUSED(name
))
67 m_fileName
= defaultFile
;
68 m_wildCard
= wildCard
;
71 m_windowStyle
= style
;
74 if (!HasFdFlag(wxFD_OPEN
) && !HasFdFlag(wxFD_SAVE
))
75 m_windowStyle
|= wxFD_OPEN
; // wxFD_OPEN is the default
77 // check that the styles are not contradictory
78 wxASSERT_MSG( !(HasFdFlag(wxFD_SAVE
) && HasFdFlag(wxFD_OPEN
)),
79 wxT("can't specify both wxFD_SAVE and wxFD_OPEN at once") );
81 wxASSERT_MSG( !HasFdFlag(wxFD_SAVE
) ||
82 (!HasFdFlag(wxFD_MULTIPLE
) && !HasFdFlag(wxFD_FILE_MUST_EXIST
)),
83 wxT("wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST can't be used with wxFD_SAVE" ) );
85 wxASSERT_MSG( !HasFdFlag(wxFD_OPEN
) || !HasFdFlag(wxFD_OVERWRITE_PROMPT
),
86 wxT("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
88 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
90 m_wildCard
= wxString::Format(_("All files (%s)|%s"),
91 wxFileSelectorDefaultWildcardStr
,
92 wxFileSelectorDefaultWildcardStr
);
94 else // have wild card
96 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
97 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
99 wxString::size_type nDot
= m_wildCard
.find(wxT("*."));
100 if ( nDot
!= wxString::npos
)
105 m_wildCard
= wxString::Format
107 _("%s files (%s)|%s"),
108 wildCard
.c_str() + nDot
,
118 #if WXWIN_COMPATIBILITY_2_6
119 long wxFileDialogBase::GetStyle() const
121 return GetWindowStyle();
124 void wxFileDialogBase::SetStyle(long style
)
126 SetWindowStyle(style
);
128 #endif // WXWIN_COMPATIBILITY_2_6
131 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
132 const wxString
&extensionList
)
134 // strip off path, to avoid problems with "path.bar/foo"
135 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
137 // if fileName is of form "foo.bar" it's ok, return it
138 int idx_dot
= fileName
.Find(wxT('.'), true);
139 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.length() - 1))
142 // get the first extension from extensionList, or all of it
143 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
145 // if ext == "foo" or "foo." there's no extension
146 int idx_ext_dot
= ext
.Find(wxT('.'), true);
147 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.length() - 1))
150 ext
= ext
.AfterLast(wxT('.'));
152 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
153 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
154 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
155 (ext
.Strip(wxString::both
).empty()))
158 // if fileName doesn't have a '.' then add one
159 if (filePath
.Last() != wxT('.'))
160 ext
= wxT(".") + ext
;
162 return filePath
+ ext
;
165 bool wxFileDialogBase::SetExtraControlCreator(ExtraControlCreatorFunction creator
)
167 wxCHECK_MSG( !m_extraControlCreator
, false,
168 "wxFileDialog::SetExtraControl() called second time" );
170 m_extraControlCreator
= creator
;
171 return SupportsExtraControl();
174 bool wxFileDialogBase::CreateExtraControl()
176 if (!m_extraControlCreator
|| m_extraControl
)
178 m_extraControl
= (*m_extraControlCreator
)(this);
182 wxSize
wxFileDialogBase::GetExtraControlSize()
184 if ( !m_extraControlCreator
)
185 return wxDefaultSize
;
187 // create the extra control in an empty dialog just to find its size: this
188 // is not terribly efficient but we do need to know the size before
189 // creating the native dialog and this seems to be the only way
190 wxDialog
dlg(NULL
, wxID_ANY
, "");
191 return (*m_extraControlCreator
)(&dlg
)->GetSize();
194 void wxFileDialogBase::SetPath(const wxString
& path
)
197 wxFileName::SplitPath(path
, &m_dir
, &m_fileName
, &ext
);
199 m_fileName
<< wxT('.') << ext
;
203 void wxFileDialogBase::SetDirectory(const wxString
& dir
)
206 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
209 void wxFileDialogBase::SetFilename(const wxString
& name
)
212 m_path
= wxFileName(m_dir
, m_fileName
).GetFullPath();
215 //----------------------------------------------------------------------------
216 // wxFileDialog convenience functions
217 //----------------------------------------------------------------------------
219 wxString
wxFileSelector(const wxString
& title
,
220 const wxString
& defaultDir
,
221 const wxString
& defaultFileName
,
222 const wxString
& defaultExtension
,
223 const wxString
& filter
,
228 // The defaultExtension, if non-empty, is
229 // appended to the filename if the user fails to type an extension. The new
230 // implementation (taken from wxFileSelectorEx) appends the extension
231 // automatically, by looking at the filter specification. In fact this
232 // should be better than the native Microsoft implementation because
233 // Windows only allows *one* default extension, whereas here we do the
234 // right thing depending on the filter the user has chosen.
236 // If there's a default extension specified but no filter, we create a
240 if ( !defaultExtension
.empty() && filter
.empty() )
241 filter2
= wxString(wxT("*.")) + defaultExtension
;
242 else if ( !filter
.empty() )
245 wxFileDialog
fileDialog(parent
, title
, defaultDir
,
246 defaultFileName
, filter2
,
247 flags
, wxPoint(x
, y
));
249 // if filter is of form "All files (*)|*|..." set correct filter index
250 if ( !defaultExtension
.empty() && filter2
.find(wxT('|')) != wxString::npos
)
254 wxArrayString descriptions
, filters
;
255 // don't care about errors, handled already by wxFileDialog
256 (void)wxParseCommonDialogsFilter(filter2
, descriptions
, filters
);
257 for (size_t n
=0; n
<filters
.GetCount(); n
++)
259 if (filters
[n
].Contains(defaultExtension
))
267 fileDialog
.SetFilterIndex(filterIndex
);
271 if ( fileDialog
.ShowModal() == wxID_OK
)
273 filename
= fileDialog
.GetPath();
279 //----------------------------------------------------------------------------
281 //----------------------------------------------------------------------------
283 wxString
wxFileSelectorEx(const wxString
& title
,
284 const wxString
& defaultDir
,
285 const wxString
& defaultFileName
,
286 int* defaultFilterIndex
,
287 const wxString
& filter
,
294 wxFileDialog
fileDialog(parent
,
299 flags
, wxPoint(x
, y
));
302 if ( fileDialog
.ShowModal() == wxID_OK
)
304 if ( defaultFilterIndex
)
305 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
307 filename
= fileDialog
.GetPath();
313 //----------------------------------------------------------------------------
314 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
315 //----------------------------------------------------------------------------
317 static wxString
wxDefaultFileSelector(bool load
,
318 const wxString
& what
,
319 const wxString
& extension
,
320 const wxString
& default_name
,
326 str
= _("Load %s file");
328 str
= _("Save %s file");
329 prompt
.Printf(str
, what
);
333 if ( !extension
.empty() )
335 if ( extension
[0u] == wxT('.') )
336 ext
= extension
.substr(1);
340 wild
.Printf(wxT("*.%s"), ext
);
342 else // no extension specified
344 wild
= wxFileSelectorDefaultWildcardStr
;
347 return wxFileSelector(prompt
, wxEmptyString
, default_name
, ext
, wild
,
348 load
? (wxFD_OPEN
| wxFD_FILE_MUST_EXIST
) : wxFD_SAVE
,
352 //----------------------------------------------------------------------------
353 // wxLoadFileSelector
354 //----------------------------------------------------------------------------
356 WXDLLEXPORT wxString
wxLoadFileSelector(const wxString
& what
,
357 const wxString
& extension
,
358 const wxString
& default_name
,
361 return wxDefaultFileSelector(true, what
, extension
, default_name
, parent
);
364 //----------------------------------------------------------------------------
365 // wxSaveFileSelector
366 //----------------------------------------------------------------------------
368 WXDLLEXPORT wxString
wxSaveFileSelector(const wxString
& what
,
369 const wxString
& extension
,
370 const wxString
& default_name
,
373 return wxDefaultFileSelector(false, what
, extension
, default_name
, parent
);
377 //----------------------------------------------------------------------------
379 //----------------------------------------------------------------------------
381 #if WXWIN_COMPATIBILITY_2_6
382 long wxDirDialogBase::GetStyle() const
384 return GetWindowStyle();
387 void wxDirDialogBase::SetStyle(long style
)
389 SetWindowStyle(style
);
391 #endif // WXWIN_COMPATIBILITY_2_6
394 #endif // wxUSE_FILEDLG