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.

250 lines
7.9 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
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 CERN
  5. * @author Maciej Suminski <maciej.suminski@cern.ch>
  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 CONDITIONAL_MENU_H
  25. #define CONDITIONAL_MENU_H
  26. #include <tool/selection_conditions.h>
  27. #include <tool/action_menu.h>
  28. #include <list>
  29. #include <wx/wx.h>
  30. class SELECTION_TOOL;
  31. class TOOL_ACTION;
  32. class TOOL_INTERACTIVE;
  33. class CONDITIONAL_MENU : public ACTION_MENU
  34. {
  35. public:
  36. ///> Constant to indicate that we do not care about an ENTRY location in the menu.
  37. static const int ANY_ORDER = -1;
  38. CONDITIONAL_MENU( bool isContextMenu, TOOL_INTERACTIVE* aTool );
  39. ACTION_MENU* create() const override;
  40. /**
  41. * Function AddItem()
  42. *
  43. * Adds a menu entry to run a TOOL_ACTION on selected items.
  44. * @param aAction is a menu entry to be added.
  45. * @param aCondition is a condition that has to be fulfilled to enable the menu entry.
  46. * @param aOrder determines location of the added item, higher numbers are put on the bottom.
  47. * You may use ANY_ORDER here if you think it does not matter.
  48. */
  49. void AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
  50. int aOrder = ANY_ORDER );
  51. void AddItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAP_DEF aIcon,
  52. const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
  53. /**
  54. * Function AddCheckItem()
  55. *
  56. * Adds a checked menu entry to run a TOOL_ACTION on selected items.
  57. * @param aAction is a menu entry to be added.
  58. * @param aCondition is a condition that has to be fulfilled to check the menu entry.
  59. * @param aOrder determines location of the added item, higher numbers are put on the bottom.
  60. * You may use ANY_ORDER here if you think it does not matter.
  61. */
  62. void AddCheckItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
  63. int aOrder = ANY_ORDER );
  64. void AddCheckItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAP_DEF aIcon,
  65. const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
  66. /**
  67. * Function AddMenu()
  68. *
  69. * Adds a submenu to the menu. CONDITIONAL_MENU takes ownership of the added menu, so it will
  70. * be freed when the CONDITIONAL_MENU object is destroyed.
  71. * @param aMenu is the submenu to be added.
  72. * @param aExpand determines if the added submenu items should be added as individual items
  73. * or as a submenu.
  74. * @param aCondition is a condition that has to be fulfilled to enable the submenu entry.
  75. * @param aOrder determines location of the added menu, higher numbers are put on the bottom.
  76. * You may use ANY_ORDER here if you think it does not matter.
  77. */
  78. void AddMenu( ACTION_MENU* aMenu,
  79. const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
  80. int aOrder = ANY_ORDER );
  81. /**
  82. * Function AddSeparator()
  83. *
  84. * Adds a separator to the menu.
  85. * @param aOrder determines location of the separator, higher numbers are put on the bottom.
  86. */
  87. void AddSeparator( int aOrder = ANY_ORDER );
  88. /**
  89. * Function Evaluate()
  90. *
  91. * Updates the contents of the menu based on the supplied conditions.
  92. */
  93. void Evaluate( SELECTION& aSelection );
  94. /**
  95. * Function Resolve()
  96. *
  97. * Updates the initial contents so that wxWidgets doesn't get its knickers tied in a knot
  98. * over the menu being empty (mainly an issue on GTK, but also on OSX with the preferences
  99. * and quit menu items).
  100. */
  101. void Resolve();
  102. private:
  103. ///> Helper class to organize menu entries.
  104. class ENTRY
  105. {
  106. public:
  107. ENTRY( const TOOL_ACTION* aAction, SELECTION_CONDITION aCondition, int aOrder,
  108. bool aCheckmark ) :
  109. m_type( ACTION ), m_icon(nullptr),
  110. m_condition( aCondition ),
  111. m_order( aOrder ),
  112. m_isCheckmarkEntry( aCheckmark )
  113. {
  114. m_data.action = aAction;
  115. }
  116. ENTRY( ACTION_MENU* aMenu, SELECTION_CONDITION aCondition, int aOrder ) :
  117. m_type( MENU ), m_icon(nullptr),
  118. m_condition( aCondition ),
  119. m_order( aOrder ),
  120. m_isCheckmarkEntry( false )
  121. {
  122. m_data.menu = aMenu;
  123. }
  124. ENTRY( const wxMenuItem& aItem, const BITMAP_OPAQUE* aWxMenuBitmap,
  125. SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
  126. m_type( WXITEM ), m_icon( aWxMenuBitmap ),
  127. m_condition( aCondition ),
  128. m_order( aOrder ),
  129. m_isCheckmarkEntry( aCheckmark )
  130. {
  131. m_data.wxItem = new wxMenuItem( nullptr, aItem.GetId(), aItem.GetItemLabel(),
  132. aItem.GetHelp(), aItem.GetKind() );
  133. }
  134. // Separator
  135. ENTRY( SELECTION_CONDITION aCondition, int aOrder ) :
  136. m_type( SEPARATOR ), m_icon(nullptr),
  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 const BITMAP_OPAQUE* 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. const BITMAP_OPAQUE* 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 */