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 <wx/textentry.h>
35 #include <tool/tool_action.h>
36 
37 class TOOL_INTERACTIVE;
38 
44 class ACTION_MENU : public wxMenu
45 {
46 public:
48  ACTION_MENU( bool isContextMenu );
49 
50  ~ACTION_MENU() override;
51 
52  ACTION_MENU( const ACTION_MENU& aMenu ) = delete;
53  ACTION_MENU& operator=( const ACTION_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 
81  wxMenuItem* Add( const wxString& aLabel, int aId, const BITMAP_OPAQUE* aIcon );
82  wxMenuItem* Add( const wxString& aLabel, const wxString& aToolTip, int aId,
83  const BITMAP_OPAQUE* aIcon );
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 
136  void ClearDirty();
137  void SetDirty();
138 
144  void SetTool( TOOL_INTERACTIVE* aTool );
145 
149  ACTION_MENU* Clone() const;
150 
151  void OnMenuEvent( wxMenuEvent& aEvent );
152  void OnIdle( wxIdleEvent& event );
153 
154 protected:
156  virtual ACTION_MENU* create() const;
157 
159  TOOL_MANAGER* getToolManager() const;
160 
162  static inline int getMenuId( const TOOL_ACTION& aAction )
163  {
164  return aAction.GetId() + ACTION_ID;
165  }
166 
171  virtual void update()
172  {
173  }
174 
179  virtual OPT_TOOL_EVENT eventHandler( const wxMenuEvent& )
180  {
181  return OPT_TOOL_EVENT();
182  }
183 
188  void copyFrom( const ACTION_MENU& aMenu );
189 
190 protected:
195  wxMenuItem* appendCopy( const wxMenuItem* aSource );
196 
198  void setupEvents();
199 
201  void updateHotKeys();
202 
205  void runEventHandlers( const wxMenuEvent& aMenuEvent, OPT_TOOL_EVENT& aToolEvent );
206 
208  void runOnSubmenus( std::function<void(ACTION_MENU*)> aFunction );
209 
211  OPT_TOOL_EVENT findToolAction( int aId );
212 
215 
216  bool m_dirty; // Menu requires update before display
217 
220 
222  wxString m_title;
223 
226 
229 
232 
234  static const int ACTION_ID = 20000;
235 
237  std::map<int, const TOOL_ACTION*> m_toolActions;
238 
240  std::list<ACTION_MENU*> m_submenus;
241 
242  friend class TOOL_INTERACTIVE;
243 };
244 
245 #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:171
bool m_titleDisplayed
Definition: action_menu.h:218
OPT_TOOL_EVENT findToolAction(int aId)
Checks if any of submenus contains a TOOL_ACTION with a specific ID.
PNG memory record (file in memory).
Definition: bitmap_def.h:29
static int getMenuId(const TOOL_ACTION &aAction)
Returns the corresponding wxMenuItem identifier for a TOOL_ACTION object.
Definition: action_menu.h:162
ACTION_MENU.
Definition: action_menu.h:44
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: action_menu.h:225
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 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:179
TOOL_MANAGER.
Definition: tool_manager.h:51
wxPoint m_forcedPosition
Definition: action_menu.h:214
void copyFrom(const ACTION_MENU &aMenu)
Copies another menus data to this instance.
bool m_isContextMenu
Definition: action_menu.h:219
wxString m_title
Menu title
Definition: action_menu.h:222
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:240
void SetIcon(const BITMAP_OPAQUE *aIcon)
Function SetIcon() Assigns an icon for the entry.
Definition: action_menu.cpp:70
ACTION_MENU & operator=(const ACTION_MENU &aMenu)=delete
int m_selected
Stores the id number of selected item.
Definition: action_menu.h:228
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:234
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:231
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:237
void OnMenuEvent(wxMenuEvent &aEvent)
bool m_isForcedPosition
Definition: action_menu.h:213
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:88
TOOL_MANAGER * getToolManager() const
Returns an instance of TOOL_MANAGER class.
TOOL_ACTION.
Definition: tool_action.h:46
void setupEvents()
Initializes handlers for events.
Definition: action_menu.cpp:76
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:45
void updateHotKeys()
Updates hot key settings for TOOL_ACTIONs in this menu.
~ACTION_MENU() override
Definition: action_menu.cpp:57
void SetDirty()
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:556
void DisplayTitle(bool aDisplay=true)
Function DisplayTitle() Decides whether a title for a pop up menu should be displayed.
Definition: action_menu.cpp:99