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 /////////////////////////////////////////////////////////////////////////////
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #include "wx/string.h"
22 #include "wx/window.h"
25 #include "wx/filedlg.h"
29 //----------------------------------------------------------------------------
31 //----------------------------------------------------------------------------
33 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase
, wxDialog
)
35 void wxFileDialogBase::Init()
37 m_filterIndex
= m_dialogStyle
= 0;
41 bool wxFileDialogBase::Create(wxWindow
*parent
,
42 const wxString
& message
,
43 const wxString
& defaultDir
,
44 const wxString
& defaultFile
,
45 const wxString
& wildCard
,
47 const wxPoint
& WXUNUSED(pos
))
51 m_fileName
= defaultFile
;
52 m_wildCard
= wildCard
;
55 m_dialogStyle
= style
;
58 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
60 m_wildCard
= wxString::Format(_("All files (%s)|%s"),
61 wxFileSelectorDefaultWildcardStr
,
62 wxFileSelectorDefaultWildcardStr
);
64 else // have wild card
66 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
67 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
69 wxString::size_type nDot
= m_wildCard
.find(_T("*."));
70 if ( nDot
!= wxString::npos
)
75 m_wildCard
= wxString::Format
77 _("%s files (%s)|%s"),
78 wildCard
.c_str() + nDot
,
88 #if WXWIN_COMPATIBILITY_2_4
89 // Parses the filterStr, returning the number of filters.
90 // Returns 0 if none or if there's a problem.
91 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
92 int wxFileDialogBase::ParseWildcard(const wxString
& filterStr
,
93 wxArrayString
& descriptions
,
94 wxArrayString
& filters
)
96 return ::wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
98 #endif // WXWIN_COMPATIBILITY_2_4
100 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
101 const wxString
&extensionList
)
103 // strip off path, to avoid problems with "path.bar/foo"
104 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
106 // if fileName is of form "foo.bar" it's ok, return it
107 int idx_dot
= fileName
.Find(wxT('.'), true);
108 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.Len() - 1))
111 // get the first extension from extensionList, or all of it
112 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
114 // if ext == "foo" or "foo." there's no extension
115 int idx_ext_dot
= ext
.Find(wxT('.'), true);
116 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.Len() - 1))
119 ext
= ext
.AfterLast(wxT('.'));
121 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
122 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
123 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
124 (ext
.Strip(wxString::both
).empty()))
127 // if fileName doesn't have a '.' then add one
128 if (filePath
.Last() != wxT('.'))
129 ext
= wxT(".") + ext
;
131 return filePath
+ ext
;
134 //----------------------------------------------------------------------------
135 // wxFileDialog convenience functions
136 //----------------------------------------------------------------------------
138 wxString
wxFileSelector(const wxChar
*title
,
139 const wxChar
*defaultDir
,
140 const wxChar
*defaultFileName
,
141 const wxChar
*defaultExtension
,
142 const wxChar
*filter
,
147 // The defaultExtension, if non-NULL, is
148 // appended to the filename if the user fails to type an extension. The new
149 // implementation (taken from wxFileSelectorEx) appends the extension
150 // automatically, by looking at the filter specification. In fact this
151 // should be better than the native Microsoft implementation because
152 // Windows only allows *one* default extension, whereas here we do the
153 // right thing depending on the filter the user has chosen.
155 // If there's a default extension specified but no filter, we create a
159 if ( defaultExtension
&& !filter
)
160 filter2
= wxString(wxT("*.")) + defaultExtension
;
164 wxString defaultDirString
;
166 defaultDirString
= defaultDir
;
168 wxString defaultFilenameString
;
170 defaultFilenameString
= defaultFileName
;
172 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
173 defaultFilenameString
, filter2
,
174 flags
, wxPoint(x
, y
));
176 // if filter is of form "All files (*)|*|..." set correct filter index
177 if((wxStrlen(defaultExtension
) != 0) && (filter2
.Find(wxT('|')) != wxNOT_FOUND
))
181 wxArrayString descriptions
, filters
;
182 // don't care about errors, handled already by wxFileDialog
183 (void)wxParseCommonDialogsFilter(filter2
, descriptions
, filters
);
184 for (size_t n
=0; n
<filters
.GetCount(); n
++)
186 if (filters
[n
].Contains(defaultExtension
))
194 fileDialog
.SetFilterIndex(filterIndex
);
198 if ( fileDialog
.ShowModal() == wxID_OK
)
200 filename
= fileDialog
.GetPath();
206 //----------------------------------------------------------------------------
208 //----------------------------------------------------------------------------
210 wxString
wxFileSelectorEx(const wxChar
*title
,
211 const wxChar
*defaultDir
,
212 const wxChar
*defaultFileName
,
213 int* defaultFilterIndex
,
214 const wxChar
*filter
,
221 wxFileDialog
fileDialog(parent
,
222 title
? title
: wxEmptyString
,
223 defaultDir
? defaultDir
: wxEmptyString
,
224 defaultFileName
? defaultFileName
: wxEmptyString
,
225 filter
? filter
: wxEmptyString
,
226 flags
, wxPoint(x
, y
));
229 if ( fileDialog
.ShowModal() == wxID_OK
)
231 if ( defaultFilterIndex
)
232 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
234 filename
= fileDialog
.GetPath();
240 //----------------------------------------------------------------------------
241 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
242 //----------------------------------------------------------------------------
244 static wxString
wxDefaultFileSelector(bool load
,
246 const wxChar
*extension
,
247 const wxChar
*default_name
,
253 str
= _("Load %s file");
255 str
= _("Save %s file");
256 prompt
.Printf(str
, what
);
259 const wxChar
*ext
= extension
;
262 if ( *ext
== wxT('.') )
265 wild
.Printf(wxT("*.%s"), ext
);
267 else // no extension specified
269 wild
= wxFileSelectorDefaultWildcardStr
;
272 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
273 load
? wxOPEN
: wxSAVE
, parent
);
276 //----------------------------------------------------------------------------
277 // wxLoadFileSelector
278 //----------------------------------------------------------------------------
280 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
281 const wxChar
*extension
,
282 const wxChar
*default_name
,
285 return wxDefaultFileSelector(true, what
, extension
, default_name
, parent
);
288 //----------------------------------------------------------------------------
289 // wxSaveFileSelector
290 //----------------------------------------------------------------------------
292 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
293 const wxChar
*extension
,
294 const wxChar
*default_name
,
297 return wxDefaultFileSelector(false, what
, extension
, default_name
, parent
);
300 #endif // wxUSE_FILEDLG