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
))
50 m_fileName
= defaultFile
;
51 m_wildCard
= wildCard
;
52 m_dialogStyle
= style
;
56 if (m_wildCard
.IsEmpty())
57 m_wildCard
= wxFileSelectorDefaultWildcardStr
;
59 // convert m_wildCard from "*.bar" to "Files (*.bar)|*.bar"
60 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
62 m_wildCard
.Printf(_("Files (%s)|%s"),
63 m_wildCard
.c_str(), m_wildCard
.c_str());
67 // Parses the filterStr, returning the number of filters.
68 // Returns 0 if none or if there's a problem.
69 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
71 int wxFileDialogBase::ParseWildcard(const wxString
& filterStr
,
72 wxArrayString
& descriptions
,
73 wxArrayString
& filters
)
78 wxString
str(filterStr
);
80 wxString description
, filter
;
81 for ( int pos
= 0; pos
!= wxNOT_FOUND
; )
83 pos
= str
.Find(wxT('|'));
84 if ( pos
== wxNOT_FOUND
)
86 // if there are no '|'s at all in the string just take the entire
88 if ( filters
.IsEmpty() )
90 descriptions
.Add(filterStr
);
91 filters
.Add(filterStr
);
95 wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
101 description
= str
.Left(pos
);
102 str
= str
.Mid(pos
+ 1);
103 pos
= str
.Find(wxT('|'));
104 if ( pos
== wxNOT_FOUND
)
110 filter
= str
.Left(pos
);
111 str
= str
.Mid(pos
+ 1);
114 descriptions
.Add(description
);
118 return filters
.GetCount();
121 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
122 const wxString
&extensionList
)
124 // strip off path, to avoid problems with "path.bar/foo"
125 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
127 // if fileName is of form "foo.bar" it's ok, return it
128 int idx_dot
= fileName
.Find(wxT('.'), TRUE
);
129 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.Len() - 1))
132 // get the first extension from extensionList, or all of it
133 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
135 // if ext == "foo" or "foo." there's no extension
136 int idx_ext_dot
= ext
.Find(wxT('.'), TRUE
);
137 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.Len() - 1))
140 ext
= ext
.AfterLast(wxT('.'));
142 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
143 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
144 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
145 (ext
.Strip(wxString::both
).IsEmpty()))
148 // if fileName doesn't have a '.' then add one
149 if (filePath
.Last() != wxT('.'))
150 ext
= wxT(".") + ext
;
152 return filePath
+ ext
;
155 //----------------------------------------------------------------------------
156 // wxFileDialog convenience functions
157 //----------------------------------------------------------------------------
159 wxString
wxFileSelector(const wxChar
*title
,
160 const wxChar
*defaultDir
,
161 const wxChar
*defaultFileName
,
162 const wxChar
*defaultExtension
,
163 const wxChar
*filter
,
168 // The defaultExtension, if non-NULL, is
169 // appended to the filename if the user fails to type an extension. The new
170 // implementation (taken from wxFileSelectorEx) appends the extension
171 // automatically, by looking at the filter specification. In fact this
172 // should be better than the native Microsoft implementation because
173 // Windows only allows *one* default extension, whereas here we do the
174 // right thing depending on the filter the user has chosen.
176 // If there's a default extension specified but no filter, we create a
180 if ( defaultExtension
&& !filter
)
181 filter2
= wxString(wxT("*.")) + defaultExtension
;
185 wxString defaultDirString
;
187 defaultDirString
= defaultDir
;
189 wxString defaultFilenameString
;
191 defaultFilenameString
= defaultFileName
;
193 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
194 defaultFilenameString
, filter2
,
195 flags
, wxPoint(x
, y
));
197 // if filter is of form "All files (*)|*|..." set correct filter index
198 if((wxStrlen(defaultExtension
) != 0) && (filter2
.Find(wxT('|')) != wxNOT_FOUND
))
202 wxArrayString descriptions
, filters
;
203 // don't care about errors, handled already by wxFileDialog
204 (void)wxFileDialogBase::ParseWildcard(filter2
, descriptions
, filters
);
205 for (size_t n
=0; n
<filters
.GetCount(); n
++)
207 if (filters
[n
].Contains(defaultExtension
))
215 fileDialog
.SetFilterIndex(filterIndex
);
219 if ( fileDialog
.ShowModal() == wxID_OK
)
221 filename
= fileDialog
.GetPath();
227 //----------------------------------------------------------------------------
229 //----------------------------------------------------------------------------
231 wxString
wxFileSelectorEx(const wxChar
*title
,
232 const wxChar
*defaultDir
,
233 const wxChar
*defaultFileName
,
234 int* defaultFilterIndex
,
235 const wxChar
*filter
,
242 wxFileDialog
fileDialog(parent
,
243 title
? title
: wxT(""),
244 defaultDir
? defaultDir
: wxT(""),
245 defaultFileName
? defaultFileName
: wxT(""),
246 filter
? filter
: wxT(""),
247 flags
, wxPoint(x
, y
));
250 if ( fileDialog
.ShowModal() == wxID_OK
)
252 if ( defaultFilterIndex
)
253 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
255 filename
= fileDialog
.GetPath();
261 //----------------------------------------------------------------------------
262 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
263 //----------------------------------------------------------------------------
265 static wxString
wxDefaultFileSelector(bool load
,
267 const wxChar
*extension
,
268 const wxChar
*default_name
,
274 str
= _("Load %s file");
276 str
= _("Save %s file");
277 prompt
.Printf(str
, what
);
280 const wxChar
*ext
= extension
;
283 if ( *ext
== wxT('.') )
286 wild
.Printf(wxT("*.%s"), ext
);
288 else // no extension specified
290 wild
= wxFileSelectorDefaultWildcardStr
;
293 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
294 load
? wxOPEN
: wxSAVE
, parent
);
297 //----------------------------------------------------------------------------
298 // wxLoadFileSelector
299 //----------------------------------------------------------------------------
301 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
302 const wxChar
*extension
,
303 const wxChar
*default_name
,
306 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
309 //----------------------------------------------------------------------------
310 // wxSaveFileSelector
311 //----------------------------------------------------------------------------
313 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
314 const wxChar
*extension
,
315 const wxChar
*default_name
,
318 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);
321 #endif // wxUSE_FILEDLG