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 /////////////////////////////////////////////////////////////////////////////
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #include "wx/string.h"
22 #include "wx/window.h"
25 #include "wx/filedlg.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
;
61 // check the given styles
62 wxASSERT_MSG(HasFlag(wxFD_OPEN
) || HasFlag(wxFD_SAVE
), wxT("You must specify one of wxFD_OPEN and wxFD_SAVE styles"));
63 if (HasFlag(wxFD_SAVE
))
64 wxASSERT_MSG( !HasFlag(wxFD_OPEN
) && !HasFlag(wxFD_MULTIPLE
) && !HasFlag(wxFD_FILE_MUST_EXIST
),
65 wxT("wxFileDialog - wxFD_OPEN, wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST used on a save dialog" ) );
66 if (HasFlag(wxFD_OPEN
))
67 wxASSERT_MSG( !HasFlag(wxFD_SAVE
) && !HasFlag(wxFD_OVERWRITE_PROMPT
),
68 wxT("wxFileDialog - wxFD_SAVE or wxFD_OVERWRITE_PROMPT used on a open dialog" ) );
71 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
73 m_wildCard
= wxString::Format(_("All files (%s)|%s"),
74 wxFileSelectorDefaultWildcardStr
,
75 wxFileSelectorDefaultWildcardStr
);
77 else // have wild card
79 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
80 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
82 wxString::size_type nDot
= m_wildCard
.find(_T("*."));
83 if ( nDot
!= wxString::npos
)
88 m_wildCard
= wxString::Format
90 _("%s files (%s)|%s"),
91 wildCard
.c_str() + nDot
,
101 #if WXWIN_COMPATIBILITY_2_4
102 // Parses the filterStr, returning the number of filters.
103 // Returns 0 if none or if there's a problem.
104 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
105 int wxFileDialogBase::ParseWildcard(const wxString
& filterStr
,
106 wxArrayString
& descriptions
,
107 wxArrayString
& filters
)
109 return ::wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
111 #endif // WXWIN_COMPATIBILITY_2_4
113 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
114 const wxString
&extensionList
)
116 // strip off path, to avoid problems with "path.bar/foo"
117 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
119 // if fileName is of form "foo.bar" it's ok, return it
120 int idx_dot
= fileName
.Find(wxT('.'), true);
121 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.Len() - 1))
124 // get the first extension from extensionList, or all of it
125 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
127 // if ext == "foo" or "foo." there's no extension
128 int idx_ext_dot
= ext
.Find(wxT('.'), true);
129 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.Len() - 1))
132 ext
= ext
.AfterLast(wxT('.'));
134 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
135 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
136 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
137 (ext
.Strip(wxString::both
).empty()))
140 // if fileName doesn't have a '.' then add one
141 if (filePath
.Last() != wxT('.'))
142 ext
= wxT(".") + ext
;
144 return filePath
+ ext
;
147 //----------------------------------------------------------------------------
148 // wxFileDialog convenience functions
149 //----------------------------------------------------------------------------
151 wxString
wxFileSelector(const wxChar
*title
,
152 const wxChar
*defaultDir
,
153 const wxChar
*defaultFileName
,
154 const wxChar
*defaultExtension
,
155 const wxChar
*filter
,
160 // The defaultExtension, if non-NULL, is
161 // appended to the filename if the user fails to type an extension. The new
162 // implementation (taken from wxFileSelectorEx) appends the extension
163 // automatically, by looking at the filter specification. In fact this
164 // should be better than the native Microsoft implementation because
165 // Windows only allows *one* default extension, whereas here we do the
166 // right thing depending on the filter the user has chosen.
168 // If there's a default extension specified but no filter, we create a
172 if ( defaultExtension
&& !filter
)
173 filter2
= wxString(wxT("*.")) + defaultExtension
;
177 wxString defaultDirString
;
179 defaultDirString
= defaultDir
;
181 wxString defaultFilenameString
;
183 defaultFilenameString
= defaultFileName
;
185 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
186 defaultFilenameString
, filter2
,
187 flags
, wxPoint(x
, y
));
189 // if filter is of form "All files (*)|*|..." set correct filter index
190 if((wxStrlen(defaultExtension
) != 0) && (filter2
.Find(wxT('|')) != wxNOT_FOUND
))
194 wxArrayString descriptions
, filters
;
195 // don't care about errors, handled already by wxFileDialog
196 (void)wxParseCommonDialogsFilter(filter2
, descriptions
, filters
);
197 for (size_t n
=0; n
<filters
.GetCount(); n
++)
199 if (filters
[n
].Contains(defaultExtension
))
207 fileDialog
.SetFilterIndex(filterIndex
);
211 if ( fileDialog
.ShowModal() == wxID_OK
)
213 filename
= fileDialog
.GetPath();
219 //----------------------------------------------------------------------------
221 //----------------------------------------------------------------------------
223 wxString
wxFileSelectorEx(const wxChar
*title
,
224 const wxChar
*defaultDir
,
225 const wxChar
*defaultFileName
,
226 int* defaultFilterIndex
,
227 const wxChar
*filter
,
234 wxFileDialog
fileDialog(parent
,
235 title
? title
: wxEmptyString
,
236 defaultDir
? defaultDir
: wxEmptyString
,
237 defaultFileName
? defaultFileName
: wxEmptyString
,
238 filter
? filter
: wxEmptyString
,
239 flags
, wxPoint(x
, y
));
242 if ( fileDialog
.ShowModal() == wxID_OK
)
244 if ( defaultFilterIndex
)
245 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
247 filename
= fileDialog
.GetPath();
253 //----------------------------------------------------------------------------
254 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
255 //----------------------------------------------------------------------------
257 static wxString
wxDefaultFileSelector(bool load
,
259 const wxChar
*extension
,
260 const wxChar
*default_name
,
266 str
= _("Load %s file");
268 str
= _("Save %s file");
269 prompt
.Printf(str
, what
);
272 const wxChar
*ext
= extension
;
275 if ( *ext
== wxT('.') )
278 wild
.Printf(wxT("*.%s"), ext
);
280 else // no extension specified
282 wild
= wxFileSelectorDefaultWildcardStr
;
285 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
286 load
? wxFD_OPEN
: wxFD_SAVE
, parent
);
289 //----------------------------------------------------------------------------
290 // wxLoadFileSelector
291 //----------------------------------------------------------------------------
293 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
294 const wxChar
*extension
,
295 const wxChar
*default_name
,
298 return wxDefaultFileSelector(true, what
, extension
, default_name
, parent
);
301 //----------------------------------------------------------------------------
302 // wxSaveFileSelector
303 //----------------------------------------------------------------------------
305 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
306 const wxChar
*extension
,
307 const wxChar
*default_name
,
310 return wxDefaultFileSelector(false, what
, extension
, default_name
, parent
);
313 #endif // wxUSE_FILEDLG