X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0cf3e587a2ec542ba1eb6e03a84c54edefae1881..8a31648287be0ef976f133de2786b137f1e98340:/src/generic/filectrlg.cpp?ds=sidebyside diff --git a/src/generic/filectrlg.cpp b/src/generic/filectrlg.cpp index 967221b7ee..0a697894dc 100644 --- a/src/generic/filectrlg.cpp +++ b/src/generic/filectrlg.cpp @@ -28,12 +28,12 @@ #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 #include "wx/dir.h" #include "wx/tokenzr.h" +#include "wx/imaglist.h" #ifdef __WXMSW__ #include "wx/msw/wrapwin.h" @@ -53,7 +53,7 @@ // ---------------------------------------------------------------------------- static -int wxCALLBACK wxFileDataNameCompare( long data1, long data2, long sortOrder) +int wxCALLBACK wxFileDataNameCompare( wxIntPtr data1, wxIntPtr data2, wxIntPtr sortOrder) { wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1); wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2); @@ -71,7 +71,7 @@ int wxCALLBACK wxFileDataNameCompare( long data1, long data2, long sortOrder) } static -int wxCALLBACK wxFileDataSizeCompare(long data1, long data2, long sortOrder) +int wxCALLBACK wxFileDataSizeCompare(wxIntPtr data1, wxIntPtr data2, wxIntPtr sortOrder) { wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1); wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2); @@ -93,7 +93,7 @@ int wxCALLBACK wxFileDataSizeCompare(long data1, long data2, long sortOrder) } static -int wxCALLBACK wxFileDataTypeCompare(long data1, long data2, long sortOrder) +int wxCALLBACK wxFileDataTypeCompare(wxIntPtr data1, wxIntPtr data2, wxIntPtr sortOrder) { wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1); wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2); @@ -115,7 +115,7 @@ int wxCALLBACK wxFileDataTypeCompare(long data1, long data2, long sortOrder) } static -int wxCALLBACK wxFileDataTimeCompare(long data1, long data2, long sortOrder) +int wxCALLBACK wxFileDataTimeCompare(wxIntPtr data1, wxIntPtr data2, wxIntPtr sortOrder) { wxFileData *fd1 = (wxFileData *)wxUIntToPtr(data1); wxFileData *fd2 = (wxFileData *)wxUIntToPtr(data2); @@ -194,7 +194,7 @@ void wxFileData::ReadData() m_type |= (fileAttribs & FILE_ATTRIBUTE_DIRECTORY) != 0 ? is_dir : 0; wxString p, f, ext; - wxSplitPath(m_filePath, & p, & f, & ext); + wxFileName::SplitPath(m_filePath, & p, & f, & ext); if (wxStricmp(ext, wxT("exe")) == 0) m_type |= is_exe; @@ -224,15 +224,9 @@ void wxFileData::ReadData() #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 -#ifndef wxNEED_WX_UNISTD_H - wxStat( m_filePath.fn_str() , &buff ); -#else wxStat( m_filePath, &buff ); -#endif #endif m_type |= (buff.st_mode & S_IFDIR) != 0 ? is_dir : 0; @@ -245,25 +239,25 @@ void wxFileData::ReadData() // __WXWINCE__ #if defined(__UNIX__) - m_permissions.Printf(_T("%c%c%c%c%c%c%c%c%c"), - buff.st_mode & wxS_IRUSR ? _T('r') : _T('-'), - buff.st_mode & wxS_IWUSR ? _T('w') : _T('-'), - buff.st_mode & wxS_IXUSR ? _T('x') : _T('-'), - buff.st_mode & wxS_IRGRP ? _T('r') : _T('-'), - buff.st_mode & wxS_IWGRP ? _T('w') : _T('-'), - buff.st_mode & wxS_IXGRP ? _T('x') : _T('-'), - buff.st_mode & wxS_IROTH ? _T('r') : _T('-'), - buff.st_mode & wxS_IWOTH ? _T('w') : _T('-'), - buff.st_mode & wxS_IXOTH ? _T('x') : _T('-')); + m_permissions.Printf(wxT("%c%c%c%c%c%c%c%c%c"), + buff.st_mode & wxS_IRUSR ? wxT('r') : wxT('-'), + buff.st_mode & wxS_IWUSR ? wxT('w') : wxT('-'), + buff.st_mode & wxS_IXUSR ? wxT('x') : wxT('-'), + buff.st_mode & wxS_IRGRP ? wxT('r') : wxT('-'), + buff.st_mode & wxS_IWGRP ? wxT('w') : wxT('-'), + buff.st_mode & wxS_IXGRP ? wxT('x') : wxT('-'), + buff.st_mode & wxS_IROTH ? wxT('r') : wxT('-'), + buff.st_mode & wxS_IWOTH ? wxT('w') : wxT('-'), + buff.st_mode & wxS_IXOTH ? wxT('x') : wxT('-')); #elif defined(__WIN32__) DWORD attribs = ::GetFileAttributes(m_filePath.c_str()); if (attribs != (DWORD)-1) { - m_permissions.Printf(_T("%c%c%c%c"), - attribs & FILE_ATTRIBUTE_ARCHIVE ? _T('A') : _T(' '), - attribs & FILE_ATTRIBUTE_READONLY ? _T('R') : _T(' '), - attribs & FILE_ATTRIBUTE_HIDDEN ? _T('H') : _T(' '), - attribs & FILE_ATTRIBUTE_SYSTEM ? _T('S') : _T(' ')); + m_permissions.Printf(wxT("%c%c%c%c"), + attribs & FILE_ATTRIBUTE_ARCHIVE ? wxT('A') : wxT(' '), + attribs & FILE_ATTRIBUTE_READONLY ? wxT('R') : wxT(' '), + attribs & FILE_ATTRIBUTE_HIDDEN ? wxT('H') : wxT(' '), + attribs & FILE_ATTRIBUTE_SYSTEM ? wxT('S') : wxT(' ')); } #endif @@ -357,7 +351,7 @@ wxString wxFileData::GetEntry( fileListFieldType num ) const #endif // defined(__UNIX__) || defined(__WIN32__) default: - wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") ); + wxFAIL_MSG( wxT("unexpected field in wxFileData::GetEntry()") ); } return s; @@ -382,37 +376,35 @@ void wxFileData::MakeItem( wxListItem &item ) if (IsLink()) { - wxColour dg = wxTheColourDatabase->Find( _T("MEDIUM GREY") ); - if ( dg.Ok() ) + wxColour dg = wxTheColourDatabase->Find( wxT("MEDIUM GREY") ); + if ( dg.IsOk() ) item.SetTextColour(dg); } item.m_data = wxPtrToUInt(this); } //----------------------------------------------------------------------------- -// 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, @@ -430,7 +422,7 @@ wxFileList::wxFileList(wxWindow *win, m_showHidden = showHidden; - m_sort_foward = 1; + m_sort_forward = true; m_sort_field = wxFileData::FileList_Name; m_dirName = wxT("*"); @@ -439,14 +431,14 @@ wxFileList::wxFileList(wxWindow *win, ChangeToReportMode(); } -void wxFileList::ChangeToListMode() +void wxFileListCtrl::ChangeToListMode() { ClearAll(); SetSingleStyle( wxLC_LIST ); UpdateFiles(); } -void wxFileList::ChangeToReportMode() +void wxFileListCtrl::ChangeToReportMode() { ClearAll(); SetSingleStyle( wxLC_REPORT ); @@ -473,20 +465,20 @@ void wxFileList::ChangeToReportMode() 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; @@ -505,7 +497,7 @@ long wxFileList::Add( wxFileData *fd, wxListItem &item ) 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")); @@ -522,7 +514,7 @@ void wxFileList::UpdateItem(const wxListItem &item) } } -void wxFileList::UpdateFiles() +void wxFileListCtrl::UpdateFiles() { // don't do anything before ShowModal() call which sets m_dirName if ( m_dirName == wxT("*") ) @@ -541,11 +533,23 @@ void wxFileList::UpdateFiles() { wxArrayString names, paths; wxArrayInt icons; - size_t n, count = wxGetAvailableDrives(paths, names, icons); + const size_t count = wxGetAvailableDrives(paths, names, icons); - for (n=0; nSetNewName( new_name, event.GetLabel() ); - ignoreChanges = true; SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); - ignoreChanges = false; UpdateItem( event.GetItem() ); EnsureVisible( event.GetItem() ); @@ -820,7 +818,7 @@ void wxFileList::OnListEndLabelEdit( wxListEvent &event ) } } -void wxFileList::OnListColClick( wxListEvent &event ) +void wxFileListCtrl::OnListColClick( wxListEvent &event ) { int col = event.GetColumn(); @@ -834,17 +832,17 @@ void wxFileList::OnListColClick( wxListEvent &event ) } 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) @@ -868,11 +866,11 @@ void wxFileList::SortItems(wxFileData::fileListFieldType field, bool forward) } } -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(); } @@ -909,6 +907,7 @@ bool wxGenericFileCtrl::Create( wxWindow *parent, this->m_style = style; m_inSelected = false; m_noSelChgEvent = false; + m_check = NULL; // check that the styles are not contradictory wxASSERT_MSG( !( ( m_style & wxFC_SAVE ) && ( m_style & wxFC_OPEN ) ), @@ -944,10 +943,11 @@ bool wxGenericFileCtrl::Create( wxWindow *parent, wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL ); if ( is_pda ) - staticsizer->Add( new wxStaticText( this, wxID_ANY, _( "Current directory:" ) ), 0, wxRIGHT, 10 ); + staticsizer->Add( new wxStaticText( this, wxID_ANY, _( "Current directory:" ) ), + wxSizerFlags().DoubleBorder(wxRIGHT) ); m_static = new wxStaticText( this, wxID_ANY, m_dir ); staticsizer->Add( m_static, 1 ); - mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 10 ); + mainsizer->Add( staticsizer, wxSizerFlags().Expand().Border()); long style2 = wxLC_LIST; if ( !( m_style & wxFC_MULTIPLE ) ) @@ -959,10 +959,10 @@ bool wxGenericFileCtrl::Create( wxWindow *parent, 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, @@ -976,33 +976,25 @@ bool wxGenericFileCtrl::Create( wxWindow *parent, wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL ); textsizer->Add( m_text, wxSizerFlags( 1 ).Centre().Border() ); + textsizer->Add( m_choice, wxSizerFlags( 1 ).Centre().Border() ); mainsizer->Add( textsizer, wxSizerFlags().Expand() ); - m_check = NULL; - textsizer->Add( m_choice, wxSizerFlags( 1 ).Centre().Border() ); } else // !is_pda { - mainsizer->Add( m_list, wxSizerFlags( 1 ).Expand().DoubleHorzBorder() ); - - wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL ); - textsizer->Add( m_text, wxSizerFlags( 1 ).Centre(). - DoubleBorder( wxLEFT | wxRIGHT | wxTOP ) ); - mainsizer->Add( textsizer, wxSizerFlags().Expand() ); - - wxSizerFlags flagsCentre; - flagsCentre.Centre().DoubleBorder(); + mainsizer->Add( m_list, wxSizerFlags( 1 ).Expand().Border() ); + mainsizer->Add( m_text, wxSizerFlags().Expand().Border() ); wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL ); - choicesizer->Add( m_choice, wxSizerFlags( flagsCentre ).Proportion( 1 ) ); + choicesizer->Add( m_choice, wxSizerFlags( 1 ).Centre() ); if ( !( m_style & wxFC_NOSHOWHIDDEN ) ) - { + { m_check = new wxCheckBox( this, ID_CHECK, _( "Show &hidden files" ) ); - choicesizer->Add( m_check, flagsCentre ); - } + choicesizer->Add( m_check, wxSizerFlags().Centre().DoubleBorder(wxLEFT) ); + } - mainsizer->Add( choicesizer, wxSizerFlags().Expand() ); + mainsizer->Add( choicesizer, wxSizerFlags().Expand().Border() ); } SetWildcard( wildCard ); @@ -1027,67 +1019,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 ( item.m_itemId == -1 ) + break; - if ( fullPath ) - filenames.Add( dir + item.m_text ); - else - filenames.Add( item.m_text ); + 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)); } } @@ -1101,11 +1128,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 ); @@ -1145,21 +1167,25 @@ bool wxGenericFileCtrl::SetFilename( const wxString& name ) void wxGenericFileCtrl::DoSetFilterIndex( int filterindex ) { - const wxString& str = (wx_static_cast(wxStringClientData *, - m_choice->GetClientObject( filterindex ))) - ->GetData(); + wxClientData *pcd = m_choice->GetClientObject( filterindex ); + if ( !pcd ) + return; + + const wxString& str = ((static_cast(pcd))->GetData()); m_list->SetWild( str ); m_filterIndex = filterindex; if ( str.Left( 2 ) == wxT( "*." ) ) { m_filterExtension = str.Mid( 1 ); - if ( m_filterExtension == _T( ".*" ) ) + if ( m_filterExtension == wxT( ".*" ) ) m_filterExtension.clear(); } else { m_filterExtension.clear(); } + + GenerateFilterChangedEvent( this, this ); } void wxGenericFileCtrl::SetWildcard( const wxString& wildCard ) @@ -1266,7 +1292,7 @@ void wxGenericFileCtrl::OnSelected( wxListEvent &event ) return; } - + m_ignoreChanges = true; m_text->SetValue( filename ); @@ -1399,7 +1425,7 @@ bool wxGenericFileCtrl::SetPath( const wxString& path ) return false; wxString ext; - wxSplitPath( path, &m_dir, &m_fileName, &ext ); + wxFileName::SplitPath( path, &m_dir, &m_fileName, &ext ); if ( !ext.empty() ) { m_fileName += wxT( "." ); @@ -1440,17 +1466,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