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 
29 #include <tool/action_menu.h>
30 #include <list>
31 #include <wx/wx.h>
32 
33 class SELECTION_TOOL;
34 class TOOL_ACTION;
35 class TOOL_INTERACTIVE;
36 
37 
39 {
40 public:
42  static const int ANY_ORDER = -1;
43 
45 
46  ACTION_MENU* create() const override;
47 
56  void AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
57  int aOrder = ANY_ORDER );
58 
59  void AddItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAP_DEF aIcon,
60  const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
61 
73  void AddCheckItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
74  int aOrder = ANY_ORDER );
75 
76  void AddCheckItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAP_DEF aIcon,
77  const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
78 
90  void AddMenu( ACTION_MENU* aMenu,
92  int aOrder = ANY_ORDER );
93 
99  void AddSeparator( int aOrder = ANY_ORDER );
100 
104  void Evaluate( SELECTION& aSelection );
105 
111  void Resolve();
112 
113 private:
115  class ENTRY
116  {
117  public:
118  ENTRY( const TOOL_ACTION* aAction, SELECTION_CONDITION aCondition, int aOrder,
119  bool aCheckmark ) :
120  m_type( ACTION ), m_icon(nullptr),
121  m_condition( aCondition ),
122  m_order( aOrder ),
123  m_isCheckmarkEntry( aCheckmark )
124  {
125  m_data.action = aAction;
126  }
127 
128  ENTRY( ACTION_MENU* aMenu, SELECTION_CONDITION aCondition, int aOrder ) :
129  m_type( MENU ), m_icon(nullptr),
130  m_condition( aCondition ),
131  m_order( aOrder ),
132  m_isCheckmarkEntry( false )
133  {
134  m_data.menu = aMenu;
135  }
136 
137  ENTRY( const wxMenuItem& aItem, const BITMAP_OPAQUE* aWxMenuBitmap,
138  SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
139  m_type( WXITEM ), m_icon( aWxMenuBitmap ),
140  m_condition( aCondition ),
141  m_order( aOrder ),
142  m_isCheckmarkEntry( aCheckmark )
143  {
144  m_data.wxItem = new wxMenuItem( nullptr, aItem.GetId(), aItem.GetItemLabel(),
145  aItem.GetHelp(), aItem.GetKind() );
146  }
147 
148  // Separator
149  ENTRY( SELECTION_CONDITION aCondition, int aOrder ) :
150  m_type( SEPARATOR ), m_icon(nullptr),
151  m_condition( aCondition ),
152  m_order( aOrder ),
153  m_isCheckmarkEntry( false )
154  {
155  }
156 
157  ENTRY( const ENTRY& aEntry );
158 
159  ~ENTRY();
160 
162  enum ENTRY_TYPE {
167  };
168 
169  inline ENTRY_TYPE Type() const
170  {
171  return m_type;
172  }
173 
174  inline const BITMAP_OPAQUE* GetIcon() const
175  {
176  return m_icon;
177  }
178 
179  inline const TOOL_ACTION* Action() const
180  {
181  assert( m_type == ACTION );
182  return m_data.action;
183  }
184 
185  inline ACTION_MENU* Menu() const
186  {
187  assert( m_type == MENU );
188  return m_data.menu;
189  }
190 
191  inline wxMenuItem* wxItem() const
192  {
193  assert( m_type == WXITEM );
194  return m_data.wxItem;
195  }
196 
197  inline bool IsCheckmarkEntry() const
198  {
199  return m_isCheckmarkEntry;
200  }
201 
202  inline const SELECTION_CONDITION& Condition() const
203  {
204  return m_condition;
205  }
206 
207  inline int Order() const
208  {
209  return m_order;
210  }
211 
212  inline void SetOrder( int aOrder )
213  {
214  m_order = aOrder;
215  }
216 
217  private:
220 
221  // This class owns the wxItem object and needs to create, copy and delete it accordingly
222  // But it does not own the action nor menu item
223  union {
226  wxMenuItem* wxItem;
227  } m_data;
228 
231 
233  int m_order;
234 
236  };
237 
239  void addEntry( ENTRY aEntry );
240 
242  std::list<ENTRY> m_entries;
243 };
244 
245 #endif /* CONDITIONAL_MENU_H */
ACTION_MENU * create() const override
Returns an instance of this class. It has to be overridden in inheriting classes.
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)
Adds a submenu to the menu.
PNG memory record (file in memory).
Definition: bitmap_def.h:29
Helper class to organize menu entries.
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:43
static const int ANY_ORDER
Constant to indicate that we do not care about an ENTRY location in the menu.
SELECTION_TOOL.
const TOOL_ACTION * action
ENTRY_TYPE
Possible entry types.
std::list< ENTRY > m_entries
List of all menu entries.
void AddCheckItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Adds a checked menu entry to run a TOOL_ACTION on selected items.
SELECTION_CONDITION m_condition
Condition to be fulfilled to show the entry in menu.
const SELECTION_CONDITION & Condition() const
void SetOrder(int aOrder)
int m_order
Order number, the higher the number the lower position it takes it is in the menu.
ENTRY(const wxMenuItem &aItem, const BITMAP_OPAQUE *aWxMenuBitmap, SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark)
void Evaluate(SELECTION &aSelection)
Updates the contents of the menu based on the supplied conditions.
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()
Updates the initial contents so that wxWidgets doesn't get its knickers tied in a knot over the menu ...
void AddSeparator(int aOrder=ANY_ORDER)
Adds a separator to the menu.
wxMenuItem * wxItem() const
const BITMAP_OPAQUE * GetIcon() const
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.
Represents a single user action.
Definition: tool_action.h:44
const BITMAP_OPAQUE * m_icon
ACTION_MENU * Menu() const
CONDITIONAL_MENU(TOOL_INTERACTIVE *aTool)
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Adds a menu entry to run a TOOL_ACTION on selected items.
ENTRY(const TOOL_ACTION *aAction, SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark)
union CONDITIONAL_MENU::ENTRY::@32 m_data
ENTRY(ACTION_MENU *aMenu, SELECTION_CONDITION aCondition, int aOrder)