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.

251 lines
8.1 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 CERN
  5. * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * @author Maciej Suminski <maciej.suminski@cern.ch>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #ifndef CONDITIONAL_MENU_H
  27. #define CONDITIONAL_MENU_H
  28. #include <tool/selection_conditions.h>
  29. #include <tool/action_menu.h>
  30. #include <list>
  31. class PCB_SELECTION_TOOL;
  32. class TOOL_ACTION;
  33. class TOOL_INTERACTIVE;
  34. enum class BITMAPS : unsigned int;
  35. class CONDITIONAL_MENU : public ACTION_MENU
  36. {
  37. public:
  38. ///< Constant to indicate that we do not care about an #ENTRY location in the menu.
  39. static const int ANY_ORDER = -1;
  40. CONDITIONAL_MENU( TOOL_INTERACTIVE* aTool );
  41. ACTION_MENU* create() const override;
  42. /**
  43. * Add a menu entry to run a #TOOL_ACTION on selected items.
  44. *
  45. * @param aAction is a menu entry to be added.
  46. * @param aCondition is a condition that has to be fulfilled to show the menu entry in the menu.
  47. * @param aOrder determines location of the added item, higher numbers are put on the bottom.
  48. * You may use ANY_ORDER here if you think it does not matter.
  49. */
  50. void AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
  51. int aOrder = ANY_ORDER );
  52. void AddItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
  53. const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
  54. /**
  55. * Add a checked menu entry to run a TOOL_ACTION on selected items.
  56. *
  57. * The condition for checking the menu entry should be supplied through a #ACTION_CONDITION
  58. * registered with the #ACTION_MANAGER.
  59. *
  60. * @param aAction is a menu entry to be added.
  61. * @param aCondition is a condition that has to be fulfilled to show the menu entry in the menu.
  62. * @param aOrder determines location of the added item, higher numbers are put on the bottom.
  63. * You may use #ANY_ORDER here if you think it does not matter.
  64. */
  65. void AddCheckItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
  66. int aOrder = ANY_ORDER );
  67. void AddCheckItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
  68. const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
  69. /**
  70. * Add a submenu to the menu.
  71. * CONDITIONAL_MENU takes ownership of the added menu, so it will be freed when the
  72. * CONDITIONAL_MENU object is destroyed.
  73. *
  74. * @param aMenu is the submenu to be added.
  75. * @param aExpand determines if the added submenu items should be added as individual items
  76. * or as a submenu.
  77. * @param aCondition is a condition that has to be fulfilled to show the submenu entry in
  78. * the menu.
  79. * @param aOrder determines location of the added menu, higher numbers are put on the bottom.
  80. * You may use ANY_ORDER here if you think it does not matter.
  81. */
  82. void AddMenu( ACTION_MENU* aMenu,
  83. const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
  84. int aOrder = ANY_ORDER );
  85. /**
  86. * Add a separator to the menu.
  87. *
  88. * @param aOrder determines location of the separator, higher numbers are put on the bottom.
  89. */
  90. void AddSeparator( int aOrder = ANY_ORDER );
  91. /**
  92. * Update the contents of the menu based on the supplied conditions.
  93. */
  94. void Evaluate( SELECTION& aSelection );
  95. /**
  96. * Update the initial contents so that wxWidgets doesn't get its knickers tied in a knot
  97. * over the menu being empty (mainly an issue on GTK, but also on OSX with the preferences
  98. * and quit menu items).
  99. */
  100. void Resolve();
  101. private:
  102. ///< Helper class to organize menu entries.
  103. class ENTRY
  104. {
  105. public:
  106. ENTRY( const TOOL_ACTION* aAction, SELECTION_CONDITION aCondition, int aOrder,
  107. bool aCheckmark ) :
  108. m_type( ACTION ), m_icon( static_cast<BITMAPS>( 0 ) ),
  109. m_condition( aCondition ),
  110. m_order( aOrder ),
  111. m_isCheckmarkEntry( aCheckmark )
  112. {
  113. m_data.action = aAction;
  114. }
  115. ENTRY( ACTION_MENU* aMenu, SELECTION_CONDITION aCondition, int aOrder ) :
  116. m_type( MENU ), m_icon( static_cast<BITMAPS>( 0 ) ),
  117. m_condition( aCondition ),
  118. m_order( aOrder ),
  119. m_isCheckmarkEntry( false )
  120. {
  121. m_data.menu = aMenu;
  122. }
  123. ENTRY( const wxMenuItem& aItem, BITMAPS aBitmap,
  124. SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
  125. m_type( WXITEM ), m_icon( aBitmap ),
  126. m_condition( aCondition ),
  127. m_order( aOrder ),
  128. m_isCheckmarkEntry( aCheckmark )
  129. {
  130. m_data.wxItem = new wxMenuItem( nullptr, aItem.GetId(), aItem.GetItemLabel(),
  131. aItem.GetHelp(), aItem.GetKind() );
  132. }
  133. // Separator
  134. ENTRY( SELECTION_CONDITION aCondition, int aOrder ) :
  135. m_type( SEPARATOR ), m_icon( static_cast<BITMAPS>( 0 ) ),
  136. m_data(),
  137. m_condition( aCondition ),
  138. m_order( aOrder ),
  139. m_isCheckmarkEntry( false )
  140. {
  141. }
  142. ENTRY( const ENTRY& aEntry );
  143. ~ENTRY();
  144. ///< Possible entry types.
  145. enum ENTRY_TYPE {
  146. ACTION,
  147. MENU,
  148. WXITEM,
  149. SEPARATOR
  150. };
  151. inline ENTRY_TYPE Type() const
  152. {
  153. return m_type;
  154. }
  155. inline BITMAPS GetIcon() const
  156. {
  157. return m_icon;
  158. }
  159. inline const TOOL_ACTION* Action() const
  160. {
  161. assert( m_type == ACTION );
  162. return m_data.action;
  163. }
  164. inline ACTION_MENU* Menu() const
  165. {
  166. assert( m_type == MENU );
  167. return m_data.menu;
  168. }
  169. inline wxMenuItem* wxItem() const
  170. {
  171. assert( m_type == WXITEM );
  172. return m_data.wxItem;
  173. }
  174. inline bool IsCheckmarkEntry() const
  175. {
  176. return m_isCheckmarkEntry;
  177. }
  178. inline const SELECTION_CONDITION& Condition() const
  179. {
  180. return m_condition;
  181. }
  182. inline int Order() const
  183. {
  184. return m_order;
  185. }
  186. inline void SetOrder( int aOrder )
  187. {
  188. m_order = aOrder;
  189. }
  190. private:
  191. ENTRY_TYPE m_type;
  192. BITMAPS m_icon;
  193. // This class owns the wxItem object and needs to create, copy and delete it accordingly
  194. // But it does not own the action nor menu item
  195. union {
  196. const TOOL_ACTION* action;
  197. ACTION_MENU* menu;
  198. wxMenuItem* wxItem;
  199. } m_data;
  200. ///< Condition to be fulfilled to show the entry in menu.
  201. SELECTION_CONDITION m_condition;
  202. ///< Order number, the higher the number the lower position it takes it is in the menu.
  203. int m_order;
  204. bool m_isCheckmarkEntry;
  205. };
  206. ///< Inserts the entry, preserving the requested order.
  207. void addEntry( ENTRY aEntry );
  208. ///< List of all menu entries.
  209. std::list<ENTRY> m_entries;
  210. };
  211. #endif /* CONDITIONAL_MENU_H */