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