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 ()
43 wxFileDialogBase::wxFileDialogBase(wxWindow
*parent
,
44 const wxString
& message
,
45 const wxString
& defaultDir
,
46 const wxString
& defaultFile
,
47 const wxString
& wildCard
,
49 const wxPoint
& WXUNUSED(pos
))
54 m_fileName
= defaultFile
;
55 m_wildCard
= wildCard
;
56 m_dialogStyle
= style
;
60 if (m_wildCard
.IsEmpty())
61 m_wildCard
= wxFileSelectorDefaultWildcardStr
;
63 // convert m_wildCard from "*.bar" to "Files (*.bar)|*.bar"
64 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
66 m_wildCard
.Printf(_("Files (%s)|%s"),
67 m_wildCard
.c_str(), m_wildCard
.c_str());
71 // Parses the filterStr, returning the number of filters.
72 // Returns 0 if none or if there's a problem.
73 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
75 int wxFileDialogBase::ParseWildcard(const wxString
& filterStr
,
76 wxArrayString
& descriptions
,
77 wxArrayString
& filters
)
82 wxString
str(filterStr
);
84 wxString description
, filter
;
85 for ( int pos
= 0; pos
!= wxNOT_FOUND
; )
87 pos
= str
.Find(wxT('|'));
88 if ( pos
== wxNOT_FOUND
)
90 // if there are no '|'s at all in the string just take the entire
92 if ( filters
.IsEmpty() )
94 descriptions
.Add(filterStr
);
95 filters
.Add(filterStr
);
99 wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
105 description
= str
.Left(pos
);
106 str
= str
.Mid(pos
+ 1);
107 pos
= str
.Find(wxT('|'));
108 if ( pos
== wxNOT_FOUND
)
114 filter
= str
.Left(pos
);
115 str
= str
.Mid(pos
+ 1);
118 descriptions
.Add(description
);
122 return filters
.GetCount();
125 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
126 const wxString
&extensionList
)
128 // strip off path, to avoid problems with "path.bar/foo"
129 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
131 // if fileName is of form "foo.bar" it's ok, return it
132 int idx_dot
= fileName
.Find(wxT('.'), TRUE
);
133 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.Len() - 1))
136 // get the first extension from extensionList, or all of it
137 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
139 // if ext == "foo" or "foo." there's no extension
140 int idx_ext_dot
= ext
.Find(wxT('.'), TRUE
);
141 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.Len() - 1))
144 ext
= ext
.AfterLast(wxT('.'));
146 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
147 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
148 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
149 (ext
.Strip(wxString::both
).IsEmpty()))
152 // if fileName doesn't have a '.' then add one
153 if (filePath
.Last() != wxT('.'))
154 ext
= wxT(".") + ext
;
156 return filePath
+ ext
;
159 //----------------------------------------------------------------------------
160 // wxFileDialog convenience functions
161 //----------------------------------------------------------------------------
163 wxString
wxFileSelector(const wxChar
*title
,
164 const wxChar
*defaultDir
,
165 const wxChar
*defaultFileName
,
166 const wxChar
*defaultExtension
,
167 const wxChar
*filter
,
172 // The defaultExtension, if non-NULL, is
173 // appended to the filename if the user fails to type an extension. The new
174 // implementation (taken from wxFileSelectorEx) appends the extension
175 // automatically, by looking at the filter specification. In fact this
176 // should be better than the native Microsoft implementation because
177 // Windows only allows *one* default extension, whereas here we do the
178 // right thing depending on the filter the user has chosen.
180 // If there's a default extension specified but no filter, we create a
184 if ( defaultExtension
&& !filter
)
185 filter2
= wxString(wxT("*.")) + defaultExtension
;
189 wxString defaultDirString
;
191 defaultDirString
= defaultDir
;
193 wxString defaultFilenameString
;
195 defaultFilenameString
= defaultFileName
;
197 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
198 defaultFilenameString
, filter2
,
199 flags
, wxPoint(x
, y
));
201 // if filter is of form "All files (*)|*|..." set correct filter index
202 if((wxStrlen(defaultExtension
) != 0) && (filter2
.Find(wxT('|')) != wxNOT_FOUND
))
206 wxArrayString descriptions
, filters
;
207 // don't care about errors, handled already by wxFileDialog
208 (void)wxFileDialogBase::ParseWildcard(filter2
, descriptions
, filters
);
209 for (size_t n
=0; n
<filters
.GetCount(); n
++)
211 if (filters
[n
].Contains(defaultExtension
))
219 fileDialog
.SetFilterIndex(filterIndex
);
223 if ( fileDialog
.ShowModal() == wxID_OK
)
225 filename
= fileDialog
.GetPath();
231 //----------------------------------------------------------------------------
233 //----------------------------------------------------------------------------
235 wxString
wxFileSelectorEx(const wxChar
*title
,
236 const wxChar
*defaultDir
,
237 const wxChar
*defaultFileName
,
238 int* defaultFilterIndex
,
239 const wxChar
*filter
,
246 wxFileDialog
fileDialog(parent
,
247 title
? title
: wxT(""),
248 defaultDir
? defaultDir
: wxT(""),
249 defaultFileName
? defaultFileName
: wxT(""),
250 filter
? filter
: wxT(""),
251 flags
, wxPoint(x
, y
));
254 if ( fileDialog
.ShowModal() == wxID_OK
)
256 if ( defaultFilterIndex
)
257 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
259 filename
= fileDialog
.GetPath();
265 //----------------------------------------------------------------------------
266 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
267 //----------------------------------------------------------------------------
269 static wxString
wxDefaultFileSelector(bool load
,
271 const wxChar
*extension
,
272 const wxChar
*default_name
,
278 str
= _("Load %s file");
280 str
= _("Save %s file");
281 prompt
.Printf(str
, what
);
284 const wxChar
*ext
= extension
;
287 if ( *ext
== wxT('.') )
290 wild
.Printf(wxT("*.%s"), ext
);
292 else // no extension specified
294 wild
= wxFileSelectorDefaultWildcardStr
;
297 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
298 load
? wxOPEN
: wxSAVE
, parent
);
301 //----------------------------------------------------------------------------
302 // wxLoadFileSelector
303 //----------------------------------------------------------------------------
305 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
306 const wxChar
*extension
,
307 const wxChar
*default_name
,
310 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
313 //----------------------------------------------------------------------------
314 // wxSaveFileSelector
315 //----------------------------------------------------------------------------
317 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
318 const wxChar
*extension
,
319 const wxChar
*default_name
,
322 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);
325 #endif // wxUSE_FILEDLG