KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Maciej Suminski <[email protected]>
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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef CONDITIONAL_MENU_H
24#define CONDITIONAL_MENU_H
25
27#include <tool/action_menu.h>
28#include <list>
29
31class TOOL_ACTION;
33
34enum class BITMAPS : unsigned int;
35
36
38{
39public:
41 static const int ANY_ORDER = -1;
42
44
45 ACTION_MENU* create() const override;
46
55 void AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
56 int aOrder = ANY_ORDER );
57
58 void AddItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
59 const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
60
72 void AddCheckItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
73 int aOrder = ANY_ORDER );
74
75 void AddCheckItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
76 const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
77
89 void AddMenu( ACTION_MENU* aMenu,
91 int aOrder = ANY_ORDER );
92
98 void AddSeparator( int aOrder = ANY_ORDER );
99
100 void AddSeparator( const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
101
105 void Evaluate( const SELECTION& aSelection );
106
112 void Resolve();
113
114private:
116 class ENTRY
117 {
118 public:
119 ENTRY( const TOOL_ACTION* aAction, SELECTION_CONDITION aCondition, int aOrder,
120 bool aCheckmark ) :
121 m_type( ACTION ),
122 m_icon( static_cast<BITMAPS>( 0 ) ),
123 m_condition( std::move( aCondition ) ),
124 m_order( aOrder ),
125 m_isCheckmarkEntry( aCheckmark )
126 {
127 m_data.action = aAction;
128 }
129
130 ENTRY( ACTION_MENU* aMenu, SELECTION_CONDITION aCondition, int aOrder ) :
131 m_type( MENU ),
132 m_icon( static_cast<BITMAPS>( 0 ) ),
133 m_condition( std::move( aCondition ) ),
134 m_order( aOrder ),
135 m_isCheckmarkEntry( false )
136 {
137 m_data.menu = aMenu;
138 }
139
140 ENTRY( const wxMenuItem& aItem, BITMAPS aBitmap,
141 SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
142 m_type( WXITEM ),
143 m_icon( aBitmap ),
144 m_condition( std::move( aCondition ) ),
145 m_order( aOrder ),
146 m_isCheckmarkEntry( aCheckmark )
147 {
148 m_data.wxItem = new wxMenuItem( nullptr, aItem.GetId(), aItem.GetItemLabel(),
149 aItem.GetHelp(), aItem.GetKind() );
150 }
151
152 // Separator
153 ENTRY( SELECTION_CONDITION aCondition, int aOrder ) :
154 m_type( SEPARATOR ),
155 m_icon( static_cast<BITMAPS>( 0 ) ),
156 m_data(),
157 m_condition( std::move( aCondition ) ),
158 m_order( aOrder ),
159 m_isCheckmarkEntry( false )
160 {
161 }
162
163 ENTRY( const ENTRY& aEntry );
164
165 ~ENTRY();
166
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;
234
237
240
242 };
243
245 void addEntry( ENTRY aEntry );
246
248 std::list<ENTRY> m_entries;
249};
250
251#endif /* CONDITIONAL_MENU_H */
BITMAPS
A list of all bitmap identifiers.
ACTION_MENU(bool isContextMenu, TOOL_INTERACTIVE *aTool=nullptr)
Default constructor.
friend class TOOL_INTERACTIVE
Helper class to organize menu entries.
int m_order
Order number, the higher the number the lower position it takes it is in the menu.
const TOOL_ACTION * Action() const
ENTRY_TYPE
Possible entry types.
ENTRY(const TOOL_ACTION *aAction, SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark)
union CONDITIONAL_MENU::ENTRY::@223133073310361045126170055313230071325071242350 m_data
const TOOL_ACTION * action
SELECTION_CONDITION m_condition
Condition to be fulfilled to show the entry in menu.
ENTRY(ACTION_MENU *aMenu, SELECTION_CONDITION aCondition, int aOrder)
ENTRY(const wxMenuItem &aItem, BITMAPS aBitmap, SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark)
wxMenuItem * wxItem() const
ACTION_MENU * Menu() const
ENTRY(SELECTION_CONDITION aCondition, int aOrder)
ENTRY_TYPE Type() const
const SELECTION_CONDITION & Condition() const
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.
ACTION_MENU * create() const override
Return an instance of this class. It has to be overridden in inheriting classes.
void addEntry(ENTRY aEntry)
Inserts the entry, preserving the requested order.
std::list< ENTRY > m_entries
List of all menu entries.
void AddSeparator(int aOrder=ANY_ORDER)
Add a separator to the menu.
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.
void AddMenu(ACTION_MENU *aMenu, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Add a submenu to the menu.
void Resolve()
Update the initial contents so that wxWidgets doesn't get its knickers tied in a knot over the menu b...
static const int ANY_ORDER
Constant to indicate that we do not care about an ENTRY location in the menu.
CONDITIONAL_MENU(TOOL_INTERACTIVE *aTool)
void Evaluate(const SELECTION &aSelection)
Update the contents of the menu based on the supplied conditions.
The selection tool: currently supports:
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
Represent a single user action.
STL namespace.
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.