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 KIFACE_I;
38 class TOOL_INTERACTIVE;
39 
43 class ACTION_MENU : public wxMenu
44 {
45 public:
47  ACTION_MENU( bool isContextMenu, TOOL_INTERACTIVE* aTool = nullptr );
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 
65  void DisplayTitle( bool aDisplay = true );
66 
72  void SetIcon( const BITMAP_OPAQUE* aIcon );
73 
78  wxMenuItem* Add( const wxString& aLabel, int aId, const BITMAP_OPAQUE* aIcon );
79  wxMenuItem* Add( const wxString& aLabel, const wxString& aToolTip, int aId,
80  const BITMAP_OPAQUE* aIcon, bool aIsCheckmarkEntry = false );
81 
88  wxMenuItem* Add( const TOOL_ACTION& aAction, bool aIsCheckmarkEntry = false );
89 
96  wxMenuItem* Add( ACTION_MENU* aMenu );
97 
104  void AddClose( wxString aAppname = "" );
105 
112  void AddQuitOrClose( KIFACE_I* aKiface, wxString aAppname = "" );
113 
118  void Clear();
119 
123  bool HasEnabledItems() const;
124 
131  inline int GetSelected() const
132  {
133  return m_selected;
134  }
135 
139  void UpdateAll();
140 
144  void ClearDirty();
145  void SetDirty();
146 
152  void SetTool( TOOL_INTERACTIVE* aTool );
153 
157  ACTION_MENU* Clone() const;
158 
159  void OnMenuEvent( wxMenuEvent& aEvent );
160  void OnIdle( wxIdleEvent& event );
161 
162  static constexpr bool CHECK = true;
163 
164 protected:
166  virtual ACTION_MENU* create() const;
167 
169  TOOL_MANAGER* getToolManager() const;
170 
175  virtual void update()
176  {
177  }
178 
183  virtual OPT_TOOL_EVENT eventHandler( const wxMenuEvent& )
184  {
185  return OPT_TOOL_EVENT();
186  }
187 
192  void copyFrom( const ACTION_MENU& aMenu );
193 
194 protected:
199  wxMenuItem* appendCopy( const wxMenuItem* aSource );
200 
202  void setupEvents();
203 
205  void updateHotKeys();
206 
209  void runEventHandlers( const wxMenuEvent& aMenuEvent, OPT_TOOL_EVENT& aToolEvent );
210 
212  void runOnSubmenus( std::function<void(ACTION_MENU*)> aFunction );
213 
215  OPT_TOOL_EVENT findToolAction( int aId );
216 
219 
220  bool m_dirty; // Menu requires update before display
221 
224 
226  wxString m_title;
227 
230 
233 
236 
238  std::map<int, const TOOL_ACTION*> m_toolActions;
239 
241  std::list<ACTION_MENU*> m_submenus;
242 
243  friend class TOOL_INTERACTIVE;
244 };
245 
246 #endif
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon)
Adds a wxWidgets-style entry to the menu.
KIFACE_I is a KIFACE (I)mplementation, with some features useful for DSOs which implement a KIFACE.
Definition: kiface_i.h:37
virtual void update()
Update menu state stub.
Definition: action_menu.h:175
bool m_titleDisplayed
Definition: action_menu.h:222
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
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:43
const BITMAP_OPAQUE * m_icon
Optional icon
Definition: action_menu.h:229
int GetSelected() const
Returns the position of selected item.
Definition: action_menu.h:131
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()
Clears the dirty flag on the menu and all descendants.
void 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)
Sets a tool that is the creator of the menu.
virtual OPT_TOOL_EVENT eventHandler(const wxMenuEvent &)
Event handler stub.
Definition: action_menu.h:183
TOOL_MANAGER.
Definition: tool_manager.h:51
wxPoint m_forcedPosition
Definition: action_menu.h:218
void copyFrom(const ACTION_MENU &aMenu)
Copies another menus data to this instance.
bool m_isContextMenu
Definition: action_menu.h:223
wxString m_title
Menu title
Definition: action_menu.h:226
std::list< ACTION_MENU * > m_submenus
List of submenus.
Definition: action_menu.h:241
void SetIcon(const BITMAP_OPAQUE *aIcon)
Assigns an icon for the entry.
Definition: action_menu.cpp:71
ACTION_MENU & operator=(const ACTION_MENU &aMenu)=delete
int m_selected
Stores the id number of selected item.
Definition: action_menu.h:232
ACTION_MENU * Clone() const
Creates a deep, recursive copy of this ACTION_MENU.
TOOL_INTERACTIVE * m_tool
Creator of the menu
Definition: action_menu.h:235
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:238
void OnMenuEvent(wxMenuEvent &aEvent)
bool m_isForcedPosition
Definition: action_menu.h:217
static constexpr bool CHECK
Definition: action_menu.h:162
void SetTitle(const wxString &aTitle) override
Sets title for the menu.
Definition: action_menu.cpp:89
void AddQuitOrClose(KIFACE_I *aKiface, wxString aAppname="")
Adds either a standard Quit or Close item to the menu.
ACTION_MENU(bool isContextMenu, TOOL_INTERACTIVE *aTool=nullptr)
Default constructor
Definition: action_menu.cpp:46
TOOL_MANAGER * getToolManager() const
Returns an instance of TOOL_MANAGER class.
Represents a single user action.
Definition: tool_action.h:44
void setupEvents()
Initializes handlers for events.
Definition: action_menu.cpp:77
void Clear()
Removes all the entries from the menu (as well as its title).
bool HasEnabledItems() const
Returns true if the menu has any enabled items.
void OnIdle(wxIdleEvent &event)
void updateHotKeys()
Updates hot key settings for TOOL_ACTIONs in this menu.
~ACTION_MENU() override
Definition: action_menu.cpp:58
void SetDirty()
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:556
void AddClose(wxString aAppname="")
Add a standard close item to the menu with the accelerator key CTRL-W.
void DisplayTitle(bool aDisplay=true)
Decides whether a title for a pop up menu should be displayed.