KiCad PCB EDA Suite
Loading...
Searching...
No Matches
file_history.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2019 Ian McInerney <[email protected]>
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
25#include <file_history.h>
26#include <id.h>
28#include <tool/action_menu.h>
30#include <wx/menu.h>
31
32#include <functional>
33using namespace std::placeholders;
34
35
36FILE_HISTORY::FILE_HISTORY( size_t aMaxFiles, int aBaseFileId, int aClearId, wxString aClearText )
37 : wxFileHistory( std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE ) ),
38 m_clearId( aClearId ),
39 m_clearText( aClearText )
40{
41 SetBaseId( aBaseFileId );
42}
43
44
45void FILE_HISTORY::Load( const APP_SETTINGS_BASE& aSettings )
46{
48
49 // file_history stores the most recent file first
50 for( auto it = aSettings.m_System.file_history.rbegin();
51 it != aSettings.m_System.file_history.rend(); ++it )
52 {
53 AddFileToHistory( *it );
54 }
55}
56
57
58void FILE_HISTORY::Load( const std::vector<wxString>& aList )
59{
61
62 for( const auto& file : aList )
63 AddFileToHistory( file );
64}
65
66
68{
69 aSettings.m_System.file_history.clear();
70
71 for( const wxString& filename : m_fileHistory )
72 aSettings.m_System.file_history.emplace_back( filename );
73}
74
75
76void FILE_HISTORY::Save( std::vector<wxString>* aList )
77{
78 aList->clear();
79
80 for( const auto& file : m_fileHistory )
81 aList->push_back( file );
82}
83
84
85void FILE_HISTORY::SetMaxFiles( size_t aMaxFiles )
86{
87 m_fileMaxFiles = std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE );
88
89 size_t numFiles = m_fileHistory.size();
90
91 while( numFiles > m_fileMaxFiles )
92 RemoveFileFromHistory( --numFiles );
93}
94
95
96void FILE_HISTORY::AddFileToHistory( const wxString &aFile )
97{
98 // Iterate over each menu removing our custom items
99 for( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
100 node; node = node->GetNext() )
101 {
102 wxMenu* menu = static_cast<wxMenu*>( node->GetData() );
103 doRemoveClearitem( menu );
104 }
105
106 // Let wx add the items in the file history
107 wxFileHistory::AddFileToHistory( aFile );
108
109 // Add our custom items back
110 for( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
111 node; node = node->GetNext() )
112 {
113 wxMenu* menu = static_cast<wxMenu*>( node->GetData() );
114 doAddClearItem( menu );
115 }
116}
117
118
119void FILE_HISTORY::AddFilesToMenu( wxMenu* aMenu )
120{
121 doRemoveClearitem( aMenu );
122 wxFileHistory::AddFilesToMenu( aMenu );
123 doAddClearItem( aMenu );
124}
125
126
128{
129 size_t itemPos;
130 wxMenuItem* clearItem = aMenu->FindChildItem( m_clearId, &itemPos );
131
132 // Remove the separator if there is one
133 if( clearItem && itemPos > 1 )
134 {
135 wxMenuItem* sepItem = aMenu->FindItemByPosition( itemPos - 1 );
136
137 if( sepItem )
138 aMenu->Destroy( sepItem );
139 }
140
141 // Remove the clear and placeholder menu items
142 if( clearItem )
143 aMenu->Destroy( m_clearId );
144
145 if( aMenu->FindChildItem( ID_FILE_LIST_EMPTY ) )
146 aMenu->Destroy( ID_FILE_LIST_EMPTY );
147}
148
149
150void FILE_HISTORY::doAddClearItem( wxMenu* aMenu )
151{
152 if( GetCount() == 0 )
153 {
154 // If the history is empty, we create an item to say there are no files
155 wxMenuItem* item = new wxMenuItem( nullptr, ID_FILE_LIST_EMPTY, _( "No Files" ) );
156
157 aMenu->Append( item );
158 aMenu->Enable( item->GetId(), false );
159 }
160
161 wxMenuItem* clearItem = new wxMenuItem( nullptr, m_clearId, m_clearText );
162
163 aMenu->AppendSeparator();
164 aMenu->Append( clearItem );
165}
166
167
168void FILE_HISTORY::UpdateClearText( wxMenu* aMenu, wxString aClearText )
169{
170 size_t itemPos;
171 wxMenuItem* clearItem = aMenu->FindChildItem( m_clearId, &itemPos );
172
173 if( clearItem && itemPos > 1 ) // clearItem is the last menu, after a separator
174 {
175 clearItem->SetItemLabel( aClearText );
176 }
177}
178
179
181{
182 while( GetCount() > 0 )
183 RemoveFileFromHistory( 0 );
184}
185
186
188{
189 return std::bind( &FILE_HISTORY::isHistoryNotEmpty, _1, std::cref( aHistory ) );
190}
191
192
193bool FILE_HISTORY::isHistoryNotEmpty( const SELECTION& aSelection, const FILE_HISTORY& aHistory )
194{
195 return aHistory.GetCount() != 0;
196}
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:92
This class implements a file history object to store a list of files, that can then be added to a men...
Definition: file_history.h:43
FILE_HISTORY(size_t aMaxFiles, int aBaseFileId, int aClearId, wxString aClearText=_("Clear Recent Files"))
Create a file history object to store a list of files and add them to a menu.
void AddFileToHistory(const wxString &aFile) override
Adds a file to the history.
void doAddClearItem(wxMenu *aMenu)
Add the clear menu item and the preceding separator to the given menu.
static bool isHistoryNotEmpty(const SELECTION &aSelection, const FILE_HISTORY &aHistory)
Test if the file history is empty.
static SELECTION_CONDITION FileHistoryNotEmpty(const FILE_HISTORY &aHistory)
Create a SELECTION_CONDITION that can be used to enable a menu item when the file history has items i...
void UpdateClearText(wxMenu *aMenu, wxString aClearText)
Update the text displayed on the menu item that clears the entire menu.
void doRemoveClearitem(wxMenu *aMenu)
Remove the clear menu item and the preceding separator from the given menu.
wxString m_clearText
Definition: file_history.h:177
void ClearFileHistory()
Clear all entries from the file history.
void Save(APP_SETTINGS_BASE &aSettings)
Saves history into a JSON settings object.
void SetMaxFiles(size_t aMaxFiles)
Update the number of files that will be contained inside the file history.
void Load(const APP_SETTINGS_BASE &aSettings)
Loads history from a JSON settings object.
void AddFilesToMenu() override
Add the files to all registered menus.
Definition: file_history.h:98
#define _(s)
#define MAX_FILE_HISTORY_SIZE
Definition: id.h:70
@ ID_FILE_LIST_EMPTY
Definition: id.h:86
STL namespace.
std::function< bool(const SELECTION &)> SELECTION_CONDITION
< Functor type that checks a specific condition for selected items.
std::vector< wxString > file_history
Definition: app_settings.h:141