KiCad PCB EDA Suite
context_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 
44 class CONTEXT_MENU : public wxMenu
45 {
46 public:
48  CONTEXT_MENU();
49 
50  virtual ~CONTEXT_MENU();
51 
52  CONTEXT_MENU( const CONTEXT_MENU& aMenu ) = delete;
53  CONTEXT_MENU& operator=( const CONTEXT_MENU& aMenu ) = delete;
54 
61  void SetTitle( const wxString& aTitle ) override;
62 
67  void DisplayTitle( bool aDisplay = true );
68 
74  inline void SetIcon( const BITMAP_OPAQUE* aIcon )
75  {
76  m_icon = aIcon;
77  }
78 
87  wxMenuItem* Add( const wxString& aLabel, int aId, const BITMAP_OPAQUE* aIcon = NULL );
88 
95  wxMenuItem* Add( const TOOL_ACTION& aAction );
96 
105  std::list<wxMenuItem*> Add( CONTEXT_MENU* aMenu, bool aExpand = false );
106 
112  void Clear();
113 
119  bool HasEnabledItems() const;
120 
127  inline int GetSelected() const
128  {
129  return m_selected;
130  }
131 
136  void UpdateAll();
137 
143  void SetTool( TOOL_INTERACTIVE* aTool );
144 
148  CONTEXT_MENU* Clone() const;
149 
150 protected:
152  virtual CONTEXT_MENU* create() const;
153 
156 
158  static inline int getMenuId( const TOOL_ACTION& aAction )
159  {
160  return aAction.GetId() + ACTION_ID;
161  }
162 
167  virtual void update()
168  {
169  }
170 
175  virtual OPT_TOOL_EVENT eventHandler( const wxMenuEvent& )
176  {
177  return OPT_TOOL_EVENT();
178  }
179 
184  void copyFrom( const CONTEXT_MENU& aMenu );
185 
186 private:
191  wxMenuItem* appendCopy( const wxMenuItem* aSource );
192 
194  void setupEvents();
195 
197  void onMenuEvent( wxMenuEvent& aEvent );
198 
200  void updateHotKeys();
201 
204  void runEventHandlers( const wxMenuEvent& aMenuEvent, OPT_TOOL_EVENT& aToolEvent );
205 
207  void runOnSubmenus( std::function<void(CONTEXT_MENU*)> aFunction );
208 
210  OPT_TOOL_EVENT findToolAction( int aId );
211 
214 
216  wxString m_title;
217 
220 
223 
225  static const int ACTION_ID = 2000;
226 
228  std::map<int, const TOOL_ACTION*> m_toolActions;
229 
231  std::list<CONTEXT_MENU*> m_submenus;
232 
235 
236  friend class TOOL_INTERACTIVE;
237 };
238 
239 #endif
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: context_menu.h:222
void updateHotKeys()
Updates hot key settings for TOOL_ACTIONs in this menu.
void runEventHandlers(const wxMenuEvent &aMenuEvent, OPT_TOOL_EVENT &aToolEvent)
Traverses the submenus tree looking for a submenu capable of handling a particular menu event...
PNG memory record (file in memory).
Definition: bitmap_types.h:38
int m_selected
Stores the id number of selected item.
Definition: context_menu.h:219
OPT_TOOL_EVENT findToolAction(int aId)
Checks if any of submenus contains a TOOL_ACTION with a specific ID.
Class CONTEXT_MENU.
Definition: context_menu.h:44
virtual void update()
Update menu state stub.
Definition: context_menu.h:167
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: context_menu.h:234
virtual OPT_TOOL_EVENT eventHandler(const wxMenuEvent &)
Event handler stub.
Definition: context_menu.h:175
static const int ACTION_ID
Menu items with ID higher than that are considered TOOL_ACTIONs
Definition: context_menu.h:225
void runOnSubmenus(std::function< void(CONTEXT_MENU *)> aFunction)
Runs a function on the menu and all its submenus.
void copyFrom(const CONTEXT_MENU &aMenu)
Copies another menus data to this instance.
int GetSelected() const
Function GetSelected() Returns the position of selected item.
Definition: context_menu.h:127
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon=NULL)
Function Add() Adds an entry to the menu.
boost::optional< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:460
CONTEXT_MENU & operator=(const CONTEXT_MENU &aMenu)=delete
Class TOOL_MANAGER.
Definition: tool_manager.h:49
CONTEXT_MENU * Clone() const
Creates a deep, recursive copy of this CONTEXT_MENU.
void setupEvents()
Initializes handlers for events.
std::map< int, const TOOL_ACTION * > m_toolActions
Associates tool actions with menu item IDs. Non-owning.
Definition: context_menu.h:228
void SetTool(TOOL_INTERACTIVE *aTool)
Function SetTool() Sets a tool that is the creator of the menu.
CONTEXT_MENU()
Default constructor
void Clear()
Function Clear() Removes all the entries from the menu (as well as its title).
bool HasEnabledItems() const
Function HasEnabledItems();.
static int getMenuId(const TOOL_ACTION &aAction)
Returns the corresponding wxMenuItem identifier for a TOOL_ACTION object.
Definition: context_menu.h:158
void SetIcon(const BITMAP_OPAQUE *aIcon)
Function SetIcon() Assigns an icon for the entry.
Definition: context_menu.h:74
virtual CONTEXT_MENU * create() const
Returns an instance of this class. It has to be overridden in inheriting classes. ...
void onMenuEvent(wxMenuEvent &aEvent)
The default menu event handler.
void UpdateAll()
Function UpdateAll() Runs update handlers for the menu and its submenus.
TOOL_MANAGER * getToolManager()
Returns an instance of TOOL_MANAGER class.
void SetTitle(const wxString &aTitle) override
Function SetTitle() Sets title for the context menu.
Class TOOL_ACTION.
Definition: tool_action.h:46
std::list< CONTEXT_MENU * > m_submenus
List of submenus.
Definition: context_menu.h:231
virtual ~CONTEXT_MENU()
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:85
void DisplayTitle(bool aDisplay=true)
Function DisplayTitle() Decides whether a title for a pop up menu should be displayed.
wxMenuItem * appendCopy(const wxMenuItem *aSource)
Function appendCopy Appends a copy of wxMenuItem.
wxString m_title
Menu title
Definition: context_menu.h:216
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: context_menu.h:213