]> git.saurik.com Git - wxWidgets.git/blame - src/common/fldlgcmn.cpp
Allow creating wxGraphicsBitmap and wxGraphicsContext from wxImage.
[wxWidgets.git] / src / common / fldlgcmn.cpp
CommitLineData
b600ed13 1/////////////////////////////////////////////////////////////////////////////
949c9f74 2// Name: src/common/fldlgcmn.cpp
b600ed13
VZ
3// Purpose: wxFileDialog common functions
4// Author: John Labenski
5// Modified by:
6// Created: 14.06.03 (extracted from src/*/filedlg.cpp)
7448de8d 7// RCS-ID: $Id$
b600ed13 8// Copyright: (c) Robert Roebling
65571936 9// Licence: wxWindows licence
b600ed13
VZ
10/////////////////////////////////////////////////////////////////////////////
11
b600ed13
VZ
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
b4715d08
VZ
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
949c9f74
WS
19#if wxUSE_FILEDLG
20
21#include "wx/filedlg.h"
cf6982fa 22#include "wx/dirdlg.h"
7430a4bb 23#include "wx/filename.h"
949c9f74 24
b600ed13
VZ
25#ifndef WX_PRECOMP
26 #include "wx/string.h"
27 #include "wx/intl.h"
28 #include "wx/window.h"
29#endif // WX_PRECOMP
30
f313deaa
PC
31extern WXDLLEXPORT_DATA(const char) wxFileDialogNameStr[] = "filedlg";
32extern WXDLLEXPORT_DATA(const char) wxFileSelectorPromptStr[] = "Select a file";
33extern WXDLLEXPORT_DATA(const char) wxFileSelectorDefaultWildcardStr[] =
34#if defined(__WXMSW__) || defined(__OS2__)
35 "*.*"
36#else // Unix/Mac
37 "*"
38#endif
39 ;
40
f74172ab
VZ
41//----------------------------------------------------------------------------
42// wxFileDialogBase
43//----------------------------------------------------------------------------
44
45IMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase, wxDialog)
46
fe6cf128 47void wxFileDialogBase::Init()
7448de8d 48{
8ce68f7f 49 m_filterIndex = 0;
ff3e84ff 50 m_windowStyle = 0;
8ce68f7f
VZ
51 m_extraControl = NULL;
52 m_extraControlCreator = NULL;
fe6cf128
VZ
53}
54
55bool wxFileDialogBase::Create(wxWindow *parent,
56 const wxString& message,
57 const wxString& defaultDir,
58 const wxString& defaultFile,
59 const wxString& wildCard,
60 long style,
ff3e84ff
VZ
61 const wxPoint& WXUNUSED(pos),
62 const wxSize& WXUNUSED(sz),
63 const wxString& WXUNUSED(name))
f74172ab 64{
fe6cf128
VZ
65 m_message = message;
66 m_dir = defaultDir;
67 m_fileName = defaultFile;
68 m_wildCard = wildCard;
69
f74172ab 70 m_parent = parent;
45f4109c 71 m_windowStyle = style;
f74172ab
VZ
72 m_filterIndex = 0;
73
b014db05 74 if (!HasFdFlag(wxFD_OPEN) && !HasFdFlag(wxFD_SAVE))
45f4109c 75 m_windowStyle |= wxFD_OPEN; // wxFD_OPEN is the default
556151f5 76
96aed0cd 77 // check that the styles are not contradictory
b014db05 78 wxASSERT_MSG( !(HasFdFlag(wxFD_SAVE) && HasFdFlag(wxFD_OPEN)),
9a83f860 79 wxT("can't specify both wxFD_SAVE and wxFD_OPEN at once") );
96aed0cd 80
b014db05
RR
81 wxASSERT_MSG( !HasFdFlag(wxFD_SAVE) ||
82 (!HasFdFlag(wxFD_MULTIPLE) && !HasFdFlag(wxFD_FILE_MUST_EXIST)),
9a83f860 83 wxT("wxFD_MULTIPLE or wxFD_FILE_MUST_EXIST can't be used with wxFD_SAVE" ) );
96aed0cd 84
b014db05 85 wxASSERT_MSG( !HasFdFlag(wxFD_OPEN) || !HasFdFlag(wxFD_OVERWRITE_PROMPT),
9a83f860 86 wxT("wxFD_OVERWRITE_PROMPT can't be used with wxFD_OPEN") );
ff3e84ff 87
d59eea26 88 if ( wildCard.empty() || wildCard == wxFileSelectorDefaultWildcardStr )
f74172ab 89 {
186545a4
VZ
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 {
9a83f860 99 wxString::size_type nDot = m_wildCard.find(wxT("*."));
186545a4
VZ
100 if ( nDot != wxString::npos )
101 nDot++;
102 else
103 nDot = 0;
104
105 m_wildCard = wxString::Format
106 (
107 _("%s files (%s)|%s"),
d59eea26
VZ
108 wildCard.c_str() + nDot,
109 wildCard.c_str(),
110 wildCard.c_str()
186545a4
VZ
111 );
112 }
f74172ab 113 }
fe6cf128
VZ
114
115 return true;
f74172ab
VZ
116}
117
cc197ed4
VZ
118#if WXWIN_COMPATIBILITY_2_6
119long wxFileDialogBase::GetStyle() const
120{
121 return GetWindowStyle();
122}
123
124void wxFileDialogBase::SetStyle(long style)
125{
126 SetWindowStyle(style);
127}
128#endif // WXWIN_COMPATIBILITY_2_6
129
130
f74172ab
VZ
131wxString 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
a62848fd 138 int idx_dot = fileName.Find(wxT('.'), true);
949c9f74 139 if ((idx_dot != wxNOT_FOUND) && (idx_dot < (int)fileName.length() - 1))
f74172ab
VZ
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
a62848fd 146 int idx_ext_dot = ext.Find(wxT('.'), true);
949c9f74 147 if ((idx_ext_dot == wxNOT_FOUND) || (idx_ext_dot == (int)ext.length() - 1))
f74172ab
VZ
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) ||
b494c48b 155 (ext.Strip(wxString::both).empty()))
f74172ab
VZ
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
d6dae1b4 165bool wxFileDialogBase::SetExtraControlCreator(ExtraControlCreatorFunction creator)
8ce68f7f
VZ
166{
167 wxCHECK_MSG( !m_extraControlCreator, false,
168 "wxFileDialog::SetExtraControl() called second time" );
169
d6dae1b4 170 m_extraControlCreator = creator;
8ce68f7f
VZ
171 return SupportsExtraControl();
172}
173
174bool wxFileDialogBase::CreateExtraControl()
175{
176 if (!m_extraControlCreator || m_extraControl)
177 return false;
178 m_extraControl = (*m_extraControlCreator)(this);
179 return true;
180}
181
6fa6d659
VZ
182wxSize 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
7430a4bb
VZ
194void wxFileDialogBase::SetPath(const wxString& path)
195{
196 wxString ext;
197 wxFileName::SplitPath(path, &m_dir, &m_fileName, &ext);
198 if ( !ext.empty() )
490e22fd 199 m_fileName << wxT('.') << ext;
7430a4bb
VZ
200 m_path = path;
201}
202
203void wxFileDialogBase::SetDirectory(const wxString& dir)
204{
205 m_dir = dir;
206 m_path = wxFileName(m_dir, m_fileName).GetFullPath();
207}
208
209void wxFileDialogBase::SetFilename(const wxString& name)
210{
211 m_fileName = name;
212 m_path = wxFileName(m_dir, m_fileName).GetFullPath();
213}
214
f74172ab
VZ
215//----------------------------------------------------------------------------
216// wxFileDialog convenience functions
217//----------------------------------------------------------------------------
218
6dc2e823
VS
219wxString 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)
b600ed13 227{
f8bcb37d 228 // The defaultExtension, if non-empty, is
b600ed13
VZ
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;
f8bcb37d 240 if ( !defaultExtension.empty() && filter.empty() )
b600ed13 241 filter2 = wxString(wxT("*.")) + defaultExtension;
f8bcb37d 242 else if ( !filter.empty() )
b600ed13
VZ
243 filter2 = filter;
244
f8bcb37d
VS
245 wxFileDialog fileDialog(parent, title, defaultDir,
246 defaultFileName, filter2,
b600ed13 247 flags, wxPoint(x, y));
b600ed13 248
f8bcb37d
VS
249 // if filter is of form "All files (*)|*|..." set correct filter index
250 if ( !defaultExtension.empty() && filter2.find(wxT('|')) != wxString::npos )
7448de8d 251 {
f74172ab 252 int filterIndex = 0;
b600ed13 253
f74172ab
VZ
254 wxArrayString descriptions, filters;
255 // don't care about errors, handled already by wxFileDialog
daf32463 256 (void)wxParseCommonDialogsFilter(filter2, descriptions, filters);
f74172ab 257 for (size_t n=0; n<filters.GetCount(); n++)
7448de8d 258 {
f74172ab 259 if (filters[n].Contains(defaultExtension))
7448de8d 260 {
f74172ab 261 filterIndex = n;
f8bcb37d 262 break;
7448de8d
WS
263 }
264 }
b600ed13 265
f74172ab
VZ
266 if (filterIndex > 0)
267 fileDialog.SetFilterIndex(filterIndex);
7448de8d 268 }
b600ed13 269
b600ed13
VZ
270 wxString filename;
271 if ( fileDialog.ShowModal() == wxID_OK )
272 {
273 filename = fileDialog.GetPath();
274 }
275
276 return filename;
277}
278
f74172ab
VZ
279//----------------------------------------------------------------------------
280// wxFileSelectorEx
281//----------------------------------------------------------------------------
b600ed13 282
6dc2e823
VS
283wxString 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)
b600ed13
VZ
292
293{
294 wxFileDialog fileDialog(parent,
f8bcb37d
VS
295 title,
296 defaultDir,
297 defaultFileName,
298 filter,
b600ed13
VZ
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
f74172ab
VZ
313//----------------------------------------------------------------------------
314// wxDefaultFileSelector - Generic load/save dialog (for internal use only)
315//----------------------------------------------------------------------------
b600ed13 316
b600ed13 317static wxString wxDefaultFileSelector(bool load,
f8bcb37d
VS
318 const wxString& what,
319 const wxString& extension,
320 const wxString& default_name,
b600ed13
VZ
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;
f8bcb37d
VS
332 wxString ext;
333 if ( !extension.empty() )
b600ed13 334 {
9a83f860 335 if ( extension[0u] == wxT('.') )
f8bcb37d
VS
336 ext = extension.substr(1);
337 else
338 ext = extension;
b600ed13
VZ
339
340 wild.Printf(wxT("*.%s"), ext);
341 }
342 else // no extension specified
343 {
344 wild = wxFileSelectorDefaultWildcardStr;
345 }
346
f8bcb37d 347 return wxFileSelector(prompt, wxEmptyString, default_name, ext, wild,
f2a18fbe
VZ
348 load ? (wxFD_OPEN | wxFD_FILE_MUST_EXIST) : wxFD_SAVE,
349 parent);
b600ed13
VZ
350}
351
f74172ab
VZ
352//----------------------------------------------------------------------------
353// wxLoadFileSelector
354//----------------------------------------------------------------------------
355
6dc2e823
VS
356WXDLLEXPORT wxString wxLoadFileSelector(const wxString& what,
357 const wxString& extension,
358 const wxString& default_name,
359 wxWindow *parent)
b600ed13 360{
a62848fd 361 return wxDefaultFileSelector(true, what, extension, default_name, parent);
b600ed13
VZ
362}
363
f74172ab
VZ
364//----------------------------------------------------------------------------
365// wxSaveFileSelector
366//----------------------------------------------------------------------------
367
6dc2e823
VS
368WXDLLEXPORT wxString wxSaveFileSelector(const wxString& what,
369 const wxString& extension,
370 const wxString& default_name,
371 wxWindow *parent)
b600ed13 372{
a62848fd 373 return wxDefaultFileSelector(false, what, extension, default_name, parent);
b600ed13
VZ
374}
375
cc197ed4
VZ
376
377//----------------------------------------------------------------------------
378// wxDirDialogBase
379//----------------------------------------------------------------------------
380
381#if WXWIN_COMPATIBILITY_2_6
382long wxDirDialogBase::GetStyle() const
383{
384 return GetWindowStyle();
385}
386
387void wxDirDialogBase::SetStyle(long style)
388{
389 SetWindowStyle(style);
390}
391#endif // WXWIN_COMPATIBILITY_2_6
392
393
b600ed13 394#endif // wxUSE_FILEDLG