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  void SetIcon( const BITMAP_OPAQUE* aIcon );
75 
84  wxMenuItem* Add( const wxString& aLabel, int aId, const BITMAP_OPAQUE* aIcon = NULL );
85 
92  wxMenuItem* Add( const TOOL_ACTION& aAction );
93 
102  std::list<wxMenuItem*> Add( CONTEXT_MENU* aMenu, bool aExpand = false );
103 
109  void Clear();
110 
116  bool HasEnabledItems() const;
117 
124  inline int GetSelected() const
125  {
126  return m_selected;
127  }
128 
133  void UpdateAll();
134 
140  void SetTool( TOOL_INTERACTIVE* aTool );
141 
145  CONTEXT_MENU* Clone() const;
146 
147 protected:
149  virtual CONTEXT_MENU* create() const;
150 
152  TOOL_MANAGER* getToolManager() const;
153 
155  static inline int getMenuId( const TOOL_ACTION& aAction )
156  {
157  return aAction.GetId() + ACTION_ID;
158  }
159 
164  virtual void update()
165  {
166  }
167 
172  virtual OPT_TOOL_EVENT eventHandler( const wxMenuEvent& )
173  {
174  return OPT_TOOL_EVENT();
175  }
176 
181  void copyFrom( const CONTEXT_MENU& aMenu );
182 
183 private:
188  wxMenuItem* appendCopy( const wxMenuItem* aSource );
189 
191  void setupEvents();
192 
194  void onMenuEvent( wxMenuEvent& aEvent );
195 
197  void updateHotKeys();
198 
201  void runEventHandlers( const wxMenuEvent& aMenuEvent, OPT_TOOL_EVENT& aToolEvent );
202 
204  void runOnSubmenus( std::function<void(CONTEXT_MENU*)> aFunction );
205 
207  OPT_TOOL_EVENT findToolAction( int aId );
208 
211 
213  wxString m_title;
214 
217 
220 
222  static const int ACTION_ID = 2000;
223 
225  std::map<int, const TOOL_ACTION*> m_toolActions;
226 
228  std::list<CONTEXT_MENU*> m_submenus;
229 
232 
233  friend class TOOL_INTERACTIVE;
234 };
235 
236 #endif
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: context_menu.h:219
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:41
int m_selected
Stores the id number of selected item.
Definition: context_menu.h:216
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:164
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: context_menu.h:231
virtual OPT_TOOL_EVENT eventHandler(const wxMenuEvent &)
Event handler stub.
Definition: context_menu.h:172
static const int ACTION_ID
Menu items with ID higher than that are considered TOOL_ACTIONs
Definition: context_menu.h:222
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:124
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon=NULL)
Function Add() Adds an entry to the menu.
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:225
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:155
void SetIcon(const BITMAP_OPAQUE *aIcon)
Function SetIcon() Assigns an icon for the entry.
TOOL_MANAGER * getToolManager() const
Returns an instance of TOOL_MANAGER class.
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.
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:228
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:213
bool m_titleDisplayed
Flag indicating that the menu title was set up.
Definition: context_menu.h:210
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:495