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
)
51 m_dialogStyle
= style
;
54 if ( wildCard
.empty() )
56 m_wildCard
= wxString::Format(_("All files (%s)|%s"),
57 wxFileSelectorDefaultWildcardStr
,
58 wxFileSelectorDefaultWildcardStr
);
60 else // have wild card
62 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
63 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
65 wxString::size_type nDot
= m_wildCard
.find(_T("*."));
66 if ( nDot
!= wxString::npos
)
71 m_wildCard
= wxString::Format
73 _("%s files (%s)|%s"),
74 m_wildCard
.c_str() + nDot
,
82 // Parses the filterStr, returning the number of filters.
83 // Returns 0 if none or if there's a problem.
84 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
86 int wxFileDialogBase::ParseWildcard(const wxString
& filterStr
,
87 wxArrayString
& descriptions
,
88 wxArrayString
& filters
)
93 wxString
str(filterStr
);
95 wxString description
, filter
;
96 for ( int pos
= 0; pos
!= wxNOT_FOUND
; )
98 pos
= str
.Find(wxT('|'));
99 if ( pos
== wxNOT_FOUND
)
101 // if there are no '|'s at all in the string just take the entire
103 if ( filters
.IsEmpty() )
105 descriptions
.Add(filterStr
);
106 filters
.Add(filterStr
);
110 wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
116 description
= str
.Left(pos
);
117 str
= str
.Mid(pos
+ 1);
118 pos
= str
.Find(wxT('|'));
119 if ( pos
== wxNOT_FOUND
)
125 filter
= str
.Left(pos
);
126 str
= str
.Mid(pos
+ 1);
129 descriptions
.Add(description
);
133 return filters
.GetCount();
136 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
137 const wxString
&extensionList
)
139 // strip off path, to avoid problems with "path.bar/foo"
140 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
142 // if fileName is of form "foo.bar" it's ok, return it
143 int idx_dot
= fileName
.Find(wxT('.'), TRUE
);
144 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.Len() - 1))
147 // get the first extension from extensionList, or all of it
148 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
150 // if ext == "foo" or "foo." there's no extension
151 int idx_ext_dot
= ext
.Find(wxT('.'), TRUE
);
152 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.Len() - 1))
155 ext
= ext
.AfterLast(wxT('.'));
157 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
158 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
159 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
160 (ext
.Strip(wxString::both
).IsEmpty()))
163 // if fileName doesn't have a '.' then add one
164 if (filePath
.Last() != wxT('.'))
165 ext
= wxT(".") + ext
;
167 return filePath
+ ext
;
170 //----------------------------------------------------------------------------
171 // wxFileDialog convenience functions
172 //----------------------------------------------------------------------------
174 wxString
wxFileSelector(const wxChar
*title
,
175 const wxChar
*defaultDir
,
176 const wxChar
*defaultFileName
,
177 const wxChar
*defaultExtension
,
178 const wxChar
*filter
,
183 // The defaultExtension, if non-NULL, is
184 // appended to the filename if the user fails to type an extension. The new
185 // implementation (taken from wxFileSelectorEx) appends the extension
186 // automatically, by looking at the filter specification. In fact this
187 // should be better than the native Microsoft implementation because
188 // Windows only allows *one* default extension, whereas here we do the
189 // right thing depending on the filter the user has chosen.
191 // If there's a default extension specified but no filter, we create a
195 if ( defaultExtension
&& !filter
)
196 filter2
= wxString(wxT("*.")) + defaultExtension
;
200 wxString defaultDirString
;
202 defaultDirString
= defaultDir
;
204 wxString defaultFilenameString
;
206 defaultFilenameString
= defaultFileName
;
208 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
209 defaultFilenameString
, filter2
,
210 flags
, wxPoint(x
, y
));
212 // if filter is of form "All files (*)|*|..." set correct filter index
213 if((wxStrlen(defaultExtension
) != 0) && (filter2
.Find(wxT('|')) != wxNOT_FOUND
))
217 wxArrayString descriptions
, filters
;
218 // don't care about errors, handled already by wxFileDialog
219 (void)wxFileDialogBase::ParseWildcard(filter2
, descriptions
, filters
);
220 for (size_t n
=0; n
<filters
.GetCount(); n
++)
222 if (filters
[n
].Contains(defaultExtension
))
230 fileDialog
.SetFilterIndex(filterIndex
);
234 if ( fileDialog
.ShowModal() == wxID_OK
)
236 filename
= fileDialog
.GetPath();
242 //----------------------------------------------------------------------------
244 //----------------------------------------------------------------------------
246 wxString
wxFileSelectorEx(const wxChar
*title
,
247 const wxChar
*defaultDir
,
248 const wxChar
*defaultFileName
,
249 int* defaultFilterIndex
,
250 const wxChar
*filter
,
257 wxFileDialog
fileDialog(parent
,
258 title
? title
: wxT(""),
259 defaultDir
? defaultDir
: wxT(""),
260 defaultFileName
? defaultFileName
: wxT(""),
261 filter
? filter
: wxT(""),
262 flags
, wxPoint(x
, y
));
265 if ( fileDialog
.ShowModal() == wxID_OK
)
267 if ( defaultFilterIndex
)
268 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
270 filename
= fileDialog
.GetPath();
276 //----------------------------------------------------------------------------
277 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
278 //----------------------------------------------------------------------------
280 static wxString
wxDefaultFileSelector(bool load
,
282 const wxChar
*extension
,
283 const wxChar
*default_name
,
289 str
= _("Load %s file");
291 str
= _("Save %s file");
292 prompt
.Printf(str
, what
);
295 const wxChar
*ext
= extension
;
298 if ( *ext
== wxT('.') )
301 wild
.Printf(wxT("*.%s"), ext
);
303 else // no extension specified
305 wild
= wxFileSelectorDefaultWildcardStr
;
308 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
309 load
? wxOPEN
: wxSAVE
, parent
);
312 //----------------------------------------------------------------------------
313 // wxLoadFileSelector
314 //----------------------------------------------------------------------------
316 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
317 const wxChar
*extension
,
318 const wxChar
*default_name
,
321 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
324 //----------------------------------------------------------------------------
325 // wxSaveFileSelector
326 //----------------------------------------------------------------------------
328 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
329 const wxChar
*extension
,
330 const wxChar
*default_name
,
333 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);
336 #endif // wxUSE_FILEDLG