File/dir dialog styles and other changes (patch 1488371):
[wxWidgets.git] / src / common / fldlgcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/fldlgcmn.cpp
3 // Purpose: wxFileDialog common functions
4 // Author: John Labenski
5 // Modified by:
6 // Created: 14.06.03 (extracted from src/*/filedlg.cpp)
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/string.h"
21 #include "wx/intl.h"
22 #include "wx/window.h"
23 #endif // WX_PRECOMP
24
25 #include "wx/filedlg.h"
26
27 #if wxUSE_FILEDLG
28
29 //----------------------------------------------------------------------------
30 // wxFileDialogBase
31 //----------------------------------------------------------------------------
32
33 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase, wxDialog)
34
35 void wxFileDialogBase::Init()
36 {
37 m_filterIndex =
38 m_windowStyle = 0;
39 }
40
41 bool wxFileDialogBase::Create(wxWindow *parent,
42 const wxString& message,
43 const wxString& defaultDir,
44 const wxString& defaultFile,
45 const wxString& wildCard,
46 long style,
47 const wxPoint& WXUNUSED(pos),
48 const wxSize& WXUNUSED(sz),
49 const wxString& WXUNUSED(name))
50 {
51 m_message = message;
52 m_dir = defaultDir;
53 m_fileName = defaultFile;
54 m_wildCard = wildCard;
55
56 m_parent = parent;
57 m_windowStyle = style;
58 m_filterIndex = 0;
59
60 #ifdef __WXDEBUG__
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" ) );
69 #endif
70
71 if ( wildCard.empty() || wildCard == wxFileSelectorDefaultWildcardStr )
72 {
73 m_wildCard = wxString::Format(_("All files (%s)|%s"),
74 wxFileSelectorDefaultWildcardStr,
75 wxFileSelectorDefaultWildcardStr);
76 }
77 else // have wild card
78 {
79 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
80 if ( m_wildCard.Find(wxT('|')) == wxNOT_FOUND )
81 {
82 wxString::size_type nDot = m_wildCard.find(_T("*."));
83 if ( nDot != wxString::npos )
84 nDot++;
85 else
86 nDot = 0;
87
88 m_wildCard = wxString::Format
89 (
90 _("%s files (%s)|%s"),
91 wildCard.c_str() + nDot,
92 wildCard.c_str(),
93 wildCard.c_str()
94 );
95 }
96 }
97
98 return true;
99 }
100
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)
108 {
109 return ::wxParseCommonDialogsFilter(filterStr, descriptions, filters);
110 }
111 #endif // WXWIN_COMPATIBILITY_2_4
112
113 wxString wxFileDialogBase::AppendExtension(const wxString &filePath,
114 const wxString &extensionList)
115 {
116 // strip off path, to avoid problems with "path.bar/foo"
117 wxString fileName = filePath.AfterLast(wxFILE_SEP_PATH);
118
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))
122 return filePath;
123
124 // get the first extension from extensionList, or all of it
125 wxString ext = extensionList.BeforeFirst(wxT(';'));
126
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))
130 return filePath;
131 else
132 ext = ext.AfterLast(wxT('.'));
133
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()))
138 return filePath;
139
140 // if fileName doesn't have a '.' then add one
141 if (filePath.Last() != wxT('.'))
142 ext = wxT(".") + ext;
143
144 return filePath + ext;
145 }
146
147 //----------------------------------------------------------------------------
148 // wxFileDialog convenience functions
149 //----------------------------------------------------------------------------
150
151 wxString wxFileSelector(const wxChar *title,
152 const wxChar *defaultDir,
153 const wxChar *defaultFileName,
154 const wxChar *defaultExtension,
155 const wxChar *filter,
156 int flags,
157 wxWindow *parent,
158 int x, int y)
159 {
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.
167
168 // If there's a default extension specified but no filter, we create a
169 // suitable filter.
170
171 wxString filter2;
172 if ( defaultExtension && !filter )
173 filter2 = wxString(wxT("*.")) + defaultExtension;
174 else if ( filter )
175 filter2 = filter;
176
177 wxString defaultDirString;
178 if (defaultDir)
179 defaultDirString = defaultDir;
180
181 wxString defaultFilenameString;
182 if (defaultFileName)
183 defaultFilenameString = defaultFileName;
184
185 wxFileDialog fileDialog(parent, title, defaultDirString,
186 defaultFilenameString, filter2,
187 flags, wxPoint(x, y));
188
189 // if filter is of form "All files (*)|*|..." set correct filter index
190 if((wxStrlen(defaultExtension) != 0) && (filter2.Find(wxT('|')) != wxNOT_FOUND))
191 {
192 int filterIndex = 0;
193
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++)
198 {
199 if (filters[n].Contains(defaultExtension))
200 {
201 filterIndex = n;
202 break;
203 }
204 }
205
206 if (filterIndex > 0)
207 fileDialog.SetFilterIndex(filterIndex);
208 }
209
210 wxString filename;
211 if ( fileDialog.ShowModal() == wxID_OK )
212 {
213 filename = fileDialog.GetPath();
214 }
215
216 return filename;
217 }
218
219 //----------------------------------------------------------------------------
220 // wxFileSelectorEx
221 //----------------------------------------------------------------------------
222
223 wxString wxFileSelectorEx(const wxChar *title,
224 const wxChar *defaultDir,
225 const wxChar *defaultFileName,
226 int* defaultFilterIndex,
227 const wxChar *filter,
228 int flags,
229 wxWindow* parent,
230 int x,
231 int y)
232
233 {
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));
240
241 wxString filename;
242 if ( fileDialog.ShowModal() == wxID_OK )
243 {
244 if ( defaultFilterIndex )
245 *defaultFilterIndex = fileDialog.GetFilterIndex();
246
247 filename = fileDialog.GetPath();
248 }
249
250 return filename;
251 }
252
253 //----------------------------------------------------------------------------
254 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
255 //----------------------------------------------------------------------------
256
257 static wxString wxDefaultFileSelector(bool load,
258 const wxChar *what,
259 const wxChar *extension,
260 const wxChar *default_name,
261 wxWindow *parent)
262 {
263 wxString prompt;
264 wxString str;
265 if (load)
266 str = _("Load %s file");
267 else
268 str = _("Save %s file");
269 prompt.Printf(str, what);
270
271 wxString wild;
272 const wxChar *ext = extension;
273 if ( ext )
274 {
275 if ( *ext == wxT('.') )
276 ext++;
277
278 wild.Printf(wxT("*.%s"), ext);
279 }
280 else // no extension specified
281 {
282 wild = wxFileSelectorDefaultWildcardStr;
283 }
284
285 return wxFileSelector(prompt, NULL, default_name, ext, wild,
286 load ? wxFD_OPEN : wxFD_SAVE, parent);
287 }
288
289 //----------------------------------------------------------------------------
290 // wxLoadFileSelector
291 //----------------------------------------------------------------------------
292
293 WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
294 const wxChar *extension,
295 const wxChar *default_name,
296 wxWindow *parent)
297 {
298 return wxDefaultFileSelector(true, what, extension, default_name, parent);
299 }
300
301 //----------------------------------------------------------------------------
302 // wxSaveFileSelector
303 //----------------------------------------------------------------------------
304
305 WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
306 const wxChar *extension,
307 const wxChar *default_name,
308 wxWindow *parent)
309 {
310 return wxDefaultFileSelector(false, what, extension, default_name, parent);
311 }
312
313 #endif // wxUSE_FILEDLG
314