X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/74ab5f5b9df928c75c119a2c00b0d2092caa49f1..d65381ac3415e9be42a378511e91755eb114b72b:/src/generic/filectrlg.cpp diff --git a/src/generic/filectrlg.cpp b/src/generic/filectrlg.cpp index 5427143e01..66ee552da4 100644 --- a/src/generic/filectrlg.cpp +++ b/src/generic/filectrlg.cpp @@ -28,7 +28,6 @@ #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 @@ -1031,67 +1030,102 @@ bool wxGenericFileCtrl::Create( wxWindow *parent, 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::DoGetFilename( const bool fullPath ) const +wxString wxGenericFileCtrl::GetDirectory() 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" ) ); + // 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(); +} - const wxString value = m_text->GetValue(); +wxFileName wxGenericFileCtrl::DoGetFileName() const +{ + wxFileName fn; - if ( !value.empty() ) - return value; - return fullPath ? ( GetProperFileListDir() + value ) : value; + 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); + + 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; } -void wxGenericFileCtrl::DoGetFilenames( wxArrayString& filenames, const bool fullPath ) const +// helper used in DoGetFilenames() and needed because Borland can't compile +// operator?: inline +static inline wxString GetFileNameOrPath(const wxFileName& fn, bool fullPath) { - filenames.Empty(); + return fullPath ? fn.GetFullPath() : fn.GetFullName(); +} - const wxString dir = GetProperFileListDir(); - const wxString value = m_text->GetValue(); +void +wxGenericFileCtrl::DoGetFilenames(wxArrayString& filenames, bool fullPath) const +{ + filenames.clear(); + + 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)); } } @@ -1105,11 +1139,6 @@ bool wxGenericFileCtrl::SetDirectory( const wxString& dir ) 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 ); @@ -1444,17 +1473,4 @@ void wxGenericFileCtrl::GoToHomeDir() 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