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 * Copyright (C) 2020 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, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#ifndef CONDITIONAL_MENU_H
28#define CONDITIONAL_MENU_H
29
31#include <tool/action_menu.h>
32#include <list>
33
35class TOOL_ACTION;
37
38enum class BITMAPS : unsigned int;
39
40
42{
43public:
45 static const int ANY_ORDER = -1;
46
48
49 ACTION_MENU* create() const override;
50
59 void AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
60 int aOrder = ANY_ORDER );
61
62 void AddItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
63 const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
64
76 void AddCheckItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
77 int aOrder = ANY_ORDER );
78
79 void AddCheckItem( int aId, const wxString& aText, const wxString& aTooltip, BITMAPS aIcon,
80 const SELECTION_CONDITION& aCondition, int aOrder = ANY_ORDER );
81
95 void AddMenu( ACTION_MENU* aMenu,
97 int aOrder = ANY_ORDER );
98
104 void AddSeparator( int aOrder = ANY_ORDER );
105
106 void AddSeparator( const SELECTION_CONDITION& aCondition, int aOrder );
107
111 void Evaluate( SELECTION& aSelection );
112
118 void Resolve();
119
120private:
122 class ENTRY
123 {
124 public:
125 ENTRY( const TOOL_ACTION* aAction, SELECTION_CONDITION aCondition, int aOrder,
126 bool aCheckmark ) :
127 m_type( ACTION ),
128 m_icon( static_cast<BITMAPS>( 0 ) ),
129 m_condition( aCondition ),
130 m_order( aOrder ),
131 m_isCheckmarkEntry( aCheckmark )
132 {
133 m_data.action = aAction;
134 }
135
136 ENTRY( ACTION_MENU* aMenu, SELECTION_CONDITION aCondition, int aOrder ) :
137 m_type( MENU ),
138 m_icon( static_cast<BITMAPS>( 0 ) ),
139 m_condition( aCondition ),
140 m_order( aOrder ),
141 m_isCheckmarkEntry( false )
142 {
143 m_data.menu = aMenu;
144 }
145
146 ENTRY( const wxMenuItem& aItem, BITMAPS aBitmap,
147 SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark ) :
148 m_type( WXITEM ),
149 m_icon( aBitmap ),
150 m_condition( aCondition ),
151 m_order( aOrder ),
152 m_isCheckmarkEntry( aCheckmark )
153 {
154 m_data.wxItem = new wxMenuItem( nullptr, aItem.GetId(), aItem.GetItemLabel(),
155 aItem.GetHelp(), aItem.GetKind() );
156 }
157
158 // Separator
159 ENTRY( SELECTION_CONDITION aCondition, int aOrder ) :
160 m_type( SEPARATOR ),
161 m_icon( static_cast<BITMAPS>( 0 ) ),
162 m_data(),
163 m_condition( aCondition ),
164 m_order( aOrder ),
165 m_isCheckmarkEntry( false )
166 {
167 }
168
169 ENTRY( const ENTRY& aEntry );
170
171 ~ENTRY();
172
179 };
180
181 inline ENTRY_TYPE Type() const
182 {
183 return m_type;
184 }
185
186 inline BITMAPS GetIcon() const
187 {
188 return m_icon;
189 }
190
191 inline const TOOL_ACTION* Action() const
192 {
193 assert( m_type == ACTION );
194 return m_data.action;
195 }
196
197 inline ACTION_MENU* Menu() const
198 {
199 assert( m_type == MENU );
200 return m_data.menu;
201 }
202
203 inline wxMenuItem* wxItem() const
204 {
205 assert( m_type == WXITEM );
206 return m_data.wxItem;
207 }
208
209 inline bool IsCheckmarkEntry() const
210 {
211 return m_isCheckmarkEntry;
212 }
213
214 inline const SELECTION_CONDITION& Condition() const
215 {
216 return m_condition;
217 }
218
219 inline int Order() const
220 {
221 return m_order;
222 }
223
224 inline void SetOrder( int aOrder )
225 {
226 m_order = aOrder;
227 }
228
229 private:
232
233 // This class owns the wxItem object and needs to create, copy and delete it accordingly
234 // But it does not own the action nor menu item
235 union {
238 wxMenuItem* wxItem;
239 } m_data;
240
243
246
248 };
249
251 void addEntry( ENTRY aEntry );
252
254 std::list<ENTRY> m_entries;
255};
256
257#endif /* CONDITIONAL_MENU_H */
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:49
< Helper class to organize menu entries. Inserts the entry, preserving the requested order.
const TOOL_ACTION * Action() const
void SetOrder(int aOrder)
ENTRY(const TOOL_ACTION *aAction, SELECTION_CONDITION aCondition, int aOrder, bool aCheckmark)
~ENTRY()
Possible entry types.
const TOOL_ACTION * action
union CONDITIONAL_MENU::ENTRY::@32 m_data
Condition to be fulfilled to show the entry in menu.
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(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.
Definition: tool_action.h:68
std::function< bool(const SELECTION &)> SELECTION_CONDITION
< Functor type that checks a specific condition for selected items.