]>
Commit | Line | Data |
---|---|---|
81169710 | 1 | ////////////////////////////////////////////////////////////////////////////// |
812a5849 | 2 | // Name: src/generic/filedlgg.cpp |
23254b13 | 3 | // Purpose: wxGenericFileDialog |
8b17ba72 RR |
4 | // Author: Robert Roebling |
5 | // Modified by: | |
6 | // Created: 12/12/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Robert Roebling | |
65571936 | 9 | // Licence: wxWindows licence |
8b17ba72 RR |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
8b17ba72 RR |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
246c5004 | 16 | #pragma hdrstop |
8b17ba72 RR |
17 | #endif |
18 | ||
4e1901b7 | 19 | #if wxUSE_FILEDLG |
1e6feb95 | 20 | |
06cc1fb9 | 21 | // NOTE : it probably also supports MAC, untested |
144cf6da | 22 | #if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__) && !defined(__OS2__) |
23254b13 | 23 | #error wxGenericFileDialog currently only supports Unix, win32 and DOS |
8b17ba72 RR |
24 | #endif |
25 | ||
88a7a4e1 WS |
26 | #ifndef WX_PRECOMP |
27 | #include "wx/hash.h" | |
28 | #include "wx/intl.h" | |
9eddec69 | 29 | #include "wx/settings.h" |
e7c80f9e | 30 | #include "wx/log.h" |
246c5004 | 31 | #include "wx/msgdlg.h" |
910b0053 | 32 | #include "wx/bmpbuttn.h" |
ab1ce969 | 33 | #include "wx/checkbox.h" |
b36e08d0 | 34 | #include "wx/choice.h" |
ccdc11bb | 35 | #include "wx/stattext.h" |
fec9cc08 | 36 | #include "wx/textctrl.h" |
ed2fbeb8 | 37 | #include "wx/sizer.h" |
949c9f74 | 38 | #include "wx/filedlg.h" // wxFD_OPEN, wxFD_SAVE... |
88a7a4e1 WS |
39 | #endif |
40 | ||
ec524671 | 41 | #include "wx/longlong.h" |
cae5359f | 42 | #include "wx/tokenzr.h" |
eaf40b23 | 43 | #include "wx/config.h" |
48fe8374 | 44 | #include "wx/imaglist.h" |
5e673a6a | 45 | #include "wx/dir.h" |
60d2cc25 | 46 | #include "wx/artprov.h" |
619111b9 | 47 | #include "wx/filefn.h" |
1265e13d | 48 | #include "wx/file.h" // for wxS_IXXX constants only |
94c47543 | 49 | #include "wx/generic/filedlgg.h" |
06cc1fb9 | 50 | #include "wx/generic/dirctrlg.h" // for wxFileIconsTable |
8b17ba72 | 51 | |
e6daf794 RR |
52 | #if wxUSE_TOOLTIPS |
53 | #include "wx/tooltip.h" | |
54 | #endif | |
55 | ||
619111b9 | 56 | #ifndef __WXWINCE__ |
e7c80f9e WS |
57 | #include <sys/types.h> |
58 | #include <sys/stat.h> | |
619111b9 | 59 | #endif |
4894ae18 VS |
60 | |
61 | #ifdef __UNIX__ | |
62 | #include <dirent.h> | |
63 | #include <pwd.h> | |
64 | #ifndef __VMS | |
65 | # include <grp.h> | |
66 | #endif | |
67 | #endif | |
68 | ||
6a8c7c70 VS |
69 | #ifdef __WINDOWS__ |
70 | #include "wx/msw/wrapwin.h" | |
71 | #include "wx/msw/mslu.h" | |
72 | #endif | |
73 | ||
4894ae18 VS |
74 | #ifdef __WATCOMC__ |
75 | #include <direct.h> | |
27df579a | 76 | #endif |
4894ae18 | 77 | |
619111b9 | 78 | #ifndef __WXWINCE__ |
7a82dabc | 79 | #include <time.h> |
619111b9 JS |
80 | #endif |
81 | ||
099d4217 | 82 | #if defined(__UNIX__) || defined(__DOS__) |
ac2def68 | 83 | #include <unistd.h> |
099d4217 | 84 | #endif |
8b17ba72 | 85 | |
655cf310 | 86 | // ---------------------------------------------------------------------------- |
06cc1fb9 | 87 | // private functions |
655cf310 VS |
88 | // ---------------------------------------------------------------------------- |
89 | ||
06cc1fb9 | 90 | static |
ec524671 | 91 | int wxCALLBACK wxFileDataNameCompare( long data1, long data2, long sortOrder) |
655cf310 | 92 | { |
ec524671 VZ |
93 | wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1); |
94 | wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2); | |
95 | ||
96 | if (fd1->GetFileName() == wxT("..")) | |
97 | return -sortOrder; | |
98 | if (fd2->GetFileName() == wxT("..")) | |
99 | return sortOrder; | |
100 | if (fd1->IsDir() && !fd2->IsDir()) | |
101 | return -sortOrder; | |
102 | if (fd2->IsDir() && !fd1->IsDir()) | |
103 | return sortOrder; | |
104 | ||
105 | return sortOrder*wxStrcmp( fd1->GetFileName(), fd2->GetFileName() ); | |
655cf310 VS |
106 | } |
107 | ||
c8c0e54c | 108 | static |
ec524671 | 109 | int wxCALLBACK wxFileDataSizeCompare(long data1, long data2, long sortOrder) |
23254b13 | 110 | { |
ec524671 VZ |
111 | wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1); |
112 | wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2); | |
113 | ||
114 | if (fd1->GetFileName() == wxT("..")) | |
115 | return -sortOrder; | |
116 | if (fd2->GetFileName() == wxT("..")) | |
117 | return sortOrder; | |
118 | if (fd1->IsDir() && !fd2->IsDir()) | |
119 | return -sortOrder; | |
120 | if (fd2->IsDir() && !fd1->IsDir()) | |
121 | return sortOrder; | |
122 | if (fd1->IsLink() && !fd2->IsLink()) | |
123 | return -sortOrder; | |
124 | if (fd2->IsLink() && !fd1->IsLink()) | |
125 | return sortOrder; | |
126 | ||
127 | return fd1->GetSize() > fd2->GetSize() ? sortOrder : -sortOrder; | |
23254b13 JS |
128 | } |
129 | ||
130 | static | |
ec524671 | 131 | int wxCALLBACK wxFileDataTypeCompare(long data1, long data2, long sortOrder) |
23254b13 | 132 | { |
ec524671 VZ |
133 | wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1); |
134 | wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2); | |
135 | ||
136 | if (fd1->GetFileName() == wxT("..")) | |
137 | return -sortOrder; | |
138 | if (fd2->GetFileName() == wxT("..")) | |
139 | return sortOrder; | |
140 | if (fd1->IsDir() && !fd2->IsDir()) | |
141 | return -sortOrder; | |
142 | if (fd2->IsDir() && !fd1->IsDir()) | |
143 | return sortOrder; | |
144 | if (fd1->IsLink() && !fd2->IsLink()) | |
145 | return -sortOrder; | |
146 | if (fd2->IsLink() && !fd1->IsLink()) | |
147 | return sortOrder; | |
148 | ||
149 | return sortOrder*wxStrcmp( fd1->GetFileType(), fd2->GetFileType() ); | |
23254b13 JS |
150 | } |
151 | ||
152 | static | |
ec524671 | 153 | int wxCALLBACK wxFileDataTimeCompare(long data1, long data2, long sortOrder) |
23254b13 | 154 | { |
ec524671 VZ |
155 | wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1); |
156 | wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2); | |
157 | ||
158 | if (fd1->GetFileName() == wxT("..")) | |
159 | return -sortOrder; | |
160 | if (fd2->GetFileName() == wxT("..")) | |
161 | return sortOrder; | |
162 | if (fd1->IsDir() && !fd2->IsDir()) | |
163 | return -sortOrder; | |
164 | if (fd2->IsDir() && !fd1->IsDir()) | |
165 | return sortOrder; | |
23254b13 | 166 | |
ec524671 | 167 | return fd1->GetDateTime().IsLaterThan(fd2->GetDateTime()) ? sortOrder : -sortOrder; |
c8c0e54c VZ |
168 | } |
169 | ||
619111b9 JS |
170 | #if defined(__WXWINCE__) |
171 | #define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/")) | |
172 | #elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__)) | |
abc2a4cc | 173 | #define IsTopMostDir(dir) (dir.empty()) |
70a7eb07 MW |
174 | #else |
175 | #define IsTopMostDir(dir) (dir == wxT("/")) | |
5e673a6a VS |
176 | #endif |
177 | ||
06cc1fb9 JS |
178 | // defined in src/generic/dirctrlg.cpp |
179 | extern size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids); | |
180 | ||
8b17ba72 RR |
181 | //----------------------------------------------------------------------------- |
182 | // wxFileData | |
183 | //----------------------------------------------------------------------------- | |
184 | ||
06cc1fb9 | 185 | wxFileData::wxFileData( const wxString &filePath, const wxString &fileName, fileType type, int image_id ) |
8b17ba72 | 186 | { |
ca9fb418 | 187 | Init(); |
06cc1fb9 JS |
188 | m_fileName = fileName; |
189 | m_filePath = filePath; | |
23254b13 | 190 | m_type = type; |
06cc1fb9 | 191 | m_image = image_id; |
7a82dabc | 192 | |
b600ed13 VZ |
193 | ReadData(); |
194 | } | |
195 | ||
ca9fb418 VZ |
196 | void wxFileData::Init() |
197 | { | |
198 | m_size = 0; | |
199 | m_type = wxFileData::is_file; | |
200 | m_image = wxFileIconsTable::file; | |
201 | } | |
202 | ||
81169710 VZ |
203 | void wxFileData::Copy( const wxFileData& fileData ) |
204 | { | |
205 | m_fileName = fileData.GetFileName(); | |
206 | m_filePath = fileData.GetFilePath(); | |
207 | m_size = fileData.GetSize(); | |
208 | m_dateTime = fileData.GetDateTime(); | |
209 | m_permissions = fileData.GetPermissions(); | |
210 | m_type = fileData.GetType(); | |
ca9fb418 | 211 | m_image = fileData.GetImageId(); |
81169710 VZ |
212 | } |
213 | ||
b600ed13 VZ |
214 | void wxFileData::ReadData() |
215 | { | |
06cc1fb9 | 216 | if (IsDrive()) |
37fd1c97 | 217 | { |
23254b13 JS |
218 | m_size = 0; |
219 | return; | |
220 | } | |
221 | ||
619111b9 | 222 | #if defined(__DOS__) || (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__OS2__) |
06cc1fb9 | 223 | // c:\.. is a drive don't stat it |
567be187 | 224 | if ((m_fileName == wxT("..")) && (m_filePath.length() <= 5)) |
23254b13 JS |
225 | { |
226 | m_type = is_drive; | |
37fd1c97 VS |
227 | m_size = 0; |
228 | return; | |
229 | } | |
7f04165e | 230 | #endif // __DOS__ || __WINDOWS__ |
c8c0e54c | 231 | |
619111b9 JS |
232 | #ifdef __WXWINCE__ |
233 | ||
234 | // WinCE | |
235 | ||
236 | DWORD fileAttribs = GetFileAttributes(m_filePath.fn_str()); | |
237 | m_type |= (fileAttribs & FILE_ATTRIBUTE_DIRECTORY) != 0 ? is_dir : 0; | |
238 | ||
239 | wxString p, f, ext; | |
240 | wxSplitPath(m_filePath, & p, & f, & ext); | |
241 | if (wxStricmp(ext, wxT("exe")) == 0) | |
242 | m_type |= is_exe; | |
243 | ||
244 | // Find out size | |
245 | m_size = 0; | |
246 | HANDLE fileHandle = CreateFile(m_filePath.fn_str(), | |
247 | GENERIC_READ, | |
248 | FILE_SHARE_READ, | |
249 | NULL, | |
250 | OPEN_EXISTING, | |
251 | FILE_ATTRIBUTE_NORMAL, | |
252 | NULL); | |
253 | ||
254 | if (fileHandle != INVALID_HANDLE_VALUE) | |
c81c9392 | 255 | { |
619111b9 JS |
256 | m_size = GetFileSize(fileHandle, 0); |
257 | CloseHandle(fileHandle); | |
258 | } | |
259 | ||
260 | m_dateTime = wxFileModificationTime(m_filePath); | |
261 | ||
262 | #else | |
263 | ||
264 | // OTHER PLATFORMS | |
265 | ||
8115f9e7 | 266 | wxStructStat buff; |
479cd5de | 267 | |
144cf6da | 268 | #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS)) |
06cc1fb9 | 269 | lstat( m_filePath.fn_str(), &buff ); |
23254b13 | 270 | m_type |= S_ISLNK( buff.st_mode ) != 0 ? is_link : 0; |
7f04165e | 271 | #else // no lstat() |
f3660dcb SC |
272 | // only translate to file charset if we don't go by our |
273 | // wxStat implementation | |
274 | #ifndef wxNEED_WX_UNISTD_H | |
275 | wxStat( m_filePath.fn_str() , &buff ); | |
276 | #else | |
06cc1fb9 | 277 | wxStat( m_filePath, &buff ); |
f3660dcb | 278 | #endif |
9f2d09aa RR |
279 | #endif |
280 | ||
23254b13 JS |
281 | m_type |= (buff.st_mode & S_IFDIR) != 0 ? is_dir : 0; |
282 | m_type |= (buff.st_mode & wxS_IXUSR) != 0 ? is_exe : 0; | |
8b17ba72 | 283 | |
ec524671 | 284 | m_size = buff.st_size; |
8b17ba72 | 285 | |
06cc1fb9 | 286 | m_dateTime = buff.st_mtime; |
619111b9 JS |
287 | #endif |
288 | // __WXWINCE__ | |
c81c9392 | 289 | |
06cc1fb9 JS |
290 | #if defined(__UNIX__) |
291 | m_permissions.Printf(_T("%c%c%c%c%c%c%c%c%c"), | |
7f04165e VZ |
292 | buff.st_mode & wxS_IRUSR ? _T('r') : _T('-'), |
293 | buff.st_mode & wxS_IWUSR ? _T('w') : _T('-'), | |
06cc1fb9 JS |
294 | buff.st_mode & wxS_IXUSR ? _T('x') : _T('-'), |
295 | buff.st_mode & wxS_IRGRP ? _T('r') : _T('-'), | |
296 | buff.st_mode & wxS_IWGRP ? _T('w') : _T('-'), | |
297 | buff.st_mode & wxS_IXGRP ? _T('x') : _T('-'), | |
298 | buff.st_mode & wxS_IROTH ? _T('r') : _T('-'), | |
299 | buff.st_mode & wxS_IWOTH ? _T('w') : _T('-'), | |
300 | buff.st_mode & wxS_IXOTH ? _T('x') : _T('-')); | |
301 | #elif defined(__WIN32__) | |
0e4e783c | 302 | DWORD attribs = ::GetFileAttributes(m_filePath.c_str()); |
06cc1fb9 JS |
303 | if (attribs != (DWORD)-1) |
304 | { | |
305 | m_permissions.Printf(_T("%c%c%c%c"), | |
306 | attribs & FILE_ATTRIBUTE_ARCHIVE ? _T('A') : _T(' '), | |
307 | attribs & FILE_ATTRIBUTE_READONLY ? _T('R') : _T(' '), | |
308 | attribs & FILE_ATTRIBUTE_HIDDEN ? _T('H') : _T(' '), | |
309 | attribs & FILE_ATTRIBUTE_SYSTEM ? _T('S') : _T(' ')); | |
310 | } | |
311 | #endif | |
619111b9 JS |
312 | |
313 | // try to get a better icon | |
314 | if (m_image == wxFileIconsTable::file) | |
315 | { | |
316 | if (m_fileName.Find(wxT('.'), true) != wxNOT_FOUND) | |
317 | { | |
318 | m_image = wxTheFileIconsTable->GetIconID( m_fileName.AfterLast(wxT('.'))); | |
319 | } else if (IsExe()) | |
320 | { | |
321 | m_image = wxFileIconsTable::executable; | |
322 | } | |
323 | } | |
8b17ba72 RR |
324 | } |
325 | ||
b600ed13 | 326 | wxString wxFileData::GetFileType() const |
8b17ba72 | 327 | { |
06cc1fb9 JS |
328 | if (IsDir()) |
329 | return _("<DIR>"); | |
330 | else if (IsLink()) | |
331 | return _("<LINK>"); | |
332 | else if (IsDrive()) | |
333 | return _("<DRIVE>"); | |
7448de8d | 334 | else if (m_fileName.Find(wxT('.'), true) != wxNOT_FOUND) |
06cc1fb9 | 335 | return m_fileName.AfterLast(wxT('.')); |
8b17ba72 | 336 | |
06cc1fb9 | 337 | return wxEmptyString; |
8b17ba72 RR |
338 | } |
339 | ||
06cc1fb9 | 340 | wxString wxFileData::GetModificationTime() const |
23254b13 | 341 | { |
06cc1fb9 JS |
342 | // want time as 01:02 so they line up nicely, no %r in WIN32 |
343 | return m_dateTime.FormatDate() + wxT(" ") + m_dateTime.Format(wxT("%I:%M:%S %p")); | |
23254b13 JS |
344 | } |
345 | ||
8b17ba72 RR |
346 | wxString wxFileData::GetHint() const |
347 | { | |
06cc1fb9 | 348 | wxString s = m_filePath; |
2b5f62a0 | 349 | s += wxT(" "); |
b8985048 | 350 | |
23254b13 | 351 | if (IsDir()) |
b8985048 | 352 | s += _("<DIR>"); |
23254b13 | 353 | else if (IsLink()) |
b8985048 | 354 | s += _("<LINK>"); |
23254b13 | 355 | else if (IsDrive()) |
b8985048 VZ |
356 | s += _("<DRIVE>"); |
357 | else // plain file | |
ec524671 VZ |
358 | s += wxString::Format(_("%ld bytes"), |
359 | wxLongLong(m_size).ToString().c_str()); | |
b8985048 VZ |
360 | |
361 | s += wxT(' '); | |
362 | ||
363 | if ( !IsDrive() ) | |
23254b13 | 364 | { |
b8985048 VZ |
365 | s << GetModificationTime() |
366 | << wxT(" ") | |
367 | << m_permissions; | |
8b17ba72 | 368 | } |
23254b13 | 369 | |
8b17ba72 | 370 | return s; |
17a1ebd1 | 371 | } |
8b17ba72 | 372 | |
23254b13 | 373 | wxString wxFileData::GetEntry( fileListFieldType num ) const |
8b17ba72 RR |
374 | { |
375 | wxString s; | |
9cedab37 | 376 | switch ( num ) |
8b17ba72 | 377 | { |
9cedab37 | 378 | case FileList_Name: |
06cc1fb9 | 379 | s = m_fileName; |
8b17ba72 | 380 | break; |
9cedab37 | 381 | |
06cc1fb9 JS |
382 | case FileList_Size: |
383 | if (!IsDir() && !IsLink() && !IsDrive()) | |
ec524671 | 384 | s = wxLongLong(m_size).ToString(); |
8b17ba72 | 385 | break; |
9cedab37 | 386 | |
06cc1fb9 | 387 | case FileList_Type: |
b600ed13 | 388 | s = GetFileType(); |
8b17ba72 | 389 | break; |
8b17ba72 | 390 | |
9cedab37 | 391 | case FileList_Time: |
7448de8d | 392 | if (!IsDrive()) |
06cc1fb9 | 393 | s = GetModificationTime(); |
9cedab37 | 394 | break; |
8b17ba72 | 395 | |
06cc1fb9 | 396 | #if defined(__UNIX__) || defined(__WIN32__) |
9cedab37 VZ |
397 | case FileList_Perm: |
398 | s = m_permissions; | |
399 | break; | |
06cc1fb9 | 400 | #endif // defined(__UNIX__) || defined(__WIN32__) |
8b17ba72 | 401 | |
9cedab37 VZ |
402 | default: |
403 | wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") ); | |
404 | } | |
8b17ba72 | 405 | |
9cedab37 | 406 | return s; |
8b17ba72 RR |
407 | } |
408 | ||
06cc1fb9 | 409 | void wxFileData::SetNewName( const wxString &filePath, const wxString &fileName ) |
8b17ba72 | 410 | { |
06cc1fb9 JS |
411 | m_fileName = fileName; |
412 | m_filePath = filePath; | |
8b17ba72 RR |
413 | } |
414 | ||
415 | void wxFileData::MakeItem( wxListItem &item ) | |
416 | { | |
06cc1fb9 | 417 | item.m_text = m_fileName; |
9b00bb16 | 418 | item.ClearAttributes(); |
9cedab37 VZ |
419 | if (IsExe()) |
420 | item.SetTextColour(*wxRED); | |
421 | if (IsDir()) | |
422 | item.SetTextColour(*wxBLUE); | |
48fe8374 | 423 | |
06cc1fb9 | 424 | item.m_image = m_image; |
655cf310 | 425 | |
8b17ba72 RR |
426 | if (IsLink()) |
427 | { | |
564a150b VZ |
428 | wxColour dg = wxTheColourDatabase->Find( _T("MEDIUM GREY") ); |
429 | if ( dg.Ok() ) | |
430 | item.SetTextColour(dg); | |
8b17ba72 | 431 | } |
9f542367 | 432 | item.m_data = wxPtrToUInt(this); |
8b17ba72 | 433 | } |
c8c0e54c | 434 | |
8b17ba72 RR |
435 | //----------------------------------------------------------------------------- |
436 | // wxFileCtrl | |
437 | //----------------------------------------------------------------------------- | |
438 | ||
619111b9 JS |
439 | static bool ignoreChanges = false; |
440 | ||
d84afea9 | 441 | IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl) |
8b17ba72 RR |
442 | |
443 | BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl) | |
ca65c044 WS |
444 | EVT_LIST_DELETE_ITEM(wxID_ANY, wxFileCtrl::OnListDeleteItem) |
445 | EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY, wxFileCtrl::OnListDeleteAllItems) | |
446 | EVT_LIST_END_LABEL_EDIT(wxID_ANY, wxFileCtrl::OnListEndLabelEdit) | |
447 | EVT_LIST_COL_CLICK(wxID_ANY, wxFileCtrl::OnListColClick) | |
8b17ba72 RR |
448 | END_EVENT_TABLE() |
449 | ||
655cf310 | 450 | |
8b17ba72 RR |
451 | wxFileCtrl::wxFileCtrl() |
452 | { | |
ca65c044 | 453 | m_showHidden = false; |
23254b13 JS |
454 | m_sort_foward = 1; |
455 | m_sort_field = wxFileData::FileList_Name; | |
8b17ba72 RR |
456 | } |
457 | ||
9cedab37 VZ |
458 | wxFileCtrl::wxFileCtrl(wxWindow *win, |
459 | wxWindowID id, | |
460 | const wxString& wild, | |
461 | bool showHidden, | |
462 | const wxPoint& pos, | |
463 | const wxSize& size, | |
464 | long style, | |
465 | const wxValidator &validator, | |
5e673a6a | 466 | const wxString &name) |
9cedab37 VZ |
467 | : wxListCtrl(win, id, pos, size, style, validator, name), |
468 | m_wild(wild) | |
8b17ba72 | 469 | { |
06cc1fb9 | 470 | wxImageList *imageList = wxTheFileIconsTable->GetSmallImageList(); |
655cf310 | 471 | |
0b855868 | 472 | SetImageList( imageList, wxIMAGE_LIST_SMALL ); |
c8c0e54c | 473 | |
9cedab37 | 474 | m_showHidden = showHidden; |
23254b13 JS |
475 | |
476 | m_sort_foward = 1; | |
477 | m_sort_field = wxFileData::FileList_Name; | |
478 | ||
479 | m_dirName = wxT("*"); | |
480 | ||
481 | if (style & wxLC_REPORT) | |
482 | ChangeToReportMode(); | |
8b17ba72 RR |
483 | } |
484 | ||
485 | void wxFileCtrl::ChangeToListMode() | |
486 | { | |
23254b13 | 487 | ClearAll(); |
8b17ba72 | 488 | SetSingleStyle( wxLC_LIST ); |
8f4fcc4e | 489 | UpdateFiles(); |
8b17ba72 RR |
490 | } |
491 | ||
492 | void wxFileCtrl::ChangeToReportMode() | |
493 | { | |
23254b13 | 494 | ClearAll(); |
8b17ba72 | 495 | SetSingleStyle( wxLC_REPORT ); |
23254b13 | 496 | |
06cc1fb9 JS |
497 | // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy |
498 | // don't hardcode since mm/dd is dd/mm elsewhere | |
23254b13 | 499 | int w, h; |
06cc1fb9 JS |
500 | wxDateTime dt(22, wxDateTime::Dec, 2002, 22, 22, 22); |
501 | wxString txt = dt.FormatDate() + wxT("22") + dt.Format(wxT("%I:%M:%S %p")); | |
502 | GetTextExtent(txt, &w, &h); | |
503 | ||
504 | InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, w ); | |
505 | InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, w/2 ); | |
506 | InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT, w/2 ); | |
507 | InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT, w ); | |
508 | #if defined(__UNIX__) | |
509 | GetTextExtent(wxT("Permissions 2"), &w, &h); | |
510 | InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, w ); | |
511 | #elif defined(__WIN32__) | |
512 | GetTextExtent(wxT("Attributes 2"), &w, &h); | |
513 | InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT, w ); | |
23254b13 JS |
514 | #endif |
515 | ||
8f4fcc4e | 516 | UpdateFiles(); |
8b17ba72 RR |
517 | } |
518 | ||
06cc1fb9 | 519 | void wxFileCtrl::ChangeToSmallIconMode() |
8b17ba72 | 520 | { |
23254b13 | 521 | ClearAll(); |
06cc1fb9 | 522 | SetSingleStyle( wxLC_SMALL_ICON ); |
8f4fcc4e | 523 | UpdateFiles(); |
8b17ba72 RR |
524 | } |
525 | ||
526 | void wxFileCtrl::ShowHidden( bool show ) | |
527 | { | |
528 | m_showHidden = show; | |
8f4fcc4e | 529 | UpdateFiles(); |
8b17ba72 RR |
530 | } |
531 | ||
0b855868 RR |
532 | long wxFileCtrl::Add( wxFileData *fd, wxListItem &item ) |
533 | { | |
534 | long ret = -1; | |
535 | item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE; | |
536 | fd->MakeItem( item ); | |
537 | long my_style = GetWindowStyleFlag(); | |
538 | if (my_style & wxLC_REPORT) | |
539 | { | |
540 | ret = InsertItem( item ); | |
23254b13 JS |
541 | for (int i = 1; i < wxFileData::FileList_Max; i++) |
542 | SetItem( item.m_itemId, i, fd->GetEntry((wxFileData::fileListFieldType)i) ); | |
0b855868 | 543 | } |
06cc1fb9 | 544 | else if ((my_style & wxLC_LIST) || (my_style & wxLC_SMALL_ICON)) |
0b855868 RR |
545 | { |
546 | ret = InsertItem( item ); | |
c8c0e54c | 547 | } |
0b855868 RR |
548 | return ret; |
549 | } | |
550 | ||
b600ed13 VZ |
551 | void wxFileCtrl::UpdateItem(const wxListItem &item) |
552 | { | |
553 | wxFileData *fd = (wxFileData*)GetItemData(item); | |
554 | wxCHECK_RET(fd, wxT("invalid filedata")); | |
555 | ||
556 | fd->ReadData(); | |
557 | ||
558 | SetItemText(item, fd->GetFileName()); | |
c3627a00 | 559 | SetItemImage(item, fd->GetImageId()); |
b600ed13 VZ |
560 | |
561 | if (GetWindowStyleFlag() & wxLC_REPORT) | |
562 | { | |
563 | for (int i = 1; i < wxFileData::FileList_Max; i++) | |
564 | SetItem( item.m_itemId, i, fd->GetEntry((wxFileData::fileListFieldType)i) ); | |
565 | } | |
566 | } | |
567 | ||
8f4fcc4e | 568 | void wxFileCtrl::UpdateFiles() |
c8c0e54c | 569 | { |
2b5f62a0 | 570 | // don't do anything before ShowModal() call which sets m_dirName |
23254b13 | 571 | if ( m_dirName == wxT("*") ) |
2b5f62a0 VZ |
572 | return; |
573 | ||
37fd1c97 | 574 | wxBusyCursor bcur; // this may take a while... |
7a82dabc | 575 | |
23254b13 | 576 | DeleteAllItems(); |
7a82dabc | 577 | |
8b17ba72 | 578 | wxListItem item; |
8b17ba72 RR |
579 | item.m_itemId = 0; |
580 | item.m_col = 0; | |
0b855868 | 581 | |
619111b9 | 582 | #if (defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)) && !defined(__WXWINCE__) |
37fd1c97 | 583 | if ( IsTopMostDir(m_dirName) ) |
abc2a4cc | 584 | { |
06cc1fb9 JS |
585 | wxArrayString names, paths; |
586 | wxArrayInt icons; | |
587 | size_t n, count = wxGetAvailableDrives(paths, names, icons); | |
588 | ||
589 | for (n=0; n<count; n++) | |
abc2a4cc | 590 | { |
999836aa | 591 | wxFileData *fd = new wxFileData(paths[n], names[n], wxFileData::is_drive, icons[n]); |
81169710 | 592 | if (Add(fd, item) != -1) |
37fd1c97 | 593 | item.m_itemId++; |
81169710 VZ |
594 | else |
595 | delete fd; | |
37fd1c97 | 596 | } |
abc2a4cc | 597 | } |
5e673a6a | 598 | else |
06cc1fb9 | 599 | #endif // defined(__DOS__) || defined(__WINDOWS__) |
8b17ba72 | 600 | { |
5e673a6a | 601 | // Real directory... |
619111b9 | 602 | if ( !IsTopMostDir(m_dirName) && !m_dirName.empty() ) |
8b17ba72 | 603 | { |
5e673a6a | 604 | wxString p(wxPathOnly(m_dirName)); |
619111b9 | 605 | #if (defined(__UNIX__) || defined(__WXWINCE__)) && !defined(__OS2__) |
abc2a4cc | 606 | if (p.empty()) p = wxT("/"); |
06cc1fb9 | 607 | #endif // __UNIX__ |
999836aa | 608 | wxFileData *fd = new wxFileData(p, wxT(".."), wxFileData::is_dir, wxFileIconsTable::folder); |
81169710 | 609 | if (Add(fd, item) != -1) |
7448de8d | 610 | item.m_itemId++; |
81169710 VZ |
611 | else |
612 | delete fd; | |
8b17ba72 | 613 | } |
c8c0e54c | 614 | |
5e673a6a | 615 | wxString dirname(m_dirName); |
144cf6da | 616 | #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__) |
5e673a6a VS |
617 | if (dirname.length() == 2 && dirname[1u] == wxT(':')) |
618 | dirname << wxT('\\'); | |
144cf6da | 619 | #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__) |
619111b9 JS |
620 | |
621 | if (dirname.empty()) | |
622 | dirname = wxFILE_SEP_PATH; | |
623 | ||
624 | wxLogNull logNull; | |
5e673a6a VS |
625 | wxDir dir(dirname); |
626 | ||
627 | if ( dir.IsOpened() ) | |
cae5359f | 628 | { |
23254b13 JS |
629 | wxString dirPrefix(dirname); |
630 | if (dirPrefix.Last() != wxFILE_SEP_PATH) | |
631 | dirPrefix += wxFILE_SEP_PATH; | |
632 | ||
5e673a6a | 633 | int hiddenFlag = m_showHidden ? wxDIR_HIDDEN : 0; |
7a82dabc | 634 | |
5e673a6a VS |
635 | bool cont; |
636 | wxString f; | |
637 | ||
638 | // Get the directories first (not matched against wildcards): | |
639 | cont = dir.GetFirst(&f, wxEmptyString, wxDIR_DIRS | hiddenFlag); | |
640 | while (cont) | |
551adc4b | 641 | { |
999836aa | 642 | wxFileData *fd = new wxFileData(dirPrefix + f, f, wxFileData::is_dir, wxFileIconsTable::folder); |
81169710 | 643 | if (Add(fd, item) != -1) |
7448de8d | 644 | item.m_itemId++; |
81169710 VZ |
645 | else |
646 | delete fd; | |
647 | ||
5e673a6a VS |
648 | cont = dir.GetNext(&f); |
649 | } | |
650 | ||
7a82dabc | 651 | // Tokenize the wildcard string, so we can handle more than 1 |
5e673a6a VS |
652 | // search pattern in a wildcard. |
653 | wxStringTokenizer tokenWild(m_wild, wxT(";")); | |
654 | while ( tokenWild.HasMoreTokens() ) | |
655 | { | |
7a82dabc | 656 | cont = dir.GetFirst(&f, tokenWild.GetNextToken(), |
5e673a6a VS |
657 | wxDIR_FILES | hiddenFlag); |
658 | while (cont) | |
659 | { | |
999836aa | 660 | wxFileData *fd = new wxFileData(dirPrefix + f, f, wxFileData::is_file, wxFileIconsTable::file); |
81169710 | 661 | if (Add(fd, item) != -1) |
7448de8d | 662 | item.m_itemId++; |
81169710 VZ |
663 | else |
664 | delete fd; | |
665 | ||
5e673a6a VS |
666 | cont = dir.GetNext(&f); |
667 | } | |
551adc4b | 668 | } |
cae5359f | 669 | } |
cae5359f | 670 | } |
c8c0e54c | 671 | |
06cc1fb9 | 672 | SortItems(m_sort_field, m_sort_foward); |
8b17ba72 RR |
673 | } |
674 | ||
0b855868 | 675 | void wxFileCtrl::SetWild( const wxString &wild ) |
8b17ba72 | 676 | { |
06cc1fb9 JS |
677 | if (wild.Find(wxT('|')) != wxNOT_FOUND) |
678 | return; | |
679 | ||
0b855868 | 680 | m_wild = wild; |
8f4fcc4e | 681 | UpdateFiles(); |
0b855868 RR |
682 | } |
683 | ||
684 | void wxFileCtrl::MakeDir() | |
685 | { | |
5e673a6a | 686 | wxString new_name( _("NewName") ); |
0b855868 | 687 | wxString path( m_dirName ); |
75ec8bd4 | 688 | path += wxFILE_SEP_PATH; |
0b855868 RR |
689 | path += new_name; |
690 | if (wxFileExists(path)) | |
8b17ba72 | 691 | { |
0b855868 RR |
692 | // try NewName0, NewName1 etc. |
693 | int i = 0; | |
c8c0e54c | 694 | do { |
0b855868 | 695 | new_name = _("NewName"); |
c8c0e54c | 696 | wxString num; |
223d09f6 | 697 | num.Printf( wxT("%d"), i ); |
c8c0e54c VZ |
698 | new_name += num; |
699 | ||
0b855868 | 700 | path = m_dirName; |
75ec8bd4 | 701 | path += wxFILE_SEP_PATH; |
0b855868 | 702 | path += new_name; |
c8c0e54c VZ |
703 | i++; |
704 | } while (wxFileExists(path)); | |
8b17ba72 | 705 | } |
c8c0e54c | 706 | |
0b855868 | 707 | wxLogNull log; |
c8c0e54c | 708 | if (!wxMkdir(path)) |
8b17ba72 | 709 | { |
0b855868 | 710 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 711 | dialog.ShowModal(); |
0b855868 | 712 | return; |
8b17ba72 | 713 | } |
8b17ba72 | 714 | |
06cc1fb9 | 715 | wxFileData *fd = new wxFileData( path, new_name, wxFileData::is_dir, wxFileIconsTable::folder ); |
0b855868 RR |
716 | wxListItem item; |
717 | item.m_itemId = 0; | |
718 | item.m_col = 0; | |
13111b2a | 719 | long id = Add( fd, item ); |
c8c0e54c | 720 | |
0b855868 RR |
721 | if (id != -1) |
722 | { | |
06cc1fb9 | 723 | SortItems(m_sort_field, m_sort_foward); |
9f542367 | 724 | id = FindItem( 0, wxPtrToUInt(fd) ); |
0b855868 RR |
725 | EnsureVisible( id ); |
726 | EditLabel( id ); | |
727 | } | |
81169710 VZ |
728 | else |
729 | delete fd; | |
8b17ba72 RR |
730 | } |
731 | ||
732 | void wxFileCtrl::GoToParentDir() | |
733 | { | |
5e673a6a | 734 | if (!IsTopMostDir(m_dirName)) |
8b17ba72 | 735 | { |
b36e08d0 | 736 | size_t len = m_dirName.length(); |
083f7497 | 737 | if (wxEndsWithPathSeparator(m_dirName)) |
353f41cb | 738 | m_dirName.Remove( len-1, 1 ); |
c8c0e54c | 739 | wxString fname( wxFileNameFromPath(m_dirName) ); |
0b855868 | 740 | m_dirName = wxPathOnly( m_dirName ); |
144cf6da | 741 | #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__) |
abc2a4cc | 742 | if (!m_dirName.empty()) |
23254b13 JS |
743 | { |
744 | if (m_dirName.Last() == wxT('.')) | |
ca65c044 | 745 | m_dirName = wxEmptyString; |
23254b13 JS |
746 | } |
747 | #elif defined(__UNIX__) | |
abc2a4cc | 748 | if (m_dirName.empty()) |
5e673a6a VS |
749 | m_dirName = wxT("/"); |
750 | #endif | |
8f4fcc4e | 751 | UpdateFiles(); |
13111b2a | 752 | long id = FindItem( 0, fname ); |
abc2a4cc | 753 | if (id != wxNOT_FOUND) |
c8c0e54c | 754 | { |
619111b9 | 755 | ignoreChanges = true; |
c8c0e54c VZ |
756 | SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); |
757 | EnsureVisible( id ); | |
619111b9 | 758 | ignoreChanges = false; |
c8c0e54c | 759 | } |
8b17ba72 RR |
760 | } |
761 | } | |
762 | ||
763 | void wxFileCtrl::GoToHomeDir() | |
764 | { | |
765 | wxString s = wxGetUserHome( wxString() ); | |
5e673a6a | 766 | GoToDir(s); |
8b17ba72 RR |
767 | } |
768 | ||
769 | void wxFileCtrl::GoToDir( const wxString &dir ) | |
770 | { | |
da865fdd | 771 | if (!wxDirExists(dir)) return; |
06cc1fb9 | 772 | |
8b17ba72 | 773 | m_dirName = dir; |
8f4fcc4e | 774 | UpdateFiles(); |
619111b9 JS |
775 | |
776 | ignoreChanges = true; | |
e6daf794 | 777 | SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); |
619111b9 JS |
778 | ignoreChanges = false; |
779 | ||
e6daf794 | 780 | EnsureVisible( 0 ); |
8b17ba72 RR |
781 | } |
782 | ||
81169710 | 783 | void wxFileCtrl::FreeItemData(wxListItem& item) |
8b17ba72 | 784 | { |
81169710 VZ |
785 | if ( item.m_data ) |
786 | { | |
787 | wxFileData *fd = (wxFileData*)item.m_data; | |
788 | delete fd; | |
789 | ||
790 | item.m_data = 0; | |
791 | } | |
8b17ba72 RR |
792 | } |
793 | ||
7a82dabc VZ |
794 | void wxFileCtrl::OnListDeleteItem( wxListEvent &event ) |
795 | { | |
796 | FreeItemData(event.m_item); | |
797 | } | |
798 | ||
a3a363f6 | 799 | void wxFileCtrl::OnListDeleteAllItems( wxListEvent & WXUNUSED(event) ) |
81169710 VZ |
800 | { |
801 | FreeAllItemsData(); | |
802 | } | |
803 | ||
7a82dabc | 804 | void wxFileCtrl::FreeAllItemsData() |
12c1b46a RR |
805 | { |
806 | wxListItem item; | |
807 | item.m_mask = wxLIST_MASK_DATA; | |
808 | ||
809 | item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL ); | |
92da8bde | 810 | while ( item.m_itemId != -1 ) |
12c1b46a RR |
811 | { |
812 | GetItem( item ); | |
7a82dabc | 813 | FreeItemData(item); |
12c1b46a RR |
814 | item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL ); |
815 | } | |
816 | } | |
817 | ||
0b855868 | 818 | void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event ) |
8b17ba72 RR |
819 | { |
820 | wxFileData *fd = (wxFileData*)event.m_item.m_data; | |
0b855868 | 821 | wxASSERT( fd ); |
c8c0e54c | 822 | |
abc2a4cc | 823 | if ((event.GetLabel().empty()) || |
0b855868 RR |
824 | (event.GetLabel() == _(".")) || |
825 | (event.GetLabel() == _("..")) || | |
75ec8bd4 | 826 | (event.GetLabel().First( wxFILE_SEP_PATH ) != wxNOT_FOUND)) |
8b17ba72 | 827 | { |
0b855868 | 828 | wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 829 | dialog.ShowModal(); |
0b855868 | 830 | event.Veto(); |
c8c0e54c | 831 | return; |
8b17ba72 | 832 | } |
c8c0e54c | 833 | |
06cc1fb9 | 834 | wxString new_name( wxPathOnly( fd->GetFilePath() ) ); |
75ec8bd4 | 835 | new_name += wxFILE_SEP_PATH; |
0b855868 | 836 | new_name += event.GetLabel(); |
c8c0e54c | 837 | |
0b855868 | 838 | wxLogNull log; |
c8c0e54c | 839 | |
0b855868 | 840 | if (wxFileExists(new_name)) |
8b17ba72 | 841 | { |
0b855868 | 842 | wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 843 | dialog.ShowModal(); |
0b855868 | 844 | event.Veto(); |
8b17ba72 | 845 | } |
c8c0e54c | 846 | |
06cc1fb9 | 847 | if (wxRenameFile(fd->GetFilePath(),new_name)) |
8b17ba72 | 848 | { |
0b855868 | 849 | fd->SetNewName( new_name, event.GetLabel() ); |
619111b9 JS |
850 | |
851 | ignoreChanges = true; | |
c8c0e54c | 852 | SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); |
619111b9 JS |
853 | ignoreChanges = false; |
854 | ||
b600ed13 | 855 | UpdateItem( event.GetItem() ); |
0b855868 | 856 | EnsureVisible( event.GetItem() ); |
8b17ba72 RR |
857 | } |
858 | else | |
859 | { | |
0b855868 | 860 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); |
c8c0e54c | 861 | dialog.ShowModal(); |
0b855868 | 862 | event.Veto(); |
8b17ba72 | 863 | } |
8b17ba72 RR |
864 | } |
865 | ||
23254b13 JS |
866 | void wxFileCtrl::OnListColClick( wxListEvent &event ) |
867 | { | |
868 | int col = event.GetColumn(); | |
869 | ||
870 | switch (col) | |
871 | { | |
872 | case wxFileData::FileList_Name : | |
06cc1fb9 | 873 | case wxFileData::FileList_Size : |
23254b13 | 874 | case wxFileData::FileList_Type : |
23254b13 JS |
875 | case wxFileData::FileList_Time : break; |
876 | default : return; | |
877 | } | |
878 | ||
879 | if ((wxFileData::fileListFieldType)col == m_sort_field) | |
06cc1fb9 | 880 | m_sort_foward = !m_sort_foward; |
23254b13 JS |
881 | else |
882 | m_sort_field = (wxFileData::fileListFieldType)col; | |
883 | ||
06cc1fb9 | 884 | SortItems(m_sort_field, m_sort_foward); |
23254b13 JS |
885 | } |
886 | ||
ec524671 | 887 | void wxFileCtrl::SortItems(wxFileData::fileListFieldType field, bool forward) |
23254b13 JS |
888 | { |
889 | m_sort_field = field; | |
ec524671 VZ |
890 | m_sort_foward = forward; |
891 | const long sort_dir = forward ? 1 : -1; | |
23254b13 JS |
892 | |
893 | switch (m_sort_field) | |
894 | { | |
06cc1fb9 | 895 | case wxFileData::FileList_Size : |
ec524671 | 896 | wxListCtrl::SortItems(wxFileDataSizeCompare, sort_dir); |
23254b13 | 897 | break; |
ec524671 | 898 | |
23254b13 | 899 | case wxFileData::FileList_Type : |
ec524671 VZ |
900 | wxListCtrl::SortItems(wxFileDataTypeCompare, sort_dir); |
901 | break; | |
902 | ||
23254b13 | 903 | case wxFileData::FileList_Time : |
ec524671 VZ |
904 | wxListCtrl::SortItems(wxFileDataTimeCompare, sort_dir); |
905 | break; | |
812a5849 WS |
906 | |
907 | case wxFileData::FileList_Name : | |
908 | default : | |
909 | wxListCtrl::SortItems(wxFileDataNameCompare, sort_dir); | |
910 | break; | |
23254b13 JS |
911 | } |
912 | } | |
913 | ||
7a82dabc VZ |
914 | wxFileCtrl::~wxFileCtrl() |
915 | { | |
118322a3 WS |
916 | // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and |
917 | // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after | |
918 | // the destruction of the wxFileCtrl we need to free any data here: | |
919 | FreeAllItemsData(); | |
7a82dabc VZ |
920 | } |
921 | ||
8b17ba72 | 922 | //----------------------------------------------------------------------------- |
23254b13 | 923 | // wxGenericFileDialog |
8b17ba72 RR |
924 | //----------------------------------------------------------------------------- |
925 | ||
5e673a6a VS |
926 | #define ID_LIST_MODE (wxID_FILEDLGG ) |
927 | #define ID_REPORT_MODE (wxID_FILEDLGG + 1) | |
928 | #define ID_UP_DIR (wxID_FILEDLGG + 5) | |
929 | #define ID_PARENT_DIR (wxID_FILEDLGG + 6) | |
930 | #define ID_NEW_DIR (wxID_FILEDLGG + 7) | |
931 | #define ID_CHOICE (wxID_FILEDLGG + 8) | |
932 | #define ID_TEXT (wxID_FILEDLGG + 9) | |
933 | #define ID_LIST_CTRL (wxID_FILEDLGG + 10) | |
5e673a6a | 934 | #define ID_CHECK (wxID_FILEDLGG + 12) |
8b17ba72 | 935 | |
f74172ab | 936 | IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog, wxFileDialogBase) |
23254b13 JS |
937 | |
938 | BEGIN_EVENT_TABLE(wxGenericFileDialog,wxDialog) | |
939 | EVT_BUTTON(ID_LIST_MODE, wxGenericFileDialog::OnList) | |
940 | EVT_BUTTON(ID_REPORT_MODE, wxGenericFileDialog::OnReport) | |
941 | EVT_BUTTON(ID_UP_DIR, wxGenericFileDialog::OnUp) | |
942 | EVT_BUTTON(ID_PARENT_DIR, wxGenericFileDialog::OnHome) | |
943 | EVT_BUTTON(ID_NEW_DIR, wxGenericFileDialog::OnNew) | |
944 | EVT_BUTTON(wxID_OK, wxGenericFileDialog::OnListOk) | |
945 | EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxGenericFileDialog::OnSelected) | |
946 | EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxGenericFileDialog::OnActivated) | |
947 | EVT_CHOICE(ID_CHOICE,wxGenericFileDialog::OnChoiceFilter) | |
948 | EVT_TEXT_ENTER(ID_TEXT,wxGenericFileDialog::OnTextEnter) | |
949 | EVT_TEXT(ID_TEXT,wxGenericFileDialog::OnTextChange) | |
950 | EVT_CHECKBOX(ID_CHECK,wxGenericFileDialog::OnCheck) | |
8b17ba72 RR |
951 | END_EVENT_TABLE() |
952 | ||
23254b13 | 953 | long wxGenericFileDialog::ms_lastViewStyle = wxLC_LIST; |
ca65c044 | 954 | bool wxGenericFileDialog::ms_lastShowHidden = false; |
655cf310 | 955 | |
fe6cf128 VZ |
956 | void wxGenericFileDialog::Init() |
957 | { | |
958 | m_bypassGenericImpl = false; | |
959 | ||
960 | m_choice = NULL; | |
961 | m_text = NULL; | |
962 | m_list = NULL; | |
963 | m_check = NULL; | |
964 | m_static = NULL; | |
965 | m_upDirButton = NULL; | |
966 | m_newDirButton = NULL; | |
967 | } | |
968 | ||
23254b13 | 969 | wxGenericFileDialog::wxGenericFileDialog(wxWindow *parent, |
9cedab37 VZ |
970 | const wxString& message, |
971 | const wxString& defaultDir, | |
972 | const wxString& defaultFile, | |
973 | const wxString& wildCard, | |
fe6cf128 | 974 | long style, |
4e1901b7 | 975 | const wxPoint& pos, |
ff3e84ff VZ |
976 | const wxSize& sz, |
977 | const wxString& name, | |
fe6cf128 | 978 | bool bypassGenericImpl ) : wxFileDialogBase() |
8b17ba72 | 979 | { |
fe6cf128 | 980 | Init(); |
ff3e84ff | 981 | Create( parent, message, defaultDir, defaultFile, wildCard, style, pos, sz, name, bypassGenericImpl ); |
4e1901b7 RR |
982 | } |
983 | ||
984 | bool wxGenericFileDialog::Create( wxWindow *parent, | |
abc2a4cc | 985 | const wxString& message, |
fe6cf128 | 986 | const wxString& defaultDir, |
abc2a4cc WS |
987 | const wxString& defaultFile, |
988 | const wxString& wildCard, | |
fe6cf128 VZ |
989 | long style, |
990 | const wxPoint& pos, | |
ff3e84ff VZ |
991 | const wxSize& sz, |
992 | const wxString& name, | |
fe6cf128 | 993 | bool bypassGenericImpl ) |
4e1901b7 | 994 | { |
fe6cf128 VZ |
995 | m_bypassGenericImpl = bypassGenericImpl; |
996 | ||
7e4fb3b8 | 997 | if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFile, |
ff3e84ff | 998 | wildCard, style, pos, sz, name)) |
fe6cf128 VZ |
999 | { |
1000 | return false; | |
1001 | } | |
1002 | ||
1003 | if (m_bypassGenericImpl) | |
1004 | return true; | |
1005 | ||
ff3e84ff VZ |
1006 | if (!wxDialog::Create( parent, wxID_ANY, message, pos, sz, |
1007 | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER, name | |
c81c9392 | 1008 | )) |
4e1901b7 RR |
1009 | { |
1010 | return false; | |
1011 | } | |
f74172ab | 1012 | |
619111b9 JS |
1013 | ignoreChanges = true; |
1014 | ||
ca65c044 | 1015 | if (wxConfig::Get(false)) |
eaf40b23 | 1016 | { |
e7d9c398 | 1017 | wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), |
9cedab37 | 1018 | &ms_lastViewStyle); |
e7d9c398 | 1019 | wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), |
9cedab37 | 1020 | &ms_lastShowHidden); |
eaf40b23 VS |
1021 | } |
1022 | ||
353f41cb | 1023 | if ((m_dir.empty()) || (m_dir == wxT("."))) |
ac2def68 | 1024 | { |
75ec8bd4 | 1025 | m_dir = wxGetCwd(); |
619111b9 JS |
1026 | if (m_dir.empty()) |
1027 | m_dir = wxFILE_SEP_PATH; | |
ac2def68 | 1028 | } |
7a82dabc | 1029 | |
b36e08d0 | 1030 | size_t len = m_dir.length(); |
083f7497 | 1031 | if ((len > 1) && (wxEndsWithPathSeparator(m_dir))) |
353f41cb RR |
1032 | m_dir.Remove( len-1, 1 ); |
1033 | ||
1034 | m_path = m_dir; | |
75ec8bd4 | 1035 | m_path += wxFILE_SEP_PATH; |
8b17ba72 | 1036 | m_path += defaultFile; |
655cf310 | 1037 | m_filterExtension = wxEmptyString; |
c8c0e54c | 1038 | |
cae5359f | 1039 | // layout |
4f5c180e | 1040 | |
2b5f62a0 | 1041 | bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA); |
c8c0e54c | 1042 | |
8b17ba72 | 1043 | wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL ); |
c8c0e54c | 1044 | |
8b17ba72 | 1045 | wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL ); |
c8c0e54c | 1046 | |
e6daf794 | 1047 | wxBitmapButton *but; |
c8c0e54c | 1048 | |
7a82dabc | 1049 | but = new wxBitmapButton(this, ID_LIST_MODE, |
36668b35 | 1050 | wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_BUTTON)); |
e6daf794 RR |
1051 | #if wxUSE_TOOLTIPS |
1052 | but->SetToolTip( _("View files as a list view") ); | |
1053 | #endif | |
1054 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
c8c0e54c | 1055 | |
60d2cc25 | 1056 | but = new wxBitmapButton(this, ID_REPORT_MODE, |
36668b35 | 1057 | wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_BUTTON)); |
e6daf794 RR |
1058 | #if wxUSE_TOOLTIPS |
1059 | but->SetToolTip( _("View files as a detailed view") ); | |
1060 | #endif | |
1061 | buttonsizer->Add( but, 0, wxALL, 5 ); | |
c8c0e54c | 1062 | |
0b855868 | 1063 | buttonsizer->Add( 30, 5, 1 ); |
e6daf794 | 1064 | |
06cc1fb9 | 1065 | m_upDirButton = new wxBitmapButton(this, ID_UP_DIR, |
36668b35 | 1066 | wxArtProvider::GetBitmap(wxART_GO_DIR_UP, wxART_BUTTON)); |
e6daf794 | 1067 | #if wxUSE_TOOLTIPS |
06cc1fb9 | 1068 | m_upDirButton->SetToolTip( _("Go to parent directory") ); |
e6daf794 | 1069 | #endif |
06cc1fb9 | 1070 | buttonsizer->Add( m_upDirButton, 0, wxALL, 5 ); |
c8c0e54c | 1071 | |
37fd1c97 | 1072 | #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS... |
60d2cc25 | 1073 | but = new wxBitmapButton(this, ID_PARENT_DIR, |
36668b35 | 1074 | wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_BUTTON)); |
e6daf794 RR |
1075 | #if wxUSE_TOOLTIPS |
1076 | but->SetToolTip( _("Go to home directory") ); | |
1077 | #endif | |
1078 | buttonsizer->Add( but, 0, wxALL, 5); | |
c8c0e54c | 1079 | |
e6daf794 | 1080 | buttonsizer->Add( 20, 20 ); |
75ec8bd4 | 1081 | #endif //!__DOS__ |
c8c0e54c | 1082 | |
06cc1fb9 | 1083 | m_newDirButton = new wxBitmapButton(this, ID_NEW_DIR, |
36668b35 | 1084 | wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_BUTTON)); |
e6daf794 | 1085 | #if wxUSE_TOOLTIPS |
06cc1fb9 | 1086 | m_newDirButton->SetToolTip( _("Create new directory") ); |
e6daf794 | 1087 | #endif |
06cc1fb9 | 1088 | buttonsizer->Add( m_newDirButton, 0, wxALL, 5 ); |
c8c0e54c | 1089 | |
2b5f62a0 | 1090 | if (is_pda) |
c15521c6 RR |
1091 | mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 0 ); |
1092 | else | |
1093 | mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 ); | |
c8c0e54c | 1094 | |
9c884972 | 1095 | wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL ); |
2b5f62a0 | 1096 | if (is_pda) |
ca65c044 WS |
1097 | staticsizer->Add( new wxStaticText( this, wxID_ANY, _("Current directory:") ), 0, wxRIGHT, 10 ); |
1098 | m_static = new wxStaticText( this, wxID_ANY, m_dir ); | |
9c884972 RR |
1099 | staticsizer->Add( m_static, 1 ); |
1100 | mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 ); | |
1101 | ||
619111b9 | 1102 | long style2 = ms_lastViewStyle; |
ff3e84ff | 1103 | if ( !HasFlag(wxFD_MULTIPLE) ) |
cd6b752b | 1104 | style2 |= wxLC_SINGLE_SEL; |
9cedab37 | 1105 | |
619111b9 JS |
1106 | #ifdef __WXWINCE__ |
1107 | style2 |= wxSIMPLE_BORDER; | |
1108 | #else | |
1109 | style2 |= wxSUNKEN_BORDER; | |
1110 | #endif | |
1111 | ||
23254b13 | 1112 | m_list = new wxFileCtrl( this, ID_LIST_CTRL, |
9548f380 | 1113 | wxEmptyString, ms_lastShowHidden, |
9cedab37 | 1114 | wxDefaultPosition, wxSize(540,200), |
cd6b752b | 1115 | style2); |
9cedab37 | 1116 | |
2b5f62a0 | 1117 | if (is_pda) |
c15521c6 | 1118 | { |
41fecb44 VS |
1119 | // PDAs have a different screen layout |
1120 | mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 ); | |
1121 | ||
41fecb44 | 1122 | wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL ); |
8e13c1ec | 1123 | m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER ); |
41fecb44 VS |
1124 | textsizer->Add( m_text, 1, wxCENTER | wxALL, 5 ); |
1125 | mainsizer->Add( textsizer, 0, wxEXPAND ); | |
1126 | ||
ca06ee0d RR |
1127 | m_check = NULL; |
1128 | m_choice = new wxChoice( this, ID_CHOICE ); | |
1129 | textsizer->Add( m_choice, 1, wxCENTER|wxALL, 5 ); | |
41fecb44 | 1130 | |
c81c9392 WS |
1131 | wxSizer *bsizer = CreateButtonSizer( wxOK|wxCANCEL , false, 5 ); |
1132 | if(bsizer->GetChildren().GetCount() > 0 ) | |
1133 | { | |
1134 | mainsizer->Add( bsizer, 0, wxEXPAND | wxALL, 5 ); | |
1135 | } | |
1136 | else | |
1137 | { | |
1138 | delete bsizer; | |
1139 | } | |
c15521c6 RR |
1140 | } |
1141 | else | |
1142 | { | |
41fecb44 VS |
1143 | mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 ); |
1144 | ||
1145 | wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL ); | |
8e13c1ec | 1146 | m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER ); |
41fecb44 | 1147 | textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 ); |
5f7bcb48 | 1148 | textsizer->Add( new wxButton( this, wxID_OK ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 ); |
41fecb44 VS |
1149 | mainsizer->Add( textsizer, 0, wxEXPAND ); |
1150 | ||
1151 | wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL ); | |
1152 | m_choice = new wxChoice( this, ID_CHOICE ); | |
1153 | choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 ); | |
1154 | m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") ); | |
9cedab37 | 1155 | m_check->SetValue( ms_lastShowHidden ); |
41fecb44 | 1156 | choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 ); |
5f7bcb48 | 1157 | choicesizer->Add( new wxButton( this, wxID_CANCEL ), 0, wxCENTER | wxALL, 10 ); |
41fecb44 | 1158 | mainsizer->Add( choicesizer, 0, wxEXPAND ); |
c15521c6 | 1159 | } |
24f932d2 | 1160 | |
b4cfe261 | 1161 | SetWildcard(wildCard); |
8b17ba72 | 1162 | |
ca65c044 | 1163 | SetAutoLayout( true ); |
8b17ba72 | 1164 | SetSizer( mainsizer ); |
c8c0e54c | 1165 | |
619111b9 JS |
1166 | if (!is_pda) |
1167 | { | |
1168 | mainsizer->Fit( this ); | |
1169 | mainsizer->SetSizeHints( this ); | |
ed58dbea | 1170 | |
619111b9 JS |
1171 | Centre( wxBOTH ); |
1172 | } | |
c81c9392 | 1173 | |
9cedab37 | 1174 | m_text->SetFocus(); |
c81c9392 | 1175 | |
619111b9 | 1176 | ignoreChanges = false; |
abc2a4cc | 1177 | |
4e1901b7 | 1178 | return true; |
8b17ba72 RR |
1179 | } |
1180 | ||
23254b13 | 1181 | wxGenericFileDialog::~wxGenericFileDialog() |
cae5359f | 1182 | { |
619111b9 JS |
1183 | ignoreChanges = true; |
1184 | ||
4e1901b7 | 1185 | if (!m_bypassGenericImpl) |
eaf40b23 | 1186 | { |
4e1901b7 RR |
1187 | if (wxConfig::Get(false)) |
1188 | { | |
1189 | wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), | |
1190 | ms_lastViewStyle); | |
1191 | wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), | |
1192 | ms_lastShowHidden); | |
1193 | } | |
c5ef41d3 | 1194 | |
4e1901b7 RR |
1195 | const int count = m_choice->GetCount(); |
1196 | for ( int i = 0; i < count; i++ ) | |
1197 | { | |
1198 | delete (wxString *)m_choice->GetClientData(i); | |
1199 | } | |
c5ef41d3 | 1200 | } |
cae5359f RR |
1201 | } |
1202 | ||
23254b13 | 1203 | int wxGenericFileDialog::ShowModal() |
9cedab37 | 1204 | { |
619111b9 JS |
1205 | ignoreChanges = true; |
1206 | ||
9cedab37 | 1207 | m_list->GoToDir(m_dir); |
06cc1fb9 | 1208 | UpdateControls(); |
2b5f62a0 | 1209 | m_text->SetValue(m_fileName); |
9cedab37 | 1210 | |
619111b9 JS |
1211 | ignoreChanges = false; |
1212 | ||
9cedab37 VZ |
1213 | return wxDialog::ShowModal(); |
1214 | } | |
1215 | ||
16dce3b3 RR |
1216 | bool wxGenericFileDialog::Show( bool show ) |
1217 | { | |
619111b9 JS |
1218 | // Called by ShowModal, so don't repeate the update |
1219 | #ifndef __WIN32__ | |
16dce3b3 RR |
1220 | if (show) |
1221 | { | |
1222 | m_list->GoToDir(m_dir); | |
1223 | UpdateControls(); | |
1224 | m_text->SetValue(m_fileName); | |
1225 | } | |
619111b9 | 1226 | #endif |
16dce3b3 RR |
1227 | |
1228 | return wxDialog::Show( show ); | |
1229 | } | |
1230 | ||
23254b13 | 1231 | void wxGenericFileDialog::DoSetFilterIndex(int filterindex) |
2b5f62a0 VZ |
1232 | { |
1233 | wxString *str = (wxString*) m_choice->GetClientData( filterindex ); | |
1234 | m_list->SetWild( *str ); | |
1235 | m_filterIndex = filterindex; | |
1236 | if ( str->Left(2) == wxT("*.") ) | |
1237 | { | |
787d22ec JS |
1238 | m_filterExtension = str->Mid(1); |
1239 | if (m_filterExtension == _T(".*")) | |
2b5f62a0 VZ |
1240 | m_filterExtension.clear(); |
1241 | } | |
1242 | else | |
1243 | { | |
1244 | m_filterExtension.clear(); | |
1245 | } | |
1246 | } | |
1247 | ||
b4cfe261 VZ |
1248 | void wxGenericFileDialog::SetWildcard(const wxString& wildCard) |
1249 | { | |
1250 | wxFileDialogBase::SetWildcard(wildCard); | |
1251 | ||
1252 | wxArrayString wildDescriptions, wildFilters; | |
1253 | const size_t count = wxParseCommonDialogsFilter(m_wildCard, | |
1254 | wildDescriptions, | |
1255 | wildFilters); | |
1256 | wxCHECK_RET( count, wxT("wxFileDialog: bad wildcard string") ); | |
1257 | ||
7e4fb3b8 VZ |
1258 | const size_t countOld = m_choice->GetCount(); |
1259 | size_t n; | |
1260 | for ( n = 0; n < countOld; n++ ) | |
fe6cf128 VZ |
1261 | { |
1262 | delete (wxString *)m_choice->GetClientData(n); | |
1263 | } | |
7e4fb3b8 | 1264 | |
fe6cf128 | 1265 | for ( n = 0; n < count; n++ ) |
b4cfe261 VZ |
1266 | { |
1267 | m_choice->Append( wildDescriptions[n], new wxString( wildFilters[n] ) ); | |
1268 | } | |
1269 | ||
1270 | SetFilterIndex( 0 ); | |
1271 | } | |
1272 | ||
23254b13 | 1273 | void wxGenericFileDialog::SetFilterIndex( int filterindex ) |
04ea7f93 RR |
1274 | { |
1275 | m_choice->SetSelection( filterindex ); | |
2b5f62a0 VZ |
1276 | |
1277 | DoSetFilterIndex(filterindex); | |
04ea7f93 RR |
1278 | } |
1279 | ||
23254b13 | 1280 | void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent &event ) |
cae5359f | 1281 | { |
2b5f62a0 | 1282 | DoSetFilterIndex((int)event.GetInt()); |
cae5359f RR |
1283 | } |
1284 | ||
23254b13 | 1285 | void wxGenericFileDialog::OnCheck( wxCommandEvent &event ) |
bf9e3e73 | 1286 | { |
9cedab37 | 1287 | m_list->ShowHidden( (ms_lastShowHidden = event.GetInt() != 0) ); |
bf9e3e73 RR |
1288 | } |
1289 | ||
23254b13 | 1290 | void wxGenericFileDialog::OnActivated( wxListEvent &event ) |
8b17ba72 | 1291 | { |
e1811a01 | 1292 | HandleAction( event.m_item.m_text ); |
8b17ba72 RR |
1293 | } |
1294 | ||
23254b13 | 1295 | void wxGenericFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) ) |
cae5359f | 1296 | { |
b28a6678 | 1297 | HandleAction( m_text->GetValue() ); |
cae5359f RR |
1298 | } |
1299 | ||
23254b13 | 1300 | void wxGenericFileDialog::OnTextChange( wxCommandEvent &WXUNUSED(event) ) |
df413171 JS |
1301 | { |
1302 | if (!ignoreChanges) | |
1303 | { | |
1304 | // Clear selections. Otherwise when the user types in a value they may | |
1305 | // not get the file whose name they typed. | |
1306 | if (m_list->GetSelectedItemCount() > 0) | |
1307 | { | |
7f04165e | 1308 | long item = m_list->GetNextItem(-1, wxLIST_NEXT_ALL, |
df413171 JS |
1309 | wxLIST_STATE_SELECTED); |
1310 | while ( item != -1 ) | |
7f04165e | 1311 | { |
df413171 JS |
1312 | m_list->SetItemState(item,0, wxLIST_STATE_SELECTED); |
1313 | item = m_list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); | |
7f04165e | 1314 | } |
df413171 JS |
1315 | } |
1316 | } | |
1317 | } | |
1318 | ||
23254b13 | 1319 | void wxGenericFileDialog::OnSelected( wxListEvent &event ) |
8b17ba72 | 1320 | { |
619111b9 JS |
1321 | static bool inSelected = false; |
1322 | ||
1323 | if (inSelected) | |
1324 | return; | |
1325 | ||
1326 | inSelected = true; | |
e1811a01 | 1327 | wxString filename( event.m_item.m_text ); |
619111b9 JS |
1328 | |
1329 | #ifdef __WXWINCE__ | |
1330 | // No double-click on most WinCE devices, so do action immediately. | |
1331 | HandleAction( filename ); | |
1332 | #else | |
957eca2f JS |
1333 | if (filename == wxT("..")) |
1334 | { | |
1335 | inSelected = false; | |
1336 | return; | |
1337 | } | |
479cd5de | 1338 | |
06cc1fb9 | 1339 | wxString dir = m_list->GetDir(); |
5e673a6a VS |
1340 | if (!IsTopMostDir(dir)) |
1341 | dir += wxFILE_SEP_PATH; | |
e1811a01 | 1342 | dir += filename; |
957eca2f JS |
1343 | if (wxDirExists(dir)) |
1344 | { | |
1345 | inSelected = false; | |
1346 | return; | |
1347 | } | |
479cd5de | 1348 | |
ca65c044 | 1349 | ignoreChanges = true; |
e1811a01 | 1350 | m_text->SetValue( filename ); |
ca65c044 | 1351 | ignoreChanges = false; |
619111b9 JS |
1352 | #endif |
1353 | inSelected = false; | |
8b17ba72 RR |
1354 | } |
1355 | ||
23254b13 | 1356 | void wxGenericFileDialog::HandleAction( const wxString &fn ) |
8b17ba72 | 1357 | { |
619111b9 JS |
1358 | if (ignoreChanges) |
1359 | return; | |
1360 | ||
e1811a01 | 1361 | wxString filename( fn ); |
c81c9392 WS |
1362 | if (filename.empty()) |
1363 | { | |
1364 | #ifdef __WXWINCE__ | |
b28a6678 | 1365 | EndModal(wxID_CANCEL); |
c81c9392 WS |
1366 | #endif |
1367 | return; | |
1368 | } | |
223d09f6 | 1369 | if (filename == wxT(".")) return; |
c8c0e54c | 1370 | |
c81c9392 WS |
1371 | wxString dir = m_list->GetDir(); |
1372 | ||
b600ed13 VZ |
1373 | // "some/place/" means they want to chdir not try to load "place" |
1374 | bool want_dir = filename.Last() == wxFILE_SEP_PATH; | |
1375 | if (want_dir) | |
1376 | filename = filename.RemoveLast(); | |
1377 | ||
223d09f6 | 1378 | if (filename == wxT("..")) |
0b855868 | 1379 | { |
619111b9 | 1380 | ignoreChanges = true; |
0b855868 RR |
1381 | m_list->GoToParentDir(); |
1382 | m_list->SetFocus(); | |
06cc1fb9 | 1383 | UpdateControls(); |
619111b9 | 1384 | ignoreChanges = false; |
c8c0e54c | 1385 | return; |
0b855868 RR |
1386 | } |
1387 | ||
75ec8bd4 | 1388 | #ifdef __UNIX__ |
223d09f6 | 1389 | if (filename == wxT("~")) |
ed58dbea | 1390 | { |
619111b9 | 1391 | ignoreChanges = true; |
ed58dbea RR |
1392 | m_list->GoToHomeDir(); |
1393 | m_list->SetFocus(); | |
06cc1fb9 | 1394 | UpdateControls(); |
619111b9 | 1395 | ignoreChanges = false; |
c8c0e54c | 1396 | return; |
ed58dbea | 1397 | } |
c8c0e54c | 1398 | |
b600ed13 | 1399 | if (filename.BeforeFirst(wxT('/')) == wxT("~")) |
ed58dbea | 1400 | { |
71f2fb52 | 1401 | filename = wxString(wxGetUserHome()) + filename.Remove(0, 1); |
ed58dbea | 1402 | } |
75ec8bd4 | 1403 | #endif // __UNIX__ |
ed58dbea | 1404 | |
ff3e84ff | 1405 | if (!HasFlag(wxFD_SAVE)) |
cae5359f | 1406 | { |
854aa1e1 RR |
1407 | if ((filename.Find(wxT('*')) != wxNOT_FOUND) || |
1408 | (filename.Find(wxT('?')) != wxNOT_FOUND)) | |
c8c0e54c | 1409 | { |
854aa1e1 RR |
1410 | if (filename.Find(wxFILE_SEP_PATH) != wxNOT_FOUND) |
1411 | { | |
1412 | wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR ); | |
1413 | return; | |
1414 | } | |
1415 | m_list->SetWild( filename ); | |
c8c0e54c VZ |
1416 | return; |
1417 | } | |
cae5359f RR |
1418 | } |
1419 | ||
5e673a6a VS |
1420 | if (!IsTopMostDir(dir)) |
1421 | dir += wxFILE_SEP_PATH; | |
1422 | if (!wxIsAbsolutePath(filename)) | |
ed58dbea RR |
1423 | { |
1424 | dir += filename; | |
1425 | filename = dir; | |
1426 | } | |
c8c0e54c | 1427 | |
da865fdd | 1428 | if (wxDirExists(filename)) |
8b17ba72 | 1429 | { |
619111b9 | 1430 | ignoreChanges = true; |
8b17ba72 | 1431 | m_list->GoToDir( filename ); |
06cc1fb9 | 1432 | UpdateControls(); |
619111b9 | 1433 | ignoreChanges = false; |
c8c0e54c | 1434 | return; |
8b17ba72 | 1435 | } |
c8c0e54c | 1436 | |
b600ed13 VZ |
1437 | // they really wanted a dir, but it doesn't exist |
1438 | if (want_dir) | |
1439 | { | |
1440 | wxMessageBox(_("Directory doesn't exist."), _("Error"), | |
1441 | wxOK | wxICON_ERROR ); | |
1442 | return; | |
1443 | } | |
1444 | ||
2b5f62a0 VZ |
1445 | // append the default extension to the filename if it doesn't have any |
1446 | // | |
1447 | // VZ: the logic of testing for !wxFileExists() only for the open file | |
1448 | // dialog is not entirely clear to me, why don't we allow saving to a | |
1449 | // file without extension as well? | |
ff3e84ff | 1450 | if ( !HasFlag(wxFD_OPEN) || !wxFileExists(filename) ) |
8b17ba72 | 1451 | { |
f74172ab | 1452 | filename = AppendExtension(filename, m_filterExtension); |
8b17ba72 | 1453 | } |
2b5f62a0 VZ |
1454 | |
1455 | // check that the file [doesn't] exist if necessary | |
ff3e84ff | 1456 | if ( HasFlag(wxFD_SAVE) && HasFlag(wxFD_OVERWRITE_PROMPT) && |
2b5f62a0 | 1457 | wxFileExists( filename ) ) |
8b17ba72 | 1458 | { |
2b5f62a0 | 1459 | wxString msg; |
88d6c988 | 1460 | msg.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename.c_str() ); |
655cf310 | 1461 | |
2b5f62a0 VZ |
1462 | if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES) |
1463 | return; | |
1464 | } | |
ff3e84ff | 1465 | else if ( HasFlag(wxFD_OPEN) && HasFlag(wxFD_FILE_MUST_EXIST) && |
2b5f62a0 VZ |
1466 | !wxFileExists(filename) ) |
1467 | { | |
1468 | wxMessageBox(_("Please choose an existing file."), _("Error"), | |
1469 | wxOK | wxICON_ERROR ); | |
8b17ba72 RR |
1470 | } |
1471 | ||
1472 | SetPath( filename ); | |
479cd5de | 1473 | |
3f6638b8 | 1474 | // change to the directory where the user went if asked |
ff3e84ff | 1475 | if ( HasFlag(wxFD_CHANGE_DIR) ) |
3f6638b8 VZ |
1476 | { |
1477 | wxString cwd; | |
1478 | wxSplitPath(filename, &cwd, NULL, NULL); | |
1479 | ||
0b98a252 | 1480 | if ( cwd != wxGetCwd() ) |
3f6638b8 VZ |
1481 | { |
1482 | wxSetWorkingDirectory(cwd); | |
1483 | } | |
1484 | } | |
1485 | ||
b28a6678 | 1486 | EndModal(wxID_OK); |
e1811a01 RR |
1487 | } |
1488 | ||
23254b13 | 1489 | void wxGenericFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) ) |
e1811a01 RR |
1490 | { |
1491 | HandleAction( m_text->GetValue() ); | |
8b17ba72 RR |
1492 | } |
1493 | ||
23254b13 | 1494 | void wxGenericFileDialog::OnList( wxCommandEvent &WXUNUSED(event) ) |
8b17ba72 | 1495 | { |
619111b9 | 1496 | ignoreChanges = true; |
8b17ba72 | 1497 | m_list->ChangeToListMode(); |
9cedab37 | 1498 | ms_lastViewStyle = wxLC_LIST; |
0b855868 | 1499 | m_list->SetFocus(); |
619111b9 | 1500 | ignoreChanges = false; |
8b17ba72 RR |
1501 | } |
1502 | ||
23254b13 | 1503 | void wxGenericFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) ) |
8b17ba72 | 1504 | { |
619111b9 | 1505 | ignoreChanges = true; |
8b17ba72 | 1506 | m_list->ChangeToReportMode(); |
9cedab37 | 1507 | ms_lastViewStyle = wxLC_REPORT; |
0b855868 | 1508 | m_list->SetFocus(); |
619111b9 | 1509 | ignoreChanges = false; |
8b17ba72 RR |
1510 | } |
1511 | ||
23254b13 | 1512 | void wxGenericFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) ) |
8b17ba72 | 1513 | { |
619111b9 | 1514 | ignoreChanges = true; |
8b17ba72 | 1515 | m_list->GoToParentDir(); |
0b855868 | 1516 | m_list->SetFocus(); |
06cc1fb9 | 1517 | UpdateControls(); |
619111b9 | 1518 | ignoreChanges = false; |
8b17ba72 RR |
1519 | } |
1520 | ||
23254b13 | 1521 | void wxGenericFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) ) |
8b17ba72 | 1522 | { |
619111b9 | 1523 | ignoreChanges = true; |
8b17ba72 | 1524 | m_list->GoToHomeDir(); |
0b855868 | 1525 | m_list->SetFocus(); |
06cc1fb9 | 1526 | UpdateControls(); |
619111b9 | 1527 | ignoreChanges = false; |
0b855868 RR |
1528 | } |
1529 | ||
23254b13 | 1530 | void wxGenericFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) ) |
0b855868 | 1531 | { |
619111b9 JS |
1532 | ignoreChanges = true; |
1533 | ||
0b855868 | 1534 | m_list->MakeDir(); |
619111b9 JS |
1535 | |
1536 | ignoreChanges = false; | |
8b17ba72 RR |
1537 | } |
1538 | ||
23254b13 | 1539 | void wxGenericFileDialog::SetPath( const wxString& path ) |
8b17ba72 RR |
1540 | { |
1541 | // not only set the full path but also update filename and dir | |
1542 | m_path = path; | |
619111b9 JS |
1543 | |
1544 | #ifdef __WXWINCE__ | |
1545 | if (m_path.empty()) | |
1546 | m_path = wxFILE_SEP_PATH; | |
1547 | #endif | |
1548 | ||
9cedab37 | 1549 | if ( !path.empty() ) |
8b17ba72 RR |
1550 | { |
1551 | wxString ext; | |
1552 | wxSplitPath(path, &m_dir, &m_fileName, &ext); | |
9cedab37 | 1553 | if (!ext.empty()) |
c8c0e54c | 1554 | { |
223d09f6 | 1555 | m_fileName += wxT("."); |
8b17ba72 | 1556 | m_fileName += ext; |
c8c0e54c | 1557 | } |
8b17ba72 RR |
1558 | } |
1559 | } | |
c8c0e54c | 1560 | |
23254b13 | 1561 | void wxGenericFileDialog::GetPaths( wxArrayString& paths ) const |
7941ba11 RR |
1562 | { |
1563 | paths.Empty(); | |
28c9c76e RR |
1564 | if (m_list->GetSelectedItemCount() == 0) |
1565 | { | |
1566 | paths.Add( GetPath() ); | |
1567 | return; | |
1568 | } | |
479cd5de | 1569 | |
7941ba11 RR |
1570 | paths.Alloc( m_list->GetSelectedItemCount() ); |
1571 | ||
06cc1fb9 | 1572 | wxString dir = m_list->GetDir(); |
5e673a6a | 1573 | #ifdef __UNIX__ |
7a82dabc | 1574 | if (dir != wxT("/")) |
619111b9 JS |
1575 | #endif |
1576 | #ifdef __WXWINCE__ | |
1577 | if (dir != wxT("/") && dir != wxT("\\")) | |
5e673a6a VS |
1578 | #endif |
1579 | dir += wxFILE_SEP_PATH; | |
7941ba11 RR |
1580 | |
1581 | wxListItem item; | |
1582 | item.m_mask = wxLIST_MASK_TEXT; | |
1583 | ||
1584 | item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
92da8bde | 1585 | while ( item.m_itemId != -1 ) |
68df5777 | 1586 | { |
7941ba11 RR |
1587 | m_list->GetItem( item ); |
1588 | paths.Add( dir + item.m_text ); | |
68df5777 | 1589 | item.m_itemId = m_list->GetNextItem( item.m_itemId, |
7941ba11 RR |
1590 | wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); |
1591 | } | |
1592 | } | |
1593 | ||
23254b13 | 1594 | void wxGenericFileDialog::GetFilenames(wxArrayString& files) const |
7941ba11 RR |
1595 | { |
1596 | files.Empty(); | |
28c9c76e RR |
1597 | if (m_list->GetSelectedItemCount() == 0) |
1598 | { | |
1599 | files.Add( GetFilename() ); | |
1600 | return; | |
1601 | } | |
7941ba11 RR |
1602 | files.Alloc( m_list->GetSelectedItemCount() ); |
1603 | ||
1604 | wxListItem item; | |
1605 | item.m_mask = wxLIST_MASK_TEXT; | |
1606 | ||
1607 | item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
92da8bde | 1608 | while ( item.m_itemId != -1 ) |
68df5777 | 1609 | { |
7941ba11 RR |
1610 | m_list->GetItem( item ); |
1611 | files.Add( item.m_text ); | |
68df5777 | 1612 | item.m_itemId = m_list->GetNextItem( item.m_itemId, |
7941ba11 RR |
1613 | wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); |
1614 | } | |
1615 | } | |
1616 | ||
06cc1fb9 JS |
1617 | void wxGenericFileDialog::UpdateControls() |
1618 | { | |
1619 | wxString dir = m_list->GetDir(); | |
1620 | m_static->SetLabel(dir); | |
1621 | ||
1622 | bool enable = !IsTopMostDir(dir); | |
1623 | m_upDirButton->Enable(enable); | |
1624 | ||
144cf6da | 1625 | #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__) |
06cc1fb9 | 1626 | m_newDirButton->Enable(enable); |
144cf6da | 1627 | #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__) |
06cc1fb9 JS |
1628 | } |
1629 | ||
a84f5f39 | 1630 | #ifdef wxUSE_GENERIC_FILEDIALOG |
655cf310 | 1631 | |
412e0d47 | 1632 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog, wxGenericFileDialog) |
655cf310 | 1633 | |
a84f5f39 | 1634 | #endif // wxUSE_GENERIC_FILEDIALOG |
23254b13 | 1635 | |
4e1901b7 | 1636 | #endif // wxUSE_FILEDLG |