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