]> git.saurik.com Git - wxWidgets.git/blob - src/common/fldlgcmn.cpp
Define __VISUALC__ for ICC under Windows again.
[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 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_FILEDLG
19
20 #include "wx/filedlg.h"
21 #include "wx/dirdlg.h"
22 #include "wx/filename.h"
23
24 #ifndef WX_PRECOMP
25 #include "wx/string.h"
26 #include "wx/intl.h"
27 #include "wx/window.h"
28 #endif // WX_PRECOMP
29
30 extern WXDLLEXPORT_DATA(const char) wxFileDialogNameStr[] = "filedlg";
31 extern WXDLLEXPORT_DATA(const char) wxFileSelectorPromptStr[] = "Select a file";
32 extern WXDLLEXPORT_DATA(const char) wxFileSelectorDefaultWildcardStr[] =
33 #if defined(__WXMSW__) || defined(__OS2__)
34 "*.*"
35 #else // Unix/Mac
36 "*"
37 #endif
38 ;
39
40 //----------------------------------------------------------------------------
41 // wxFileDialogBase
42 //----------------------------------------------------------------------------
43
44 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase, wxDialog)
45
46 void wxFileDialogBase::Init()
47 {
48 m_filterIndex = 0;
49 m_windowStyle = 0;
50 m_extraControl = NULL;
51 m_extraControlCreator = NULL;
52 }
53
54 bool wxFileDialogBase::Create(wxWindow *parent,
55 const wxString& message,
56 const wxString& defaultDir,
57 const wxString& defaultFile,
58 const wxString& wildCard,
59 long style,
60 const wxPoint& WXUNUSED(pos),
61 const wxSize& WXUNUSED(sz),
62 const wxString& WXUNUSED(name))
63 {
64 m_message = message;
65 m_dir = defaultDir;
66 m_fileName = defaultFile;
67 m_wildCard = wildCard;
68
69 m_parent = parent;
70 m_windowStyle = style;
71 m_filterIndex = 0;
72
73 if (!HasFdFlag(wxFD_OPEN) && !HasFdFlag(wxFD_SAVE))
74 m_windowStyle |= wxFD_OPEN; // wxFD_OPEN is the default
75
76 // check that the styles are not contradictory
77 wxASSERT_MSG( !(HasFdFlag(wxFD_SAVE) && HasFdFlag(wxFD_OPEN)),
78 wxT("can't specify both wxFD_SAVE and wxFD_OPEN at once") );
79
80 wxASSERT_MSG( !HasFdFlag(wxFD_SAVE) ||
81 (!HasFdFlag(wxFD_MULTIPLE) && !HasFdFlag(wxFD_FILE_MUST_EXIST)),
82 wxT("wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST can't be used with wxFD_SAVE" ) );
83
84 wxASSERT_MSG( !HasFdFlag(wxFD_OPEN) || !HasFdFlag(wxFD_OVERWRITE_PROMPT),
85 wxT("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
86
87 if ( wildCard.empty() || wildCard == wxFileSelectorDefaultWildcardStr )
88 {
89 m_wildCard = wxString::Format(_("All files (%s)|%s"),
90 wxFileSelectorDefaultWildcardStr,
91 wxFileSelectorDefaultWildcardStr);
92 }
93 else // have wild card
94 {
95 // convert m_wildCard from "*.bar" to "bar files (*.bar)|*.bar"
96 if ( m_wildCard.Find(wxT('|')) == wxNOT_FOUND )
97 {
98 wxString::size_type nDot = m_wildCard.find(wxT("*."));
99 if ( nDot != wxString::npos )
100 nDot++;
101 else
102 nDot = 0;
103
104 m_wildCard = wxString::Format
105 (
106 _("%s files (%s)|%s"),
107 wildCard.c_str() + nDot,
108 wildCard.c_str(),
109 wildCard.c_str()
110 );
111 }
112 }
113
114 return true;
115 }
116
117 #if WXWIN_COMPATIBILITY_2_6
118 long wxFileDialogBase::GetStyle() const
119 {
120 return GetWindowStyle();
121 }
122
123 void wxFileDialogBase::SetStyle(long style)
124 {
125 SetWindowStyle(style);
126 }
127 #endif // WXWIN_COMPATIBILITY_2_6
128
129
130 wxString wxFileDialogBase::AppendExtension(const wxString &filePath,
131 const wxString &extensionList)
132 {
133 // strip off path, to avoid problems with "path.bar/foo"
134 wxString fileName = filePath.AfterLast(wxFILE_SEP_PATH);
135
136 // if fileName is of form "foo.bar" it's ok, return it
137 int idx_dot = fileName.Find(wxT('.'), true);
138 if ((idx_dot != wxNOT_FOUND) && (idx_dot < (int)fileName.length() - 1))
139 return filePath;
140
141 // get the first extension from extensionList, or all of it
142 wxString ext = extensionList.BeforeFirst(wxT(';'));
143
144 // if ext == "foo" or "foo." there's no extension
145 int idx_ext_dot = ext.Find(wxT('.'), true);
146 if ((idx_ext_dot == wxNOT_FOUND) || (idx_ext_dot == (int)ext.length() - 1))
147 return filePath;
148 else
149 ext = ext.AfterLast(wxT('.'));
150
151 // if ext == "*" or "bar*" or "b?r" or " " then its not valid
152 if ((ext.Find(wxT('*')) != wxNOT_FOUND) ||
153 (ext.Find(wxT('?')) != wxNOT_FOUND) ||
154 (ext.Strip(wxString::both).empty()))
155 return filePath;
156
157 // if fileName doesn't have a '.' then add one
158 if (filePath.Last() != wxT('.'))
159 ext = wxT(".") + ext;
160
161 return filePath + ext;
162 }
163
164 bool wxFileDialogBase::SetExtraControlCreator(ExtraControlCreatorFunction creator)
165 {
166 wxCHECK_MSG( !m_extraControlCreator, false,
167 "wxFileDialog::SetExtraControl() called second time" );
168
169 m_extraControlCreator = creator;
170 return SupportsExtraControl();
171 }
172
173 bool wxFileDialogBase::CreateExtraControl()
174 {
175 if (!m_extraControlCreator || m_extraControl)
176 return false;
177 m_extraControl = (*m_extraControlCreator)(this);
178 return true;
179 }
180
181 wxSize wxFileDialogBase::GetExtraControlSize()
182 {
183 if ( !m_extraControlCreator )
184 return wxDefaultSize;
185
186 // create the extra control in an empty dialog just to find its size: this
187 // is not terribly efficient but we do need to know the size before
188 // creating the native dialog and this seems to be the only way
189 wxDialog dlg(NULL, wxID_ANY, "");
190 return (*m_extraControlCreator)(&dlg)->GetSize();
191 }
192
193 void wxFileDialogBase::SetPath(const wxString& path)
194 {
195 wxString ext;
196 wxFileName::SplitPath(path, &m_dir, &m_fileName, &ext);
197 if ( !ext.empty() )
198 m_fileName << wxT('.') << ext;
199 m_path = path;
200 }
201
202 void wxFileDialogBase::SetDirectory(const wxString& dir)
203 {
204 m_dir = dir;
205 m_path = wxFileName(m_dir, m_fileName).GetFullPath();
206 }
207
208 void wxFileDialogBase::SetFilename(const wxString& name)
209 {
210 m_fileName = name;
211 m_path = wxFileName(m_dir, m_fileName).GetFullPath();
212 }
213
214 //----------------------------------------------------------------------------
215 // wxFileDialog convenience functions
216 //----------------------------------------------------------------------------
217
218 wxString wxFileSelector(const wxString& title,
219 const wxString& defaultDir,
220 const wxString& defaultFileName,
221 const wxString& defaultExtension,
222 const wxString& filter,
223 int flags,
224 wxWindow *parent,
225 int x, int y)
226 {
227 // The defaultExtension, if non-empty, is
228 // appended to the filename if the user fails to type an extension. The new
229 // implementation (taken from wxFileSelectorEx) appends the extension
230 // automatically, by looking at the filter specification. In fact this
231 // should be better than the native Microsoft implementation because
232 // Windows only allows *one* default extension, whereas here we do the
233 // right thing depending on the filter the user has chosen.
234
235 // If there's a default extension specified but no filter, we create a
236 // suitable filter.
237
238 wxString filter2;
239 if ( !defaultExtension.empty() && filter.empty() )
240 filter2 = wxString(wxT("*.")) + defaultExtension;
241 else if ( !filter.empty() )
242 filter2 = filter;
243
244 wxFileDialog fileDialog(parent, title, defaultDir,
245 defaultFileName, filter2,
246 flags, wxPoint(x, y));
247
248 // if filter is of form "All files (*)|*|..." set correct filter index
249 if ( !defaultExtension.empty() && filter2.find(wxT('|')) != wxString::npos )
250 {
251 int filterIndex = 0;
252
253 wxArrayString descriptions, filters;
254 // don't care about errors, handled already by wxFileDialog
255 (void)wxParseCommonDialogsFilter(filter2, descriptions, filters);
256 for (size_t n=0; n<filters.GetCount(); n++)
257 {
258 if (filters[n].Contains(defaultExtension))
259 {
260 filterIndex = n;
261 break;
262 }
263 }
264
265 if (filterIndex > 0)
266 fileDialog.SetFilterIndex(filterIndex);
267 }
268
269 wxString filename;
270 if ( fileDialog.ShowModal() == wxID_OK )
271 {
272 filename = fileDialog.GetPath();
273 }
274
275 return filename;
276 }
277
278 //----------------------------------------------------------------------------
279 // wxFileSelectorEx
280 //----------------------------------------------------------------------------
281
282 wxString wxFileSelectorEx(const wxString& title,
283 const wxString& defaultDir,
284 const wxString& defaultFileName,
285 int* defaultFilterIndex,
286 const wxString& filter,
287 int flags,
288 wxWindow* parent,
289 int x,
290 int y)
291
292 {
293 wxFileDialog fileDialog(parent,
294 title,
295 defaultDir,
296 defaultFileName,
297 filter,
298 flags, wxPoint(x, y));
299
300 wxString filename;
301 if ( fileDialog.ShowModal() == wxID_OK )
302 {
303 if ( defaultFilterIndex )
304 *defaultFilterIndex = fileDialog.GetFilterIndex();
305
306 filename = fileDialog.GetPath();
307 }
308
309 return filename;
310 }
311
312 //----------------------------------------------------------------------------
313 // wxDefaultFileSelector - Generic load/save dialog (for internal use only)
314 //----------------------------------------------------------------------------
315
316 static wxString wxDefaultFileSelector(bool load,
317 const wxString& what,
318 const wxString& extension,
319 const wxString& default_name,
320 wxWindow *parent)
321 {
322 wxString prompt;
323 wxString str;
324 if (load)
325 str = _("Load %s file");
326 else
327 str = _("Save %s file");
328 prompt.Printf(str, what);
329
330 wxString wild;
331 wxString ext;
332 if ( !extension.empty() )
333 {
334 if ( extension[0u] == wxT('.') )
335 ext = extension.substr(1);
336 else
337 ext = extension;
338
339 wild.Printf(wxT("*.%s"), ext);
340 }
341 else // no extension specified
342 {
343 wild = wxFileSelectorDefaultWildcardStr;
344 }
345
346 return wxFileSelector(prompt, wxEmptyString, default_name, ext, wild,
347 load ? (wxFD_OPEN | wxFD_FILE_MUST_EXIST) : wxFD_SAVE,
348 parent);
349 }
350
351 //----------------------------------------------------------------------------
352 // wxLoadFileSelector
353 //----------------------------------------------------------------------------
354
355 WXDLLEXPORT wxString wxLoadFileSelector(const wxString& what,
356 const wxString& extension,
357 const wxString& default_name,
358 wxWindow *parent)
359 {
360 return wxDefaultFileSelector(true, what, extension, default_name, parent);
361 }
362
363 //----------------------------------------------------------------------------
364 // wxSaveFileSelector
365 //----------------------------------------------------------------------------
366
367 WXDLLEXPORT wxString wxSaveFileSelector(const wxString& what,
368 const wxString& extension,
369 const wxString& default_name,
370 wxWindow *parent)
371 {
372 return wxDefaultFileSelector(false, what, extension, default_name, parent);
373 }
374
375
376 //----------------------------------------------------------------------------
377 // wxDirDialogBase
378 //----------------------------------------------------------------------------
379
380 #if WXWIN_COMPATIBILITY_2_6
381 long wxDirDialogBase::GetStyle() const
382 {
383 return GetWindowStyle();
384 }
385
386 void wxDirDialogBase::SetStyle(long style)
387 {
388 SetWindowStyle(style);
389 }
390 #endif // WXWIN_COMPATIBILITY_2_6
391
392
393 #endif // wxUSE_FILEDLG