KiCad PCB EDA Suite
Loading...
Searching...
No Matches
action_toolbar.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <map>
23#include <memory>
24#include <vector>
25#include <wx/bitmap.h> // Needed for the auibar include
26#include <wx/aui/auibar.h>
27#include <wx/aui/framemanager.h>
28#include <wx/popupwin.h>
29#include <wx/panel.h>
30#include <tool/action_manager.h>
31#include <frame_type.h>
32
33class ACTION_MENU;
34class BITMAP_BUTTON;
35class EDA_BASE_FRAME;
36class TOOL_ACTION;
37class TOOL_MANAGER;
39
44{
45public:
46 // Make the toolbar a friend so it can easily access everything inside here
47 friend class ACTION_TOOLBAR;
48
49 ACTION_GROUP( const std::string_view& aName );
50
51 ACTION_GROUP( const std::string_view& aName, const std::vector<const TOOL_ACTION*>& aActions );
52
61 void SetDefaultAction( const TOOL_ACTION& aDefault );
62
66 const TOOL_ACTION* GetDefaultAction() const { return m_defaultAction; }
67
71 std::string GetName() const { return m_name; }
72
76 int GetUIId() const;
77
85 void SetActions( const std::vector<const TOOL_ACTION*>& aActions );
86
90 const std::vector<const TOOL_ACTION*>& GetActions() const { return m_actions; }
91
92protected:
94 int m_id;
95
97 std::string m_name;
98
101
103 std::vector<const TOOL_ACTION*> m_actions;
104};
105
106
110class ACTION_TOOLBAR_PALETTE : public wxPopupTransientWindow
111{
112public:
120 ACTION_TOOLBAR_PALETTE( wxWindow* aParent, bool aVertical );
121
127 void AddAction( const TOOL_ACTION& aAction );
128
135 void EnableAction( const TOOL_ACTION& aAction, bool aEnable = true );
136
143 void CheckAction( const TOOL_ACTION& aAction, bool aCheck = true );
144
152 void SetButtonSize( wxRect& aSize ) { m_buttonSize = aSize; }
153
159 void Popup( wxWindow* aFocus = nullptr ) override;
160
161 wxSize GetFittedSize();
162
166 void SetGroup( ACTION_GROUP* aGroup ) { m_group = aGroup; }
168
169#ifdef __WXMSW__
170 bool MSWHandleMessage( WXLRESULT* aResult, WXUINT aMessage, WXWPARAM aWParam,
171 WXLPARAM aLParam ) override;
172#endif
173
174protected:
175 void onCharHook( wxKeyEvent& aEvent );
176
177 // The group that the buttons in the palette are part of
179
182
185
186 wxPanel* m_panel;
187 wxBoxSizer* m_mainSizer;
188 wxBoxSizer* m_buttonSizer;
189
191 std::map<int, BITMAP_BUTTON*> m_buttons;
192};
193
194// Forward declare this because the toolbar wants it
196
200class ACTION_TOOLBAR : public wxAuiToolBar
201{
202public:
203 ACTION_TOOLBAR( EDA_BASE_FRAME* parent, wxWindowID id = wxID_ANY,
204 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
205 long style = wxAUI_TB_DEFAULT_STYLE );
206
207 virtual ~ACTION_TOOLBAR();
208
214 void SetAuiManager( wxAuiManager* aManager ) { m_auiManager = aManager; }
215
216 void SetToolManager( TOOL_MANAGER* aManager ) { m_toolManager = aManager; }
217
227 void Add( const TOOL_ACTION& aAction );
228
238 void Add( const TOOL_ACTION& aAction, bool aIsToggleEntry,
239 bool aIsCancellable = false );
240
246 void AddButton( const TOOL_ACTION& aAction );
247
254 void AddScaledSeparator( wxWindow* aWindow );
255
259 void Add( wxControl* aControl, const wxString& aLabel = wxEmptyString );
260
270 void AddToolContextMenu( const TOOL_ACTION& aAction, std::unique_ptr<ACTION_MENU> aMenu );
271
282 void AddGroup( std::unique_ptr<ACTION_GROUP> aGroup );
283
290 void SelectAction( ACTION_GROUP* aGroup, const TOOL_ACTION& aAction );
291
296 void SelectAction( const TOOL_ACTION& aAction );
297
304 void ApplyConfiguration( const TOOLBAR_CONFIGURATION& aConfig );
305
309 void UpdateControlWidths();
310
316 void UpdateControlWidth( int aID );
317
321 void ClearToolbar();
322
328 void SetToolBitmap( const TOOL_ACTION& aAction, const wxBitmapBundle& aBitmap );
329
335 void Toggle( const TOOL_ACTION& aAction, bool aState );
336
337 void Toggle( const TOOL_ACTION& aAction, bool aEnabled, bool aChecked );
338
347 bool KiRealize();
348
352 void RefreshBitmaps();
353
357 static std::list<ACTION_TOOLBAR_CONTROL*>& GetAllCustomControls()
358 {
359 static std::list<ACTION_TOOLBAR_CONTROL*> m_controls;
360 return m_controls;
361 }
362
366 static std::list<ACTION_TOOLBAR_CONTROL*> GetCustomControlList( FRAME_T aContext );
367
368 static constexpr bool TOGGLE = true;
369 static constexpr bool CANCEL = true;
370
371protected:
377 void doSelectAction( ACTION_GROUP* aGroup, const TOOL_ACTION& aAction );
378
383 void popupPalette( wxAuiToolBarItem* aItem );
384
386 void onMouseClick( wxMouseEvent& aEvent );
387
389 void onItemDrag( wxAuiToolBarEvent& aEvent );
390
392 void onToolEvent( wxAuiToolBarEvent& aEvent );
393
395 void onRightClick( wxAuiToolBarEvent& aEvent );
396
399 void onRightUp( wxMouseEvent& aEvent );
400
402 void showContextMenu( int aToolId );
403
405 void onPaletteEvent( wxCommandEvent& aEvent );
406
408 void onTimerDone( wxTimerEvent& aEvent );
409
410 void onThemeChanged( wxSysColourChangedEvent &aEvent );
411
414 void OnCustomRender( wxDC& aDc, const wxAuiToolBarItem& aItem, const wxRect& aRect ) override;
415
416 void DoSetToolTipText( const wxString& aTip ) override;
417
418protected:
419 // Timer used to determine when the palette should be opened after a group item is pressed
422
423 wxAuiManager* m_auiManager;
426
427 std::map<int, bool> m_toolKinds;
428 std::map<int, bool> m_toolCancellable;
429 std::map<int, const TOOL_ACTION*> m_toolActions;
430
432 std::vector<int> m_controlIDs;
433
434 std::map<int, std::unique_ptr<ACTION_GROUP>> m_actionGroups;
435 std::map<int, std::unique_ptr<ACTION_MENU>> m_toolMenus;
436
438 std::map<std::string, std::string> m_groupSelections;
439};
440
448typedef std::function<void ( ACTION_TOOLBAR* )> ACTION_TOOLBAR_CONTROL_FACTORY;
449
450
455{
456public:
457 ACTION_TOOLBAR_CONTROL( const std::string& aName, const wxString& aUiName,
458 const wxString& aDescription, std::vector<FRAME_T> aSupportedContexts ) :
459 m_name( aName ),
460 m_uiname( aUiName ),
461 m_description( aDescription ),
462 m_supportedContexts( aSupportedContexts )
463 {
464 wxASSERT_MSG( aName.starts_with( "control" ),
465 wxString::Format( "Control name \"%s\" must start with \"control\"", aName ) );
466
467 ACTION_TOOLBAR::GetAllCustomControls().push_back( this );
468 }
469
470 const std::string& GetName() const { return m_name; }
471 const wxString& GetUiName() const { return m_uiname; }
472 const wxString& GetDescription() const { return m_description; }
473
474 bool SupportedFor( FRAME_T aFrame ) const
475 {
476 for( FRAME_T candidate : m_supportedContexts )
477 {
478 if( aFrame == candidate )
479 return true;
480 }
481
482 return false;
483 }
484
485protected:
489 std::string m_name;
490
494 wxString m_uiname;
495
500
504 std::vector<FRAME_T> m_supportedContexts;
505};
506
A group of actions that will be displayed together on a toolbar palette.
void SetDefaultAction(const TOOL_ACTION &aDefault)
Set the default action to use when first creating the toolbar palette icon.
friend class ACTION_TOOLBAR
ACTION_GROUP(const std::string_view &aName)
const std::vector< const TOOL_ACTION * > & GetActions() const
Get a vector of all the actions contained inside this group.
std::vector< const TOOL_ACTION * > m_actions
void SetActions(const std::vector< const TOOL_ACTION * > &aActions)
Set the actions contained in this group.
int GetUIId() const
Get the ID used in the UI to reference this group.
int m_id
< The action ID for this action group
const TOOL_ACTION * m_defaultAction
The actions that compose the group. Non-owning.
const TOOL_ACTION * GetDefaultAction() const
Get the default action to use when first creating this group's toolbar palette icon.
std::string m_name
The default action to display on the toolbar item.
std::string GetName() const
Get the name of the group.
Define the structure of a menu based on ACTIONs.
Definition action_menu.h:43
static ACTION_TOOLBAR_CONTROL gridSelect
static ACTION_TOOLBAR_CONTROL overrideLocks
static ACTION_TOOLBAR_CONTROL layerSelector
static ACTION_TOOLBAR_CONTROL zoomSelect
static ACTION_TOOLBAR_CONTROL unitSelector
static ACTION_TOOLBAR_CONTROL ipcScripting
static ACTION_TOOLBAR_CONTROL bodyStyleSelector
Class to hold basic information about controls that can be added to the toolbars.
wxString m_uiname
Short description to show for the control.
ACTION_TOOLBAR_CONTROL(const std::string &aName, const wxString &aUiName, const wxString &aDescription, std::vector< FRAME_T > aSupportedContexts)
std::string m_name
Name of the control - must start with "control.".
const wxString & GetDescription() const
const wxString & GetUiName() const
bool SupportedFor(FRAME_T aFrame) const
std::vector< FRAME_T > m_supportedContexts
List of frame types that support the control.
const std::string & GetName() const
wxString m_description
User-visible tooltip for the control.
A popup window that contains a row of toolbar-like buttons for the user to choose from.
void CheckAction(const TOOL_ACTION &aAction, bool aCheck=true)
Check/Toggle the button for an action on the palette.
void onCharHook(wxKeyEvent &aEvent)
wxBoxSizer * m_buttonSizer
The buttons that act as the toolbar on the palette.
void AddAction(const TOOL_ACTION &aAction)
Add an action to the palette.
ACTION_TOOLBAR_PALETTE(wxWindow *aParent, bool aVertical)
Create the palette.
void SetButtonSize(wxRect &aSize)
Set the size all the buttons on this palette should be.
void SetGroup(ACTION_GROUP *aGroup)
Set the action group that this palette contains the actions for.
wxRect m_buttonSize
True if the palette uses vertical buttons, false for horizontal buttons.
void EnableAction(const TOOL_ACTION &aAction, bool aEnable=true)
Enable the button for an action on the palette.
ACTION_GROUP * m_group
The size each button on the toolbar should be.
std::map< int, BITMAP_BUTTON * > m_buttons
ACTION_GROUP * GetGroup()
void Popup(wxWindow *aFocus=nullptr) override
Popup this window.
Define the structure of a toolbar with buttons that invoke ACTIONs.
void RefreshBitmaps()
Reload all the bitmaps for the tools (e.g.
void SetToolBitmap(const TOOL_ACTION &aAction, const wxBitmapBundle &aBitmap)
Updates the bitmap of a particular tool.
void OnCustomRender(wxDC &aDc, const wxAuiToolBarItem &aItem, const wxRect &aRect) override
static constexpr bool TOGGLE
void onTimerDone(wxTimerEvent &aEvent)
void doSelectAction(ACTION_GROUP *aGroup, const TOOL_ACTION &aAction)
Update a group toolbar item to look like a specific action.
static constexpr bool CANCEL
void SetAuiManager(wxAuiManager *aManager)
Set the AUI manager that this toolbar belongs to.
void onMouseClick(wxMouseEvent &aEvent)
Handler for when a drag event occurs on an item.
void AddButton(const TOOL_ACTION &aAction)
Add a large button such as used in the KiCad Manager Frame's launch bar.
wxAuiManager * m_auiManager
void UpdateControlWidth(int aID)
Update the toolbar item width of a control using its best size.
ACTION_TOOLBAR(EDA_BASE_FRAME *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxAUI_TB_DEFAULT_STYLE)
void Toggle(const TOOL_ACTION &aAction, bool aState)
Apply the default toggle action.
void SelectAction(ACTION_GROUP *aGroup, const TOOL_ACTION &aAction)
Select an action inside a group.
void Add(const TOOL_ACTION &aAction)
Add a TOOL_ACTION-based button to the toolbar.
static std::list< ACTION_TOOLBAR_CONTROL * > & GetAllCustomControls()
Get the list of custom controls that could be used on toolbars.
void onPaletteEvent(wxCommandEvent &aEvent)
Handle the palette timer triggering.
void onRightClick(wxAuiToolBarEvent &aEvent)
Handle a right mouse button release; resolves the tool ourselves to work around a wxAuiToolBar hit-te...
wxTimer * m_paletteTimer
std::map< int, std::unique_ptr< ACTION_MENU > > m_toolMenus
void onToolEvent(wxAuiToolBarEvent &aEvent)
Handle a right-click on a menu item.
void onItemDrag(wxAuiToolBarEvent &aEvent)
The default tool event handler.
std::map< int, bool > m_toolKinds
static std::list< ACTION_TOOLBAR_CONTROL * > GetCustomControlList(FRAME_T aContext)
Get the list of custom controls that could be used on a particular frame type.
std::map< int, std::unique_ptr< ACTION_GROUP > > m_actionGroups
void AddGroup(std::unique_ptr< ACTION_GROUP > aGroup)
Add a set of actions to a toolbar as a group.
void AddToolContextMenu(const TOOL_ACTION &aAction, std::unique_ptr< ACTION_MENU > aMenu)
Add a context menu to a specific tool item on the toolbar.
void DoSetToolTipText(const wxString &aTip) override
std::map< int, bool > m_toolCancellable
void AddScaledSeparator(wxWindow *aWindow)
Add a separator that introduces space on either side to not squash the tools when scaled.
bool KiRealize()
Use this over Realize() to avoid a rendering glitch with fixed orientation toolbars.
void popupPalette(wxAuiToolBarItem *aItem)
Popup the ACTION_TOOLBAR_PALETTE associated with the ACTION_GROUP of the given toolbar item.
virtual ~ACTION_TOOLBAR()
ACTION_TOOLBAR_PALETTE * m_palette
std::vector< int > m_controlIDs
IDs for all the control items in this toolbar.
void onThemeChanged(wxSysColourChangedEvent &aEvent)
Render the triangle in the lower-right corner that represents that an action palette is available for...
EDA_BASE_FRAME * m_parent
void SetToolManager(TOOL_MANAGER *aManager)
std::map< int, const TOOL_ACTION * > m_toolActions
std::map< std::string, std::string > m_groupSelections
Selected action per group name.
void ClearToolbar()
Clear the toolbar and remove all associated menus.
void onRightUp(wxMouseEvent &aEvent)
Show the context menu registered for the given tool ID (handles group remapping).
void showContextMenu(int aToolId)
Handle the button select inside the palette.
void UpdateControlWidths()
Update the width of all wxControl tools on thsi toolbar.
void ApplyConfiguration(const TOOLBAR_CONFIGURATION &aConfig)
Replace the contents of this toolbar with the configuration given in aConfig.
TOOL_MANAGER * m_toolManager
A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn.
The base frame for deriving all KiCad main window classes.
Represent a single user action.
Master controller class:
std::function< void(ACTION_TOOLBAR *)> ACTION_TOOLBAR_CONTROL_FACTORY
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition frame_type.h:29