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( bool isContextMenu );
48 
49  ~ACTION_MENU() override;
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 
80  wxMenuItem* Add( const wxString& aLabel, int aId, const BITMAP_OPAQUE* aIcon );
81  wxMenuItem* Add( const wxString& aLabel, const wxString& aToolTip, int aId,
82  const BITMAP_OPAQUE* aIcon );
83 
90  wxMenuItem* Add( const TOOL_ACTION& aAction, bool aIsCheckmarkEntry = false );
91 
98  wxMenuItem* Add( ACTION_MENU* aMenu );
99 
105  void Clear();
106 
112  bool HasEnabledItems() const;
113 
120  inline int GetSelected() const
121  {
122  return m_selected;
123  }
124 
129  void UpdateAll();
130 
135  void ClearDirty();
136  void SetDirty();
137 
143  void SetTool( TOOL_INTERACTIVE* aTool );
144 
148  ACTION_MENU* Clone() const;
149 
150  void OnMenuEvent( wxMenuEvent& aEvent );
151  void OnIdle( wxIdleEvent& event );
152 
153 protected:
155  virtual ACTION_MENU* create() const;
156 
158  TOOL_MANAGER* getToolManager() const;
159 
161  static inline int getMenuId( const TOOL_ACTION& aAction )
162  {
163  return aAction.GetId() + ACTION_ID;
164  }
165 
170  virtual void update()
171  {
172  }
173 
178  virtual OPT_TOOL_EVENT eventHandler( const wxMenuEvent& )
179  {
180  return OPT_TOOL_EVENT();
181  }
182 
187  void copyFrom( const ACTION_MENU& aMenu );
188 
189 protected:
194  wxMenuItem* appendCopy( const wxMenuItem* aSource );
195 
197  void setupEvents();
198 
200  void updateHotKeys();
201 
204  void runEventHandlers( const wxMenuEvent& aMenuEvent, OPT_TOOL_EVENT& aToolEvent );
205 
207  void runOnSubmenus( std::function<void(ACTION_MENU*)> aFunction );
208 
210  OPT_TOOL_EVENT findToolAction( int aId );
211 
212  bool m_dirty; // Menu requires update before display
213 
216 
218  wxString m_title;
219 
221  const BITMAP_OPAQUE* m_icon;
222 
225 
228 
230  static const int ACTION_ID = 20000;
231 
233  std::map<int, const TOOL_ACTION*> m_toolActions;
234 
236  std::list<ACTION_MENU*> m_submenus;
237 
238  friend class TOOL_INTERACTIVE;
239 };
240 
241 #endif
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon)
Function Add() Adds a wxWidgets-style entry to the menu.
virtual void update()
Update menu state stub.
Definition: action_menu.h:170
bool m_titleDisplayed
Definition: action_menu.h:214
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:161
Class ACTION_MENU.
Definition: action_menu.h:43
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: action_menu.h:221
int GetSelected() const
Function GetSelected() Returns the position of selected item.
Definition: action_menu.h:120
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 ClearDirty()
Function ClearDirty() Clears the dirty flag on the menu and all descendants.
void UpdateAll()
Function UpdateAll() Runs update handlers for the menu and its submenus.
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:178
Class TOOL_MANAGER.
Definition: tool_manager.h:50
void copyFrom(const ACTION_MENU &aMenu)
Copies another menus data to this instance.
bool m_isContextMenu
Definition: action_menu.h:215
wxString m_title
Menu title
Definition: action_menu.h:218
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:236
void SetIcon(const BITMAP_OPAQUE *aIcon)
Function SetIcon() Assigns an icon for the entry.
Definition: action_menu.cpp:68
ACTION_MENU & operator=(const ACTION_MENU &aMenu)=delete
int m_selected
Stores the id number of selected item.
Definition: action_menu.h:224
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:230
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:227
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:233
void OnMenuEvent(wxMenuEvent &aEvent)
int GetId() const
Function GetId() Returns the unique id of the TOOL_ACTION object.
Definition: tool_action.h:100
void SetTitle(const wxString &aTitle) override
Function SetTitle() Sets title for the menu.
Definition: action_menu.cpp:86
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:74
void Clear()
Function Clear() Removes all the entries from the menu (as well as its title).
bool HasEnabledItems() const
Function HasEnabledItems();.
void OnIdle(wxIdleEvent &event)
ACTION_MENU(bool isContextMenu)
Default constructor
Definition: action_menu.cpp:43
void updateHotKeys()
Updates hot key settings for TOOL_ACTIONs in this menu.
~ACTION_MENU() override
Definition: action_menu.cpp:55
void SetDirty()
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:548
void DisplayTitle(bool aDisplay=true)
Function DisplayTitle() Decides whether a title for a pop up menu should be displayed.
Definition: action_menu.cpp:97