KiCad PCB EDA Suite
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  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef __CONTEXT_MENU_H
27 #define __CONTEXT_MENU_H
28 
29 #include <map>
30 #include <list>
31 #include <functional>
32 
33 #include <wx/menu.h>
34 #include <tool/tool_action.h>
35 
36 class TOOL_INTERACTIVE;
37 
43 class ACTION_MENU : public wxMenu
44 {
45 public:
47  ACTION_MENU();
48 
49  virtual ~ACTION_MENU();
50 
51  ACTION_MENU( const ACTION_MENU& aMenu ) = delete;
52  ACTION_MENU& operator=( const ACTION_MENU& aMenu ) = delete;
53 
60  void SetTitle( const wxString& aTitle ) override;
61 
66  void DisplayTitle( bool aDisplay = true );
67 
73  void SetIcon( const BITMAP_OPAQUE* aIcon );
74 
83  wxMenuItem* Add( const wxString& aLabel, int aId, const BITMAP_OPAQUE* aIcon = NULL );
84 
91  wxMenuItem* Add( const TOOL_ACTION& aAction, bool aIsCheckmarkEntry = false );
92 
99  wxMenuItem* Add( ACTION_MENU* aMenu );
100 
106  void Clear();
107 
113  bool HasEnabledItems() const;
114 
121  inline int GetSelected() const
122  {
123  return m_selected;
124  }
125 
130  void UpdateAll();
131 
137  void SetTool( TOOL_INTERACTIVE* aTool );
138 
142  ACTION_MENU* Clone() const;
143 
144 public:
146  bool m_Dirty;
147 
148 protected:
150  virtual ACTION_MENU* create() const;
151 
153  TOOL_MANAGER* getToolManager() const;
154 
156  static inline int getMenuId( const TOOL_ACTION& aAction )
157  {
158  return aAction.GetId() + ACTION_ID;
159  }
160 
165  virtual void update()
166  {
167  }
168 
173  virtual OPT_TOOL_EVENT eventHandler( const wxMenuEvent& )
174  {
175  return OPT_TOOL_EVENT();
176  }
177 
182  void copyFrom( const ACTION_MENU& aMenu );
183 
184 protected:
189  wxMenuItem* appendCopy( const wxMenuItem* aSource );
190 
192  void setupEvents();
193 
195  void onMenuEvent( wxMenuEvent& aEvent );
196 
198  void updateHotKeys();
199 
202  void runEventHandlers( const wxMenuEvent& aMenuEvent, OPT_TOOL_EVENT& aToolEvent );
203 
205  void runOnSubmenus( std::function<void(ACTION_MENU*)> aFunction );
206 
208  OPT_TOOL_EVENT findToolAction( int aId );
209 
212 
214  wxString m_title;
215 
218 
221 
223  static const int ACTION_ID = 20000;
224 
226  std::map<int, const TOOL_ACTION*> m_toolActions;
227 
229  std::list<ACTION_MENU*> m_submenus;
230 
232  const BITMAP_OPAQUE* m_icon;
233 
234  friend class TOOL_INTERACTIVE;
235 };
236 
237 #endif
virtual void update()
Update menu state stub.
Definition: action_menu.h:165
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: action_menu.h:211
OPT_TOOL_EVENT findToolAction(int aId)
Checks if any of submenus contains a TOOL_ACTION with a specific ID.
static int getMenuId(const TOOL_ACTION &aAction)
Returns the corresponding wxMenuItem identifier for a TOOL_ACTION object.
Definition: action_menu.h:156
Class ACTION_MENU.
Definition: action_menu.h:43
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: action_menu.h:232
int GetSelected() const
Function GetSelected() Returns the position of selected item.
Definition: action_menu.h:121
void runEventHandlers(const wxMenuEvent &aMenuEvent, OPT_TOOL_EVENT &aToolEvent)
Traverses the submenus tree looking for a submenu capable of handling a particular menu event.
void UpdateAll()
Function UpdateAll() Runs update handlers for the menu and its submenus.
ACTION_MENU()
Default constructor
Definition: action_menu.cpp:40
virtual ACTION_MENU * create() const
Returns an instance of this class. It has to be overridden in inheriting classes.
void SetTool(TOOL_INTERACTIVE *aTool)
Function SetTool() Sets a tool that is the creator of the menu.
virtual OPT_TOOL_EVENT eventHandler(const wxMenuEvent &)
Event handler stub.
Definition: action_menu.h:173
Class TOOL_MANAGER.
Definition: tool_manager.h:49
void copyFrom(const ACTION_MENU &aMenu)
Copies another menus data to this instance.
virtual ~ACTION_MENU()
Definition: action_menu.cpp:51
wxString m_title
Menu title
Definition: action_menu.h:214
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:229
void SetIcon(const BITMAP_OPAQUE *aIcon)
Function SetIcon() Assigns an icon for the entry.
Definition: action_menu.cpp:82
ACTION_MENU & operator=(const ACTION_MENU &aMenu)=delete
int m_selected
Stores the id number of selected item.
Definition: action_menu.h:217
ACTION_MENU * Clone() const
Creates a deep, recursive copy of this ACTION_MENU.
static const int ACTION_ID
Menu items with ID higher than that are considered TOOL_ACTIONs
Definition: action_menu.h:223
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:220
wxMenuItem * appendCopy(const wxMenuItem *aSource)
Function appendCopy Appends a copy of wxMenuItem.
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Runs a function on the menu and all its submenus.
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: action_menu.h:226
bool m_Dirty
Menu requires updating before display.
Definition: action_menu.h:146
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:85
void SetTitle(const wxString &aTitle) override
Function SetTitle() Sets title for the menu.
Definition: action_menu.cpp:96
TOOL_MANAGER * getToolManager() const
Returns an instance of TOOL_MANAGER class.
Class TOOL_ACTION.
Definition: tool_action.h:46
void setupEvents()
Initializes handlers for events.
Definition: action_menu.cpp:88
void Clear()
Function Clear() Removes all the entries from the menu (as well as its title).
bool HasEnabledItems() const
Function HasEnabledItems();.
void onMenuEvent(wxMenuEvent &aEvent)
The default menu event handler.
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon=NULL)
Function Add() Adds an entry to the menu.
void updateHotKeys()
Updates hot key settings for TOOL_ACTIONs in this menu.
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:486
void DisplayTitle(bool aDisplay=true)
Function DisplayTitle() Decides whether a title for a pop up menu should be displayed.