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 <boost/unordered_map.hpp>
30 #include <list>
31 #include <wx/wx.h>
32 
33 class SELECTION_TOOL;
34 class TOOL_ACTION;
35 class TOOL_INTERACTIVE;
36 class CONTEXT_MENU;
37 
39 {
40 public:
42  static const int ANY_ORDER = -1;
43 
45  m_tool( aTool )
46  {}
47 
57  void AddItem( const TOOL_ACTION& aAction,
59  int aOrder = ANY_ORDER );
60 
73  void AddMenu( CONTEXT_MENU* aMenu, bool aExpand = false,
75  int aOrder = ANY_ORDER );
76 
86  int aOrder = ANY_ORDER );
87 
95  CONTEXT_MENU* Generate( SELECTION& aSelection );
96 
97 private:
99  class ENTRY
100  {
101  public:
102  ENTRY( const TOOL_ACTION* aAction,
104  int aOrder = ANY_ORDER ) :
105  m_type( ACTION ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
106  {
107  m_data.action = aAction;
108  }
109 
110  ENTRY( CONTEXT_MENU* aMenu, bool aExpand = false,
112  int aOrder = ANY_ORDER ) :
113  m_type( MENU ), m_condition( aCondition ), m_order( aOrder ), m_expand( aExpand )
114  {
115  m_data.menu = aMenu;
116  }
117 
118  ENTRY( wxMenuItem* aItem, const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
119  int aOrder = ANY_ORDER ) :
120  m_type( WXITEM ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
121  {
122  m_data.wxItem = aItem;
123  }
124 
125  // Separator
127  int aOrder = ANY_ORDER ) :
128  m_type( SEPARATOR ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
129  {
130  m_data.wxItem = NULL;
131  }
132 
134  enum ENTRY_TYPE {
139  };
140 
141  inline ENTRY_TYPE Type() const
142  {
143  return m_type;
144  }
145 
146  inline const TOOL_ACTION* Action() const
147  {
148  assert( m_type == ACTION );
149  return m_data.action;
150  }
151 
152  inline CONTEXT_MENU* Menu() const
153  {
154  assert( m_type == MENU );
155  return m_data.menu;
156  }
157 
158  inline wxMenuItem* wxItem() const
159  {
160  assert( m_type == WXITEM );
161  return m_data.wxItem;
162  }
163 
164  inline bool Expand() const
165  {
166  assert( m_type == MENU );
167  return m_expand;
168  }
169 
170  inline const SELECTION_CONDITION& Condition() const
171  {
172  return m_condition;
173  }
174 
175  inline int Order() const
176  {
177  return m_order;
178  }
179 
180  inline void SetOrder( int aOrder )
181  {
182  m_order = aOrder;
183  }
184 
185  private:
187 
188  union {
191  wxMenuItem* wxItem;
192  } m_data;
193 
196 
198  int m_order;
199 
201  bool m_expand;
202  };
203 
205  void addEntry( ENTRY aEntry );
206 
208  std::list<ENTRY> m_entries;
209 
212 };
213 
214 #endif /* CONDITIONAL_MENU_H */
static bool ShowAlways(const SELECTION &aSelection)
Function ShowAlways The default condition function (always returns true).
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)
union CONDITIONAL_MENU::ENTRY::@54 m_data