1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "filedlg.h"
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
24 #include "wx/string.h"
26 #include "wx/window.h"
29 #include "wx/filedlg.h"
33 //----------------------------------------------------------------------------
35 //----------------------------------------------------------------------------
37 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase
, wxDialog
)
39 wxFileDialogBase::wxFileDialogBase(wxWindow
*parent
,
40 const wxString
& message
,
41 const wxString
& defaultDir
,
42 const wxString
& defaultFile
,
43 const wxString
& wildCard
,
45 const wxPoint
& WXUNUSED(pos
))
48 m_fileName(defaultFile
),
52 m_dialogStyle
= style
;
55 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
57 m_wildCard
= wxString::Format(_("All files (%s)|%s"),
58 wxFileSelectorDefaultWildcardStr
,
59 wxFileSelectorDefaultWildcardStr
);
61 else // have wild card
63 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
64 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
66 wxString::size_type nDot
= m_wildCard
.find(_T("*."));
67 if ( nDot
!= wxString::npos
)
72 m_wildCard
= wxString::Format
74 _("%s files (%s)|%s"),
75 wildCard
.c_str() + nDot
,
83 #if WXWIN_COMPATIBILITY_2_4
84 // Parses the filterStr, returning the number of filters.
85 // Returns 0 if none or if there's a problem.
86 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
87 int wxFileDialogBase::ParseWildcard(const wxString
& filterStr
,
88 wxArrayString
& descriptions
,
89 wxArrayString
& filters
)
91 return ::wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
93 #endif // WXWIN_COMPATIBILITY_2_4
95 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
96 const wxString
&extensionList
)
98 // strip off path, to avoid problems with "path.bar/foo"
99 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
101 // if fileName is of form "foo.bar" it's ok, return it
102 int idx_dot
= fileName
.Find(wxT('.'), true);
103 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.Len() - 1))
106 // get the first extension from extensionList, or all of it
107 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
109 // if ext == "foo" or "foo." there's no extension
110 int idx_ext_dot
= ext
.Find(wxT('.'), true);
111 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.Len() - 1))
114 ext
= ext
.AfterLast(wxT('.'));
116 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
117 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
118 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
119 (ext
.Strip(wxString::both
).empty()))
122 // if fileName doesn't have a '.' then add one
123 if (filePath
.Last() != wxT('.'))
124 ext
= wxT(".") + ext
;
126 return filePath
+ ext
;
129 //----------------------------------------------------------------------------
130 // wxFileDialog convenience functions
131 //----------------------------------------------------------------------------
133 wxString
wxFileSelector(const wxChar
*title
,
134 const wxChar
*defaultDir
,
135 const wxChar
*defaultFileName
,
136 const wxChar
*defaultExtension
,
137 const wxChar
*filter
,
142 // The defaultExtension, if non-NULL, is
143 // appended to the filename if the user fails to type an extension. The new
144 // implementation (taken from wxFileSelectorEx) appends the extension
145 // automatically, by looking at the filter specification. In fact this
146 // should be better than the native Microsoft implementation because
147 // Windows only allows *one* default extension, whereas here we do the
148 // right thing depending on the filter the user has chosen.
150 // If there's a default extension specified but no filter, we create a
154 if ( defaultExtension
&& !filter
)
155 filter2
= wxString(wxT("*.")) + defaultExtension
;
159 wxString defaultDirString
;
161 defaultDirString
= defaultDir
;
163 wxString defaultFilenameString
;
165 defaultFilenameString
= defaultFileName
;
167 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
168 defaultFilenameString
, filter2
,
169 flags
, wxPoint(x
, y
));
171 // if filter is of form "All files (*)|*|..." set correct filter index
172 if((wxStrlen(defaultExtension
) != 0) && (filter2
.Find(wxT('|')) != wxNOT_FOUND
))
176 wxArrayString descriptions
, filters
;
177 // don't care about errors, handled already by wxFileDialog
178 (void)wxParseCommonDialogsFilter(filter2
, descriptions
, filters
);
179 for (size_t n
=0; n
<filters
.GetCount(); n
++)
181 if (filters
[n
].Contains(defaultExtension
))
189 fileDialog
.SetFilterIndex(filterIndex
);
193 if ( fileDialog
.ShowModal() == wxID_OK
)
195 filename
= fileDialog
.GetPath();
201 //----------------------------------------------------------------------------
203 //----------------------------------------------------------------------------
205 wxString
wxFileSelectorEx(const wxChar
*title
,
206 const wxChar
*defaultDir
,
207 const wxChar
*defaultFileName
,
208 int* defaultFilterIndex
,
209 const wxChar
*filter
,
216 wxFileDialog
fileDialog(parent
,
217 title
? title
: wxEmptyString
,
218 defaultDir
? defaultDir
: wxEmptyString
,
219 defaultFileName
? defaultFileName
: wxEmptyString
,
220 filter
? filter
: wxEmptyString
,
221 flags
, wxPoint(x
, y
));
224 if ( fileDialog
.ShowModal() == wxID_OK
)
226 if ( defaultFilterIndex
)
227 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
229 filename
= fileDialog
.GetPath();
235 //----------------------------------------------------------------------------
236 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
237 //----------------------------------------------------------------------------
239 static wxString
wxDefaultFileSelector(bool load
,
241 const wxChar
*extension
,
242 const wxChar
*default_name
,
248 str
= _("Load %s file");
250 str
= _("Save %s file");
251 prompt
.Printf(str
, what
);
254 const wxChar
*ext
= extension
;
257 if ( *ext
== wxT('.') )
260 wild
.Printf(wxT("*.%s"), ext
);
262 else // no extension specified
264 wild
= wxFileSelectorDefaultWildcardStr
;
267 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
268 load
? wxOPEN
: wxSAVE
, parent
);
271 //----------------------------------------------------------------------------
272 // wxLoadFileSelector
273 //----------------------------------------------------------------------------
275 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
276 const wxChar
*extension
,
277 const wxChar
*default_name
,
280 return wxDefaultFileSelector(true, what
, extension
, default_name
, parent
);
283 //----------------------------------------------------------------------------
284 // wxSaveFileSelector
285 //----------------------------------------------------------------------------
287 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
288 const wxChar
*extension
,
289 const wxChar
*default_name
,
292 return wxDefaultFileSelector(false, what
, extension
, default_name
, parent
);
295 #endif // wxUSE_FILEDLG