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 class KIFACE_I;
37 
38 
40 {
41 public:
43  static const int ANY_ORDER = -1;
44 
45  CONDITIONAL_MENU( bool isContextMenu, TOOL_INTERACTIVE* aTool );
46 
47  ACTION_MENU* create() const override;
48 
58  void AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
59  int aOrder = ANY_ORDER );
60 
61  void AddItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAP_DEF aIcon,
62  const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
63 
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 
91  void AddMenu( ACTION_MENU* aMenu,
93  int aOrder = ANY_ORDER );
94 
101  void AddSeparator( int aOrder = ANY_ORDER );
102 
111  void AddClose( wxString aAppname = "" );
112 
121  void AddQuitOrClose( KIFACE_I* aKiface, wxString aAppname = "" );
122 
128  void Evaluate( SELECTION& aSelection );
129 
137  void Resolve();
138 
139 private:
141  class ENTRY
142  {
143  public:
144  ENTRY( const TOOL_ACTION* aAction, SELECTION_CONDITION aCondition, int aOrder,
145  bool aCheckmark ) :
146  m_type( ACTION ), m_icon(nullptr),
147  m_condition( aCondition ),
148  m_order( aOrder ),
149  m_isCheckmarkEntry( aCheckmark )
150  {
151  m_data.action = aAction;
152  }
153 
154  ENTRY( ACTION_MENU* aMenu, SELECTION_CONDITION aCondition, int aOrder ) :
155  m_type( MENU ), m_icon(nullptr),
156  m_condition( aCondition ),
157  m_order( aOrder ),
158  m_isCheckmarkEntry( false )
159  {
160  m_data.menu = aMenu;
161  }
162 
163  ENTRY( wxMenuItem* aItem, const BITMAP_OPAQUE* aWxMenuBitmap,
164  SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
165  m_type( WXITEM ), m_icon( aWxMenuBitmap ),
166  m_condition( aCondition ),
167  m_order( aOrder ),
168  m_isCheckmarkEntry( aCheckmark )
169  {
170  m_data.wxItem = aItem;
171  }
172 
173  // Separator
174  ENTRY( SELECTION_CONDITION aCondition, int aOrder ) :
175  m_type( SEPARATOR ), m_icon(nullptr),
176  m_condition( aCondition ),
177  m_order( aOrder ),
178  m_isCheckmarkEntry( false )
179  {
180  }
181 
183  enum ENTRY_TYPE {
188  };
189 
190  inline ENTRY_TYPE Type() const
191  {
192  return m_type;
193  }
194 
195  inline const BITMAP_OPAQUE* GetIcon() const
196  {
197  return m_icon;
198  }
199 
200  inline const TOOL_ACTION* Action() const
201  {
202  assert( m_type == ACTION );
203  return m_data.action;
204  }
205 
206  inline ACTION_MENU* Menu() const
207  {
208  assert( m_type == MENU );
209  return m_data.menu;
210  }
211 
212  inline wxMenuItem* wxItem() const
213  {
214  assert( m_type == WXITEM );
215  return m_data.wxItem;
216  }
217 
218  inline bool IsCheckmarkEntry() const
219  {
220  return m_isCheckmarkEntry;
221  }
222 
223  inline const SELECTION_CONDITION& Condition() const
224  {
225  return m_condition;
226  }
227 
228  inline int Order() const
229  {
230  return m_order;
231  }
232 
233  inline void SetOrder( int aOrder )
234  {
235  m_order = aOrder;
236  }
237 
238  private:
241 
242  union {
245  wxMenuItem* wxItem;
246  } m_data;
247 
250 
252  int m_order;
253 
255  };
256 
258  void addEntry( ENTRY aEntry );
259 
261  std::list<ENTRY> m_entries;
262 };
263 
264 #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)
Function ShowAlways The default condition function (always returns true).
void AddMenu(ACTION_MENU *aMenu, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Function AddMenu()
KIFACE_I is a KIFACE (I)mplementation, with some features useful for DSOs which implement a KIFACE.
Definition: kiface_i.h:37
PNG memory record (file in memory).
Definition: bitmap_def.h:29
Helper class to organize menu entries.
ACTION_MENU.
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.
union CONDITIONAL_MENU::ENTRY::@37 m_data
std::list< ENTRY > m_entries
List of all menu entries.
CONDITIONAL_MENU(bool isContextMenu, TOOL_INTERACTIVE *aTool)
void AddCheckItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddCheckItem()
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.
void Evaluate(SELECTION &aSelection)
Function Evaluate()
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()
Function Resolve()
void AddSeparator(int aOrder=ANY_ORDER)
Function AddSeparator()
void AddQuitOrClose(KIFACE_I *aKiface, wxString aAppname="")
Functions AddQuitOrClose()
void AddClose(wxString aAppname="")
Function AddClose()
wxMenuItem * wxItem() const
const BITMAP_OPAQUE * GetIcon() const
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.
ENTRY(wxMenuItem *aItem, const BITMAP_OPAQUE *aWxMenuBitmap, SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark)
TOOL_ACTION.
Definition: tool_action.h:46
const BITMAP_OPAQUE * m_icon
ACTION_MENU * Menu() const
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
ENTRY(const TOOL_ACTION *aAction, SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark)
ENTRY(ACTION_MENU *aMenu, SELECTION_CONDITION aCondition, int aOrder)