-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(), 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...
-
- FreeAllItemsData();
- DeleteAllItems();
-
- wxListItem item;
- item.m_itemId = 0;
- item.m_col = 0;
-
-#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__WXPM__)
- 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]);
- Add(fd, item);
- item.m_itemId++;
- }
- }
- else
-#endif // defined(__DOS__) || defined(__WINDOWS__)
- {
- // Real directory...
- if ( !IsTopMostDir(m_dirName) )
- {
- wxString p(wxPathOnly(m_dirName));
-#ifdef __UNIX__
- if (p.IsEmpty()) p = wxT("/");
-#endif // __UNIX__
- wxFileData *fd = new wxFileData(p, wxT(".."), wxFileData::is_dir, wxFileIconsTable::folder);
- Add(fd, item);
- item.m_itemId++;
- }
-
- wxString dirname(m_dirName);
-#if defined(__DOS__) || defined(__WINDOWS__)
- if (dirname.length() == 2 && dirname[1u] == wxT(':'))
- dirname << wxT('\\');
-#endif // defined(__DOS__) || defined(__WINDOWS__)
- 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);
- Add(fd, item);
- item.m_itemId++;
- 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);
- Add(fd, item);
- item.m_itemId++;
- 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 );
- }
-}
-
-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__)
- if (!m_dirName.IsEmpty())
- {
- if (m_dirName.Last() == wxT('.'))
- m_dirName = wxT("");
- }
-#elif defined(__UNIX__)
- if (m_dirName.IsEmpty())
- m_dirName = wxT("/");
-#endif
- UpdateFiles();
- long id = FindItem( 0, fname );
- if (id != -1)
- {
- 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 (!wxDirExists(dir)) return;
-
- m_dirName = dir;
- UpdateFiles();
- SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
- EnsureVisible( 0 );
-}
-
-void wxFileCtrl::FreeItemData(const wxListItem& item)