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