You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.7 KiB

  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-2022 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. #ifndef FILEHISTORY_H_
  25. #define FILEHISTORY_H_
  26. #include <tool/action_menu.h>
  27. #include <tool/selection_conditions.h>
  28. #include <wx/filehistory.h>
  29. #include <wx/menu.h>
  30. class APP_SETTINGS_BASE;
  31. /**
  32. * This class implements a file history object to store a list of files, that can then
  33. * be added to a menu.
  34. *
  35. * This class extends the wxWidgets wxFileHistory class to include KiCad specific items.
  36. */
  37. class FILE_HISTORY : public wxFileHistory
  38. {
  39. public:
  40. /**
  41. * Create a file history object to store a list of files and add them to a menu.
  42. *
  43. * @param aMaxFiles is the number of files to store in the history
  44. * @param aBaseFileId is the ID to use for the first file menu item
  45. * @param aClearId is the ID to use for the clear menu menu item
  46. * @param aClearText is the text to use for the menu item that clears the history.
  47. */
  48. FILE_HISTORY( size_t aMaxFiles, int aBaseFileId, int aClearId,
  49. wxString aClearText = _( "Clear Recent Files" ) );
  50. /**
  51. * Loads history from a JSON settings object
  52. * @param aSettings is the settings object for this application
  53. */
  54. void Load( const APP_SETTINGS_BASE& aSettings );
  55. /**
  56. * Loads history from a list of strings
  57. * @param aList is a list of filenames to load
  58. */
  59. void Load( const std::vector<wxString>& aList );
  60. /**
  61. * Saves history into a JSON settings object
  62. * @param aSettings is the settings object to save into
  63. */
  64. void Save( APP_SETTINGS_BASE& aSettings );
  65. /**
  66. * Saves history into a list of strings
  67. * @param aList is a pointer to a string vector to clear and fill with the file history
  68. */
  69. void Save( std::vector<wxString>* aList );
  70. // Hide warnings about these virtual functions
  71. using wxFileHistory::Load;
  72. using wxFileHistory::Save;
  73. /**
  74. * Adds a file to the history.
  75. *
  76. * This function overrides the default wxWidgets method to iterate through all
  77. * menus associated with the file history, remove the added menu items, lets wx
  78. * add the new files, and then re-adds the clear menu item.
  79. *
  80. * @param aFile is the filename of the file to add to the history.
  81. */
  82. void AddFileToHistory( const wxString& aFile ) override;
  83. /**
  84. * Add the files to all registered menus.
  85. */
  86. void AddFilesToMenu() override
  87. {
  88. // This is needed to ensure that the proper base class function is called
  89. wxFileHistory::AddFilesToMenu();
  90. }
  91. /**
  92. * Add the files to the specified menu
  93. *
  94. * @param aMenu is the menu to operate on.
  95. */
  96. void AddFilesToMenu( wxMenu* aMenu ) override;
  97. /**
  98. * Update the number of files that will be contained inside the file history.
  99. *
  100. * @param aMaxFiles is the new number of files for the history
  101. */
  102. void SetMaxFiles( size_t aMaxFiles );
  103. /**
  104. * Set the text displayed on the menu item that clears the entire menu.
  105. *
  106. * @param aClearText is the text to use for the menu item
  107. */
  108. void SetClearText( wxString aClearText )
  109. {
  110. m_clearText = aClearText;
  111. }
  112. /**
  113. * Update the text displayed on the menu item that clears the entire menu.
  114. * useful after language change.
  115. *
  116. * @param aMenu is the menu to operate on.
  117. * @param aClearText is the new text to use for the menu item
  118. */
  119. void UpdateClearText( wxMenu* aMenu, wxString aClearText );
  120. /**
  121. * Clear all entries from the file history.
  122. */
  123. void ClearFileHistory();
  124. /**
  125. * Create a #SELECTION_CONDITION that can be used to enable a menu item when the
  126. * file history has items in it.
  127. *
  128. * @param aHistory is the file history to check for items
  129. * @return the selection condition function
  130. */
  131. static SELECTION_CONDITION FileHistoryNotEmpty( const FILE_HISTORY& aHistory );
  132. protected:
  133. /**
  134. * Remove the clear menu item and the preceding separator from the given menu.
  135. *
  136. * @param aMenu is the menu to operate on
  137. */
  138. void doRemoveClearitem( wxMenu* aMenu );
  139. /**
  140. * Add the clear menu item and the preceding separator to the given menu.
  141. *
  142. * @param aMenu is the menu to operate on
  143. */
  144. void doAddClearItem( wxMenu* aMenu );
  145. private:
  146. /**
  147. * Test if the file history is empty. This function is designed to be used with a
  148. * #SELECTION_CONDITION to enable/disable the file history menu.
  149. *
  150. * @param aSelection is unused
  151. * @param aHistory is the file history to test for items
  152. */
  153. static bool isHistoryNotEmpty( const SELECTION& aSelection, const FILE_HISTORY& aHistory );
  154. int m_clearId;
  155. wxString m_clearText;
  156. };
  157. #endif