KiCad PCB EDA Suite
Loading...
Searching...
No Matches
tool_action.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-2015 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 __TOOL_ACTION_H
29#define __TOOL_ACTION_H
30
31#include <string>
32#include <cassert>
33
34#include <wx/string.h>
35
36class TOOL_EVENT;
37
38enum class BITMAPS : unsigned int;
39
42{
46};
47
50{
53 AF_NOTIFY = 2
54};
55
68{
69public:
70 TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope = AS_CONTEXT,
71 int aDefaultHotKey = 0, const std::string& aLegacyHotKeyName = "",
72 const wxString& aMenuText = wxEmptyString,
73 const wxString& aTooltip = wxEmptyString,
74 BITMAPS aIcon = static_cast<BITMAPS>( 0 ), TOOL_ACTION_FLAGS aFlags = AF_NONE,
75 void* aParam = nullptr );
76
78
79 // TOOL_ACTIONS are singletons; don't be copying them around....
80 TOOL_ACTION( const TOOL_ACTION& ) = delete;
81 TOOL_ACTION& operator= ( const TOOL_ACTION& ) = delete;
82
83 bool operator==( const TOOL_ACTION& aRhs ) const
84 {
85 return m_id == aRhs.m_id;
86 }
87
88 bool operator!=( const TOOL_ACTION& aRhs ) const
89 {
90 return m_id != aRhs.m_id;
91 }
92
101 const std::string& GetName() const { return m_name; }
102
106 int GetDefaultHotKey() const { return m_defaultHotKey; }
107
111 int GetHotKey() const { return m_hotKey; }
112 void SetHotKey( int aKeycode );
113
121 int GetId() const { return m_id; }
122
123 /*
124 * Get the unique ID for this action in the user interface system.
125 *
126 * This is simply the action ID offset by @c ACTION_BASE_UI_ID.
127 *
128 * @return The unique ID number for use in the user interface system.
129 */
130 int GetUIId() const
131 {
132 // Hack for wxWidgets' use in stuff like search controls in standard file dialogs. If
133 // it doesn't find these specific IDs somewhere in the menus then it won't enable
134 // cut/copy/paste.
135 if( m_param == (void*) wxID_CUT )
136 return wxID_CUT;
137 else if( m_param == (void*) wxID_COPY )
138 return wxID_COPY;
139 else if( m_param == (void*) wxID_PASTE )
140 return wxID_PASTE;
141
142 return m_id + ACTION_BASE_UI_ID;
143 }
144
145 /*
146 * Get the base value used to offset the user interface IDs for the actions.
147 */
148 static int GetBaseUIId() { return ACTION_BASE_UI_ID; }
149
154 TOOL_EVENT MakeEvent() const;
155
156 wxString GetLabel() const;
157 wxString GetMenuItem() const;
158 wxString GetDescription( bool aIncludeHotkey = true ) const;
159
161
162 void* GetParam() const { return m_param; }
163
169 std::string GetToolName() const;
170
174 bool IsActivation() const
175 {
176 return m_flags & AF_ACTIVATE;
177 }
178
182 bool IsNotification() const
183 {
184 return m_flags & AF_NOTIFY;
185 }
186
193 {
194 return m_icon;
195 }
196
197protected:
198 TOOL_ACTION();
199
200 friend class ACTION_MANAGER;
201
203 static constexpr int ACTION_BASE_UI_ID = 20000;
204
206 std::string m_name;
208
209 const int m_defaultHotKey; // Default hot key
210 int m_hotKey; // The current hotkey (post-user-settings-application)
211 const std::string m_legacyName; // Name for reading legacy hotkey settings
212
213 wxString m_label;
214 wxString m_tooltip;
215 BITMAPS m_icon; // Icon for the menu entry
216
217 int m_id; // Unique ID for maps. Assigned by ACTION_MANAGER.
218
220 void* m_param; // Generic parameter
221};
222
223#endif
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
Manage TOOL_ACTION objects.
Represent a single user action.
Definition: tool_action.h:68
TOOL_ACTION & operator=(const TOOL_ACTION &)=delete
static int GetBaseUIId()
Definition: tool_action.h:148
BITMAPS GetIcon() const
Return an icon associated with the action.
Definition: tool_action.h:192
TOOL_ACTION_SCOPE m_scope
Definition: tool_action.h:207
bool operator!=(const TOOL_ACTION &aRhs) const
Definition: tool_action.h:88
bool IsActivation() const
Return true if the action is intended to activate a tool.
Definition: tool_action.h:174
bool operator==(const TOOL_ACTION &aRhs) const
Definition: tool_action.h:83
void SetHotKey(int aKeycode)
wxString GetLabel() const
Definition: tool_action.cpp:83
TOOL_ACTION_FLAGS m_flags
Definition: tool_action.h:219
BITMAPS m_icon
Definition: tool_action.h:215
TOOL_ACTION_SCOPE GetScope() const
Definition: tool_action.h:160
std::string GetToolName() const
Return name of the tool associated with the action.
wxString m_label
Definition: tool_action.h:213
void * GetParam() const
Definition: tool_action.h:162
wxString GetDescription(bool aIncludeHotkey=true) const
Definition: tool_action.cpp:97
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:121
bool IsNotification() const
Return true if the action is a notification.
Definition: tool_action.h:182
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:101
wxString m_tooltip
Definition: tool_action.h:214
TOOL_ACTION(const TOOL_ACTION &)=delete
wxString GetMenuItem() const
Definition: tool_action.cpp:89
int GetHotKey() const
Return the hotkey keycode which initiates the action.
Definition: tool_action.h:111
const int m_defaultHotKey
Definition: tool_action.h:209
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
Definition: tool_action.cpp:72
static constexpr int ACTION_BASE_UI_ID
Name of the action (convention is "app.tool.actionName")
Definition: tool_action.h:203
int GetDefaultHotKey() const
Return the default hotkey (if any) for the action.
Definition: tool_action.h:106
const std::string m_legacyName
Definition: tool_action.h:211
std::string m_name
Definition: tool_action.h:206
int GetUIId() const
Definition: tool_action.h:130
void * m_param
Definition: tool_action.h:220
Generic, UI-independent tool event.
Definition: tool_event.h:156
TOOL_ACTION_SCOPE
Scope of tool actions.
Definition: tool_action.h:42
@ AS_GLOBAL
Global action (toolbar/main menu event, global shortcut)
Definition: tool_action.h:45
@ AS_ACTIVE
All active tools.
Definition: tool_action.h:44
@ AS_CONTEXT
Action belongs to a particular tool (i.e. a part of a pop-up menu)
Definition: tool_action.h:43
TOOL_ACTION_FLAGS
Flags for tool actions.
Definition: tool_action.h:50
@ AF_ACTIVATE
Action activates a tool.
Definition: tool_action.h:52
@ AF_NOTIFY
Action is a notification (it is by default passed to all tools)
Definition: tool_action.h:53
@ AF_NONE
Definition: tool_action.h:51