#include "wx/filedlg.h"
#endif
-#include "wx/filename.h"
#include "wx/clntdata.h"
#include "wx/file.h" // for wxS_IXXX constants only
#include "wx/generic/dirctrlg.h" // for wxFileIconsTable
#if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
lstat( m_filePath.fn_str(), &buff );
- m_type |= S_ISLNK( buff.st_mode ) != 0 ? is_link : 0;
+ m_type |= S_ISLNK(buff.st_mode) ? is_link : 0;
#else // no lstat()
// only translate to file charset if we don't go by our
// wxStat implementation
}
//-----------------------------------------------------------------------------
-// wxFileList
+// wxFileListCtrl
//-----------------------------------------------------------------------------
-static bool ignoreChanges = false;
+IMPLEMENT_DYNAMIC_CLASS(wxFileListCtrl,wxListCtrl)
-IMPLEMENT_DYNAMIC_CLASS(wxFileList,wxListCtrl)
-
-BEGIN_EVENT_TABLE(wxFileList,wxListCtrl)
- EVT_LIST_DELETE_ITEM(wxID_ANY, wxFileList::OnListDeleteItem)
- EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY, wxFileList::OnListDeleteAllItems)
- EVT_LIST_END_LABEL_EDIT(wxID_ANY, wxFileList::OnListEndLabelEdit)
- EVT_LIST_COL_CLICK(wxID_ANY, wxFileList::OnListColClick)
+BEGIN_EVENT_TABLE(wxFileListCtrl,wxListCtrl)
+ EVT_LIST_DELETE_ITEM(wxID_ANY, wxFileListCtrl::OnListDeleteItem)
+ EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY, wxFileListCtrl::OnListDeleteAllItems)
+ EVT_LIST_END_LABEL_EDIT(wxID_ANY, wxFileListCtrl::OnListEndLabelEdit)
+ EVT_LIST_COL_CLICK(wxID_ANY, wxFileListCtrl::OnListColClick)
END_EVENT_TABLE()
-wxFileList::wxFileList()
+wxFileListCtrl::wxFileListCtrl()
{
m_showHidden = false;
- m_sort_foward = 1;
+ m_sort_forward = true;
m_sort_field = wxFileData::FileList_Name;
}
-wxFileList::wxFileList(wxWindow *win,
+wxFileListCtrl::wxFileListCtrl(wxWindow *win,
wxWindowID id,
const wxString& wild,
bool showHidden,
m_showHidden = showHidden;
- m_sort_foward = 1;
+ m_sort_forward = true;
m_sort_field = wxFileData::FileList_Name;
m_dirName = wxT("*");
ChangeToReportMode();
}
-void wxFileList::ChangeToListMode()
+void wxFileListCtrl::ChangeToListMode()
{
ClearAll();
SetSingleStyle( wxLC_LIST );
UpdateFiles();
}
-void wxFileList::ChangeToReportMode()
+void wxFileListCtrl::ChangeToReportMode()
{
ClearAll();
SetSingleStyle( wxLC_REPORT );
UpdateFiles();
}
-void wxFileList::ChangeToSmallIconMode()
+void wxFileListCtrl::ChangeToSmallIconMode()
{
ClearAll();
SetSingleStyle( wxLC_SMALL_ICON );
UpdateFiles();
}
-void wxFileList::ShowHidden( bool show )
+void wxFileListCtrl::ShowHidden( bool show )
{
m_showHidden = show;
UpdateFiles();
}
-long wxFileList::Add( wxFileData *fd, wxListItem &item )
+long wxFileListCtrl::Add( wxFileData *fd, wxListItem &item )
{
long ret = -1;
item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE;
return ret;
}
-void wxFileList::UpdateItem(const wxListItem &item)
+void wxFileListCtrl::UpdateItem(const wxListItem &item)
{
wxFileData *fd = (wxFileData*)GetItemData(item);
wxCHECK_RET(fd, wxT("invalid filedata"));
}
}
-void wxFileList::UpdateFiles()
+void wxFileListCtrl::UpdateFiles()
{
// don't do anything before ShowModal() call which sets m_dirName
if ( m_dirName == wxT("*") )
{
wxArrayString names, paths;
wxArrayInt icons;
- size_t n, count = wxGetAvailableDrives(paths, names, icons);
+ const size_t count = wxGetAvailableDrives(paths, names, icons);
- for (n=0; n<count; n++)
+ for ( size_t n = 0; n < count; n++ )
{
- wxFileData *fd = new wxFileData(paths[n], names[n], wxFileData::is_drive, icons[n]);
+ // use paths[n] as the drive name too as our HandleAction() can't
+ // deal with the drive names (of the form "System (C:)") currently
+ // as it mistakenly treats them as file names
+ //
+ // it would be preferable to show names, and not paths, in the
+ // dialog just as the native dialog does but for this we must:
+ // a) store the item type as item data and modify HandleAction()
+ // to use it instead of wxDirExists() to check whether the item
+ // is a directory
+ // b) store the drives by their drive letters and not their
+ // descriptions as otherwise it's pretty confusing to the user
+ wxFileData *fd = new wxFileData(paths[n], paths[n],
+ wxFileData::is_drive, icons[n]);
if (Add(fd, item) != -1)
item.m_itemId++;
else
}
}
- SortItems(m_sort_field, m_sort_foward);
+ SortItems(m_sort_field, m_sort_forward);
}
-void wxFileList::SetWild( const wxString &wild )
+void wxFileListCtrl::SetWild( const wxString &wild )
{
if (wild.Find(wxT('|')) != wxNOT_FOUND)
return;
UpdateFiles();
}
-void wxFileList::MakeDir()
+void wxFileListCtrl::MakeDir()
{
wxString new_name( _("NewName") );
wxString path( m_dirName );
if (id != -1)
{
- SortItems(m_sort_field, m_sort_foward);
+ SortItems(m_sort_field, m_sort_forward);
id = FindItem( 0, wxPtrToUInt(fd) );
EnsureVisible( id );
EditLabel( id );
delete fd;
}
-void wxFileList::GoToParentDir()
+void wxFileListCtrl::GoToParentDir()
{
if (!IsTopMostDir(m_dirName))
{
long id = FindItem( 0, fname );
if (id != wxNOT_FOUND)
{
- ignoreChanges = true;
SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
EnsureVisible( id );
- ignoreChanges = false;
}
}
}
-void wxFileList::GoToHomeDir()
+void wxFileListCtrl::GoToHomeDir()
{
wxString s = wxGetUserHome( wxString() );
GoToDir(s);
}
-void wxFileList::GoToDir( const wxString &dir )
+void wxFileListCtrl::GoToDir( const wxString &dir )
{
if (!wxDirExists(dir)) return;
m_dirName = dir;
UpdateFiles();
- ignoreChanges = true;
SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
- ignoreChanges = false;
EnsureVisible( 0 );
}
-void wxFileList::FreeItemData(wxListItem& item)
+void wxFileListCtrl::FreeItemData(wxListItem& item)
{
if ( item.m_data )
{
}
}
-void wxFileList::OnListDeleteItem( wxListEvent &event )
+void wxFileListCtrl::OnListDeleteItem( wxListEvent &event )
{
FreeItemData(event.m_item);
}
-void wxFileList::OnListDeleteAllItems( wxListEvent & WXUNUSED(event) )
+void wxFileListCtrl::OnListDeleteAllItems( wxListEvent & WXUNUSED(event) )
{
FreeAllItemsData();
}
-void wxFileList::FreeAllItemsData()
+void wxFileListCtrl::FreeAllItemsData()
{
wxListItem item;
item.m_mask = wxLIST_MASK_DATA;
}
}
-void wxFileList::OnListEndLabelEdit( wxListEvent &event )
+void wxFileListCtrl::OnListEndLabelEdit( wxListEvent &event )
{
wxFileData *fd = (wxFileData*)event.m_item.m_data;
wxASSERT( fd );
{
fd->SetNewName( new_name, event.GetLabel() );
- ignoreChanges = true;
SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
- ignoreChanges = false;
UpdateItem( event.GetItem() );
EnsureVisible( event.GetItem() );
}
}
-void wxFileList::OnListColClick( wxListEvent &event )
+void wxFileListCtrl::OnListColClick( wxListEvent &event )
{
int col = event.GetColumn();
}
if ((wxFileData::fileListFieldType)col == m_sort_field)
- m_sort_foward = !m_sort_foward;
+ m_sort_forward = !m_sort_forward;
else
m_sort_field = (wxFileData::fileListFieldType)col;
- SortItems(m_sort_field, m_sort_foward);
+ SortItems(m_sort_field, m_sort_forward);
}
-void wxFileList::SortItems(wxFileData::fileListFieldType field, bool forward)
+void wxFileListCtrl::SortItems(wxFileData::fileListFieldType field, bool forward)
{
m_sort_field = field;
- m_sort_foward = forward;
+ m_sort_forward = forward;
const long sort_dir = forward ? 1 : -1;
switch (m_sort_field)
}
}
-wxFileList::~wxFileList()
+wxFileListCtrl::~wxFileListCtrl()
{
// Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
- // wxFileList::OnListDeleteAllItems. But if the event is generated after
- // the destruction of the wxFileList we need to free any data here:
+ // wxFileListCtrl::OnListDeleteAllItems. But if the event is generated after
+ // the destruction of the wxFileListCtrl we need to free any data here:
FreeAllItemsData();
}
style2 |= wxSUNKEN_BORDER;
#endif
- m_list = new wxFileList( this, ID_FILELIST_CTRL,
- wxEmptyString, false,
- wxDefaultPosition, wxSize( 400, 140 ),
- style2 );
+ m_list = new wxFileListCtrl( this, ID_FILELIST_CTRL,
+ wxEmptyString, false,
+ wxDefaultPosition, wxSize( 400, 140 ),
+ style2 );
m_text = new wxTextCtrl( this, ID_TEXT, wxEmptyString,
wxDefaultPosition, wxDefaultSize,
return true;
}
+// NB: there is an unfortunate mismatch between wxFileName and wxFileDialog
+// method names but our GetDirectory() does correspond to wxFileName::
+// GetPath() while our GetPath() is wxFileName::GetFullPath()
wxString wxGenericFileCtrl::GetPath() const
{
- return DoGetFilename( true );
+ wxASSERT_MSG ( !(m_style & wxFC_MULTIPLE), "use GetPaths() instead" );
+
+ return DoGetFileName().GetFullPath();
}
wxString wxGenericFileCtrl::GetFilename() const
{
- return DoGetFilename( false );
+ wxASSERT_MSG ( !(m_style & wxFC_MULTIPLE), "use GetFilenames() instead" );
+
+ return DoGetFileName().GetFullName();
+}
+
+wxString wxGenericFileCtrl::GetDirectory() const
+{
+ // don't check for wxFC_MULTIPLE here, this one is probably safe to call in
+ // any case as it can be always taken to mean "current directory"
+ return DoGetFileName().GetPath();
}
-wxString wxGenericFileCtrl::DoGetFilename( const bool fullPath ) const
+wxFileName wxGenericFileCtrl::DoGetFileName() const
{
- wxASSERT_MSG( ( m_style & wxFC_MULTIPLE ) == 0,
- wxT( "With controls that has wxFC_MULTIPLE style " )
- wxT( "use GetFilenames/GetPaths to get all filenames/paths selected" ) );
+ wxFileName fn;
- const wxString value = m_text->GetValue();
+ wxString value = m_text->GetValue();
+ if ( value.empty() )
+ {
+ // nothing in the text control, get the selected file from the list
+ wxListItem item;
+ item.m_itemId = m_list->GetNextItem(-1, wxLIST_NEXT_ALL,
+ wxLIST_STATE_SELECTED);
+ m_list->GetItem(item);
- if ( !value.empty() )
- return value;
- return fullPath ? ( GetProperFileListDir() + value ) : value;
+ fn.Assign(m_list->GetDir(), item.m_text);
+ }
+ else // user entered the value
+ {
+ // the path can be either absolute or relative
+ fn.Assign(value);
+ if ( fn.IsRelative() )
+ fn.MakeAbsolute(m_list->GetDir());
+ }
+
+ return fn;
+}
+
+// helper used in DoGetFilenames() and needed because Borland can't compile
+// operator?: inline
+static inline wxString GetFileNameOrPath(const wxFileName& fn, bool fullPath)
+{
+ return fullPath ? fn.GetFullPath() : fn.GetFullName();
}
-void wxGenericFileCtrl::DoGetFilenames( wxArrayString& filenames, const bool fullPath ) const
+void
+wxGenericFileCtrl::DoGetFilenames(wxArrayString& filenames, bool fullPath) const
{
- filenames.Empty();
+ filenames.clear();
- const wxString dir = GetProperFileListDir();
- const wxString value = m_text->GetValue();
+ const wxString dir = m_list->GetDir();
+ const wxString value = m_text->GetValue();
if ( !value.empty() )
{
- if ( fullPath )
- filenames.Add( dir + value );
- else
- filenames.Add( value );
+ wxFileName fn(value);
+ if ( fn.IsRelative() )
+ fn.MakeAbsolute(dir);
+
+ filenames.push_back(GetFileNameOrPath(fn, fullPath));
return;
}
- if ( m_list->GetSelectedItemCount() == 0 )
- {
+ const int numSel = m_list->GetSelectedItemCount();
+ if ( !numSel )
return;
- }
- filenames.Alloc( m_list->GetSelectedItemCount() );
+ filenames.reserve(numSel);
wxListItem item;
item.m_mask = wxLIST_MASK_TEXT;
-
- item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
- while ( item.m_itemId != -1 )
+ item.m_itemId = -1;
+ for ( ;; )
{
- m_list->GetItem( item );
+ item.m_itemId = m_list->GetNextItem(item.m_itemId, wxLIST_NEXT_ALL,
+ wxLIST_STATE_SELECTED);
- if ( fullPath )
- filenames.Add( dir + item.m_text );
- else
- filenames.Add( item.m_text );
+ if ( item.m_itemId == -1 )
+ break;
+
+ m_list->GetItem(item);
- item.m_itemId = m_list->GetNextItem( item.m_itemId,
- wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
+ const wxFileName fn(dir, item.m_text);
+ filenames.push_back(GetFileNameOrPath(fn, fullPath));
}
}
return wxFileName( dir ).SameAs( m_list->GetDir() );
}
-wxString wxGenericFileCtrl::GetDirectory() const
-{
- return m_list->GetDir();
-}
-
bool wxGenericFileCtrl::SetFilename( const wxString& name )
{
const long item = m_list->FindItem( -1, name );
UpdateControls();
}
-wxString wxGenericFileCtrl::GetProperFileListDir() const
-{
- wxString dir = m_list->GetDir();
-#ifdef __UNIX__
- if ( dir != wxT( "/" ) )
-#elif defined(__WXWINCE__)
- if ( dir != wxT( "/" ) && dir != wxT( "\\" ) )
-#endif
- dir += wxFILE_SEP_PATH;
-
- return dir;
-}
-
#endif // wxUSE_FILECTRL