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