KiCad PCB EDA Suite
FILE_HISTORY Class Reference

This class implements a file history object to store a list of files, that can then be added to a menu. More...

#include <file_history.h>

Inheritance diagram for FILE_HISTORY:

Public Member Functions

 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. More...
 
void Load (const APP_SETTINGS_BASE &aSettings)
 Loads history from a JSON settings object. More...
 
void Load (const std::vector< wxString > &aList)
 Loads history from a list of strings. More...
 
void Save (APP_SETTINGS_BASE &aSettings)
 Saves history into a JSON settings object. More...
 
void Save (std::vector< wxString > *aList)
 Saves history into a list of strings. More...
 
void AddFileToHistory (const wxString &aFile) override
 Adds a file to the history. More...
 
void AddFilesToMenu () override
 Add the files to all registered menus. More...
 
void AddFilesToMenu (wxMenu *aMenu) override
 Add the files to the specified menu. More...
 
void SetMaxFiles (size_t aMaxFiles)
 Update the number of files that will be contained inside the file history. More...
 
void SetClearText (wxString aClearText)
 Set the text displayed on the menu item that clears the entire menu. More...
 
void UpdateClearText (wxMenu *aMenu, wxString aClearText)
 Update the text displayed on the menu item that clears the entire menu. More...
 
void ClearFileHistory ()
 Clear all entries from the file history. More...
 

Static Public Member Functions

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 in it. More...
 

Protected Member Functions

void doRemoveClearitem (wxMenu *aMenu)
 Remove the clear menu item and the preceding separator from the given menu. More...
 
void doAddClearItem (wxMenu *aMenu)
 Add the clear menu item and the preceding separator to the given menu. More...
 

Static Private Member Functions

static bool isHistoryNotEmpty (const SELECTION &aSelection, const FILE_HISTORY &aHistory)
 Test if the file history is empty. More...
 

Private Attributes

int m_clearId
 
wxString m_clearText
 

Detailed Description

This class implements a file history object to store a list of files, that can then be added to a menu.

This class extends the wxWidgets wxFileHistory class to include KiCad specific items.

Definition at line 42 of file file_history.h.

Constructor & Destructor Documentation

◆ FILE_HISTORY()

FILE_HISTORY::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.

Parameters
aMaxFilesis the number of files to store in the history
aBaseFileIdis the ID to use for the first file menu item
aClearIdis the ID to use for the clear menu menu item
aClearTextis the text to use for the menu item that clears the history.

Definition at line 36 of file file_history.cpp.

37 : wxFileHistory( std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE ) ),
38 m_clearId( aClearId ),
39 m_clearText( aClearText )
40{
41 SetBaseId( aBaseFileId );
42}
wxString m_clearText
Definition: file_history.h:177
#define MAX_FILE_HISTORY_SIZE
Definition: id.h:70

Member Function Documentation

◆ AddFilesToMenu() [1/2]

void FILE_HISTORY::AddFilesToMenu ( )
inlineoverride

Add the files to all registered menus.

Definition at line 98 of file file_history.h.

99 {
100 // This is needed to ensure that the proper base class function is called
101 wxFileHistory::AddFilesToMenu();
102 }

Referenced by SCH_EDIT_FRAME::doReCreateMenuBar(), GERBVIEW_FRAME::doReCreateMenuBar(), KICAD_MANAGER_FRAME::doReCreateMenuBar(), PL_EDITOR_FRAME::doReCreateMenuBar(), and PCB_EDIT_FRAME::doReCreateMenuBar().

◆ AddFilesToMenu() [2/2]

void FILE_HISTORY::AddFilesToMenu ( wxMenu *  aMenu)
override

Add the files to the specified menu.

Parameters
aMenuis the menu to operate on.

Definition at line 119 of file file_history.cpp.

