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 void wxFileDialogBase::Init()
41 m_filterIndex
= m_dialogStyle
= 0;
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
))
55 m_fileName
= defaultFile
;
56 m_wildCard
= wildCard
;
59 m_dialogStyle
= style
;
62 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
64 m_wildCard
= wxString::Format(_("All files (%s)|%s"),
65 wxFileSelectorDefaultWildcardStr
,
66 wxFileSelectorDefaultWildcardStr
);
68 else // have wild card
70 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
71 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
73 wxString::size_type nDot
= m_wildCard
.find(_T("*."));
74 if ( nDot
!= wxString::npos
)
79 m_wildCard
= wxString::Format
81 _("%s files (%s)|%s"),
82 wildCard
.c_str() + nDot
,
92 #if WXWIN_COMPATIBILITY_2_4
93 // Parses the filterStr, returning the number of filters.
94 // Returns 0 if none or if there's a problem.
95 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
96 int wxFileDialogBase::ParseWildcard(const wxString
& filterStr
,
97 wxArrayString
& descriptions
,
98 wxArrayString
& filters
)
100 return ::wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
102 #endif // WXWIN_COMPATIBILITY_2_4
104 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
105 const wxString
&extensionList
)
107 // strip off path, to avoid problems with "path.bar/foo"
108 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
110 // if fileName is of form "foo.bar" it's ok, return it
111 int idx_dot
= fileName
.Find(wxT('.'), true);
112 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.Len() - 1))
115 // get the first extension from extensionList, or all of it
116 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
118 // if ext == "foo" or "foo." there's no extension
119 int idx_ext_dot
= ext
.Find(wxT('.'), true);
120 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.Len() - 1))
123 ext
= ext
.AfterLast(wxT('.'));
125 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
126 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
127 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
128 (ext
.Strip(wxString::both
).empty()))
131 // if fileName doesn't have a '.' then add one
132 if (filePath
.Last() != wxT('.'))
133 ext
= wxT(".") + ext
;
135 return filePath
+ ext
;
138 //----------------------------------------------------------------------------
139 // wxFileDialog convenience functions
140 //----------------------------------------------------------------------------
142 wxString
wxFileSelector(const wxChar
*title
,
143 const wxChar
*defaultDir
,
144 const wxChar
*defaultFileName
,
145 const wxChar
*defaultExtension
,
146 const wxChar
*filter
,
151 // The defaultExtension, if non-NULL, is
152 // appended to the filename if the user fails to type an extension. The new
153 // implementation (taken from wxFileSelectorEx) appends the extension
154 // automatically, by looking at the filter specification. In fact this
155 // should be better than the native Microsoft implementation because
156 // Windows only allows *one* default extension, whereas here we do the
157 // right thing depending on the filter the user has chosen.
159 // If there's a default extension specified but no filter, we create a
163 if ( defaultExtension
&& !filter
)
164 filter2
= wxString(wxT("*.")) + defaultExtension
;
168 wxString defaultDirString
;
170 defaultDirString
= defaultDir
;
172 wxString defaultFilenameString
;
174 defaultFilenameString
= defaultFileName
;
176 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
177 defaultFilenameString
, filter2
,
178 flags
, wxPoint(x
, y
));
180 // if filter is of form "All files (*)|*|..." set correct filter index
181 if((wxStrlen(defaultExtension
) != 0) && (filter2
.Find(wxT('|')) != wxNOT_FOUND
))
185 wxArrayString descriptions
, filters
;
186 // don't care about errors, handled already by wxFileDialog
187 (void)wxParseCommonDialogsFilter(filter2
, descriptions
, filters
);
188 for (size_t n
=0; n
<filters
.GetCount(); n
++)
190 if (filters
[n
].Contains(defaultExtension
))
198 fileDialog
.SetFilterIndex(filterIndex
);
202 if ( fileDialog
.ShowModal() == wxID_OK
)
204 filename
= fileDialog
.GetPath();
210 //----------------------------------------------------------------------------
212 //----------------------------------------------------------------------------
214 wxString
wxFileSelectorEx(const wxChar
*title
,
215 const wxChar
*defaultDir
,
216 const wxChar
*defaultFileName
,
217 int* defaultFilterIndex
,
218 const wxChar
*filter
,
225 wxFileDialog
fileDialog(parent
,
226 title
? title
: wxEmptyString
,
227 defaultDir
? defaultDir
: wxEmptyString
,
228 defaultFileName
? defaultFileName
: wxEmptyString
,
229 filter
? filter
: wxEmptyString
,
230 flags
, wxPoint(x
, y
));
233 if ( fileDialog
.ShowModal() == wxID_OK
)
235 if ( defaultFilterIndex
)
236 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
238 filename
= fileDialog
.GetPath();
244 //----------------------------------------------------------------------------
245 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
246 //----------------------------------------------------------------------------
248 static wxString
wxDefaultFileSelector(bool load
,
250 const wxChar
*extension
,
251 const wxChar
*default_name
,
257 str
= _("Load %s file");
259 str
= _("Save %s file");
260 prompt
.Printf(str
, what
);
263 const wxChar
*ext
= extension
;
266 if ( *ext
== wxT('.') )
269 wild
.Printf(wxT("*.%s"), ext
);
271 else // no extension specified
273 wild
= wxFileSelectorDefaultWildcardStr
;
276 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
277 load
? wxOPEN
: wxSAVE
, parent
);
280 //----------------------------------------------------------------------------
281 // wxLoadFileSelector
282 //----------------------------------------------------------------------------
284 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
285 const wxChar
*extension
,
286 const wxChar
*default_name
,
289 return wxDefaultFileSelector(true, what
, extension
, default_name
, parent
);
292 //----------------------------------------------------------------------------
293 // wxSaveFileSelector
294 //----------------------------------------------------------------------------
296 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
297 const wxChar
*extension
,
298 const wxChar
*default_name
,
301 return wxDefaultFileSelector(false, what
, extension
, default_name
, parent
);
304 #endif // wxUSE_FILEDLG