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 (!HasFdFlag(wxFD_OPEN
) && !HasFdFlag(wxFD_SAVE
))
61 m_windowStyle
|= wxFD_OPEN
; // wxFD_OPEN is the default
63 // check that the styles are not contradictory
64 wxASSERT_MSG( !(HasFdFlag(wxFD_SAVE
) && HasFdFlag(wxFD_OPEN
)),
65 _T("can't specify both wxFD_SAVE and wxFD_OPEN at once") );
67 wxASSERT_MSG( !HasFdFlag(wxFD_SAVE
) ||
68 (!HasFdFlag(wxFD_MULTIPLE
) && !HasFdFlag(wxFD_FILE_MUST_EXIST
)),
69 _T("wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST can't be used with wxFD_SAVE" ) );
71 wxASSERT_MSG( !HasFdFlag(wxFD_OPEN
) || !HasFdFlag(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 #if WXWIN_COMPATIBILITY_2_6
117 long wxFileDialogBase::GetStyle() const
119 return GetWindowStyle();
122 void wxFileDialogBase::SetStyle(long style
)
124 SetWindowStyle(style
);
126 #endif // WXWIN_COMPATIBILITY_2_6
129 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
130 const wxString
&extensionList
)
132 // strip off path, to avoid problems with "path.bar/foo"
133 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
135 // if fileName is of form "foo.bar" it's ok, return it
136 int idx_dot
= fileName
.Find(wxT('.'), true);
137 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.length() - 1))
140 // get the first extension from extensionList, or all of it
141 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
143 // if ext == "foo" or "foo." there's no extension
144 int idx_ext_dot
= ext
.Find(wxT('.'), true);
145 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.length() - 1))
148 ext
= ext
.AfterLast(wxT('.'));
150 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
151 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
152 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
153 (ext
.Strip(wxString::both
).empty()))
156 // if fileName doesn't have a '.' then add one
157 if (filePath
.Last() != wxT('.'))
158 ext
= wxT(".") + ext
;
160 return filePath
+ ext
;
163 //----------------------------------------------------------------------------
164 // wxFileDialog convenience functions
165 //----------------------------------------------------------------------------
167 wxString
wxFileSelector(const wxChar
*title
,
168 const wxChar
*defaultDir
,
169 const wxChar
*defaultFileName
,
170 const wxChar
*defaultExtension
,
171 const wxChar
*filter
,
176 // The defaultExtension, if non-NULL, is
177 // appended to the filename if the user fails to type an extension. The new
178 // implementation (taken from wxFileSelectorEx) appends the extension
179 // automatically, by looking at the filter specification. In fact this
180 // should be better than the native Microsoft implementation because
181 // Windows only allows *one* default extension, whereas here we do the
182 // right thing depending on the filter the user has chosen.
184 // If there's a default extension specified but no filter, we create a
188 if ( defaultExtension
&& !filter
)
189 filter2
= wxString(wxT("*.")) + defaultExtension
;
193 wxString defaultDirString
;
195 defaultDirString
= defaultDir
;
197 wxString defaultFilenameString
;
199 defaultFilenameString
= defaultFileName
;
201 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
202 defaultFilenameString
, filter2
,
203 flags
, wxPoint(x
, y
));
205 // if filter is of form "All files (*)|*|..." set correct filter index
206 if((wxStrlen(defaultExtension
) != 0) && (filter2
.Find(wxT('|')) != wxNOT_FOUND
))
210 wxArrayString descriptions
, filters
;
211 // don't care about errors, handled already by wxFileDialog
212 (void)wxParseCommonDialogsFilter(filter2
, descriptions
, filters
);
213 for (size_t n
=0; n
<filters
.GetCount(); n
++)
215 if (filters
[n
].Contains(defaultExtension
))
223 fileDialog
.SetFilterIndex(filterIndex
);
227 if ( fileDialog
.ShowModal() == wxID_OK
)
229 filename
= fileDialog
.GetPath();
235 //----------------------------------------------------------------------------
237 //----------------------------------------------------------------------------
239 wxString
wxFileSelectorEx(const wxChar
*title
,
240 const wxChar
*defaultDir
,
241 const wxChar
*defaultFileName
,
242 int* defaultFilterIndex
,
243 const wxChar
*filter
,
250 wxFileDialog
fileDialog(parent
,
251 title
? title
: wxEmptyString
,
252 defaultDir
? defaultDir
: wxEmptyString
,
253 defaultFileName
? defaultFileName
: wxEmptyString
,
254 filter
? filter
: wxEmptyString
,
255 flags
, wxPoint(x
, y
));
258 if ( fileDialog
.ShowModal() == wxID_OK
)
260 if ( defaultFilterIndex
)
261 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
263 filename
= fileDialog
.GetPath();
269 //----------------------------------------------------------------------------
270 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
271 //----------------------------------------------------------------------------
273 static wxString
wxDefaultFileSelector(bool load
,
275 const wxChar
*extension
,
276 const wxChar
*default_name
,
282 str
= _("Load %s file");
284 str
= _("Save %s file");
285 prompt
.Printf(str
, what
);
288 const wxChar
*ext
= extension
;
291 if ( *ext
== wxT('.') )
294 wild
.Printf(wxT("*.%s"), ext
);
296 else // no extension specified
298 wild
= wxFileSelectorDefaultWildcardStr
;
301 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
302 load
? wxFD_OPEN
: wxFD_SAVE
, parent
);
305 //----------------------------------------------------------------------------
306 // wxLoadFileSelector
307 //----------------------------------------------------------------------------
309 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
310 const wxChar
*extension
,
311 const wxChar
*default_name
,
314 return wxDefaultFileSelector(true, what
, extension
, default_name
, parent
);
317 //----------------------------------------------------------------------------
318 // wxSaveFileSelector
319 //----------------------------------------------------------------------------
321 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
322 const wxChar
*extension
,
323 const wxChar
*default_name
,
326 return wxDefaultFileSelector(false, what
, extension
, default_name
, parent
);
330 //----------------------------------------------------------------------------
332 //----------------------------------------------------------------------------
334 #if WXWIN_COMPATIBILITY_2_6
335 long wxDirDialogBase::GetStyle() const
337 return GetWindowStyle();
340 void wxDirDialogBase::SetStyle(long style
)
342 SetWindowStyle(style
);
344 #endif // WXWIN_COMPATIBILITY_2_6
347 #endif // wxUSE_FILEDLG