120{
121 doRemoveClearitem( aMenu );
122 wxFileHistory::AddFilesToMenu( aMenu );
123 doAddClearItem( aMenu );
124}
void doAddClearItem(wxMenu *aMenu)
Add the clear menu item and the preceding separator to the given menu.
void doRemoveClearitem(wxMenu *aMenu)
Remove the clear menu item and the preceding separator from the given menu.

References doAddClearItem(), and doRemoveClearitem().

◆ AddFileToHistory()

void FILE_HISTORY::AddFileToHistory ( const wxString &  aFile)
override

Adds a file to the history.

This function overrides the default wxWidgets method to iterate through all menus associated with the file history, remove the added menu items, lets wx add the new files, and then re-adds the clear menu item.

Parameters
aFileis the filename of the file to add to the history.

Definition at line 96 of file file_history.cpp.

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}

References doAddClearItem(), and doRemoveClearitem().

Referenced by Load(), and EDA_BASE_FRAME::UpdateFileHistory().

◆ ClearFileHistory()

void FILE_HISTORY::ClearFileHistory ( )

Clear all entries from the file history.

Definition at line 180 of file file_history.cpp.

181{
182 while( GetCount() > 0 )
183 RemoveFileFromHistory( 0 );
184}

Referenced by EDA_BASE_FRAME::ClearFileHistory(), and Load().

◆ doAddClearItem()

void FILE_HISTORY::doAddClearItem ( wxMenu *  aMenu)
protected

Add the clear menu item and the preceding separator to the given menu.

Parameters
aMenuis the menu to operate on

Definition at line 150 of file file_history.cpp.

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}
#define _(s)
@ ID_FILE_LIST_EMPTY
Definition: id.h:86

References _, ID_FILE_LIST_EMPTY, m_clearId, and m_clearText.

Referenced by AddFilesToMenu(), and AddFileToHistory().

◆ doRemoveClearitem()

void FILE_HISTORY::doRemoveClearitem ( wxMenu *  aMenu)
protected

Remove the clear menu item and the preceding separator from the given menu.

Parameters
aMenuis the menu to operate on

Definition at line 127 of file file_history.cpp.

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}

References ID_FILE_LIST_EMPTY, and m_clearId.

Referenced by AddFilesToMenu(), and AddFileToHistory().

◆ FileHistoryNotEmpty()

SELECTION_CONDITION FILE_HISTORY::FileHistoryNotEmpty ( const FILE_HISTORY aHistory)
static

Create a SELECTION_CONDITION that can be used to enable a menu item when the file history has items in it.

Parameters
aHistoryis the file history to check for items
Returns
the selection condition function

Definition at line 187 of file file_history.cpp.

188{
189 return std::bind( &FILE_HISTORY::isHistoryNotEmpty, _1, std::cref( aHistory ) );
190}
static bool isHistoryNotEmpty(const SELECTION &aSelection, const FILE_HISTORY &aHistory)
Test if the file history is empty.

References isHistoryNotEmpty().

Referenced by SCH_EDIT_FRAME::doReCreateMenuBar(), KICAD_MANAGER_FRAME::doReCreateMenuBar(), PL_EDITOR_FRAME::doReCreateMenuBar(), and PCB_EDIT_FRAME::doReCreateMenuBar().

◆ isHistoryNotEmpty()

bool FILE_HISTORY::isHistoryNotEmpty ( const SELECTION aSelection,
const FILE_HISTORY aHistory 
)
staticprivate

Test if the file history is empty.

This function is designed to be used with a SELECTION_CONDITION to enable/disable the file history menu.

Parameters
aSelectionis unused
aHistoryis the file history to test for items

Definition at line 193 of file file_history.cpp.

194{
195 return aHistory.GetCount() != 0;
196}

Referenced by FileHistoryNotEmpty().

◆ Load() [1/2]

void FILE_HISTORY::Load ( const APP_SETTINGS_BASE aSettings)

Loads history from a JSON settings object.

