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