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"
22 #include "wx/dirdlg.h"
25 #include "wx/string.h"
27 #include "wx/window.h"
30 //----------------------------------------------------------------------------
32 //----------------------------------------------------------------------------
34 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase
, wxDialog
)
36 void wxFileDialogBase::Init()
42 bool wxFileDialogBase::Create(wxWindow
*parent
,
43 const wxString
& message
,
44 const wxString
& defaultDir
,
45 const wxString
& defaultFile
,
46 const wxString
& wildCard
,
48 const wxPoint
& WXUNUSED(pos
),
49 const wxSize
& WXUNUSED(sz
),
50 const wxString
& WXUNUSED(name
))
54 m_fileName
= defaultFile
;
55 m_wildCard
= wildCard
;
58 m_windowStyle
= style
;
61 if (!HasFdFlag(wxFD_OPEN
) && !HasFdFlag(wxFD_SAVE
))
62 m_windowStyle
|= wxFD_OPEN
; // wxFD_OPEN is the default
64 // check that the styles are not contradictory
65 wxASSERT_MSG( !(HasFdFlag(wxFD_SAVE
) && HasFdFlag(wxFD_OPEN
)),
66 _T("can't specify both wxFD_SAVE and wxFD_OPEN at once") );
68 wxASSERT_MSG( !HasFdFlag(wxFD_SAVE
) ||
69 (!HasFdFlag(wxFD_MULTIPLE
) && !HasFdFlag(wxFD_FILE_MUST_EXIST
)),
70 _T("wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST can't be used with wxFD_SAVE" ) );
72 wxASSERT_MSG( !HasFdFlag(wxFD_OPEN
) || !HasFdFlag(wxFD_OVERWRITE_PROMPT
),
73 _T("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
75 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
77 m_wildCard
= wxString::Format(_("All files (%s)|%s"),
78 wxFileSelectorDefaultWildcardStr
,
79 wxFileSelectorDefaultWildcardStr
);
81 else // have wild card
83 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
84 if ( m_wildCard
.Find(wxT('|')) == wxNOT_FOUND
)
86 wxString::size_type nDot
= m_wildCard
.find(_T("*."));
87 if ( nDot
!= wxString::npos
)
92 m_wildCard
= wxString::Format
94 _("%s files (%s)|%s"),
95 wildCard
.c_str() + nDot
,
105 #if WXWIN_COMPATIBILITY_2_4
106 // Parses the filterStr, returning the number of filters.
107 // Returns 0 if none or if there's a problem.
108 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
109 int wxFileDialogBase::ParseWildcard(const wxString
& filterStr
,
110 wxArrayString
& descriptions
,
111 wxArrayString
& filters
)
113 return ::wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
115 #endif // WXWIN_COMPATIBILITY_2_4
117 #if WXWIN_COMPATIBILITY_2_6
118 long wxFileDialogBase::GetStyle() const
120 return GetWindowStyle();
123 void wxFileDialogBase::SetStyle(long style
)
125 SetWindowStyle(style
);
127 #endif // WXWIN_COMPATIBILITY_2_6
130 wxString
wxFileDialogBase::AppendExtension(const wxString
&filePath
,
131 const wxString
&extensionList
)
133 // strip off path, to avoid problems with "path.bar/foo"
134 wxString fileName
= filePath
.AfterLast(wxFILE_SEP_PATH
);
136 // if fileName is of form "foo.bar" it's ok, return it
137 int idx_dot
= fileName
.Find(wxT('.'), true);
138 if ((idx_dot
!= wxNOT_FOUND
) && (idx_dot
< (int)fileName
.length() - 1))
141 // get the first extension from extensionList, or all of it
142 wxString ext
= extensionList
.BeforeFirst(wxT(';'));
144 // if ext == "foo" or "foo." there's no extension
145 int idx_ext_dot
= ext
.Find(wxT('.'), true);
146 if ((idx_ext_dot
== wxNOT_FOUND
) || (idx_ext_dot
== (int)ext
.length() - 1))
149 ext
= ext
.AfterLast(wxT('.'));
151 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
152 if ((ext
.Find(wxT('*')) != wxNOT_FOUND
) ||
153 (ext
.Find(wxT('?')) != wxNOT_FOUND
) ||
154 (ext
.Strip(wxString::both
).empty()))
157 // if fileName doesn't have a '.' then add one
158 if (filePath
.Last() != wxT('.'))
159 ext
= wxT(".") + ext
;
161 return filePath
+ ext
;
164 //----------------------------------------------------------------------------
165 // wxFileDialog convenience functions
166 //----------------------------------------------------------------------------
168 wxString
wxFileSelector(const wxChar
*title
,
169 const wxChar
*defaultDir
,
170 const wxChar
*defaultFileName
,
171 const wxChar
*defaultExtension
,
172 const wxChar
*filter
,
177 // The defaultExtension, if non-NULL, is
178 // appended to the filename if the user fails to type an extension. The new
179 // implementation (taken from wxFileSelectorEx) appends the extension
180 // automatically, by looking at the filter specification. In fact this
181 // should be better than the native Microsoft implementation because
182 // Windows only allows *one* default extension, whereas here we do the
183 // right thing depending on the filter the user has chosen.
185 // If there's a default extension specified but no filter, we create a
189 if ( defaultExtension
&& !filter
)
190 filter2
= wxString(wxT("*.")) + defaultExtension
;
194 wxString defaultDirString
;
196 defaultDirString
= defaultDir
;
198 wxString defaultFilenameString
;
200 defaultFilenameString
= defaultFileName
;
202 wxFileDialog
fileDialog(parent
, title
, defaultDirString
,
203 defaultFilenameString
, filter2
,
204 flags
, wxPoint(x
, y
));
206 // if filter is of form "All files (*)|*|..." set correct filter index
207 if((wxStrlen(defaultExtension
) != 0) && (filter2
.Find(wxT('|')) != wxNOT_FOUND
))
211 wxArrayString descriptions
, filters
;
212 // don't care about errors, handled already by wxFileDialog
213 (void)wxParseCommonDialogsFilter(filter2
, descriptions
, filters
);
214 for (size_t n
=0; n
<filters
.GetCount(); n
++)
216 if (filters
[n
].Contains(defaultExtension
))
224 fileDialog
.SetFilterIndex(filterIndex
);
228 if ( fileDialog
.ShowModal() == wxID_OK
)
230 filename
= fileDialog
.GetPath();
236 //----------------------------------------------------------------------------
238 //----------------------------------------------------------------------------
240 wxString
wxFileSelectorEx(const wxChar
*title
,
241 const wxChar
*defaultDir
,
242 const wxChar
*defaultFileName
,
243 int* defaultFilterIndex
,
244 const wxChar
*filter
,
251 wxFileDialog
fileDialog(parent
,
252 title
? title
: wxEmptyString
,
253 defaultDir
? defaultDir
: wxEmptyString
,
254 defaultFileName
? defaultFileName
: wxEmptyString
,
255 filter
? filter
: wxEmptyString
,
256 flags
, wxPoint(x
, y
));
259 if ( fileDialog
.ShowModal() == wxID_OK
)
261 if ( defaultFilterIndex
)
262 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
264 filename
= fileDialog
.GetPath();
270 //----------------------------------------------------------------------------
271 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
272 //----------------------------------------------------------------------------
274 static wxString
wxDefaultFileSelector(bool load
,
276 const wxChar
*extension
,
277 const wxChar
*default_name
,
283 str
= _("Load %s file");
285 str
= _("Save %s file");
286 prompt
.Printf(str
, what
);
289 const wxChar
*ext
= extension
;
292 if ( *ext
== wxT('.') )
295 wild
.Printf(wxT("*.%s"), ext
);
297 else // no extension specified
299 wild
= wxFileSelectorDefaultWildcardStr
;
302 return wxFileSelector(prompt
, NULL
, default_name
, ext
, wild
,
303 load
? wxFD_OPEN
: wxFD_SAVE
, parent
);
306 //----------------------------------------------------------------------------
307 // wxLoadFileSelector
308 //----------------------------------------------------------------------------
310 WXDLLEXPORT wxString
wxLoadFileSelector(const wxChar
*what
,
311 const wxChar
*extension
,
312 const wxChar
*default_name
,
315 return wxDefaultFileSelector(true, what
, extension
, default_name
, parent
);
318 //----------------------------------------------------------------------------
319 // wxSaveFileSelector
320 //----------------------------------------------------------------------------
322 WXDLLEXPORT wxString
wxSaveFileSelector(const wxChar
*what
,
323 const wxChar
*extension
,
324 const wxChar
*default_name
,
327 return wxDefaultFileSelector(false, what
, extension
, default_name
, parent
);
331 //----------------------------------------------------------------------------
333 //----------------------------------------------------------------------------
335 #if WXWIN_COMPATIBILITY_2_6
336 long wxDirDialogBase::GetStyle() const
338 return GetWindowStyle();
341 void wxDirDialogBase::SetStyle(long style
)
343 SetWindowStyle(style
);
345 #endif // WXWIN_COMPATIBILITY_2_6
348 #endif // wxUSE_FILEDLG