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