1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 /////////////////////////////////////////////////////////////////////////////
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
21 #include "wx/filedlg.h"
24 #include "wx/string.h"
26 #include "wx/window.h"
29 //----------------------------------------------------------------------------
31 //----------------------------------------------------------------------------
33 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase
, wxDialog
)
35 void wxFileDialogBase::Init()
41 bool wxFileDialogBase::Create(wxWindow
*parent
,
42 const wxString
& message
,
43 const wxString
& defaultDir
,
44 const wxString
& defaultFile
,
45 const wxString
& wildCard
,
47 const wxPoint
& WXUNUSED(pos
),
48 const wxSize
& WXUNUSED(sz
),
49 const wxString
& WXUNUSED(name
))
53 m_fileName
= defaultFile
;
54 m_wildCard
= wildCard
;
57 m_windowStyle
= style
;
60 if (!HasFlag(wxFD_OPEN
) && !HasFlag(wxFD_SAVE
))
61 m_windowStyle
|= wxFD_OPEN
; // wxFD_OPEN is the default
63 // check that the styles are not contradictory
64 wxASSERT_MSG( !(HasFlag(wxFD_SAVE
) && HasFlag(wxFD_OPEN
)),
65 _T("can't specify both wxFD_SAVE and wxFD_OPEN at once") );
67 wxASSERT_MSG( !HasFlag(wxFD_SAVE
) ||
68 (!HasFlag(wxFD_MULTIPLE
) && !HasFlag(wxFD_FILE_MUST_EXIST
)),
69 _T("wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST can't be used with wxFD_SAVE" ) );
71 wxASSERT_MSG( !HasFlag(wxFD_OPEN
) || !HasFlag(wxFD_OVERWRITE_PROMPT
),
72 _T("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
74 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
76 m_wildCard
= wxString::Format(_("All files (%s)|%s"),
77 wxFileSelectorDefaultWildcardStr
,
78 wxFileSelectorDefaultWildcardStr
);
80 else // have wild card
82 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
83 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
85 wxString::size_type nDot
= m_wildCard
.find(_T("*."));
86 if ( nDot
!= wxString::npos
)
91 m_wildCard
= wxString::Format
93 _("%s files (%s)|%s"),
94 wildCard
.c_str() + nDot
,
104 #if WXWIN_COMPATIBILITY_2_4
105 // Parses the filterStr, returning the number of filters.
106 // Returns 0 if none or if there's a problem.
107 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
108 int wxFileDialogBase::ParseWildcard(const wxString
& filterStr
,
109 wxArrayString
& descriptions
,
110 wxArrayString
& filters
)
112 return ::wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
114 #endif // WXWIN_COMPATIBILITY_2_4
116 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
117 const wxString
&extensionList
)
119 // strip off path, to avoid problems with "path.bar/foo"
120 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
122 // if fileName is of form "foo.bar" it's ok, return it
123 int idx_dot
= fileName
.Find(wxT('.'), true);
124 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.length() - 1))
127 // get the first extension from extensionList, or all of it
128 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
130 // if ext == "foo" or "foo." there's no extension
131 int idx_ext_dot
= ext
.Find(wxT('.'), true);
132 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.length() - 1))
135 ext
= ext
.AfterLast(wxT('.'));
137 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
138 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
139 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
140 (ext
.Strip(wxString::both
).empty()))
143 // if fileName doesn't have a '.' then add one
144 if (filePath
.Last() != wxT('.'))
145 ext
= wxT(".") + ext
;
147 return filePath
+ ext
;
150 //----------------------------------------------------------------------------
151 // wxFileDialog convenience functions
152 //----------------------------------------------------------------------------
154 wxString
wxFileSelector(const wxChar
*title
,
155 const wxChar
*defaultDir
,
156 const wxChar
*defaultFileName
,
157 const wxChar
*defaultExtension
,
158 const wxChar
*filter
,
163 // The defaultExtension, if non-NULL, is
164 // appended to the filename if the user fails to type an extension. The new
165 // implementation (taken from wxFileSelectorEx) appends the extension
166 // automatically, by looking at the filter specification. In fact this
167 // should be better than the native Microsoft implementation because
168 // Windows only allows *one* default extension, whereas here we do the
169 // right thing depending on the filter the user has chosen.
171 // If there's a default extension specified but no filter, we create a
175 if ( defaultExtension
&& !filter
)
176 filter2
= wxString(wxT("*.")) + defaultExtension
;
180 wxString defaultDirString
;
182 defaultDirString
= defaultDir
;
184 wxString defaultFilenameString
;
186 defaultFilenameString
= defaultFileName
;
188 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
189 defaultFilenameString
, filter2
,
190 flags
, wxPoint(x
, y
));
192 // if filter is of form "All files (*)|*|..." set correct filter index
193 if((wxStrlen(defaultExtension
) != 0) && (filter2
.Find(wxT('|')) != wxNOT_FOUND
))
197 wxArrayString descriptions
, filters
;
198 // don't care about errors, handled already by wxFileDialog
199 (void)wxParseCommonDialogsFilter(filter2
, descriptions
, filters
);
200 for (size_t n
=0; n
<filters
.GetCount(); n
++)
202 if (filters
[n
].Contains(defaultExtension
))
210 fileDialog
.SetFilterIndex(filterIndex
);
214 if ( fileDialog
.ShowModal() == wxID_OK
)
216 filename
= fileDialog
.GetPath();
222 //----------------------------------------------------------------------------
224 //----------------------------------------------------------------------------
226 wxString
wxFileSelectorEx(const wxChar
*title
,
227 const wxChar
*defaultDir
,
228 const wxChar
*defaultFileName
,
229 int* defaultFilterIndex
,
230 const wxChar
*filter
,
237 wxFileDialog
fileDialog(parent
,
238 title
? title
: wxEmptyString
,
239 defaultDir
? defaultDir
: wxEmptyString
,
240 defaultFileName
? defaultFileName
: wxEmptyString
,
241 filter
? filter
: wxEmptyString
,
242 flags
, wxPoint(x
, y
));
245 if ( fileDialog
.ShowModal() == wxID_OK
)
247 if ( defaultFilterIndex
)
248 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
250 filename
= fileDialog
.GetPath();
256 //----------------------------------------------------------------------------
257 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
258 //----------------------------------------------------------------------------
260 static wxString
wxDefaultFileSelector(bool load
,
262 const wxChar
*extension
,
263 const wxChar
*default_name
,
269 str
= _("Load %s file");
271 str
= _("Save %s file");
272 prompt
.Printf(str
, what
);
275 const wxChar
*ext
= extension
;
278 if ( *ext
== wxT('.') )
281 wild
.Printf(wxT("*.%s"), ext
);
283 else // no extension specified
285 wild
= wxFileSelectorDefaultWildcardStr
;
288 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
289 load
? wxFD_OPEN
: wxFD_SAVE
, parent
);
292 //----------------------------------------------------------------------------
293 // wxLoadFileSelector
294 //----------------------------------------------------------------------------
296 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
297 const wxChar
*extension
,
298 const wxChar
*default_name
,
301 return wxDefaultFileSelector(true, what
, extension
, default_name
, parent
);
304 //----------------------------------------------------------------------------
305 // wxSaveFileSelector
306 //----------------------------------------------------------------------------
308 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
309 const wxChar
*extension
,
310 const wxChar
*default_name
,
313 return wxDefaultFileSelector(false, what
, extension
, default_name
, parent
);
316 #endif // wxUSE_FILEDLG