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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <file_history.h>
22#include <id.h>
24#include <tool/action_menu.h>
26#include <wx/menu.h>
27
28#include <functional>
29using namespace std::placeholders;
30
31
32FILE_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
40
41void FILE_HISTORY::Load( const APP_SETTINGS_BASE& aSettings )
42{
44
45 // file_history stores the most recent file first
46 for( auto it = aSettings.m_System.file_history.rbegin();
47 it != aSettings.m_System.file_history.rend(); ++it )
48 {
49 AddFileToHistory( *it );
50 }
51}
52
53
54void FILE_HISTORY::Load( const std::vector<wxString>& aList )
55{
57
58 for( const auto& file : aList )
59 AddFileToHistory( file );
60}
61
62
64{
65 aSettings.m_System.file_history.clear();
66
67 for( const wxString& filename : m_fileHistory )
68 aSettings.m_System.file_history.emplace_back( filename );
69}
70
71
72void FILE_HISTORY::Save( std::vector<wxString>* aList )
73{
74 aList->clear();
75
76 for( const auto& file : m_fileHistory )
77 aList->push_back( file );
78}
79
80
81void FILE_HISTORY::SetMaxFiles( size_t aMaxFiles )
82{
83 m_fileMaxFiles = std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE );
84
85 size_t numFiles = m_fileHistory.size();
86
87 while( numFiles > m_fileMaxFiles )
88 RemoveFileFromHistory( --numFiles );
89}
90
91
92void FILE_HISTORY::AddFileToHistory( const wxString &aFile )
93{
94 // Iterate over each menu removing our custom items
95 for( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
96 node; node = node->GetNext() )
97 {
98 wxMenu* menu = static_cast<wxMenu*>( node->GetData() );
99 doRemoveClearitem( menu );
100 }
101
102 // Let wx add the items in the file history
103 wxFileHistory::AddFileToHistory( aFile );
104
105 // Add our custom items back
106 for( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
107 node; node = node->GetNext() )
108 {
109 wxMenu* menu = static_cast<wxMenu*>( node->GetData() );
110 doAddClearItem( menu );
111 }
112}
113
114
115void FILE_HISTORY::AddFilesToMenu( wxMenu* aMenu )
116{
117 doRemoveClearitem( aMenu );
118 wxFileHistory::AddFilesToMenu( aMenu );
119 doAddClearItem( aMenu );
120}
121
122
124{
125 size_t itemPos;
126 wxMenuItem* clearItem = aMenu->FindChildItem( m_clearId, &itemPos );
127
128 // Remove the separator if there is one
129 if( clearItem && itemPos > 1 )
130 {
131 wxMenuItem* sepItem = aMenu->FindItemByPosition( itemPos - 1 );
132
133 if( sepItem )
134 aMenu->Destroy( sepItem );
135 }
136
137 // Remove the clear and placeholder menu items
138 if( clearItem )
139 aMenu->Destroy( m_clearId );
140
141 if( aMenu->FindChildItem( ID_FILE_LIST_EMPTY ) )
142 aMenu->Destroy( ID_FILE_LIST_EMPTY );
143}
144
145
146void FILE_HISTORY::doAddClearItem( wxMenu* aMenu )
147{
148 if( GetCount() == 0 )
149 {
150 // If the history is empty, we create an item to say there are no files
151 wxMenuItem* item = new wxMenuItem( nullptr, ID_FILE_LIST_EMPTY, _( "No Files" ) );
152
153 aMenu->Append( item );
154 aMenu->Enable( item->GetId(), false );
155 }
156
157 wxMenuItem* clearItem = new wxMenuItem( nullptr, m_clearId, m_clearText );
158
159 aMenu->AppendSeparator();
160 aMenu->Append( clearItem );
161}
162
163
164void FILE_HISTORY::UpdateClearText( wxMenu* aMenu, wxString aClearText )
165{
166 size_t itemPos;
167 wxMenuItem* clearItem = aMenu->FindChildItem( m_clearId, &itemPos );
168
169 if( clearItem && itemPos > 1 ) // clearItem is the last menu, after a separator
170 {
171 clearItem->SetItemLabel( aClearText );
172 }
173}
174
175
177{
178 while( GetCount() > 0 )
179 RemoveFileFromHistory( 0 );
180}
181
182
184{
185 return std::bind( &FILE_HISTORY::isHistoryNotEmpty, _1, std::cref( aHistory ) );
186}
187
188
189bool FILE_HISTORY::isHistoryNotEmpty( const SELECTION& aSelection, const FILE_HISTORY& aHistory )
190{
191 return aHistory.GetCount() != 0;
192}
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
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
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.
#define _(s)
#define MAX_FILE_HISTORY_SIZE
Definition id.h:46
@ ID_FILE_LIST_EMPTY
Definition id.h:57
STL namespace.
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.
std::vector< wxString > file_history