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