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( long data1
, long 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(long data1
, long 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(long data1
, long 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(long data1
, long 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 lstat( m_filePath
.fn_str(), &buff
);
227 m_type
|= S_ISLNK(buff
.st_mode
) ? is_link
: 0;
229 wxStat( m_filePath
, &buff
);
232 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
233 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
235 m_size
= buff
.st_size
;
237 m_dateTime
= buff
.st_mtime
;
241 #if defined(__UNIX__)
242 m_permissions
.Printf(wxT("%c%c%c%c%c%c%c%c%c"),
243 buff
.st_mode
& wxS_IRUSR
? wxT('r') : wxT('-'),
244 buff
.st_mode
& wxS_IWUSR
? wxT('w') : wxT('-'),
245 buff
.st_mode
& wxS_IXUSR
? wxT('x') : wxT('-'),
246 buff
.st_mode
& wxS_IRGRP
? wxT('r') : wxT('-'),
247 buff
.st_mode
& wxS_IWGRP
? wxT('w') : wxT('-'),
248 buff
.st_mode
& wxS_IXGRP
? wxT('x') : wxT('-'),
249 buff
.st_mode
& wxS_IROTH
? wxT('r') : wxT('-'),
250 buff
.st_mode
& wxS_IWOTH
? wxT('w') : wxT('-'),
251 buff
.st_mode
& wxS_IXOTH
? wxT('x') : wxT('-'));
252 #elif defined(__WIN32__)
253 DWORD attribs
= ::GetFileAttributes(m_filePath
.c_str());
254 if (attribs
!= (DWORD
)-1)
256 m_permissions
.Printf(wxT("%c%c%c%c"),
257 attribs
& FILE_ATTRIBUTE_ARCHIVE
? wxT('A') : wxT(' '),
258 attribs
& FILE_ATTRIBUTE_READONLY
? wxT('R') : wxT(' '),
259 attribs
& FILE_ATTRIBUTE_HIDDEN
? wxT('H') : wxT(' '),
260 attribs
& FILE_ATTRIBUTE_SYSTEM
? wxT('S') : wxT(' '));
264 // try to get a better icon
265 if (m_image
== wxFileIconsTable::file
)
267 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
269 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
272 m_image
= wxFileIconsTable::executable
;
277 wxString
wxFileData::GetFileType() const
285 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
286 return m_fileName
.AfterLast(wxT('.'));
288 return wxEmptyString
;
291 wxString
wxFileData::GetModificationTime() const
293 // want time as 01:02 so they line up nicely, no %r in WIN32
294 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
297 wxString
wxFileData::GetHint() const
299 wxString s
= m_filePath
;
309 s
+= wxString::Format(wxPLURAL("%ld byte", "%ld bytes", m_size
),
310 wxLongLong(m_size
).ToString().c_str());
316 s
<< GetModificationTime()
324 wxString
wxFileData::GetEntry( fileListFieldType num
) const
334 if (!IsDir() && !IsLink() && !IsDrive())
335 s
= wxLongLong(m_size
).ToString();
344 s
= GetModificationTime();
347 #if defined(__UNIX__) || defined(__WIN32__)
351 #endif // defined(__UNIX__) || defined(__WIN32__)
354 wxFAIL_MSG( wxT("unexpected field in wxFileData::GetEntry()") );
360 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
362 m_fileName
= fileName
;
363 m_filePath
= filePath
;
366 void wxFileData::MakeItem( wxListItem
&item
)
368 item
.m_text
= m_fileName
;
369 item
.ClearAttributes();
371 item
.SetTextColour(*wxRED
);
373 item
.SetTextColour(*wxBLUE
);
375 item
.m_image
= m_image
;
379 wxColour dg
= wxTheColourDatabase
->Find( wxT("MEDIUM GREY") );
381 item
.SetTextColour(dg
);
383 item
.m_data
= wxPtrToUInt(this);
386 //-----------------------------------------------------------------------------
388 //-----------------------------------------------------------------------------
390 IMPLEMENT_DYNAMIC_CLASS(wxFileListCtrl
,wxListCtrl
)
392 BEGIN_EVENT_TABLE(wxFileListCtrl
,wxListCtrl
)
393 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileListCtrl::OnListDeleteItem
)
394 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileListCtrl::OnListDeleteAllItems
)
395 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileListCtrl::OnListEndLabelEdit
)
396 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileListCtrl::OnListColClick
)
400 wxFileListCtrl::wxFileListCtrl()
402 m_showHidden
= false;
403 m_sort_forward
= true;
404 m_sort_field
= wxFileData::FileList_Name
;
407 wxFileListCtrl::wxFileListCtrl(wxWindow
*win
,
409 const wxString
& wild
,
414 const wxValidator
&validator
,
415 const wxString
&name
)
416 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
419 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
421 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
423 m_showHidden
= showHidden
;
425 m_sort_forward
= true;
426 m_sort_field
= wxFileData::FileList_Name
;
428 m_dirName
= wxT("*");
430 if (style
& wxLC_REPORT
)
431 ChangeToReportMode();
434 void wxFileListCtrl::ChangeToListMode()
437 SetSingleStyle( wxLC_LIST
);
441 void wxFileListCtrl::ChangeToReportMode()
444 SetSingleStyle( wxLC_REPORT
);
446 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
447 // don't hardcode since mm/dd is dd/mm elsewhere
449 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
450 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
451 GetTextExtent(txt
, &w
, &h
);
453 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
454 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
455 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
456 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
457 #if defined(__UNIX__)
458 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
459 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
460 #elif defined(__WIN32__)
461 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
462 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
468 void wxFileListCtrl::ChangeToSmallIconMode()
471 SetSingleStyle( wxLC_SMALL_ICON
);
475 void wxFileListCtrl::ShowHidden( bool show
)
481 long wxFileListCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
484 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
485 fd
->MakeItem( item
);
486 long my_style
= GetWindowStyleFlag();
487 if (my_style
& wxLC_REPORT
)
489 ret
= InsertItem( item
);
490 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
491 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
493 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
495 ret
= InsertItem( item
);
500 void wxFileListCtrl::UpdateItem(const wxListItem
&item
)
502 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
503 wxCHECK_RET(fd
, wxT("invalid filedata"));
507 SetItemText(item
, fd
->GetFileName());
508 SetItemImage(item
, fd
->GetImageId());
510 if (GetWindowStyleFlag() & wxLC_REPORT
)
512 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
513 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
517 void wxFileListCtrl::UpdateFiles()
519 // don't do anything before ShowModal() call which sets m_dirName
520 if ( m_dirName
== wxT("*") )
523 wxBusyCursor bcur
; // this may take a while...
531 #if (defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)) && !defined(__WXWINCE__)
532 if ( IsTopMostDir(m_dirName
) )
534 wxArrayString names
, paths
;
536 const size_t count
= wxGetAvailableDrives(paths
, names
, icons
);
538 for ( size_t n
= 0; n
< count
; n
++ )
540 // use paths[n] as the drive name too as our HandleAction() can't
541 // deal with the drive names (of the form "System (C:)") currently
542 // as it mistakenly treats them as file names
544 // it would be preferable to show names, and not paths, in the
545 // dialog just as the native dialog does but for this we must:
546 // a) store the item type as item data and modify HandleAction()
547 // to use it instead of wxDirExists() to check whether the item
549 // b) store the drives by their drive letters and not their
550 // descriptions as otherwise it's pretty confusing to the user
551 wxFileData
*fd
= new wxFileData(paths
[n
], paths
[n
],
552 wxFileData::is_drive
, icons
[n
]);
553 if (Add(fd
, item
) != -1)
560 #endif // defined(__DOS__) || defined(__WINDOWS__)
563 if ( !IsTopMostDir(m_dirName
) && !m_dirName
.empty() )
565 wxString
p(wxPathOnly(m_dirName
));
566 #if (defined(__UNIX__) || defined(__WXWINCE__)) && !defined(__OS2__)
567 if (p
.empty()) p
= wxT("/");
569 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
570 if (Add(fd
, item
) != -1)
576 wxString
dirname(m_dirName
);
577 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
578 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
579 dirname
<< wxT('\\');
580 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
583 dirname
= wxFILE_SEP_PATH
;
588 if ( dir
.IsOpened() )
590 wxString
dirPrefix(dirname
);
591 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
592 dirPrefix
+= wxFILE_SEP_PATH
;
594 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
599 // Get the directories first (not matched against wildcards):
600 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
603 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
604 if (Add(fd
, item
) != -1)
609 cont
= dir
.GetNext(&f
);
612 // Tokenize the wildcard string, so we can handle more than 1
613 // search pattern in a wildcard.
614 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
615 while ( tokenWild
.HasMoreTokens() )
617 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
618 wxDIR_FILES
| hiddenFlag
);
621 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
622 if (Add(fd
, item
) != -1)
627 cont
= dir
.GetNext(&f
);
633 SortItems(m_sort_field
, m_sort_forward
);
636 void wxFileListCtrl::SetWild( const wxString
&wild
)
638 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
645 void wxFileListCtrl::MakeDir()
647 wxString
new_name( _("NewName") );
648 wxString
path( m_dirName
);
649 path
+= wxFILE_SEP_PATH
;
651 if (wxFileExists(path
))
653 // try NewName0, NewName1 etc.
656 new_name
= _("NewName");
658 num
.Printf( wxT("%d"), i
);
662 path
+= wxFILE_SEP_PATH
;
665 } while (wxFileExists(path
));
671 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
676 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
680 long id
= Add( fd
, item
);
684 SortItems(m_sort_field
, m_sort_forward
);
685 id
= FindItem( 0, wxPtrToUInt(fd
) );
693 void wxFileListCtrl::GoToParentDir()
695 if (!IsTopMostDir(m_dirName
))
697 size_t len
= m_dirName
.length();
698 if (wxEndsWithPathSeparator(m_dirName
))
699 m_dirName
.Remove( len
-1, 1 );
700 wxString
fname( wxFileNameFromPath(m_dirName
) );
701 m_dirName
= wxPathOnly( m_dirName
);
702 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
703 if (!m_dirName
.empty())
705 if (m_dirName
.Last() == wxT('.'))
706 m_dirName
= wxEmptyString
;
708 #elif defined(__UNIX__)
709 if (m_dirName
.empty())
710 m_dirName
= wxT("/");
713 long id
= FindItem( 0, fname
);
714 if (id
!= wxNOT_FOUND
)
716 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
722 void wxFileListCtrl::GoToHomeDir()
724 wxString s
= wxGetUserHome( wxString() );
728 void wxFileListCtrl::GoToDir( const wxString
&dir
)
730 if (!wxDirExists(dir
)) return;
735 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
740 void wxFileListCtrl::FreeItemData(wxListItem
& item
)
744 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
751 void wxFileListCtrl::OnListDeleteItem( wxListEvent
&event
)
753 FreeItemData(event
.m_item
);
756 void wxFileListCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
761 void wxFileListCtrl::FreeAllItemsData()
764 item
.m_mask
= wxLIST_MASK_DATA
;
766 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
767 while ( item
.m_itemId
!= -1 )
771 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
775 void wxFileListCtrl::OnListEndLabelEdit( wxListEvent
&event
)
777 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
780 if ((event
.GetLabel().empty()) ||
781 (event
.GetLabel() == wxT(".")) ||
782 (event
.GetLabel() == wxT("..")) ||
783 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
785 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
791 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
792 new_name
+= wxFILE_SEP_PATH
;
793 new_name
+= event
.GetLabel();
797 if (wxFileExists(new_name
))
799 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
804 if (wxRenameFile(fd
->GetFilePath(),new_name
))
806 fd
->SetNewName( new_name
, event
.GetLabel() );
808 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
810 UpdateItem( event
.GetItem() );
811 EnsureVisible( event
.GetItem() );
815 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
821 void wxFileListCtrl::OnListColClick( wxListEvent
&event
)
823 int col
= event
.GetColumn();
827 case wxFileData::FileList_Name
:
828 case wxFileData::FileList_Size
:
829 case wxFileData::FileList_Type
:
830 case wxFileData::FileList_Time
: break;
834 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
835 m_sort_forward
= !m_sort_forward
;
837 m_sort_field
= (wxFileData::fileListFieldType
)col
;
839 SortItems(m_sort_field
, m_sort_forward
);
842 void wxFileListCtrl::SortItems(wxFileData::fileListFieldType field
, bool forward
)
844 m_sort_field
= field
;
845 m_sort_forward
= forward
;
846 const long sort_dir
= forward
? 1 : -1;
848 switch (m_sort_field
)
850 case wxFileData::FileList_Size
:
851 wxListCtrl::SortItems(wxFileDataSizeCompare
, sort_dir
);
854 case wxFileData::FileList_Type
:
855 wxListCtrl::SortItems(wxFileDataTypeCompare
, sort_dir
);
858 case wxFileData::FileList_Time
:
859 wxListCtrl::SortItems(wxFileDataTimeCompare
, sort_dir
);
862 case wxFileData::FileList_Name
:
864 wxListCtrl::SortItems(wxFileDataNameCompare
, sort_dir
);
869 wxFileListCtrl::~wxFileListCtrl()
871 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
872 // wxFileListCtrl::OnListDeleteAllItems. But if the event is generated after
873 // the destruction of the wxFileListCtrl we need to free any data here:
877 #define ID_CHOICE (wxID_FILECTRL + 1)
878 #define ID_TEXT (wxID_FILECTRL + 2)
879 #define ID_FILELIST_CTRL (wxID_FILECTRL + 3)
880 #define ID_CHECK (wxID_FILECTRL + 4)
882 ///////////////////////////////////////////////////////////////////////////////
883 // wxGenericFileCtrl implementation
884 ///////////////////////////////////////////////////////////////////////////////
886 IMPLEMENT_DYNAMIC_CLASS( wxGenericFileCtrl
, wxPanel
)
888 BEGIN_EVENT_TABLE( wxGenericFileCtrl
, wxPanel
)
889 EVT_LIST_ITEM_SELECTED( ID_FILELIST_CTRL
, wxGenericFileCtrl::OnSelected
)
890 EVT_LIST_ITEM_ACTIVATED( ID_FILELIST_CTRL
, wxGenericFileCtrl::OnActivated
)
891 EVT_CHOICE( ID_CHOICE
, wxGenericFileCtrl::OnChoiceFilter
)
892 EVT_TEXT_ENTER( ID_TEXT
, wxGenericFileCtrl::OnTextEnter
)
893 EVT_TEXT( ID_TEXT
, wxGenericFileCtrl::OnTextChange
)
894 EVT_CHECKBOX( ID_CHECK
, wxGenericFileCtrl::OnCheck
)
897 bool wxGenericFileCtrl::Create( wxWindow
*parent
,
899 const wxString
& defaultDirectory
,
900 const wxString
& defaultFileName
,
901 const wxString
& wildCard
,
905 const wxString
& name
)
907 this->m_style
= style
;
908 m_inSelected
= false;
909 m_noSelChgEvent
= false;
912 // check that the styles are not contradictory
913 wxASSERT_MSG( !( ( m_style
& wxFC_SAVE
) && ( m_style
& wxFC_OPEN
) ),
914 wxT( "can't specify both wxFC_SAVE and wxFC_OPEN at once" ) );
916 wxASSERT_MSG( !( ( m_style
& wxFC_SAVE
) && ( m_style
& wxFC_MULTIPLE
) ),
917 wxT( "wxFC_MULTIPLE can't be used with wxFC_SAVE" ) );
919 wxPanel::Create( parent
, id
, pos
, size
, wxTAB_TRAVERSAL
, name
);
921 m_dir
= defaultDirectory
;
923 m_ignoreChanges
= true;
925 if ( ( m_dir
.empty() ) || ( m_dir
== wxT( "." ) ) )
929 m_dir
= wxFILE_SEP_PATH
;
932 const size_t len
= m_dir
.length();
933 if ( ( len
> 1 ) && ( wxEndsWithPathSeparator( m_dir
) ) )
934 m_dir
.Remove( len
- 1, 1 );
936 m_filterExtension
= wxEmptyString
;
940 const bool is_pda
= ( wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
942 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
944 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
946 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _( "Current directory:" ) ),
947 wxSizerFlags().DoubleBorder(wxRIGHT
) );
948 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
949 staticsizer
->Add( m_static
, 1 );
950 mainsizer
->Add( staticsizer
, wxSizerFlags().Expand().Border());
952 long style2
= wxLC_LIST
;
953 if ( !( m_style
& wxFC_MULTIPLE
) )
954 style2
|= wxLC_SINGLE_SEL
;
957 style2
|= wxSIMPLE_BORDER
;
959 style2
|= wxSUNKEN_BORDER
;
962 m_list
= new wxFileListCtrl( this, ID_FILELIST_CTRL
,
963 wxEmptyString
, false,
964 wxDefaultPosition
, wxSize( 400, 140 ),
967 m_text
= new wxTextCtrl( this, ID_TEXT
, wxEmptyString
,
968 wxDefaultPosition
, wxDefaultSize
,
969 wxTE_PROCESS_ENTER
);
970 m_choice
= new wxChoice( this, ID_CHOICE
);
974 // PDAs have a different screen layout
975 mainsizer
->Add( m_list
, wxSizerFlags( 1 ).Expand().HorzBorder() );
977 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
978 textsizer
->Add( m_text
, wxSizerFlags( 1 ).Centre().Border() );
979 textsizer
->Add( m_choice
, wxSizerFlags( 1 ).Centre().Border() );
980 mainsizer
->Add( textsizer
, wxSizerFlags().Expand() );
985 mainsizer
->Add( m_list
, wxSizerFlags( 1 ).Expand().Border() );
986 mainsizer
->Add( m_text
, wxSizerFlags().Expand().Border() );
988 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
989 choicesizer
->Add( m_choice
, wxSizerFlags( 1 ).Centre() );
991 if ( !( m_style
& wxFC_NOSHOWHIDDEN
) )
993 m_check
= new wxCheckBox( this, ID_CHECK
, _( "Show &hidden files" ) );
994 choicesizer
->Add( m_check
, wxSizerFlags().Centre().DoubleBorder(wxLEFT
) );
997 mainsizer
->Add( choicesizer
, wxSizerFlags().Expand().Border() );
1000 SetWildcard( wildCard
);
1002 SetAutoLayout( true );
1003 SetSizer( mainsizer
);
1007 mainsizer
->Fit( this );
1010 m_list
->GoToDir( m_dir
);
1012 m_text
->SetValue( m_fileName
);
1014 m_ignoreChanges
= false;
1016 // must be after m_ignoreChanges = false
1017 SetFilename( defaultFileName
);
1022 // NB: there is an unfortunate mismatch between wxFileName and wxFileDialog
1023 // method names but our GetDirectory() does correspond to wxFileName::
1024 // GetPath() while our GetPath() is wxFileName::GetFullPath()
1025 wxString
wxGenericFileCtrl::GetPath() const
1027 wxASSERT_MSG ( !(m_style
& wxFC_MULTIPLE
), "use GetPaths() instead" );
1029 return DoGetFileName().GetFullPath();
1032 wxString
wxGenericFileCtrl::GetFilename() const
1034 wxASSERT_MSG ( !(m_style
& wxFC_MULTIPLE
), "use GetFilenames() instead" );
1036 return DoGetFileName().GetFullName();
1039 wxString
wxGenericFileCtrl::GetDirectory() const
1041 // don't check for wxFC_MULTIPLE here, this one is probably safe to call in
1042 // any case as it can be always taken to mean "current directory"
1043 return DoGetFileName().GetPath();
1046 wxFileName
wxGenericFileCtrl::DoGetFileName() const
1050 wxString value
= m_text
->GetValue();
1051 if ( value
.empty() )
1053 // nothing in the text control, get the selected file from the list
1055 item
.m_itemId
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1056 wxLIST_STATE_SELECTED
);
1057 m_list
->GetItem(item
);
1059 fn
.Assign(m_list
->GetDir(), item
.m_text
);
1061 else // user entered the value
1063 // the path can be either absolute or relative
1065 if ( fn
.IsRelative() )
1066 fn
.MakeAbsolute(m_list
->GetDir());
1072 // helper used in DoGetFilenames() and needed because Borland can't compile
1073 // operator?: inline
1074 static inline wxString
GetFileNameOrPath(const wxFileName
& fn
, bool fullPath
)
1076 return fullPath
? fn
.GetFullPath() : fn
.GetFullName();
1080 wxGenericFileCtrl::DoGetFilenames(wxArrayString
& filenames
, bool fullPath
) const
1084 const wxString dir
= m_list
->GetDir();
1086 const wxString value
= m_text
->GetValue();
1087 if ( !value
.empty() )
1089 wxFileName
fn(value
);
1090 if ( fn
.IsRelative() )
1091 fn
.MakeAbsolute(dir
);
1093 filenames
.push_back(GetFileNameOrPath(fn
, fullPath
));
1097 const int numSel
= m_list
->GetSelectedItemCount();
1101 filenames
.reserve(numSel
);
1104 item
.m_mask
= wxLIST_MASK_TEXT
;
1108 item
.m_itemId
= m_list
->GetNextItem(item
.m_itemId
, wxLIST_NEXT_ALL
,
1109 wxLIST_STATE_SELECTED
);
1111 if ( item
.m_itemId
== -1 )
1114 m_list
->GetItem(item
);
1116 const wxFileName
fn(dir
, item
.m_text
);
1117 filenames
.push_back(GetFileNameOrPath(fn
, fullPath
));
1121 bool wxGenericFileCtrl::SetDirectory( const wxString
& dir
)
1123 m_ignoreChanges
= true;
1124 m_list
->GoToDir( dir
);
1126 m_ignoreChanges
= false;
1128 return wxFileName( dir
).SameAs( m_list
->GetDir() );
1131 bool wxGenericFileCtrl::SetFilename( const wxString
& name
)
1133 const long item
= m_list
->FindItem( -1, name
);
1135 if ( item
== -1 ) // file not found either because it doesn't exist or the
1136 // current filter doesn't show it.
1139 m_noSelChgEvent
= true;
1141 // Deselect selected items
1143 const int numSelectedItems
= m_list
->GetSelectedItemCount();
1145 if ( numSelectedItems
> 0 )
1147 long itemIndex
= -1;
1151 itemIndex
= m_list
->GetNextItem( itemIndex
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1152 if ( itemIndex
== -1 )
1155 m_list
->SetItemState( itemIndex
, 0, wxLIST_STATE_SELECTED
);
1160 m_list
->SetItemState( item
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
1161 m_list
->EnsureVisible( item
);
1163 m_noSelChgEvent
= false;
1168 void wxGenericFileCtrl::DoSetFilterIndex( int filterindex
)
1170 wxClientData
*pcd
= m_choice
->GetClientObject( filterindex
);
1174 const wxString
& str
= ((static_cast<wxStringClientData
*>(pcd
))->GetData());
1175 m_list
->SetWild( str
);
1176 m_filterIndex
= filterindex
;
1177 if ( str
.Left( 2 ) == wxT( "*." ) )
1179 m_filterExtension
= str
.Mid( 1 );
1180 if ( m_filterExtension
== wxT( ".*" ) )
1181 m_filterExtension
.clear();
1185 m_filterExtension
.clear();
1188 GenerateFilterChangedEvent( this, this );
1191 void wxGenericFileCtrl::SetWildcard( const wxString
& wildCard
)
1193 if ( wildCard
.empty() || wildCard
== wxFileSelectorDefaultWildcardStr
)
1195 m_wildCard
= wxString::Format( _( "All files (%s)|%s" ),
1196 wxFileSelectorDefaultWildcardStr
,
1197 wxFileSelectorDefaultWildcardStr
);
1200 m_wildCard
= wildCard
;
1202 wxArrayString wildDescriptions
, wildFilters
;
1203 const size_t count
= wxParseCommonDialogsFilter( m_wildCard
,
1206 wxCHECK_RET( count
, wxT( "wxFileDialog: bad wildcard string" ) );
1210 for ( size_t n
= 0; n
< count
; n
++ )
1212 m_choice
->Append(wildDescriptions
[n
], new wxStringClientData(wildFilters
[n
]));
1215 SetFilterIndex( 0 );
1218 void wxGenericFileCtrl::SetFilterIndex( int filterindex
)
1220 m_choice
->SetSelection( filterindex
);
1222 DoSetFilterIndex( filterindex
);
1225 void wxGenericFileCtrl::OnChoiceFilter( wxCommandEvent
&event
)
1227 DoSetFilterIndex( ( int )event
.GetInt() );
1230 void wxGenericFileCtrl::OnCheck( wxCommandEvent
&event
)
1232 m_list
->ShowHidden( event
.GetInt() != 0 );
1235 void wxGenericFileCtrl::OnActivated( wxListEvent
&event
)
1237 HandleAction( event
.m_item
.m_text
);
1240 void wxGenericFileCtrl::OnTextEnter( wxCommandEvent
&WXUNUSED( event
) )
1242 HandleAction( m_text
->GetValue() );
1245 void wxGenericFileCtrl::OnTextChange( wxCommandEvent
&WXUNUSED( event
) )
1247 if ( !m_ignoreChanges
)
1249 // Clear selections. Otherwise when the user types in a value they may
1250 // not get the file whose name they typed.
1251 if ( m_list
->GetSelectedItemCount() > 0 )
1253 long item
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
,
1254 wxLIST_STATE_SELECTED
);
1255 while ( item
!= -1 )
1257 m_list
->SetItemState( item
, 0, wxLIST_STATE_SELECTED
);
1258 item
= m_list
->GetNextItem( item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1264 void wxGenericFileCtrl::OnSelected( wxListEvent
&event
)
1266 if ( m_ignoreChanges
)
1272 m_inSelected
= true;
1273 const wxString
filename( event
.m_item
.m_text
);
1276 // No double-click on most WinCE devices, so do action immediately.
1277 HandleAction( filename
);
1279 if ( filename
== wxT( ".." ) )
1281 m_inSelected
= false;
1285 wxString dir
= m_list
->GetDir();
1286 if ( !IsTopMostDir( dir
) )
1287 dir
+= wxFILE_SEP_PATH
;
1289 if ( wxDirExists( dir
) )
1291 m_inSelected
= false;
1297 m_ignoreChanges
= true;
1298 m_text
->SetValue( filename
);
1300 if ( m_list
->GetSelectedItemCount() > 1 )
1305 if ( !m_noSelChgEvent
)
1306 GenerateSelectionChangedEvent( this, this );
1308 m_ignoreChanges
= false;
1310 m_inSelected
= false;
1313 void wxGenericFileCtrl::HandleAction( const wxString
&fn
)
1315 if ( m_ignoreChanges
)
1318 wxString
filename( fn
);
1319 if ( filename
.empty() )
1323 if ( filename
== wxT( "." ) ) return;
1325 wxString dir
= m_list
->GetDir();
1327 // "some/place/" means they want to chdir not try to load "place"
1328 const bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1330 filename
= filename
.RemoveLast();
1332 if ( filename
== wxT( ".." ) )
1334 m_ignoreChanges
= true;
1335 m_list
->GoToParentDir();
1337 GenerateFolderChangedEvent( this, this );
1340 m_ignoreChanges
= false;
1345 if ( filename
== wxT( "~" ) )
1347 m_ignoreChanges
= true;
1348 m_list
->GoToHomeDir();
1350 GenerateFolderChangedEvent( this, this );
1353 m_ignoreChanges
= false;
1357 if ( filename
.BeforeFirst( wxT( '/' ) ) == wxT( "~" ) )
1359 filename
= wxString( wxGetUserHome() ) + filename
.Remove( 0, 1 );
1363 if ( !( m_style
& wxFC_SAVE
) )
1365 if ( ( filename
.Find( wxT( '*' ) ) != wxNOT_FOUND
) ||
1366 ( filename
.Find( wxT( '?' ) ) != wxNOT_FOUND
) )
1368 if ( filename
.Find( wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1370 wxMessageBox( _( "Illegal file specification." ),
1371 _( "Error" ), wxOK
| wxICON_ERROR
, this );
1374 m_list
->SetWild( filename
);
1379 if ( !IsTopMostDir( dir
) )
1380 dir
+= wxFILE_SEP_PATH
;
1381 if ( !wxIsAbsolutePath( filename
) )
1387 if ( wxDirExists( filename
) )
1389 m_ignoreChanges
= true;
1390 m_list
->GoToDir( filename
);
1393 GenerateFolderChangedEvent( this, this );
1395 m_ignoreChanges
= false;
1399 // they really wanted a dir, but it doesn't exist
1402 wxMessageBox( _( "Directory doesn't exist." ), _( "Error" ),
1403 wxOK
| wxICON_ERROR
, this );
1407 // append the default extension to the filename if it doesn't have any
1409 // VZ: the logic of testing for !wxFileExists() only for the open file
1410 // dialog is not entirely clear to me, why don't we allow saving to a
1411 // file without extension as well?
1412 if ( !( m_style
& wxFC_OPEN
) || !wxFileExists( filename
) )
1414 filename
= wxFileDialogBase::AppendExtension( filename
, m_filterExtension
);
1415 GenerateFileActivatedEvent( this, this, wxFileName( filename
).GetFullName() );
1419 GenerateFileActivatedEvent( this, this );
1422 bool wxGenericFileCtrl::SetPath( const wxString
& path
)
1424 if ( !wxFileName::FileExists( ( path
) ) )
1428 wxFileName::SplitPath( path
, &m_dir
, &m_fileName
, &ext
);
1431 m_fileName
+= wxT( "." );
1435 SetDirectory( m_dir
);
1436 SetFilename( m_fileName
);
1441 void wxGenericFileCtrl::GetPaths( wxArrayString
& paths
) const
1443 DoGetFilenames( paths
, true );
1446 void wxGenericFileCtrl::GetFilenames( wxArrayString
& files
) const
1448 DoGetFilenames( files
, false );
1451 void wxGenericFileCtrl::UpdateControls()
1453 const wxString dir
= m_list
->GetDir();
1454 m_static
->SetLabel( dir
);
1457 void wxGenericFileCtrl::GoToParentDir()
1459 m_list
->GoToParentDir();
1463 void wxGenericFileCtrl::GoToHomeDir()
1465 m_list
->GoToHomeDir();
1469 #endif // wxUSE_FILECTRL