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/filename.h"
32 #include "wx/clntdata.h"
33 #include "wx/file.h" // for wxS_IXXX constants only
34 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
36 #include "wx/tokenzr.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( long data1
, long data2
, long 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(long data1
, long data2
, long 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(long data1
, long data2
, long 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(long data1
, long data2
, long 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 wxSplitPath(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 lstat( m_filePath
.fn_str(), &buff
);
227 m_type
|= S_ISLNK(buff
.st_mode
) ? is_link
: 0;
229 // only translate to file charset if we don't go by our
230 // wxStat implementation
231 #ifndef wxNEED_WX_UNISTD_H
232 wxStat( m_filePath
.fn_str() , &buff
);
234 wxStat( m_filePath
, &buff
);
238 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
239 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
241 m_size
= buff
.st_size
;
243 m_dateTime
= buff
.st_mtime
;
247 #if defined(__UNIX__)
248 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
249 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
250 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
251 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
252 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
253 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
254 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
255 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
256 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
257 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
258 #elif defined(__WIN32__)
259 DWORD attribs
= ::GetFileAttributes(m_filePath
.c_str());
260 if (attribs
!= (DWORD
)-1)
262 m_permissions
.Printf(_T("%c%c%c%c"),
263 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
264 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
265 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
266 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
270 // try to get a better icon
271 if (m_image
== wxFileIconsTable::file
)
273 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
275 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
278 m_image
= wxFileIconsTable::executable
;
283 wxString
wxFileData::GetFileType() const
291 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
292 return m_fileName
.AfterLast(wxT('.'));
294 return wxEmptyString
;
297 wxString
wxFileData::GetModificationTime() const
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"));
303 wxString
wxFileData::GetHint() const
305 wxString s
= m_filePath
;
315 s
+= wxString::Format(wxPLURAL("%ld byte", "%ld bytes", m_size
),
316 wxLongLong(m_size
).ToString().c_str());
322 s
<< GetModificationTime()
330 wxString
wxFileData::GetEntry( fileListFieldType num
) const
340 if (!IsDir() && !IsLink() && !IsDrive())
341 s
= wxLongLong(m_size
).ToString();
350 s
= GetModificationTime();
353 #if defined(__UNIX__) || defined(__WIN32__)
357 #endif // defined(__UNIX__) || defined(__WIN32__)
360 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
366 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
368 m_fileName
= fileName
;
369 m_filePath
= filePath
;
372 void wxFileData::MakeItem( wxListItem
&item
)
374 item
.m_text
= m_fileName
;
375 item
.ClearAttributes();
377 item
.SetTextColour(*wxRED
);
379 item
.SetTextColour(*wxBLUE
);
381 item
.m_image
= m_image
;
385 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
387 item
.SetTextColour(dg
);
389 item
.m_data
= wxPtrToUInt(this);
392 //-----------------------------------------------------------------------------
394 //-----------------------------------------------------------------------------
396 // FIXME: what is this for? It's never read
397 static bool ignoreChanges
= false;
399 IMPLEMENT_DYNAMIC_CLASS(wxFileListCtrl
,wxListCtrl
)
401 BEGIN_EVENT_TABLE(wxFileListCtrl
,wxListCtrl
)
402 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileListCtrl::OnListDeleteItem
)
403 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileListCtrl::OnListDeleteAllItems
)
404 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileListCtrl::OnListEndLabelEdit
)
405 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileListCtrl::OnListColClick
)
409 wxFileListCtrl::wxFileListCtrl()
411 m_showHidden
= false;
412 m_sort_forward
= true;
413 m_sort_field
= wxFileData::FileList_Name
;
416 wxFileListCtrl::wxFileListCtrl(wxWindow
*win
,
418 const wxString
& wild
,
423 const wxValidator
&validator
,
424 const wxString
&name
)
425 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
428 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
430 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
432 m_showHidden
= showHidden
;
434 m_sort_forward
= true;
435 m_sort_field
= wxFileData::FileList_Name
;
437 m_dirName
= wxT("*");
439 if (style
& wxLC_REPORT
)
440 ChangeToReportMode();
443 void wxFileListCtrl::ChangeToListMode()
446 SetSingleStyle( wxLC_LIST
);
450 void wxFileListCtrl::ChangeToReportMode()
453 SetSingleStyle( wxLC_REPORT
);
455 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
456 // don't hardcode since mm/dd is dd/mm elsewhere
458 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
459 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
460 GetTextExtent(txt
, &w
, &h
);
462 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
463 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
464 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
465 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
466 #if defined(__UNIX__)
467 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
468 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
469 #elif defined(__WIN32__)
470 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
471 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
477 void wxFileListCtrl::ChangeToSmallIconMode()
480 SetSingleStyle( wxLC_SMALL_ICON
);
484 void wxFileListCtrl::ShowHidden( bool show
)
490 long wxFileListCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
493 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
494 fd
->MakeItem( item
);
495 long my_style
= GetWindowStyleFlag();
496 if (my_style
& wxLC_REPORT
)
498 ret
= InsertItem( item
);
499 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
500 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
502 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
504 ret
= InsertItem( item
);
509 void wxFileListCtrl::UpdateItem(const wxListItem
&item
)
511 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
512 wxCHECK_RET(fd
, wxT("invalid filedata"));
516 SetItemText(item
, fd
->GetFileName());
517 SetItemImage(item
, fd
->GetImageId());
519 if (GetWindowStyleFlag() & wxLC_REPORT
)
521 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
522 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
526 void wxFileListCtrl::UpdateFiles()
528 // don't do anything before ShowModal() call which sets m_dirName
529 if ( m_dirName
== wxT("*") )
532 wxBusyCursor bcur
; // this may take a while...
540 #if (defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)) && !defined(__WXWINCE__)
541 if ( IsTopMostDir(m_dirName
) )
543 wxArrayString names
, paths
;
545 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
547 for (n
=0; n
<count
; n
++)
549 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
550 if (Add(fd
, item
) != -1)
557 #endif // defined(__DOS__) || defined(__WINDOWS__)
560 if ( !IsTopMostDir(m_dirName
) && !m_dirName
.empty() )
562 wxString
p(wxPathOnly(m_dirName
));
563 #if (defined(__UNIX__) || defined(__WXWINCE__)) && !defined(__OS2__)
564 if (p
.empty()) p
= wxT("/");
566 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
567 if (Add(fd
, item
) != -1)
573 wxString
dirname(m_dirName
);
574 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
575 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
576 dirname
<< wxT('\\');
577 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
580 dirname
= wxFILE_SEP_PATH
;
585 if ( dir
.IsOpened() )
587 wxString
dirPrefix(dirname
);
588 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
589 dirPrefix
+= wxFILE_SEP_PATH
;
591 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
596 // Get the directories first (not matched against wildcards):
597 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
600 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
601 if (Add(fd
, item
) != -1)
606 cont
= dir
.GetNext(&f
);
609 // Tokenize the wildcard string, so we can handle more than 1
610 // search pattern in a wildcard.
611 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
612 while ( tokenWild
.HasMoreTokens() )
614 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
615 wxDIR_FILES
| hiddenFlag
);
618 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
619 if (Add(fd
, item
) != -1)
624 cont
= dir
.GetNext(&f
);
630 SortItems(m_sort_field
, m_sort_forward
);
633 void wxFileListCtrl::SetWild( const wxString
&wild
)
635 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
642 void wxFileListCtrl::MakeDir()
644 wxString
new_name( _("NewName") );
645 wxString
path( m_dirName
);
646 path
+= wxFILE_SEP_PATH
;
648 if (wxFileExists(path
))
650 // try NewName0, NewName1 etc.
653 new_name
= _("NewName");
655 num
.Printf( wxT("%d"), i
);
659 path
+= wxFILE_SEP_PATH
;
662 } while (wxFileExists(path
));
668 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
673 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
677 long id
= Add( fd
, item
);
681 SortItems(m_sort_field
, m_sort_forward
);
682 id
= FindItem( 0, wxPtrToUInt(fd
) );
690 void wxFileListCtrl::GoToParentDir()
692 if (!IsTopMostDir(m_dirName
))
694 size_t len
= m_dirName
.length();
695 if (wxEndsWithPathSeparator(m_dirName
))
696 m_dirName
.Remove( len
-1, 1 );
697 wxString
fname( wxFileNameFromPath(m_dirName
) );
698 m_dirName
= wxPathOnly( m_dirName
);
699 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
700 if (!m_dirName
.empty())
702 if (m_dirName
.Last() == wxT('.'))
703 m_dirName
= wxEmptyString
;
705 #elif defined(__UNIX__)
706 if (m_dirName
.empty())
707 m_dirName
= wxT("/");
710 long id
= FindItem( 0, fname
);
711 if (id
!= wxNOT_FOUND
)
713 ignoreChanges
= true;
714 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
716 ignoreChanges
= false;
721 void wxFileListCtrl::GoToHomeDir()
723 wxString s
= wxGetUserHome( wxString() );
727 void wxFileListCtrl::GoToDir( const wxString
&dir
)
729 if (!wxDirExists(dir
)) return;
734 ignoreChanges
= true;
735 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
736 ignoreChanges
= false;
741 void wxFileListCtrl::FreeItemData(wxListItem
& item
)
745 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
752 void wxFileListCtrl::OnListDeleteItem( wxListEvent
&event
)
754 FreeItemData(event
.m_item
);
757 void wxFileListCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
762 void wxFileListCtrl::FreeAllItemsData()
765 item
.m_mask
= wxLIST_MASK_DATA
;
767 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
768 while ( item
.m_itemId
!= -1 )
772 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
776 void wxFileListCtrl::OnListEndLabelEdit( wxListEvent
&event
)
778 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
781 if ((event
.GetLabel().empty()) ||
782 (event
.GetLabel() == wxT(".")) ||
783 (event
.GetLabel() == wxT("..")) ||
784 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
786 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
792 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
793 new_name
+= wxFILE_SEP_PATH
;
794 new_name
+= event
.GetLabel();
798 if (wxFileExists(new_name
))
800 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
805 if (wxRenameFile(fd
->GetFilePath(),new_name
))
807 fd
->SetNewName( new_name
, event
.GetLabel() );
809 ignoreChanges
= true;
810 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
811 ignoreChanges
= false;
813 UpdateItem( event
.GetItem() );
814 EnsureVisible( event
.GetItem() );
818 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
824 void wxFileListCtrl::OnListColClick( wxListEvent
&event
)
826 int col
= event
.GetColumn();
830 case wxFileData::FileList_Name
:
831 case wxFileData::FileList_Size
:
832 case wxFileData::FileList_Type
:
833 case wxFileData::FileList_Time
: break;
837 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
838 m_sort_forward
= !m_sort_forward
;
840 m_sort_field
= (wxFileData::fileListFieldType
)col
;
842 SortItems(m_sort_field
, m_sort_forward
);
845 void wxFileListCtrl::SortItems(wxFileData::fileListFieldType field
, bool forward
)
847 m_sort_field
= field
;
848 m_sort_forward
= forward
;
849 const long sort_dir
= forward
? 1 : -1;
851 switch (m_sort_field
)
853 case wxFileData::FileList_Size
:
854 wxListCtrl::SortItems(wxFileDataSizeCompare
, sort_dir
);
857 case wxFileData::FileList_Type
:
858 wxListCtrl::SortItems(wxFileDataTypeCompare
, sort_dir
);
861 case wxFileData::FileList_Time
:
862 wxListCtrl::SortItems(wxFileDataTimeCompare
, sort_dir
);
865 case wxFileData::FileList_Name
:
867 wxListCtrl::SortItems(wxFileDataNameCompare
, sort_dir
);
872 wxFileListCtrl::~wxFileListCtrl()
874 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
875 // wxFileListCtrl::OnListDeleteAllItems. But if the event is generated after
876 // the destruction of the wxFileListCtrl we need to free any data here:
880 #define ID_CHOICE (wxID_FILECTRL + 1)
881 #define ID_TEXT (wxID_FILECTRL + 2)
882 #define ID_FILELIST_CTRL (wxID_FILECTRL + 3)
883 #define ID_CHECK (wxID_FILECTRL + 4)
885 ///////////////////////////////////////////////////////////////////////////////
886 // wxGenericFileCtrl implementation
887 ///////////////////////////////////////////////////////////////////////////////
889 IMPLEMENT_DYNAMIC_CLASS( wxGenericFileCtrl
, wxPanel
)
891 BEGIN_EVENT_TABLE( wxGenericFileCtrl
, wxPanel
)
892 EVT_LIST_ITEM_SELECTED( ID_FILELIST_CTRL
, wxGenericFileCtrl::OnSelected
)
893 EVT_LIST_ITEM_ACTIVATED( ID_FILELIST_CTRL
, wxGenericFileCtrl::OnActivated
)
894 EVT_CHOICE( ID_CHOICE
, wxGenericFileCtrl::OnChoiceFilter
)
895 EVT_TEXT_ENTER( ID_TEXT
, wxGenericFileCtrl::OnTextEnter
)
896 EVT_TEXT( ID_TEXT
, wxGenericFileCtrl::OnTextChange
)
897 EVT_CHECKBOX( ID_CHECK
, wxGenericFileCtrl::OnCheck
)
900 bool wxGenericFileCtrl::Create( wxWindow
*parent
,
902 const wxString
& defaultDirectory
,
903 const wxString
& defaultFileName
,
904 const wxString
& wildCard
,
908 const wxString
& name
)
910 this->m_style
= style
;
911 m_inSelected
= false;
912 m_noSelChgEvent
= false;
914 // check that the styles are not contradictory
915 wxASSERT_MSG( !( ( m_style
& wxFC_SAVE
) && ( m_style
& wxFC_OPEN
) ),
916 wxT( "can't specify both wxFC_SAVE and wxFC_OPEN at once" ) );
918 wxASSERT_MSG( !( ( m_style
& wxFC_SAVE
) && ( m_style
& wxFC_MULTIPLE
) ),
919 wxT( "wxFC_MULTIPLE can't be used with wxFC_SAVE" ) );
921 wxPanel::Create( parent
, id
, pos
, size
, wxTAB_TRAVERSAL
, name
);
923 m_dir
= defaultDirectory
;
925 m_ignoreChanges
= true;
927 if ( ( m_dir
.empty() ) || ( m_dir
== wxT( "." ) ) )
931 m_dir
= wxFILE_SEP_PATH
;
934 const size_t len
= m_dir
.length();
935 if ( ( len
> 1 ) && ( wxEndsWithPathSeparator( m_dir
) ) )
936 m_dir
.Remove( len
- 1, 1 );
938 m_filterExtension
= wxEmptyString
;
942 const bool is_pda
= ( wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
944 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
946 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
948 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _( "Current directory:" ) ), 0, wxRIGHT
, 10 );
949 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
950 staticsizer
->Add( m_static
, 1 );
951 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
| wxRIGHT
| wxBOTTOM
, 10 );
953 long style2
= wxLC_LIST
;
954 if ( !( m_style
& wxFC_MULTIPLE
) )
955 style2
|= wxLC_SINGLE_SEL
;
958 style2
|= wxSIMPLE_BORDER
;
960 style2
|= wxSUNKEN_BORDER
;
963 m_list
= new wxFileListCtrl( this, ID_FILELIST_CTRL
,
964 wxEmptyString
, false,
965 wxDefaultPosition
, wxSize( 400, 140 ),
968 m_text
= new wxTextCtrl( this, ID_TEXT
, wxEmptyString
,
969 wxDefaultPosition
, wxDefaultSize
,
970 wxTE_PROCESS_ENTER
);
971 m_choice
= new wxChoice( this, ID_CHOICE
);
975 // PDAs have a different screen layout
976 mainsizer
->Add( m_list
, wxSizerFlags( 1 ).Expand().HorzBorder() );
978 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
979 textsizer
->Add( m_text
, wxSizerFlags( 1 ).Centre().Border() );
980 mainsizer
->Add( textsizer
, wxSizerFlags().Expand() );
983 textsizer
->Add( m_choice
, wxSizerFlags( 1 ).Centre().Border() );
987 mainsizer
->Add( m_list
, wxSizerFlags( 1 ).Expand().DoubleHorzBorder() );
989 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
990 textsizer
->Add( m_text
, wxSizerFlags( 1 ).Centre().
991 DoubleBorder( wxLEFT
| wxRIGHT
| wxTOP
) );
992 mainsizer
->Add( textsizer
, wxSizerFlags().Expand() );
994 wxSizerFlags flagsCentre
;
995 flagsCentre
.Centre().DoubleBorder();
997 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
998 choicesizer
->Add( m_choice
, wxSizerFlags( flagsCentre
).Proportion( 1 ) );
1000 if ( !( m_style
& wxFC_NOSHOWHIDDEN
) )
1002 m_check
= new wxCheckBox( this, ID_CHECK
, _( "Show &hidden files" ) );
1003 choicesizer
->Add( m_check
, flagsCentre
);
1006 mainsizer
->Add( choicesizer
, wxSizerFlags().Expand() );
1009 SetWildcard( wildCard
);
1011 SetAutoLayout( true );
1012 SetSizer( mainsizer
);
1016 mainsizer
->Fit( this );
1019 m_list
->GoToDir( m_dir
);
1021 m_text
->SetValue( m_fileName
);
1023 m_ignoreChanges
= false;
1025 // must be after m_ignoreChanges = false
1026 SetFilename( defaultFileName
);
1031 wxString
wxGenericFileCtrl::GetPath() const
1033 return DoGetFilename( true );
1036 wxString
wxGenericFileCtrl::GetFilename() const
1038 return DoGetFilename( false );
1041 wxString
wxGenericFileCtrl::DoGetFilename( const bool fullPath
) const
1043 wxASSERT_MSG( ( m_style
& wxFC_MULTIPLE
) == 0,
1044 wxT( "With controls that has wxFC_MULTIPLE style " )
1045 wxT( "use GetFilenames/GetPaths to get all filenames/paths selected" ) );
1047 const wxString value
= m_text
->GetValue();
1049 if ( !value
.empty() )
1051 return fullPath
? ( GetProperFileListDir() + value
) : value
;
1054 void wxGenericFileCtrl::DoGetFilenames( wxArrayString
& filenames
, const bool fullPath
) const
1058 const wxString dir
= GetProperFileListDir();
1059 const wxString value
= m_text
->GetValue();
1061 if ( !value
.empty() )
1064 filenames
.Add( dir
+ value
);
1066 filenames
.Add( value
);
1070 if ( m_list
->GetSelectedItemCount() == 0 )
1075 filenames
.Alloc( m_list
->GetSelectedItemCount() );
1078 item
.m_mask
= wxLIST_MASK_TEXT
;
1080 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1081 while ( item
.m_itemId
!= -1 )
1083 m_list
->GetItem( item
);
1086 filenames
.Add( dir
+ item
.m_text
);
1088 filenames
.Add( item
.m_text
);
1090 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1091 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1095 bool wxGenericFileCtrl::SetDirectory( const wxString
& dir
)
1097 m_ignoreChanges
= true;
1098 m_list
->GoToDir( dir
);
1100 m_ignoreChanges
= false;
1102 return wxFileName( dir
).SameAs( m_list
->GetDir() );
1105 wxString
wxGenericFileCtrl::GetDirectory() const
1107 return m_list
->GetDir();
1110 bool wxGenericFileCtrl::SetFilename( const wxString
& name
)
1112 const long item
= m_list
->FindItem( -1, name
);
1114 if ( item
== -1 ) // file not found either because it doesn't exist or the
1115 // current filter doesn't show it.
1118 m_noSelChgEvent
= true;
1120 // Deselect selected items
1122 const int numSelectedItems
= m_list
->GetSelectedItemCount();
1124 if ( numSelectedItems
> 0 )
1126 long itemIndex
= -1;
1130 itemIndex
= m_list
->GetNextItem( itemIndex
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1131 if ( itemIndex
== -1 )
1134 m_list
->SetItemState( itemIndex
, 0, wxLIST_STATE_SELECTED
);
1139 m_list
->SetItemState( item
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
1140 m_list
->EnsureVisible( item
);
1142 m_noSelChgEvent
= false;
1147 void wxGenericFileCtrl::DoSetFilterIndex( int filterindex
)
1149 const wxString
& str
= (wx_static_cast(wxStringClientData
*,
1150 m_choice
->GetClientObject( filterindex
)))
1152 m_list
->SetWild( str
);
1153 m_filterIndex
= filterindex
;
1154 if ( str
.Left( 2 ) == wxT( "*." ) )
1156 m_filterExtension
= str
.Mid( 1 );
1157 if ( m_filterExtension
== _T( ".*" ) )
1158 m_filterExtension
.clear();
1162 m_filterExtension
.clear();
1166 void wxGenericFileCtrl::SetWildcard( const wxString
& wildCard
)
1168 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
1170 m_wildCard
= wxString::Format( _( "All files (%s)|%s" ),
1171 wxFileSelectorDefaultWildcardStr
,
1172 wxFileSelectorDefaultWildcardStr
);
1175 m_wildCard
= wildCard
;
1177 wxArrayString wildDescriptions
, wildFilters
;
1178 const size_t count
= wxParseCommonDialogsFilter( m_wildCard
,
1181 wxCHECK_RET( count
, wxT( "wxFileDialog: bad wildcard string" ) );
1185 for ( size_t n
= 0; n
< count
; n
++ )
1187 m_choice
->Append(wildDescriptions
[n
], new wxStringClientData(wildFilters
[n
]));
1190 SetFilterIndex( 0 );
1193 void wxGenericFileCtrl::SetFilterIndex( int filterindex
)
1195 m_choice
->SetSelection( filterindex
);
1197 DoSetFilterIndex( filterindex
);
1200 void wxGenericFileCtrl::OnChoiceFilter( wxCommandEvent
&event
)
1202 DoSetFilterIndex( ( int )event
.GetInt() );
1205 void wxGenericFileCtrl::OnCheck( wxCommandEvent
&event
)
1207 m_list
->ShowHidden( event
.GetInt() != 0 );
1210 void wxGenericFileCtrl::OnActivated( wxListEvent
&event
)
1212 HandleAction( event
.m_item
.m_text
);
1215 void wxGenericFileCtrl::OnTextEnter( wxCommandEvent
&WXUNUSED( event
) )
1217 HandleAction( m_text
->GetValue() );
1220 void wxGenericFileCtrl::OnTextChange( wxCommandEvent
&WXUNUSED( event
) )
1222 if ( !m_ignoreChanges
)
1224 // Clear selections. Otherwise when the user types in a value they may
1225 // not get the file whose name they typed.
1226 if ( m_list
->GetSelectedItemCount() > 0 )
1228 long item
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
,
1229 wxLIST_STATE_SELECTED
);
1230 while ( item
!= -1 )
1232 m_list
->SetItemState( item
, 0, wxLIST_STATE_SELECTED
);
1233 item
= m_list
->GetNextItem( item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1239 void wxGenericFileCtrl::OnSelected( wxListEvent
&event
)
1241 if ( m_ignoreChanges
)
1247 m_inSelected
= true;
1248 const wxString
filename( event
.m_item
.m_text
);
1251 // No double-click on most WinCE devices, so do action immediately.
1252 HandleAction( filename
);
1254 if ( filename
== wxT( ".." ) )
1256 m_inSelected
= false;
1260 wxString dir
= m_list
->GetDir();
1261 if ( !IsTopMostDir( dir
) )
1262 dir
+= wxFILE_SEP_PATH
;
1264 if ( wxDirExists( dir
) )
1266 m_inSelected
= false;
1272 m_ignoreChanges
= true;
1273 m_text
->SetValue( filename
);
1275 if ( m_list
->GetSelectedItemCount() > 1 )
1280 if ( !m_noSelChgEvent
)
1281 GenerateSelectionChangedEvent( this, this );
1283 m_ignoreChanges
= false;
1285 m_inSelected
= false;
1288 void wxGenericFileCtrl::HandleAction( const wxString
&fn
)
1290 if ( m_ignoreChanges
)
1293 wxString
filename( fn
);
1294 if ( filename
.empty() )
1298 if ( filename
== wxT( "." ) ) return;
1300 wxString dir
= m_list
->GetDir();
1302 // "some/place/" means they want to chdir not try to load "place"
1303 const bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1305 filename
= filename
.RemoveLast();
1307 if ( filename
== wxT( ".." ) )
1309 m_ignoreChanges
= true;
1310 m_list
->GoToParentDir();
1312 GenerateFolderChangedEvent( this, this );
1315 m_ignoreChanges
= false;
1320 if ( filename
== wxT( "~" ) )
1322 m_ignoreChanges
= true;
1323 m_list
->GoToHomeDir();
1325 GenerateFolderChangedEvent( this, this );
1328 m_ignoreChanges
= false;
1332 if ( filename
.BeforeFirst( wxT( '/' ) ) == wxT( "~" ) )
1334 filename
= wxString( wxGetUserHome() ) + filename
.Remove( 0, 1 );
1338 if ( !( m_style
& wxFC_SAVE
) )
1340 if ( ( filename
.Find( wxT( '*' ) ) != wxNOT_FOUND
) ||
1341 ( filename
.Find( wxT( '?' ) ) != wxNOT_FOUND
) )
1343 if ( filename
.Find( wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1345 wxMessageBox( _( "Illegal file specification." ),
1346 _( "Error" ), wxOK
| wxICON_ERROR
, this );
1349 m_list
->SetWild( filename
);
1354 if ( !IsTopMostDir( dir
) )
1355 dir
+= wxFILE_SEP_PATH
;
1356 if ( !wxIsAbsolutePath( filename
) )
1362 if ( wxDirExists( filename
) )
1364 m_ignoreChanges
= true;
1365 m_list
->GoToDir( filename
);
1368 GenerateFolderChangedEvent( this, this );
1370 m_ignoreChanges
= false;
1374 // they really wanted a dir, but it doesn't exist
1377 wxMessageBox( _( "Directory doesn't exist." ), _( "Error" ),
1378 wxOK
| wxICON_ERROR
, this );
1382 // append the default extension to the filename if it doesn't have any
1384 // VZ: the logic of testing for !wxFileExists() only for the open file
1385 // dialog is not entirely clear to me, why don't we allow saving to a
1386 // file without extension as well?
1387 if ( !( m_style
& wxFC_OPEN
) || !wxFileExists( filename
) )
1389 filename
= wxFileDialogBase::AppendExtension( filename
, m_filterExtension
);
1390 GenerateFileActivatedEvent( this, this, wxFileName( filename
).GetFullName() );
1394 GenerateFileActivatedEvent( this, this );
1397 bool wxGenericFileCtrl::SetPath( const wxString
& path
)
1399 if ( !wxFileName::FileExists( ( path
) ) )
1403 wxSplitPath( path
, &m_dir
, &m_fileName
, &ext
);
1406 m_fileName
+= wxT( "." );
1410 SetDirectory( m_dir
);
1411 SetFilename( m_fileName
);
1416 void wxGenericFileCtrl::GetPaths( wxArrayString
& paths
) const
1418 DoGetFilenames( paths
, true );
1421 void wxGenericFileCtrl::GetFilenames( wxArrayString
& files
) const
1423 DoGetFilenames( files
, false );
1426 void wxGenericFileCtrl::UpdateControls()
1428 const wxString dir
= m_list
->GetDir();
1429 m_static
->SetLabel( dir
);
1432 void wxGenericFileCtrl::GoToParentDir()
1434 m_list
->GoToParentDir();
1438 void wxGenericFileCtrl::GoToHomeDir()
1440 m_list
->GoToHomeDir();
1444 wxString
wxGenericFileCtrl::GetProperFileListDir() const
1446 wxString dir
= m_list
->GetDir();
1448 if ( dir
!= wxT( "/" ) )
1449 #elif defined(__WXWINCE__)
1450 if ( dir
!= wxT( "/" ) && dir
!= wxT( "\\" ) )
1452 dir
+= wxFILE_SEP_PATH
;
1457 #endif // wxUSE_FILECTRL