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
91 void AddMenu( ACTION_MENU* aMenu,
93 int aOrder = ANY_ORDER );
94
100 void AddSeparator( int aOrder = ANY_ORDER );
101
102 void AddSeparator( const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
103
107 void Evaluate( const SELECTION& aSelection );
108
114 void Resolve();
115
116private:
118 class ENTRY
119 {
120 public:
121 ENTRY( const TOOL_ACTION* aAction, SELECTION_CONDITION aCondition, int aOrder,
122 bool aCheckmark ) :
123 m_type( ACTION ),
124 m_icon( static_cast<BITMAPS>( 0 ) ),
125 m_condition( std::move( aCondition ) ),
126 m_order( aOrder ),
127 m_isCheckmarkEntry( aCheckmark )
128 {
129 m_data.action = aAction;
130 }
131
132 ENTRY( ACTION_MENU* aMenu, SELECTION_CONDITION aCondition, int aOrder ) :
133 m_type( MENU ),
134 m_icon( static_cast<BITMAPS>( 0 ) ),
135 m_condition( std::move( aCondition ) ),
136 m_order( aOrder ),
137 m_isCheckmarkEntry( false )
138 {
139 m_data.menu = aMenu;
140 }
141
142 ENTRY( const wxMenuItem& aItem, BITMAPS aBitmap,
143 SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
144 m_type( WXITEM ),
145 m_icon( aBitmap ),
146 m_condition( std::move( aCondition ) ),
147 m_order( aOrder ),
148 m_isCheckmarkEntry( aCheckmark )
149 {
150 m_data.wxItem = new wxMenuItem( nullptr, aItem.GetId(), aItem.GetItemLabel(),
151 aItem.GetHelp(), aItem.GetKind() );
152 }
153
154 // Separator
155 ENTRY( SELECTION_CONDITION aCondition, int aOrder ) :
156 m_type( SEPARATOR ),
157 m_icon( static_cast<BITMAPS>( 0 ) ),
158 m_data(),
159 m_condition( std::move( aCondition ) ),
160 m_order( aOrder ),
161 m_isCheckmarkEntry( false )
162 {
163 }
164
165 ENTRY( const ENTRY& aEntry );
166
167 ~ENTRY();
168
176
177 inline ENTRY_TYPE Type() const
178 {
179 return m_type;
180 }
181
182 inline BITMAPS GetIcon() const
183 {
184 return m_icon;
185 }
186
187 inline const TOOL_ACTION* Action() const
188 {
189 assert( m_type == ACTION );
190 return m_data.action;
191 }
192
193 inline ACTION_MENU* Menu() const
194 {
195 assert( m_type == MENU );
196 return m_data.menu;
197 }
198
199 inline wxMenuItem* wxItem() const
200 {
201 assert( m_type == WXITEM );
202 return m_data.wxItem;
203 }
204
205 inline bool IsCheckmarkEntry() const
206 {
207 return m_isCheckmarkEntry;
208 }
209
210 inline const SELECTION_CONDITION& Condition() const
211 {
212 return m_condition;
213 }
214
215 inline int Order() const
216 {
217 return m_order;
218 }
219
220 inline void SetOrder( int aOrder )
221 {
222 m_order = aOrder;
223 }
224
225 private:
228
229 // This class owns the wxItem object and needs to create, copy and delete it accordingly
230 // But it does not own the action nor menu item
231 union {
234 wxMenuItem* wxItem;
235 } m_data;
236
239
242
244 };
245
247 void addEntry( ENTRY aEntry );
248
250 std::list<ENTRY> m_entries;
251};
252
253#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.Inserts the entry, preserving the requested order.
const TOOL_ACTION * Action() const
ENTRY(const TOOL_ACTION *aAction, SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark)
~ENTRY()
Possible entry types.
union CONDITIONAL_MENU::ENTRY::@223133073310361045126170055313230071325071242350 m_data
Condition to be fulfilled to show the entry in menu.
const TOOL_ACTION * action
SELECTION_CONDITION m_condition
Order number, the higher the number the lower position it takes it is in the 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)
List of all menu entries.
std::list< ENTRY > m_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.