diff --git a/include/wx/filehistory.h b/include/wx/filehistory.h index 96b90abe7c..123b0115d2 100644 --- a/include/wx/filehistory.h +++ b/include/wx/filehistory.h @@ -78,6 +78,9 @@ private: // this to ensure the same normalization is used everywhere. static wxString NormalizeFileName(const wxFileName& filename); + // Remove any existing entries from the associated menus. + void RemoveExistingHistory(); + wxDECLARE_NO_COPY_CLASS(wxFileHistoryBase); }; diff --git a/src/common/filehistorycmn.cpp b/src/common/filehistorycmn.cpp index 176923d972..75bf1118b5 100644 --- a/src/common/filehistorycmn.cpp +++ b/src/common/filehistorycmn.cpp @@ -215,6 +215,8 @@ void wxFileHistoryBase::RemoveMenu(wxMenu *menu) #if wxUSE_CONFIG void wxFileHistoryBase::Load(const wxConfigBase& config) { + RemoveExistingHistory(); + m_fileHistory.Clear(); wxString buf; @@ -275,4 +277,31 @@ void wxFileHistoryBase::AddFilesToMenu(wxMenu* menu) } } +void wxFileHistoryBase::RemoveExistingHistory() +{ + size_t count = m_fileHistory.GetCount(); + if ( !count ) + return; + + for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst(); + node; + node = node->GetNext() ) + { + wxMenu * const menu = static_cast(node->GetData()); + + // Notice that we remove count+1 items from the menu as we also remove + // the separator preceding them. + for ( size_t n = 0; n <= count; n++ ) + { + const wxMenuItemList::compatibility_iterator + nodeLast = menu->GetMenuItems().GetLast(); + if ( nodeLast ) + { + wxMenuItem * const lastMenuItem = nodeLast->GetData(); + menu->Delete(lastMenuItem); + } + } + } +} + #endif // wxUSE_FILE_HISTORY