]> git.saurik.com Git - wxWidgets.git/blob - src/common/fldlgcmn.cpp
Added headers that didn't get installed.
[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 #ifdef __GNUG__
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 wxString wxFileSelector(const wxChar *title,
34 const wxChar *defaultDir,
35 const wxChar *defaultFileName,
36 const wxChar *defaultExtension,
37 const wxChar *filter,
38 int flags,
39 wxWindow *parent,
40 int x, int y)
41 {
42 // The defaultExtension, if non-NULL, is
43 // appended to the filename if the user fails to type an extension. The new
44 // implementation (taken from wxFileSelectorEx) appends the extension
45 // automatically, by looking at the filter specification. In fact this
46 // should be better than the native Microsoft implementation because
47 // Windows only allows *one* default extension, whereas here we do the
48 // right thing depending on the filter the user has chosen.
49
50 // If there's a default extension specified but no filter, we create a
51 // suitable filter.
52
53 wxString filter2;
54 if ( defaultExtension && !filter )
55 filter2 = wxString(wxT("*.")) + defaultExtension;
56 else if ( filter )
57 filter2 = filter;
58
59 wxString defaultDirString;
60 if (defaultDir)
61 defaultDirString = defaultDir;
62
63 wxString defaultFilenameString;
64 if (defaultFileName)
65 defaultFilenameString = defaultFileName;
66
67 wxFileDialog fileDialog(parent, title, defaultDirString,
68 defaultFilenameString, filter2,
69 flags, wxPoint(x, y));
70 if( wxStrlen(defaultExtension) != 0 )
71 {
72 int filterFind = 0,
73 filterIndex = 0;
74
75 for( unsigned int i = 0; i < filter2.Len(); i++ )
76 {
77 if( filter2.GetChar(i) == wxT('|') )
78 {
79 // save the start index of the new filter
80 unsigned int is = i++;
81
82 // find the end of the filter
83 for( ; i < filter2.Len(); i++ )
84 {
85 if(filter2[i] == wxT('|'))
86 break;
87 }
88
89 if( i-is-1 > 0 && is+1 < filter2.Len() )
90 {
91 if( filter2.Mid(is+1,i-is-1).Contains(defaultExtension) )
92 {
93 filterFind = filterIndex;
94 break;
95 }
96 }
97
98 filterIndex++;
99 }
100 }
101
102 fileDialog.SetFilterIndex(filterFind);
103 }
104
105 wxString filename;
106 if ( fileDialog.ShowModal() == wxID_OK )
107 {
108 filename = fileDialog.GetPath();
109 }
110
111 return filename;
112 }
113
114
115 /*
116 wxString wxFileSelector( const wxChar *title,
117 const wxChar *defaultDir,
118 const wxChar *defaultFileName,
119 const wxChar *defaultExtension,
120 const wxChar *filter,
121 int flags,
122 wxWindow *parent,
123 int x,
124 int y )
125 {
126 wxString filter2;
127 if ( defaultExtension && !filter )
128 filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ;
129 else if ( filter )
130 filter2 = filter;
131
132 wxString defaultDirString;
133 if (defaultDir)
134 defaultDirString = defaultDir;
135
136 wxString defaultFilenameString;
137 if (defaultFileName)
138 defaultFilenameString = defaultFileName;
139
140 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
141
142 if ( fileDialog.ShowModal() == wxID_OK )
143 {
144 return fileDialog.GetPath();
145 }
146 else
147 {
148 return wxEmptyString;
149 }
150 }
151 */
152
153
154 wxString wxFileSelectorEx(const wxChar *title,
155 const wxChar *defaultDir,
156 const wxChar *defaultFileName,
157 int* defaultFilterIndex,
158 const wxChar *filter,
159 int flags,
160 wxWindow* parent,
161 int x,
162 int y)
163
164 {
165 wxFileDialog fileDialog(parent,
166 title ? title : wxT(""),
167 defaultDir ? defaultDir : wxT(""),
168 defaultFileName ? defaultFileName : wxT(""),
169 filter ? filter : wxT(""),
170 flags, wxPoint(x, y));
171
172 wxString filename;
173 if ( fileDialog.ShowModal() == wxID_OK )
174 {
175 if ( defaultFilterIndex )
176 *defaultFilterIndex = fileDialog.GetFilterIndex();
177
178 filename = fileDialog.GetPath();
179 }
180
181 return filename;
182 }
183
184
185
186 // Generic file load/save dialog (for internal use only)
187 // see wx[Load/Save]FileSelector
188 static wxString wxDefaultFileSelector(bool load,
189 const wxChar *what,
190 const wxChar *extension,
191 const wxChar *default_name,
192 wxWindow *parent)
193 {
194 wxString prompt;
195 wxString str;
196 if (load)
197 str = _("Load %s file");
198 else
199 str = _("Save %s file");
200 prompt.Printf(str, what);
201
202 wxString wild;
203 const wxChar *ext = extension;
204 if ( ext )
205 {
206 if ( *ext == wxT('.') )
207 ext++;
208
209 wild.Printf(wxT("*.%s"), ext);
210 }
211 else // no extension specified
212 {
213 wild = wxFileSelectorDefaultWildcardStr;
214 }
215
216 return wxFileSelector(prompt, NULL, default_name, ext, wild,
217 load ? wxOPEN : wxSAVE, parent);
218 }
219
220 // Generic file load dialog
221 WXDLLEXPORT wxString wxLoadFileSelector(const wxChar *what,
222 const wxChar *extension,
223 const wxChar *default_name,
224 wxWindow *parent)
225 {
226 return wxDefaultFileSelector(TRUE, what, extension, default_name, parent);
227 }
228
229 // Generic file save dialog
230 WXDLLEXPORT wxString wxSaveFileSelector(const wxChar *what,
231 const wxChar *extension,
232 const wxChar *default_name,
233 wxWindow *parent)
234 {
235 return wxDefaultFileSelector(FALSE, what, extension, default_name, parent);
236 }
237
238
239 // Parses the filterStr, returning the number of filters.
240 // Returns 0 if none or if there's a problem.
241 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
242
243 int wxParseFileFilter(const wxString& filterStr,
244 wxArrayString& descriptions,
245 wxArrayString& filters)
246 {
247 descriptions.Clear();
248 filters.Clear();
249
250 wxString str(filterStr);
251
252 wxString description, filter;
253 for ( int pos = 0; pos != wxNOT_FOUND; )
254 {
255 pos = str.Find(wxT('|'));
256 if ( pos == wxNOT_FOUND )
257 {
258 // if there are no '|'s at all in the string just take the entire
259 // string as filter
260 if ( filters.IsEmpty() )
261 {
262 descriptions.Add(filterStr);
263 filters.Add(filterStr);
264 }
265 else
266 {
267 wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
268 }
269
270 break;
271 }
272
273 description = str.Left(pos);
274 str = str.Mid(pos + 1);
275 pos = str.Find(wxT('|'));
276 if ( pos == wxNOT_FOUND )
277 {
278 filter = str;
279 }
280 else
281 {
282 filter = str.Left(pos);
283 str = str.Mid(pos + 1);
284 }
285
286 descriptions.Add(description);
287 filters.Add(filter);
288 }
289
290 return filters.GetCount();
291 }
292
293 #endif // wxUSE_FILEDLG
294