]> git.saurik.com Git - wxWidgets.git/blob - src/common/fldlgcmn.cpp
don't crash when loading images with verbose==false (patch 1449823)
[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_dialogStyle = 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 {
49 m_message = message;
50 m_dir = defaultDir;
51 m_fileName = defaultFile;
52 m_wildCard = wildCard;
53
54 m_parent = parent;
55 m_dialogStyle = style;
56 m_filterIndex = 0;
57
58 if ( wildCard.empty() || wildCard == wxFileSelectorDefaultWildcardStr )
59 {
60 m_wildCard = wxString::Format(_("All files (%s)|%s"),
61 wxFileSelectorDefaultWildcardStr,
62 wxFileSelectorDefaultWildcardStr);
63 }
64 else // have wild card
65 {
66 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
67 if ( m_wildCard.Find(wxT('|')) == wxNOT_FOUND )
68 {
69 wxString::size_type nDot = m_wildCard.find(_T("*."));
70 if ( nDot != wxString::npos )
71 nDot++;
72 else
73 nDot = 0;
74
75 m_wildCard = wxString::Format
76 (
77 _("%s files (%s)|%s"),
78 wildCard.c_str() + nDot,
79 wildCard.c_str(),
80 wildCard.c_str()
81 );
82 }
83 }
84
85 return true;
86 }
87
88 #if WXWIN_COMPATIBILITY_2_4
89 // Parses the filterStr, returning the number of filters.
90 // Returns 0 if none or if there's a problem.
91 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
92 int wxFileDialogBase::ParseWildcard(const wxString& filterStr,
93 wxArrayString& descriptions,
94 wxArrayString& filters)
95 {
96 return ::wxParseCommonDialogsFilter(filterStr, descriptions, filters);
97 }
98 #endif // WXWIN_COMPATIBILITY_2_4
99
100 wxString wxFileDialogBase::AppendExtension(const wxString &filePath,
101 const wxString &extensionList)
102 {
103 // strip off path, to avoid problems with "path.bar/foo"
104 wxString fileName = filePath.AfterLast(wxFILE_SEP_PATH);
105
106 // if fileName is of form "foo.bar" it's ok, return it
107 int idx_dot = fileName.Find(wxT('.'), true);
108 if ((idx_dot != wxNOT_FOUND) && (idx_dot < (int)fileName.Len() - 1))
109 return filePath;
110
111 // get the first extension from extensionList, or all of it
112 wxString ext = extensionList.BeforeFirst(wxT(';'));
113
114 // if ext == "foo" or "foo." there's no extension
115 int idx_ext_dot = ext.Find(wxT('.'), true);
116 if ((idx_ext_dot == wxNOT_FOUND) || (idx_ext_dot == (int)ext.Len() - 1))
117 return filePath;
118 else
119 ext = ext.AfterLast(wxT('.'));
120
121 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
122 if ((ext.Find(wxT('*')) != wxNOT_FOUND) ||
123 (ext.Find(wxT('?')) != wxNOT_FOUND) ||
124 (ext.Strip(wxString::both).empty()))
125 return filePath;
126
127 // if fileName doesn't have a '.' then add one
128 if (filePath.Last() != wxT('.'))
129 ext = wxT(".") + ext;
130
131 return filePath + ext;
132 }
133
134 //----------------------------------------------------------------------------
135 // wxFileDialog convenience functions
136 //----------------------------------------------------------------------------
137
138 wxString wxFileSelector(const wxChar *title,
139 const wxChar *defaultDir,
140 const wxChar *defaultFileName,
141 const wxChar *defaultExtension,
142 const wxChar *filter,
143 int flags,
144 wxWindow *parent,
145 int x, int y)
146 {
147 // The defaultExtension, if non-NULL, is
148 // appended to the filename if the user fails to type an extension. The new
149 // implementation (taken from wxFileSelectorEx) appends the extension
150 // automatically, by looking at the filter specification. In fact this
151 // should be better than the native Microsoft implementation because
152 // Windows only allows *one* default extension, whereas here we do the
153 // right thing depending on the filter the user has chosen.
154
155 // If there's a default extension specified but no filter, we create a
156 // suitable filter.
157
158 wxString filter2;
159 if ( defaultExtension && !filter )
160 filter2 = wxString(wxT("*.")) + defaultExtension;
161 else if ( filter )
162 filter2 = filter;
163
164 wxString defaultDirString;
165 if (defaultDir)
166 defaultDirString = defaultDir;
167
168 wxString defaultFilenameString;
169 if (defaultFileName)
170 defaultFilenameString = defaultFileName;
171
172 wxFileDialog fileDialog(parent, title, defaultDirString,
173 defaultFilenameString, filter2,
174 flags, wxPoint(x, y));
175
176 // if filter is of form "All files (*)|*|..." set correct filter index
177 if((wxStrlen(defaultExtension) != 0) && (filter2.Find(wxT('|')) != wxNOT_FOUND))
178 {
179 int filterIndex = 0;
180
181 wxArrayString descriptions, filters;
182 // don't care about errors, handled already by wxFileDialog
183 (void)wxParseCommonDialogsFilter(filter2, descriptions, filters);
184 for (size_t n=0; n<filters.GetCount(); n++)
185 {
186 if (filters[n].Contains(defaultExtension))
187 {
188 filterIndex = n;
189 break;
190 }
191 }
192
193 if (filterIndex > 0)
194 fileDialog.SetFilterIndex(filterIndex);
195 }
196
197 wxString filename;
198 if ( fileDialog.ShowModal() == wxID_OK )
199 {
200 filename = fileDialog.GetPath();
201 }
202
203 return filename;
204 }
205
206 //----------------------------------------------------------------------------
207 // wxFileSelectorEx
208 //----------------------------------------------------------------------------
209
210 wxString wxFileSelectorEx(const wxChar *title,
211 const wxChar *defaultDir,
212 const wxChar *defaultFileName,
213 int* defaultFilterIndex,
214 const wxChar *filter,
215 int flags,
216 wxWindow* parent,
217 int x,
218 int y)
219
220 {
221 wxFileDialog fileDialog(parent,
222 title ? title : wxEmptyString,
223 defaultDir ? defaultDir : wxEmptyString,
224 defaultFileName ? defaultFileName : wxEmptyString,
225 filter ? filter : wxEmptyString,
226 flags, wxPoint(x, y));
227
228 wxString filename;
229 if ( fileDialog.ShowModal() == wxID_OK )
230 {
231 if ( defaultFilterIndex )
232 *defaultFilterIndex = fileDialog.GetFilterIndex();
233
234 filename = fileDialog.GetPath();
235 }
236
237 return filename;
238 }
239
240 //----------------------------------------------------------------------------
241 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
242 //----------------------------------------------------------------------------
243
244 static wxString wxDefaultFileSelector(bool load,
245 const wxChar *what,
246 const wxChar *extension,
247 const wxChar *default_name,
248 wxWindow *parent)
249 {
250 wxString prompt;
251 wxString str;
252 if (load)
253 str = _("Load %s file");
254 else
255 str = _("Save %s file");
256 prompt.Printf(str, what);
257
258 wxString wild;
259 const wxChar *ext = extension;
260 if ( ext )
261 {
262 if ( *ext == wxT('.') )
263 ext++;
264
265 wild.Printf(wxT("*.%s"), ext);
266 }
267 else // no extension specified
268 {
269 wild = wxFileSelectorDefaultWildcardStr;
270 }
271
272 return wxFileSelector(prompt, NULL, default_name, ext, wild,
273 load ? wxOPEN : wxSAVE, parent);
274 }
275
276 //----------------------------------------------------------------------------
277 // wxLoadFileSelector
278 //----------------------------------------------------------------------------
279
280 WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
281 const wxChar *extension,
282 const wxChar *default_name,
283 wxWindow *parent)
284 {
285 return wxDefaultFileSelector(true, what, extension, default_name, parent);
286 }
287
288 //----------------------------------------------------------------------------
289 // wxSaveFileSelector
290 //----------------------------------------------------------------------------
291
292 WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
293 const wxChar *extension,
294 const wxChar *default_name,
295 wxWindow *parent)
296 {
297 return wxDefaultFileSelector(false, what, extension, default_name, parent);
298 }
299
300 #endif // wxUSE_FILEDLG
301