Browse Source
Add clear recent files action to the menus
Add clear recent files action to the menus
ADDED: Menu item to allow the recent file lists in each program to be cleared Fixes: lp:1821685 * https://bugs.launchpad.net/kicad/+bug/1821685merge-requests/1/head
23 changed files with 442 additions and 165 deletions
-
1common/CMakeLists.txt
-
3common/bin_mod.cpp
-
10common/eda_base_frame.cpp
-
1common/eda_draw_frame.cpp
-
145common/filehistory.cpp
-
18common/pgm_base.cpp
-
30eeschema/menubar.cpp
-
6eeschema/sch_edit_frame.cpp
-
6gerbview/gerbview_frame.cpp
-
2gerbview/gerbview_frame.h
-
113gerbview/menubar.cpp
-
23include/eda_base_frame.h
-
125include/filehistory.h
-
1include/id.h
-
10include/pgm_base.h
-
3kicad/kicad.cpp
-
7kicad/kicad_manager_frame.cpp
-
32kicad/menubar.cpp
-
27pagelayout_editor/menubar.cpp
-
9pagelayout_editor/pl_editor_frame.cpp
-
2pagelayout_editor/pl_editor_frame.h
-
28pcbnew/menubar_pcb_editor.cpp
-
5pcbnew/pcb_edit_frame.cpp
@ -0,0 +1,145 @@ |
|||
/*
|
|||
* This program source code file is part of KiCad, a free EDA CAD application. |
|||
* |
|||
* Copyright (C) 2019 Ian McInerney <Ian.S.McInerney@ieee.org> |
|||
* Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors. |
|||
* |
|||
* This program is free software; you can redistribute it and/or |
|||
* modify it under the terms of the GNU General Public License |
|||
* as published by the Free Software Foundation; either version 2 |
|||
* of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program; if not, you may find one here: |
|||
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|||
* or you may search the http://www.gnu.org website for the version 2 license,
|
|||
* or you may write to the Free Software Foundation, Inc., |
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|||
*/ |
|||
|
|||
#include <filehistory.h>
|
|||
#include <id.h>
|
|||
#include <tool/action_menu.h>
|
|||
#include <tool/selection_conditions.h>
|
|||
#include <wx/menu.h>
|
|||
|
|||
#include <functional>
|
|||
using namespace std::placeholders; |
|||
|
|||
|
|||
FILE_HISTORY::FILE_HISTORY( size_t aMaxFiles, int aBaseFileId ) : |
|||
wxFileHistory( std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE ) ) |
|||
{ |
|||
SetBaseId( aBaseFileId ); |
|||
} |
|||
|
|||
|
|||
void FILE_HISTORY::SetMaxFiles( size_t aMaxFiles ) |
|||
{ |
|||
m_fileMaxFiles = std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE ); |
|||
|
|||
size_t numFiles = m_fileHistory.size(); |
|||
|
|||
while( numFiles > m_fileMaxFiles ) |
|||
RemoveFileFromHistory( --numFiles ); |
|||
} |
|||
|
|||
|
|||
void FILE_HISTORY::AddFileToHistory( const wxString &aFile ) |
|||
{ |
|||
wxFileHistory::AddFileToHistory( aFile ); |
|||
|
|||
// Iterate over each menu associated with this file history, and if it is one of our
|
|||
// FILE_HISTORY_MENUs, we force it to be refreshed (so that the items are all in the
|
|||
// correct locations).
|
|||
for( wxList::compatibility_iterator node = m_fileMenus.GetFirst(); |
|||
node; node = node->GetNext() ) |
|||
{ |
|||
wxMenu* menu = static_cast<wxMenu*>( node->GetData() ); |
|||
|
|||
FILE_HISTORY_MENU* fileMenu = dynamic_cast<FILE_HISTORY_MENU*>( menu ); |
|||
|
|||
if( fileMenu ) |
|||
fileMenu->RefreshMenu(); |
|||
} |
|||
} |
|||
|
|||
|
|||
SELECTION_CONDITION FILE_HISTORY::FileHistoryNotEmpty( const FILE_HISTORY& aHistory ) |
|||
{ |
|||
return std::bind( &FILE_HISTORY::isHistoryNotEmpty, _1, std::cref( aHistory ) ); |
|||
} |
|||
|
|||
|
|||
bool FILE_HISTORY::isHistoryNotEmpty( const SELECTION& aSelection, const FILE_HISTORY& aHistory ) |
|||
{ |
|||
return aHistory.GetCount() != 0; |
|||
} |
|||
|
|||
|
|||
FILE_HISTORY_MENU::FILE_HISTORY_MENU( FILE_HISTORY& aHistory, wxString aClearText ) : |
|||
ACTION_MENU( false ), |
|||
m_fileHistory( aHistory ), |
|||
m_clearText( aClearText ) |
|||
{ |
|||
m_fileHistory.UseMenu( this ); |
|||
buildMenu(); |
|||
} |
|||
|
|||
|
|||
FILE_HISTORY_MENU::~FILE_HISTORY_MENU() |
|||
{ |
|||
m_fileHistory.RemoveMenu( this ); |
|||
} |
|||
|
|||
|
|||
void FILE_HISTORY_MENU::RefreshMenu() |
|||
{ |
|||
// We have to manually delete all menu items before we rebuild the menu
|
|||
for( int i = GetMenuItemCount() - 1; i >= 0; --i ) |
|||
Destroy( FindItemByPosition( i ) ); |
|||
|
|||
buildMenu(); |
|||
} |
|||
|
|||
|
|||
void FILE_HISTORY_MENU::buildMenu() |
|||
{ |
|||
if( m_fileHistory.GetCount() == 0 ) |
|||
{ |
|||
// If the history is empty, we create an item to say there are no files
|
|||
wxMenuItem* item = new wxMenuItem( this, wxID_ANY, _( "No Files" ) ); |
|||
|
|||
Append( item ); |
|||
Enable( item->GetId(), false ); |
|||
} |
|||
else |
|||
m_fileHistory.AddFilesToMenu( this ); |
|||
|
|||
wxMenuItem* clearItem = new wxMenuItem( this, ID_FILE_LIST_CLEAR, m_clearText ); |
|||
|
|||
AppendSeparator(); |
|||
Append( clearItem ); |
|||
Connect( ID_FILE_LIST_CLEAR, wxEVT_COMMAND_MENU_SELECTED, |
|||
wxMenuEventHandler( FILE_HISTORY_MENU::onClearEntries ), NULL, this ); |
|||
} |
|||
|
|||
|
|||
void FILE_HISTORY_MENU::onClearEntries( wxMenuEvent& aEvent ) |
|||
{ |
|||
while( m_fileHistory.GetCount() > 0 ) |
|||
m_fileHistory.RemoveFileFromHistory( 0 ); |
|||
|
|||
RefreshMenu(); |
|||
} |
|||
|
|||
|
|||
ACTION_MENU* FILE_HISTORY_MENU::create() const |
|||
{ |
|||
return new FILE_HISTORY_MENU( m_fileHistory, m_clearText ); |
|||
} |
|||
@ -0,0 +1,125 @@ |
|||
/* |
|||
* This program source code file is part of KiCad, a free EDA CAD application. |
|||
* |
|||
* Copyright (C) 2019 Ian McInerney <Ian.S.McInerney@ieee.org> |
|||
* Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors. |
|||
* |
|||
* This program is free software; you can redistribute it and/or |
|||
* modify it under the terms of the GNU General Public License |
|||
* as published by the Free Software Foundation; either version 2 |
|||
* of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program; if not, you may find one here: |
|||
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
|||
* or you may search the http://www.gnu.org website for the version 2 license, |
|||
* or you may write to the Free Software Foundation, Inc., |
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|||
*/ |
|||
|
|||
#ifndef FILEHISTORY_H_ |
|||
#define FILEHISTORY_H_ |
|||
|
|||
#include <tool/action_menu.h> |
|||
#include <tool/selection_conditions.h> |
|||
#include <wx/filehistory.h> |
|||
#include <wx/menu.h> |
|||
|
|||
|
|||
/** |
|||
* 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. |
|||
*/ |
|||
class FILE_HISTORY : public wxFileHistory |
|||
{ |
|||
public: |
|||
/** |
|||
* Create a file history object to store a list of files and add them to a menu. |
|||
* |
|||
* @param aMaxFiles is the number of files to store in the history |
|||
* @param aBaseFileId is the ID to use for the first file menu item |
|||
*/ |
|||
FILE_HISTORY( size_t aMaxFiles, int aBaseFileId ); |
|||
|
|||
/** |
|||
* Adds a file to the history. |
|||
* |
|||
* This function overrides the default wxWidgets method to iterate through all |
|||
* menus associated with the file history, and if they are of the FILE_HISTORY_MENU |
|||
* type, call their RefreshMenu() function to update the menu display. |
|||
* |
|||
* @param aFile is the filename of the file to add to the history. |
|||
*/ |
|||
void AddFileToHistory( const wxString &aFile ) override; |
|||
|
|||
/** |
|||
* Update the number of files that will be contained inside the file history. |
|||
* |
|||
* @param aMaxFiles is the new number of files for the history |
|||
*/ |
|||
void SetMaxFiles( size_t aMaxFiles ); |
|||
|
|||
/** |
|||
* Create a SELECTION_CONDITION that can be used to enable a menu item when the |
|||
* file history has items in it. |
|||
* |
|||
* @param aHistory is the file history to check for items |
|||
* @return the selection condition function |
|||
*/ |
|||
static SELECTION_CONDITION FileHistoryNotEmpty( const FILE_HISTORY& aHistory ); |
|||
|
|||
private: |
|||
static bool isHistoryNotEmpty( const SELECTION& aSelection, const FILE_HISTORY& aHistory ); |
|||
}; |
|||
|
|||
/** |
|||
* This class implements a menu container for a file history. It adds in the ability to clear |
|||
* the file history through a menu item. |
|||
*/ |
|||
class FILE_HISTORY_MENU : public ACTION_MENU |
|||
{ |
|||
public: |
|||
/** |
|||
* Create the file history menu. |
|||
* |
|||
* @param aHistory is the file history to use in the menu |
|||
* @param aClearText is the text to use for the menu item that clears the history. |
|||
*/ |
|||
FILE_HISTORY_MENU( FILE_HISTORY& aHistory, wxString aClearText = _( "Clear Recent Files" ) ); |
|||
|
|||
~FILE_HISTORY_MENU(); |
|||
|
|||
/** |
|||
* Refresh the menu. This removes all entries from the menu and readds them, to ensure that the |
|||
* clear menu item is at the bottom of the menu. |
|||
*/ |
|||
void RefreshMenu(); |
|||
|
|||
private: |
|||
//! @copydoc ACTION_MENU::create() |
|||
ACTION_MENU* create() const override; |
|||
|
|||
/** |
|||
* Construct the menu by adding the file history and menu items. |
|||
*/ |
|||
void buildMenu(); |
|||
|
|||
/** |
|||
* Event handler for when the clear menu item is activated. |
|||
* |
|||
* @param aEvent the menu event |
|||
*/ |
|||
void onClearEntries( wxMenuEvent& aEvent ); |
|||
|
|||
FILE_HISTORY& m_fileHistory; |
|||
wxString m_clearText; |
|||
}; |
|||
|
|||
#endif |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue