KiCad PCB EDA Suite
Loading...
Searching...
No Matches
action_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) 2013-2017 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Maciej Suminski <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, you may find one here:
22 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23 * or you may search the http://www.gnu.org website for the version 2 license,
24 * or you may write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 */
27
28#pragma once
29
30#include <map>
31#include <list>
32#include <functional>
33
34#include <wx/menu.h>
35#include <tool/tool_event.h>
36
37class KIFACE_BASE;
39class TOOL_MANAGER;
40
41enum class BITMAPS : unsigned int;
42
46class ACTION_MENU : public wxMenu
47{
48public:
50 ACTION_MENU( bool isContextMenu, TOOL_INTERACTIVE* aTool = nullptr );
51
52 ~ACTION_MENU() override;
53
54 ACTION_MENU( const ACTION_MENU& aMenu ) = delete;
55 ACTION_MENU& operator=( const ACTION_MENU& aMenu ) = delete;
56
63 void SetTitle( const wxString& aTitle ) override;
64
65 // Yes, it hides a non-virtual method in the parent class.
66 wxString GetTitle() const { return m_title; }
67
71 void DisplayTitle( bool aDisplay = true );
72
78 void SetIcon( BITMAPS aIcon );
79
85 wxMenuItem* Add( const wxString& aLabel, int aId, BITMAPS aIcon );
86 wxMenuItem* Add( const wxString& aLabel, const wxString& aToolTip, int aId,
87 BITMAPS aIcon, bool aIsCheckmarkEntry = false );
88
99 wxMenuItem* Add( const TOOL_ACTION& aAction, bool aIsCheckmarkEntry = false,
100 const wxString& aOverrideLabel = wxEmptyString );
101
111 wxMenuItem* Add( ACTION_MENU* aMenu );
112
120 void AddClose( const wxString& aAppname = "" );
121
130 void AddQuitOrClose( KIFACE_BASE* aKiface, wxString aAppname = "" );
131
139 void AddQuit( const wxString& aAppname = "" );
140
146 void Clear();
147
151 bool HasEnabledItems() const;
152
160 inline int GetSelected() const
161 {
162 return m_selected;
163 }
164
168 void UpdateAll();
169
173 virtual void UpdateTitle() {}
174
178 void ClearDirty();
179 void SetDirty();
180
186 void SetTool( TOOL_INTERACTIVE* aTool );
187
191 ACTION_MENU* Clone() const;
192
193 void OnMenuEvent( wxMenuEvent& aEvent );
194 void OnIdle( wxIdleEvent& event );
195
196 virtual bool PassHelpTextToHandler() { return false; }
197
198 static constexpr bool NORMAL = false;
199 static constexpr bool CHECK = true;
200
201protected:
203 virtual ACTION_MENU* create() const;
204
207
214 virtual void update()
215 {
216 }
217
224 virtual OPT_TOOL_EVENT eventHandler( const wxMenuEvent& )
225 {
226 return OPT_TOOL_EVENT();
227 }
228
234 void copyFrom( const ACTION_MENU& aMenu );
235
236protected:
240 wxMenuItem* appendCopy( const wxMenuItem* aSource );
241
243 void setupEvents();
244
246 void updateHotKeys();
247
250 void runEventHandlers( const wxMenuEvent& aMenuEvent, OPT_TOOL_EVENT& aToolEvent );
251
253 void runOnSubmenus( std::function<void(ACTION_MENU*)> aFunction );
254
257
260
261 bool m_dirty; // Menu requires update before display
262
265
267 wxString m_title;
268
271
274
277
279 std::map<int, const TOOL_ACTION*> m_toolActions;
280
282 std::list<ACTION_MENU*> m_submenus;
283
284 friend class TOOL_INTERACTIVE;
285};
BITMAPS
A list of all bitmap identifiers.
Define the structure of a menu based on ACTIONs.
Definition action_menu.h:47
void updateHotKeys()
Update hot key settings for TOOL_ACTIONs in this menu.
ACTION_MENU(const ACTION_MENU &aMenu)=delete
ACTION_MENU(bool isContextMenu, TOOL_INTERACTIVE *aTool=nullptr)
Default constructor.
wxPoint m_forcedPosition
TOOL_MANAGER * getToolManager() const
Return an instance of TOOL_MANAGER class.
void setupEvents()
Initialize handlers for events.
wxString m_title
Menu title.
virtual ACTION_MENU * create() const
Return an instance of this class. It has to be overridden in inheriting classes.
virtual void update()
Update menu state stub.
void AddQuitOrClose(KIFACE_BASE *aKiface, wxString aAppname="")
Add either a standard Quit or Close item to the menu.
void ClearDirty()
Clear the dirty flag on the menu and all descendants.
bool m_isForcedPosition
wxMenuItem * appendCopy(const wxMenuItem *aSource)
Append a copy of wxMenuItem.
OPT_TOOL_EVENT findToolAction(int aId)
Check if any of submenus contains a TOOL_ACTION with a specific ID.
ACTION_MENU & operator=(const ACTION_MENU &aMenu)=delete
void runEventHandlers(const wxMenuEvent &aMenuEvent, OPT_TOOL_EVENT &aToolEvent)
Traverse the submenus tree looking for a submenu capable of handling a particular menu event.
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
bool m_isContextMenu
virtual OPT_TOOL_EVENT eventHandler(const wxMenuEvent &)
Event handler stub.
static constexpr bool CHECK
void OnMenuEvent(wxMenuEvent &aEvent)
void AddClose(const wxString &aAppname="")
Add a standard close item to the menu with the accelerator key CTRL-W.
void DisplayTitle(bool aDisplay=true)
Decide whether a title for a pop up menu should be displayed.
void AddQuit(const wxString &aAppname="")
Add a standard Quit item to the menu.
ACTION_MENU * Clone() const
Create a deep, recursive copy of this ACTION_MENU.
void Clear()
Remove all the entries from the menu (as well as its title).
void UpdateAll()
Run update handlers for the menu and its submenus.
void SetTitle(const wxString &aTitle) override
Set title for the menu.
static constexpr bool NORMAL
void SetTool(TOOL_INTERACTIVE *aTool)
Set a tool that is the creator of the menu.
void OnIdle(wxIdleEvent &event)
std::list< ACTION_MENU * > m_submenus
List of submenus.
void SetIcon(BITMAPS aIcon)
Assign an icon for the entry.
~ACTION_MENU() override
virtual void UpdateTitle()
Used by some menus to just-in-time translate their titles.
wxString GetTitle() const
Definition action_menu.h:66
wxMenuItem * Add(const wxString &aLabel, int aId, BITMAPS aIcon)
Add a wxWidgets-style entry to the menu.
bool m_titleDisplayed
int GetSelected() const
Return the position of selected item.
friend class TOOL_INTERACTIVE
int m_selected
Store the id number of selected item.
BITMAPS m_icon
Optional icon.
bool HasEnabledItems() const
Return true if the menu has any enabled items.
TOOL_INTERACTIVE * m_tool
Creator of the menu.
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Run a function on the menu and all its submenus.
virtual bool PassHelpTextToHandler()
void copyFrom(const ACTION_MENU &aMenu)
Copy another menus data to this instance.
A KIFACE implementation.
Definition kiface_base.h:39
Represent a single user action.
Master controller class:
std::optional< TOOL_EVENT > OPT_TOOL_EVENT
Definition tool_event.h:641