1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/filectrlg.cpp
3 // Purpose: wxGenericFileCtrl Implementation
4 // Author: Diaa M. Sami
7 // Copyright: (c) Diaa M. Sami
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
19 #include "wx/generic/filectrlg.h"
22 #include "wx/settings.h"
24 #include "wx/stattext.h"
25 #include "wx/checkbox.h"
26 #include "wx/msgdlg.h"
28 #include "wx/filedlg.h"
31 #include "wx/clntdata.h"
32 #include "wx/file.h" // for wxS_IXXX constants only
33 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
35 #include "wx/tokenzr.h"
36 #include "wx/imaglist.h"
39 #include "wx/msw/wrapwin.h"
42 #if defined(__WXWINCE__)
43 #define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
44 #elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
45 #define IsTopMostDir(dir) (dir.empty())
47 #define IsTopMostDir(dir) (dir == wxT("/"))
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
56 int wxCALLBACK
wxFileDataNameCompare( wxIntPtr data1
, wxIntPtr data2
, wxIntPtr sortOrder
)
58 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
59 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
61 if (fd1
->GetFileName() == wxT(".."))
63 if (fd2
->GetFileName() == wxT(".."))
65 if (fd1
->IsDir() && !fd2
->IsDir())
67 if (fd2
->IsDir() && !fd1
->IsDir())
70 return sortOrder
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
74 int wxCALLBACK
wxFileDataSizeCompare(wxIntPtr data1
, wxIntPtr data2
, wxIntPtr sortOrder
)
76 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
77 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
79 if (fd1
->GetFileName() == wxT(".."))
81 if (fd2
->GetFileName() == wxT(".."))
83 if (fd1
->IsDir() && !fd2
->IsDir())
85 if (fd2
->IsDir() && !fd1
->IsDir())
87 if (fd1
->IsLink() && !fd2
->IsLink())
89 if (fd2
->IsLink() && !fd1
->IsLink())
92 return fd1
->GetSize() > fd2
->GetSize() ? sortOrder
: -sortOrder
;
96 int wxCALLBACK
wxFileDataTypeCompare(wxIntPtr data1
, wxIntPtr data2
, wxIntPtr sortOrder
)
98 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
99 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
101 if (fd1
->GetFileName() == wxT(".."))
103 if (fd2
->GetFileName() == wxT(".."))
105 if (fd1
->IsDir() && !fd2
->IsDir())
107 if (fd2
->IsDir() && !fd1
->IsDir())
109 if (fd1
->IsLink() && !fd2
->IsLink())
111 if (fd2
->IsLink() && !fd1
->IsLink())
114 return sortOrder
*wxStrcmp( fd1
->GetFileType(), fd2
->GetFileType() );
118 int wxCALLBACK
wxFileDataTimeCompare(wxIntPtr data1
, wxIntPtr data2
, wxIntPtr sortOrder
)
120 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
121 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
123 if (fd1
->GetFileName() == wxT(".."))
125 if (fd2
->GetFileName() == wxT(".."))
127 if (fd1
->IsDir() && !fd2
->IsDir())
129 if (fd2
->IsDir() && !fd1
->IsDir())
132 return fd1
->GetDateTime().IsLaterThan(fd2
->GetDateTime()) ? sortOrder
: -sortOrder
;
135 // defined in src/generic/dirctrlg.cpp
136 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
138 //-----------------------------------------------------------------------------
140 //-----------------------------------------------------------------------------
142 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
145 m_fileName
= fileName
;
146 m_filePath
= filePath
;
153 void wxFileData::Init()
156 m_type
= wxFileData::is_file
;
157 m_image
= wxFileIconsTable::file
;
160 void wxFileData::Copy( const wxFileData
& fileData
)
162 m_fileName
= fileData
.GetFileName();
163 m_filePath
= fileData
.GetFilePath();
164 m_size
= fileData
.GetSize();
165 m_dateTime
= fileData
.GetDateTime();
166 m_permissions
= fileData
.GetPermissions();
167 m_type
= fileData
.GetType();
168 m_image
= fileData
.GetImageId();
171 void wxFileData::ReadData()
179 #if defined(__DOS__) || (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__OS2__)
180 // c:\.. is a drive don't stat it
181 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
187 #endif // __DOS__ || __WINDOWS__
193 DWORD fileAttribs
= GetFileAttributes(m_filePath
.fn_str());
194 m_type
|= (fileAttribs
& FILE_ATTRIBUTE_DIRECTORY
) != 0 ? is_dir
: 0;
197 wxFileName::SplitPath(m_filePath
, & p
, & f
, & ext
);
198 if (wxStricmp(ext
, wxT("exe")) == 0)
203 HANDLE fileHandle
= CreateFile(m_filePath
.fn_str(),
208 FILE_ATTRIBUTE_NORMAL
,
211 if (fileHandle
!= INVALID_HANDLE_VALUE
)
213 m_size
= GetFileSize(fileHandle
, 0);
214 CloseHandle(fileHandle
);
217 m_dateTime
= wxFileModificationTime(m_filePath
);
225 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
226 const bool hasStat
= lstat( m_filePath
.fn_str(), &buff
) == 0;
228 m_type
|= S_ISLNK(buff
.st_mode
) ? is_link
: 0;
230 const bool hasStat
= wxStat( m_filePath
, &buff
) == 0;
235 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
236 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
238 m_size
= buff
.st_size
;
240 m_dateTime
= buff
.st_mtime
;
245 #if defined(__UNIX__)
248 m_permissions
.Printf(wxT("%c%c%c%c%c%c%c%c%c"),
249 buff
.st_mode
& wxS_IRUSR
? wxT('r') : wxT('-'),
250 buff
.st_mode
& wxS_IWUSR
? wxT('w') : wxT('-'),
251 buff
.st_mode
& wxS_IXUSR
? wxT('x') : wxT('-'),
252 buff
.st_mode
& wxS_IRGRP
? wxT('r') : wxT('-'),
253 buff
.st_mode
& wxS_IWGRP
? wxT('w') : wxT('-'),
254 buff
.st_mode
& wxS_IXGRP
? wxT('x') : wxT('-'),
255 buff
.st_mode
& wxS_IROTH
? wxT('r') : wxT('-'),
256 buff
.st_mode
& wxS_IWOTH
? wxT('w') : wxT('-'),
257 buff
.st_mode
& wxS_IXOTH
? wxT('x') : wxT('-'));
259 #elif defined(__WIN32__)
260 DWORD attribs
= ::GetFileAttributes(m_filePath
.c_str());
261 if (attribs
!= (DWORD
)-1)
263 m_permissions
.Printf(wxT("%c%c%c%c"),
264 attribs
& FILE_ATTRIBUTE_ARCHIVE
? wxT('A') : wxT(' '),
265 attribs
& FILE_ATTRIBUTE_READONLY
? wxT('R') : wxT(' '),
266 attribs
& FILE_ATTRIBUTE_HIDDEN
? wxT('H') : wxT(' '),
267 attribs
& FILE_ATTRIBUTE_SYSTEM
? wxT('S') : wxT(' '));
271 // try to get a better icon
272 if (m_image
== wxFileIconsTable::file
)
274 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
276 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
279 m_image
= wxFileIconsTable::executable
;
284 wxString
wxFileData::GetFileType() const
292 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
293 return m_fileName
.AfterLast(wxT('.'));
295 return wxEmptyString
;
298 wxString
wxFileData::GetModificationTime() const
300 // want time as 01:02 so they line up nicely, no %r in WIN32
301 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
304 wxString
wxFileData::GetHint() const
306 wxString s
= m_filePath
;
316 s
+= wxString::Format(wxPLURAL("%ld byte", "%ld bytes", m_size
),
317 wxLongLong(m_size
).ToString().c_str());
323 s
<< GetModificationTime()
331 wxString
wxFileData::GetEntry( fileListFieldType num
) const
341 if (!IsDir() && !IsLink() && !IsDrive())
342 s
= wxLongLong(m_size
).ToString();
351 s
= GetModificationTime();
354 #if defined(__UNIX__) || defined(__WIN32__)
358 #endif // defined(__UNIX__) || defined(__WIN32__)
361 wxFAIL_MSG( wxT("unexpected field in wxFileData::GetEntry()") );
367 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
369 m_fileName
= fileName
;
370 m_filePath
= filePath
;
373 void wxFileData::MakeItem( wxListItem
&item
)
375 item
.m_text
= m_fileName
;
376 item
.ClearAttributes();
378 item
.SetTextColour(*wxRED
);
380 item
.SetTextColour(*wxBLUE
);
382 item
.m_image
= m_image
;
386 wxColour dg
= wxTheColourDatabase
->Find( wxT("MEDIUM GREY") );
388 item
.SetTextColour(dg
);
390 item
.m_data
= wxPtrToUInt(this);
393 //-----------------------------------------------------------------------------
395 //-----------------------------------------------------------------------------
397 IMPLEMENT_DYNAMIC_CLASS(wxFileListCtrl
,wxListCtrl
)
399 BEGIN_EVENT_TABLE(wxFileListCtrl
,wxListCtrl
)
400 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileListCtrl::OnListDeleteItem
)
401 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileListCtrl::OnListDeleteAllItems
)
402 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileListCtrl::OnListEndLabelEdit
)
403 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileListCtrl::OnListColClick
)
407 wxFileListCtrl::wxFileListCtrl()
409 m_showHidden
= false;
410 m_sort_forward
= true;
411 m_sort_field
= wxFileData::FileList_Name
;
414 wxFileListCtrl::wxFileListCtrl(wxWindow
*win
,
416 const wxString
& wild
,
421 const wxValidator
&validator
,
422 const wxString
&name
)
423 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
426 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
428 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
430 m_showHidden
= showHidden
;
432 m_sort_forward
= true;
433 m_sort_field
= wxFileData::FileList_Name
;
435 m_dirName
= wxT("*");
437 if (style
& wxLC_REPORT
)
438 ChangeToReportMode();
441 void wxFileListCtrl::ChangeToListMode()
444 SetSingleStyle( wxLC_LIST
);
448 void wxFileListCtrl::ChangeToReportMode()
451 SetSingleStyle( wxLC_REPORT
);
453 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
454 // don't hardcode since mm/dd is dd/mm elsewhere
456 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
457 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
458 GetTextExtent(txt
, &w
, &h
);
460 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
461 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
462 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
463 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
464 #if defined(__UNIX__)
465 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
466 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
467 #elif defined(__WIN32__)
468 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
469 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
475 void wxFileListCtrl::ChangeToSmallIconMode()
478 SetSingleStyle( wxLC_SMALL_ICON
);
482 void wxFileListCtrl::ShowHidden( bool show
)
488 long wxFileListCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
491 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
492 fd
->MakeItem( item
);
493 long my_style
= GetWindowStyleFlag();
494 if (my_style
& wxLC_REPORT
)
496 ret
= InsertItem( item
);
497 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
498 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
500 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
502 ret
= InsertItem( item
);
507 void wxFileListCtrl::UpdateItem(const wxListItem
&item
)
509 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
510 wxCHECK_RET(fd
, wxT("invalid filedata"));
514 SetItemText(item
, fd
->GetFileName());
515 SetItemImage(item
, fd
->GetImageId());
517 if (GetWindowStyleFlag() & wxLC_REPORT
)
519 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
520 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
524 void wxFileListCtrl::UpdateFiles()
526 // don't do anything before ShowModal() call which sets m_dirName
527 if ( m_dirName
== wxT("*") )
530 wxBusyCursor bcur
; // this may take a while...
538 #if (defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)) && !defined(__WXWINCE__)
539 if ( IsTopMostDir(m_dirName
) )
541 wxArrayString names
, paths
;
543 const size_t count
= wxGetAvailableDrives(paths
, names
, icons
);
545 for ( size_t n
= 0; n
< count
; n
++ )
547 // use paths[n] as the drive name too as our HandleAction() can't
548 // deal with the drive names (of the form "System (C:)") currently
549 // as it mistakenly treats them as file names
551 // it would be preferable to show names, and not paths, in the
552 // dialog just as the native dialog does but for this we must:
553 // a) store the item type as item data and modify HandleAction()
554 // to use it instead of wxDirExists() to check whether the item
556 // b) store the drives by their drive letters and not their
557 // descriptions as otherwise it's pretty confusing to the user
558 wxFileData
*fd
= new wxFileData(paths
[n
], paths
[n
],
559 wxFileData::is_drive
, icons
[n
]);
560 if (Add(fd
, item
) != -1)
567 #endif // defined(__DOS__) || defined(__WINDOWS__)
570 if ( !IsTopMostDir(m_dirName
) && !m_dirName
.empty() )
572 wxString
p(wxPathOnly(m_dirName
));
573 #if (defined(__UNIX__) || defined(__WXWINCE__)) && !defined(__OS2__)
574 if (p
.empty()) p
= wxT("/");
576 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
577 if (Add(fd
, item
) != -1)
583 wxString
dirname(m_dirName
);
584 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
585 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
586 dirname
<< wxT('\\');
587 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
590 dirname
= wxFILE_SEP_PATH
;
595 if ( dir
.IsOpened() )
597 wxString
dirPrefix(dirname
);
598 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
599 dirPrefix
+= wxFILE_SEP_PATH
;
601 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
606 // Get the directories first (not matched against wildcards):
607 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
610 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
611 if (Add(fd
, item
) != -1)
616 cont
= dir
.GetNext(&f
);
619 // Tokenize the wildcard string, so we can handle more than 1
620 // search pattern in a wildcard.
621 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
622 while ( tokenWild
.HasMoreTokens() )
624 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
625 wxDIR_FILES
| hiddenFlag
);
628 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
629 if (Add(fd
, item
) != -1)
634 cont
= dir
.GetNext(&f
);
640 SortItems(m_sort_field
, m_sort_forward
);
643 void wxFileListCtrl::SetWild( const wxString
&wild
)
645 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
652 void wxFileListCtrl::MakeDir()
654 wxString
new_name( _("NewName") );
655 wxString
path( m_dirName
);
656 path
+= wxFILE_SEP_PATH
;
658 if (wxFileExists(path
))
660 // try NewName0, NewName1 etc.
663 new_name
= _("NewName");
665 num
.Printf( wxT("%d"), i
);
669 path
+= wxFILE_SEP_PATH
;
672 } while (wxFileExists(path
));
678 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
683 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
687 long itemid
= Add( fd
, item
);
691 SortItems(m_sort_field
, m_sort_forward
);
692 itemid
= FindItem( 0, wxPtrToUInt(fd
) );
693 EnsureVisible( itemid
);
700 void wxFileListCtrl::GoToParentDir()
702 if (!IsTopMostDir(m_dirName
))
704 size_t len
= m_dirName
.length();
705 if (wxEndsWithPathSeparator(m_dirName
))
706 m_dirName
.Remove( len
-1, 1 );
707 wxString
fname( wxFileNameFromPath(m_dirName
) );
708 m_dirName
= wxPathOnly( m_dirName
);
709 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
710 if (!m_dirName
.empty())
712 if (m_dirName
.Last() == wxT('.'))
713 m_dirName
= wxEmptyString
;
715 #elif defined(__UNIX__)
716 if (m_dirName
.empty())
717 m_dirName
= wxT("/");
720 long id
= FindItem( 0, fname
);
721 if (id
!= wxNOT_FOUND
)
723 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
729 void wxFileListCtrl::GoToHomeDir()
731 wxString s
= wxGetUserHome( wxString() );
735 void wxFileListCtrl::GoToDir( const wxString
&dir
)
737 if (!wxDirExists(dir
)) return;
742 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
747 void wxFileListCtrl::FreeItemData(wxListItem
& item
)
751 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
758 void wxFileListCtrl::OnListDeleteItem( wxListEvent
&event
)
760 FreeItemData(event
.m_item
);
763 void wxFileListCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
768 void wxFileListCtrl::FreeAllItemsData()
771 item
.m_mask
= wxLIST_MASK_DATA
;
773 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
774 while ( item
.m_itemId
!= -1 )
778 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
782 void wxFileListCtrl::OnListEndLabelEdit( wxListEvent
&event
)
784 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
787 if ((event
.GetLabel().empty()) ||
788 (event
.GetLabel() == wxT(".")) ||
789 (event
.GetLabel() == wxT("..")) ||
790 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
792 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
798 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
799 new_name
+= wxFILE_SEP_PATH
;
800 new_name
+= event
.GetLabel();
804 if (wxFileExists(new_name
))
806 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
811 if (wxRenameFile(fd
->GetFilePath(),new_name
))
813 fd
->SetNewName( new_name
, event
.GetLabel() );
815 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
817 UpdateItem( event
.GetItem() );
818 EnsureVisible( event
.GetItem() );
822 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
828 void wxFileListCtrl::OnListColClick( wxListEvent
&event
)
830 int col
= event
.GetColumn();
834 case wxFileData::FileList_Name
:
835 case wxFileData::FileList_Size
:
836 case wxFileData::FileList_Type
:
837 case wxFileData::FileList_Time
: break;
841 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
842 m_sort_forward
= !m_sort_forward
;
844 m_sort_field
= (wxFileData::fileListFieldType
)col
;
846 SortItems(m_sort_field
, m_sort_forward
);
849 void wxFileListCtrl::SortItems(wxFileData::fileListFieldType field
, bool forward
)
851 m_sort_field
= field
;
852 m_sort_forward
= forward
;
853 const long sort_dir
= forward
? 1 : -1;
855 switch (m_sort_field
)
857 case wxFileData::FileList_Size
:
858 wxListCtrl::SortItems(wxFileDataSizeCompare
, sort_dir
);
861 case wxFileData::FileList_Type
:
862 wxListCtrl::SortItems(wxFileDataTypeCompare
, sort_dir
);
865 case wxFileData::FileList_Time
:
866 wxListCtrl::SortItems(wxFileDataTimeCompare
, sort_dir
);
869 case wxFileData::FileList_Name
:
871 wxListCtrl::SortItems(wxFileDataNameCompare
, sort_dir
);
876 wxFileListCtrl::~wxFileListCtrl()
878 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
879 // wxFileListCtrl::OnListDeleteAllItems. But if the event is generated after
880 // the destruction of the wxFileListCtrl we need to free any data here:
884 #define ID_CHOICE (wxID_FILECTRL + 1)
885 #define ID_TEXT (wxID_FILECTRL + 2)
886 #define ID_FILELIST_CTRL (wxID_FILECTRL + 3)
887 #define ID_CHECK (wxID_FILECTRL + 4)
889 ///////////////////////////////////////////////////////////////////////////////
890 // wxGenericFileCtrl implementation
891 ///////////////////////////////////////////////////////////////////////////////
893 IMPLEMENT_DYNAMIC_CLASS( wxGenericFileCtrl
, wxNavigationEnabled
<wxControl
> )
895 BEGIN_EVENT_TABLE( wxGenericFileCtrl
, wxNavigationEnabled
<wxControl
> )
896 EVT_LIST_ITEM_SELECTED( ID_FILELIST_CTRL
, wxGenericFileCtrl::OnSelected
)
897 EVT_LIST_ITEM_ACTIVATED( ID_FILELIST_CTRL
, wxGenericFileCtrl::OnActivated
)
898 EVT_CHOICE( ID_CHOICE
, wxGenericFileCtrl::OnChoiceFilter
)
899 EVT_TEXT_ENTER( ID_TEXT
, wxGenericFileCtrl::OnTextEnter
)
900 EVT_TEXT( ID_TEXT
, wxGenericFileCtrl::OnTextChange
)
901 EVT_CHECKBOX( ID_CHECK
, wxGenericFileCtrl::OnCheck
)
904 bool wxGenericFileCtrl::Create( wxWindow
*parent
,
906 const wxString
& defaultDirectory
,
907 const wxString
& defaultFileName
,
908 const wxString
& wildCard
,
912 const wxString
& name
)
914 this->m_style
= style
;
915 m_inSelected
= false;
916 m_noSelChgEvent
= false;
919 // check that the styles are not contradictory
920 wxASSERT_MSG( !( ( m_style
& wxFC_SAVE
) && ( m_style
& wxFC_OPEN
) ),
921 wxT( "can't specify both wxFC_SAVE and wxFC_OPEN at once" ) );
923 wxASSERT_MSG( !( ( m_style
& wxFC_SAVE
) && ( m_style
& wxFC_MULTIPLE
) ),
924 wxT( "wxFC_MULTIPLE can't be used with wxFC_SAVE" ) );
926 wxNavigationEnabled
<wxControl
>::Create( parent
, id
,
932 m_dir
= defaultDirectory
;
934 m_ignoreChanges
= true;
936 if ( ( m_dir
.empty() ) || ( m_dir
== wxT( "." ) ) )
940 m_dir
= wxFILE_SEP_PATH
;
943 const size_t len
= m_dir
.length();
944 if ( ( len
> 1 ) && ( wxEndsWithPathSeparator( m_dir
) ) )
945 m_dir
.Remove( len
- 1, 1 );
947 m_filterExtension
= wxEmptyString
;
951 const bool is_pda
= ( wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
953 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
955 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
957 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _( "Current directory:" ) ),
958 wxSizerFlags().DoubleBorder(wxRIGHT
) );
959 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
960 staticsizer
->Add( m_static
, 1 );
961 mainsizer
->Add( staticsizer
, wxSizerFlags().Expand().Border());
963 long style2
= wxLC_LIST
;
964 if ( !( m_style
& wxFC_MULTIPLE
) )
965 style2
|= wxLC_SINGLE_SEL
;
968 style2
|= wxSIMPLE_BORDER
;
970 style2
|= wxSUNKEN_BORDER
;
973 m_list
= new wxFileListCtrl( this, ID_FILELIST_CTRL
,
974 wxEmptyString
, false,
975 wxDefaultPosition
, wxSize( 400, 140 ),
978 m_text
= new wxTextCtrl( this, ID_TEXT
, wxEmptyString
,
979 wxDefaultPosition
, wxDefaultSize
,
980 wxTE_PROCESS_ENTER
);
981 m_choice
= new wxChoice( this, ID_CHOICE
);
985 // PDAs have a different screen layout
986 mainsizer
->Add( m_list
, wxSizerFlags( 1 ).Expand().HorzBorder() );
988 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
989 textsizer
->Add( m_text
, wxSizerFlags( 1 ).Centre().Border() );
990 textsizer
->Add( m_choice
, wxSizerFlags( 1 ).Centre().Border() );
991 mainsizer
->Add( textsizer
, wxSizerFlags().Expand() );
996 mainsizer
->Add( m_list
, wxSizerFlags( 1 ).Expand().Border() );
997 mainsizer
->Add( m_text
, wxSizerFlags().Expand().Border() );
999 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1000 choicesizer
->Add( m_choice
, wxSizerFlags( 1 ).Centre() );
1002 if ( !( m_style
& wxFC_NOSHOWHIDDEN
) )
1004 m_check
= new wxCheckBox( this, ID_CHECK
, _( "Show &hidden files" ) );
1005 choicesizer
->Add( m_check
, wxSizerFlags().Centre().DoubleBorder(wxLEFT
) );
1008 mainsizer
->Add( choicesizer
, wxSizerFlags().Expand().Border() );
1011 SetWildcard( wildCard
);
1013 SetAutoLayout( true );
1014 SetSizer( mainsizer
);
1018 mainsizer
->Fit( this );
1021 m_list
->GoToDir( m_dir
);
1023 m_text
->SetValue( m_fileName
);
1025 m_ignoreChanges
= false;
1027 // must be after m_ignoreChanges = false
1028 SetFilename( defaultFileName
);
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
1038 wxASSERT_MSG ( !(m_style
& wxFC_MULTIPLE
), "use GetPaths() instead" );
1040 return DoGetFileName().GetFullPath();
1043 wxString
wxGenericFileCtrl::GetFilename() const
1045 wxASSERT_MSG ( !(m_style
& wxFC_MULTIPLE
), "use GetFilenames() instead" );
1047 return DoGetFileName().GetFullName();
1050 wxString
wxGenericFileCtrl::GetDirectory() const
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();
1057 wxFileName
wxGenericFileCtrl::DoGetFileName() const
1061 wxString value
= m_text
->GetValue();
1062 if ( value
.empty() )
1064 // nothing in the text control, get the selected file from the list
1066 item
.m_itemId
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1067 wxLIST_STATE_SELECTED
);
1068 m_list
->GetItem(item
);
1070 fn
.Assign(m_list
->GetDir(), item
.m_text
);
1072 else // user entered the value
1074 // the path can be either absolute or relative
1076 if ( fn
.IsRelative() )
1077 fn
.MakeAbsolute(m_list
->GetDir());
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
)
1087 return fullPath
? fn
.GetFullPath() : fn
.GetFullName();
1091 wxGenericFileCtrl::DoGetFilenames(wxArrayString
& filenames
, bool fullPath
) const
1095 const wxString dir
= m_list
->GetDir();
1097 const wxString value
= m_text
->GetValue();
1098 if ( !value
.empty() )
1100 wxFileName
fn(value
);
1101 if ( fn
.IsRelative() )
1102 fn
.MakeAbsolute(dir
);
1104 filenames
.push_back(GetFileNameOrPath(fn
, fullPath
));
1108 const int numSel
= m_list
->GetSelectedItemCount();
1112 filenames
.reserve(numSel
);
1115 item
.m_mask
= wxLIST_MASK_TEXT
;
1119 item
.m_itemId
= m_list
->GetNextItem(item
.m_itemId
, wxLIST_NEXT_ALL
,
1120 wxLIST_STATE_SELECTED
);
1122 if ( item
.m_itemId
== -1 )
1125 m_list
->GetItem(item
);
1127 const wxFileName
fn(dir
, item
.m_text
);
1128 filenames
.push_back(GetFileNameOrPath(fn
, fullPath
));
1132 bool wxGenericFileCtrl::SetDirectory( const wxString
& dir
)
1134 m_ignoreChanges
= true;
1135 m_list
->GoToDir( dir
);
1137 m_ignoreChanges
= false;
1139 return wxFileName( dir
).SameAs( m_list
->GetDir() );
1142 bool wxGenericFileCtrl::SetFilename( const wxString
& name
)
1144 const long item
= m_list
->FindItem( -1, name
);
1146 if ( item
== -1 ) // file not found either because it doesn't exist or the
1147 // current filter doesn't show it.
1150 m_noSelChgEvent
= true;
1152 // Deselect selected items
1154 const int numSelectedItems
= m_list
->GetSelectedItemCount();
1156 if ( numSelectedItems
> 0 )
1158 long itemIndex
= -1;
1162 itemIndex
= m_list
->GetNextItem( itemIndex
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1163 if ( itemIndex
== -1 )
1166 m_list
->SetItemState( itemIndex
, 0, wxLIST_STATE_SELECTED
);
1171 m_list
->SetItemState( item
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
1172 m_list
->EnsureVisible( item
);
1174 m_noSelChgEvent
= false;
1179 void wxGenericFileCtrl::DoSetFilterIndex( int filterindex
)
1181 wxClientData
*pcd
= m_choice
->GetClientObject( filterindex
);
1185 const wxString
& str
= ((static_cast<wxStringClientData
*>(pcd
))->GetData());
1186 m_list
->SetWild( str
);
1187 m_filterIndex
= filterindex
;
1188 if ( str
.Left( 2 ) == wxT( "*." ) )
1190 m_filterExtension
= str
.Mid( 1 );
1191 if ( m_filterExtension
== wxT( ".*" ) )
1192 m_filterExtension
.clear();
1196 m_filterExtension
.clear();
1199 GenerateFilterChangedEvent( this, this );
1202 void wxGenericFileCtrl::SetWildcard( const wxString
& wildCard
)
1204 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
1206 m_wildCard
= wxString::Format( _( "All files (%s)|%s" ),
1207 wxFileSelectorDefaultWildcardStr
,
1208 wxFileSelectorDefaultWildcardStr
);
1211 m_wildCard
= wildCard
;
1213 wxArrayString wildDescriptions
, wildFilters
;
1214 const size_t count
= wxParseCommonDialogsFilter( m_wildCard
,
1217 wxCHECK_RET( count
, wxT( "wxFileDialog: bad wildcard string" ) );
1221 for ( size_t n
= 0; n
< count
; n
++ )
1223 m_choice
->Append(wildDescriptions
[n
], new wxStringClientData(wildFilters
[n
]));
1226 SetFilterIndex( 0 );
1229 void wxGenericFileCtrl::SetFilterIndex( int filterindex
)
1231 m_choice
->SetSelection( filterindex
);
1233 DoSetFilterIndex( filterindex
);
1236 void wxGenericFileCtrl::OnChoiceFilter( wxCommandEvent
&event
)
1238 DoSetFilterIndex( ( int )event
.GetInt() );
1241 void wxGenericFileCtrl::OnCheck( wxCommandEvent
&event
)
1243 m_list
->ShowHidden( event
.GetInt() != 0 );
1246 void wxGenericFileCtrl::OnActivated( wxListEvent
&event
)
1248 HandleAction( event
.m_item
.m_text
);
1251 void wxGenericFileCtrl::OnTextEnter( wxCommandEvent
&WXUNUSED( event
) )
1253 HandleAction( m_text
->GetValue() );
1256 void wxGenericFileCtrl::OnTextChange( wxCommandEvent
&WXUNUSED( event
) )
1258 if ( !m_ignoreChanges
)
1260 // Clear selections. Otherwise when the user types in a value they may
1261 // not get the file whose name they typed.
1262 if ( m_list
->GetSelectedItemCount() > 0 )
1264 long item
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
,
1265 wxLIST_STATE_SELECTED
);
1266 while ( item
!= -1 )
1268 m_list
->SetItemState( item
, 0, wxLIST_STATE_SELECTED
);
1269 item
= m_list
->GetNextItem( item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1275 void wxGenericFileCtrl::OnSelected( wxListEvent
&event
)
1277 if ( m_ignoreChanges
)
1283 m_inSelected
= true;
1284 const wxString
filename( event
.m_item
.m_text
);
1287 // No double-click on most WinCE devices, so do action immediately.
1288 HandleAction( filename
);
1290 if ( filename
== wxT( ".." ) )
1292 m_inSelected
= false;
1296 wxString dir
= m_list
->GetDir();
1297 if ( !IsTopMostDir( dir
) )
1298 dir
+= wxFILE_SEP_PATH
;
1300 if ( wxDirExists( dir
) )
1302 m_inSelected
= false;
1308 m_ignoreChanges
= true;
1309 m_text
->SetValue( filename
);
1311 if ( m_list
->GetSelectedItemCount() > 1 )
1316 if ( !m_noSelChgEvent
)
1317 GenerateSelectionChangedEvent( this, this );
1319 m_ignoreChanges
= false;
1321 m_inSelected
= false;
1324 void wxGenericFileCtrl::HandleAction( const wxString
&fn
)
1326 if ( m_ignoreChanges
)
1329 wxString
filename( fn
);
1330 if ( filename
.empty() )
1334 if ( filename
== wxT( "." ) ) return;
1336 wxString dir
= m_list
->GetDir();
1338 // "some/place/" means they want to chdir not try to load "place"
1339 const bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1341 filename
= filename
.RemoveLast();
1343 if ( filename
== wxT( ".." ) )
1345 m_ignoreChanges
= true;
1346 m_list
->GoToParentDir();
1348 GenerateFolderChangedEvent( this, this );
1351 m_ignoreChanges
= false;
1356 if ( filename
== wxT( "~" ) )
1358 m_ignoreChanges
= true;
1359 m_list
->GoToHomeDir();
1361 GenerateFolderChangedEvent( this, this );
1364 m_ignoreChanges
= false;
1368 if ( filename
.BeforeFirst( wxT( '/' ) ) == wxT( "~" ) )
1370 filename
= wxString( wxGetUserHome() ) + filename
.Remove( 0, 1 );
1374 if ( !( m_style
& wxFC_SAVE
) )
1376 if ( ( filename
.Find( wxT( '*' ) ) != wxNOT_FOUND
) ||
1377 ( filename
.Find( wxT( '?' ) ) != wxNOT_FOUND
) )
1379 if ( filename
.Find( wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1381 wxMessageBox( _( "Illegal file specification." ),
1382 _( "Error" ), wxOK
| wxICON_ERROR
, this );
1385 m_list
->SetWild( filename
);
1390 if ( !IsTopMostDir( dir
) )
1391 dir
+= wxFILE_SEP_PATH
;
1392 if ( !wxIsAbsolutePath( filename
) )
1398 if ( wxDirExists( filename
) )
1400 m_ignoreChanges
= true;
1401 m_list
->GoToDir( filename
);
1404 GenerateFolderChangedEvent( this, this );
1406 m_ignoreChanges
= false;
1410 // they really wanted a dir, but it doesn't exist
1413 wxMessageBox( _( "Directory doesn't exist." ), _( "Error" ),
1414 wxOK
| wxICON_ERROR
, this );
1418 // append the default extension to the filename if it doesn't have any
1420 // VZ: the logic of testing for !wxFileExists() only for the open file
1421 // dialog is not entirely clear to me, why don't we allow saving to a
1422 // file without extension as well?
1423 if ( !( m_style
& wxFC_OPEN
) || !wxFileExists( filename
) )
1425 filename
= wxFileDialogBase::AppendExtension( filename
, m_filterExtension
);
1426 GenerateFileActivatedEvent( this, this, wxFileName( filename
).GetFullName() );
1430 GenerateFileActivatedEvent( this, this );
1433 bool wxGenericFileCtrl::SetPath( const wxString
& path
)
1435 if ( !wxFileName::FileExists( ( path
) ) )
1439 wxFileName::SplitPath( path
, &m_dir
, &m_fileName
, &ext
);
1442 m_fileName
+= wxT( "." );
1446 SetDirectory( m_dir
);
1447 SetFilename( m_fileName
);
1452 void wxGenericFileCtrl::GetPaths( wxArrayString
& paths
) const
1454 DoGetFilenames( paths
, true );
1457 void wxGenericFileCtrl::GetFilenames( wxArrayString
& files
) const
1459 DoGetFilenames( files
, false );
1462 void wxGenericFileCtrl::UpdateControls()
1464 const wxString dir
= m_list
->GetDir();
1465 m_static
->SetLabel( dir
);
1468 void wxGenericFileCtrl::GoToParentDir()
1470 m_list
->GoToParentDir();
1474 void wxGenericFileCtrl::GoToHomeDir()
1476 m_list
->GoToHomeDir();
1480 #endif // wxUSE_FILECTRL