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  * @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 
25 #ifndef CONDITIONAL_MENU_H
26 #define CONDITIONAL_MENU_H
27 
28 #include "selection_conditions.h"
29 #include <list>
30 #include <wx/wx.h>
31 
32 class SELECTION_TOOL;
33 class TOOL_ACTION;
34 class TOOL_INTERACTIVE;
35 class CONTEXT_MENU;
36 
38 {
39 public:
41  static const int ANY_ORDER = -1;
42 
44  m_tool( aTool )
45  {}
46 
56  void AddItem( const TOOL_ACTION& aAction,
58  int aOrder = ANY_ORDER );
59 
72  void AddMenu( CONTEXT_MENU* aMenu, bool aExpand = false,
74  int aOrder = ANY_ORDER );
75 
85  int aOrder = ANY_ORDER );
86 
94  CONTEXT_MENU* Generate( SELECTION& aSelection );
95 
96 private:
98  class ENTRY
99  {
100  public:
101  ENTRY( const TOOL_ACTION* aAction,
103  int aOrder = ANY_ORDER ) :
104  m_type( ACTION ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
105  {
106  m_data.action = aAction;
107  }
108 
109  ENTRY( CONTEXT_MENU* aMenu, bool aExpand = false,
111  int aOrder = ANY_ORDER ) :
112  m_type( MENU ), m_condition( aCondition ), m_order( aOrder ), m_expand( aExpand )
113  {
114  m_data.menu = aMenu;
115  }
116 
117  ENTRY( wxMenuItem* aItem, const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
118  int aOrder = ANY_ORDER ) :
119  m_type( WXITEM ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
120  {
121  m_data.wxItem = aItem;
122  }
123 
124  // Separator
126  int aOrder = ANY_ORDER ) :
127  m_type( SEPARATOR ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
128  {
129  m_data.wxItem = NULL;
130  }
131 
133  enum ENTRY_TYPE {
138  };
139 
140  inline ENTRY_TYPE Type() const
141  {
142  return m_type;
143  }
144 
145  inline const TOOL_ACTION* Action() const
146  {
147  assert( m_type == ACTION );
148  return m_data.action;
149  }
150 
151  inline CONTEXT_MENU* Menu() const
152  {
153  assert( m_type == MENU );
154  return m_data.menu;
155  }
156 
157  inline wxMenuItem* wxItem() const
158  {
159  assert( m_type == WXITEM );
160  return m_data.wxItem;
161  }
162 
163  inline bool Expand() const
164  {
165  assert( m_type == MENU );
166  return m_expand;
167  }
168 
169  inline const SELECTION_CONDITION& Condition() const
170  {
171  return m_condition;
172  }
173 
174  inline int Order() const
175  {
176  return m_order;
177  }
178 
179  inline void SetOrder( int aOrder )
180  {
181  m_order = aOrder;
182  }
183 
184  private:
186 
187  union {
190  wxMenuItem* wxItem;
191  } m_data;
192 
195 
197  int m_order;
198 
200  bool m_expand;
201  };
202 
204  void addEntry( ENTRY aEntry );
205 
207  std::list<ENTRY> m_entries;
208 
211 };
212 
213 #endif /* CONDITIONAL_MENU_H */
static bool ShowAlways(const SELECTION &aSelection)
Function ShowAlways The default condition function (always returns true).
union CONDITIONAL_MENU::ENTRY::@62 m_data
TOOL_INTERACTIVE * m_tool
tool owning the menu
Helper class to organize menu entries.
Class CONTEXT_MENU.
Definition: context_menu.h:44
static const int ANY_ORDER
Constant to indicate that we do not care about an ENTRY location in the menu.
void AddSeparator(const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Function AddSeparator()
CONTEXT_MENU * Menu() const
Class SELECTION_TOOL.
const TOOL_ACTION * action
ENTRY_TYPE
Possible entry types.
std::list< ENTRY > m_entries
List of all menu entries.
const SELECTION_CONDITION & Condition() const
ENTRY_TYPE Type() const
SELECTION_CONDITION m_condition
Condition to be fulfilled to show the entry in menu.
ENTRY(const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
void SetOrder(int aOrder)
int m_order
Order number, the higher the number the lower position it takes it is in the menu.
const TOOL_ACTION * Action() const
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.
void AddMenu(CONTEXT_MENU *aMenu, bool aExpand=false, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Function AddMenu()
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Function AddItem()
CONTEXT_MENU * Generate(SELECTION &aSelection)
Function Generate()
Class TOOL_ACTION.
Definition: tool_action.h:46
ENTRY(wxMenuItem *aItem, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
bool m_expand
CONTEXT_MENU expand flag
wxMenuItem * wxItem() const
ENTRY(const TOOL_ACTION *aAction, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
ENTRY(CONTEXT_MENU *aMenu, bool aExpand=false, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
CONDITIONAL_MENU(TOOL_INTERACTIVE *aTool)