2019-11-23 16:54:48 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Ian McInerney <Ian.S.McInerney@ieee.org>
|
2022-04-11 07:11:26 +00:00
|
|
|
* Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2019-11-23 16:54:48 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <filehistory.h>
|
|
|
|
#include <id.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <settings/app_settings.h>
|
2019-11-23 16:54:48 +00:00
|
|
|
#include <tool/action_menu.h>
|
|
|
|
#include <tool/selection_conditions.h>
|
|
|
|
#include <wx/menu.h>
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
using namespace std::placeholders;
|
|
|
|
|
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
FILE_HISTORY::FILE_HISTORY( size_t aMaxFiles, int aBaseFileId, int aClearId, wxString aClearText )
|
|
|
|
: wxFileHistory( std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE ) ),
|
|
|
|
m_clearId( aClearId ),
|
|
|
|
m_clearText( aClearText )
|
2019-11-23 16:54:48 +00:00
|
|
|
{
|
|
|
|
SetBaseId( aBaseFileId );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
void FILE_HISTORY::Load( const APP_SETTINGS_BASE& aSettings )
|
|
|
|
{
|
2020-07-08 03:42:47 +00:00
|
|
|
ClearFileHistory();
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
// file_history stores the most recent file first
|
|
|
|
for( auto it = aSettings.m_System.file_history.rbegin();
|
|
|
|
it != aSettings.m_System.file_history.rend(); ++it )
|
2020-08-05 13:48:03 +00:00
|
|
|
{
|
2020-01-13 01:44:19 +00:00
|
|
|
AddFileToHistory( *it );
|
2020-08-05 13:48:03 +00:00
|
|
|
}
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FILE_HISTORY::Load( const std::vector<wxString>& aList )
|
|
|
|
{
|
2020-07-08 03:42:47 +00:00
|
|
|
ClearFileHistory();
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
for( const auto& file : aList )
|
|
|
|
AddFileToHistory( file );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FILE_HISTORY::Save( APP_SETTINGS_BASE& aSettings )
|
|
|
|
{
|
|
|
|
aSettings.m_System.file_history.clear();
|
|
|
|
|
2020-08-05 13:48:03 +00:00
|
|
|
for( const wxString& filename : m_fileHistory )
|
|
|
|
aSettings.m_System.file_history.emplace_back( filename );
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FILE_HISTORY::Save( std::vector<wxString>* aList )
|
|
|
|
{
|
|
|
|
aList->clear();
|
|
|
|
|
|
|
|
for( const auto& file : m_fileHistory )
|
|
|
|
aList->push_back( file );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-23 16:54:48 +00:00
|
|
|
void FILE_HISTORY::SetMaxFiles( size_t aMaxFiles )
|
|
|
|
{
|
|
|
|
m_fileMaxFiles = std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE );
|
|
|
|
|
|
|
|
size_t numFiles = m_fileHistory.size();
|
|
|
|
|
|
|
|
while( numFiles > m_fileMaxFiles )
|
|
|
|
RemoveFileFromHistory( --numFiles );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FILE_HISTORY::AddFileToHistory( const wxString &aFile )
|
|
|
|
{
|
2020-02-25 15:46:56 +00:00
|
|
|
// Iterate over each menu removing our custom items
|
2019-11-23 16:54:48 +00:00
|
|
|
for( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
|
|
|
|
node; node = node->GetNext() )
|
|
|
|
{
|
|
|
|
wxMenu* menu = static_cast<wxMenu*>( node->GetData() );
|
2020-02-25 15:46:56 +00:00
|
|
|
doRemoveClearitem( menu );
|
2019-11-23 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
// Let wx add the items in the file history
|
|
|
|
wxFileHistory::AddFileToHistory( aFile );
|
2019-11-23 16:54:48 +00:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
// Add our custom items back
|
|
|
|
for( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
|
|
|
|
node; node = node->GetNext() )
|
|
|
|
{
|
|
|
|
wxMenu* menu = static_cast<wxMenu*>( node->GetData() );
|
|
|
|
doAddClearItem( menu );
|
|
|
|
}
|
2019-11-23 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
void FILE_HISTORY::AddFilesToMenu( wxMenu* aMenu )
|
2019-11-23 16:54:48 +00:00
|
|
|
{
|
2020-02-25 15:46:56 +00:00
|
|
|
doRemoveClearitem( aMenu );
|
|
|
|
wxFileHistory::AddFilesToMenu( aMenu );
|
|
|
|
doAddClearItem( aMenu );
|
2019-11-23 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
void FILE_HISTORY::doRemoveClearitem( wxMenu* aMenu )
|
2019-11-23 16:54:48 +00:00
|
|
|
{
|
2020-02-25 15:46:56 +00:00
|
|
|
size_t itemPos;
|
|
|
|
wxMenuItem* clearItem = aMenu->FindChildItem( m_clearId, &itemPos );
|
2019-11-23 16:54:48 +00:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
// Remove the separator if there is one
|
|
|
|
if( clearItem && itemPos > 1 )
|
|
|
|
{
|
|
|
|
wxMenuItem* sepItem = aMenu->FindItemByPosition( itemPos - 1 );
|
2019-11-23 16:54:48 +00:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
if( sepItem )
|
|
|
|
aMenu->Destroy( sepItem );
|
|
|
|
}
|
2019-11-23 16:54:48 +00:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
// Remove the clear and placeholder menu items
|
|
|
|
if( clearItem )
|
|
|
|
aMenu->Destroy( m_clearId );
|
2019-11-23 16:54:48 +00:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
if( aMenu->FindChildItem( ID_FILE_LIST_EMPTY ) )
|
|
|
|
aMenu->Destroy( ID_FILE_LIST_EMPTY );
|
2019-11-23 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
void FILE_HISTORY::doAddClearItem( wxMenu* aMenu )
|
2019-11-23 16:54:48 +00:00
|
|
|
{
|
2020-02-25 15:46:56 +00:00
|
|
|
if( GetCount() == 0 )
|
2019-11-23 16:54:48 +00:00
|
|
|
{
|
|
|
|
// If the history is empty, we create an item to say there are no files
|
2020-02-25 15:46:56 +00:00
|
|
|
wxMenuItem* item = new wxMenuItem( nullptr, ID_FILE_LIST_EMPTY, _( "No Files" ) );
|
2019-11-23 16:54:48 +00:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
aMenu->Append( item );
|
|
|
|
aMenu->Enable( item->GetId(), false );
|
2019-11-23 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
wxMenuItem* clearItem = new wxMenuItem( nullptr, m_clearId, m_clearText );
|
2019-11-23 16:54:48 +00:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
aMenu->AppendSeparator();
|
|
|
|
aMenu->Append( clearItem );
|
2019-11-23 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-11 07:11:26 +00:00
|
|
|
void FILE_HISTORY::UpdateClearText( wxMenu* aMenu, wxString aClearText )
|
|
|
|
{
|
|
|
|
size_t itemPos;
|
|
|
|
wxMenuItem* clearItem = aMenu->FindChildItem( m_clearId, &itemPos );
|
|
|
|
|
|
|
|
if( clearItem && itemPos > 1 ) // clearItem is the last menu, after a separator
|
|
|
|
{
|
|
|
|
clearItem->SetItemLabel( aClearText );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
void FILE_HISTORY::ClearFileHistory()
|
2019-11-23 16:54:48 +00:00
|
|
|
{
|
2020-02-25 15:46:56 +00:00
|
|
|
while( GetCount() > 0 )
|
|
|
|
RemoveFileFromHistory( 0 );
|
|
|
|
}
|
|
|
|
|
2019-11-23 16:54:48 +00:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
SELECTION_CONDITION FILE_HISTORY::FileHistoryNotEmpty( const FILE_HISTORY& aHistory )
|
|
|
|
{
|
|
|
|
return std::bind( &FILE_HISTORY::isHistoryNotEmpty, _1, std::cref( aHistory ) );
|
2019-11-23 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
bool FILE_HISTORY::isHistoryNotEmpty( const SELECTION& aSelection, const FILE_HISTORY& aHistory )
|
2019-11-23 16:54:48 +00:00
|
|
|
{
|
2020-02-25 15:46:56 +00:00
|
|
|
return aHistory.GetCount() != 0;
|
2019-11-23 16:54:48 +00:00
|
|
|
}
|