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