-}
-
-wxString wxFileData::GetFileType() const
-{
- if (IsDir())
- return _("<DIR>");
- else if (IsLink())
- return _("<LINK>");
- else if (IsDrive())
- return _("<DRIVE>");
- else if (m_fileName.Find(wxT('.'), true) != wxNOT_FOUND)
- return m_fileName.AfterLast(wxT('.'));
-
- return wxEmptyString;
-}
-
-wxString wxFileData::GetModificationTime() const
-{
- // want time as 01:02 so they line up nicely, no %r in WIN32
- return m_dateTime.FormatDate() + wxT(" ") + m_dateTime.Format(wxT("%I:%M:%S %p"));
-}
-
-wxString wxFileData::GetHint() const
-{
- wxString s = m_filePath;
- s += wxT(" ");
-
- if (IsDir())
- s += _("<DIR>");
- else if (IsLink())
- s += _("<LINK>");
- else if (IsDrive())
- s += _("<DRIVE>");
- else // plain file
- s += wxString::Format( _("%ld bytes"), m_size );
-
- s += wxT(' ');
-
- if ( !IsDrive() )
- {
- s << GetModificationTime()
- << wxT(" ")
- << m_permissions;
- }
-
- return s;
-};
-
-wxString wxFileData::GetEntry( fileListFieldType num ) const
-{
- wxString s;
- switch ( num )
- {
- case FileList_Name:
- s = m_fileName;
- break;
-
- case FileList_Size:
- if (!IsDir() && !IsLink() && !IsDrive())
- s.Printf(_T("%ld"), m_size);
- break;
-
- case FileList_Type:
- s = GetFileType();
- break;
-
- case FileList_Time:
- if (!IsDrive())
- s = GetModificationTime();
- break;
-
-#if defined(__UNIX__) || defined(__WIN32__)
- case FileList_Perm:
- s = m_permissions;
- break;
-#endif // defined(__UNIX__) || defined(__WIN32__)
-
- default:
- wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
- }
-
- return s;
-}
-
-void wxFileData::SetNewName( const wxString &filePath, const wxString &fileName )
-{
- m_fileName = fileName;
- m_filePath = filePath;
-}
-
-void wxFileData::MakeItem( wxListItem &item )
-{
- item.m_text = m_fileName;
- item.ClearAttributes();
- if (IsExe())
- item.SetTextColour(*wxRED);
- if (IsDir())
- item.SetTextColour(*wxBLUE);
-
- item.m_image = m_image;
-
- if (IsLink())
- {
- wxColour dg = wxTheColourDatabase->Find( _T("MEDIUM GREY") );
- if ( dg.Ok() )
- item.SetTextColour(dg);
- }
- item.m_data = (long)this;
-}
-
-//-----------------------------------------------------------------------------
-// wxFileCtrl
-//-----------------------------------------------------------------------------
-
-IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl)
-
-BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl)
- EVT_LIST_DELETE_ITEM(wxID_ANY, wxFileCtrl::OnListDeleteItem)
- EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY, wxFileCtrl::OnListDeleteAllItems)
- EVT_LIST_END_LABEL_EDIT(wxID_ANY, wxFileCtrl::OnListEndLabelEdit)
- EVT_LIST_COL_CLICK(wxID_ANY, wxFileCtrl::OnListColClick)
-END_EVENT_TABLE()
-
-
-wxFileCtrl::wxFileCtrl()
-{
- m_showHidden = false;
- m_sort_foward = 1;
- m_sort_field = wxFileData::FileList_Name;
-}
-
-wxFileCtrl::wxFileCtrl(wxWindow *win,
- wxWindowID id,
- const wxString& wild,
- bool showHidden,
- const wxPoint& pos,
- const wxSize& size,
- long style,
- const wxValidator &validator,
- const wxString &name)
- : wxListCtrl(win, id, pos, size, style, validator, name),
- m_wild(wild)
-{
- wxImageList *imageList = wxTheFileIconsTable->GetSmallImageList();
-
- SetImageList( imageList, wxIMAGE_LIST_SMALL );
-
- m_showHidden = showHidden;
-
- m_sort_foward = 1;
- m_sort_field = wxFileData::FileList_Name;
-
- m_dirName = wxT("*");
-
- if (style & wxLC_REPORT)
- ChangeToReportMode();
-}
-
-void wxFileCtrl::ChangeToListMode()
-{
- ClearAll();
- SetSingleStyle( wxLC_LIST );
- UpdateFiles();
-}
-
-void wxFileCtrl::ChangeToReportMode()
-{
- ClearAll();
- SetSingleStyle( wxLC_REPORT );
-
- // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
- // don't hardcode since mm/dd is dd/mm elsewhere
- int w, h;
- wxDateTime dt(22, wxDateTime::Dec, 2002, 22, 22, 22);
- wxString txt = dt.FormatDate() + wxT("22") + dt.Format(wxT("%I:%M:%S %p"));
- GetTextExtent(txt, &w, &h);
-
- InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, w );
- InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, w/2 );
- InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT, w/2 );
- InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT, w );
-#if defined(__UNIX__)
- GetTextExtent(wxT("Permissions 2"), &w, &h);
- InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, w );
-#elif defined(__WIN32__)
- GetTextExtent(wxT("Attributes 2"), &w, &h);
- InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT, w );
-#endif
-
- UpdateFiles();
-}
-
-void wxFileCtrl::ChangeToSmallIconMode()
-{
- ClearAll();
- SetSingleStyle( wxLC_SMALL_ICON );
- UpdateFiles();
-}
-
-void wxFileCtrl::ShowHidden( bool show )
-{
- m_showHidden = show;
- UpdateFiles();
-}
-
-long wxFileCtrl::Add( wxFileData *fd, wxListItem &item )
-{
- long ret = -1;
- item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE;
- fd->MakeItem( item );
- long my_style = GetWindowStyleFlag();
- if (my_style & wxLC_REPORT)
- {
- ret = InsertItem( item );
- for (int i = 1; i < wxFileData::FileList_Max; i++)
- SetItem( item.m_itemId, i, fd->GetEntry((wxFileData::fileListFieldType)i) );
- }
- else if ((my_style & wxLC_LIST) || (my_style & wxLC_SMALL_ICON))
- {
- ret = InsertItem( item );
- }
- return ret;
-}
-
-void wxFileCtrl::UpdateItem(const wxListItem &item)
-{
- wxFileData *fd = (wxFileData*)GetItemData(item);
- wxCHECK_RET(fd, wxT("invalid filedata"));
-
- fd->ReadData();
-
- SetItemText(item, fd->GetFileName());
- SetItemImage(item, fd->GetImageId());
-
- if (GetWindowStyleFlag() & wxLC_REPORT)
- {
- for (int i = 1; i < wxFileData::FileList_Max; i++)
- SetItem( item.m_itemId, i, fd->GetEntry((wxFileData::fileListFieldType)i) );
- }
-}
-
-void wxFileCtrl::UpdateFiles()
-{
- // don't do anything before ShowModal() call which sets m_dirName
- if ( m_dirName == wxT("*") )
- return;
-
- wxBusyCursor bcur; // this may take a while...
-
- DeleteAllItems();
-
- wxListItem item;
- item.m_itemId = 0;
- item.m_col = 0;
-
-#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)
- if ( IsTopMostDir(m_dirName) )
- {
- wxArrayString names, paths;
- wxArrayInt icons;
- size_t n, count = wxGetAvailableDrives(paths, names, icons);
-
- for (n=0; n<count; n++)
- {
- wxFileData *fd = new wxFileData(paths[n], names[n], wxFileData::is_drive, icons[n]);
- if (Add(fd, item) != -1)
- item.m_itemId++;
- else
- delete fd;
- }
- }
- else
-#endif // defined(__DOS__) || defined(__WINDOWS__)
- {
- // Real directory...
- if ( !IsTopMostDir(m_dirName) )
- {
- wxString p(wxPathOnly(m_dirName));
-#if defined(__UNIX__) && !defined(__OS2__)
- if (p.empty()) p = wxT("/");
-#endif // __UNIX__
- wxFileData *fd = new wxFileData(p, wxT(".."), wxFileData::is_dir, wxFileIconsTable::folder);
- if (Add(fd, item) != -1)
- item.m_itemId++;
- else
- delete fd;
- }
-
- wxString dirname(m_dirName);
-#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
- if (dirname.length() == 2 && dirname[1u] == wxT(':'))
- dirname << wxT('\\');
-#endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
- wxDir dir(dirname);
-
- if ( dir.IsOpened() )
- {
- wxString dirPrefix(dirname);
- if (dirPrefix.Last() != wxFILE_SEP_PATH)
- dirPrefix += wxFILE_SEP_PATH;
-
- int hiddenFlag = m_showHidden ? wxDIR_HIDDEN : 0;
-
- bool cont;
- wxString f;
-
- // Get the directories first (not matched against wildcards):
- cont = dir.GetFirst(&f, wxEmptyString, wxDIR_DIRS | hiddenFlag);
- while (cont)
- {
- wxFileData *fd = new wxFileData(dirPrefix + f, f, wxFileData::is_dir, wxFileIconsTable::folder);
- if (Add(fd, item) != -1)
- item.m_itemId++;
- else
- delete fd;
-
- cont = dir.GetNext(&f);
- }
-
- // Tokenize the wildcard string, so we can handle more than 1
- // search pattern in a wildcard.
- wxStringTokenizer tokenWild(m_wild, wxT(";"));
- while ( tokenWild.HasMoreTokens() )
- {
- cont = dir.GetFirst(&f, tokenWild.GetNextToken(),
- wxDIR_FILES | hiddenFlag);
- while (cont)
- {
- wxFileData *fd = new wxFileData(dirPrefix + f, f, wxFileData::is_file, wxFileIconsTable::file);
- if (Add(fd, item) != -1)
- item.m_itemId++;
- else
- delete fd;
-
- cont = dir.GetNext(&f);
- }
- }
- }
- }
-
- SortItems(m_sort_field, m_sort_foward);
-}
-
-void wxFileCtrl::SetWild( const wxString &wild )
-{
- if (wild.Find(wxT('|')) != wxNOT_FOUND)
- return;
-
- m_wild = wild;
- UpdateFiles();
-}
-
-void wxFileCtrl::MakeDir()
-{
- wxString new_name( _("NewName") );
- wxString path( m_dirName );
- path += wxFILE_SEP_PATH;
- path += new_name;
- if (wxFileExists(path))
- {
- // try NewName0, NewName1 etc.
- int i = 0;
- do {
- new_name = _("NewName");
- wxString num;
- num.Printf( wxT("%d"), i );
- new_name += num;
-
- path = m_dirName;
- path += wxFILE_SEP_PATH;
- path += new_name;
- i++;
- } while (wxFileExists(path));
- }
-
- wxLogNull log;
- if (!wxMkdir(path))
- {
- wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
- dialog.ShowModal();
- return;
- }
-
- wxFileData *fd = new wxFileData( path, new_name, wxFileData::is_dir, wxFileIconsTable::folder );
- wxListItem item;
- item.m_itemId = 0;
- item.m_col = 0;
- long id = Add( fd, item );
-
- if (id != -1)
- {
- SortItems(m_sort_field, m_sort_foward);
- id = FindItem( 0, (long)fd );
- EnsureVisible( id );
- EditLabel( id );
- }
- else
- delete fd;
-}
-
-void wxFileCtrl::GoToParentDir()
-{
- if (!IsTopMostDir(m_dirName))
- {
- size_t len = m_dirName.Len();
- if (wxEndsWithPathSeparator(m_dirName))
- m_dirName.Remove( len-1, 1 );
- wxString fname( wxFileNameFromPath(m_dirName) );
- m_dirName = wxPathOnly( m_dirName );
-#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
- if (!m_dirName.empty())
- {
- if (m_dirName.Last() == wxT('.'))
- m_dirName = wxEmptyString;
- }
-#elif defined(__UNIX__)
- if (m_dirName.empty())
- m_dirName = wxT("/");
-#endif
- UpdateFiles();
- long id = FindItem( 0, fname );
- if (id != wxNOT_FOUND)
- {
- SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
- EnsureVisible( id );
- }
- }
-}
-
-void wxFileCtrl::GoToHomeDir()
-{
- wxString s = wxGetUserHome( wxString() );
- GoToDir(s);
-}
-
-void wxFileCtrl::GoToDir( const wxString &dir )
-{
- if (!wxPathExists(dir)) return;
-
- m_dirName = dir;
- UpdateFiles();
- SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
- EnsureVisible( 0 );
-}
-
-void wxFileCtrl::FreeItemData(wxListItem& item)
-{
- if ( item.m_data )
- {
- wxFileData *fd = (wxFileData*)item.m_data;
- delete fd;
-
- item.m_data = 0;
- }
-}
-
-void wxFileCtrl::OnListDeleteItem( wxListEvent &event )
-{
- FreeItemData(event.m_item);
-}
-
-void wxFileCtrl::OnListDeleteAllItems( wxListEvent & WXUNUSED(event) )
-{
- FreeAllItemsData();
-}
-
-void wxFileCtrl::FreeAllItemsData()
-{
- wxListItem item;
- item.m_mask = wxLIST_MASK_DATA;
-
- item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL );
- while ( item.m_itemId != -1 )
- {
- GetItem( item );
- FreeItemData(item);
- item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL );
- }
-}
-
-void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event )
-{
- wxFileData *fd = (wxFileData*)event.m_item.m_data;
- wxASSERT( fd );
-
- if ((event.GetLabel().empty()) ||
- (event.GetLabel() == _(".")) ||
- (event.GetLabel() == _("..")) ||
- (event.GetLabel().First( wxFILE_SEP_PATH ) != wxNOT_FOUND))
- {
- wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
- dialog.ShowModal();
- event.Veto();
- return;
- }
-
- wxString new_name( wxPathOnly( fd->GetFilePath() ) );
- new_name += wxFILE_SEP_PATH;
- new_name += event.GetLabel();
-
- wxLogNull log;
-
- if (wxFileExists(new_name))
- {
- wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
- dialog.ShowModal();
- event.Veto();
- }
-
- if (wxRenameFile(fd->GetFilePath(),new_name))
- {
- fd->SetNewName( new_name, event.GetLabel() );
- SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
- UpdateItem( event.GetItem() );
- EnsureVisible( event.GetItem() );
- }
- else
- {
- wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
- dialog.ShowModal();
- event.Veto();
- }
-}
-
-void wxFileCtrl::OnListColClick( wxListEvent &event )
-{
- int col = event.GetColumn();
-
- switch (col)
- {
- case wxFileData::FileList_Name :
- case wxFileData::FileList_Size :
- case wxFileData::FileList_Type :
- case wxFileData::FileList_Time : break;
- default : return;
- }
-
- if ((wxFileData::fileListFieldType)col == m_sort_field)
- m_sort_foward = !m_sort_foward;
- else
- m_sort_field = (wxFileData::fileListFieldType)col;
-
- SortItems(m_sort_field, m_sort_foward);
-}
-
-void wxFileCtrl::SortItems(wxFileData::fileListFieldType field, bool foward)
-{
- m_sort_field = field;
- m_sort_foward = foward;
- long sort_dir = foward ? 1 : -1;
-
- switch (m_sort_field)
- {
- case wxFileData::FileList_Name :
- {
- wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataNameCompare, sort_dir);
- break;
- }
- case wxFileData::FileList_Size :
- {
- wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataSizeCompare, sort_dir);
- break;
- }
- case wxFileData::FileList_Type :
- {
- wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataTypeCompare, sort_dir);
- break;
- }
- case wxFileData::FileList_Time :
- {
- wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataTimeCompare, sort_dir);
- break;
- }
- default : break;
- }
-}
-
-wxFileCtrl::~wxFileCtrl()
-{
- // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
- // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after
- // the destruction of the wxFileCtrl we need to free any data here:
- FreeAllItemsData();
-}