]> git.saurik.com Git - wxWidgets.git/blame - src/generic/filectrlg.cpp
Add test for absence of events from wxSpinCtrlDouble ctor.
[wxWidgets.git] / src / generic / filectrlg.cpp
CommitLineData
0cf3e587
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/filectrlg.cpp
3// Purpose: wxGenericFileCtrl Implementation
4// Author: Diaa M. Sami
5// Created: 2007-07-07
0cf3e587
VZ
6// Copyright: (c) Diaa M. Sami
7// Licence: wxWindows licence
8///////////////////////////////////////////////////////////////////////////////
9
10#include "wx/wxprec.h"
11
12#ifdef __BORLANDC__
13#pragma hdrstop
14#endif
15
16#if wxUSE_FILECTRL
17
18#include "wx/generic/filectrlg.h"
19
20#ifndef WX_PRECOMP
21 #include "wx/settings.h"
22 #include "wx/sizer.h"
23 #include "wx/stattext.h"
24 #include "wx/checkbox.h"
25 #include "wx/msgdlg.h"
26 #include "wx/log.h"
27 #include "wx/filedlg.h"
28#endif
29
0cf3e587
VZ
30#include "wx/clntdata.h"
31#include "wx/file.h" // for wxS_IXXX constants only
32#include "wx/generic/dirctrlg.h" // for wxFileIconsTable
33#include "wx/dir.h"
34#include "wx/tokenzr.h"
690a6489 35#include "wx/imaglist.h"
0cf3e587 36
bb5a9514 37#ifdef __WINDOWS__
0cf3e587
VZ
38 #include "wx/msw/wrapwin.h"
39#endif
40
41#if defined(__WXWINCE__)
42#define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
43#elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
44#define IsTopMostDir(dir) (dir.empty())
45#else
46#define IsTopMostDir(dir) (dir == wxT("/"))
47#endif
48
49
50// ----------------------------------------------------------------------------
51// private functions
52// ----------------------------------------------------------------------------
53
54static
d3ca8487 55int wxCALLBACK wxFileDataNameCompare( wxIntPtr data1, wxIntPtr data2, wxIntPtr sortOrder)
0cf3e587
VZ
56{
57 wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1);
58 wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2);
59
60 if (fd1->GetFileName() == wxT(".."))
61 return -sortOrder;
62 if (fd2->GetFileName() == wxT(".."))
63 return sortOrder;
64 if (fd1->IsDir() && !fd2->IsDir())
65 return -sortOrder;
66 if (fd2->IsDir() && !fd1->IsDir())
67 return sortOrder;
68
69 return sortOrder*wxStrcmp( fd1->GetFileName(), fd2->GetFileName() );
70}
71
72static
d3ca8487 73int wxCALLBACK wxFileDataSizeCompare(wxIntPtr data1, wxIntPtr data2, wxIntPtr sortOrder)
0cf3e587
VZ
74{
75 wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1);
76 wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2);
77
78 if (fd1->GetFileName() == wxT(".."))
79 return -sortOrder;
80 if (fd2->GetFileName() == wxT(".."))
81 return sortOrder;
82 if (fd1->IsDir() && !fd2->IsDir())
83 return -sortOrder;
84 if (fd2->IsDir() && !fd1->IsDir())
85 return sortOrder;
86 if (fd1->IsLink() && !fd2->IsLink())
87 return -sortOrder;
88 if (fd2->IsLink() && !fd1->IsLink())
89 return sortOrder;
90
91 return fd1->GetSize() > fd2->GetSize() ? sortOrder : -sortOrder;
92}
93
94static
d3ca8487 95int wxCALLBACK wxFileDataTypeCompare(wxIntPtr data1, wxIntPtr data2, wxIntPtr sortOrder)
0cf3e587
VZ
96{
97 wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1);
98 wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2);
99
100 if (fd1->GetFileName() == wxT(".."))
101 return -sortOrder;
102 if (fd2->GetFileName() == wxT(".."))
103 return sortOrder;
104 if (fd1->IsDir() && !fd2->IsDir())
105 return -sortOrder;
106 if (fd2->IsDir() && !fd1->IsDir())
107 return sortOrder;
108 if (fd1->IsLink() && !fd2->IsLink())
109 return -sortOrder;
110 if (fd2->IsLink() && !fd1->IsLink())
111 return sortOrder;
112
113 return sortOrder*wxStrcmp( fd1->GetFileType(), fd2->GetFileType() );
114}
115
116static
d3ca8487 117int wxCALLBACK wxFileDataTimeCompare(wxIntPtr data1, wxIntPtr data2, wxIntPtr sortOrder)
0cf3e587
VZ
118{
119 wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1);
120 wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2);
121
122 if (fd1->GetFileName() == wxT(".."))
123 return -sortOrder;
124 if (fd2->GetFileName() == wxT(".."))
125 return sortOrder;
126 if (fd1->IsDir() && !fd2->IsDir())
127 return -sortOrder;
128 if (fd2->IsDir() && !fd1->IsDir())
129 return sortOrder;
130
131 return fd1->GetDateTime().IsLaterThan(fd2->GetDateTime()) ? sortOrder : -sortOrder;
132}
133
134// defined in src/generic/dirctrlg.cpp
135extern size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids);
136
137//-----------------------------------------------------------------------------
138// wxFileData
139//-----------------------------------------------------------------------------
140
141wxFileData::wxFileData( const wxString &filePath, const wxString &fileName, fileType type, int image_id )
142{
143 Init();
144 m_fileName = fileName;
145 m_filePath = filePath;
146 m_type = type;
147 m_image = image_id;
148
149 ReadData();
150}
151
152void wxFileData::Init()
153{
154 m_size = 0;
155 m_type = wxFileData::is_file;
156 m_image = wxFileIconsTable::file;
157}
158
159void wxFileData::Copy( const wxFileData& fileData )
160{
161 m_fileName = fileData.GetFileName();
162 m_filePath = fileData.GetFilePath();
163 m_size = fileData.GetSize();
164 m_dateTime = fileData.GetDateTime();
165 m_permissions = fileData.GetPermissions();
166 m_type = fileData.GetType();
167 m_image = fileData.GetImageId();
168}
169
170void wxFileData::ReadData()
171{
172 if (IsDrive())
173 {
174 m_size = 0;
175 return;
176 }
177
178#if defined(__DOS__) || (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__OS2__)
179 // c:\.. is a drive don't stat it
180 if ((m_fileName == wxT("..")) && (m_filePath.length() <= 5))
181 {
182 m_type = is_drive;
183 m_size = 0;
184 return;
185 }
186#endif // __DOS__ || __WINDOWS__
187
188#ifdef __WXWINCE__
189
190 // WinCE
191
192 DWORD fileAttribs = GetFileAttributes(m_filePath.fn_str());
193 m_type |= (fileAttribs & FILE_ATTRIBUTE_DIRECTORY) != 0 ? is_dir : 0;
194
195 wxString p, f, ext;
bd365871 196 wxFileName::SplitPath(m_filePath, & p, & f, & ext);
0cf3e587
VZ
197 if (wxStricmp(ext, wxT("exe")) == 0)
198 m_type |= is_exe;
199
200 // Find out size
201 m_size = 0;
202 HANDLE fileHandle = CreateFile(m_filePath.fn_str(),
203 GENERIC_READ,
204 FILE_SHARE_READ,
205 NULL,
206 OPEN_EXISTING,
207 FILE_ATTRIBUTE_NORMAL,
208 NULL);
209
210 if (fileHandle != INVALID_HANDLE_VALUE)
211 {
212 m_size = GetFileSize(fileHandle, 0);
213 CloseHandle(fileHandle);
214 }
215
216 m_dateTime = wxFileModificationTime(m_filePath);
217
218#else
219
220 // OTHER PLATFORMS
221
222 wxStructStat buff;
223
224#if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
4d35172f
VZ
225 const bool hasStat = lstat( m_filePath.fn_str(), &buff ) == 0;
226 if ( hasStat )
227 m_type |= S_ISLNK(buff.st_mode) ? is_link : 0;
0cf3e587 228#else // no lstat()
4d35172f 229 const bool hasStat = wxStat( m_filePath, &buff ) == 0;
0cf3e587
VZ
230#endif
231
4d35172f
VZ
232 if ( hasStat )
233 {
234 m_type |= (buff.st_mode & S_IFDIR) != 0 ? is_dir : 0;
235 m_type |= (buff.st_mode & wxS_IXUSR) != 0 ? is_exe : 0;
0cf3e587 236
4d35172f 237 m_size = buff.st_size;
0cf3e587 238
4d35172f
VZ
239 m_dateTime = buff.st_mtime;
240 }
0cf3e587
VZ
241#endif
242 // __WXWINCE__
243
244#if defined(__UNIX__)
4d35172f
VZ
245 if ( hasStat )
246 {
247 m_permissions.Printf(wxT("%c%c%c%c%c%c%c%c%c"),
248 buff.st_mode & wxS_IRUSR ? wxT('r') : wxT('-'),
249 buff.st_mode & wxS_IWUSR ? wxT('w') : wxT('-'),
250 buff.st_mode & wxS_IXUSR ? wxT('x') : wxT('-'),
251 buff.st_mode & wxS_IRGRP ? wxT('r') : wxT('-'),
252 buff.st_mode & wxS_IWGRP ? wxT('w') : wxT('-'),
253 buff.st_mode & wxS_IXGRP ? wxT('x') : wxT('-'),
254 buff.st_mode & wxS_IROTH ? wxT('r') : wxT('-'),
255 buff.st_mode & wxS_IWOTH ? wxT('w') : wxT('-'),
256 buff.st_mode & wxS_IXOTH ? wxT('x') : wxT('-'));
257 }
0cf3e587
VZ
258#elif defined(__WIN32__)
259 DWORD attribs = ::GetFileAttributes(m_filePath.c_str());
260 if (attribs != (DWORD)-1)
261 {
9a83f860
VZ
262 m_permissions.Printf(wxT("%c%c%c%c"),
263 attribs & FILE_ATTRIBUTE_ARCHIVE ? wxT('A') : wxT(' '),
264 attribs & FILE_ATTRIBUTE_READONLY ? wxT('R') : wxT(' '),
265 attribs & FILE_ATTRIBUTE_HIDDEN ? wxT('H') : wxT(' '),
266 attribs & FILE_ATTRIBUTE_SYSTEM ? wxT('S') : wxT(' '));
0cf3e587
VZ
267 }
268#endif
269
270 // try to get a better icon
271 if (m_image == wxFileIconsTable::file)
272 {
273 if (m_fileName.Find(wxT('.'), true) != wxNOT_FOUND)
274 {
275 m_image = wxTheFileIconsTable->GetIconID( m_fileName.AfterLast(wxT('.')));
276 } else if (IsExe())
277 {
278 m_image = wxFileIconsTable::executable;
279 }
280 }
281}
282
283wxString wxFileData::GetFileType() const
284{
285 if (IsDir())
286 return _("<DIR>");
287 else if (IsLink())
288 return _("<LINK>");
289 else if (IsDrive())
290 return _("<DRIVE>");
291 else if (m_fileName.Find(wxT('.'), true) != wxNOT_FOUND)
292 return m_fileName.AfterLast(wxT('.'));
293
294 return wxEmptyString;
295}
296
297wxString wxFileData::GetModificationTime() const
298{
299 // want time as 01:02 so they line up nicely, no %r in WIN32
300 return m_dateTime.FormatDate() + wxT(" ") + m_dateTime.Format(wxT("%I:%M:%S %p"));
301}
302
303wxString wxFileData::GetHint() const
304{
305 wxString s = m_filePath;
306 s += wxT(" ");
307
308 if (IsDir())
309 s += _("<DIR>");
310 else if (IsLink())
311 s += _("<LINK>");
312 else if (IsDrive())
313 s += _("<DRIVE>");
314 else // plain file
315 s += wxString::Format(wxPLURAL("%ld byte", "%ld bytes", m_size),
316 wxLongLong(m_size).ToString().c_str());
317
318 s += wxT(' ');
319
320 if ( !IsDrive() )
321 {
322 s << GetModificationTime()
323 << wxT(" ")
324 << m_permissions;
325 }
326
327 return s;
328}
329
330wxString wxFileData::GetEntry( fileListFieldType num ) const
331{
332 wxString s;
333 switch ( num )
334 {
335 case FileList_Name:
336 s = m_fileName;
337 break;
338
339 case FileList_Size:
340 if (!IsDir() && !IsLink() && !IsDrive())
341 s = wxLongLong(m_size).ToString();
342 break;
343
344 case FileList_Type:
345 s = GetFileType();
346 break;
347
348 case FileList_Time:
349 if (!IsDrive())
350 s = GetModificationTime();
351 break;
352
353#if defined(__UNIX__) || defined(__WIN32__)
354 case FileList_Perm:
355 s = m_permissions;
356 break;
357#endif // defined(__UNIX__) || defined(__WIN32__)
358
359 default:
9a83f860 360 wxFAIL_MSG( wxT("unexpected field in wxFileData::GetEntry()") );
0cf3e587
VZ
361 }
362
363 return s;
364}
365
366void wxFileData::SetNewName( const wxString &filePath, const wxString &fileName )
367{
368 m_fileName = fileName;
369 m_filePath = filePath;
370}
371
372void wxFileData::MakeItem( wxListItem &item )
373{
374 item.m_text = m_fileName;
375 item.ClearAttributes();
376 if (IsExe())
377 item.SetTextColour(*wxRED);
378 if (IsDir())
379 item.SetTextColour(*wxBLUE);
380
381 item.m_image = m_image;
382
383 if (IsLink())
384 {
9a83f860 385 wxColour dg = wxTheColourDatabase->Find( wxT("MEDIUM GREY") );
a1b806b9 386 if ( dg.IsOk() )
0cf3e587
VZ
387 item.SetTextColour(dg);
388 }
389 item.m_data = wxPtrToUInt(this);
390}
391
392//-----------------------------------------------------------------------------
16a6b53d 393// wxFileListCtrl
0cf3e587
VZ
394//-----------------------------------------------------------------------------
395
16a6b53d 396IMPLEMENT_DYNAMIC_CLASS(wxFileListCtrl,wxListCtrl)
0cf3e587 397
16a6b53d
VZ
398BEGIN_EVENT_TABLE(wxFileListCtrl,wxListCtrl)
399 EVT_LIST_DELETE_ITEM(wxID_ANY, wxFileListCtrl::OnListDeleteItem)
400 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY, wxFileListCtrl::OnListDeleteAllItems)
401 EVT_LIST_END_LABEL_EDIT(wxID_ANY, wxFileListCtrl::OnListEndLabelEdit)
402 EVT_LIST_COL_CLICK(wxID_ANY, wxFileListCtrl::OnListColClick)
0cf3e587
VZ
403END_EVENT_TABLE()
404
405
16a6b53d 406wxFileListCtrl::wxFileListCtrl()
0cf3e587
VZ
407{
408 m_showHidden = false;
260020e3 409 m_sort_forward = true;
0cf3e587
VZ
410 m_sort_field = wxFileData::FileList_Name;
411}
412
16a6b53d 413wxFileListCtrl::wxFileListCtrl(wxWindow *win,
0cf3e587
VZ
414 wxWindowID id,
415 const wxString& wild,
416 bool showHidden,
417 const wxPoint& pos,
418 const wxSize& size,
419 long style,
420 const wxValidator &validator,
421 const wxString &name)
422 : wxListCtrl(win, id, pos, size, style, validator, name),
423 m_wild(wild)
424{
425 wxImageList *imageList = wxTheFileIconsTable->GetSmallImageList();
426
427 SetImageList( imageList, wxIMAGE_LIST_SMALL );
428
429 m_showHidden = showHidden;
430
260020e3 431 m_sort_forward = true;
0cf3e587
VZ
432 m_sort_field = wxFileData::FileList_Name;
433
434 m_dirName = wxT("*");
435
436 if (style & wxLC_REPORT)
437 ChangeToReportMode();
438}
439
16a6b53d 440void wxFileListCtrl::ChangeToListMode()
0cf3e587
VZ
441{
442 ClearAll();
443 SetSingleStyle( wxLC_LIST );
444 UpdateFiles();
445}
446
16a6b53d 447void wxFileListCtrl::ChangeToReportMode()
0cf3e587
VZ
448{
449 ClearAll();
450 SetSingleStyle( wxLC_REPORT );
451
452 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
453 // don't hardcode since mm/dd is dd/mm elsewhere
454 int w, h;
455 wxDateTime dt(22, wxDateTime::Dec, 2002, 22, 22, 22);
456 wxString txt = dt.FormatDate() + wxT("22") + dt.Format(wxT("%I:%M:%S %p"));
457 GetTextExtent(txt, &w, &h);
458
459 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, w );
460 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, w/2 );
461 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT, w/2 );
462 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT, w );
463#if defined(__UNIX__)
464 GetTextExtent(wxT("Permissions 2"), &w, &h);
465 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, w );
466#elif defined(__WIN32__)
467 GetTextExtent(wxT("Attributes 2"), &w, &h);
468 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT, w );
469#endif
470
471 UpdateFiles();
472}
473
16a6b53d 474void wxFileListCtrl::ChangeToSmallIconMode()
0cf3e587
VZ
475{
476 ClearAll();
477 SetSingleStyle( wxLC_SMALL_ICON );
478 UpdateFiles();
479}
480
16a6b53d 481void wxFileListCtrl::ShowHidden( bool show )
0cf3e587
VZ
482{
483 m_showHidden = show;
484 UpdateFiles();
485}
486
16a6b53d 487long wxFileListCtrl::Add( wxFileData *fd, wxListItem &item )
0cf3e587
VZ
488{
489 long ret = -1;
490 item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE;
491 fd->MakeItem( item );
492 long my_style = GetWindowStyleFlag();
493 if (my_style & wxLC_REPORT)
494 {
495 ret = InsertItem( item );
496 for (int i = 1; i < wxFileData::FileList_Max; i++)
497 SetItem( item.m_itemId, i, fd->GetEntry((wxFileData::fileListFieldType)i) );
498 }
499 else if ((my_style & wxLC_LIST) || (my_style & wxLC_SMALL_ICON))
500 {
501 ret = InsertItem( item );
502 }
503 return ret;
504}
505
16a6b53d 506void wxFileListCtrl::UpdateItem(const wxListItem &item)
0cf3e587
VZ
507{
508 wxFileData *fd = (wxFileData*)GetItemData(item);
509 wxCHECK_RET(fd, wxT("invalid filedata"));
510
511 fd->ReadData();
512
513 SetItemText(item, fd->GetFileName());
514 SetItemImage(item, fd->GetImageId());
515
516 if (GetWindowStyleFlag() & wxLC_REPORT)
517 {
518 for (int i = 1; i < wxFileData::FileList_Max; i++)
519 SetItem( item.m_itemId, i, fd->GetEntry((wxFileData::fileListFieldType)i) );
520 }
521}
522
16a6b53d 523void wxFileListCtrl::UpdateFiles()
0cf3e587
VZ
524{
525 // don't do anything before ShowModal() call which sets m_dirName
526 if ( m_dirName == wxT("*") )
527 return;
528
529 wxBusyCursor bcur; // this may take a while...
530
531 DeleteAllItems();
532
533 wxListItem item;
534 item.m_itemId = 0;
535 item.m_col = 0;
536
537#if (defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)) && !defined(__WXWINCE__)
538 if ( IsTopMostDir(m_dirName) )
539 {
540 wxArrayString names, paths;
541 wxArrayInt icons;
e8548d99 542 const size_t count = wxGetAvailableDrives(paths, names, icons);
0cf3e587 543
e8548d99 544 for ( size_t n = 0; n < count; n++ )
0cf3e587 545 {
e8548d99
VZ
546 // use paths[n] as the drive name too as our HandleAction() can't
547 // deal with the drive names (of the form "System (C:)") currently
548 // as it mistakenly treats them as file names
549 //
550 // it would be preferable to show names, and not paths, in the
551 // dialog just as the native dialog does but for this we must:
552 // a) store the item type as item data and modify HandleAction()
553 // to use it instead of wxDirExists() to check whether the item
554 // is a directory
555 // b) store the drives by their drive letters and not their
556 // descriptions as otherwise it's pretty confusing to the user
557 wxFileData *fd = new wxFileData(paths[n], paths[n],
558 wxFileData::is_drive, icons[n]);
0cf3e587
VZ
559 if (Add(fd, item) != -1)
560 item.m_itemId++;
561 else
562 delete fd;
563 }
564 }
565 else
566#endif // defined(__DOS__) || defined(__WINDOWS__)
567 {
568 // Real directory...
569 if ( !IsTopMostDir(m_dirName) && !m_dirName.empty() )
570 {
571 wxString p(wxPathOnly(m_dirName));
572#if (defined(__UNIX__) || defined(__WXWINCE__)) && !defined(__OS2__)
573 if (p.empty()) p = wxT("/");
574#endif // __UNIX__
575 wxFileData *fd = new wxFileData(p, wxT(".."), wxFileData::is_dir, wxFileIconsTable::folder);
576 if (Add(fd, item) != -1)
577 item.m_itemId++;
578 else
579 delete fd;
580 }
581
582 wxString dirname(m_dirName);
583#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
584 if (dirname.length() == 2 && dirname[1u] == wxT(':'))
585 dirname << wxT('\\');
586#endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
587
588 if (dirname.empty())
589 dirname = wxFILE_SEP_PATH;
590
591 wxLogNull logNull;
592 wxDir dir(dirname);
593
594 if ( dir.IsOpened() )
595 {
596 wxString dirPrefix(dirname);
597 if (dirPrefix.Last() != wxFILE_SEP_PATH)
598 dirPrefix += wxFILE_SEP_PATH;
599
600 int hiddenFlag = m_showHidden ? wxDIR_HIDDEN : 0;
601
602 bool cont;
603 wxString f;
604
605 // Get the directories first (not matched against wildcards):
606 cont = dir.GetFirst(&f, wxEmptyString, wxDIR_DIRS | hiddenFlag);
607 while (cont)
608 {
609 wxFileData *fd = new wxFileData(dirPrefix + f, f, wxFileData::is_dir, wxFileIconsTable::folder);
610 if (Add(fd, item) != -1)
611 item.m_itemId++;
612 else
613 delete fd;
614
615 cont = dir.GetNext(&f);
616 }
617
618 // Tokenize the wildcard string, so we can handle more than 1
619 // search pattern in a wildcard.
620 wxStringTokenizer tokenWild(m_wild, wxT(";"));
621 while ( tokenWild.HasMoreTokens() )
622 {
623 cont = dir.GetFirst(&f, tokenWild.GetNextToken(),
624 wxDIR_FILES | hiddenFlag);
625 while (cont)
626 {
627 wxFileData *fd = new wxFileData(dirPrefix + f, f, wxFileData::is_file, wxFileIconsTable::file);
628 if (Add(fd, item) != -1)
629 item.m_itemId++;
630 else
631 delete fd;
632
633 cont = dir.GetNext(&f);
634 }
635 }
636 }
637 }
638
260020e3 639 SortItems(m_sort_field, m_sort_forward);
0cf3e587
VZ
640}
641
16a6b53d 642void wxFileListCtrl::SetWild( const wxString &wild )
0cf3e587
VZ
643{
644 if (wild.Find(wxT('|')) != wxNOT_FOUND)
645 return;
646
647 m_wild = wild;
648 UpdateFiles();
649}
650
16a6b53d 651void wxFileListCtrl::MakeDir()
0cf3e587
VZ
652{
653 wxString new_name( _("NewName") );
654 wxString path( m_dirName );
655 path += wxFILE_SEP_PATH;
656 path += new_name;
657 if (wxFileExists(path))
658 {
659 // try NewName0, NewName1 etc.
660 int i = 0;
661 do {
662 new_name = _("NewName");
663 wxString num;
664 num.Printf( wxT("%d"), i );
665 new_name += num;
666
667 path = m_dirName;
668 path += wxFILE_SEP_PATH;
669 path += new_name;
670 i++;
671 } while (wxFileExists(path));
672 }
673
674 wxLogNull log;
675 if (!wxMkdir(path))
676 {
677 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
678 dialog.ShowModal();
679 return;
680 }
681
682 wxFileData *fd = new wxFileData( path, new_name, wxFileData::is_dir, wxFileIconsTable::folder );
683 wxListItem item;
684 item.m_itemId = 0;
685 item.m_col = 0;
6485c8d7 686 long itemid = Add( fd, item );
0cf3e587 687
6485c8d7 688 if (itemid != -1)
0cf3e587 689 {
260020e3 690 SortItems(m_sort_field, m_sort_forward);
6485c8d7
SC
691 itemid = FindItem( 0, wxPtrToUInt(fd) );
692 EnsureVisible( itemid );
693 EditLabel( itemid );
0cf3e587
VZ
694 }
695 else
696 delete fd;
697}
698
16a6b53d 699void wxFileListCtrl::GoToParentDir()
0cf3e587
VZ
700{
701 if (!IsTopMostDir(m_dirName))
702 {
703 size_t len = m_dirName.length();
704 if (wxEndsWithPathSeparator(m_dirName))
705 m_dirName.Remove( len-1, 1 );
706 wxString fname( wxFileNameFromPath(m_dirName) );
707 m_dirName = wxPathOnly( m_dirName );
708#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
709 if (!m_dirName.empty())
710 {
711 if (m_dirName.Last() == wxT('.'))
712 m_dirName = wxEmptyString;
713 }
714#elif defined(__UNIX__)
715 if (m_dirName.empty())
716 m_dirName = wxT("/");
717#endif
718 UpdateFiles();
719 long id = FindItem( 0, fname );
720 if (id != wxNOT_FOUND)
721 {
0cf3e587
VZ
722 SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
723 EnsureVisible( id );
0cf3e587
VZ
724 }
725 }
726}
727
16a6b53d 728void wxFileListCtrl::GoToHomeDir()
0cf3e587
VZ
729{
730 wxString s = wxGetUserHome( wxString() );
731 GoToDir(s);
732}
733
16a6b53d 734void wxFileListCtrl::GoToDir( const wxString &dir )
0cf3e587
VZ
735{
736 if (!wxDirExists(dir)) return;
737
738 m_dirName = dir;
739 UpdateFiles();
740
0cf3e587 741 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
0cf3e587
VZ
742
743 EnsureVisible( 0 );
744}
745
16a6b53d 746void wxFileListCtrl::FreeItemData(wxListItem& item)
0cf3e587
VZ
747{
748 if ( item.m_data )
749 {
750 wxFileData *fd = (wxFileData*)item.m_data;
751 delete fd;
752
753 item.m_data = 0;
754 }
755}
756
16a6b53d 757void wxFileListCtrl::OnListDeleteItem( wxListEvent &event )
0cf3e587
VZ
758{
759 FreeItemData(event.m_item);
760}
761
16a6b53d 762void wxFileListCtrl::OnListDeleteAllItems( wxListEvent & WXUNUSED(event) )
0cf3e587
VZ
763{
764 FreeAllItemsData();
765}
766
16a6b53d 767void wxFileListCtrl::FreeAllItemsData()
0cf3e587
VZ
768{
769 wxListItem item;
770 item.m_mask = wxLIST_MASK_DATA;
771
772 item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL );
773 while ( item.m_itemId != -1 )
774 {
775 GetItem( item );
776 FreeItemData(item);
777 item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL );
778 }
779}
780
16a6b53d 781void wxFileListCtrl::OnListEndLabelEdit( wxListEvent &event )
0cf3e587
VZ
782{
783 wxFileData *fd = (wxFileData*)event.m_item.m_data;
784 wxASSERT( fd );
785
786 if ((event.GetLabel().empty()) ||
787 (event.GetLabel() == wxT(".")) ||
788 (event.GetLabel() == wxT("..")) ||
789 (event.GetLabel().First( wxFILE_SEP_PATH ) != wxNOT_FOUND))
790 {
791 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
792 dialog.ShowModal();
793 event.Veto();
794 return;
795 }
796
797 wxString new_name( wxPathOnly( fd->GetFilePath() ) );
798 new_name += wxFILE_SEP_PATH;
799 new_name += event.GetLabel();
800
801 wxLogNull log;
802
803 if (wxFileExists(new_name))
804 {
805 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
806 dialog.ShowModal();
807 event.Veto();
808 }
809
810 if (wxRenameFile(fd->GetFilePath(),new_name))
811 {
812 fd->SetNewName( new_name, event.GetLabel() );
813
0cf3e587 814 SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
0cf3e587
VZ
815
816 UpdateItem( event.GetItem() );
817 EnsureVisible( event.GetItem() );
818 }
819 else
820 {
821 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
822 dialog.ShowModal();
823 event.Veto();
824 }
825}
826
16a6b53d 827void wxFileListCtrl::OnListColClick( wxListEvent &event )
0cf3e587
VZ
828{
829 int col = event.GetColumn();
830
831 switch (col)
832 {
833 case wxFileData::FileList_Name :
834 case wxFileData::FileList_Size :
835 case wxFileData::FileList_Type :
836 case wxFileData::FileList_Time : break;
837 default : return;
838 }
839
840 if ((wxFileData::fileListFieldType)col == m_sort_field)
260020e3 841 m_sort_forward = !m_sort_forward;
0cf3e587
VZ
842 else
843 m_sort_field = (wxFileData::fileListFieldType)col;
844
260020e3 845 SortItems(m_sort_field, m_sort_forward);
0cf3e587
VZ
846}
847
16a6b53d 848void wxFileListCtrl::SortItems(wxFileData::fileListFieldType field, bool forward)
0cf3e587
VZ
849{
850 m_sort_field = field;
260020e3 851 m_sort_forward = forward;
0cf3e587
VZ
852 const long sort_dir = forward ? 1 : -1;
853
854 switch (m_sort_field)
855 {
856 case wxFileData::FileList_Size :
857 wxListCtrl::SortItems(wxFileDataSizeCompare, sort_dir);
858 break;
859
860 case wxFileData::FileList_Type :
861 wxListCtrl::SortItems(wxFileDataTypeCompare, sort_dir);
862 break;
863
864 case wxFileData::FileList_Time :
865 wxListCtrl::SortItems(wxFileDataTimeCompare, sort_dir);
866 break;
867
868 case wxFileData::FileList_Name :
869 default :
870 wxListCtrl::SortItems(wxFileDataNameCompare, sort_dir);
871 break;
872 }
873}
874
16a6b53d 875wxFileListCtrl::~wxFileListCtrl()
0cf3e587
VZ
876{
877 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
16a6b53d
VZ
878 // wxFileListCtrl::OnListDeleteAllItems. But if the event is generated after
879 // the destruction of the wxFileListCtrl we need to free any data here:
0cf3e587
VZ
880 FreeAllItemsData();
881}
882
883#define ID_CHOICE (wxID_FILECTRL + 1)
884#define ID_TEXT (wxID_FILECTRL + 2)
885#define ID_FILELIST_CTRL (wxID_FILECTRL + 3)
886#define ID_CHECK (wxID_FILECTRL + 4)
887
888///////////////////////////////////////////////////////////////////////////////
889// wxGenericFileCtrl implementation
890///////////////////////////////////////////////////////////////////////////////
891
5804d539 892IMPLEMENT_DYNAMIC_CLASS( wxGenericFileCtrl, wxNavigationEnabled<wxControl> )
0cf3e587 893
5804d539 894BEGIN_EVENT_TABLE( wxGenericFileCtrl, wxNavigationEnabled<wxControl> )
0cf3e587
VZ
895 EVT_LIST_ITEM_SELECTED( ID_FILELIST_CTRL, wxGenericFileCtrl::OnSelected )
896 EVT_LIST_ITEM_ACTIVATED( ID_FILELIST_CTRL, wxGenericFileCtrl::OnActivated )
897 EVT_CHOICE( ID_CHOICE, wxGenericFileCtrl::OnChoiceFilter )
898 EVT_TEXT_ENTER( ID_TEXT, wxGenericFileCtrl::OnTextEnter )
899 EVT_TEXT( ID_TEXT, wxGenericFileCtrl::OnTextChange )
900 EVT_CHECKBOX( ID_CHECK, wxGenericFileCtrl::OnCheck )
901END_EVENT_TABLE()
902
903bool wxGenericFileCtrl::Create( wxWindow *parent,
904 wxWindowID id,
905 const wxString& defaultDirectory,
906 const wxString& defaultFileName,
907 const wxString& wildCard,
908 long style,
909 const wxPoint& pos,
910 const wxSize& size,
911 const wxString& name )
912{
913 this->m_style = style;
914 m_inSelected = false;
915 m_noSelChgEvent = false;
8ce68f7f 916 m_check = NULL;
0cf3e587
VZ
917
918 // check that the styles are not contradictory
919 wxASSERT_MSG( !( ( m_style & wxFC_SAVE ) && ( m_style & wxFC_OPEN ) ),
920 wxT( "can't specify both wxFC_SAVE and wxFC_OPEN at once" ) );
921
922 wxASSERT_MSG( !( ( m_style & wxFC_SAVE ) && ( m_style & wxFC_MULTIPLE ) ),
923 wxT( "wxFC_MULTIPLE can't be used with wxFC_SAVE" ) );
924
5804d539
VZ
925 wxNavigationEnabled<wxControl>::Create( parent, id,
926 pos, size,
927 wxTAB_TRAVERSAL,
928 wxDefaultValidator,
929 name );
0cf3e587
VZ
930
931 m_dir = defaultDirectory;
932
933 m_ignoreChanges = true;
934
935 if ( ( m_dir.empty() ) || ( m_dir == wxT( "." ) ) )
936 {
937 m_dir = wxGetCwd();
938 if ( m_dir.empty() )
939 m_dir = wxFILE_SEP_PATH;
940 }
941
942 const size_t len = m_dir.length();
943 if ( ( len > 1 ) && ( wxEndsWithPathSeparator( m_dir ) ) )
944 m_dir.Remove( len - 1, 1 );
945
946 m_filterExtension = wxEmptyString;
947
948 // layout
949
950 const bool is_pda = ( wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA );
951
952 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
953
954 wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL );
955 if ( is_pda )
8ce68f7f
VZ
956 staticsizer->Add( new wxStaticText( this, wxID_ANY, _( "Current directory:" ) ),
957 wxSizerFlags().DoubleBorder(wxRIGHT) );
0cf3e587
VZ
958 m_static = new wxStaticText( this, wxID_ANY, m_dir );
959 staticsizer->Add( m_static, 1 );
8ce68f7f 960 mainsizer->Add( staticsizer, wxSizerFlags().Expand().Border());
0cf3e587
VZ
961
962 long style2 = wxLC_LIST;
963 if ( !( m_style & wxFC_MULTIPLE ) )
964 style2 |= wxLC_SINGLE_SEL;
965
966#ifdef __WXWINCE__
967 style2 |= wxSIMPLE_BORDER;
968#else
969 style2 |= wxSUNKEN_BORDER;
970#endif
971
16a6b53d
VZ
972 m_list = new wxFileListCtrl( this, ID_FILELIST_CTRL,
973 wxEmptyString, false,
974 wxDefaultPosition, wxSize( 400, 140 ),
975 style2 );
0cf3e587
VZ
976
977 m_text = new wxTextCtrl( this, ID_TEXT, wxEmptyString,
978 wxDefaultPosition, wxDefaultSize,
979 wxTE_PROCESS_ENTER );
980 m_choice = new wxChoice( this, ID_CHOICE );
981
982 if ( is_pda )
983 {
984 // PDAs have a different screen layout
985 mainsizer->Add( m_list, wxSizerFlags( 1 ).Expand().HorzBorder() );
986
987 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
988 textsizer->Add( m_text, wxSizerFlags( 1 ).Centre().Border() );
8ce68f7f 989 textsizer->Add( m_choice, wxSizerFlags( 1 ).Centre().Border() );
0cf3e587
VZ
990 mainsizer->Add( textsizer, wxSizerFlags().Expand() );
991
0cf3e587
VZ
992 }
993 else // !is_pda
994 {
8ce68f7f
VZ
995 mainsizer->Add( m_list, wxSizerFlags( 1 ).Expand().Border() );
996 mainsizer->Add( m_text, wxSizerFlags().Expand().Border() );
0cf3e587
VZ
997
998 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
8ce68f7f 999 choicesizer->Add( m_choice, wxSizerFlags( 1 ).Centre() );
0cf3e587
VZ
1000
1001 if ( !( m_style & wxFC_NOSHOWHIDDEN ) )
8ce68f7f 1002 {
0cf3e587 1003 m_check = new wxCheckBox( this, ID_CHECK, _( "Show &hidden files" ) );
8ce68f7f
VZ
1004 choicesizer->Add( m_check, wxSizerFlags().Centre().DoubleBorder(wxLEFT) );
1005 }
0cf3e587 1006
8ce68f7f 1007 mainsizer->Add( choicesizer, wxSizerFlags().Expand().Border() );
0cf3e587
VZ
1008 }
1009
1010 SetWildcard( wildCard );
1011
1012 SetAutoLayout( true );
1013 SetSizer( mainsizer );
1014
1015 if ( !is_pda )
1016 {
1017 mainsizer->Fit( this );
1018 }
1019
1020 m_list->GoToDir( m_dir );
1021 UpdateControls();
1022 m_text->SetValue( m_fileName );
1023
1024 m_ignoreChanges = false;
1025
1026 // must be after m_ignoreChanges = false
1027 SetFilename( defaultFileName );
1028
1029 return true;
1030}
1031
398eebb1
VZ
1032// NB: there is an unfortunate mismatch between wxFileName and wxFileDialog
1033// method names but our GetDirectory() does correspond to wxFileName::
1034// GetPath() while our GetPath() is wxFileName::GetFullPath()
0cf3e587
VZ
1035wxString wxGenericFileCtrl::GetPath() const
1036{
398eebb1
VZ
1037 wxASSERT_MSG ( !(m_style & wxFC_MULTIPLE), "use GetPaths() instead" );
1038
1039 return DoGetFileName().GetFullPath();
0cf3e587
VZ
1040}
1041
1042wxString wxGenericFileCtrl::GetFilename() const
1043{
398eebb1
VZ
1044 wxASSERT_MSG ( !(m_style & wxFC_MULTIPLE), "use GetFilenames() instead" );
1045
1046 return DoGetFileName().GetFullName();
0cf3e587
VZ
1047}
1048
398eebb1 1049wxString wxGenericFileCtrl::GetDirectory() const
0cf3e587 1050{
398eebb1
VZ
1051 // don't check for wxFC_MULTIPLE here, this one is probably safe to call in
1052 // any case as it can be always taken to mean "current directory"
1053 return DoGetFileName().GetPath();
1054}
0cf3e587 1055
398eebb1
VZ
1056wxFileName wxGenericFileCtrl::DoGetFileName() const
1057{
1058 wxFileName fn;
0cf3e587 1059
398eebb1
VZ
1060 wxString value = m_text->GetValue();
1061 if ( value.empty() )
1062 {
1063 // nothing in the text control, get the selected file from the list
1064 wxListItem item;
1065 item.m_itemId = m_list->GetNextItem(-1, wxLIST_NEXT_ALL,
1066 wxLIST_STATE_SELECTED);
1067 m_list->GetItem(item);
1068
1069 fn.Assign(m_list->GetDir(), item.m_text);
1070 }
1071 else // user entered the value
1072 {
1073 // the path can be either absolute or relative
1074 fn.Assign(value);
1075 if ( fn.IsRelative() )
1076 fn.MakeAbsolute(m_list->GetDir());
1077 }
1078
1079 return fn;
0cf3e587
VZ
1080}
1081
d42b8535
VZ
1082// helper used in DoGetFilenames() and needed because Borland can't compile
1083// operator?: inline
1084static inline wxString GetFileNameOrPath(const wxFileName& fn, bool fullPath)
1085{
1086 return fullPath ? fn.GetFullPath() : fn.GetFullName();
1087}
1088
1089void
1090wxGenericFileCtrl::DoGetFilenames(wxArrayString& filenames, bool fullPath) const
0cf3e587 1091{
7d42f4d6 1092 filenames.clear();
0cf3e587 1093
398eebb1 1094 const wxString dir = m_list->GetDir();
0cf3e587 1095
7d42f4d6 1096 const wxString value = m_text->GetValue();
0cf3e587
VZ
1097 if ( !value.empty() )
1098 {
7d42f4d6
VZ
1099 wxFileName fn(value);
1100 if ( fn.IsRelative() )
1101 fn.MakeAbsolute(dir);
1102
d42b8535 1103 filenames.push_back(GetFileNameOrPath(fn, fullPath));
0cf3e587
VZ
1104 return;
1105 }
1106
7d42f4d6
VZ
1107 const int numSel = m_list->GetSelectedItemCount();
1108 if ( !numSel )
0cf3e587 1109 return;
0cf3e587 1110
7d42f4d6 1111 filenames.reserve(numSel);
0cf3e587
VZ
1112
1113 wxListItem item;
1114 item.m_mask = wxLIST_MASK_TEXT;
7d42f4d6
VZ
1115 item.m_itemId = -1;
1116 for ( ;; )
0cf3e587 1117 {
7d42f4d6
VZ
1118 item.m_itemId = m_list->GetNextItem(item.m_itemId, wxLIST_NEXT_ALL,
1119 wxLIST_STATE_SELECTED);
0cf3e587 1120
7d42f4d6
VZ
1121 if ( item.m_itemId == -1 )
1122 break;
1123
1124 m_list->GetItem(item);
0cf3e587 1125
7d42f4d6 1126 const wxFileName fn(dir, item.m_text);
d42b8535 1127 filenames.push_back(GetFileNameOrPath(fn, fullPath));
0cf3e587
VZ
1128 }
1129}
1130
1131bool wxGenericFileCtrl::SetDirectory( const wxString& dir )
1132{
1133 m_ignoreChanges = true;
1134 m_list->GoToDir( dir );
1135 UpdateControls();
1136 m_ignoreChanges = false;
1137
1138 return wxFileName( dir ).SameAs( m_list->GetDir() );
1139}
1140
0cf3e587
VZ
1141bool wxGenericFileCtrl::SetFilename( const wxString& name )
1142{
1143 const long item = m_list->FindItem( -1, name );
1144
1145 if ( item == -1 ) // file not found either because it doesn't exist or the
1146 // current filter doesn't show it.
1147 return false;
1148
1149 m_noSelChgEvent = true;
1150
1151 // Deselect selected items
1152 {
1153 const int numSelectedItems = m_list->GetSelectedItemCount();
1154
1155 if ( numSelectedItems > 0 )
1156 {
1157 long itemIndex = -1;
1158
1159 for ( ;; )
1160 {
1161 itemIndex = m_list->GetNextItem( itemIndex, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1162 if ( itemIndex == -1 )
1163 break;
1164
1165 m_list->SetItemState( itemIndex, 0, wxLIST_STATE_SELECTED );
1166 }
1167 }
1168 }
1169
1170 m_list->SetItemState( item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
1171 m_list->EnsureVisible( item );
1172
1173 m_noSelChgEvent = false;
1174
1175 return true;
1176}
1177
1178void wxGenericFileCtrl::DoSetFilterIndex( int filterindex )
1179{
a85ad1db
VZ
1180 wxClientData *pcd = m_choice->GetClientObject( filterindex );
1181 if ( !pcd )
1182 return;
1183
5c33522f 1184 const wxString& str = ((static_cast<wxStringClientData *>(pcd))->GetData());
0cf3e587
VZ
1185 m_list->SetWild( str );
1186 m_filterIndex = filterindex;
1187 if ( str.Left( 2 ) == wxT( "*." ) )
1188 {
1189 m_filterExtension = str.Mid( 1 );
9a83f860 1190 if ( m_filterExtension == wxT( ".*" ) )
0cf3e587
VZ
1191 m_filterExtension.clear();
1192 }
1193 else
1194 {
1195 m_filterExtension.clear();
1196 }
6305f044
VZ
1197
1198 GenerateFilterChangedEvent( this, this );
0cf3e587
VZ
1199}
1200
1201void wxGenericFileCtrl::SetWildcard( const wxString& wildCard )
1202{
1203 if ( wildCard.empty() || wildCard == wxFileSelectorDefaultWildcardStr )
1204 {
1205 m_wildCard = wxString::Format( _( "All files (%s)|%s" ),
1206 wxFileSelectorDefaultWildcardStr,
1207 wxFileSelectorDefaultWildcardStr );
1208 }
1209 else
1210 m_wildCard = wildCard;
1211
1212 wxArrayString wildDescriptions, wildFilters;
1213 const size_t count = wxParseCommonDialogsFilter( m_wildCard,
1214 wildDescriptions,
1215 wildFilters );
1216 wxCHECK_RET( count, wxT( "wxFileDialog: bad wildcard string" ) );
1217
1218 m_choice->Clear();
1219
1220 for ( size_t n = 0; n < count; n++ )
1221 {
1222 m_choice->Append(wildDescriptions[n], new wxStringClientData(wildFilters[n]));
1223 }
1224
1225 SetFilterIndex( 0 );
1226}
1227
1228void wxGenericFileCtrl::SetFilterIndex( int filterindex )
1229{
1230 m_choice->SetSelection( filterindex );
1231
1232 DoSetFilterIndex( filterindex );
1233}
1234
1235void wxGenericFileCtrl::OnChoiceFilter( wxCommandEvent &event )
1236{
1237 DoSetFilterIndex( ( int )event.GetInt() );
1238}
1239
1240void wxGenericFileCtrl::OnCheck( wxCommandEvent &event )
1241{
1242 m_list->ShowHidden( event.GetInt() != 0 );
1243}
1244
1245void wxGenericFileCtrl::OnActivated( wxListEvent &event )
1246{
1247 HandleAction( event.m_item.m_text );
1248}
1249
1250void wxGenericFileCtrl::OnTextEnter( wxCommandEvent &WXUNUSED( event ) )
1251{
1252 HandleAction( m_text->GetValue() );
1253}
1254
1255void wxGenericFileCtrl::OnTextChange( wxCommandEvent &WXUNUSED( event ) )
1256{
1257 if ( !m_ignoreChanges )
1258 {
1259 // Clear selections. Otherwise when the user types in a value they may
1260 // not get the file whose name they typed.
1261 if ( m_list->GetSelectedItemCount() > 0 )
1262 {
1263 long item = m_list->GetNextItem( -1, wxLIST_NEXT_ALL,
1264 wxLIST_STATE_SELECTED );
1265 while ( item != -1 )
1266 {
1267 m_list->SetItemState( item, 0, wxLIST_STATE_SELECTED );
1268 item = m_list->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1269 }
1270 }
1271 }
1272}
1273
1274void wxGenericFileCtrl::OnSelected( wxListEvent &event )
1275{
1276 if ( m_ignoreChanges )
1277 return;
1278
1279 if ( m_inSelected )
1280 return;
1281
1282 m_inSelected = true;
1283 const wxString filename( event.m_item.m_text );
1284
1285#ifdef __WXWINCE__
1286 // No double-click on most WinCE devices, so do action immediately.
1287 HandleAction( filename );
1288#else
1289 if ( filename == wxT( ".." ) )
1290 {
1291 m_inSelected = false;
1292 return;
1293 }
1294
1295 wxString dir = m_list->GetDir();
1296 if ( !IsTopMostDir( dir ) )
1297 dir += wxFILE_SEP_PATH;
1298 dir += filename;
1299 if ( wxDirExists( dir ) )
1300 {
1301 m_inSelected = false;
1302
1303 return;
1304 }
bd365871 1305
0cf3e587
VZ
1306
1307 m_ignoreChanges = true;
1308 m_text->SetValue( filename );
1309
1310 if ( m_list->GetSelectedItemCount() > 1 )
1311 {
1312 m_text->Clear();
1313 }
1314
1315 if ( !m_noSelChgEvent )
1316 GenerateSelectionChangedEvent( this, this );
1317
1318 m_ignoreChanges = false;
1319#endif
1320 m_inSelected = false;
1321}
1322
1323void wxGenericFileCtrl::HandleAction( const wxString &fn )
1324{
1325 if ( m_ignoreChanges )
1326 return;
1327
1328 wxString filename( fn );
1329 if ( filename.empty() )
1330 {
1331 return;
1332 }
1333 if ( filename == wxT( "." ) ) return;
1334
1335 wxString dir = m_list->GetDir();
1336
1337 // "some/place/" means they want to chdir not try to load "place"
1338 const bool want_dir = filename.Last() == wxFILE_SEP_PATH;
1339 if ( want_dir )
1340 filename = filename.RemoveLast();
1341
1342 if ( filename == wxT( ".." ) )
1343 {
1344 m_ignoreChanges = true;
1345 m_list->GoToParentDir();
1346
1347 GenerateFolderChangedEvent( this, this );
1348
1349 UpdateControls();
1350 m_ignoreChanges = false;
1351 return;
1352 }
1353
1354#ifdef __UNIX__
1355 if ( filename == wxT( "~" ) )
1356 {
1357 m_ignoreChanges = true;
1358 m_list->GoToHomeDir();
1359
1360 GenerateFolderChangedEvent( this, this );
1361
1362 UpdateControls();
1363 m_ignoreChanges = false;
1364 return;
1365 }
1366
1367 if ( filename.BeforeFirst( wxT( '/' ) ) == wxT( "~" ) )
1368 {
1369 filename = wxString( wxGetUserHome() ) + filename.Remove( 0, 1 );
1370 }
1371#endif // __UNIX__
1372
1373 if ( !( m_style & wxFC_SAVE ) )
1374 {
1375 if ( ( filename.Find( wxT( '*' ) ) != wxNOT_FOUND ) ||
1376 ( filename.Find( wxT( '?' ) ) != wxNOT_FOUND ) )
1377 {
1378 if ( filename.Find( wxFILE_SEP_PATH ) != wxNOT_FOUND )
1379 {
1380 wxMessageBox( _( "Illegal file specification." ),
1381 _( "Error" ), wxOK | wxICON_ERROR, this );
1382 return;
1383 }
1384 m_list->SetWild( filename );
1385 return;
1386 }
1387 }
1388
1389 if ( !IsTopMostDir( dir ) )
1390 dir += wxFILE_SEP_PATH;
1391 if ( !wxIsAbsolutePath( filename ) )
1392 {
1393 dir += filename;
1394 filename = dir;
1395 }
1396
1397 if ( wxDirExists( filename ) )
1398 {
1399 m_ignoreChanges = true;
1400 m_list->GoToDir( filename );
1401 UpdateControls();
1402
1403 GenerateFolderChangedEvent( this, this );
1404
1405 m_ignoreChanges = false;
1406 return;
1407 }
1408
1409 // they really wanted a dir, but it doesn't exist
1410 if ( want_dir )
1411 {
1412 wxMessageBox( _( "Directory doesn't exist." ), _( "Error" ),
1413 wxOK | wxICON_ERROR, this );
1414 return;
1415 }
1416
1417 // append the default extension to the filename if it doesn't have any
1418 //
1419 // VZ: the logic of testing for !wxFileExists() only for the open file
1420 // dialog is not entirely clear to me, why don't we allow saving to a
1421 // file without extension as well?
1422 if ( !( m_style & wxFC_OPEN ) || !wxFileExists( filename ) )
1423 {
1424 filename = wxFileDialogBase::AppendExtension( filename, m_filterExtension );
1425 GenerateFileActivatedEvent( this, this, wxFileName( filename ).GetFullName() );
1426 return;
1427 }
1428
1429 GenerateFileActivatedEvent( this, this );
1430}
1431
1432bool wxGenericFileCtrl::SetPath( const wxString& path )
1433{
1434 if ( !wxFileName::FileExists( ( path ) ) )
1435 return false;
1436
1437 wxString ext;
bd365871 1438 wxFileName::SplitPath( path, &m_dir, &m_fileName, &ext );
0cf3e587
VZ
1439 if ( !ext.empty() )
1440 {
1441 m_fileName += wxT( "." );
1442 m_fileName += ext;
1443 }
1444
1445 SetDirectory( m_dir );
1446 SetFilename( m_fileName );
1447
1448 return true;
1449}
1450
1451void wxGenericFileCtrl::GetPaths( wxArrayString& paths ) const
1452{
1453 DoGetFilenames( paths, true );
1454}
1455
1456void wxGenericFileCtrl::GetFilenames( wxArrayString& files ) const
1457{
1458 DoGetFilenames( files, false );
1459}
1460
1461void wxGenericFileCtrl::UpdateControls()
1462{
1463 const wxString dir = m_list->GetDir();
1464 m_static->SetLabel( dir );
1465}
1466
1467void wxGenericFileCtrl::GoToParentDir()
1468{
1469 m_list->GoToParentDir();
1470 UpdateControls();
1471}
1472
1473void wxGenericFileCtrl::GoToHomeDir()
1474{
1475 m_list->GoToHomeDir();
1476 UpdateControls();
1477}
1478
0cf3e587 1479#endif // wxUSE_FILECTRL