KiCad PCB EDA Suite
conditional_menu.h
Go to the documentation of this file.
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 
27 #ifndef CONDITIONAL_MENU_H
28 #define CONDITIONAL_MENU_H
29 
31 #include <tool/action_menu.h>
32 #include <list>
33 
34 class PCB_SELECTION_TOOL;
35 class TOOL_ACTION;
36 class TOOL_INTERACTIVE;
37 
38 enum class BITMAPS : unsigned int;
39 
40 
42 {
43 public:
45  static const int ANY_ORDER = -1;
46 
48 
49  ACTION_MENU* create() const override;
50 
59  void AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
60  int aOrder = ANY_ORDER );
61 
62  void AddItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
63  const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
64 
76  void AddCheckItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
77  int aOrder = ANY_ORDER );
78 
79  void AddCheckItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
80  const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
81 
95  void AddMenu( ACTION_MENU* aMenu,
97  int aOrder = ANY_ORDER );
98 
104  void AddSeparator( int aOrder = ANY_ORDER );
105 
109  void Evaluate( SELECTION& aSelection );
110 
116  void Resolve();
117 
118 private:
120  class ENTRY
121  {
122  public:
123  ENTRY( const TOOL_ACTION* aAction, SELECTION_CONDITION aCondition, int aOrder,
124  bool aCheckmark ) :
125  m_type( ACTION ), m_icon( static_cast<BITMAPS>( 0 ) ),
126  m_condition( aCondition ),
127  m_order( aOrder ),
128  m_isCheckmarkEntry( aCheckmark )
129  {
130  m_data.action = aAction;
131  }
132 
133  ENTRY( ACTION_MENU* aMenu, SELECTION_CONDITION aCondition, int aOrder ) :
134  m_type( MENU ), m_icon( static_cast<BITMAPS>( 0 ) ),
135  m_condition( aCondition ),
136  m_order( aOrder ),
137  m_isCheckmarkEntry( false )
138  {
139  m_data.menu = aMenu;
140  }
141 
142  ENTRY( const wxMenuItem& aItem, BITMAPS aBitmap,
143  SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
144  m_type( WXITEM ), m_icon( aBitmap ),
145  m_condition( aCondition ),
146  m_order( aOrder ),
147  m_isCheckmarkEntry( aCheckmark )
148  {
149  m_data.wxItem = new wxMenuItem( nullptr, aItem.GetId(), aItem.GetItemLabel(),
150  aItem.GetHelp(), aItem.GetKind() );
151  }
152 
153  // Separator
154  ENTRY( SELECTION_CONDITION aCondition, int aOrder ) :
155  m_type( SEPARATOR ), m_icon( static_cast<BITMAPS>( 0 ) ),
156  m_data(),
157  m_condition( aCondition ),
158  m_order( aOrder ),
159  m_isCheckmarkEntry( false )
160  {
161  }
162 
163  ENTRY( const ENTRY& aEntry );
164 
165  ~ENTRY();
166 
168  enum ENTRY_TYPE {
172  SEPARATOR
173  };
174 
175  inline ENTRY_TYPE Type() const
176  {
177  return m_type;
178  }
179 
180  inline BITMAPS GetIcon() const
181  {
182  return m_icon;
183  }
184 
185  inline const TOOL_ACTION* Action() const
186  {
187  assert( m_type == ACTION );
188  return m_data.action;
189  }
190 
191  inline ACTION_MENU* Menu() const
192  {
193  assert( m_type == MENU );
194  return m_data.menu;
195  }
196 
197  inline wxMenuItem* wxItem() const
198  {
199  assert( m_type == WXITEM );
200  return m_data.wxItem;
201  }
202 
203  inline bool IsCheckmarkEntry() const
204  {
205  return m_isCheckmarkEntry;
206  }
207 
208  inline const SELECTION_CONDITION& Condition() const
209  {
210  return m_condition;
211  }
212 
213  inline int Order() const
214  {
215  return m_order;
216  }
217 
218  inline void SetOrder( int aOrder )
219  {
220  m_order = aOrder;
221  }
222 
223  private:
226 
227  // This class owns the wxItem object and needs to create, copy and delete it accordingly
228  // But it does not own the action nor menu item
229  union {
232  wxMenuItem* wxItem;
233  } m_data;
234 
237 
239  int m_order;
240 
242  };
243 
245  void addEntry( ENTRY aEntry );
246 
248  std::list<ENTRY> m_entries;
249 };
250 
251 #endif /* CONDITIONAL_MENU_H */
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
void AddMenu(ACTION_MENU *aMenu, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Add a submenu to the menu.
< Helper class to organize menu entries. Inserts the entry, preserving the requested order.
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:48
const TOOL_ACTION * action
std::list< ENTRY > m_entries
void AddCheckItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a checked menu entry to run a TOOL_ACTION on selected items.
double Evaluate(AM_PARAM_EVAL_STACK &aExp)
Evaluate an basic arithmetic expression (infix notation) with precedence The expression is a sequence...
Definition: evaluate.cpp:102
SELECTION_CONDITION m_condition
Order number, the higher the number the lower position it takes it is in the menu.
virtual ACTION_MENU * create() const
< Return an instance of this class. It has to be overridden in inheriting classes.
const SELECTION_CONDITION & Condition() const
void SetOrder(int aOrder)
ENTRY_TYPE Type() const
std::function< bool(const SELECTION &)> SELECTION_CONDITION
< Functor type that checks a specific condition for selected items.
ENTRY(SELECTION_CONDITION aCondition, int aOrder)
const TOOL_ACTION * Action() const
void Resolve()
Update the initial contents so that wxWidgets doesn't get its knickers tied in a knot over the menu b...
void AddSeparator(int aOrder=ANY_ORDER)
Add a separator to the menu.
wxMenuItem * wxItem() const
ENTRY(const wxMenuItem &aItem, BITMAPS aBitmap, SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark)
void addEntry(ENTRY aEntry)
List of all menu entries.
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
Represent a single user action.
Definition: tool_action.h:67
The selection tool: currently supports:
ACTION_MENU * Menu() const
CONDITIONAL_MENU(TOOL_INTERACTIVE *aTool)
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
ENTRY(const TOOL_ACTION *aAction, SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark)
ENTRY(ACTION_MENU *aMenu, SELECTION_CONDITION aCondition, int aOrder)