KiCad PCB EDA Suite
filehistory.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 <Ian.S.McInerney@ieee.org>
5  * Copyright (C) 2019 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 <filehistory.h>
26 #include <id.h>
27 #include <settings/app_settings.h>
28 #include <tool/action_menu.h>
30 #include <wx/menu.h>
31 
32 #include <functional>
33 using namespace std::placeholders;
34 
35 
36 FILE_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 
45 void 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 
58 void 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 
76 void 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 
85 void 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 
96 void 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 
119 void FILE_HISTORY::AddFilesToMenu( wxMenu* aMenu )
120 {
121  doRemoveClearitem( aMenu );
122  wxFileHistory::AddFilesToMenu( aMenu );
123  doAddClearItem( aMenu );
124 }
125 
126 
127 void FILE_HISTORY::doRemoveClearitem( wxMenu* aMenu )
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 
150 void 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 
169 {
170  while( GetCount() > 0 )
171  RemoveFileFromHistory( 0 );
172 }
173 
174 
176 {
177  return std::bind( &FILE_HISTORY::isHistoryNotEmpty, _1, std::cref( aHistory ) );
178 }
179 
180 
181 bool FILE_HISTORY::isHistoryNotEmpty( const SELECTION& aSelection, const FILE_HISTORY& aHistory )
182 {
183  return aHistory.GetCount() != 0;
184 }
void AddFilesToMenu() override
Add the files to all registered menus.
Definition: filehistory.h:98
std::vector< wxString > file_history
Definition: app_settings.h:146
static bool isHistoryNotEmpty(const SELECTION &aSelection, const FILE_HISTORY &aHistory)
Test if the file history is empty.
void Save(APP_SETTINGS_BASE &aSettings)
Saves history into a JSON settings object.
Definition: filehistory.cpp:67
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:58
void Load(const APP_SETTINGS_BASE &aSettings)
Loads history from a JSON settings object.
Definition: filehistory.cpp:45
This class implements a file history object to store a list of files, that can then be added to a men...
Definition: filehistory.h:42
void ClearFileHistory()
Clear all entries from the file history.
wxString m_clearText
Definition: filehistory.h:168
void AddFileToHistory(const wxString &aFile) override
Adds a file to the history.
Definition: filehistory.cpp:96
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.
Definition: filehistory.cpp:36
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:99
void doRemoveClearitem(wxMenu *aMenu)
Remove the clear menu item and the preceding separator from the given menu.
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.
void SetMaxFiles(size_t aMaxFiles)
Update the number of files that will be contained inside the file history.
Definition: filehistory.cpp:85
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...
#define _(s)
Definition: 3d_actions.cpp:33
#define MAX_FILE_HISTORY_SIZE
Definition: id.h:70
void doAddClearItem(wxMenu *aMenu)
Add the clear menu item and the preceding separator to the given menu.