]> git.saurik.com Git - wxWidgets.git/blob - src/common/fldlgcmn.cpp
307c1ffd8a6a78d2d58018561469bfa8384152e9
[wxWidgets.git] / src / common / fldlgcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 #if wxUSE_FILEDLG
20
21 #include "wx/filedlg.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/string.h"
25 #include "wx/intl.h"
26 #include "wx/window.h"
27 #endif // WX_PRECOMP
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 if (!HasFdFlag(wxFD_OPEN) && !HasFdFlag(wxFD_SAVE))
61 m_windowStyle |= wxFD_OPEN; // wxFD_OPEN is the default
62
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") );
66
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" ) );
70
71 wxASSERT_MSG( !HasFdFlag(wxFD_OPEN) || !HasFdFlag(wxFD_OVERWRITE_PROMPT),
72 _T("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
73
74 if ( wildCard.empty() || wildCard == wxFileSelectorDefaultWildcardStr )
75 {
76 m_wildCard = wxString::Format(_("All files (%s)|%s"),
77 wxFileSelectorDefaultWildcardStr,
78 wxFileSelectorDefaultWildcardStr);
79 }
80 else // have wild card
81 {
82 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
83 if ( m_wildCard.Find(wxT('|')) == wxNOT_FOUND )
84 {
85 wxString::size_type nDot = m_wildCard.find(_T("*."));
86 if ( nDot != wxString::npos )
87 nDot++;
88 else
89 nDot = 0;
90
91 m_wildCard = wxString::Format
92 (
93 _("%s files (%s)|%s"),
94 wildCard.c_str() + nDot,
95 wildCard.c_str(),
96 wildCard.c_str()
97 );
98 }
99 }
100
101 return true;
102 }
103
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)
111 {
112 return ::wxParseCommonDialogsFilter(filterStr, descriptions, filters);
113 }
114 #endif // WXWIN_COMPATIBILITY_2_4
115
116 #if WXWIN_COMPATIBILITY_2_6
117 long wxFileDialogBase::GetStyle() const
118 {
119 return GetWindowStyle();
120 }
121
122 void wxFileDialogBase::SetStyle(long style)
123 {
124 SetWindowStyle(style);
125 }
126 #endif // WXWIN_COMPATIBILITY_2_6
127
128
129 wxString wxFileDialogBase::AppendExtension(const wxString &filePath,
130 const wxString &extensionList)
131 {
132 // strip off path, to avoid problems with "path.bar/foo"
133 wxString fileName = filePath.AfterLast(wxFILE_SEP_PATH);
134
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))
138 return filePath;
139
140 // get the first extension from extensionList, or all of it
141 wxString ext = extensionList.BeforeFirst(wxT(';'));
142
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))
146 return filePath;
147 else
148 ext = ext.AfterLast(wxT('.'));
149
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()))
154 return filePath;
155
156 // if fileName doesn't have a '.' then add one
157 if (filePath.Last() != wxT('.'))
158 ext = wxT(".") + ext;
159
160 return filePath + ext;
161 }
162
163 //----------------------------------------------------------------------------
164 // wxFileDialog convenience functions
165 //----------------------------------------------------------------------------
166
167 wxString wxFileSelector(const wxChar *title,
168 const wxChar *defaultDir,
169 const wxChar *defaultFileName,
170 const wxChar *defaultExtension,
171 const wxChar *filter,
172 int flags,
173 wxWindow *parent,
174 int x, int y)
175 {
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.
183
184 // If there's a default extension specified but no filter, we create a
185 // suitable filter.
186
187 wxString filter2;
188 if ( defaultExtension && !filter )
189 filter2 = wxString(wxT("*.")) + defaultExtension;
190 else if ( filter )
191 filter2 = filter;
192
193 wxString defaultDirString;
194 if (defaultDir)
195 defaultDirString = defaultDir;
196
197 wxString defaultFilenameString;
198 if (defaultFileName)
199 defaultFilenameString = defaultFileName;
200
201 wxFileDialog fileDialog(parent, title, defaultDirString,
202 defaultFilenameString, filter2,
203 flags, wxPoint(x, y));
204
205 // if filter is of form "All files (*)|*|..." set correct filter index
206 if((wxStrlen(defaultExtension) != 0) && (filter2.Find(wxT('|')) != wxNOT_FOUND))
207 {
208 int filterIndex = 0;
209
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++)
214 {
215 if (filters[n].Contains(defaultExtension))
216 {
217 filterIndex = n;
218 break;
219 }
220 }
221
222 if (filterIndex > 0)
223 fileDialog.SetFilterIndex(filterIndex);
224 }
225
226 wxString filename;
227 if ( fileDialog.ShowModal() == wxID_OK )
228 {
229 filename = fileDialog.GetPath();
230 }
231
232 return filename;
233 }
234
235 //----------------------------------------------------------------------------
236 // wxFileSelectorEx
237 //----------------------------------------------------------------------------
238
239 wxString wxFileSelectorEx(const wxChar *title,
240 const wxChar *defaultDir,
241 const wxChar *defaultFileName,
242 int* defaultFilterIndex,
243 const wxChar *filter,
244 int flags,
245 wxWindow* parent,
246 int x,
247 int y)
248
249 {
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));
256
257 wxString filename;
258 if ( fileDialog.ShowModal() == wxID_OK )
259 {
260 if ( defaultFilterIndex )
261 *defaultFilterIndex = fileDialog.GetFilterIndex();
262
263 filename = fileDialog.GetPath();
264 }
265
266 return filename;
267 }
268
269 //----------------------------------------------------------------------------
270 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
271 //----------------------------------------------------------------------------
272
273 static wxString wxDefaultFileSelector(bool load,
274 const wxChar *what,
275 const wxChar *extension,
276 const wxChar *default_name,
277 wxWindow *parent)
278 {
279 wxString prompt;
280 wxString str;
281 if (load)
282 str = _("Load %s file");
283 else
284 str = _("Save %s file");
285 prompt.Printf(str, what);
286
287 wxString wild;
288 const wxChar *ext = extension;
289 if ( ext )
290 {
291 if ( *ext == wxT('.') )
292 ext++;
293
294 wild.Printf(wxT("*.%s"), ext);
295 }
296 else // no extension specified
297 {
298 wild = wxFileSelectorDefaultWildcardStr;
299 }
300
301 return wxFileSelector(prompt, NULL, default_name, ext, wild,
302 load ? wxFD_OPEN : wxFD_SAVE, parent);
303 }
304
305 //----------------------------------------------------------------------------
306 // wxLoadFileSelector
307 //----------------------------------------------------------------------------
308
309 WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
310 const wxChar *extension,
311 const wxChar *default_name,
312 wxWindow *parent)
313 {
314 return wxDefaultFileSelector(true, what, extension, default_name, parent);
315 }
316
317 //----------------------------------------------------------------------------
318 // wxSaveFileSelector
319 //----------------------------------------------------------------------------
320
321 WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
322 const wxChar *extension,
323 const wxChar *default_name,
324 wxWindow *parent)
325 {
326 return wxDefaultFileSelector(false, what, extension, default_name, parent);
327 }
328
329
330 //----------------------------------------------------------------------------
331 // wxDirDialogBase
332 //----------------------------------------------------------------------------
333
334 #if WXWIN_COMPATIBILITY_2_6
335 long wxDirDialogBase::GetStyle() const
336 {
337 return GetWindowStyle();
338 }
339
340 void wxDirDialogBase::SetStyle(long style)
341 {
342 SetWindowStyle(style);
343 }
344 #endif // WXWIN_COMPATIBILITY_2_6
345
346
347 #endif // wxUSE_FILEDLG