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 // Parses the filterStr, returning the number of filters.
84 // Returns 0 if none or if there's a problem.
85 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
87 int wxFileDialogBase::ParseWildcard(const wxString
& filterStr
,
88 wxArrayString
& descriptions
,
89 wxArrayString
& filters
)
94 wxString
str(filterStr
);
96 wxString description
, filter
;
97 for ( int pos
= 0; pos
!= wxNOT_FOUND
; )
99 pos
= str
.Find(wxT('|'));
100 if ( pos
== wxNOT_FOUND
)
102 // if there are no '|'s at all in the string just take the entire
104 if ( filters
.IsEmpty() )
106 descriptions
.Add(filterStr
);
107 filters
.Add(filterStr
);
111 wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
117 description
= str
.Left(pos
);
118 str
= str
.Mid(pos
+ 1);
119 pos
= str
.Find(wxT('|'));
120 if ( pos
== wxNOT_FOUND
)
126 filter
= str
.Left(pos
);
127 str
= str
.Mid(pos
+ 1);
130 descriptions
.Add(description
);
134 return filters
.GetCount();
137 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
138 const wxString
&extensionList
)
140 // strip off path, to avoid problems with "path.bar/foo"
141 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
143 // if fileName is of form "foo.bar" it's ok, return it
144 int idx_dot
= fileName
.Find(wxT('.'), TRUE
);
145 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.Len() - 1))
148 // get the first extension from extensionList, or all of it
149 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
151 // if ext == "foo" or "foo." there's no extension
152 int idx_ext_dot
= ext
.Find(wxT('.'), TRUE
);
153 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.Len() - 1))
156 ext
= ext
.AfterLast(wxT('.'));
158 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
159 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
160 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
161 (ext
.Strip(wxString::both
).IsEmpty()))
164 // if fileName doesn't have a '.' then add one
165 if (filePath
.Last() != wxT('.'))
166 ext
= wxT(".") + ext
;
168 return filePath
+ ext
;
171 //----------------------------------------------------------------------------
172 // wxFileDialog convenience functions
173 //----------------------------------------------------------------------------
175 wxString
wxFileSelector(const wxChar
*title
,
176 const wxChar
*defaultDir
,
177 const wxChar
*defaultFileName
,
178 const wxChar
*defaultExtension
,
179 const wxChar
*filter
,
184 // The defaultExtension, if non-NULL, is
185 // appended to the filename if the user fails to type an extension. The new
186 // implementation (taken from wxFileSelectorEx) appends the extension
187 // automatically, by looking at the filter specification. In fact this
188 // should be better than the native Microsoft implementation because
189 // Windows only allows *one* default extension, whereas here we do the
190 // right thing depending on the filter the user has chosen.
192 // If there's a default extension specified but no filter, we create a
196 if ( defaultExtension
&& !filter
)
197 filter2
= wxString(wxT("*.")) + defaultExtension
;
201 wxString defaultDirString
;
203 defaultDirString
= defaultDir
;
205 wxString defaultFilenameString
;
207 defaultFilenameString
= defaultFileName
;
209 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
210 defaultFilenameString
, filter2
,
211 flags
, wxPoint(x
, y
));
213 // if filter is of form "All files (*)|*|..." set correct filter index
214 if((wxStrlen(defaultExtension
) != 0) && (filter2
.Find(wxT('|')) != wxNOT_FOUND
))
218 wxArrayString descriptions
, filters
;
219 // don't care about errors, handled already by wxFileDialog
220 (void)wxFileDialogBase::ParseWildcard(filter2
, descriptions
, filters
);
221 for (size_t n
=0; n
<filters
.GetCount(); n
++)
223 if (filters
[n
].Contains(defaultExtension
))
231 fileDialog
.SetFilterIndex(filterIndex
);
235 if ( fileDialog
.ShowModal() == wxID_OK
)
237 filename
= fileDialog
.GetPath();
243 //----------------------------------------------------------------------------
245 //----------------------------------------------------------------------------
247 wxString
wxFileSelectorEx(const wxChar
*title
,
248 const wxChar
*defaultDir
,
249 const wxChar
*defaultFileName
,
250 int* defaultFilterIndex
,
251 const wxChar
*filter
,
258 wxFileDialog
fileDialog(parent
,
259 title
? title
: wxT(""),
260 defaultDir
? defaultDir
: wxT(""),
261 defaultFileName
? defaultFileName
: wxT(""),
262 filter
? filter
: wxT(""),
263 flags
, wxPoint(x
, y
));
266 if ( fileDialog
.ShowModal() == wxID_OK
)
268 if ( defaultFilterIndex
)
269 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
271 filename
= fileDialog
.GetPath();
277 //----------------------------------------------------------------------------
278 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
279 //----------------------------------------------------------------------------
281 static wxString
wxDefaultFileSelector(bool load
,
283 const wxChar
*extension
,
284 const wxChar
*default_name
,
290 str
= _("Load %s file");
292 str
= _("Save %s file");
293 prompt
.Printf(str
, what
);
296 const wxChar
*ext
= extension
;
299 if ( *ext
== wxT('.') )
302 wild
.Printf(wxT("*.%s"), ext
);
304 else // no extension specified
306 wild
= wxFileSelectorDefaultWildcardStr
;
309 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
310 load
? wxOPEN
: wxSAVE
, parent
);
313 //----------------------------------------------------------------------------
314 // wxLoadFileSelector
315 //----------------------------------------------------------------------------
317 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
318 const wxChar
*extension
,
319 const wxChar
*default_name
,
322 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
325 //----------------------------------------------------------------------------
326 // wxSaveFileSelector
327 //----------------------------------------------------------------------------
329 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
330 const wxChar
*extension
,
331 const wxChar
*default_name
,
334 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);
337 #endif // wxUSE_FILEDLG