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 <tool/action_menu.h>
29 #include <wx/menu.h>
30 
31 #include <functional>
32 using namespace std::placeholders;
33 
34 
35 FILE_HISTORY::FILE_HISTORY( size_t aMaxFiles, int aBaseFileId ) :
36  wxFileHistory( std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE ) )
37 {
38  SetBaseId( aBaseFileId );
39 }
40 
41 
42 void FILE_HISTORY::SetMaxFiles( size_t aMaxFiles )
43 {
44  m_fileMaxFiles = std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE );
45 
46  size_t numFiles = m_fileHistory.size();
47 
48  while( numFiles > m_fileMaxFiles )
49  RemoveFileFromHistory( --numFiles );
50 }
51 
52 
53 void FILE_HISTORY::AddFileToHistory( const wxString &aFile )
54 {
55  wxFileHistory::AddFileToHistory( aFile );
56 
57  // Iterate over each menu associated with this file history, and if it is one of our
58  // FILE_HISTORY_MENUs, we force it to be refreshed (so that the items are all in the
59  // correct locations).
60  for( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
61  node; node = node->GetNext() )
62  {
63  wxMenu* menu = static_cast<wxMenu*>( node->GetData() );
64 
65  FILE_HISTORY_MENU* fileMenu = dynamic_cast<FILE_HISTORY_MENU*>( menu );
66 
67  if( fileMenu )
68  fileMenu->RefreshMenu();
69  }
70 }
71 
72 
74 {
75  return std::bind( &FILE_HISTORY::isHistoryNotEmpty, _1, std::cref( aHistory ) );
76 }
77 
78 
79 bool FILE_HISTORY::isHistoryNotEmpty( const SELECTION& aSelection, const FILE_HISTORY& aHistory )
80 {
81  return aHistory.GetCount() != 0;
82 }
83 
84 
85 FILE_HISTORY_MENU::FILE_HISTORY_MENU( FILE_HISTORY& aHistory, wxString aClearText ) :
86  ACTION_MENU( false ),
87  m_fileHistory( aHistory ),
88  m_clearText( aClearText )
89 {
90  m_fileHistory.UseMenu( this );
91  buildMenu();
92 }
93 
94 
96 {
97  m_fileHistory.RemoveMenu( this );
98 }
99 
100 
102 {
103  // We have to manually delete all menu items before we rebuild the menu
104  for( int i = GetMenuItemCount() - 1; i >= 0; --i )
105  Destroy( FindItemByPosition( i ) );
106 
107  buildMenu();
108 }
109 
110 
112 {
113  if( m_fileHistory.GetCount() == 0 )
114  {
115  // If the history is empty, we create an item to say there are no files
116  wxMenuItem* item = new wxMenuItem( this, wxID_ANY, _( "No Files" ) );
117 
118  Append( item );
119  Enable( item->GetId(), false );
120  }
121  else
122  m_fileHistory.AddFilesToMenu( this );
123 
124  wxMenuItem* clearItem = new wxMenuItem( this, ID_FILE_LIST_CLEAR, m_clearText );
125 
126  AppendSeparator();
127  Append( clearItem );
128  Connect( ID_FILE_LIST_CLEAR, wxEVT_COMMAND_MENU_SELECTED,
129  wxMenuEventHandler( FILE_HISTORY_MENU::onClearEntries ), NULL, this );
130 }
131 
132 
133 void FILE_HISTORY_MENU::onClearEntries( wxMenuEvent& aEvent )
134 {
135  while( m_fileHistory.GetCount() > 0 )
136  m_fileHistory.RemoveFileFromHistory( 0 );
137 
138  RefreshMenu();
139 }
140 
141 
143 {
145 }
FILE_HISTORY(size_t aMaxFiles, int aBaseFileId)
Create a file history object to store a list of files and add them to a menu.
Definition: filehistory.cpp:35
static bool isHistoryNotEmpty(const SELECTION &aSelection, const FILE_HISTORY &aHistory)
Definition: filehistory.cpp:79
ACTION_MENU.
Definition: action_menu.h:43
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:55
This class implements a menu container for a file history.
Definition: filehistory.h:86
This class implements a file history object to store a list of files, that can then be added to a men...
Definition: filehistory.h:40
FILE_HISTORY & m_fileHistory
Definition: filehistory.h:121
void AddFileToHistory(const wxString &aFile) override
Adds a file to the history.
Definition: filehistory.cpp:53
#define NULL
wxString m_clearText
Definition: filehistory.h:122
ACTION_MENU * create() const override
Returns an instance of this class. It has to be overridden in inheriting classes.
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.
void onClearEntries(wxMenuEvent &aEvent)
Event handler for when the clear menu item is activated.
void SetMaxFiles(size_t aMaxFiles)
Update the number of files that will be contained inside the file history.
Definition: filehistory.cpp:42
FILE_HISTORY_MENU(FILE_HISTORY &aHistory, wxString aClearText=_("Clear Recent Files"))
Create the file history menu.
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...
Definition: filehistory.cpp:73
#define _(s)
Definition: 3d_actions.cpp:31
void buildMenu()
Construct the menu by adding the file history and menu items.
#define MAX_FILE_HISTORY_SIZE
Definition: id.h:70
void RefreshMenu()
Refresh the menu.