Parameters
aSettingsis the settings object for this application

Definition at line 45 of file file_history.cpp.

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}
void AddFileToHistory(const wxString &aFile) override
Adds a file to the history.
void ClearFileHistory()
Clear all entries from the file history.
std::vector< wxString > file_history
Definition: app_settings.h:158

References AddFileToHistory(), ClearFileHistory(), APP_SETTINGS_BASE::SYSTEM::file_history, and APP_SETTINGS_BASE::m_System.

Referenced by EDA_BASE_FRAME::LoadSettings(), and GERBVIEW_FRAME::LoadSettings().

◆ Load() [2/2]

void FILE_HISTORY::Load ( const std::vector< wxString > &  aList)

Loads history from a list of strings.

Parameters
aListis a list of filenames to load

Definition at line 58 of file file_history.cpp.

59{
61
62 for( const auto& file : aList )
63 AddFileToHistory( file );
64}

References AddFileToHistory(), and ClearFileHistory().

◆ Save() [1/2]

void FILE_HISTORY::Save ( APP_SETTINGS_BASE aSettings)

Saves history into a JSON settings object.

Parameters
aSettingsis the settings object to save into

Definition at line 67 of file file_history.cpp.

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}

References APP_SETTINGS_BASE::SYSTEM::file_history, and APP_SETTINGS_BASE::m_System.

Referenced by EDA_BASE_FRAME::SaveSettings(), and GERBVIEW_FRAME::SaveSettings().

◆ Save() [2/2]

void FILE_HISTORY::Save ( std::vector< wxString > *  aList)

Saves history into a list of strings.

Parameters
aListis a pointer to a string vector to clear and fill with the file history

Definition at line 76 of file file_history.cpp.

77{
78 aList->clear();
79
80 for( const auto& file : m_fileHistory )
81 aList->push_back( file );
82}

◆ SetClearText()

void FILE_HISTORY::SetClearText ( wxString  aClearText)
inline

Set the text displayed on the menu item that clears the entire menu.

Parameters
aClearTextis the text to use for the menu item

Definition at line 123 of file file_history.h.

124 {
125 m_clearText = aClearText;
126 }

References m_clearText.

Referenced by GERBVIEW_FRAME::doReCreateMenuBar(), and KICAD_MANAGER_FRAME::doReCreateMenuBar().

◆ SetMaxFiles()

void FILE_HISTORY::SetMaxFiles ( size_t  aMaxFiles)

Update the number of files that will be contained inside the file history.

Parameters
aMaxFilesis the new number of files for the history

Definition at line 85 of file file_history.cpp.

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}

References MAX_FILE_HISTORY_SIZE.

Referenced by EDA_BASE_FRAME::CommonSettingsChanged(), and GERBVIEW_FRAME::GERBVIEW_FRAME().

◆ UpdateClearText()

void FILE_HISTORY::UpdateClearText ( wxMenu *  aMenu,
wxString  aClearText 
)

Update the text displayed on the menu item that clears the entire menu.

useful after language change.

Parameters
aMenuis the menu to operate on.
aClearTextis the new text to use for the menu item

Definition at line 168 of file file_history.cpp.

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}

References m_clearId.

Referenced by SCH_EDIT_FRAME::doReCreateMenuBar(), GERBVIEW_FRAME::doReCreateMenuBar(), PL_EDITOR_FRAME::doReCreateMenuBar(), and PCB_EDIT_FRAME::doReCreateMenuBar().

Member Data Documentation

◆ m_clearId

int FILE_HISTORY::m_clearId
private

Definition at line 176 of file file_history.h.

Referenced by doAddClearItem(), doRemoveClearitem(), and UpdateClearText().

◆ m_clearText

wxString FILE_HISTORY::m_clearText
private

Definition at line 177 of file file_history.h.

Referenced by doAddClearItem(), and SetClearText().


The documentation for this class was generated from the following files: