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  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Tomasz Wlostowski <[email protected]>
8  * @author Maciej Suminski <[email protected]>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 #ifndef __CONTEXT_MENU_H
29 #define __CONTEXT_MENU_H
30 
31 #include <map>
32 #include <list>
33 #include <functional>
34 
35 #include <wx/menu.h>
36 #include <wx/textentry.h>
37 #include <tool/tool_event.h>
38 
39 class KIFACE_BASE;
40 class TOOL_INTERACTIVE;
41 class TOOL_MANAGER;
42 
43 enum class BITMAPS : unsigned int;
44 
48 class ACTION_MENU : public wxMenu
49 {
50 public:
52  ACTION_MENU( bool isContextMenu, TOOL_INTERACTIVE* aTool = nullptr );
53 
54  ~ACTION_MENU() override;
55 
56  ACTION_MENU( const ACTION_MENU& aMenu ) = delete;
57  ACTION_MENU& operator=( const ACTION_MENU& aMenu ) = delete;
58 
65  void SetTitle( const wxString& aTitle ) override;
66 
70  void DisplayTitle( bool aDisplay = true );
71 
77  void SetIcon( BITMAPS aIcon );
78 
84  wxMenuItem* Add( const wxString& aLabel, int aId, BITMAPS aIcon );
85  wxMenuItem* Add( const wxString& aLabel, const wxString& aToolTip, int aId,
86  BITMAPS aIcon, bool aIsCheckmarkEntry = false );
87 
98  wxMenuItem* Add( const TOOL_ACTION& aAction, bool aIsCheckmarkEntry = false,
99  const wxString& aOverrideLabel = wxEmptyString );
100 
110  wxMenuItem* Add( ACTION_MENU* aMenu );
111 
119  void AddClose( const wxString& aAppname = "" );
120 
129  void AddQuitOrClose( KIFACE_BASE* aKiface, wxString aAppname = "" );
130 
136  void Clear();
137 
141  bool HasEnabledItems() const;
142 
150  inline int GetSelected() const
151  {
152  return m_selected;
153  }
154 
158  void UpdateAll();
159 
163  virtual void UpdateTitle() {}
164 
168  void ClearDirty();
169  void SetDirty();
170 
176  void SetTool( TOOL_INTERACTIVE* aTool );
177 
181  ACTION_MENU* Clone() const;
182 
183  void OnMenuEvent( wxMenuEvent& aEvent );
184  void OnIdle( wxIdleEvent& event );
185 
186  virtual bool PassHelpTextToHandler() { return false; }
187 
188  static constexpr bool NORMAL = false;
189  static constexpr bool CHECK = true;
190 
191 protected:
193  virtual ACTION_MENU* create() const;
194 
196  TOOL_MANAGER* getToolManager() const;
197 
204  virtual void update()
205  {
206  }
207 
214  virtual OPT_TOOL_EVENT eventHandler( const wxMenuEvent& )
215  {
216  return OPT_TOOL_EVENT();
217  }
218 
224  void copyFrom( const ACTION_MENU& aMenu );
225 
226 protected:
230  wxMenuItem* appendCopy( const wxMenuItem* aSource );
231 
233  void setupEvents();
234 
236  void updateHotKeys();
237 
240  void runEventHandlers( const wxMenuEvent& aMenuEvent, OPT_TOOL_EVENT& aToolEvent );
241 
243  void runOnSubmenus( std::function<void(ACTION_MENU*)> aFunction );
244 
246  OPT_TOOL_EVENT findToolAction( int aId );
247 
250 
251  bool m_dirty; // Menu requires update before display
252 
255 
257  wxString m_title;
258 
261 
264 
267 
269  std::map<int, const TOOL_ACTION*> m_toolActions;
270 
272  std::list<ACTION_MENU*> m_submenus;
273 
274  friend class TOOL_INTERACTIVE;
275 };
276 
277 #endif
virtual void UpdateTitle()
Used by some menus to just-in-time translate their titles.
Definition: action_menu.h:163
virtual void update()
Update menu state stub.
Definition: action_menu.h:204
bool m_titleDisplayed
Definition: action_menu.h:253
OPT_TOOL_EVENT findToolAction(int aId)
A KIFACE implementation.
Definition: kiface_base.h:37
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:48
void SetIcon(BITMAPS aIcon)
Assign an icon for the entry.
Definition: action_menu.cpp:73
int GetSelected() const
Return the position of selected item.
Definition: action_menu.h:150
void runEventHandlers(const wxMenuEvent &aMenuEvent, OPT_TOOL_EVENT &aToolEvent)
Run a function on the menu and all its submenus.
void ClearDirty()
Clear the dirty flag on the menu and all descendants.
void UpdateAll()
Run update handlers for the menu and its submenus.
BITMAPS m_icon
Stores the id number of selected item.
Definition: action_menu.h:260
virtual ACTION_MENU * create() const
< Return an instance of this class. It has to be overridden in inheriting classes.
void SetTool(TOOL_INTERACTIVE *aTool)
Set a tool that is the creator of the menu.
virtual OPT_TOOL_EVENT eventHandler(const wxMenuEvent &)
Event handler stub.
Definition: action_menu.h:214
virtual bool PassHelpTextToHandler()
Definition: action_menu.h:186
Master controller class:
Definition: tool_manager.h:54
wxPoint m_forcedPosition
Definition: action_menu.h:249
void copyFrom(const ACTION_MENU &aMenu)
Copy another menus data to this instance.
bool m_isContextMenu
Menu title.
Definition: action_menu.h:254
Use all material properties from model file.
wxString m_title
Optional icon.
Definition: action_menu.h:257
std::list< ACTION_MENU * > m_submenus
Definition: action_menu.h:272
ACTION_MENU & operator=(const ACTION_MENU &aMenu)=delete
int m_selected
Creator of the menu.
Definition: action_menu.h:263
ACTION_MENU * Clone() const
Create a deep, recursive copy of this ACTION_MENU.
TOOL_INTERACTIVE * m_tool
Associates tool actions with menu item IDs. Non-owning.
Definition: action_menu.h:266
wxMenuItem * appendCopy(const wxMenuItem *aSource)
Append a copy of wxMenuItem.
void runOnSubmenus(std::function< void(ACTION_MENU *)> aFunction)
Check if any of submenus contains a TOOL_ACTION with a specific ID.
std::map< int, const TOOL_ACTION * > m_toolActions
List of submenus.
Definition: action_menu.h:269
void OnMenuEvent(wxMenuEvent &aEvent)
bool m_isForcedPosition
Definition: action_menu.h:248
void SetTitle(const wxString &aTitle) override
Set title for the menu.
Definition: action_menu.cpp:87
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
ACTION_MENU(bool isContextMenu, TOOL_INTERACTIVE *aTool=nullptr)
< Default constructor
Definition: action_menu.cpp:47
TOOL_MANAGER * getToolManager() const
Represent a single user action.
Definition: tool_action.h:67
void setupEvents()
Update hot key settings for TOOL_ACTIONs in this menu.
Definition: action_menu.cpp:79
wxMenuItem * Add(const wxString &aLabel, int aId, BITMAPS aIcon)
Add a wxWidgets-style entry to the menu.
void Clear()
Remove 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)
#define CHECK(x)
void AddQuitOrClose(KIFACE_BASE *aKiface, wxString aAppname="")
Add either a standard Quit or Close item to the menu.
void updateHotKeys()
Traverse the submenus tree looking for a submenu capable of handling a particular menu event.
~ACTION_MENU() override
Definition: action_menu.cpp:60
void SetDirty()
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:548
void AddClose(const wxString &aAppname="")
Add a standard close item to the menu with the accelerator key CTRL-W.
void DisplayTitle(bool aDisplay=true)
Decide whether a title for a pop up menu should be displayed.
Definition: action_menu.cpp:98