You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.3 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 Ian McInerney <Ian.S.McInerney@ieee.org>
  5. * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <file_history.h>
  25. #include <id.h>
  26. #include <settings/app_settings.h>
  27. #include <tool/action_menu.h>
  28. #include <tool/selection_conditions.h>
  29. #include <wx/menu.h>
  30. #include <functional>
  31. using namespace std::placeholders;
  32. FILE_HISTORY::FILE_HISTORY( size_t aMaxFiles, int aBaseFileId, int aClearId, wxString aClearText )
  33. : wxFileHistory( std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE ) ),
  34. m_clearId( aClearId ),
  35. m_clearText( aClearText )
  36. {
  37. SetBaseId( aBaseFileId );
  38. }
  39. void FILE_HISTORY::Load( const APP_SETTINGS_BASE& aSettings )
  40. {
  41. ClearFileHistory();
  42. // file_history stores the most recent file first
  43. for( auto it = aSettings.m_System.file_history.rbegin();
  44. it != aSettings.m_System.file_history.rend(); ++it )
  45. {
  46. AddFileToHistory( *it );
  47. }
  48. }
  49. void FILE_HISTORY::Load( const std::vector<wxString>& aList )
  50. {
  51. ClearFileHistory();
  52. for( const auto& file : aList )
  53. AddFileToHistory( file );
  54. }
  55. void FILE_HISTORY::Save( APP_SETTINGS_BASE& aSettings )
  56. {
  57. aSettings.m_System.file_history.clear();
  58. for( const wxString& filename : m_fileHistory )
  59. aSettings.m_System.file_history.emplace_back( filename );
  60. }
  61. void FILE_HISTORY::Save( std::vector<wxString>* aList )
  62. {
  63. aList->clear();
  64. for( const auto& file : m_fileHistory )
  65. aList->push_back( file );
  66. }
  67. void FILE_HISTORY::SetMaxFiles( size_t aMaxFiles )
  68. {
  69. m_fileMaxFiles = std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE );
  70. size_t numFiles = m_fileHistory.size();
  71. while( numFiles > m_fileMaxFiles )
  72. RemoveFileFromHistory( --numFiles );
  73. }
  74. void FILE_HISTORY::AddFileToHistory( const wxString &aFile )
  75. {
  76. // Iterate over each menu removing our custom items
  77. for( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
  78. node; node = node->GetNext() )
  79. {
  80. wxMenu* menu = static_cast<wxMenu*>( node->GetData() );
  81. doRemoveClearitem( menu );
  82. }
  83. // Let wx add the items in the file history
  84. wxFileHistory::AddFileToHistory( aFile );
  85. // Add our custom items back
  86. for( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
  87. node; node = node->GetNext() )
  88. {
  89. wxMenu* menu = static_cast<wxMenu*>( node->GetData() );
  90. doAddClearItem( menu );
  91. }
  92. }
  93. void FILE_HISTORY::AddFilesToMenu( wxMenu* aMenu )
  94. {
  95. doRemoveClearitem( aMenu );
  96. wxFileHistory::AddFilesToMenu( aMenu );
  97. doAddClearItem( aMenu );
  98. }
  99. void FILE_HISTORY::doRemoveClearitem( wxMenu* aMenu )
  100. {
  101. size_t itemPos;
  102. wxMenuItem* clearItem = aMenu->FindChildItem( m_clearId, &itemPos );
  103. // Remove the separator if there is one
  104. if( clearItem && itemPos > 1 )
  105. {
  106. wxMenuItem* sepItem = aMenu->FindItemByPosition( itemPos - 1 );
  107. if( sepItem )
  108. aMenu->Destroy( sepItem );
  109. }
  110. // Remove the clear and placeholder menu items
  111. if( clearItem )
  112. aMenu->Destroy( m_clearId );
  113. if( aMenu->FindChildItem( ID_FILE_LIST_EMPTY ) )
  114. aMenu->Destroy( ID_FILE_LIST_EMPTY );
  115. }
  116. void FILE_HISTORY::doAddClearItem( wxMenu* aMenu )
  117. {
  118. if( GetCount() == 0 )
  119. {
  120. // If the history is empty, we create an item to say there are no files
  121. wxMenuItem* item = new wxMenuItem( nullptr, ID_FILE_LIST_EMPTY, _( "No Files" ) );
  122. aMenu->Append( item );
  123. aMenu->Enable( item->GetId(), false );
  124. }
  125. wxMenuItem* clearItem = new wxMenuItem( nullptr, m_clearId, m_clearText );
  126. aMenu->AppendSeparator();
  127. aMenu->Append( clearItem );
  128. }
  129. void FILE_HISTORY::UpdateClearText( wxMenu* aMenu, wxString aClearText )
  130. {
  131. size_t itemPos;
  132. wxMenuItem* clearItem = aMenu->FindChildItem( m_clearId, &itemPos );
  133. if( clearItem && itemPos > 1 ) // clearItem is the last menu, after a separator
  134. {
  135. clearItem->SetItemLabel( aClearText );
  136. }
  137. }
  138. void FILE_HISTORY::ClearFileHistory()
  139. {
  140. while( GetCount() > 0 )
  141. RemoveFileFromHistory( 0 );
  142. }
  143. SELECTION_CONDITION FILE_HISTORY::FileHistoryNotEmpty( const FILE_HISTORY& aHistory )
  144. {
  145. return std::bind( &FILE_HISTORY::isHistoryNotEmpty, _1, std::cref( aHistory ) );
  146. }
  147. bool FILE_HISTORY::isHistoryNotEmpty( const SELECTION& aSelection, const FILE_HISTORY& aHistory )
  148. {
  149. return aHistory.GetCount() != 0;
  150. }