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 /////////////////////////////////////////////////////////////////////////////
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 wxString
wxFileSelector(const wxChar
*title
,
34 const wxChar
*defaultDir
,
35 const wxChar
*defaultFileName
,
36 const wxChar
*defaultExtension
,
42 // The defaultExtension, if non-NULL, is
43 // appended to the filename if the user fails to type an extension. The new
44 // implementation (taken from wxFileSelectorEx) appends the extension
45 // automatically, by looking at the filter specification. In fact this
46 // should be better than the native Microsoft implementation because
47 // Windows only allows *one* default extension, whereas here we do the
48 // right thing depending on the filter the user has chosen.
50 // If there's a default extension specified but no filter, we create a
54 if ( defaultExtension
&& !filter
)
55 filter2
= wxString(wxT("*.")) + defaultExtension
;
59 wxString defaultDirString
;
61 defaultDirString
= defaultDir
;
63 wxString defaultFilenameString
;
65 defaultFilenameString
= defaultFileName
;
67 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
68 defaultFilenameString
, filter2
,
69 flags
, wxPoint(x
, y
));
70 if( wxStrlen(defaultExtension
) != 0 )
75 for( unsigned int i
= 0; i
< filter2
.Len(); i
++ )
77 if( filter2
.GetChar(i
) == wxT('|') )
79 // save the start index of the new filter
80 unsigned int is
= i
++;
82 // find the end of the filter
83 for( ; i
< filter2
.Len(); i
++ )
85 if(filter2
[i
] == wxT('|'))
89 if( i
-is
-1 > 0 && is
+1 < filter2
.Len() )
91 if( filter2
.Mid(is
+1,i
-is
-1).Contains(defaultExtension
) )
93 filterFind
= filterIndex
;
102 fileDialog
.SetFilterIndex(filterFind
);
106 if ( fileDialog
.ShowModal() == wxID_OK
)
108 filename
= fileDialog
.GetPath();
116 wxString wxFileSelector( const wxChar *title,
117 const wxChar *defaultDir,
118 const wxChar *defaultFileName,
119 const wxChar *defaultExtension,
120 const wxChar *filter,
127 if ( defaultExtension && !filter )
128 filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ;
132 wxString defaultDirString;
134 defaultDirString = defaultDir;
136 wxString defaultFilenameString;
138 defaultFilenameString = defaultFileName;
140 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
142 if ( fileDialog.ShowModal() == wxID_OK )
144 return fileDialog.GetPath();
148 return wxEmptyString;
154 wxString
wxFileSelectorEx(const wxChar
*title
,
155 const wxChar
*defaultDir
,
156 const wxChar
*defaultFileName
,
157 int* defaultFilterIndex
,
158 const wxChar
*filter
,
165 wxFileDialog
fileDialog(parent
,
166 title
? title
: wxT(""),
167 defaultDir
? defaultDir
: wxT(""),
168 defaultFileName
? defaultFileName
: wxT(""),
169 filter
? filter
: wxT(""),
170 flags
, wxPoint(x
, y
));
173 if ( fileDialog
.ShowModal() == wxID_OK
)
175 if ( defaultFilterIndex
)
176 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
178 filename
= fileDialog
.GetPath();
186 // Generic file load/save dialog (for internal use only)
187 // see wx[Load/Save]FileSelector
188 static wxString
wxDefaultFileSelector(bool load
,
190 const wxChar
*extension
,
191 const wxChar
*default_name
,
197 str
= _("Load %s file");
199 str
= _("Save %s file");
200 prompt
.Printf(str
, what
);
203 const wxChar
*ext
= extension
;
206 if ( *ext
== wxT('.') )
209 wild
.Printf(wxT("*.%s"), ext
);
211 else // no extension specified
213 wild
= wxFileSelectorDefaultWildcardStr
;
216 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
217 load
? wxOPEN
: wxSAVE
, parent
);
220 // Generic file load dialog
221 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
222 const wxChar
*extension
,
223 const wxChar
*default_name
,
226 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
229 // Generic file save dialog
230 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
231 const wxChar
*extension
,
232 const wxChar
*default_name
,
235 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);
239 // Parses the filterStr, returning the number of filters.
240 // Returns 0 if none or if there's a problem.
241 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
243 int wxParseFileFilter(const wxString
& filterStr
,
244 wxArrayString
& descriptions
,
245 wxArrayString
& filters
)
247 descriptions
.Clear();
250 wxString
str(filterStr
);
252 wxString description
, filter
;
253 for ( int pos
= 0; pos
!= wxNOT_FOUND
; )
255 pos
= str
.Find(wxT('|'));
256 if ( pos
== wxNOT_FOUND
)
258 // if there are no '|'s at all in the string just take the entire
260 if ( filters
.IsEmpty() )
262 descriptions
.Add(filterStr
);
263 filters
.Add(filterStr
);
267 wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
273 description
= str
.Left(pos
);
274 str
= str
.Mid(pos
+ 1);
275 pos
= str
.Find(wxT('|'));
276 if ( pos
== wxNOT_FOUND
)
282 filter
= str
.Left(pos
);
283 str
= str
.Mid(pos
+ 1);
286 descriptions
.Add(description
);
290 return filters
.GetCount();
293 #endif // wxUSE_FILEDLG