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